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

Fix sddmm2 when nnz=0 #300

Merged
merged 1 commit into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions tests/test_custom_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ def test_sddmm_sputnik(device):


@cuda_only
@pytest.mark.parametrize("prob", [0.5, 1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for adding this, nice catch

@pytest.mark.parametrize("K", [32, 17])
@pytest.mark.parametrize("M", [30, 17])
@pytest.mark.parametrize("L", [30, 17])
def test_sddmm_csr(L, M, K):
def test_sddmm_csr(L, M, K, prob):
device = torch.device("cuda")
# TODO add more checks for different nnz
B = 8
prob = 0.5
a = torch.rand(B, L, K, device=device)
b = torch.rand(B, M, K, device=device)
mask = _create_random_sparsity(
Expand All @@ -188,7 +188,7 @@ def test_sddmm_csr(L, M, K):


@cuda_only
@pytest.mark.parametrize("nnz", [4, 16, 20, 36])
@pytest.mark.parametrize("nnz", [0, 4, 16, 20, 36])
def test_sddmm_csr_per_nnz(nnz):
device = torch.device("cuda")
B = 8
Expand All @@ -215,14 +215,14 @@ def test_sddmm_csr_per_nnz(nnz):


@cuda_only
@pytest.mark.parametrize("prob", [0.5, 1])
@pytest.mark.parametrize("K", [32, 17])
@pytest.mark.parametrize("M", [30, 17])
@pytest.mark.parametrize("L", [30, 17])
def test_sddmm_coo(L, M, K):
def test_sddmm_coo(L, M, K, prob):
device = torch.device("cuda")
# TODO add more checks for different nnz
B = 8
prob = 0.5
a = torch.rand(B, L, K, device=device)
b = torch.rand(B, M, K, device=device)
mask = _create_random_sparsity(
Expand Down
8 changes: 8 additions & 0 deletions xformers/components/attention/csrc/cuda/sddmm2_cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ torch::Tensor sddmm_cuda_coo(
const auto nnz = rowind.size(0);
auto out = torch::empty({batch_size, nnz}, D1.options());

if (out.numel() == 0)
return out;

cudaStream_t stream = at::cuda::getCurrentCUDAStream();
dim3 grid_dim(nnz / 16 + (nnz & 15), batch_size, 1);
if ((k % 4) == 0) {
Expand Down Expand Up @@ -496,6 +499,7 @@ torch::Tensor sddmm_cuda_coo(
D2.data_ptr<float>(),
out.data_ptr<float>());
}
AT_CUDA_CHECK(cudaGetLastError());
return out;
}

Expand All @@ -511,6 +515,9 @@ torch::Tensor sddmm_cuda_csr(
const auto nnz = colind.size(0);
auto out = torch::empty({batch_size, nnz}, D1.options());

if (out.numel() == 0)
return out;

cudaStream_t stream = at::cuda::getCurrentCUDAStream();
dim3 grid_dim(nnz / 16 + (nnz & 15), batch_size, 1);

Expand Down Expand Up @@ -539,6 +546,7 @@ torch::Tensor sddmm_cuda_csr(
D2.data_ptr<float>(),
out.data_ptr<float>());
}
AT_CUDA_CHECK(cudaGetLastError());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, makes sense :)

return out;
}
} // namespace ge_spmm
Expand Down