Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Device object and device kwarg for creation #156

Closed
emcastillo opened this issue Mar 30, 2021 · 6 comments · Fixed by #259
Closed

Device object and device kwarg for creation #156

emcastillo opened this issue Mar 30, 2021 · 6 comments · Fixed by #259
Labels
Question General question. topic: Device Handling Device handling.
Milestone

Comments

@emcastillo
Copy link

Hi,

When reading the section regarding devices, I stumbled upon the following note:

The only way to obtain a Device object is from the .device property on the array object, hence there is no Device object in the array API itself that can be instantiated to point to a specific physical or logical device.

I understood this, as users can never instantiate a Device object themselves.
But in the paragraph inmediately above there are sentences like:

A device=None keyword for array creation functions, which takes an instance of a Device object.

A .to_device(device) method on the array object, with device again being a Device object, to move an array to a different device.

And both require a Device object, that according to the note, can't be directly instantiated, or be obtained from elsewhere than a created array.

@leofang
Copy link
Contributor

leofang commented Mar 30, 2021

That's right. AFAIK the intention is to use device=a.device, i.e. when you already have a compliant array a and so you can use its device attribute. As for how to determine the device for the very first array, it is currently listed as out of scope, and each library will use its existing strategy for the choice of the default device, performing device management, etc.

@rgommers
Copy link
Member

We first had a separate Device object, but after discussion found that there were too many differences between libraries to make it straightforward/sensible to standardize.

For library code, typically what you want to do is:

def somefunc(x, y, ...):
    device = x.device
    new_array = xp.array([...], device=device)
    if not x.device == y.device:
        # either raise exception, or
        y = y.to_device(device)

rather than using an explicit device = Device('cuda:0') or some such thing. The use of explicit devices is more appropriate for end user code, and there each library already has its own way of creating new arrays on a specific device.

@rgommers
Copy link
Member

Discussion on this API is in gh-96

@rgommers rgommers added the Question General question. label Mar 30, 2021
@emcastillo
Copy link
Author

Crystal clear!
Thanks😄

@leofang
Copy link
Contributor

leofang commented Apr 3, 2021

Feel free to reopen if you have further questions on the device aspect, @emcastillo!

@leofang
Copy link
Contributor

leofang commented Sep 13, 2021

As for how to determine the device for the very first array, it is currently listed as out of scope, and each library will use its existing strategy for the choice of the default device, performing device management, etc.

The use of explicit devices is more appropriate for end user code, and there each library already has its own way of creating new arrays on a specific device.

I think we should keep this issue open until we revise the description in question

The only way to obtain a `Device` object is from the `.device` property on
the array object, hence there is no `Device` object in the array API itself
that can be instantiated to point to a specific physical or logical device.

as it wasn't clear to either library developers nor end users what to do. In particular, for CuPy this implies the Device object needs to have a cpu flavor so that we can facilitate host-device transfer, a point I overlooked.

@leofang leofang reopened this Sep 13, 2021
@kgryte kgryte added this to the v2021 milestone Oct 4, 2021
@rgommers rgommers added the topic: Device Handling Device handling. label May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question General question. topic: Device Handling Device handling.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants