-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Quick and dirty matmul #5878
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
Quick and dirty matmul #5878
Conversation
0c9e8fd
to
8b52f96
Compare
Yeah, for a class defined in C you have to fill in the new |
@njsmith Thought so, but I couldn't find it in a quick grep through the Python source. Now I need to worry about left and right and NotImplented. Ugh. |
And it means it need to be tested with Python3.5. |
} | ||
|
||
errval = PyUFunc_CheckOverride(cached_npy_matmul, "__call__", | ||
args, kwds, &override, 2); |
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.
I feel like PyArray_Matmul
should also do the override check, i.e. it should have identical semantics to the matmul
function?
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.
That raises the question if it should be in the C-API at this point. I'm tempted to leave it out and just go with the matmul
method.
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.
I was also feeling a little hesitant about adding it to the C api, so +1 on
waiting until we're more sure what we're doing here.
On May 14, 2015 11:32 PM, "Charles Harris" notifications@github.com wrote:
In numpy/core/src/multiarray/multiarraymodule.c
#5878 (comment):
- PyObject *v, *a, *o = NULL;
- char* kwlist[] = {"a", "b", "out", NULL };
- if (cached_npy_matmul == NULL) {
PyObject *module, *dict, *matmul;
module = PyImport_ImportModule("numpy.core.multiarray");
dict = PyModule_GetDict(module);
matmul = PyDict_GetItemString(dict, "matmul");
cached_npy_matmul = (PyUFuncObject*)matmul;
Py_INCREF(cached_npy_matmul);
Py_DECREF(module);
- }
- errval = PyUFunc_CheckOverride(cached_npy_matmul, "call",
args, kwds, &override, 2);
That raises the question if it should be in the C-API at this point. I'm
tempted to leave it out and just go with the matmul method.—
Reply to this email directly or view it on GitHub
https://github.com/numpy/numpy/pull/5878/files#r30387829.
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.
Removed.
8b52f96
to
8cda72b
Compare
Works in Python3.5 now.
|
Woohoo! Should probably be a ValueError I guess?
|
8cda72b
to
f347bae
Compare
ValueError used. |
Seems to work with stacks as it should.
|
f347bae
to
f981ef2
Compare
Can you remind why this cannot be implemented as a gufunc, which would get |
@pv It can be, I have an old branch that makes a start on that, which is why this is quick and dirty ;) |
f981ef2
to
70134f8
Compare
I don't object to shipping something quick-and-dirty in 1.10, but I'll note On Fri, May 15, 2015 at 7:01 AM, Charles Harris notifications@github.com
Nathaniel J. Smith -- http://vorpus.org |
To kee
8000
p track of the optional dimensions we need to add more items to the |
While hiding it away is doubtless a good idea, we're already adding new items to |
I actually wrote four gufuncs for this last year, together with some changes to the ufunc generator to make them easier to declare. Ideally BLAS needs to always be available, so we really need the This PR provides tests and a chance for people to fool with the operator and provide feedback, I don't think Python 3.5 will be standard for another year or so. One problem I've run into is that the operator overrides aren't working as they should, although the matmul function does just fine. I was looking into that when a big chunk of my cpu memory crapped out, so until I put in new memory and get hold the work sitting on disk I'm a bit crippled, probably until Wednesday or so if I'm lucky. |
The problem is that it's pretty fragile to try patching this together out On Mon, May 18, 2015 at 12:59 AM, Charles Harris notifications@github.com
Nathaniel J. Smith -- http://vorpus.org |
70134f8
to
9f6cf44
Compare
9264b19
to
020a156
Compare
I think this is ready to go pending tests. The override mechanism is the same as currently implemented for other functions, we can fix it up when the issue is decided and a transition path laid out. |
Take that back, there are still some problems with the operator. |
020a156
to
aafe932
Compare
Should all be fixed now. |
aafe932
to
8e8b3b3
Compare
I'm going to merge this in a day or two if noone beets me to it and there are no strenuous objections. |
The function was not static, which led to multiple definition errors.
This is the functional counterpart of the '@' operator that will be available in Python 3.5 with the addition of an out keyword. It operates like the dot function except that - scalar multiplication is not allowed. - multiplication of arrays with more than 2 dimensions broadcasts. The last means that when arrays have more than 2 dimensions they are treated as stacks of matrices and those stacks are broadcast against each other unlike the current behavior of dot that does an outer product. Like dot, matmul is aware of `__numpy_ufunc__` and can be overridden. The current version of the function uses einsum when cblas cannot be used, hence object arrays are not yet supported.
Start labels at beginning of line. Break a long line Whitespace.
Document the matmul function and add '@' to the operator section of the reference manual.
8e8b3b3
to
24fcc25
Compare
Adds
__matmul__
and__rmatmul__
. This is a work in progress and currently lacks__imatmul__
and tests. The methods seem to work, but python3.5 fails to call them for the@
operator. I suspect this may be related to ndarray being a class defined in C, as a subclass with the operators added does seem to work.The methods use einsum for the stacked case and types not available in cblas, consequently they do not work for object arrays. However, they are faster for integer arrays of non-trivial size than the current dot.