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.

pytree.py 7.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 collections
  10. from collections import OrderedDict
  11. from typing import Callable, NamedTuple
  12. import numpy as np
  13. from ...core._imperative_rt.common import CompNode
  14. from ...core._imperative_rt.core2 import Tensor as RawTensor
  15. from ...core._wrap import Device
  16. from ...core.tensor.dtype import QuantDtypeMeta
  17. from ...module import Module
  18. from ...quantization.utils import LSQParams, QParams, QuantMode
  19. from ...tensor import Parameter, Tensor
  20. from .node import ModuleNode, Node, NodeMixin, TensorNode
  21. class ArgsIndex:
  22. def __init__(self, index=0, name="") -> None:
  23. self.index = index
  24. self.name = name
  25. def __repr__(self) -> str:
  26. return self.name
  27. SUPPORTED_TYPE = {}
  28. # if type(object) or obj in SUPPORTED_LEAF_TYPE, the object could be treated as leaf node of pytree
  29. SUPPORTED_LEAF_TYPE = {
  30. RawTensor,
  31. Tensor,
  32. Parameter,
  33. str,
  34. int,
  35. float,
  36. bool,
  37. QuantDtypeMeta,
  38. CompNode,
  39. Device,
  40. type(None),
  41. type(Ellipsis),
  42. QuantMode,
  43. ArgsIndex,
  44. }
  45. # if isinstance(object, SUPPORTED_LEAF_CLS) or issubclass(obj, SUPPORTED_LEAF_CLS) is True, the object could be threated as leaf node of pytree
  46. SUPPORTED_LEAF_CLS = [Module, Node, NodeMixin, np.dtype, np.ndarray, np.number]
  47. NodeType = NamedTuple("NodeType", [("flatten", Callable), ("unflatten", Callable)])
  48. def register_supported_type(type, flatten=None, unflatten=None):
  49. if flatten and unflatten:
  50. SUPPORTED_TYPE[type] = NodeType(flatten, unflatten)
  51. else:
  52. SUPPORTED_LEAF_CLS.append(type)
  53. def _dict_flatten(inp):
  54. aux_data = []
  55. results = []
  56. for key, value in sorted(inp.items()):
  57. results.append(value)
  58. aux_data.append(key)
  59. return results, tuple(aux_data)
  60. def _dict_unflatten(inps, aux_data):
  61. return dict(zip(aux_data, inps))
  62. def _ordereddict_flatten(inp):
  63. aux_data = []
  64. results = []
  65. for key, value in inp.items():
  66. results.append(value)
  67. aux_data.append(key)
  68. return results, tuple(aux_data)
  69. def _ordereddict_unflatten(inps, aux_data):
  70. return OrderedDict(zip(aux_data, inps))
  71. def qparams_flatten(inp):
  72. aux_data = []
  73. results = []
  74. for key in inp.__slots__:
  75. aux_data.append(key)
  76. results.append(getattr(inp, key, None))
  77. return results, tuple(aux_data)
  78. def qparams_unflatten(inp, aux_data):
  79. obj = QParams.__new__(QParams)
  80. for k, v in zip(aux_data, inp):
  81. setattr(obj, k, v)
  82. return obj
  83. register_supported_type(list, lambda x: (x, None), lambda x, aux_data: list(x))
  84. register_supported_type(tuple, lambda x: (x, None), lambda x, aux_data: tuple(x))
  85. register_supported_type(dict, _dict_flatten, _dict_unflatten)
  86. register_supported_type(
  87. collections.OrderedDict, _ordereddict_flatten, _ordereddict_unflatten
  88. )
  89. register_supported_type(
  90. slice,
  91. lambda x: ([x.start, x.stop, x.step], None),
  92. lambda x, aux_data: slice(x[0], x[1], x[2]),
  93. )
  94. register_supported_type(QParams, qparams_flatten, qparams_unflatten)
  95. def _is_leaf(obj):
  96. if isinstance(obj, type):
  97. return issubclass(obj, tuple(SUPPORTED_LEAF_CLS)) or obj in SUPPORTED_LEAF_TYPE
  98. return (
  99. isinstance(obj, tuple(SUPPORTED_LEAF_CLS)) or type(obj) in SUPPORTED_LEAF_TYPE
  100. )
  101. def _leaf_type(node):
  102. if isinstance(node, (RawTensor, TensorNode)):
  103. return (Tensor, TensorNode, ArgsIndex)
  104. elif isinstance(node, (NodeMixin, Module, ModuleNode)):
  105. return (Module, ModuleNode, NodeMixin, ArgsIndex)
  106. else:
  107. return (type(node), ArgsIndex)
  108. def _is_const_leaf(node):
  109. if isinstance(node, (RawTensor, NodeMixin, Module)):
  110. return False
  111. return True
  112. def tree_flatten(
  113. values,
  114. leaf_type: Callable = _leaf_type,
  115. is_leaf: Callable = _is_leaf,
  116. is_const_leaf: Callable = _is_const_leaf,
  117. ):
  118. if type(values) not in SUPPORTED_TYPE:
  119. assert is_leaf(values), values
  120. node = LeafDef(leaf_type(values))
  121. if is_const_leaf(values):
  122. node.const_val = values
  123. return [values,], node
  124. rst = []
  125. children_defs = []
  126. children_values, aux_data = SUPPORTED_TYPE[type(values)].flatten(values)
  127. for v in children_values:
  128. v_list, treedef = tree_flatten(v, leaf_type, is_leaf, is_const_leaf)
  129. rst.extend(v_list)
  130. children_defs.append(treedef)
  131. return rst, TreeDef(type(values), aux_data, children_defs)
  132. class TreeDef:
  133. def __init__(self, type, aux_data, children_defs):
  134. self.type = type
  135. self.aux_data = aux_data
  136. self.children_defs = children_defs
  137. self.num_leaves = sum(ch.num_leaves for ch in children_defs)
  138. def unflatten(self, leaves):
  139. assert len(leaves) == self.num_leaves
  140. start = 0
  141. children = []
  142. for ch in self.children_defs:
  143. children.append(ch.unflatten(leaves[start : start + ch.num_leaves]))
  144. start += ch.num_leaves
  145. return SUPPORTED_TYPE[self.type].unflatten(children, self.aux_data)
  146. def __hash__(self):
  147. return hash(
  148. tuple(
  149. [
  150. self.type,
  151. self.aux_data,
  152. self.num_leaves,
  153. tuple([hash(x) for x in self.children_defs]),
  154. ]
  155. )
  156. )
  157. def __lt__(self, other):
  158. return self.__hash__() < other.__hash__()
  159. def __gt__(self, other):
  160. return self.__hash__() > other.__hash__()
  161. def __eq__(self, other):
  162. return (
  163. self.type == other.type
  164. and self.aux_data == other.aux_data
  165. and self.num_leaves == other.num_leaves
  166. and self.children_defs == other.children_defs
  167. )
  168. def __repr__(self):
  169. return "{}[{}]".format(self.type.__name__, self.children_defs)
  170. class LeafDef(TreeDef):
  171. def __init__(self, type):
  172. if not isinstance(type, collections.abc.Sequence):
  173. type = (type,)
  174. super().__init__(type, None, [])
  175. self.num_leaves = 1
  176. self.const_val = None
  177. def unflatten(self, leaves):
  178. assert len(leaves) == 1
  179. assert isinstance(leaves[0], self.type), self.type
  180. return leaves[0]
  181. def __eq__(self, other):
  182. if isinstance(self.const_val, np.ndarray):
  183. return self.type == other.type and (self.const_val == other.const_val).all()
  184. return self.type == other.type and self.const_val == other.const_val
  185. def __hash__(self):
  186. if isinstance(self.const_val, np.ndarray):
  187. return hash(tuple([self.type, str(self.const_val)]))
  188. return hash(tuple([self.type, self.const_val]))
  189. def __repr__(self):
  190. return "Leaf({}[{}])".format(
  191. ", ".join(t.__name__ for t in self.type), self.const_val
  192. )

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