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.

_config.py 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # -*- coding: utf-8 -*-
  2. import os
  3. from contextlib import contextmanager
  4. from ._imperative_rt.core2 import _clear_algorithm_cache, get_option, set_option
  5. __compute_mode = "default"
  6. __conv_format = "default"
  7. _benchmark_kernel = False
  8. _deterministic_kernel = False
  9. __all__ = [
  10. "benchmark_kernel",
  11. "deterministic_kernel",
  12. "async_level",
  13. "disable_memory_forwarding",
  14. "_compute_mode",
  15. "_conv_format",
  16. "_override",
  17. ]
  18. @property
  19. def benchmark_kernel(mod):
  20. r"""Whether or not run possible algorithms on real device to find the best one. The default option is false,
  21. which means use heuristic to choose the fastest algorithm.
  22. Examples:
  23. .. code-block::
  24. import megengine as mge
  25. mge.config.benchmark_kernel = True
  26. """
  27. return _benchmark_kernel
  28. @benchmark_kernel.setter
  29. def benchmark_kernel(mod, option: bool):
  30. global _benchmark_kernel
  31. # try different strategy, then clear algorithm cache
  32. if option != _benchmark_kernel:
  33. _clear_algorithm_cache()
  34. _benchmark_kernel = option
  35. @property
  36. def deterministic_kernel(mod):
  37. r"""Whether or not the fastest algorithm choosed is reproducible. The default option is false,
  38. which means the algorithm is not reproducible.
  39. Examples:
  40. .. code-block::
  41. import megengine as mge
  42. mge.config.deterministic_kernel = True
  43. """
  44. return _deterministic_kernel
  45. @deterministic_kernel.setter
  46. def deterministic_kernel(mod, option: bool):
  47. global _deterministic_kernel
  48. _deterministic_kernel = option
  49. @property
  50. def async_level(mod) -> int:
  51. r"""Get or set config whether raise error exactly when invoking op. The default level is 2,
  52. which means both device and user side errors are async.
  53. Examples:
  54. .. code-block::
  55. import megengine as mge
  56. mge.config.async_level = 2
  57. """
  58. return get_option("async_level")
  59. @async_level.setter
  60. def async_level(mod, level: int):
  61. assert level >= 0 and level <= 2, "async_level should be 0, 1 or 2"
  62. set_option("async_level", level)
  63. @property
  64. def disable_memory_forwarding(mod) -> bool:
  65. r"""Get or set config whether to disable memory forwarding. The default option is false,
  66. which means storage may be shared among tensors.
  67. Examples:
  68. .. code-block::
  69. import megengine as mge
  70. mge.config.disable_memory_forwarding = False
  71. """
  72. return bool(get_option("disable_memory_forwarding"))
  73. @disable_memory_forwarding.setter
  74. def disable_memory_forwarding(mod, disable: bool):
  75. set_option("disable_memory_forwarding", disable)
  76. @property
  77. def _compute_mode(mod):
  78. r"""Get or set the precision of intermediate results. The default option is "default",
  79. which means that no special requirements will be placed on. When set to 'float32', it
  80. would be used for accumulator and intermediate result, but only effective when input and
  81. output are of float16 dtype.
  82. Examples:
  83. .. code-block::
  84. import megengine as mge
  85. mge.config._compute_mode = "default"
  86. """
  87. return __compute_mode
  88. @_compute_mode.setter
  89. def _compute_mode(mod, _compute_mode: str):
  90. global __compute_mode
  91. __compute_mode = _compute_mode
  92. @property
  93. def _conv_format(mod):
  94. r"""Get or set convolution data/filter/output layout format. The default option is "default",
  95. which means that no special format will be placed on. There are all layout definitions
  96. ``NCHW`` layout: ``{N, C, H, W}``
  97. ``NHWC`` layout: ``{N, H, W, C}``
  98. ``NHWCD4`` layout: ``{N, H, (C + 3) / 4, W, 4}``
  99. ``NHWCD4I`` layout: with ``align_axis = 2``
  100. ``NCHW4`` layout: ``{N, C/4, H, W, 4}``
  101. ``NCHW88`` layout: ``{N, C/8, H, W, 8}``
  102. ``CHWN4`` layout: ``{C/4, H, W, N, 4}``
  103. ``NCHW64`` layout: ``{N, C/64, H, W, 64}``
  104. Examples:
  105. .. code-block::
  106. import megengine as mge
  107. mge.config._conv_format = "NHWC"
  108. """
  109. return __conv_format
  110. @_conv_format.setter
  111. def _conv_format(mod, format: str):
  112. global __conv_format
  113. __conv_format = format
  114. def _reset_execution_config(
  115. benchmark_kernel=None,
  116. deterministic_kernel=None,
  117. async_level=None,
  118. compute_mode=None,
  119. conv_format=None,
  120. ):
  121. global _benchmark_kernel, _deterministic_kernel, _async_level, __compute_mode, __conv_format
  122. orig_flags = (
  123. _benchmark_kernel,
  124. _deterministic_kernel,
  125. get_option("async_level"),
  126. __compute_mode,
  127. __conv_format,
  128. )
  129. if benchmark_kernel is not None:
  130. _benchmark_kernel = benchmark_kernel
  131. if deterministic_kernel is not None:
  132. _deterministic_kernel = deterministic_kernel
  133. if async_level is not None:
  134. set_option("async_level", async_level)
  135. if compute_mode is not None:
  136. __compute_mode = compute_mode
  137. if conv_format is not None:
  138. __conv_format = conv_format
  139. return orig_flags
  140. @contextmanager
  141. def _override(
  142. benchmark_kernel=None,
  143. deterministic_kernel=None,
  144. async_level=None,
  145. compute_mode=None,
  146. conv_format=None,
  147. ):
  148. r"""A context manager that users can opt in by attaching the decorator to set
  149. the config of the global variable.
  150. Examples:
  151. .. code-block::
  152. import megengine as mge
  153. @mge.config._override(
  154. benchmark_kernel = True,
  155. deterministic_kernel = Fasle,
  156. async_level=2,
  157. compute_mode="float32",
  158. conv_format="NHWC",
  159. )
  160. def train():
  161. """
  162. orig_flags = _reset_execution_config(
  163. benchmark_kernel, deterministic_kernel, async_level, compute_mode, conv_format,
  164. )
  165. try:
  166. yield
  167. finally:
  168. # recover the previous values
  169. _reset_execution_config(*orig_flags)
  170. def _get_actual_op_param(function_param, config_param):
  171. return function_param if config_param == "default" else config_param