MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / _check_obj_attr

Function _check_obj_attr

imperative/python/megengine/traced_module/utils.py:121–145  ·  view source on GitHub ↗
(obj)

Source from the content-addressed store, hash-verified

119
120
121def _check_obj_attr(obj):
122 # check if all the attributes of a obj is serializable
123 from .pytree import tree_flatten
124 from .pytree import SUPPORTED_LEAF_CLS, SUPPORTED_LEAF_TYPE, TreeDef
125 from .expr import Expr
126 from .traced_module import TracedModule, InternalGraph, NameSpace
127
128 def _check_leaf_type(leaf):
129 leaf_type = leaf if isinstance(leaf, type) else type(leaf)
130 traced_module_types = [Expr, TreeDef, TracedModule, InternalGraph, NameSpace]
131 return (
132 issubclass(leaf_type, tuple(SUPPORTED_LEAF_CLS + traced_module_types))
133 or leaf_type in SUPPORTED_LEAF_TYPE
134 )
135
136 for _, v in obj.items():
137 leafs, _ = tree_flatten(v, is_leaf=lambda _: True)
138 for leaf in leafs:
139 assert _check_leaf_type(leaf), (
140 "Type {} is not supported in TracedModule serialization by default. "
141 "If you want to save this object to file, please call tm.register_supported_type({}) "
142 "before saving.".format(
143 leaf if isinstance(leaf, type) else type(leaf), type(leaf).__name__
144 )
145 )
146
147
148def _check_builtin_module_attr(mod):

Callers 10

__getstate__Method · 0.85
__getstate__Method · 0.85
__getstate__Method · 0.85
__getstate__Method · 0.85
__getstate__Method · 0.85
__getstate__Method · 0.85
__getstate__Method · 0.85
__getstate__Method · 0.85
__getstate__Method · 0.85
__getstate__Method · 0.85

Calls 4

tree_flattenFunction · 0.85
_check_leaf_typeFunction · 0.85
itemsMethod · 0.80
formatMethod · 0.45

Tested by

no test coverage detected