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

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