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

How to use transposed conv3d correctly? #346

Open
GCChen97 opened this issue Feb 25, 2025 · 1 comment
Open

How to use transposed conv3d correctly? #346

GCChen97 opened this issue Feb 25, 2025 · 1 comment

Comments

@GCChen97
Copy link

Hi, thanks for the cool torchsparse! Recently I am trying to use it but I don't know how to upsample the input.
I made the following model to test, but it raised an empty kmaps error:

model = tsnn.Conv3d(3, 32, 2, 2, transposed=True).to('cuda')

After debugging, I found that it seems to require some "downsample" convolutions before transposed convolutions. Is my guess right?

Besides, how to set_kmap_mode correctly?

F.set_kmap_mode("hashmap")
model = tsnn.Conv3d(3, 3, 2, 2, transposed=True, generative=True).to('cuda')

I've set the mode as the code above. But it still raised an error and told me to set "hashmap" instead of "hashmap_on_the_fly".

@GCChen97
Copy link
Author

GCChen97 commented Feb 25, 2025

I made a hack, and it seems to work currently:

def conv3d(...) 
    ...
    if kernel_size == (1, 1, 1) and stride == (1, 1, 1) and dilation == (1, 1, 1):
        ...    
    else:
        tensor_stride = tuple(input.stride[k] // stride[k] for k in range(3))
        if not generative:
            kmap = input._caches.kmaps.get(
                (tensor_stride, kernel_size, stride, dilation)
            )
            
            # region modification
            hashmap_keys, hashmap_vals = None, None
            if kmap is None:
                kmap = F.build_kernel_map(
                    coords,
                    feats.shape[0],
                    kernel_size,
                    stride,
                    padding,
                    hashmap_keys,
                    hashmap_vals,
                    input.spatial_range,
                    kmap_mode,
                    dataflow,
                    downsample_mode=config.downsample_mode,
                    training=training,
                    ifsort=config.ifsort,
                    split_mask_num=config.split_mask_num,
                    split_mask_num_bwd=config.split_mask_num_bwd,
                )

                hashmap = [kmap["hashmap_keys"], kmap["hashmap_vals"]]

                input._caches.kmaps[(input.stride, kernel_size, stride, dilation)] = kmap
                input._caches.hashmaps[input.stride] = hashmap
            # endregion
    ...

Besides, two convolutions are required before a tranposed one:

model = nn.Sequential(
    tsnn.Conv3d(3, 3, 3, 2),
    tsnn.Conv3d(3, 3, 3, 2),
    tsnn.Conv3d(3, 3, 3, 2),
    tsnn.Conv3d(3, 3, 2, 2, transposed=True),
    tsnn.Conv3d(3, 3, 2, 2, transposed=True),
).to('cuda')

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

1 participant