Skip to content

Add bits and smallest_normal to finfo - #3679

Merged
zcbenz merged 1 commit into
ml-explore:mainfrom
katlun-lgtm:finfo-conformance
Jun 16, 2026
Merged

zcbenz merged 1 commit into
ml-explore:mainfrom
katlun-lgtm:finfo-conformance

Conversation

@katlun-lgtm

Copy link
Copy Markdown
Contributor

Proposed changes

The array API finfo object requires bits (number of bits of the type) and smallest_normal (smallest positive normal number), in addition to the min/max/eps/dtype that mlx.core.finfo already exposes. This adds both.

It is also a practical prerequisite for running the conformance suite against MLX: hypothesis.extra.array_api reads finfo(dtype).bits and finfo(dtype).smallest_normal when generating float arrays, and currently raises AttributeError: 'mlx.core.finfo' object has no attribute 'bits', which aborts data generation for every float test.

Changes:

  • Add bits and smallest_normal to mlx::finfo (mlx/utils.h, mlx/utils.cpp) and to the Python binding (python/src/array.cpp).
  • Add min() (smallest positive normal) to the float16_t / bfloat16_t numeric_limits specializations in mlx/types/limits.h, which previously defined only lowest()/max()/epsilon(). float/double already inherit it from std::numeric_limits.
  • Extend test_finfo to cover bits and smallest_normal (vs NumPy for float32/float16, vs known IEEE values for bfloat16, and the real-component remap for complex64).

Resulting values:

dtype bits smallest_normal
float32 32 1.1754943508222875e-38
float16 16 6.103515625e-05
bfloat16 16 1.1754943508222875e-38 (2^-126)
float64 64 2.2250738585072014e-308
complex64 32 1.1754943508222875e-38 (reports the real component)

Checklist

  • I have read the CONTRIBUTING document
  • clang-format and black (the formatters configured in .pre-commit-config.yaml) report no changes on the modified files
  • Added tests (test_finfo)
  • Built from source and ran the tests locally: test_array.TestDtypes passes. Verified against data-apis/array-api-tests (ARRAY_API_TESTS_MODULE=mlx.core) that the previous finfo AttributeError no longer occurs and real-float elementwise tests (e.g. test_ceil, test_floor) now run and pass where they previously aborted during data generation

The array API finfo object requires `bits` (the number of bits of the
type) and `smallest_normal` (the smallest positive normal number), in
addition to the existing min/max/eps/dtype. These are also needed by
hypothesis' array API strategy (hypothesis.extra.array_api), which
reads `finfo(dtype).bits` and `finfo(dtype).smallest_normal` and
currently raises AttributeError against mlx.core.

Adds both fields to mlx::finfo and the Python binding. Also adds a
`min()` (smallest normal) to the float16/bfloat16 numeric_limits
specializations, which previously only defined lowest/max/epsilon.

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@zcbenz
zcbenz merged commit 694d1ef into ml-explore:main Jun 16, 2026
16 checks passed
@katlun-lgtm
katlun-lgtm deleted the finfo-conformance branch June 16, 2026 02:38
@BrewTestBot BrewTestBot mentioned this pull request Jul 7, 2026
1 task done
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