Skip to content

Fix generic Array API rounding exponents - #69

Merged
awf merged 3 commits into
graphcore-research:mainfrom
yashb98:fix/array-api-rounding
Nov 8, 2026
Merged

awf merged 3 commits into
graphcore-research:mainfrom
yashb98:fix/array-api-rounding

Conversation

@yashb98

@yashb98 yashb98 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Rounding 2.5 to E2M1 with array-api-strict currently returns 2.5, which the format cannot represent; scalar and NumPy rounding return 2.0. The generic _frexp fallback returns a base-2 order instead of the exponent expected by its caller. I propose correcting the exponent convention and checking estimates immediately below powers of two, while keeping the comparison finite at the largest input and preserving subnormal values.

The regression tests compare scalar, NumPy and strict results across float32/float64, all rounding modes, both saturation settings and three formats, with signed-zero, strided, duplicate and empty inputs. Separate exponent checks cover adjacent powers and finite extremes.

Testing:

  • Full Python 3.10 CPU suite: 1,966 passed, including notebooks. Identical baseline tests: 122 failed, 1,844 passed.
  • Full x86/ARM32 unit suites: 1,877 passed and three expected optional-framework module skips on each; identical baselines: 122 failed, 1,755 passed and the same skips.
  • Pre-commit, mypy and Sphinx passed.

The 32-bit runs used local PRoot/QEMU and unchanged ml_dtypes cross-built with GCC 14.2 and the guest headers; ARM32 pyzmq used Debian ZeroMQ.

@awf
awf requested a balanced review from Copilot November 6, 2026 22:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block the Array API exponent-convention fix.

No open issues 🎉

What changed in this PR

Corrects the generic Array API _frexp fallback so rounding uses the expected exponent convention, fixing unrepresentable results such as rounding 2.5 to E2M1.

Changes:

  • Adjusts exponent estimates near powers of two while keeping comparisons finite and preserving subnormal values.
  • Adds scalar, NumPy, and strict Array API parity tests across formats, rounding modes, saturation settings, and float32/float64 inputs.
  • Covers exponent boundaries, signed zero, strided and duplicate values, and empty arrays.
File Description
test/​test_array_api.py Adds exponent boundary checks and rounding regression tests.
src/​gfloat/​round_ndarray.py Corrects fallback exponents and handles estimates just below powers of two.

Review effort: Balanced

@awf

awf commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator

Thanks @yashb98 — nice spot, great tests. I've pushed a small follow-up commit that keeps your logic and results identical but rewrites the comment and comparison slightly.

@awf
awf marked this pull request as ready for review November 7, 2026 16:40
@yashb98

yashb98 commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for reviewing this and clarifying the comments. I've kept your rewrite and switched v_maybe_shifted back to division, since NumPy's ARM32 NEON multiplication path can flush float32 subnormals to zero. CI now passes on native, x86 and ARM32.

@awf

awf commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator

Thanks!

@awf
awf merged commit ee3b1aa into graphcore-research:main Nov 8, 2026
3 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.

3 participants