Skip to content

Commit 1dc55fe

Browse files
committed
PR: fallback remove + comments
Signed-off-by: Chizkiyahu Raful <chizkiyahu.raful@arm.com> Change-Id: If9f6d3ccc3d5d7cebd69306b7894df16bb4a9088
1 parent 3039600 commit 1dc55fe

3 files changed

Lines changed: 8 additions & 40 deletions

File tree

exir/_serialize/_flatbuffer_program.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,8 @@ def _convert_double(val: Double) -> Any:
128128
result = _flatbuffer_t_class(Double)()
129129
double_val = val.double_val
130130
if isinstance(double_val, str):
131-
if double_val == "inf":
132-
result.doubleVal = float("inf")
133-
elif double_val == "-inf":
134-
result.doubleVal = float("-inf")
135-
else:
136-
result.doubleVal = float(double_val)
131+
# the string is read as a Union of float and string (see exir/schema.py).
132+
result.doubleVal = float(double_val)
137133
else:
138134
result.doubleVal = double_val
139135
return result

exir/_serialize/_program.py

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import copy
1111
import json
12-
import logging
1312
import math
1413
import re
1514

@@ -21,7 +20,6 @@
2120
from executorch.exir._serialize._flatbuffer import (
2221
_FlatbufferResult,
2322
_program_flatbuffer_to_json,
24-
_program_json_to_flatbuffer,
2523
)
2624
from executorch.exir._serialize._flatbuffer_program import _program_to_flatbuffer
2725
from executorch.exir._serialize._named_data_store import (
@@ -51,8 +49,6 @@
5149
# endian.
5250
_HEADER_BYTEORDER: Literal["little"] = "little"
5351

54-
logger = logging.getLogger(__name__)
55-
5652

5753
@dataclass
5854
class PTEFile:
@@ -423,35 +419,6 @@ def _extract_named_data(
423419
program.named_data = named_data
424420

425421

426-
def _program_to_flatbuffer_with_fallback(
427-
program: Program,
428-
*,
429-
constant_tensor_alignment: int,
430-
delegate_alignment: Optional[int],
431-
) -> _FlatbufferResult:
432-
"""
433-
Serializes the Program into a FlatBuffer, with a JSON fallback for robustness.
434-
435-
The FlatBuffer serialization path is the preferred fast path, offering
436-
significantly better runtime performance and lower memory usage in benchmarks.
437-
The JSON path is retained solely as a fallback to ensure robustness in cases
438-
where FlatBuffer serialization fails.
439-
"""
440-
try:
441-
return _program_to_flatbuffer(
442-
program,
443-
constant_tensor_alignment=constant_tensor_alignment,
444-
delegate_alignment=delegate_alignment,
445-
)
446-
except Exception:
447-
logger.exception("FlatBuffer serialization failed. Falling back to JSON path.")
448-
return _program_json_to_flatbuffer(
449-
_program_to_json(program),
450-
constant_tensor_alignment=constant_tensor_alignment,
451-
delegate_alignment=delegate_alignment,
452-
)
453-
454-
455422
def serialize_pte_binary(
456423
pte_file: PTEFile,
457424
*,
@@ -557,7 +524,9 @@ def serialize_pte_binary(
557524
segments_data.append(segment.data)
558525

559526
# Convert to a standard flatbuffer binary.
560-
result: _FlatbufferResult = _program_to_flatbuffer_with_fallback(
527+
# Prefer the direct flatbuffer path to avoid JSON+flatc roundtrip,
528+
# which reduces CPU/memory and avoids temp file I/O.
529+
result: _FlatbufferResult = _program_to_flatbuffer(
561530
program,
562531
constant_tensor_alignment=constant_tensor_alignment,
563532
delegate_alignment=delegate_alignment,

exir/_serialize/test/test_flatbuffer_program.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ def _make_program(self) -> Program:
6969
EValue(Int(1)),
7070
EValue(Bool(True)),
7171
EValue(Double(float("inf"))),
72+
EValue(Double(float("-inf"))),
73+
EValue(Double(float("5e-10"))),
74+
EValue(Double(4.5)),
7275
EValue(String("hello")),
7376
EValue(IntList([1, 2, 3])),
7477
EValue(DoubleList([1.5, 2.5])),

0 commit comments

Comments
 (0)