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

BUG: <PERF: Using np.reshape(tensor) is slow> #25591

Closed
HarryCollins2 opened this issue Jan 16, 2024 · 4 comments
Closed

BUG: <PERF: Using np.reshape(tensor) is slow> #25591

HarryCollins2 opened this issue Jan 16, 2024 · 4 comments
Labels

Comments

@HarryCollins2
Copy link

Describe the issue:

I made a test on this problem and the time difference on tensor is large (10x). Seems that it needs a fix for the data structure that can be used as an array.

Reproduce the code example:

import numpy as np
import torch
import time

a_np = np.random.rand(1000, 1000)
b_np = np.random.rand(1000, 1000)
a_torch = torch.from_numpy(a_np)
b_torch = torch.from_numpy(b_np)


%timeit reshaped_np = np.reshape(a_torch, (2000, 500))
%timeit reshaped_torch = np.reshape(a_torch.numpy(), (2000, 500))


### Error message:

```shell
17.5 µs ± 155 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
1.43 µs ± 11.9 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

Python and NumPy Versions:

1.26.0
3.9.18 (main, Sep 11 2023, 08:25:10)
[Clang 14.0.6 ]

Runtime Environment:

No response

Context for the issue:

No response

@seberg
Copy link
Member

seberg commented Jan 16, 2024

That is for torch to know, maybe torch converts to ndarray and that is just slow. There is nothing about NumPy involvement though, NumPy would allow torch to make it fast in principle through protocols.

@seberg seberg closed this as not planned Won't fix, can't repro, duplicate, stale Jan 16, 2024
@HarryCollins2
Copy link
Author

Thank you for your response. But pandas and matplotlib both support the data structure having array interface to be handled like array, maybe it only small modification in numpy to convert it and it can boost performance. pandas-dev/pandas#44616, matplotlib/matplotlib#25882

@seberg
Copy link
Member

seberg commented Jan 16, 2024

I cannot say about whether torch wants or can do it. But as I said: NumPy provides the protocols (not just __array__). If anyone can do something about it, it's torch and not NumPy.

@HarryCollins2
Copy link
Author

HarryCollins2 commented Jan 16, 2024

Ok, thanks for your patience and clarification

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

No branches or pull requests

2 participants