Commit bf473de0 authored by Yan Yan's avatar Yan Yan
Browse files

1. change indicePairs layout to [2, KV, num]

2. working on batch indice conv
parent 6c767a51
......@@ -38,7 +38,7 @@ class SparseConv3dTestTorch(nn.Module):
padding=padding,
dilation=dilation,
bias=False,
use_hash=True)
use_hash=False)
]
for i in range(1, num_layers):
layers.append(
......@@ -48,7 +48,8 @@ class SparseConv3dTestTorch(nn.Module):
stride,
padding=padding,
dilation=dilation,
bias=False))
bias=False,
use_hash=False))
self.net = spconv.SparseSequential(*layers, )
# self.grid = torch.full([3, *shape], -1, dtype=torch.int32).cuda()
self.grid = None
......@@ -84,7 +85,8 @@ class SubMConv3dTestTorch(nn.Module):
dilation=dilation,
bias=False))
self.net = spconv.SparseSequential(*layers, )
self.grid = torch.full([3, *shape], -1, dtype=torch.int32).cuda()
# self.grid = torch.full([3, *shape], -1, dtype=torch.int32).cuda()
self.grid = None
self.shape = shape
def forward(self, features, coors, batch_size):
......@@ -325,7 +327,7 @@ def scatter_nd(indices, updates, shape):
class TestSpConv(TestCase):
def testSpConv3d(self):
np.random.seed(484)
devices = ["cpu:0"]
devices = ["cuda:0", "cpu:0"]
shapes = [[19, 18, 17]]
batchsizes = [1, 2]
......@@ -523,12 +525,12 @@ class TestSpConv(TestCase):
def testSpMaxPool3d(self):
np.random.seed(485)
devices = ["cuda:0", "cpu:0"]
devices = ["cuda:0"]
shapes = [[19, 18, 17]]
batchsizes = [1, 2]
in_channels = [64]
out_channels = [64]
in_channels = [62]
out_channels = [62]
ksizes = [2, 3]
strides = [1, 2, 3]
paddings = [0, 1]
......@@ -566,6 +568,7 @@ class TestSpConv(TestCase):
out = net(features_t, indices_t, bs)
outids = out.indices
outfeatures = out.features
outids_dev = outids.float()
out_dense = out.dense(channels_first=False)
out = out_dense.permute(0, 4, 1, 2, 3).contiguous()
......@@ -581,13 +584,13 @@ class TestSpConv(TestCase):
1).contiguous()
din_sparse = gather_nd(din_dense, indices_t.long())
din = features_t.grad.detach()
din_np = din.cpu().numpy()
din_sparse_np = din_sparse.cpu().numpy()
self.assertAllClose(din_np, din_sparse_np, atol=1e-4)
out_np = out.detach().cpu().numpy()
out_ref_np = out_ref.detach().cpu().numpy()
self.assertAllClose(out_np, out_ref_np, atol=1e-4)
din_np = din.cpu().numpy()
din_sparse_np = din_sparse.cpu().numpy()
self.assertAllClose(din_np, din_sparse_np, atol=1e-4)
def main():
......@@ -653,7 +656,70 @@ def main():
out_ref.detach().cpu().numpy()))
def main_subm():
# function for develop.
np.random.seed(484)
# devices = ["cuda:0"]
devices = ["cuda:0"]
shapes = [[50, 30, 30]]
batchsizes = [2]
in_channels = [32]
out_channels = [64]
ksizes = [(3, 3, 3)]
strides = [1]
paddings = [1]
dilations = [1]
for dev, shape, bs, IC, OC, k, s, p, d in params_grid(
devices, shapes, batchsizes, in_channels, out_channels, ksizes,
strides, paddings, dilations):
if all([s > 1, d > 1]):
continue
device = torch.device(dev)
num_points = [500] * bs
sparse_dict = generate_sparse_data(shape, num_points, IC)
features = np.ascontiguousarray(sparse_dict["features"]).astype(
np.float32)
indices = np.ascontiguousarray(
sparse_dict["indices"][:, [3, 0, 1, 2]]).astype(np.int32)
features_dense = sparse_dict["features_dense"].astype(np.float32)
indices_t = torch.from_numpy(indices)
filters = np.random.uniform(0, 1, size=[k[0], 1, 1, IC,
OC]).astype(np.float32)
indices_t = torch.from_numpy(indices).int().to(device).float()
features_t = torch.from_numpy(features).to(device).float()
features_dense_t = torch.from_numpy(features_dense).to(device).float()
net = SubMConv3dTestTorch(1, 3, shape, IC, OC, k, s, p,
d).to(device).float()
net_ref = Conv3dTestTorch(1, 3, shape, IC, OC, k, s, p,
d).to(device).float()
filters_t = torch.from_numpy(filters).to(device).float()
net_ref.net[0].weight[:] = filters_t.permute(4, 3, 0, 1,
2).contiguous()
net.net[0].weight[:] = filters_t
out_ref = net_ref(features_dense_t)
times = []
for i in range(100):
t = time.time()
out = net(features_t, indices_t, bs)
torch.cuda.synchronize()
times.append(time.time() - t)
# print((net.grid == -1).float().sum(), net.grid.numel())
# print("spconv time", time.time() - t)
print("spconv time", np.mean(times[10:]))
out = net(features_t, indices_t, bs)
# print(out.indices)
out = out.dense()
print(
np.linalg.norm(out.detach().cpu().numpy() -
out_ref.detach().cpu().numpy()))
if __name__ == '__main__':
# main()
main_subm()
# unittest.main()
TestSpConv().testSpDeConv3d()
# TestSpConv().testSpConv3d()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment