-
Notifications
You must be signed in to change notification settings - Fork 24k
ptype propagation on numpy functions #22247
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
Conversation
…rch functions on Tensors (Parameters are unaffected).
…mpy functions on Tensors (Parameters are unaffected).
Looks like some builds failed (likely Python 2 compatibility issues). I'll get this fixed and submit another PR. |
I am not convinced that this is an API behavior that we should commit to supporting. I'm also concerned about the performance implications of THPVariable_result_ptype |
The actual diff here is a little confused, because it's mixed up with changes from #22235 which I definitely agree need some discussion about what exactly we want to support. However, the goal of this patch is just to make it so that you can pass a PyTorch tensor straight to numpy operations, and get out (non-backwards-supporting) PyTorch tensors at the end. This seems like a pretty harmless affordance that end users might like. Do you have an objection to this bit? |
@dylanbespalko I'm not sure I can properly review this PR if it also contains changes from #22235. Can these two PRs be setup so that they don't have any dependency on each other? |
Hi, NumPy maintainer here. Interesting PR, thanks for the ping @ezyang. I'll just make some high-level comments for now:
A variation of this works for ufuncs with More importantly, the logic in this PR seems exactly reversed from what it should be. This PR does something like:
It should be:
The whole idea of the NumPy array protocols is to allow using the NumPy API with minimal overhead, so users can write generic functions with only numpy calls in them, and those then work for |
Thanks for your much needed perspective on Numpy architecture. I think
I'm someone who is more likely to apply this stuff, rather than design it. My original goal here was to create two functions:
Since these types are not related by inheritance, neither solution is indeed "universal" in that:
The latter problem was the bottleneck for me. I decided that I needed to maintain a 90/10 torch/numpy mix in my own code, because it is impossible to deal with compatibility issues on a daily basis. I'm sure that the age of distributed hardware will make these decisions very tough, so any compatibility between these frameworks would be highly appreciated. |
yes indeed. that was a typo, I've edited my comment.
The question this raises is why you need a protocol for that. The
Okay then I understood your PR right. I think it's just not something that you should want - there's likely a better solution.
Mixing types of arrays/tensors like that is going to be inherently messy and inefficient. You're probably better off converting explicitly. |
Just a quick clarification: an ndarray can be converted into a Tensor without necessitating a data copy. So it's not as expensive as you seem to imply here. |
I was thinking mainly about tensors that live on the GPU. The but yes, point taken: should have said "potentially expensive" |
I'm late to the party, but isn't the method @perone explores in http://blog.christianperone.com/2019/07/numpy-dispatcher-when-numpy-becomes-a-protocol-for-an-ecosystem/ a way good to implement many of the functions without needing much from PyTorch? |
Hi @dylanbespalko! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks! |
Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as |
Fixes issue #17249
Added support for python subclass (ptype) propagation when calling numpy functions on Tensors (Parameters are unaffected).
I have addressed and responded to the comments on the previous PR. #18610. Please visit this link to see that issues that were resolved. As suggested the changes have been broken in to two PR:
A simple python script demonstrating ptype propagation is as follows: