Skip to content

ENH: add a float32-only device#206

Merged
rgommers merged 10 commits into
data-apis:mainfrom
ev-br:f32_device
Jul 7, 2026
Merged

ENH: add a float32-only device#206
rgommers merged 10 commits into
data-apis:mainfrom
ev-br:f32_device

Conversation

@ev-br

@ev-br ev-br commented Apr 24, 2026

Copy link
Copy Markdown
Member

Add a new device which only supports single precision floats and does not support double precision floats.

The new device mimics torch "mps" device in that it does not have f64 but supports int64---unlike JAX which either has both or none.

  • Make inspection APIs (dtypes, default_dtypes) device-aware;
  • Make creation functions (ones, empty etc) use the device-specific default dtype when given dtype=None, device=f32_device

TODO:

  • Finish adding device-specific default dtypes to all creation functions
  • device-specific default dtypes for fft.{fftfreq, rfftfreq}
  • Audit the codebase for missing device= arguments in internal constructions

TBD:

  • re-purpose an existing device2 or add a new F32_device (if so, bikeshed the name)
  • standardize the behavior for incompatible arguments: dtype=float64, device=f32_only_device should raise? torch "mps" tensors raise a TypeError, follow it or mandate a ValueError?

Intends to close gh-64,
Gives a way to close gh-38 --- if we have a f32-only device, we probably do not need a global flag
Addresses a large part of gh-70

Cross-ref the spec RFC to allow for missing dtypes , data-apis/array-api#998 --- note that this array-api-strict PR can only land after the spec is updated;
Also cross-ref the test suite tracker data-apis/array-api-tests#431: the test suite is actually fairly far along.

EDIT: data-apis/array-api#1005 is I believe the spec update to make this spec-compliant.

Comment thread array_api_strict/_info.py
@betatim

betatim commented Apr 27, 2026

Copy link
Copy Markdown
Member

I had a quick look at the diff, I think this is going in a direction I like.

One thought to which I have no good answer: would it make sense to repurpose device2 as the one that doesn't support float64 instead of adding "yet another device"? A reason to reuse it is to avoid having many devices, a reason to have a new device is "explicit is better than implicit" (and it is basically free to add devices).

Interested what other think about this.

@ogrisel

ogrisel commented Apr 30, 2026

Copy link
Copy Markdown

I am fine with repurposing device2.

@ogrisel

ogrisel commented Apr 30, 2026

Copy link
Copy Markdown

I think it would make sense for the float32 only device to raise an exception on __setitem__ operations that trigger an implicit data transfer.

Note that this is not the case for PyTorch on the MPS devices for some reason:

>>> import torch
>>> a = torch.ones(10, device="mps")
>>> b = 2 * torch.ones(10)  # CPU
>>> a + b
Traceback (most recent call last):
  Cell In[9], line 1
    a + b
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, mps:0 and cpu!
>>> b[:5] = a[:5]  # implicit MPS to CPU transfer does not raise!
>>> b
tensor([1., 1., 1., 1., 1., 2., 2., 2., 2., 2.])

I would have expected the last statement to also raise RuntimeError for consistency. I think array-api-strict should raise in this case.

EDIT: actually, this is not related to dtypes. Maybe I should open a dedicated issue for this.

EDIT2: done: #207

@ev-br

ev-br commented May 25, 2026

Copy link
Copy Markdown
Member Author

Okay, this is ready from my side.

Re yet another device vs repurpose device2, the latter is more churn; the former works today and a new device is free, so am opting to adding a new device unless someone has a strong opinion to the contrary.

@ev-br ev-br changed the title WIP: add a float32-only device ENH: add a float32-only device May 29, 2026
ev-br and others added 4 commits June 30, 2026 17:26
@ev-br

ev-br commented Jun 30, 2026

Copy link
Copy Markdown
Member Author

Rebased on top of gh-212.
@betatim could I tempt you to take a look at this PR if you've the time?

@rgommers

rgommers commented Jul 6, 2026

Copy link
Copy Markdown
Member

This indeed seems useful, thanks for pushing this forward!

The new device mimics torch "mps" device in that it does not have f64 but supports int64---unlike JAX which either has both or none.

Also MLX on GPU: https://ml-explore.github.io/mlx/build/html/python/data_types.html

@betatim betatim 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.

I like having this device! Here some review comments.

We need some kind of nice name for this device

Comment thread array_api_strict/_devices.py Outdated
Comment thread array_api_strict/_devices.py
Comment thread array_api_strict/_devices.py Outdated
Comment thread array_api_strict/_devices.py Outdated
Comment thread array_api_strict/_creation_functions.py Outdated
Comment thread array_api_strict/_creation_functions.py Outdated
Comment thread array_api_strict/_creation_functions.py Outdated
Comment thread array_api_strict/_devices.py Outdated
Comment thread array_api_strict/_devices.py Outdated
Comment thread array_api_strict/tests/test_creation_functions.py
@ev-br

ev-br commented Jul 6, 2026

Copy link
Copy Markdown
Member Author

Thanks for the review @betatim ! I pushed an update which addressed most of your comments. Also took liberty to resolve those GH comments where an update seemed to just go with your suggestions, so that they don't distract from the few comments which are less clear-cut and where I'd be looking for more input.

This way, "device2" mimics a pytorch CPU device (supports f64,
defaults to f32), and "no_float64" mimics an MPS device (does
not support double precision at all).

While at it, fix the logic in asarray: whether a device does or does not
support a dtype is different from what is the default dtype for this device.
The the decision on the latter should not depend on the former.

@betatim betatim 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 for working on this and the rounds of nice review!

LGTM

@betatim

betatim commented Jul 7, 2026

Copy link
Copy Markdown
Member

@rgommers do you want to look at this or ponder things about it? Otherwise hit "merge" after reading this.

I will come back tomorrow or the day after to merge this if it is not yet merged.

@rgommers rgommers merged commit 34f301f into data-apis:main Jul 7, 2026
15 checks passed
@rgommers

rgommers commented Jul 7, 2026

Copy link
Copy Markdown
Member

Otherwise hit "merge" after reading this.

✅ 🚀

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.

Make one of the devices have a lower maximum float precision Allow changing the default dtypes

4 participants