16
16
DataEntry ,
17
17
DataPayload ,
18
18
DataSerializer ,
19
- TensorEntry ,
20
19
TensorLayout ,
21
20
)
22
21
@@ -29,22 +28,22 @@ def serialize_for_executorch(
29
28
emitter_output : EmitterOutput ,
30
29
config : ExecutorchBackendConfig ,
31
30
data_serializer : DataSerializer ,
32
- named_data : Optional [NamedDataStoreOutput ] = None ,
31
+ named_data_store : Optional [NamedDataStoreOutput ] = None ,
33
32
) -> Tuple [Cord , Dict [str , Cord ]]:
34
33
"""Serialize the output from Emitter into ExecuTorch artifacts; PTE and PTD files."""
35
34
36
35
# Serialize PTE file.
37
36
pte_named_data = None
38
37
if (
39
- named_data is not None
40
- and len (named_data .buffers ) > 0
41
- and len (named_data .pte_data ) > 0
38
+ named_data_store is not None
39
+ and len (named_data_store .buffers ) > 0
40
+ and len (named_data_store .pte_data ) > 0
42
41
):
43
42
# Create a separate NamedDataStoreOutput with only pte_data; exclude
44
43
# external_data, which shouldn't be serialized with the PTE file.
45
44
pte_named_data = NamedDataStoreOutput (
46
- buffers = named_data .buffers ,
47
- pte_data = named_data .pte_data ,
45
+ buffers = named_data_store .buffers ,
46
+ pte_data = named_data_store .pte_data ,
48
47
external_data = {},
49
48
)
50
49
pte : Cord = _serialize_pte_binary (
@@ -72,22 +71,23 @@ def serialize_for_executorch(
72
71
and tensor .extra_tensor_info .location is TensorDataLocation .EXTERNAL
73
72
):
74
73
fqn_to_tensor_layout [
74
+ # pyre-ignore Undefined attribute [16]: `Optional` has no attribute `fully_qualified_name`
75
75
tensor .extra_tensor_info .fully_qualified_name
76
76
] = TensorLayout (tensor .scalar_type , tensor .sizes , tensor .dim_order )
77
77
78
78
if len (fqn_to_tensor_layout ) == 0 and (
79
- named_data is None or len (named_data .external_data ) == 0
79
+ named_data_store is None or len (named_data_store .external_data ) == 0
80
80
):
81
81
return pte , ptd_files
82
82
83
83
# Consolidate tensors and opaque data with the same external tag so they
84
84
# can be saved to the same PTD.
85
85
all_external_tags : Set [str ] = set ()
86
- if named_data is not None and len (named_data .external_data ) > 0 :
86
+ if named_data_store is not None and len (named_data_store .external_data ) > 0 :
87
87
assert (
88
- len (named_data .buffers ) > 0
88
+ len (named_data_store .buffers ) > 0
89
89
), "External data exists, but there are no buffers provided."
90
- all_external_tags = set (named_data .external_data .keys ())
90
+ all_external_tags = set (named_data_store .external_data .keys ())
91
91
92
92
if len (fqn_to_tensor_layout ) > 0 :
93
93
# emitter_output.external_constant_map contains the mapping from
@@ -103,35 +103,38 @@ def serialize_for_executorch(
103
103
104
104
for tag in all_external_tags :
105
105
buffers = []
106
- fqn_to_tensor_entry : Dict [str , TensorEntry ] = {}
106
+ key_to_data_entry : Dict [str , DataEntry ] = {}
107
107
# pyre-ignore[16]: Undefined attribute: `Optional` has no attribute `get`.
108
108
fqn_to_index = emitter_output .external_constant_map .get (tag , {})
109
- # Create a TensorEntry for each external tensor.
109
+ # Create a DataEntry for each external tensor.
110
110
for fqn , index in fqn_to_index .items ():
111
111
assert fqn in fqn_to_tensor_layout
112
- fqn_to_tensor_entry [fqn ] = TensorEntry (
112
+ assert fqn not in key_to_data_entry # fqn must be unique
113
+ key_to_data_entry [fqn ] = DataEntry (
113
114
buffer_index = len (buffers ),
114
- layout = fqn_to_tensor_layout [fqn ],
115
+ alignment = config .constant_tensor_alignment ,
116
+ tensor_layout = fqn_to_tensor_layout [fqn ],
115
117
)
116
118
buffers .append (emitter_output .external_constant_buffer [index ])
117
119
118
120
# Extract external data.
119
- key_to_data : Dict [str , DataEntry ] = {}
120
121
# pyre-ignore[16]: Undefined attribute: `Optional` has no attribute `get`.
121
- key_to_buffer_index = named_data .external_data .get (tag , {})
122
+ key_to_buffer_index = named_data_store .external_data .get (tag , {})
122
123
for key , index in key_to_buffer_index .items ():
123
- # pyre-ignore[16]: Undefined attribute: `Optional` has no attribute `buffers`.
124
- key_to_data [key ] = DataEntry (
125
- len (buffers ), named_data .buffers [index ].alignment
124
+ assert key not in key_to_data_entry # key must be unique
125
+ key_to_data_entry [key ] = DataEntry (
126
+ buffer_index = len (buffers ),
127
+ # pyre-ignore[16]: Undefined attribute: `Optional` has no attribute `buffers`.
128
+ alignment = named_data_store .buffers [index ].alignment ,
129
+ tensor_layout = None ,
126
130
)
127
- buffers .append (named_data .buffers [index ].buffer )
131
+ buffers .append (named_data_store .buffers [index ].buffer )
128
132
129
133
# Serialize into PTD file.
130
134
ptd_files [tag ] = data_serializer .serialize (
131
135
DataPayload (
132
136
buffers = buffers ,
133
- fqn_to_tensor = fqn_to_tensor_entry ,
134
- key_to_data = key_to_data ,
137
+ named_data = key_to_data_entry ,
135
138
)
136
139
)
137
140
0 commit comments