mit-han-lab / torchsparse

[MICRO'23, MLSys'22] TorchSparse: Efficient Training and Inference Framework for Sparse Convolution on GPUs.
https://torchsparse.mit.edu
MIT License
1.15k stars 131 forks source link

How to make a deep copy of SparseTensor #267

Closed huohuohuohuohuohuohuohuo closed 6 months ago

huohuohuohuohuohuohuohuo commented 7 months ago

I want to make a deep copy of data (expect the features are new generated) as follows:

data_copy = SparseTensor( feats=new_feats coords=data.C ) data_copy._caches = data._caches

But it will not copy the stride and spatial range. Does copying theses variables separately is ok for subsequent network modules?

additionally, y = x

self.shortcut = nn.Identity() y = self.shortcut(x)

are all shallow copies.

In MinkowskiEngine, the deep copy is as follows:

data_copy = ME.SparseTensor( features=new_feats, coordinate_map_key=data.coordinate_map_key, coordinate_manager=data.coordinate_manager, device=data.device)

And how does the “data_copy._caches = data._caches” affect the coordinates calculation in calculation of sparse tensors such as data_copy + data ?

ys-2020 commented 7 months ago

Hi. I think you can designate the stride / spatial range variables either during initializing the new SparseTensor or later.

For the shallow copy problem, I think you do not need to copy the SparseTensor._caches if you only want to copy the coordinates. You can still do data_copy + data without copying the ._caches of data to data_copy, as long as they have the same coordinates and feature dimensions.

The SparseTensor._caches is a little bit more complicated. And it is similar to coordinate_manager in MinkowskiEngine. In this _caches, we will store information like kmaps, hashmaps, cmaps. The cmaps records the coordinate information indexed by relevant tensor stride.

huohuohuohuohuohuohuohuo commented 7 months ago

So, I can reinitialize a sparseTensor to achieve a deep copy as follows: data_copy = SparseTensor( feats=new_feats, coords=data.C, stride=data.stride, spatial_range=data.spatial_range ) without the need of "data_copy._caches = data._caches". If I want to use the transposed convolution with stride of 2 to restore the coordinates which has been downscaled by a convolution with stride of 2, does I need to do data_copy._caches = data._caches?

zhijian-liu commented 6 months ago

@ys-2020, could you please take a look at this issue when you have time? Thanks!

ys-2020 commented 6 months ago

So, I can reinitialize a sparseTensor to achieve a deep copy as follows: data_copy = SparseTensor( feats=new_feats, coords=data.C, stride=data.stride, spatial_range=data.spatial_range ) without the need of "data_copy._caches = data._caches". If I want to use the transposed convolution with stride of 2 to restore the coordinates which has been downscaled by a convolution with stride of 2, does I need to do data_copy._caches = data._caches?

Yes. I think your understanding is correct.

ys-2020 commented 6 months ago

Close this issue as completed. Please feel free to reopen it if you have any further questions.