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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 strategy values:
  41. * "HEURISTIC": uses heuristic to choose the fastest algorithm.
  42. * "PROFILE": runs possible algorithms on a real device to find the best one.
  43. * "REPRODUCIBLE": uses algorithms that are reproducible.
  44. * "OPTIMIZED": uses algorithms that are optimized.
  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. if isinstance(option, Strategy):
  58. _config._benchmark_kernel = (
  59. True if option & _valid_string_option["PROFILE"] != Strategy(0) else False
  60. )
  61. _config._deterministic_kernel = (
  62. True
  63. if option & _valid_string_option["REPRODUCIBLE"] != Strategy(0)
  64. else False
  65. )
  66. return
  67. assert isinstance(option, str)
  68. _config._benchmark_kernel = False
  69. _config._deterministic_kernel = False
  70. for opt in option.split("_"):
  71. if not opt in _valid_string_option:
  72. raise ValueError(
  73. "Valid option can only be one of {}, or combine them with '_'.".format(
  74. _valid_string_option.keys()
  75. )
  76. )
  77. _config._benchmark_kernel |= _valid_string_option[opt] == Strategy.PROFILE
  78. _config._deterministic_kernel |= (
  79. _valid_string_option[opt] == Strategy.REPRODUCIBLE
  80. )
  81. @deprecated(version="1.3", reason="use get_execution_strategy() instead")
  82. def get_conv_execution_strategy() -> str:
  83. return get_execution_strategy()
  84. @deprecated(version="1.3", reason="use set_execution_strategy() instead")
  85. def set_conv_execution_strategy(option: str):
  86. return set_execution_strategy(option)
  87. set_execution_strategy(os.getenv("MEGENGINE_EXECUTION_STRATEGY", "HEURISTIC"))