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

Method flatten_inputs

imperative/python/megengine/jit/tracing.py:244–274  ·  view source on GitHub ↗
(self, *args, **kwargs)

Source from the content-addressed store, hash-verified

242 return self._trace.without_host
243
244 def flatten_inputs(self, *args, **kwargs):
245 from ..traced_module.pytree import tree_flatten, SUPPORTED_LEAF_CLS
246 from ..module import Module
247 from ..optimizer import Optimizer
248
249 if Optimizer not in SUPPORTED_LEAF_CLS:
250 SUPPORTED_LEAF_CLS.append(Optimizer)
251
252 tensor_args = []
253 modules = []
254 fargs, _ = tree_flatten((args, kwargs))
255 for a in fargs:
256 if isinstance(a, RawTensor):
257 tensor_args.append(a)
258 elif isinstance(a, Module):
259 modules.append(a)
260 elif isinstance(a, Optimizer):
261 states = a._state.values()
262 for s in states:
263 assert isinstance(s, dict)
264 tensor_args.extend(list(s.values()))
265 for m in modules:
266 tensor_args.extend(list(m.parameters()))
267 grads = []
268 for t in tensor_args:
269 if t.grad is not None:
270 grads.append(t.grad)
271 for m in modules:
272 tensor_args.extend(list(m.buffers()))
273 tensor_args.extend(grads)
274 return tensor_args
275
276 def compile(self):
277 raise NotImplementedError

Callers 2

trace_without_hostMethod · 0.95
wrapped_funcFunction · 0.80

Calls 6

tree_flattenFunction · 0.85
listFunction · 0.85
valuesMethod · 0.80
parametersMethod · 0.80
buffersMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected