question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CuPy to support accepting `order` as a parameter to functions other than `cupy.array`

See original GitHub issue

Would it be possible that functions can accept an order parameter? For example, I would love to use https://docs-cupy.chainer.org/en/stable/reference/generated/cupy.random.standard_normal.html#cupy.random.standard_normal with order='F'. As it stands, the only way to convert a C matrix to F is by doing X = cp.array(X, order='F'). This temporarily blows up memory usage to 2x as it is most likely creating a copy of the transpose, I believe. For memory intensive applications, this is not the best as OOMs are common.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
divyegalacommented, Mar 24, 2020

Apparently if we wanna draw 1D samples, we should not specify size in multivariate_normal:

>>> import cupy as cp
>>>
>>> size = 1000
>>> mean = cp.random.random(size)
>>> cov_matrix = cp.diag(cp.ones(size))
>>> a = cp.random.multivariate_normal(mean, cov_matrix, dtype='float32')
>>> a.shape
(1000,)
>>> a = a.reshape(4, 250, order='F')
>>> a.shape
(4, 250)
>>> a.flags
  C_CONTIGUOUS : False
  F_CONTIGUOUS : True
  OWNDATA : False

@leofang upon more testing, that works but this is also what works for me: X = X.T.reshape(X.shape, order='F')

The problem with the approach you suggested is that I am using the covariance matrix to verify whether my PCA algorithm worked correctly. Reshaping point samples of size n to form 1D samples would not reproduce the same covariance matrix for me from PCA, I believe, as then the columns would become features of their own. If you could verify for me if my approach is correct, then it would be really helpful! 😃

1reaction
emcastillocommented, Mar 24, 2020

Hi! Thanks for the feedback. With random functions, all the elements are generated independently so just transposing the matrix should give you a Fortran order matrix without an additional copy.

For the random package, NumPy does not support the order argument for the routines. So in order to keep as close as possible to NumPy we won’t be adding such option. But we are working on adding Generalized Universal Functions, so that order will be an argument of several NumPy core routines.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Interoperability — CuPy 11.4.0 documentation
DLPack data exchange protocol​​ The function cupy. from_dlpack() accepts such object and returns a cupy. ndarray that is safely accessible on CuPy's current ......
Read more >
Differences between CuPy and NumPy
Unlike NumPy, Universal Functions in CuPy only work with CuPy array or scalar. They do not accept other objects (e.g., lists or numpy.ndarray...
Read more >
cupy.ndarray — CuPy 11.4.0 documentation
Multi-dimensional array on a CUDA device. This class implements a subset of methods of numpy.ndarray . The difference is that this class allocates...
Read more >
Basics of CuPy — CuPy 11.4.0 documentation
cupy. asarray() can accept cupy. ndarray , which means we can transfer the array between devices with this function. cupy.
Read more >
v9.1.0 PDF - CuPy Documentation
Unlike NumPy, Universal Functions in CuPy only work with CuPy array or scalar. They do not accept other objects.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found