1111import re
1212
1313from dataclasses import dataclass
14- from typing import ClassVar , List , Literal , Optional , Tuple
14+ from typing import ClassVar , List , Optional , Tuple
1515
1616from executorch .exir ._serialize ._cord import Cord
1717from executorch .exir ._serialize ._dataclass import _DataclassEncoder , _json_to_dataclass
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+
2431from executorch .exir .schema import (
2532 BackendDelegateDataReference ,
2633 BackendDelegateInlineData ,
3340from 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-
4243def _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-
6654def _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-
233202def _get_extended_header (program_data : bytes ) -> Optional [_ExtendedHeader ]:
234203 """Returns the extended header of the program data, if present and valid."""
235204 try :
0 commit comments