| 74 | |
| 75 | |
| 76 | def _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 | |
| 100 | class NodeMetadata: |