11
11
import re
12
12
13
13
from dataclasses import dataclass
14
- from typing import ClassVar , List , Literal , Optional , Tuple
14
+ from typing import ClassVar , List , Optional , Tuple
15
15
16
16
from executorch .exir ._serialize ._cord import Cord
17
17
from executorch .exir ._serialize ._dataclass import _DataclassEncoder , _json_to_dataclass
21
21
_program_json_to_flatbuffer ,
22
22
)
23
23
24
+ from executorch .exir ._serialize .utils import (
25
+ _aligned_size ,
26
+ _HEADER_BYTEORDER ,
27
+ _pad_to ,
28
+ _padding_required ,
29
+ )
30
+
24
31
from executorch .exir .schema import (
25
32
BackendDelegateDataReference ,
26
33
BackendDelegateInlineData ,
33
40
from executorch .exir .tensor import ALIGNMENT
34
41
35
42
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
-
42
43
def _program_to_json (program : Program ) -> str :
43
44
"""Returns the JSON representation of the given Program."""
44
45
return json .dumps (program , cls = _DataclassEncoder )
@@ -50,19 +51,6 @@ def _json_to_program(program_json: bytes) -> Program:
50
51
return _json_to_dataclass (json .loads (program_json ), cls = Program )
51
52
52
53
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
-
66
54
def _insert_flatbuffer_header (
67
55
flatbuffer_data : bytes , magic_regex : str , header_data : bytes
68
56
) -> bytes :
@@ -211,25 +199,6 @@ def to_bytes(self) -> bytes:
211
199
return data
212
200
213
201
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
-
233
202
def _get_extended_header (program_data : bytes ) -> Optional [_ExtendedHeader ]:
234
203
"""Returns the extended header of the program data, if present and valid."""
235
204
try :
0 commit comments