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.

max_recursion_limit.py 2.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 platform
  10. import sys
  11. import threading
  12. # Windows do not imp resource package
  13. if platform.system() != "Windows":
  14. import resource
  15. class AlternativeRecursionLimit:
  16. r"""A reentrant context manager for setting global recursion limits."""
  17. def __init__(self, new_py_limit):
  18. self.new_py_limit = new_py_limit
  19. self.count = 0
  20. self.lock = threading.Lock()
  21. self.orig_py_limit = 0
  22. self.orig_rlim_stack_soft = 0
  23. self.orig_rlim_stack_hard = 0
  24. def __enter__(self):
  25. with self.lock:
  26. if self.count == 0:
  27. self.orig_py_limit = sys.getrecursionlimit()
  28. if platform.system() != "Windows":
  29. (
  30. self.orig_rlim_stack_soft,
  31. self.orig_rlim_stack_hard,
  32. ) = resource.getrlimit(resource.RLIMIT_STACK)
  33. # FIXME: https://bugs.python.org/issue34602, python3 release version
  34. # on Macos always have this issue, not all user install python3 from src
  35. try:
  36. resource.setrlimit(
  37. resource.RLIMIT_STACK,
  38. (self.orig_rlim_stack_hard, self.orig_rlim_stack_hard),
  39. )
  40. except ValueError as exc:
  41. if platform.system() != "Darwin":
  42. raise exc
  43. # increase recursion limit
  44. sys.setrecursionlimit(self.new_py_limit)
  45. self.count += 1
  46. def __exit__(self, type, value, traceback):
  47. with self.lock:
  48. self.count -= 1
  49. if self.count == 0:
  50. sys.setrecursionlimit(self.orig_py_limit)
  51. if platform.system() != "Windows":
  52. try:
  53. resource.setrlimit(
  54. resource.RLIMIT_STACK,
  55. (self.orig_rlim_stack_soft, self.orig_rlim_stack_hard),
  56. )
  57. except ValueError as exc:
  58. if platform.system() != "Darwin":
  59. raise exc
  60. _max_recursion_limit_context_manager = AlternativeRecursionLimit(2 ** 31 - 1)
  61. def max_recursion_limit():
  62. r"""Sets recursion limit to the max possible value."""
  63. return _max_recursion_limit_context_manager

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