Skip to content

MOD-14559 Fix float serialization precision for typed FPHA arrays#19

Merged
AvivDavid23 merged 6 commits intomasterfrom
MOD-14559
Mar 23, 2026
Merged

MOD-14559 Fix float serialization precision for typed FPHA arrays#19
AvivDavid23 merged 6 commits intomasterfrom
MOD-14559

Conversation

@AvivDavid23
Copy link

@AvivDavid23 AvivDavid23 commented Mar 18, 2026

When serializing typed float arrays, values were promoted to f64 (via INumber) before being passed to serde_json's serialize_f64. This caused serde_json's ryu formatter to produce unnecessarily long decimal strings, because ryu needs more digits to uniquely identify a value among all f64s than among all f32s.

For example, 0.3 stored in an F32 array would serialize as "0.30000001192092896" instead of "0.3".

The fix bypasses the IValue/INumber conversion for F32/F16/BF16 typed arrays during serialization, instead passing the raw float values directly to serde. This routes F32 values through serialize_f32 (and F16/BF16 through serialize_f32 via f32::from()), where ryu's f32 algorithm produces the shortest string that round-trips through f32 — matching user expectations.

Performance:

Version Mean avg (ms) Improvement
Before (original, promote to IValue/f64) 62.9 baseline
After (arithmetic round_to_sig_digits) 47.3 1.33x faster than original

Note

Medium Risk
Changes JSON serialization output for typed F32/F16/BF16 arrays, which may affect downstream consumers that relied on the previous (over-precise) decimal strings. Logic adds custom rounding/round-trip checks for half types, so edge cases around non-finite values and precision boundaries need review.

Overview
Fixes typed float array JSON serialization to avoid promoting elements through INumber/f64, which previously caused serde_json/ryu to emit overly long decimal strings (e.g. 0.3 becoming 0.30000001192092896).

IArray::serialize now special-cases ArraySliceRef::F32 to serialize elements as f32, and ArraySliceRef::F16/BF16 to compute the shortest decimal that round-trips through the stored half-precision value (via round_to_sig_digits + find_shortest_roundtrip_f64) before serializing. Adds unit tests covering short formatting, round-trip stability across F16/BF16/F32/F64, and expected precision-loss cases (including negatives).

Written by Cursor Bugbot for commit f630ed5. This will update automatically on new commits. Configure here.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

@AvivDavid23 AvivDavid23 requested a review from gabsow March 19, 2026 07:04
@gabsow
Copy link

gabsow commented Mar 23, 2026

run some preformence test

Copy link

@gabsow gabsow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run some pr

@AvivDavid23 AvivDavid23 requested a review from gabsow March 23, 2026 12:26
@AvivDavid23 AvivDavid23 merged commit 3dcd744 into master Mar 23, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants