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

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