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.

proposer_factory.py 2.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 proposer factory."""
  16. import threading
  17. import mindinsight.profiler.proposer.allproposers as proposer_module
  18. from mindinsight.profiler.common.log import logger
  19. class ProposerFactory:
  20. """The Proposer factory is used to create Proposer special instance."""
  21. _lock = threading.Lock()
  22. _instance = None
  23. def __new__(cls, *args, **kwargs):
  24. if cls._instance is None:
  25. with cls._lock:
  26. if cls._instance is None:
  27. cls._instance = super().__new__(cls, *args, **kwargs)
  28. return cls._instance
  29. @classmethod
  30. def instance(cls):
  31. """The factory instance."""
  32. if cls._instance is None:
  33. cls._instance = cls()
  34. return cls._instance
  35. def get_proposer(self, proposer_type, *args):
  36. """
  37. Get the specified proposer according to the proposer type.
  38. Args:
  39. proposer_type (str): The proposer type.
  40. args (list): The parameters required for the specific proposer class.
  41. Returns:
  42. Proposer, the specified proposer instance.
  43. Examples:
  44. >>> proposer_type = 'step_trace'
  45. >>> proposer = ProposerFactory.instance().get_proposer(proposer_type, self.profiling_dir, self.device_id)
  46. """
  47. logger.debug("The 'proposer_type' is %s,The 'args' is %s", proposer_type, str(args))
  48. proposer_instance = None
  49. sub_name = proposer_type.split('_')
  50. proposer_class_name = ''.join([name.capitalize() for name in sub_name])
  51. proposer_class_name += 'Proposer'
  52. if hasattr(proposer_module, proposer_class_name):
  53. proposer_instance = getattr(proposer_module, proposer_class_name)(*args)
  54. else:
  55. logger.warning("The proposer class %s does not exist.", proposer_class_name)
  56. return proposer_instance