dev.py 2.28 KB
Newer Older
yan.yan's avatar
yan.yan committed
1
2
import spconv.pytorch as spconv 
from spconv.core import ConvAlgo
yan.yan's avatar
sync  
yan.yan committed
3

yan.yan's avatar
yan.yan committed
4
5
import spconv.pytorch as spconv
from spconv.test_utils import TestCase, generate_sparse_data, params_grid
yan.yan's avatar
sync  
yan.yan committed
6

yan.yan's avatar
yan.yan committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import torch 
import numpy as np 
class SparseMaxPool2dTestTorch(torch.nn.Module):
    def __init__(self, num_layers, ndim, shape, kernel_size, stride, padding,
                 dilation, algo):
        super().__init__()
        self.algo = algo
        layers = [
            spconv.SparseMaxPool2d(kernel_size, stride, padding, dilation, algo=algo)
        ]
        for i in range(1, num_layers):
            layers.append(
                spconv.SparseMaxPool2d(kernel_size, stride, padding, dilation, algo=algo))
        self.net = spconv.SparseSequential(*layers, )
        self.shape = shape

    def forward(self, features, coors, batch_size):
        coors = coors.int()
        x = spconv.SparseConvTensor(features, coors, self.shape, batch_size)
        return self.net(x)  # .dense()
shapes = [[65536, 65536]]
batchsizes = [32]

in_channels = [32]
out_channels = [32]
ksizes = [2]
strides = [2]
paddings = [0]
dilations = [1]
algos = [
    # ConvAlgo.Native, 
    ConvAlgo.MaskImplicitGemm,
    # ConvAlgo.MaskSplitImplicitGemm
]
devices = ["cuda:0"]


for dev, shape, bs, IC, OC, k, s, p, d, al in params_grid(
        devices, shapes, batchsizes, in_channels, out_channels, ksizes,
        strides, paddings, dilations, algos):
    device = torch.device(dev)
    num_points = [1000] * bs
    print(1)
    sparse_dict = generate_sparse_data(shape,
                                        num_points,
                                        IC,
                                        with_dense=False,
                                        data_range=[0.1, 1],
                                        shape_scale = 64)
    print(2)
    net = SparseMaxPool2dTestTorch(1, 2, shape, k, s, p, d, al).to(device)
    features = np.ascontiguousarray(sparse_dict["features"]).astype(
        np.float32)
    indices = np.ascontiguousarray(
        sparse_dict["indices"][:, [2, 0, 1]]).astype(np.int32)
    print(indices.max(0))
    indices_t = torch.from_numpy(indices).int().to(device)
    features_t = torch.from_numpy(features).to(device)
    features_t.requires_grad = True

    out = net(features_t, indices_t, bs)
    print(out.indices.min(0))