Read non-finite float values back from an ETDump - #22785
Open
Dev-next-gen wants to merge 1 commit into
Open
Conversation
deserialize_from_etdump_flatcc turns the binary into JSON with flatc and then parses it with json.loads. flatc writes non-finite float and double values as bare inf, -inf, nan and -nan, and json.loads accepts none of those, so an ETDump holding a single infinite or NaN value raised JSONDecodeError and the Inspector could not load it. The runtime logs double and float EValues into debug events as they are, so a -inf mask fill or a NaN output is enough to hit this. Rewrite those tokens into the Infinity, -Infinity and NaN spellings json.loads does accept before parsing, leaving string literals alone.
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/22785
Note: Links to docs will display an error until the docs builds have been completed.
|
This PR needs a
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
I tried to round-trip an ETDump whose debug event holds a
-infdouble, anddeserialize_from_etdump_flatccfailed withjson.decoder.JSONDecodeError: Expecting value. The same happens forinf,nan, and for aFloatvalue. A finite value round-trips fine.The function turns the binary into JSON with
flatc --json --strict-jsonand parses that withjson.loads. flatc writes non-finite values as bare tokens:"double_val": -inf,inf,nan, and-nanwhen the sign bit is set (which is what0.0 / 0.0gives on x86).json.loadsonly understandsInfinity,-InfinityandNaN, so a single non-finite value anywhere in the dump makes the whole file unreadable. The runtime logs double and float EValues into debug events as they are, so an attention mask filled with-infor a NaN intermediate is enough for the Inspector to be unable to load the ETDump, which is the moment you most want to look at it.The serialize direction already works since #22152, so this only touches the read side: before parsing, the flatc tokens are rewritten into the spellings
json.loadsaccepts. String literals are matched first and copied through, so an event or run named "inf" or "nan" is left untouched. Nothing else calls_deserialize_from_json_to_etdump_flatcc, and JSON without those tokens is unchanged.Test plan
I added
test_serialize_non_finite_floatstodevtools/etdump/tests/serialize_test.py. It putsinfin theFloatand-infin theDoubleof the sample dump and checks the round trip is equal, then checks anandouble comes back as NaN.Without the fix the new test errors with
JSONDecodeError: Expecting value: line 81 column 30; with it both tests pass (flatc 24.3.25 built from the pinnedthird-party/flatbufferscommit, Python 3.12, Linux). Separately I patched a negative NaN (0xFFF8000000000000) into a serialized dump, checked flatc prints it as-nan, and confirmed it now reads back as NaN, and that event names "inf" and "nan" survive the rewrite. I did not run lintrunner locally.This PR was written with the help of Claude Code.