-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: scalars missing several methods for array api compat #27305
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
Comments
So one issue with the in-place operators is that scalars are immutable. Maybe we could implement them but make them return a copy? But maybe a in-place operation returning a copy is confusing? Implementing |
This is the current behavior:
I think implementing I added a 2.2.0 milestone. I think For |
Setting up array-api-tests to run with scalars will probably be helpful as well. |
@ngoldbaum have we decided to do this ? |
@Ishankoradia go ahead. Having a milestone means we're planning to do it. We also don't claim issues, just go ahead and work on it. |
Gotcha !! @ngoldbaum I am looking at the |
No, in order to implement these functions you'll need to modify the C internals of NumPy. Here is where the numpy/numpy/_core/src/umath/scalarmath.c.src Lines 1974 to 1997 in f37a4f3
This is inside a file that is written in NumPy's custom templating language used for codegen internally. You can see that none of the For
If you've never touched the CPython C API before this is probably a big project, although tbh it would be a decent way to learn about the C internals of NumPy or how to work with Python C extensions using the C API directly. |
Thanks a ton @ngoldbaum , this helps a lot. |
I found going through https://docs.python.org/3/extending/extending.html and https://llllllllll.github.io/c-extension-tutorial/ helped immensely to understand this stuff better. |
Got it !! I will read through them before I dive in. Thank you for sharing them. |
@ngoldbaum I have spent a lot of time reading the material you shared. I have good understanding of how c extensions work. I built out 2(easy ones) and was able to run them from python I was looking at this template file you pointed out [updated] |
Hi, sorry for taking a few days to respond, I've been on vacation. The The templating for the block of code I linked to is set up immediately above that code: numpy/numpy/_core/src/umath/scalarmath.c.src Lines 1968 to 1973 in f37a4f3
Here, the template system is saying to replace In order to implement |
@ngoldbaum no problem. Thanks for getting back. (Also, hope you had a great vacation and got good time to recharge) So my first attempt was to use the same table. I tried to add a new entry there for But the mistake I did was, I implemented a method I have added this now and i see the compiled file
If you can clear these up , it would be great. Sorry for the back & forth. Thank you for all the help in this one. |
The ctype functions are used to define the "c-level" version of the operation. There's also a "python-level" version of the operation that is defined using a second level of templating to define a function named e.g. If you look up at my analysis when I originally opened the issue - numpy scalars already let you use the in-place operators, they just return a copy, so maybe in principle you can just re-use the existing implementations that are getting used already, but now by explicitly using the slot so people can call the dunder methods directly from Python.
I think that's right, although instead of asking me and then waiting for a response, you should try experimenting to see for yourself. I would need to poke around with a C debugger to figure that out. There are If you don't like debuggers, printf debugging also works :) |
Understood. Thank you Nathan !! |
Sorry for a late side comment, but reading this are we sure that this #27305 (comment) has been followed up:
Do other array libraries in fact do that for immutable scalars? I ask because certainly |
That is correct, inplace operators cannot and thus must not be implemented. |
Uh oh!
There was an error while loading. Please reload this page.
Describe the issue:
I keep getting stuck trying to get tests in https://github.com/data-apis/array-api-tests to pass and the final errors are often due to numpy arrays having turned into scalars and the tests not expecting that, (https://data-apis.org/array-api/draft/API_specification/array_object.html)
Reading #26850 it seems the intention is that the scalars should support the array api spec?
Reproduce the code example:
Error message:
No response
Python and NumPy Versions:
import sys, numpy; print(numpy.version); print(sys.version)
2.1.0
3.12.2 | packaged by conda-forge | (main, Feb 16 2024, 20:42:31) [MSC v.1937 64 bit (AMD64)]
Runtime Environment:
No response
Context for the issue:
No response
The text was updated successfully, but these errors were encountered: