Skip to content

Commit 384e7ed

Browse files
committed
[executorch][serialization] Refactor flatbuffer utils into separate file
For usage in extension/flat_tensor/serialize. Differential Revision: [D66854756](https://our.internmc.facebook.com/intern/diff/D66854756/) ghstack-source-id: 257299390 Pull Request resolved: #7254
1 parent e6aa3fa commit 384e7ed

File tree

3 files changed

+51
-39
lines changed

3 files changed

+51
-39
lines changed

exir/_serialize/TARGETS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ runtime.python_library(
3333
"_dataclass.py",
3434
"_flatbuffer.py",
3535
"_program.py",
36+
"utils.py",
3637
],
3738
resources = {
3839
"//executorch/schema:program.fbs": "program.fbs",

exir/_serialize/_program.py

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import re
1212

1313
from dataclasses import dataclass
14-
from typing import ClassVar, List, Literal, Optional, Tuple
14+
from typing import ClassVar, List, Optional, Tuple
1515

1616
from executorch.exir._serialize._cord import Cord
1717
from executorch.exir._serialize._dataclass import _DataclassEncoder, _json_to_dataclass
@@ -21,6 +21,13 @@
2121
_program_json_to_flatbuffer,
2222
)
2323

24+
from executorch.exir._serialize.utils import (
25+
_aligned_size,
26+
_HEADER_BYTEORDER,
27+
_pad_to,
28+
_padding_required,
29+
)
30+
2431
from executorch.exir.schema import (
2532
BackendDelegateDataReference,
2633
BackendDelegateInlineData,
@@ -33,12 +40,6 @@
3340
from executorch.exir.tensor import ALIGNMENT
3441

3542

36-
# Byte order of numbers written to program headers. Always little-endian
37-
# regardless of the host system, since all commonly-used modern CPUs are little
38-
# endian.
39-
_HEADER_BYTEORDER: Literal["little"] = "little"
40-
41-
4243
def _program_to_json(program: Program) -> str:
4344
"""Returns the JSON representation of the given Program."""
4445
return json.dumps(program, cls=_DataclassEncoder)
@@ -50,19 +51,6 @@ def _json_to_program(program_json: bytes) -> Program:
5051
return _json_to_dataclass(json.loads(program_json), cls=Program)
5152

5253

53-
def _padding_required(offset: int, alignment: int) -> int:
54-
"""Returns the padding required to align `offset` to `alignment`."""
55-
remainder: int = offset % alignment
56-
if remainder != 0:
57-
return alignment - remainder
58-
return 0
59-
60-
61-
def _aligned_size(input_size: int, alignment: int) -> int:
62-
"""Returns input_size padded up to the next whole multiple of alignment."""
63-
return input_size + _padding_required(input_size, alignment)
64-
65-
6654
def _insert_flatbuffer_header(
6755
flatbuffer_data: bytes, magic_regex: str, header_data: bytes
6856
) -> bytes:
@@ -211,25 +199,6 @@ def to_bytes(self) -> bytes:
211199
return data
212200

213201

214-
def _pad_to(data: bytes, length: int) -> bytes:
215-
"""Returns the input followed by enough zero bytes to become the requested length.
216-
217-
Args:
218-
data: The data to pad.
219-
length: The length of the returned data.
220-
Returns:
221-
The padded data.
222-
Raises:
223-
ValueError: If the requested length is less than the input length.
224-
"""
225-
if length < len(data):
226-
raise ValueError(f"Data length {len(data)} > padded length {length}")
227-
if length > len(data):
228-
data = data + b"\x00" * (length - len(data))
229-
assert len(data) == length
230-
return data
231-
232-
233202
def _get_extended_header(program_data: bytes) -> Optional[_ExtendedHeader]:
234203
"""Returns the extended header of the program data, if present and valid."""
235204
try:

exir/_serialize/utils.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
2+
3+
# pyre-strict
4+
5+
from typing import Literal
6+
7+
# Byte order of numbers written to program headers. Always little-endian
8+
# regardless of the host system, since all commonly-used modern CPUs are little
9+
# endian.
10+
_HEADER_BYTEORDER: Literal["little"] = "little"
11+
12+
13+
def _pad_to(data: bytes, length: int) -> bytes:
14+
"""Returns the input followed by enough zero bytes to become the requested length.
15+
16+
Args:
17+
data: The data to pad.
18+
length: The length of the returned data.
19+
Returns:
20+
The padded data.
21+
Raises:
22+
ValueError: If the requested length is less than the input length.
23+
"""
24+
if length < len(data):
25+
raise ValueError(f"Data length {len(data)} > padded length {length}")
26+
if length > len(data):
27+
data = data + b"\x00" * (length - len(data))
28+
assert len(data) == length
29+
return data
30+
31+
32+
def _padding_required(offset: int, alignment: int) -> int:
33+
"""Returns the padding required to align `offset` to `alignment`."""
34+
remainder: int = offset % alignment
35+
if remainder != 0:
36+
return alignment - remainder
37+
return 0
38+
39+
40+
def _aligned_size(input_size: int, alignment: int) -> int:
41+
"""Returns input_size padded up to the next whole multiple of alignment."""
42+
return input_size + _padding_required(input_size, alignment)

0 commit comments

Comments
 (0)