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 3.6 kB

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

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台