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.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. "flatten",
  73. "sum",
  74. "prod",
  75. "min",
  76. "max",
  77. "mean",
  78. "__getitem__",
  79. "__setitem__",
  80. ]
  81. BUILTIN_TENSOR_WRAP_METHOD = [
  82. "T",
  83. "to",
  84. "size",
  85. "shape",
  86. "detach",
  87. "device",
  88. "dtype",
  89. "grad",
  90. "item",
  91. "name",
  92. "ndim",
  93. "numpy",
  94. "qparams",
  95. "set_value",
  96. "reset_zero",
  97. "requires_grad",
  98. "_reset",
  99. "_isscalar",
  100. "_setscalar",
  101. "_tuple_shape",
  102. "_unsetscalar",
  103. ]
  104. def get_tensor_wrapable_method():
  105. return BUILTIN_TENSOR_WRAP_METHOD + BUILTIN_ARRAY_METHOD
  106. def active_module_tracer():
  107. return _active_module_tracer
  108. def set_active_module_tracer(tracer):
  109. global _active_module_tracer
  110. _active_module_tracer = tracer
  111. class module_tracer:
  112. # builtin types
  113. _opaque_types = set()
  114. _active_scopes = None
  115. def __init__(self, wrap_fn):
  116. self._active_scopes = []
  117. self.patcher = Patcher(wrap_fn)
  118. @classmethod
  119. def register_as_builtin(cls, mod):
  120. assert issubclass(mod, Module)
  121. cls._opaque_types.add(mod)
  122. return mod
  123. @classmethod
  124. def is_builtin(cls, mod):
  125. return type(mod) in cls._opaque_types
  126. def push_scope(self, scope):
  127. self._active_scopes.append(scope)
  128. def pop_scope(self):
  129. self._active_scopes.pop()
  130. def current_scope(self):
  131. if self._active_scopes:
  132. return self._active_scopes[-1]
  133. return None
  134. class NotExist:
  135. pass
  136. class PatchedFn:
  137. frame_dict = None
  138. name = None
  139. origin_fn = None
  140. def __init__(self, frame_dict, name):
  141. self.frame_dict = frame_dict
  142. self.name = name
  143. self.origin_fn = (
  144. self.frame_dict[name]
  145. if isinstance(frame_dict, collections.abc.Mapping)
  146. else getattr(frame_dict, name, NotExist)
  147. )
  148. def set_func(self, func):
  149. if isinstance(self.frame_dict, collections.abc.Mapping):
  150. self.frame_dict[self.name] = func
  151. else:
  152. if func is not NotExist:
  153. setattr(self.frame_dict, self.name, func)
  154. else:
  155. delattr(self.frame_dict, self.name)
  156. class Patcher:
  157. _builtin_functions = []
  158. _builtin_modules = [
  159. F,
  160. F.distributed,
  161. F.elemwise,
  162. F.inplace,
  163. F.loss,
  164. F.math,
  165. F.metric,
  166. F.nn,
  167. F.quantized,
  168. F.tensor,
  169. F.utils,
  170. F.vision,
  171. ]
  172. _builtin_methods = [
  173. Tensor,
  174. ArrayMethodMixin,
  175. ]
  176. def __init__(self, wrap_fn):
  177. self.patched_fn_ids = set()
  178. self.patched_fn = []
  179. self.visited_frames_ids = set()
  180. self.wrap_fn = wrap_fn
  181. for module in self._builtin_modules:
  182. self.patch_module(module)
  183. # some functions in F.nn are import from other module, and not in __all__
  184. self.auto_patch(F.nn.__dict__, False)
  185. for meth in BUILTIN_ARRAY_METHOD:
  186. self.patch_method(ArrayMethodMixin, meth, self.wrap_fn)
  187. self.patch_method(Tensor, "detach", self.wrap_fn)
  188. self.patch_method(Tensor, "__new__", self.wrap_fn)
  189. self.patch_method(QATModule, "_apply_fakequant_with_observer", self.wrap_fn)
  190. for i, j in self._builtin_functions:
  191. if id(i) not in self.visited_frames_ids:
  192. self.patch_function(i, j, self.wrap_fn)
  193. for m in module_tracer._opaque_types:
  194. self.auto_patch(getattr(getattr(m, "forward", m), "__globals__", {}))
  195. def patch_function(self, frame_dict, fn, wrap_fn):
  196. patched_fn = PatchedFn(frame_dict, fn)
  197. self.patched_fn_ids.add(id(patched_fn.origin_fn))
  198. patched_fn.set_func(wrap_fn(patched_fn.origin_fn))
  199. self.patched_fn.append(patched_fn)
  200. def patch_method(self, cls, name, wrap_fn):
  201. self.patch_function(cls, name, wrap_fn)
  202. def patch_cls(self, cls):
  203. import inspect
  204. if id(cls) not in self.visited_frames_ids:
  205. for k, v in cls.__dict__.items():
  206. if inspect.isfunction(v) and not k.startswith("_"):
  207. self.patch_function(cls, k, self.wrap_fn)
  208. self.visited_frames_ids.add(id(cls))
  209. def patch_module(self, module):
  210. import inspect
  211. if id(module.__dict__) not in self.visited_frames_ids:
  212. keys = (
  213. getattr(module, "__all__")
  214. if hasattr(module, "__all__")
  215. else module.__dict__.keys()
  216. )
  217. for k in keys:
  218. v = getattr(module, k)
  219. if inspect.isfunction(v) and not k.startswith("_"):
  220. self.patch_function(module.__dict__, k, self.wrap_fn)
  221. self.visited_frames_ids.add(id(module.__dict__))
  222. def auto_patch(self, frame_dict, check_frame_id=True):
  223. if id(frame_dict) not in self.visited_frames_ids or not check_frame_id:
  224. for k, v in frame_dict.items():
  225. if id(v) in self.patched_fn_ids:
  226. self.patch_function(frame_dict, k, self.wrap_fn)
  227. self.visited_frames_ids.add(id(frame_dict))
  228. def __enter__(self):
  229. return self
  230. def __exit__(self, type, vlaue, trace):
  231. while self.patched_fn:
  232. pf = self.patched_fn.pop()
  233. pf.set_func(pf.origin_fn)
  234. self.visited_frames_ids.clear()

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