| 19 | # executorch/extension/flat_tensor/serialize/flat_tensor.fbs |
| 20 | @dataclass |
| 21 | class TensorLayout: |
| 22 | scalar_type: ScalarType |
| 23 | sizes: List[int] |
| 24 | dim_order: List[int] |
| 25 | |
| 26 | @classmethod |
| 27 | def from_tensor(cls, tensor: torch.Tensor) -> "TensorLayout": |
| 28 | if not ( |
| 29 | tensor.is_contiguous(memory_format=torch.contiguous_format) |
| 30 | or tensor.is_contiguous(memory_format=torch.channels_last) |
| 31 | ): |
| 32 | raise ValueError( |
| 33 | "Tensor is not contiguous. Please call .contiguous() before creating the TensorLayout." |
| 34 | ) |
| 35 | return TensorLayout( |
| 36 | scalar_type=scalar_type_enum(tensor.dtype), |
| 37 | sizes=list(tensor.shape), |
| 38 | dim_order=list(dim_order_from_stride(tensor.stride())), |
| 39 | ) |
no outgoing calls