Skip to content

BUG,ENH: Relax finfo to be easier accessible for all user dtypes#31563

Open
seberg wants to merge 2 commits into
numpy:mainfrom
seberg:finfo-user-dtypes
Open

BUG,ENH: Relax finfo to be easier accessible for all user dtypes#31563
seberg wants to merge 2 commits into
numpy:mainfrom
seberg:finfo-user-dtypes

Conversation

@seberg

@seberg seberg commented Jun 4, 2026

Copy link
Copy Markdown
Member

This does two small tweaks:

  1. Remove the requirement for inexact scalar derivation (because I had not realized that this is problematic for bfloat16, etc.).
    • This makes the error message very slightly nicer for non-float finfo inputs, I expect that is OK. (This means we require all finfo fields to be filled in right now which quaddtype does as the only user, we could omit the decimal precision or so if someone asks for it.)
  2. For complex dtypes rather than hard-code ours, assume that arr.imag succeeds and use arr.imag.dtype when it is a view to find the "real" dtype. After that, we assume that finfo filling will succeed.

Together (with the "new dtype backporting") this will allow ml_dtypes to support np.finfo() fully.
(It is very annoying to test it here, but we run ml_dtypes tests now and they will test it as soon as we start using it there.)

(No AI use.)

CC @SwayamInSync since you were involved in the first part. I don't think requiring all "finfo" fields that we currently have in C to be filled should be a problem, right?
(If that changes, there is always the work-around to fill it with NaN in practice, probably.)

(I find the obj2sctype annoying and potentially fishy even for quaddtype since it is slightly parametric(?), but that part I don't dare to touch here.)


FWIW, I would like to consider this a bug-fix and backport. The only change should be the error message change in practice.

This does two small tweaks:
1. Remove the requirement for `inexact` scalar derivation (because I had
   not realized that this is problematic for bfloat16, etc.).
   * This makes the error message very slightly nicer for non-float
     finfo inputs, I expect that is OK.
     (This means we require all finfo fields to be filled in right now
     which quaddtype does as the only user, we could omit the decimal
     precision or so if someone asks for it.)
2. For complex dtypes rather than hard-code ours, assume that `arr.imag`
   succeeds and use `arr.imag.dtype` when it is a view to find the "real"
   dtype.
   After that, we assume that `finfo` filling will succeed.

Together (with the "new dtype backporting") this will allow `ml_dtypes`
to support `np.finfo()` fully.
(It is very annoying to test it here, but we run `ml_dtypes` tests now
and they will test it as soon as we start using it there.)
@seberg seberg added the 09 - Backport-Candidate PRs tagged should be backported label Jun 4, 2026
Comment thread numpy/_core/getlimits.py
try:
arr = numeric.empty(1, dtype=dtype)
imag_part = arr.imag
real_part = arr.real

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

To be clear, my first plan for this function was to guard it with the AbstractDType hierarchy, but since that is stalled behind heap-types, etc. and the .imag path is good and used either way, that guard felt a bit unnecessary.
(ml_dtypes cannot inherit from the scalar hierarchy easily, I think because it would mean inheriting the scalar methods and they make wrong assumptions.)

@SwayamInSync

Copy link
Copy Markdown
Member

I don't think requiring all "finfo" fields that we currently have in C to be filled should be a problem, right?

Yes, no problem, we return 1 for all six now-mandatory float constants (and the four int ones), so quaddtype is unaffected.
And yeah there bf16 is inherited from generic, I think it'll be okay to remove the finfo from dtype class hierarchy dependency and make a developer choice.

@SwayamInSync

Copy link
Copy Markdown
Member

(I find the obj2sctype annoying and potentially fishy even for quaddtype since it is slightly parametric(?), but that part I don't dare to touch here.)

I didn't thought about it earlier but yeah that logic routes the input to default scalar type. I think we can leave obj2sctype as it is (it does what it meant) and add a caching path in finfo.new that preserve the backend info? (out of scope for this PR, but worth discussing)

@seberg

seberg commented Jun 8, 2026

Copy link
Copy Markdown
Member Author

Actually, pushed to use np.result_type(dtype) rather than the current code instead. Basically, by this time we have a dtype instance, so the only thing left to do is to normalize away the byte-order.
np.result_type() does that better than obj2sctype, so we can just do that. And while not needed, it seems rather OK to just do it here.

@charris charris closed this Jun 9, 2026
@charris charris reopened this Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

09 - Backport-Candidate PRs tagged should be backported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants