(obj)
| 119 | |
| 120 | |
| 121 | def _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 | |
| 148 | def _check_builtin_module_attr(mod): |
no test coverage detected