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.

debug_param.py 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. import os
  3. from ..core import _config
  4. from ..core._imperative_rt.core2 import _clear_algorithm_cache
  5. from ..core.ops import builtin
  6. from ..logger import get_logger
  7. from ..utils.deprecation import deprecated
  8. Strategy = builtin.ops.Convolution.Strategy
  9. if os.getenv("MEGENGINE_CONV_EXECUTION_STRATEGY") != None:
  10. get_logger().warning(
  11. "Environment variable `MEGENGINE_CONV_EXECUTION_STRATEGY` is deprecated, please use `MEGENGINE_EXECUTION_STRATEGY`"
  12. )
  13. _valid_string_option = {
  14. "REPRODUCIBLE": Strategy.REPRODUCIBLE,
  15. "HEURISTIC": Strategy.HEURISTIC,
  16. "PROFILE": Strategy.PROFILE,
  17. }
  18. @deprecated(
  19. version="1.10",
  20. reason="use ``megengine.config.benchmark_kernel`` and ``megengine.config.deterministic_kernel`` instead",
  21. )
  22. def get_execution_strategy() -> Strategy:
  23. r"""Returns the execution strategy of :class:`~.module.Conv2d` and :func:`~.matmul`
  24. See :func:`~.set_execution_strategy` for possible return values
  25. """
  26. strategy = Strategy(0)
  27. if _config._benchmark_kernel:
  28. strategy |= Strategy.PROFILE
  29. else:
  30. strategy |= Strategy.HEURISTIC
  31. if _config._deterministic_kernel:
  32. strategy |= Strategy.REPRODUCIBLE
  33. return strategy
  34. @deprecated(
  35. version="1.10",
  36. reason="use ``megengine.config.benchmark_kernel`` and ``megengine.config.deterministic_kernel`` instead",
  37. )
  38. def set_execution_strategy(option):
  39. r"""Sets the execution strategy of :class:`~.module.Conv2d` and :func:`~.matmul`
  40. Args:
  41. option: Decides how :class:`~.module.Conv2d` and :func:`~.matmul` algorithms are chosen.
  42. Available strategy values:
  43. * "HEURISTIC": uses heuristic to choose the fastest algorithm.
  44. * "PROFILE": runs possible algorithms on a real device to find the best one.
  45. * "REPRODUCIBLE": uses algorithms that are reproducible.
  46. The default strategy is "HEURISTIC", these options can be combined to
  47. form a combination option, e.g. PROFILE_REPRODUCIBLE is a combination
  48. of "PROFILE" and "REPRODUCIBLE", which means using the fastest profiling
  49. result that is also reproducible.
  50. Available values string:
  51. * "HEURISTIC" uses heuristic to choose the fastest algorithm.
  52. * "PROFILE" runs possible algorithms on a real device to find the best one.
  53. * "PROFILE_REPRODUCIBLE" uses the fastest profiling result that is also reproducible.
  54. * "HEURISTIC_REPRODUCIBLE" uses heuristic to choose the fastest algorithm that is also reproducible.
  55. The default strategy is "HEURISTIC".
  56. It can also be set through the environment variable ``MEGENGINE_EXECUTION_STRATEGY``.
  57. """
  58. _benchmark_kernel = False
  59. _deterministic_kernel = False
  60. if isinstance(option, Strategy):
  61. _benchmark_kernel = (
  62. True if option & _valid_string_option["PROFILE"] != Strategy(0) else False
  63. )
  64. _deterministic_kernel = (
  65. True
  66. if option & _valid_string_option["REPRODUCIBLE"] != Strategy(0)
  67. else False
  68. )
  69. if _benchmark_kernel != _config._benchmark_kernel:
  70. _clear_algorithm_cache()
  71. _config._benchmark_kernel = _benchmark_kernel
  72. _config._deterministic_kernel = _deterministic_kernel
  73. return
  74. assert isinstance(option, str)
  75. for opt in option.split("_"):
  76. if not opt in _valid_string_option:
  77. raise ValueError(
  78. "Valid option can only be one of {}, or combine them with '_'.".format(
  79. _valid_string_option.keys()
  80. )
  81. )
  82. _benchmark_kernel |= _valid_string_option[opt] == Strategy.PROFILE
  83. _deterministic_kernel |= _valid_string_option[opt] == Strategy.REPRODUCIBLE
  84. if _benchmark_kernel != _config._benchmark_kernel:
  85. _clear_algorithm_cache()
  86. _config._benchmark_kernel = _benchmark_kernel
  87. _config._deterministic_kernel = _deterministic_kernel
  88. @deprecated(version="1.3", reason="use get_execution_strategy() instead")
  89. def get_conv_execution_strategy() -> str:
  90. return get_execution_strategy()
  91. @deprecated(version="1.3", reason="use set_execution_strategy() instead")
  92. def set_conv_execution_strategy(option: str):
  93. return set_execution_strategy(option)