You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

compose_proposer.py 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """The Compose Proposals."""
  16. from collections import OrderedDict
  17. from mindinsight.profiler.common.log import logger
  18. from mindinsight.profiler.common.util import get_options
  19. from mindinsight.profiler.proposer.proposer_factory import ProposerFactory
  20. class ComposeProposal:
  21. """Get the proposals from multiple different proposers."""
  22. def __init__(self, profiling_path, device_id, type_list=None):
  23. self.profiling_path = profiling_path
  24. self.device_id = device_id
  25. self.compose_proposer_type_list = type_list
  26. # Postfix of category label, used for UI to identify the label as category label.
  27. self.type_label_postfix = "-proposer_type_label"
  28. def get_proposal(self, options=None):
  29. """
  30. Get compose proposals.
  31. Args:
  32. options (dict): options for composed proposal.
  33. - compose_proposal_result: execution results of the already running proposers.
  34. - step_trace: include optional parameters for step trace,The dictionary key is iter_interval
  35. used to get the analyser options for iteration interval time.
  36. Returns:
  37. dict, the proposals from multiple different proposers.
  38. Examples:
  39. >>> type_list = ['common', 'step_trace']
  40. >>> condition = {"filter_condition": {'mode': "proc", "proc_name": "iteration_interval"}}
  41. >>> options = {'step_trace': {"iter_interval": condition}}
  42. >>> cp = ComposeProposal(self.profiling_dir, self.device_id, type_list)
  43. >>> result_proposal = cp.get_proposal(options=options)
  44. """
  45. logger.info("The ComposeProposal is running")
  46. options = get_options(options)
  47. logger.debug("The 'options' is %s", str(options))
  48. # The flag whether to write category label.
  49. type_label_flag = options.get("type_label_flag", True)
  50. compose_proposal_result = OrderedDict()
  51. logger.debug("The 'compose_proposer_type_list' is %s", str(self.compose_proposer_type_list))
  52. for proposer_type in self.compose_proposer_type_list:
  53. proposer = ProposerFactory.instance().get_proposer(proposer_type, self.profiling_path, self.device_id)
  54. if proposer is None:
  55. continue
  56. # Write the result of proposals to option for other proposer to get.
  57. options["compose_proposal_result"] = compose_proposal_result
  58. result = proposer.analyze(options)
  59. # Insert category label.
  60. if result and type_label_flag:
  61. proposer_type_label = proposer_type + "-type_label"
  62. # Get the name of the category label, the default is the same as the proposer type.
  63. type_label_name = options.get(proposer_type_label, proposer_type)
  64. # Add postfix to category label name
  65. type_proposal_label = type_label_name + self.type_label_postfix
  66. compose_proposal_result[type_proposal_label] = None
  67. # Merge results to the proposals dictionary.
  68. compose_proposal_result.update(result)
  69. elif result and not type_label_flag:
  70. # Merge results to the proposals dictionary.
  71. compose_proposal_result.update(result)
  72. logger.debug("The 'compose_proposal_result' is %s", str(compose_proposal_result))
  73. return compose_proposal_result