forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor_layout.py
More file actions
39 lines (32 loc) · 1.23 KB
/
tensor_layout.py
File metadata and controls
39 lines (32 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
# pyre-unsafe
from dataclasses import dataclass
from typing import List
import torch
from executorch.exir.scalar_type import ScalarType
from executorch.exir.tensor import dim_order_from_stride, scalar_type_enum
# Note: keep this in sync with the TensorLayout definition in
# executorch/extension/flat_tensor/serialize/flat_tensor.fbs
@dataclass
class TensorLayout:
scalar_type: ScalarType
sizes: List[int]
dim_order: List[int]
@classmethod
def from_tensor(cls, tensor: torch.Tensor) -> "TensorLayout":
if not (
tensor.is_contiguous(memory_format=torch.contiguous_format)
or tensor.is_contiguous(memory_format=torch.channels_last)
):
raise ValueError(
"Tensor is not contiguous. Please call .contiguous() before creating the TensorLayout."
)
return TensorLayout(
scalar_type=scalar_type_enum(tensor.dtype),
sizes=list(tensor.shape),
dim_order=list(dim_order_from_stride(tensor.stride())),
)