MCPcopy Create free account
hub / github.com/pytorch/executorch / _unstack_pytree

Function _unstack_pytree

exir/pass_base.py:76–97  ·  view source on GitHub ↗
(xs)

Source from the content-addressed store, hash-verified

74
75
76def _unstack_pytree(xs) -> List[PyTree]: # pyre-ignore
77 flat_xs, inspec = pytree.tree_flatten(xs)
78 if not all(isinstance(xs, torch.Tensor) for xs in flat_xs):
79 raise RuntimeError(f"Leaves of xs must be Tensor {flat_xs}")
80
81 if not all(xs.shape[0] == flat_xs[0].shape[0] for xs in flat_xs):
82 raise RuntimeError(
83 f"Leaves of xs must have same leading dimension size {[xs.shape for xs in flat_xs]}"
84 )
85
86 ctx = (
87 FunctionalTensorMode
88 if any(isinstance(x, FunctionalTensor) for x in flat_xs)
89 else nullcontext
90 )
91 with ctx():
92 a = zip(*flat_xs)
93
94 pytrees = []
95 for tuple in a:
96 pytrees.append(pytree.tree_unflatten(tuple, inspec))
97 return pytrees
98
99
100class NodeMetadata:

Callers 1

call_mapMethod · 0.85

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected