Skip to content

Commit d985fdf

Browse files
committed
[executorch][schema] Add 'EXTERNAL' to DataLocation in schema
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. Differential Revision: [D66523171](https://our.internmc.facebook.com/intern/diff/D66523171/) ghstack-source-id: 256645340 Pull Request resolved: #7191
1 parent 8861b9a commit d985fdf

File tree

6 files changed

+40
-14
lines changed

6 files changed

+40
-14
lines changed

exir/emit/_emitter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,7 @@ def _get_empty_tensor_evalue() -> EValue:
11251125
data_buffer_idx=0,
11261126
allocation_info=None,
11271127
shape_dynamism=TensorShapeDynamism.STATIC,
1128+
location=DataLocation.SEGMENT,
11281129
)
11291130
)
11301131

exir/passes/replace_view_copy_with_view_pass.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ def __init__(self, base: TensorSpec, shape: List[int]) -> None:
109109
"mem_obj_id",
110110
"mem_offset",
111111
"dtype", # property
112+
"extra_tensor_info", # property
113+
"location", # property
112114
]
113115

114116
# Make sure _self_fields and _base_fields are disjoint

exir/schema.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ class ExtraTensorInfo:
5353
fully_qualified_name: Optional[str] = None
5454

5555

56+
class DataLocation(IntEnum):
57+
INLINE = 0
58+
SEGMENT = 1
59+
EXTERNAL = 2
60+
61+
5662
@dataclass
5763
class Tensor:
5864
scalar_type: ScalarType
@@ -66,6 +72,7 @@ class Tensor:
6672

6773
# check program.fbs for explanations.
6874
shape_dynamism: TensorShapeDynamism
75+
location: DataLocation
6976
extra_tensor_info: Optional[ExtraTensorInfo] = None
7077

7178

@@ -223,11 +230,6 @@ class FrameList:
223230
items: List[Frame]
224231

225232

226-
class DataLocation(IntEnum):
227-
INLINE = 0
228-
SEGMENT = 1
229-
230-
231233
@dataclass
232234
class BackendDelegateDataReference:
233235
location: DataLocation

exir/tensor.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
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 (
22+
DataLocation,
23+
ExtraTensorInfo,
24+
ScalarType,
25+
TensorShapeDynamism,
26+
)
2227
from executorch.exir.sym_util import eval_shape
2328

2429

@@ -132,6 +137,8 @@ def __init__(
132137
is_sparse: bool = False,
133138
const: bool = False,
134139
requires_grad: bool = False,
140+
extra_tensor_info: Optional[ExtraTensorInfo] = None,
141+
location: DataLocation = DataLocation.SEGMENT,
135142
) -> None:
136143
self.scalar_type = dtype
137144
self.const = const
@@ -146,6 +153,8 @@ def __init__(
146153
self.is_sparse = is_sparse
147154
self.init_mem_planning_fields()
148155
self.shape_dynamism: TensorShapeDynamism = determine_tensor_dynanism(self.shape)
156+
self.extra_tensor_info = extra_tensor_info
157+
self.location = location
149158

150159
@property
151160
def allocated_memory(self) -> int:
@@ -346,6 +355,8 @@ def to_list(
346355
allocation_info=allocation_info,
347356
layout=layout_enum(spec.layout),
348357
shape_dynamism=spec.shape_dynamism,
358+
location=spec.location,
359+
extra_tensor_info=spec.extra_tensor_info,
349360
)
350361
return flatbuffer_tensor
351362

exir/tests/common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
AllocationDetails,
1515
Chain,
1616
ContainerMetadata,
17+
DataLocation,
1718
EValue,
1819
ExecutionPlan,
1920
Instruction,
@@ -56,6 +57,7 @@ def get_test_program() -> Program:
5657
memory_offset_low=16,
5758
),
5859
shape_dynamism=TensorShapeDynamism.STATIC,
60+
location=DataLocation.SEGMENT,
5961
)
6062
),
6163
],

schema/program.fbs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ table ExtraTensorInfo {
6767
fully_qualified_name: string;
6868
}
6969

70+
// Indicates where a piece of data is stored.
71+
enum DataLocation : byte {
72+
// Stored directly in the flatbuffer.
73+
INLINE = 0,
74+
// Stored in a segment.
75+
SEGMENT = 1,
76+
// Stored outside of the flatbuffer.
77+
EXTERNAL = 2,
78+
}
79+
7080
table Tensor {
7181
scalar_type: ScalarType;
7282

@@ -142,6 +152,12 @@ table Tensor {
142152
// [Optional] Additional information about the Tensor that is not applicable
143153
// to most tensors.
144154
extra_tensor_info: ExtraTensorInfo;
155+
156+
// Specifies where the tensor is stored; either in a segment or external
157+
// location. If a constant tensor is stored externally, data_buffer_idx
158+
// is not relevant; use extra_tensor_info.fully_qualified_name to match up
159+
// the external tensor.
160+
location: DataLocation;
145161
}
146162

147163
table Int {
@@ -275,14 +291,6 @@ table FrameList {
275291
items: [Frame];
276292
}
277293

278-
// Indicates where a piece of data is stored.
279-
enum DataLocation : byte {
280-
// Stored directly in the flatbuffer.
281-
INLINE = 0,
282-
// Stored in a segment.
283-
SEGMENT = 1,
284-
}
285-
286294
// Indicates where the delegate data is stored
287295
table BackendDelegateDataReference {
288296
// Indicates which list to index into:

0 commit comments

Comments
 (0)