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 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import collections
  2. from collections import OrderedDict, defaultdict
  3. from functools import partial
  4. from inspect import FullArgSpec
  5. from typing import Any, Callable, Dict, List, NamedTuple, Tuple
  6. import numpy as np
  7. from ..core._imperative_rt import OpDef
  8. from ..core._imperative_rt.common import CompNode
  9. from ..core._imperative_rt.core2 import Tensor as RawTensor
  10. from ..core._wrap import Device
  11. from ..core.tensor.dtype import QuantDtypeMeta
  12. from ..distributed import Group
  13. from ..module import Module
  14. from ..quantization.utils import LSQParams, QParams, QuantMode
  15. from ..tensor import Parameter, Tensor
  16. from .node import ModuleNode, Node, NodeMixin, TensorNode
  17. class ArgsIndex:
  18. def __init__(self, index=0, name="") -> None:
  19. self.index = index
  20. self.name = name
  21. def __repr__(self) -> str:
  22. return self.name
  23. SUPPORTED_TYPE = {}
  24. # if type(object) or obj in SUPPORTED_LEAF_TYPE, the object could be treated as leaf node of pytree
  25. SUPPORTED_LEAF_TYPE = {
  26. RawTensor,
  27. Tensor,
  28. Parameter,
  29. str,
  30. int,
  31. float,
  32. bool,
  33. bytes,
  34. bytearray,
  35. QuantDtypeMeta,
  36. CompNode,
  37. Device,
  38. type(None),
  39. type(Ellipsis),
  40. QuantMode,
  41. ArgsIndex,
  42. Group,
  43. FullArgSpec,
  44. }
  45. USER_REGISTERED_LEAF_TYPE = []
  46. USER_REGISTERED_CONTAINER_TYPE = []
  47. # if isinstance(object, SUPPORTED_LEAF_CLS) or issubclass(obj, SUPPORTED_LEAF_CLS) is True, the object could be threated as leaf node of pytree
  48. SUPPORTED_LEAF_CLS = [
  49. Module,
  50. Node,
  51. NodeMixin,
  52. np.dtype,
  53. np.ndarray,
  54. np.number,
  55. np.bool_,
  56. OpDef,
  57. ]
  58. NodeType = NamedTuple("NodeType", [("flatten", Callable), ("unflatten", Callable)])
  59. def register_supported_type(
  60. type,
  61. flatten_fn: Callable[[Any], Tuple[List, Any]] = None,
  62. unflatten_fn: Callable[[List, Any], Any] = None,
  63. ):
  64. r"""Call this function to register the ``type`` as a built-in type. The registered ``type``
  65. can be used and serialized correctly in :py:class:`TracedModule`.
  66. Examples:
  67. .. code-block::
  68. def dict_flatten(obj: Dict):
  69. context, values = [], []
  70. # obj.keys() needs to be sortable
  71. keys = sorted(obj.keys())
  72. for key in keys:
  73. values.append(obj[key])
  74. context.append(key)
  75. return values, tuple(context)
  76. def dict_unflatten(values: List, context: Any):
  77. return dict(zip(context, values))
  78. register_supported_type(dict, dict_flatten, dict_unflatten)
  79. Args:
  80. type: the type that needs to be registered.
  81. flatten_fn: a function that should take an object created from ``type`` and return a
  82. flat list of values. It can also return some context that is used in reconstructing
  83. the object. Default: None
  84. unflatten_fn: a function that should take a flat list of values and some context
  85. (returned by flatten_fn). It returns the object by reconstructing
  86. it from the list and the context. Default: None
  87. """
  88. tp_info = (type.__module__, type.__qualname__)
  89. if flatten_fn and unflatten_fn:
  90. USER_REGISTERED_CONTAINER_TYPE.append(tp_info)
  91. else:
  92. USER_REGISTERED_LEAF_TYPE.append(tp_info)
  93. _register_supported_type(type, flatten_fn, unflatten_fn)
  94. def _register_supported_type(type, flatten_fn=None, unflatten_fn=None):
  95. if flatten_fn and unflatten_fn:
  96. SUPPORTED_TYPE[type] = NodeType(flatten_fn, unflatten_fn)
  97. else:
  98. SUPPORTED_LEAF_CLS.append(type)
  99. def _dict_flatten(ordered, inp):
  100. aux_data = []
  101. results = []
  102. dict_items = inp.items() if ordered else sorted(inp.items())
  103. for key, value in dict_items:
  104. results.append(value)
  105. aux_data.append(key)
  106. return results, tuple(aux_data)
  107. def _dict_unflatten(dict_type, inps, aux_data):
  108. return dict_type(zip(aux_data, inps))
  109. def qparams_flatten(inp):
  110. aux_data = []
  111. results = []
  112. for key in inp.__slots__:
  113. aux_data.append(key)
  114. results.append(getattr(inp, key, None))
  115. return results, tuple(aux_data)
  116. def qparams_unflatten(qparam_type, inp, aux_data):
  117. obj = qparam_type.__new__(qparam_type)
  118. for k, v in zip(aux_data, inp):
  119. setattr(obj, k, v)
  120. return obj
  121. _register_supported_type(list, lambda x: (x, None), lambda x, aux_data: list(x))
  122. _register_supported_type(tuple, lambda x: (x, None), lambda x, aux_data: tuple(x))
  123. _register_supported_type(
  124. dict, partial(_dict_flatten, False), partial(_dict_unflatten, dict)
  125. )
  126. _register_supported_type(
  127. defaultdict, partial(_dict_flatten, False), partial(_dict_unflatten, defaultdict)
  128. )
  129. _register_supported_type(
  130. OrderedDict, partial(_dict_flatten, True), partial(_dict_unflatten, OrderedDict)
  131. )
  132. _register_supported_type(
  133. slice,
  134. lambda x: ([x.start, x.stop, x.step], None),
  135. lambda x, aux_data: slice(x[0], x[1], x[2]),
  136. )
  137. _register_supported_type(QParams, qparams_flatten, partial(qparams_unflatten, QParams))
  138. _register_supported_type(
  139. LSQParams, qparams_flatten, partial(qparams_unflatten, LSQParams)
  140. )
  141. def _is_leaf(obj):
  142. obj_type = obj if isinstance(obj, type) else type(obj)
  143. return (
  144. issubclass(obj_type, tuple(SUPPORTED_LEAF_CLS))
  145. or obj_type in SUPPORTED_LEAF_TYPE
  146. )
  147. def _leaf_type(node):
  148. if isinstance(node, (RawTensor, TensorNode)):
  149. return (Tensor, TensorNode, ArgsIndex)
  150. elif isinstance(node, (NodeMixin, Module, ModuleNode)):
  151. return (Module, ModuleNode, NodeMixin, ArgsIndex)
  152. else:
  153. return (type(node), ArgsIndex)
  154. def _is_const_leaf(node):
  155. if isinstance(node, (RawTensor, Node, NodeMixin, Module)):
  156. return False
  157. return True
  158. def tree_flatten(
  159. values,
  160. leaf_type: Callable = _leaf_type,
  161. is_leaf: Callable = _is_leaf,
  162. is_const_leaf: Callable = _is_const_leaf,
  163. ):
  164. r"""Flattens a pytree into a list of values and a :class:`TreeDef` that can be used
  165. to reconstruct the pytree.
  166. """
  167. if type(values) not in SUPPORTED_TYPE:
  168. assert is_leaf(
  169. values
  170. ), 'doesn\'t support {} type, MUST use "register_supported_type" method to register self-defined type'.format(
  171. values
  172. )
  173. node = LeafDef(leaf_type(values))
  174. if is_const_leaf(values):
  175. node.const_val = values
  176. return [values,], node
  177. rst = []
  178. children_defs = []
  179. children_values, aux_data = SUPPORTED_TYPE[type(values)].flatten(values)
  180. for v in children_values:
  181. v_list, treedef = tree_flatten(v, leaf_type, is_leaf, is_const_leaf)
  182. rst.extend(v_list)
  183. children_defs.append(treedef)
  184. return rst, TreeDef(type(values), aux_data, children_defs)
  185. class TreeDef:
  186. r"""A ``TreeDef`` represents the structure of a pytree.
  187. Args:
  188. type: the type of root Node of the pytree.
  189. aux_data: some const data that is useful in unflattening the pytree.
  190. children_defs: ``TreeDef`` for each child of the root Node.
  191. num_leaves: the number of leaves.
  192. """
  193. def __init__(self, type, aux_data, children_defs):
  194. self.type = type
  195. self.aux_data = aux_data
  196. self.children_defs = children_defs
  197. self.num_leaves = sum(ch.num_leaves for ch in children_defs)
  198. def unflatten(self, leaves):
  199. r"""Given a list of values and a ``TreeDef``, builds a pytree.
  200. This is the inverse operation of ``tree_flatten``.
  201. """
  202. assert len(leaves) == self.num_leaves
  203. start = 0
  204. children = []
  205. for ch in self.children_defs:
  206. children.append(ch.unflatten(leaves[start : start + ch.num_leaves]))
  207. start += ch.num_leaves
  208. return SUPPORTED_TYPE[self.type].unflatten(children, self.aux_data)
  209. def __hash__(self):
  210. return hash(
  211. tuple(
  212. [
  213. self.type,
  214. self.aux_data,
  215. self.num_leaves,
  216. tuple([hash(x) for x in self.children_defs]),
  217. ]
  218. )
  219. )
  220. def __ne__(self, other) -> bool:
  221. return not self.__eq__(other)
  222. def __eq__(self, other) -> bool:
  223. return (
  224. self.type == other.type
  225. and self.aux_data == other.aux_data
  226. and self.num_leaves == other.num_leaves
  227. and self.children_defs == other.children_defs
  228. )
  229. def _args_kwargs_repr(self):
  230. if (
  231. len(self.children_defs) == 2
  232. and issubclass(self.children_defs[0].type, (List, Tuple))
  233. and issubclass(self.children_defs[1].type, Dict)
  234. ):
  235. args_def = self.children_defs[0]
  236. content = ", ".join(repr(i) for i in args_def.children_defs)
  237. kwargs_def = self.children_defs[1]
  238. if kwargs_def.aux_data:
  239. content += ", "
  240. content += ", ".join(
  241. str(i) + "=" + repr(j)
  242. for i, j in zip(kwargs_def.aux_data, kwargs_def.children_defs)
  243. )
  244. return content
  245. else:
  246. return repr(self)
  247. def __repr__(self):
  248. format_str = self.type.__name__ + "({})"
  249. aux_data_delimiter = "="
  250. if issubclass(self.type, List):
  251. format_str = "[{}]"
  252. if issubclass(self.type, Tuple):
  253. format_str = "({})"
  254. if issubclass(self.type, Dict):
  255. format_str = "{{{}}}"
  256. aux_data_delimiter = ":"
  257. if self.aux_data:
  258. content = ", ".join(
  259. repr(i) + aux_data_delimiter + repr(j)
  260. for i, j in zip(self.aux_data, self.children_defs)
  261. )
  262. else:
  263. content = ", ".join(repr(i) for i in self.children_defs)
  264. return format_str.format(content)
  265. class LeafDef(TreeDef):
  266. def __init__(self, type):
  267. if not isinstance(type, collections.abc.Sequence):
  268. type = (type,)
  269. super().__init__(type, None, [])
  270. self.num_leaves = 1
  271. self.const_val = None
  272. def unflatten(self, leaves):
  273. assert len(leaves) == 1
  274. assert isinstance(leaves[0], self.type), self.type
  275. return leaves[0]
  276. def __ne__(self, other) -> bool:
  277. return not self.__eq__(other)
  278. def __eq__(self, other):
  279. if isinstance(self.const_val, np.ndarray):
  280. return self.type == other.type and (self.const_val == other.const_val).all()
  281. return self.type == other.type and self.const_val == other.const_val
  282. def __hash__(self):
  283. if isinstance(self.const_val, np.ndarray):
  284. return hash(tuple([self.type, str(self.const_val)]))
  285. return hash(tuple([self.type, self.const_val]))
  286. def __repr__(self):
  287. return "{}".format(
  288. self.const_val
  289. if self.const_val is not None or type(None) in self.type
  290. else self.type[0].__name__
  291. )