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.0 kB

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