-
Notifications
You must be signed in to change notification settings - Fork 992
Expand file tree
/
Copy pathserialize.py
More file actions
390 lines (335 loc) · 14.4 KB
/
serialize.py
File metadata and controls
390 lines (335 loc) · 14.4 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# 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-strict
import json
import os
import tempfile
from dataclasses import dataclass
from typing import ClassVar, Dict, List, Literal, Optional, Sequence
import pkg_resources
from executorch.exir._serialize._cord import Cord
from executorch.exir._serialize._dataclass import _DataclassEncoder, _json_to_dataclass
from executorch.exir._serialize._flatbuffer import _flatc_compile, _flatc_decompile
from executorch.exir._serialize._program import _insert_flatbuffer_header
from executorch.exir._serialize.data_serializer import (
DataPayload,
DataSerializer,
TensorEntry,
)
from executorch.exir._serialize.padding import aligned_size, pad_to, padding_required
from executorch.extension.flat_tensor.serialize.flat_tensor_schema import (
DataSegment,
FlatTensor,
TensorMetadata,
)
# Byte order of numbers written to flat tensor headers. Always little-endian
# regardless of the host system, since all commonly-used modern CPUs are little
# endian.
_HEADER_BYTEORDER: Literal["little"] = "little"
def _serialize_to_flatbuffer(flat_tensor: FlatTensor) -> Cord:
"""Serializes a FlatTensor to a flatbuffer and returns the serialized data."""
flat_tensor_json = json.dumps(flat_tensor, cls=_DataclassEncoder)
with tempfile.TemporaryDirectory() as d:
schema_path = os.path.join(d, "flat_tensor.fbs")
with open(schema_path, "wb") as schema_file:
schema_file.write(
pkg_resources.resource_string(__name__, "flat_tensor.fbs")
)
scalar_type_path = os.path.join(d, "scalar_type.fbs")
with open(scalar_type_path, "wb") as scalar_type_file:
scalar_type_file.write(
pkg_resources.resource_string(__name__, "scalar_type.fbs")
)
json_path = os.path.join(d, "flat_tensor.json")
with open(json_path, "wb") as json_file:
json_file.write(flat_tensor_json.encode("ascii"))
_flatc_compile(d, schema_path, json_path)
output_path = os.path.join(d, "flat_tensor.ptd")
with open(output_path, "rb") as output_file:
return Cord(output_file.read())
def _deserialize_to_flat_tensor(flatbuffer: bytes) -> FlatTensor:
"""Deserializes a flatbuffer to a FlatTensor and returns the dataclass."""
with tempfile.TemporaryDirectory() as d:
schema_path = os.path.join(d, "flat_tensor.fbs")
with open(schema_path, "wb") as schema_file:
schema_file.write(
pkg_resources.resource_string(__name__, "flat_tensor.fbs")
)
scalar_type_path = os.path.join(d, "scalar_type.fbs")
with open(scalar_type_path, "wb") as scalar_type_file:
scalar_type_file.write(
pkg_resources.resource_string(__name__, "scalar_type.fbs")
)
bin_path = os.path.join(d, "flat_tensor.bin")
with open(bin_path, "wb") as bin_file:
bin_file.write(flatbuffer)
_flatc_decompile(d, schema_path, bin_path, ["--raw-binary"])
json_path = os.path.join(d, "flat_tensor.json")
with open(json_path, "rb") as output_file:
return _json_to_dataclass(json.load(output_file), cls=FlatTensor)
@dataclass
class FlatTensorConfig:
tensor_alignment: int = 16
segment_alignment: int = 16
@dataclass
class FlatTensorHeader:
# Class constants.
# The magic bytes that should be at the beginning of the header.
# This should be in sync with the magic in
# executorch/extension/flat_tensor/serialize/flat_tensor_header.h
EXPECTED_MAGIC: ClassVar[bytes] = b"FH01"
EXPECTED_LENGTH: ClassVar[int] = (
# Header magic
4
# Header length
+ 4
# Flatbuffer offset
+ 8
# Flatbuffer data size
+ 8
# Segment base offset
+ 8
# Data size
+ 8
)
# Instance attributes. @dataclass will turn these into ctor args.
# Offset to the start of the flatbuffer data, in bytes.
flatbuffer_offset: int
# The size of the serialized data in bytes.
flatbuffer_size: int
# Offset to the start of the first segment, or zero if there
# are no segments.
segment_base_offset: int
# Size of all the segment data, in bytes.
segment_data_size: int
# The magic bytes read from or to be written to the binary header.
magic: bytes = EXPECTED_MAGIC
# The header length, in bytes, read from or to be written to the binary
# header.
length: int = EXPECTED_LENGTH
@staticmethod
def from_bytes(data: bytes) -> "FlatTensorHeader":
"""Tries to read an flat_tensor header from the provided data.
Does not validate that the header is well-formed. Callers should
use is_valid().
Args:
data: The data to read from.
Returns:
The contents of the flat_tensor header.
Raises:
ValueError: If not enough data is provided.
"""
if len(data) < FlatTensorHeader.EXPECTED_LENGTH:
raise ValueError(
f"Not enough data for flat_tensor header: {len(data)} "
+ f"< {FlatTensorHeader.EXPECTED_LENGTH}"
)
return FlatTensorHeader(
magic=data[0:4],
length=int.from_bytes(data[4:8], byteorder=_HEADER_BYTEORDER),
flatbuffer_offset=int.from_bytes(data[8:16], byteorder=_HEADER_BYTEORDER),
flatbuffer_size=int.from_bytes(data[16:24], byteorder=_HEADER_BYTEORDER),
segment_base_offset=int.from_bytes(
data[24:32], byteorder=_HEADER_BYTEORDER
),
segment_data_size=int.from_bytes(data[32:40], byteorder=_HEADER_BYTEORDER),
)
def is_valid(self) -> bool:
"""Returns true if the flat_tensor header appears to be well-formed."""
return (
self.magic == FlatTensorHeader.EXPECTED_MAGIC
and self.length >= FlatTensorHeader.EXPECTED_LENGTH
)
def to_bytes(self) -> bytes:
"""Returns the binary representation of the flat_tensor header.
Note that this will ignore self.magic and self.length and will always
write the proper magic/length.
"""
data: bytes = (
# Extended header magic. This lets consumers detect whether the
# header was inserted or not. Always use the proper magic value
# (i.e., ignore self.magic) since there's no reason to create an
# invalid header.
self.EXPECTED_MAGIC
# uint32_t: Size of this header. This makes it easier to add new
# fields to this header in the future. Always use the proper size
# (i.e., ignore self.length) since there's no reason to create an
# invalid header.
+ self.EXPECTED_LENGTH.to_bytes(4, byteorder=_HEADER_BYTEORDER)
# uint64_t: Offset to the start of the flatbuffer data, in bytes.
+ self.flatbuffer_offset.to_bytes(8, byteorder=_HEADER_BYTEORDER)
# uint64_t: Size of the serialized data in bytes.
+ self.flatbuffer_size.to_bytes(8, byteorder=_HEADER_BYTEORDER)
# uint64_t: Offset to the start of the first segment, or zero if
# there are no segments.
+ self.segment_base_offset.to_bytes(8, byteorder=_HEADER_BYTEORDER)
# uint64_t: Size of all the segment data, in bytes.
+ self.segment_data_size.to_bytes(8, byteorder=_HEADER_BYTEORDER)
)
return data
def _get_extended_header(flat_tensor_data: bytes) -> Optional[FlatTensorHeader]:
"""Returns the extended header of the flat_tensor data, if present and valid."""
try:
eh = FlatTensorHeader.from_bytes(flat_tensor_data[8:])
if eh.is_valid():
return eh
except ValueError:
pass
return None
def _extract_tensors(
fqn_to_tensor: Dict[str, TensorEntry],
buffers: Sequence[bytes],
segments: List[Cord],
tensor_alignment: int,
) -> List[TensorMetadata]:
"""Places tensors into a single segment, aligned to tensor_alignment within
the segment.
Args:
fqn_to_tensor: A map from fully qualified names to tensor entries.
buffers: A sequence of tensor buffers.
segments: A list of segments to append the tensor data to. Modified in-place.
tensor_alignment: The alignment of the tensor data.
Returns:
A list of TensorMetadata, which describes the tensors in the segment.
"""
tensor_data: Cord = Cord()
tensors: List[TensorMetadata] = []
# {idx, offset}
saved_offsets: Dict[int, int] = {}
for fqn, tensor_entry in fqn_to_tensor.items():
assert tensor_entry.layout is not None
# Check index into the tensor buffers is valid.
assert tensor_entry.buffer_index < len(
buffers
), f"Invalid index {tensor_entry.buffer_index} is greater than tensor buffer size {len(buffers)}."
# Check if the tensor has already been appended to the flat_tensor_data.
offset = saved_offsets.get(tensor_entry.buffer_index, -1)
if offset == -1:
if len(tensor_data) > 0:
# Add padding to round off the previous tensor offset.
pad_length = padding_required(len(tensor_data), tensor_alignment)
tensor_data.append(b"\x00" * pad_length)
# Add to saved offsets.
offset = len(tensor_data)
saved_offsets[tensor_entry.buffer_index] = offset
# Append to flat_tensor_data at the offset.
tensor_data.append(buffers[tensor_entry.buffer_index])
tensors.append(
TensorMetadata(
fully_qualified_name=fqn,
scalar_type=tensor_entry.layout.scalar_type,
sizes=tensor_entry.layout.sizes,
dim_order=tensor_entry.layout.dim_order,
segment_index=len(segments),
offset=offset,
)
)
segments.append(tensor_data)
return tensors
class FlatTensorSerializer(DataSerializer):
"""A concrete implementation of the DataSerializer interface that
serializes and deserializes data to/from the FlatTensor format.
"""
def __init__(self, config: Optional[FlatTensorConfig] = None) -> None:
"""FlatTensorConfig holds information required for serialization,
eg. alignment.
"""
if config is None:
self.config: FlatTensorConfig = FlatTensorConfig()
else:
self.config: FlatTensorConfig = config
def serialize(
self,
data: DataPayload,
) -> Cord:
"""Serializes a list of tensors and named data into a blob."""
segments: List[Cord] = []
tensors = _extract_tensors(
data.fqn_to_tensor,
data.buffers,
segments,
self.config.tensor_alignment,
)
data_segments: List[DataSegment] = []
segment_data = Cord()
for segment in segments:
prev_end = (
(data_segments[-1].offset + data_segments[-1].size)
if data_segments
else 0
)
data_segments.append(
DataSegment(
offset=aligned_size(prev_end, self.config.segment_alignment),
size=len(segment),
)
)
# Pad segment_data to segment alignment.
segment_pad_length = padding_required(
len(segment_data), self.config.segment_alignment
)
if segment_pad_length > 0:
segment_data.append(b"\x00" * segment_pad_length)
segment_data.append(segment)
# Create FlatTensor, which describes of the contents of the file and
# points to all the data segments. It will be serialized to flatbuffer.
flat_tensor = FlatTensor(
version=0, # Keep in sync with c++ version number in serialize.h
tensor_alignment=self.config.tensor_alignment,
tensors=tensors,
segments=data_segments,
named_data=[],
)
flatbuffer_payload = _serialize_to_flatbuffer(flat_tensor)
padded_flatbuffer_length: int = aligned_size(
input_size=len(flatbuffer_payload),
alignment=self.config.tensor_alignment,
)
padded_header_length: int = aligned_size(
input_size=FlatTensorHeader.EXPECTED_LENGTH,
alignment=self.config.tensor_alignment,
)
segment_base_offset = aligned_size(
padded_flatbuffer_length + padded_header_length,
self.config.segment_alignment,
)
# Create FlatTensorHeader, which stores the offsets and sizes of the
# FlatTensor flatbuffer and the segment data.
header_data: bytes = FlatTensorHeader(
flatbuffer_offset=padded_header_length,
flatbuffer_size=len(flatbuffer_payload),
segment_base_offset=segment_base_offset,
segment_data_size=len(segment_data),
).to_bytes()
# Pad header and payload to segment alignment.
header_data = pad_to(header_data, padded_header_length)
original_flatbuffer_payload_size = len(flatbuffer_payload)
flatbuffer_payload.append(
b"\x00" * (padded_flatbuffer_length - len(flatbuffer_payload))
)
injected_flatbuffer_data: bytes = _insert_flatbuffer_header(
flatbuffer_data=flatbuffer_payload.__bytes__(),
magic_regex=r"FT[0-9a-zA-Z][0-9a-zA-Z]",
header_data=header_data,
)
eh = _get_extended_header(injected_flatbuffer_data)
assert eh is not None
assert eh.flatbuffer_size == original_flatbuffer_payload_size
assert eh.segment_base_offset == segment_base_offset
assert eh.flatbuffer_offset == padded_header_length
assert eh.segment_data_size == len(segment_data)
del header_data
del flatbuffer_payload
# Place everything into one segment.
payload = Cord()
payload.append(injected_flatbuffer_data)
payload.append(segment_data)
return payload
def deserialize(self, blob: Cord) -> DataPayload:
"""
Deserializes a flat_tensor blob into a list of tensor metadata and tensors.
"""
raise NotImplementedError("deserialize_data")