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.

module_tracer.py 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  2. #
  3. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  4. #
  5. # Unless required by applicable law or agreed to in writing,
  6. # software distributed under the License is distributed on an
  7. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8. import collections
  9. from .. import Tensor
  10. from .. import functional as F
  11. from ..core.tensor.array_method import ArrayMethodMixin
  12. from ..module import Module
  13. from ..module.qat import QATModule
  14. _active_module_tracer = None
  15. BUILTIN_ARRAY_METHOD = [
  16. "__lt__",
  17. "__le__",
  18. "__gt__",
  19. "__ge__",
  20. "__eq__",
  21. "__ne__",
  22. "__neg__",
  23. "__pos__",
  24. "__abs__",
  25. "__invert__",
  26. "__round__",
  27. "__floor__",
  28. "__ceil__",
  29. "__add__",
  30. "__sub__",
  31. "__mul__",
  32. "__matmul__",
  33. "__truediv__",
  34. "__floordiv__",
  35. "__mod__",
  36. "__pow__",
  37. "__lshift__",
  38. "__rshift__",
  39. "__and__",
  40. "__or__",
  41. "__xor__",
  42. "__radd__",
  43. "__rsub__",
  44. "__rmul__",
  45. "__rmatmul__",
  46. "__rtruediv__",
  47. "__rfloordiv__",
  48. "__rmod__",
  49. "__rpow__",
  50. "__rlshift__",
  51. "__rrshift__",
  52. "__rand__",
  53. "__ror__",
  54. "__rxor__",
  55. "__iadd__",
  56. "__isub__",
  57. "__imul__",
  58. "__imatmul__",
  59. "__itruediv__",
  60. "__ifloordiv__",
  61. "__imod__",
  62. "__ipow__",
  63. "__ilshift__",
  64. "__irshift__",
  65. "__iand__",
  66. "__ior__",
  67. "__ixor__",
  68. "transpose",
  69. "astype",
  70. "reshape",
  71. "_broadcast",
  72. "transpose",
  73. "flatten",
  74. "sum",
  75. "prod",
  76. "min",
  77. "max",
  78. "mean",
  79. "__getitem__",
  80. "__setitem__",
  81. ]
  82. BUILTIN_TENSOR_WRAP_METHOD = [
  83. "T",
  84. "to",
  85. "size",
  86. "shape",
  87. "detach",
  88. "device",
  89. "dtype",
  90. "grad",
  91. "item",
  92. "name",
  93. "ndim",
  94. "numpy",
  95. "qparams",
  96. "set_value",
  97. "reset_zero",
  98. "requires_grad",
  99. "_reset",
  100. "_isscalar",
  101. "_setscalar",
  102. "_tuple_shape",
  103. "_unsetscalar",
  104. ]
  105. def get_tensor_wrapable_method():
  106. return BUILTIN_TENSOR_WRAP_METHOD + BUILTIN_ARRAY_METHOD
  107. def active_module_tracer():
  108. return _active_module_tracer
  109. def set_active_module_tracer(tracer):
  110. global _active_module_tracer
  111. _active_module_tracer = tracer
  112. class module_tracer:
  113. # builtin types
  114. _opaque_types = set()
  115. _active_scopes = None
  116. def __init__(self, wrap_fn):
  117. self._active_scopes = []
  118. self.patcher = Patcher(wrap_fn)
  119. @classmethod
  120. def register_as_builtin(cls, mod):
  121. assert issubclass(mod, Module)
  122. cls._opaque_types.add(mod)
  123. return mod
  124. @classmethod
  125. def is_builtin(cls, mod):
  126. return type(mod) in cls._opaque_types
  127. def push_scope(self, scope):
  128. self._active_scopes.append(scope)
  129. def pop_scope(self):
  130. self._active_scopes.pop()
  131. def current_scope(self):
  132. if self._active_scopes:
  133. return self._active_scopes[-1]
  134. return None
  135. class NotExist:
  136. pass
  137. class PatchedFn:
  138. frame_dict = None
  139. name = None
  140. origin_fn = None
  141. def __init__(self, frame_dict, name):
  142. self.frame_dict = frame_dict
  143. self.name = name
  144. self.origin_fn = (
  145. self.frame_dict[name]
  146. if isinstance(frame_dict, collections.abc.Mapping)
  147. else getattr(frame_dict, name, NotExist)
  148. )
  149. def set_func(self, func):
  150. if isinstance(self.frame_dict, collections.abc.Mapping):
  151. self.frame_dict[self.name] = func
  152. else:
  153. if func is not NotExist:
  154. setattr(self.frame_dict, self.name, func)
  155. else:
  156. delattr(self.frame_dict, self.name)
  157. class Patcher:
  158. _builtin_functions = []
  159. _builtin_modules = [
  160. F,
  161. F.distributed,
  162. F.elemwise,
  163. F.inplace,
  164. F.loss,
  165. F.math,
  166. F.metric,
  167. F.nn,
  168. F.quantized,
  169. F.tensor,
  170. F.utils,
  171. F.vision,
  172. ]
  173. _builtin_methods = [
  174. Tensor,
  175. ArrayMethodMixin,
  176. ]
  177. def __init__(self, wrap_fn):
  178. self.patched_fn_ids = set()
  179. self.patched_fn = []
  180. self.visited_frames_ids = set()
  181. self.wrap_fn = wrap_fn
  182. for module in self._builtin_modules:
  183. self.patch_module(module)
  184. for meth in BUILTIN_ARRAY_METHOD:
  185. self.patch_method(ArrayMethodMixin, meth, self.wrap_fn)
  186. self.patch_method(Tensor, "detach", self.wrap_fn)
  187. self.patch_method(Tensor, "__new__", self.wrap_fn)
  188. self.patch_method(QATModule, "_apply_fakequant_with_observer", self.wrap_fn)
  189. for i, j in self._builtin_functions:
  190. if id(i) not in self.visited_frames_ids:
  191. self.patch_function(i, j, self.wrap_fn)
  192. for m in module_tracer._opaque_types:
  193. self.auto_patch(getattr(getattr(m, "forward", m), "__globals__", {}))
  194. def patch_function(self, frame_dict, fn, wrap_fn):
  195. patched_fn = PatchedFn(frame_dict, fn)
  196. self.patched_fn_ids.add(id(patched_fn.origin_fn))
  197. patched_fn.set_func(wrap_fn(patched_fn.origin_fn))
  198. self.patched_fn.append(patched_fn)
  199. def patch_method(self, cls, name, wrap_fn):
  200. self.patch_function(cls, name, wrap_fn)
  201. def patch_cls(self, cls):
  202. import inspect
  203. if id(cls) not in self.visited_frames_ids:
  204. for k, v in cls.__dict__.items():
  205. if inspect.isfunction(v) and not k.startswith("_"):
  206. self.patch_function(cls, k, self.wrap_fn)
  207. self.visited_frames_ids.add(id(cls))
  208. def patch_module(self, module):
  209. import inspect
  210. if id(module.__dict__) not in self.visited_frames_ids:
  211. keys = (
  212. getattr(module, "__all__")
  213. if hasattr(module, "__all__")
  214. else module.__dict__.keys()
  215. )
  216. for k in keys:
  217. v = getattr(module, k)
  218. if inspect.isfunction(v) and not k.startswith("_"):
  219. self.patch_function(module.__dict__, k, self.wrap_fn)
  220. self.visited_frames_ids.add(id(module.__dict__))
  221. def auto_patch(self, frame_dict):
  222. if id(frame_dict) not in self.visited_frames_ids:
  223. for k, v in frame_dict.items():
  224. if id(v) in self.patched_fn_ids:
  225. self.patch_function(frame_dict, k, self.wrap_fn)
  226. self.visited_frames_ids.add(id(frame_dict))
  227. def __enter__(self):
  228. return self
  229. def __exit__(self, type, vlaue, trace):
  230. while self.patched_fn:
  231. pf = self.patched_fn.pop()
  232. pf.set_func(pf.origin_fn)
  233. self.visited_frames_ids.clear()

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