mit-han-lab / torchsparse

[MICRO'23, MLSys'22] TorchSparse: Efficient Training and Inference Framework for Sparse Convolution on GPUs.

Home Page:https://torchsparse.mit.edu

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] <title>How to solve "File "torchsparse/tensor.pyx", line 101, in torchsparse.tensor.SparseTensor.dense AssertionError"

nancyhxn opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

File "torchsparse/tensor.pyx", line 101, in torchsparse.tensor.SparseTensor.dense
AssertionError

Expected Behavior

No response

Environment

- GCC:
- NVCC:
- PyTorch:
- PyTorch CUDA:
- TorchSparse:

Anything else?

No response

Thanks for reporting this issue! Could you please provide a short code snippet to reproduce this error?

Code:
input = torch.randn(20, 32, 7, 64, 64).cuda(2)
shape = input.shape
coors = torch.nonzero(input[:, 0, :, :, :])
for i in range(input.shape[1]):
fea = torch.zeros(coors.shape[0])
if i == 0:
feats = torch.unsqueeze(input[coors[:, 0], i, coors[:, 1], coors[:, 2], coors[:, 3]], dim=0)
else:
fea = torch.unsqueeze(input[coors[:, 0], i, coors[:, 1], coors[:, 2], coors[:, 3]], dim=0)
feats = torch.cat([feats, fea], dim=0)
feats = torch.transpose(feats, dim0=0, dim1=1)
coors = coors.int()
x = tsp.SparseTensor(feats, coors)
X = Conv3d(32, 32, 3, 1, 1)
y = X(x)
dense = x.dense()
print(dense.shape)

Error:
Traceback (most recent call last):
File "/workspace/thirdpaper/VSR-main/test_sparse.py", line 42, in
dense = x.dense()
File "torchsparse/tensor.pyx", line 101, in torchsparse.tensor.SparseTensor.dense
AssertionError

I also encountered the same problem of 'AssertionError', below is my code:

  • Create a sparse tensor first.

sp_tensor = SparseTensor(coors = coors, feats = feats)

  • Then I try to call SparseTensor.dense() to convert SparseTensor to its dense counterpart.

dense_tensor = sp_tensor.dense()

Hi @nancyhxn , @treemanzzz . You are meeting this error because you didn't set the spatial_range when constructing the sparse tensor. Thus, when you are calling .dense(). The function cannot know the spatial shape of the corresponding dense tensor you want.

Try to see if this code can solve you problem:

import torch
import torchsparse
import torchsparse.nn as spnn
import torchsparse.nn.functional as F

torch.manual_seed(42)

num_points = 100
dim = 4
feats = torch.randn([num_points, dim]).cuda()
coords = torch.randint(0, 10, [num_points, 4]).cuda()
coords[:, 0] = 0        # Set batch_idx to 0

spatial_range = (1, 10, 10, 10)
x = torchsparse.SparseTensor(feats, coords, spatial_range = spatial_range)

y = x.dense()

Thank you so much for your response!✨ By the way, regarding the 'spatial_range' in line 14 spatial_range = (1, 10, 10, 10), I understand that the last three numbers (10, 10, 10) represent the depth, height, and width of the space in Voxel units. However, what does the first number (1) refer to? Is that the batch_index?

Yes. It is batch size

I got that, thank you so much!!!