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

Cupy backpropagation error #50

Closed
tangzhenjie opened this issue Dec 20, 2023 · 5 comments
Closed

Cupy backpropagation error #50

tangzhenjie opened this issue Dec 20, 2023 · 5 comments

Comments

@tangzhenjie
Copy link

tangzhenjie commented Dec 20, 2023

I encountered this problem when using cupy for backpropagation. I don’t know what’s going on? There is no problem in the forward direction, but in the reverse direction, there is a dimension mismatch.

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

code location:
mgbx = mgradbselect * xselect
gradA = torch.sum(mgbx, dim=1)

@tvercaut
Copy link
Member

tvercaut commented Dec 20, 2023

Can you provide a minimal reproducible example?

@tangzhenjie
Copy link
Author

tangzhenjie commented Dec 20, 2023

I wrote a sample code that will cause this problem.

import torch
import torch.optim as optim
import torchsparsegradutils.cupy.cupy_sparse_solve as cupy_solve


A = torch.randn(12, 12, requires_grad=True)
b = torch.randn(12, requires_grad=True)

target = torch.randn(12, 1, requires_grad=True)

learning_rate = 0.05
optimizer = optim.SGD([A, b], lr=learning_rate)

for i in range(100):

    x = torch.unsqueeze(cupy_solve.sparse_solve_c4t(A.to_sparse(), b), 1)

    loss = torch.mean(target - x * 10)

    optimizer.zero_grad()

    loss.backward()

    optimizer.step()

@tvercaut
Copy link
Member

The issue is that we currently expect b to be a matrix in the backward pass. This is not cupy related.

You can workaround the issue by making b a 4x1 matrix (i.e. unsqueeze it). I'll open up a specific issue for handling vectors rhs in teh sparse solver routines.

@tangzhenjie
Copy link
Author

tangzhenjie commented Dec 21, 2023

Thank you very much for your prompt answer, but I changed the code to the one below and the same problem still occurs.

import torch
import torchsparsegradutils.cupy.cupy_sparse_solve as cupy_solve

A = torch.randn(12, 12, requires_grad=True).to_sparse()
b = torch.randn(12, 1, requires_grad=True)

x = cupy_solve.sparse_solve_c4t(A, b)
loss = x.sum()
loss.backward()

tvercaut added a commit that referenced this issue Dec 21, 2023
@tvercaut
Copy link
Member

Indeed, there was another similar issue in the cupy solver. This should now be fixed albeit with missing unit tests:
https://colab.research.google.com/drive/1vZ23g_tamJPFGCoeV9RbKB5ObEMfkkv7?usp=sharing

Completion of unit tests will be tracked in #51

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

2 participants