Skip to content

Commit 06e85a8

Browse files
pytorchbotlucylq
andauthored
[executorch][schema] Add 'EXTERNAL' to DataLocation in schema
Pull Request resolved: #7191 To indicate if a tensor is external to the PTE file or not. Currently, we can also use the existence of 'fqn' to determine if a tensor is external or not. I think it's better to have a specific location field as fqn may be required for cases besides external tensor storage. ghstack-source-id: 257035024 @exported-using-ghexport Differential Revision: [D66523171](https://our.internmc.facebook.com/intern/diff/D66523171/) Co-authored-by: lucylq <[email protected]>
1 parent ffc1273 commit 06e85a8

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

exir/passes/replace_view_copy_with_view_pass.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def __init__(self, base: TensorSpec, shape: List[int]) -> None:
109109
"mem_obj_id",
110110
"mem_offset",
111111
"dtype", # property
112+
"extra_tensor_info", # property
112113
]
113114

114115
# Make sure _self_fields and _base_fields are disjoint

exir/schema.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,20 @@ class TensorShapeDynamism(IntEnum):
4343
DYNAMIC_UNBOUND = 2
4444

4545

46+
class TensorDataLocation(IntEnum):
47+
SEGMENT = 0
48+
EXTERNAL = 1
49+
50+
4651
@dataclass
4752
class ExtraTensorInfo:
4853
"""
4954
Check program.fbs for explanations of this enum.
5055
"""
5156

52-
mutable_data_segments_idx: Optional[int] = None
57+
mutable_data_segments_idx: int = 0
5358
fully_qualified_name: Optional[str] = None
59+
location: TensorDataLocation = TensorDataLocation.SEGMENT
5460

5561

5662
@dataclass

exir/tensor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import executorch.exir.schema as schema
1919
import torch
2020
from executorch.exir.error import internal_assert
21-
from executorch.exir.schema import ScalarType, TensorShapeDynamism
21+
from executorch.exir.schema import ExtraTensorInfo, ScalarType, TensorShapeDynamism
2222
from executorch.exir.sym_util import eval_shape
2323

2424

@@ -132,6 +132,7 @@ def __init__(
132132
is_sparse: bool = False,
133133
const: bool = False,
134134
requires_grad: bool = False,
135+
extra_tensor_info: Optional[ExtraTensorInfo] = None,
135136
) -> None:
136137
self.scalar_type = dtype
137138
self.const = const
@@ -146,6 +147,7 @@ def __init__(
146147
self.is_sparse = is_sparse
147148
self.init_mem_planning_fields()
148149
self.shape_dynamism: TensorShapeDynamism = determine_tensor_dynanism(self.shape)
150+
self.extra_tensor_info = extra_tensor_info
149151

150152
@property
151153
def allocated_memory(self) -> int:
@@ -346,6 +348,7 @@ def to_list(
346348
allocation_info=allocation_info,
347349
layout=layout_enum(spec.layout),
348350
shape_dynamism=spec.shape_dynamism,
351+
extra_tensor_info=spec.extra_tensor_info,
349352
)
350353
return flatbuffer_tensor
351354

schema/program.fbs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,32 @@ enum TensorShapeDynamism : byte {
5353
DYNAMIC_UNBOUND = 2,
5454
}
5555

56+
// Indicates where a tensor is stored.
57+
enum TensorDataLocation : byte {
58+
// Stored in a segment of the PTE file.
59+
SEGMENT = 0,
60+
// Stored outside of the PTE file.
61+
EXTERNAL = 1,
62+
}
5663

5764
// Table to put additional information about tensors in that is not applicable
5865
// to the vast majority of tensors in the vast majority of programs.
5966
table ExtraTensorInfo {
6067
// [Optional] Specifies the SubsegmentOffsets in
6168
// program.mutable_data_segments that specifies where the data is located in.
6269
// If not present and the data is located in a segment, then the data is in
63-
// the first index.
70+
// index zero.
6471
mutable_data_segments_idx: uint64;
6572

6673
// [Optional] The unique name of the tensor. e.g. 'mod.linear.weight'
6774
fully_qualified_name: string;
75+
76+
// [Optional] Specifies where the tensor's data is stored.
77+
// - SEGMENT (default): Data is stored in a segment.
78+
// - EXTERNAL: Data is stored outside of the PTE file. fully_qualified_name
79+
// must be non-empty, and is used as a key to find the tensor's external
80+
// data. Tensor.data_buffer_idx is ignored.
81+
location: TensorDataLocation;
6882
}
6983

7084
table Tensor {

0 commit comments

Comments
 (0)