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

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