-
Notifications
You must be signed in to change notification settings - Fork 15
BUG: Revert dlpack device #221
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
744a5d4
Revert "raise if (device_type, device_id) is not found in the DLPack …
ev-br f92f9ba
Revert "add dlpack_device dunder to the F32_device"
ev-br 43edaaa
Revert "manually sync dlpack enum updates to _devices.py"
ev-br 1178e46
Manually fixup reverting dlpack_device additions
ev-br 21b4404
MAINT: remove a stale comment
ev-br File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could keep this test no? It seems like it checks something that is somewhat related to the DLPack device ID but also not related.
I'd keep it (unless I'm missing something)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An issue is that this test just did not work in 2.5? So if we're just reverting the change made in 2.6, the test is broken; if we also fix
from_dlpack, that's not just a revert...An attempt at a fix:
Looks simple and correct, right?
So one cannot just reuse the device of
x, one has to map the device ofxonto the device of array-api-strict! And the device naming is not standardized, so pretty much the only way to map them is via DLPACK enum value, but that's what we reverting here.TBH I don't see a way out, do you @betatim ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I thought that this test should "just work" is that both
xandyare array-api-strict arrays. But spending two minutes staring at the newly failing test in your comment doesn't really help me understand why your proposed fix shouldn't work.I think this means that this is more complicated than I think :D So fine to fix this in the future or never or realise that this isn't actually broken
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is confusing yes! And I had a head start to stare at this during EuroScipy. The bottom line AFAIU is this. Currently (in 2.5 and 2.6.1):
The failed fix from #221 (comment) was checking
hasattr(a, "device")which was a Yes, and then it attacheda.devicetob. Buta.deviceis a string'cpu' not anxp.Deviceinstance.Note that it currently works mainly by chance:
xp.from_dlpackuses the default device unless explicitly provided as a kwarg:So the pickle we're in for
xp.from_dlpack(x)is this:if hasattr(x, "device"): device=x.devicedance --- and then ifxis a foreign array, we attach a foreign device to an xp-array;device=Nonemeans the array-api-strict default device---and then ifxis an xp-array on a non-default device, we effectively transfer it to the default"CPU_DEVICE".Your test was guarding for the latter, but the fix was breaking the former.
Now, the 2.5 and 2.6.1 behavior, strictly speaking, is questionable in view of the spec requirement (emphasis mine)
So which array-api-strict device is the same as NumPy's
"cpu"device? And if"device1"is different, then how to tell it is different without somehow mapping the device values between different libraries?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naively (as an unsuspecting user) the behaviour that the standard describes makes sense. It is what I'd expect to happen.
That the device changes in:
seems super weird as a user because we don't change array library in the process.
I think attaching the device from a different array library to the result of
from_dlpackis the wrong thing to do. The standard says that how a device is specified is an implementation detail of the library (some use a string, some a class, etc). So attaching a pytorch device to a cupy array won't do anything useful, as none of the cupy code will understand it.I think the question of "how do you translate a device?" is the key. Can we use the device type and ID of the source array to find the corresponding device in array-api-strict?
So I'd adjust the device handling in
array-api-strict/array_api_strict/_creation_functions.py
Line 234 in f02a5da
__dlpack_device__()and see if there is a matching device in array-api-strict if the argumentdevice=None. My intuition would be that__dlpack_device__returns(1, 0)forDevice("CPU_DEVICE")and(1, 1)forDevice("device1"). Or something like that. As we learnt we can't change the type because that screws up things, but I think we can use the device ID to tell these otherwise similar devices apart.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed that the current behaviour is weird! And agreed about the key question, too. Any chance you've cycles to spare to prototype a solution? You seem to have a setup for downstream testing (in scikit-learn or where was that the previous attempt wrought havoc in?).