Skip to content

Commit 77c82ad

Browse files
committed
[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: 257019860 @exported-using-ghexport Differential Revision: [D66523171](https://our.internmc.facebook.com/intern/diff/D66523171/)
1 parent 63238ab commit 77c82ad

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
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+
CONSTANT_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.CONSTANT_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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ enum TensorShapeDynamism : byte {
5353
DYNAMIC_UNBOUND = 2,
5454
}
5555

56+
// Indicates where a tensor is stored.
57+
enum TensorDataLocation : byte {
58+
// Stored in the constant segment of the PTE file.
59+
CONSTANT_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.
@@ -65,6 +72,12 @@ table ExtraTensorInfo {
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 is stored; either in the constant segment
77+
// (default case) or an external location. If a constant tensor is stored externally,
78+
// data_buffer_idx is not relevant; use extra_tensor_info.fully_qualified_name to
79+
// match up the external tensor.
80+
location: TensorDataLocation;
6881
}
6982

7083
table Tensor {

0 commit comments

Comments
 (0)