Skip to content
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

CupyBackend matrices are np.ndarrays #196

Open
BrunoLiegiBastonLiegi opened this issue Dec 18, 2024 · 4 comments
Open

CupyBackend matrices are np.ndarrays #196

BrunoLiegiBastonLiegi opened this issue Dec 18, 2024 · 4 comments

Comments

@BrunoLiegiBastonLiegi
Copy link
Contributor

While working on qiboteam/qibo#1548 I noticed that if you try to get the matrices from the CupyBackend, namely:

from qibojit.backends import CupyBackend

b = CupyBackend()
print(type(b.matrices.X))

you get a np.ndarray rather than a cp.ndarray. This does not seem to be the case for other backends that do not have np.ndarray as native arrays, e.g. PyTorchBackend or Tensorflowbackend.
I was wondering what was the reason behind this, because this makes some things in qiboteam/qibo#1548 more annoying, requiring for you to explicitely cast to cupy arrays when needed.

@andrea-pasquale
Copy link
Contributor

I believe that there were some discussion about this in the past qiboteam/qibo#928
In the end we decided to use numpy for all backends, which do not require other types due to automatic differentiation (such as tensorflow) since numpy is expected to be faster for small matrices #125 (comment)
Even though I agree with the fact that it is weird to have CupyMatrices which are np.arrays...

@BrunoLiegiBastonLiegi
Copy link
Contributor Author

Mmmh I see, I would still argue that at some point you have to cast to cp.arrays anyway, thus even if numpy is faster for the small matrices you will have to move to cupy eventually. At that point, isn't it better to just have them as cupy cached arrays? It is gonna be slow the first time, but identical to numpy afterwards.

@renatomello
Copy link
Contributor

I believe that there were some discussion about this in the past qiboteam/qibo#928 In the end we decided to use numpy for all backends, which do not require other types due to automatic differentiation (such as tensorflow) since numpy is expected to be faster for small matrices #125 (comment) Even though I agree with the fact that it is weird to have CupyMatrices which are np.arrays...

This was true at the time but it changed since the torch inclusion because torch does not do autograd on numpy arrays.

@andrea-pasquale
Copy link
Contributor

andrea-pasquale commented Dec 19, 2024

Indeed, I would say that in general we should cast to whichever type is necessary as long as we don't trigger qiboteam/qibo#928 (or we find an alternative solution for it).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants