Skip to content

BUG: test, fix matmul, dot for vector array with stride[i]=0 - #12494

Merged
charris merged 2 commits into
numpy:masterfrom
mattip:disallow-dgemm
Dec 7, 2018
Merged

charris merged 2 commits into
numpy:masterfrom
mattip:disallow-dgemm

Conversation

@mattip

@mattip mattip commented Dec 6, 2018

Copy link
Copy Markdown
Member

Fixes #12156.

  • Test added for 0-strided vector array
  • Fixed new matmul logic to detect this condition by adding logic from assert inside @typ@_gemv to checks before calling `@typ@_gemv
  • Fixed old dot logic to check for this condition in helper function
  • Verified (by using gdb) that gemv is used where possible in matmul, and that copying is done as necessary in dot

Comment thread numpy/core/src/common/cblasfuncs.c Resolved
Comment thread numpy/core/tests/test_multiarray.py Outdated
assert_equal(func(edf.T, edf), edtdf)

@pytest.mark.parametrize('func', (np.dot, np.matmul))
def test_no_dgemv(self, func):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to just check doubles?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking f, d, c, z, maybe separately for the two inputs. That way can also test the promotion. The advantage is that if the code gets refactored and introduces new code paths. Maybe those cases are already tested somewhere for the usual case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is difficult to test blas functionality

  • we have no external indication whether blas is used or not
  • we do not get a warning/error if blas is called incorrectly (the xerblas mechanism is not working correctly). Perhaps it is worthwhile opening an issue for this but the working assumption is we cannot call blas incorrectly.

I determined that my fix works by manually setting a breakpoint at the blas calls and verifying, via runtests.py -vv and manually examining dimensions and strides that the calls were correct, and by noticing that xerblas error messages do not appear on stderr.

While it would be simple to add the other blas-able cases, what would the expected error vs. non-error output be? It is not clear to me there is added value in quadrupling the test.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I was expanding the tests anyway I added the other BLAS dtypes and int as well for good measure.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the other BLAS dtypes and int as well for good measure

I see you requested combinations of dtypes. This is covered in other tests of the ufunc machinery, that it promotes and copies input where needed.

Comment thread numpy/core/src/common/cblasfuncs.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this condition for matmul too? Or does the ufunc machinery ensure this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the check for "non-zero dimension but stride==0" to matmul as well, see changes to matmul.c.src. Or did I not understand the comment?

@eric-wieser eric-wieser Dec 7, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean specifically the ((npy_intp)(PyArray_DATA(ap)) % itemsize) != 0 condition - ie. check the alignment of the pointers themselves.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ufunc flag NPY_ITER_ALIGNED handles this for us. I added a test and checked that

  • the unaligned ndarrays are copied in nditer_const.c:2914 and
  • when entering @typ@_matmul pointers are always aligned

@mattip

mattip commented Dec 7, 2018

Copy link
Copy Markdown
Member Author

Windows Python 3.7 32 bit build is failing when using gemv for complex64. I don't understand the failure, and why other 32 bit platforms do not fail:

self = <numpy.core.tests.test_multiarray.TestMethods object at 0x0D0C1630>
2018-12-07T08:19:50.7125582Z func = <ufunc 'matmul'>, dtype = 'F'
2018-12-07T08:19:50.7125616Z 
2018-12-07T08:19:50.7125667Z     @pytest.mark.parametrize('func', (np.dot, np.matmul))
2018-12-07T08:19:50.7126208Z     @pytest.mark.parametrize('dtype', 'ifdFD')
2018-12-07T08:19:50.7126804Z     def test_no_dgemv(self, func, dtype):
2018-12-07T08:19:50.7127050Z         # check vector arg for contiguous before gemv
2018-12-07T08:19:50.7127203Z         # gh-12156
2018-12-07T08:19:50.7127623Z         a = np.arange(8.0, dtype=dtype).reshape(2, 4)
2018-12-07T08:19:50.7127845Z         b = np.broadcast_to(1., (4, 1))
2018-12-07T08:19:50.7128281Z         ret1 = func(a, b)
2018-12-07T08:19:50.7129082Z         ret2 = func(a, b.copy())
2018-12-07T08:19:50.7129236Z         assert_equal(ret1, ret2)
2018-12-07T08:19:50.7129461Z     
2018-12-07T08:19:50.7129721Z         ret1 = func(b.T, a.T)
2018-12-07T08:19:50.7130045Z         ret2 = func(b.T.copy(), a.T)
2018-12-07T08:19:50.7130444Z         assert_equal(ret1, ret2)
2018-12-07T08:19:50.7130665Z     
2018-12-07T08:19:50.7131076Z         # check for unaligned data
2018-12-07T08:19:50.7131452Z         dt = np.dtype(dtype)
2018-12-07T08:19:50.7132169Z         a = np.zeros(8 * dt.itemsize // 2 + 1, dtype='int16')[1:].view(dtype)
2018-12-07T08:19:50.7132497Z         a = a.reshape(2, 4)
2018-12-07T08:19:50.7132760Z         b = a[0]
2018-12-07T08:19:50.7133483Z         # make sure it is not aligned
2018-12-07T08:19:50.7133879Z         assert_(a.__array_interface__['data'][0] % dt.itemsize != 0)
2018-12-07T08:19:50.7134239Z         ret1 = func(a, b)
2018-12-07T08:19:50.7134999Z >       ret2 = func(a.copy(), b.copy())
2018-12-07T08:19:50.7135319Z E       RuntimeWarning: invalid value encountered in matmul
2018-12-07T08:19:50.7135488Z 
2018-12-07T08:19:50.7135841Z a          = array([[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
2018-12-07T08:19:50.7136034Z        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]], dtype=complex64)
2018-12-07T08:19:50.7136848Z b          = array([0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], dtype=complex64)
2018-12-07T08:19:50.7137141Z dt         = dtype('complex64')
2018-12-07T08:19:50.7137451Z dtype      = 'F'
2018-12-07T08:19:50.7137809Z func       = <ufunc 'matmul'>
2018-12-07T08:19:50.7138322Z ret1       = array([0.+0.j, 0.+0.j], dtype=complex64)
2018-12-07T08:19:50.7138798Z ret2       = array([[ 6.+0.j, 22.+0.j]])
2018-12-07T08:19:50.7139740Z self       = <numpy.core.tests.test_multiarray.TestMethods object at 0x0D0C1630>

@charris

charris commented Dec 7, 2018 •

Copy link
Copy Markdown
Member

I'm rerunning the azure tests, that warning has turned up (once) before and I was thinking something wasn't properly suppressing warnings and they were slipping through from some other spot. We will see if it is repeatable.

EDIT: I suppose it could also be in the library.

@charris

charris commented Dec 7, 2018

Copy link
Copy Markdown
Member

Here is the other test failure: https://dev.azure.com/numpy/numpy/_build/results?buildId=1133. Different test, Same error, 32 bits, matmul. Are we testing matmul with invalid values anywhere?

@tylerjereddy

Copy link
Copy Markdown
Contributor

The other failure cited is Python 3.6 vs. 3.7 reported here, but both indeed involve matmul with both operands complex64 it seems.

32-bit Linux Azure is likely using an older openblas backend for now, since it uses apt-get to install the libs (until I sync that with wheels approach), so that may explain Linux vs. Win here.

@charris
charris merged commit 5000356 into numpy:master Dec 7, 2018
@charris

charris commented Dec 7, 2018

Copy link
Copy Markdown
Member

I'm going to put this in as I suspect that the problem isn't directly due to matmul and looks difficult to pin down. Note that the 32 bit code is likely compiled for a different FPU than the 64 bit code. The warning itself looks harmless, if annoying.

Thanks Matti.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants