Skip to content

FIX Add stride to y in _ger_memview #25323

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

Merged
merged 1 commit into from
Jan 9, 2023

Conversation

jakirkham
Copy link
Contributor

@jakirkham jakirkham commented Jan 7, 2023

There was a missing 1 in the striding of y in _ger_memview. This adds it.


For more context the increment of y (incy) is 1 below

_ger(order, m, n, alpha, &x[0], 1, &y[0], 1, &A[0, 0], lda)

Note incy follows y in the signature definition

cdef void _ger(BLAS_Order order, int m, int n, floating alpha, floating *x,
int incx, floating *y, int incy, floating *A, int lda) nogil:

There was a missing `1` in the striding of `y` in `_ger_memview`. This
adds it.
Copy link
Member

@lorentzenchr lorentzenchr left a comment

Choose a reason for hiding this comment

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

Nice catch!
Fortunately, tt seems that we don't use _ger_memview in any code, only in test_cython_blas.py.

@lorentzenchr lorentzenchr added the Quick Review For PRs that are quick to review label Jan 7, 2023
@jakirkham
Copy link
Contributor Author

Thanks for the quick review, Christian! 🙏

Copy link
Member

@jjerphan jjerphan left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you, @jakirkham

Note that in the case of one dimensional array, this stride information does not change anything as Fortran- and C-contiguity is identical. Still, it is nice for uniformity.

@jjerphan jjerphan enabled auto-merge (squash) January 9, 2023 09:36
@jjerphan jjerphan disabled auto-merge January 9, 2023 10:18
@jjerphan jjerphan merged commit fb9c549 into scikit-learn:main Jan 9, 2023
@jakirkham jakirkham deleted the fix_ger_memview_y branch January 9, 2023 18:12
@jakirkham
Copy link
Contributor Author

Thanks all! 🙏


For more context, perhaps this example makes the bug clearer?

from libc.stdint cimport uint8_t

cpdef Py_ssize_t test1(uint8_t[::] a) nogil:
    return a.strides[0]

cpdef Py_ssize_t test2(uint8_t[::1] a) nogil:
    return a.strides[0]
import numpy as np

a1 = np.arange(9, dtype="u1")
a2 = np.arange(27, dtype="u1")[::3]

test1(a1)  # 1
test1(a2)  # 3
test2(a1)  # 1
test2(a2)  # ValueError: ndarray is not C-contiguous

So leaving the 1 out meant the array did not have to be contiguous (even though the function assumed it was internally). The change here makes sure this is caught when passing the array in.

@jjerphan
Copy link
Member

Good catch! I was unconscious of possibilities for no -continuous arrays!

There are plenty of memoryviews' definitions and declarations (including but not limited to functions and methods' signatures) that aren't specific enough in scikit-learn's codebase then.

I think we can open a meta-issue for referencing the problems and list such definitions and declarations.

@thomasjpfan
Copy link
Member

I think we can open a meta-issue for referencing the problems and list such definitions and declarations.

Note that scikit-learn's codebase will use non-contiguous memoryviews when the input is not guaranteed to be contiguous.

jjerphan pushed a commit to jjerphan/scikit-learn that referenced this pull request Jan 20, 2023
There was a missing `1` in the striding of `y` in `_ger_memview`. This
adds it.
jjerphan pushed a commit to jjerphan/scikit-learn that referenced this pull request Jan 20, 2023
There was a missing `1` in the striding of `y` in `_ger_memview`. This
adds it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cython module:utils Quick Review For PRs that are quick to review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants