-
Notifications
You must be signed in to change notification settings - Fork 50
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
Adding .view(...)
/reinterpret dtype
method
#266
Comments
This is different from |
One could do Because the memory is allocated to receive the message before any of that information arrives (it needs to be written somewhere in memory). Only after the metadata and data are stored, can they go through the deserialization process |
Equivalent to I think I understand the use case, but there's no way to get an array that's untyped in the API, so the "reinterpret memory" use case seems quite niche. And I expect that there will be libraries that don't allow this kind of thing, because memory layout is an implementation detail not exposed to the user. So I'm leaning towards "out of scope" here. It seems like serialization falls under I/O, which is out of scope completely. |
Not if Maybe a short example helps? Imagine In [1]: import numpy as np
In [2]: b = b"\x00\x00\x00\x00\x00\x00\x80?\x00\x00\x00@"
In [3]: np.asarray(memoryview(b)).view(np.float32)
Out[3]: array([0., 1., 2.], dtype=float32)
In [4]: np.asarray(memoryview(b), dtype=np.float32)
Out[4]:
array([ 0., 0., 0., 0., 0., 0., 128., 63., 0., 0., 0., 64.], dtype=float32)
In our usual case it is not so much that the data is untyped, but the type doesn't necessarily match what it should. Taking the example above, we have... In [6]: np.asarray(memoryview(b)).dtype
Out[6]: dtype('uint8') IOW we often have something that is
For clarity, am not looking to manipulate the underlying memory in any way and don't really care how it is represented. Am just trying to patch on the correct formatting. Another way to think of this would be altering the
It is certainly useful in I/O contexts (communication, file I/O, etc.). Though am not really looking for the protocol to handle the I/O portion or even serialization. Just the ability to perform this cast. |
Thanks, that is helpful. The "it has the wrong dtype" has come up in at least one other place I think, using DLPack to transfer bool arrays - those weren't supported, so it was done as uint8. I think the next step here is figure out how other array libraries do this (if they allow it). |
Another use case for reinterpretation is ability to convert to and from the underlying byte representation of floating-point numbers. This is common in the implementation of transcendental functions where you want to manipulate the underlying bits of a IEEE 754 floating-point number directly. Go, e.g., provides dedicated APIs for such reinterpretation ( The ability to reinterpret the underlying memory (i.e., have a data "view") can certainly be useful in certain classes of numerical algorithms and when you want to vectorize operations. The ability to reinterpret without needing to perform a copy would afford performance benefits. Currently, the only way to achieve reinterpretation according to the specification is via either (1) manual iteration and data copy or (2) a combination of |
cc @seberg (in case you have thoughts on this one :) |
For the use-case of reading blobs from the buffer protocol, I prefer the |
Think the main value of Whereas with |
As this proposal is currently without a champion, I'll go ahead and close. |
In NumPy (and some other libraries) arrays have a method to
view
the data as anotherdtype
. This is different fromastype
as this taking data that may not be typed likebytes
orbytearray
and applying differentdtype
metadata on top of it. As an example reinterpreting the data in this way can be useful particularly in distributed setting where the data goes through serialization/deserialization steps where metadata is extracted, sent along, and then reapply to the data. Though this can come up in other situations as well.cc @rgommers @kgryte (since we discussed this briefly earlier)
The text was updated successfully, but these errors were encountered: