最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - NumbaPerformanceWarning about contiguous arrays, although both arrays are already contiguous - Stack Overflow

programmeradmin0浏览0评论

I am having a problem with removing this warning, before publishing a package on PyPI.

As a summary, this is the function that I am using to speed up the np.dot() function:

@nb.jit(nb.float64[:,:](nb.float64[:,:], nb.float64[:,:]), nopython=True)
def fastDot(X, Y):
    return np.dot(X, Y)

And the aim is to use this matrix to multiply a matrix of lagged signals with the eigenvectors, it can be also any other matrix:

# Compute principal components
PC = fastDot(X, eigenVectors)

This is where I get the following warning:

NumbaPerformanceWarning: np.dot() is faster on contiguous arrays, called on (Array(float64, 2, 'A', False, aligned=True), Array(float64, 2, 'A', False, aligned=True))
    return np.dot(X, Y)

I have also used this line just before the fastDot() call:

eigenVectors, X = np.ascontiguousarray(eigenVectors), np.ascontiguousarray(X)

Still no success.

I know it's not a huge problem, but I would like to remove this warning without using statically typed warnings.

Can someone please help me in:

  1. Understanding why this is happening
  2. How can I remove this?

Thank you so much in advance!

I am having a problem with removing this warning, before publishing a package on PyPI.

As a summary, this is the function that I am using to speed up the np.dot() function:

@nb.jit(nb.float64[:,:](nb.float64[:,:], nb.float64[:,:]), nopython=True)
def fastDot(X, Y):
    return np.dot(X, Y)

And the aim is to use this matrix to multiply a matrix of lagged signals with the eigenvectors, it can be also any other matrix:

# Compute principal components
PC = fastDot(X, eigenVectors)

This is where I get the following warning:

NumbaPerformanceWarning: np.dot() is faster on contiguous arrays, called on (Array(float64, 2, 'A', False, aligned=True), Array(float64, 2, 'A', False, aligned=True))
    return np.dot(X, Y)

I have also used this line just before the fastDot() call:

eigenVectors, X = np.ascontiguousarray(eigenVectors), np.ascontiguousarray(X)

Still no success.

I know it's not a huge problem, but I would like to remove this warning without using statically typed warnings.

Can someone please help me in:

  1. Understanding why this is happening
  2. How can I remove this?

Thank you so much in advance!

Share Improve this question asked 2 days ago Tino DTino D 2,6382 gold badges8 silver badges25 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Numba raises this warning because np.dot() is optimized for contiguous and properly memory-aligned arrays. While np.ascontiguousarray() guarantees C-contiguity, it doesn't always guarantee proper memory alignment. Since alignment isn't guaranteed, Numba will assume non-memory alignment if the function signature allows non-contiguous arrays.

Solution 1

Modify the function signature to force Numba to recognize arrays as C-contiguous in the JIT compilation. Using nb.float64[:, ::1] explicitly tells Numba that the array must be C-contiguous.

@nb.jit(nb.float64[:, :](nb.float64[:, ::1], nb.float64[:, ::1]), fastmath=True, nopython=True)
def fastDot(X, Y):
    return np.dot(X, Y)

Solution 2

Since the existing array may not be properly memory aligned after np.contiguousarray(), you can create a copy of it that forces Numpy to create a new aligned memory block. However, this method will double the memory usage.

X = np.ascontiguousarray(X).copy()
eigenVectors = np.ascontiguousarray(eigenVectors).copy()

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论