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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 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. """
  18. def __init__(self, new_py_limit):
  19. self.new_py_limit = new_py_limit
  20. self.count = 0
  21. self.lock = threading.Lock()
  22. self.orig_py_limit = 0
  23. self.orig_rlim_stack_soft = 0
  24. self.orig_rlim_stack_hard = 0
  25. def __enter__(self):
  26. with self.lock:
  27. if self.count == 0:
  28. self.orig_py_limit = sys.getrecursionlimit()
  29. if platform.system() != "Windows":
  30. (
  31. self.orig_rlim_stack_soft,
  32. self.orig_rlim_stack_hard,
  33. ) = resource.getrlimit(resource.RLIMIT_STACK)
  34. # FIXME: https://bugs.python.org/issue34602, python3 release version
  35. # on Macos always have this issue, not all user install python3 from src
  36. try:
  37. resource.setrlimit(
  38. resource.RLIMIT_STACK,
  39. (self.orig_rlim_stack_hard, self.orig_rlim_stack_hard),
  40. )
  41. except ValueError as exc:
  42. if platform.system() != "Darwin":
  43. raise exc
  44. # increase recursion limit
  45. sys.setrecursionlimit(self.new_py_limit)
  46. self.count += 1
  47. def __exit__(self, type, value, traceback):
  48. with self.lock:
  49. self.count -= 1
  50. if self.count == 0:
  51. sys.setrecursionlimit(self.orig_py_limit)
  52. if platform.system() != "Windows":
  53. try:
  54. resource.setrlimit(
  55. resource.RLIMIT_STACK,
  56. (self.orig_rlim_stack_soft, self.orig_rlim_stack_hard),
  57. )
  58. except ValueError as exc:
  59. if platform.system() != "Darwin":
  60. raise exc
  61. _max_recursion_limit_context_manager = AlternativeRecursionLimit(2 ** 31 - 1)
  62. def max_recursion_limit():
  63. r"""Sets recursion limit to the max possible value
  64. """
  65. return _max_recursion_limit_context_manager

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