Skip to content

Commit d3c23bf

Browse files
Gasoonjiafacebook-github-bot
authored andcommitted
get around of +/- inf issue when compile flatbuffer file (#2460)
Summary: Pull Request resolved: #2460 Reviewed By: guangy10 Differential Revision: D54961333 fbshipit-source-id: 31fc556aafa0421e7537f19dc2a367739f510003
1 parent 1346824 commit d3c23bf

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

exir/_serialize/_flatbuffer.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
import re
1212
import shutil
1313
import subprocess
14+
1415
import tempfile
1516

1617
from dataclasses import dataclass
1718
from typing import Callable, Dict, List, Optional, Sequence
1819

19-
2020
# If this environment variable is set to true, save the flatc input files when
2121
# serialization fails.
2222
_SAVE_FLATC_ENV: str = "ET_EXIR_SAVE_FLATC_INPUTS_ON_FAILURE"
@@ -29,6 +29,14 @@ def _is_valid_alignment(alignment: int) -> bool:
2929
return alignment > 0 and (alignment & (alignment - 1)) == 0
3030

3131

32+
# TODO(T182299196): Replace this hack with a proper flatc binary.
33+
def _replace_infinity_in_json_file(content: str) -> str:
34+
content = re.sub(
35+
r'"double_val"\s*:\s*(-)?Infinity', r'"double_val": "\g<1>inf"', content
36+
)
37+
return content
38+
39+
3240
def _patch_schema_alignment(
3341
schema: bytes,
3442
constant_tensor_alignment: Optional[int],
@@ -281,8 +289,11 @@ def _program_json_to_flatbuffer(
281289
json_path = os.path.join(temp_dir, file_stem + ".json")
282290
output_path = os.path.join(temp_dir, file_stem + ".pte")
283291

292+
# TODO(T182299196): Replace this hack with a proper flatc binary.
293+
replaced_program_json = _replace_infinity_in_json_file(program_json)
294+
284295
with open(json_path, "wb") as json_file:
285-
json_file.write(program_json.encode("ascii"))
296+
json_file.write(replaced_program_json.encode("ascii"))
286297

287298
try:
288299
_flatc_compile(temp_dir, schema_info.root_path, json_path)

0 commit comments

Comments
 (0)