conv.py 34.3 KB
Newer Older
yan.yan's avatar
yan.yan committed
1
# Copyright 2021 Yan Yan
yan.yan's avatar
v2.1  
yan.yan committed
2
#
traveller59's avatar
traveller59 committed
3
4
5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
yan.yan's avatar
v2.1  
yan.yan committed
6
#
traveller59's avatar
traveller59 committed
7
#     http://www.apache.org/licenses/LICENSE-2.0
yan.yan's avatar
v2.1  
yan.yan committed
8
#
traveller59's avatar
traveller59 committed
9
10
11
12
13
14
15
16
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import math
import time
yan.yan's avatar
bug fix  
yan.yan committed
17
from typing import List, Optional, Tuple, Union
traveller59's avatar
traveller59 committed
18
19
20
21
22
23
24

import numpy as np
import torch
from torch import nn
from torch.nn import init
from torch.nn.parameter import Parameter

yan.yan's avatar
yan.yan committed
25
from spconv import pytorch as spconv
yan.yan's avatar
v2.1  
yan.yan committed
26
from spconv.core import ConvAlgo
yan.yan's avatar
yan.yan committed
27
28
import spconv.pytorch.functional as Fsp
from spconv.pytorch import ops
yan.yan's avatar
v2.1  
yan.yan committed
29
from spconv.pytorch.core import IndiceData, SparseConvTensor, ImplicitGemmIndiceData
yan.yan's avatar
yan.yan committed
30
31
from spconv.pytorch.modules import SparseModule
from spconv.constants import FILTER_HWIO
traveller59's avatar
traveller59 committed
32

yan.yan's avatar
v2.1  
yan.yan committed
33
34

def _calculate_fan_in_and_fan_out_hwio(tensor, algo: ConvAlgo):
traveller59's avatar
traveller59 committed
35
36
37
38
39
40
41
42
43
44
    dimensions = tensor.ndimension()
    if dimensions < 2:
        raise ValueError(
            "Fan in and fan out can not be computed for tensor with fewer than 2 dimensions"
        )

    if dimensions == 2:  # Linear
        fan_in = tensor.size(-2)
        fan_out = tensor.size(-1)
    else:
yan.yan's avatar
v2.1  
yan.yan committed
45
46
47
48
49
50
51
52
53
54
55
        if algo == ConvAlgo.Native:
            if FILTER_HWIO:
                num_input_fmaps = tensor.size(-2)
                num_output_fmaps = tensor.size(-1)
            else:
                num_input_fmaps = tensor.size(-1)
                num_output_fmaps = tensor.size(-2)

            receptive_field_size = 1
            if tensor.dim() > 2:
                receptive_field_size = tensor[..., 0, 0].numel()
yan.yan's avatar
bug fix  
yan.yan committed
56
57
        else:
            num_input_fmaps = tensor.size(-1)
yan.yan's avatar
v2.1  
yan.yan committed
58
59
60
61
62
            num_output_fmaps = tensor.size(0)
            receptive_field_size = 1
            if tensor.dim() > 2:
                receptive_field_size = int(np.prod(tensor.shape[1:-1]))

traveller59's avatar
traveller59 committed
63
64
65
66
67
68
69
        fan_in = num_input_fmaps * receptive_field_size
        fan_out = num_output_fmaps * receptive_field_size

    return fan_in, fan_out


class SparseConvolution(SparseModule):
70
71
    __constants__ = [
        'stride', 'padding', 'dilation', 'groups', 'bias', 'subm', 'inverse',
yan.yan's avatar
v2.1  
yan.yan committed
72
        'transposed', 'output_padding'
73
74
    ]

traveller59's avatar
traveller59 committed
75
    def __init__(self,
yan.yan's avatar
bug fix  
yan.yan committed
76
77
78
                 ndim: int,
                 in_channels: int,
                 out_channels: int,
yan.yan's avatar
v2.1  
yan.yan committed
79
80
81
82
83
84
85
86
87
88
89
90
                 kernel_size: Union[int, List[int], Tuple[int, ...]] = 3,
                 stride: Union[int, List[int], Tuple[int, ...]] = 1,
                 padding: Union[int, List[int], Tuple[int, ...]] = 0,
                 dilation: Union[int, List[int], Tuple[int, ...]] = 1,
                 groups: Union[int, List[int], Tuple[int, ...]] = 1,
                 bias: bool = True,
                 subm: bool = False,
                 output_padding: Union[int, List[int], Tuple[int, ...]] = 0,
                 transposed: bool = False,
                 inverse: bool = False,
                 indice_key: Optional[str] = None,
                 algo: Optional[ConvAlgo] = None,
yanyan's avatar
yanyan committed
91
92
                 name=None):
        super(SparseConvolution, self).__init__(name=name)
yan.yan's avatar
v2.1  
yan.yan committed
93
        assert groups == 1, "don't support groups for now"
traveller59's avatar
traveller59 committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
        if not isinstance(kernel_size, (list, tuple)):
            kernel_size = [kernel_size] * ndim
        if not isinstance(stride, (list, tuple)):
            stride = [stride] * ndim
        if not isinstance(padding, (list, tuple)):
            padding = [padding] * ndim
        if not isinstance(dilation, (list, tuple)):
            dilation = [dilation] * ndim
        if not isinstance(output_padding, (list, tuple)):
            output_padding = [output_padding] * ndim
        self.ndim = ndim
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.kernel_size = kernel_size
yan.yan's avatar
v2.1  
yan.yan committed
108
109
        kv = int(np.prod(kernel_size))
        self.conv1x1 = kv == 1
traveller59's avatar
traveller59 committed
110
111
112
113
114
115
116
117
118
        self.stride = stride
        self.padding = padding
        self.dilation = dilation
        self.transposed = transposed
        self.inverse = inverse
        self.output_padding = output_padding
        self.groups = groups
        self.subm = subm
        self.indice_key = indice_key
yan.yan's avatar
v2.1  
yan.yan committed
119
120
121
122
123
124
125
126
127
128
129
        if algo is None:
            if kv <= 32:
                if kv < 8:
                    algo = ConvAlgo.MaskImplicitGemm
                else:
                    algo = ConvAlgo.MaskImplicitGemm
            else:
                algo = ConvAlgo.Native
        if kv > 32:
            assert algo == ConvAlgo.Native, "implicit gemm don't support kv >= 32 for now"

yan.yan's avatar
yan.yan committed
130
        self.algo = algo
yan.yan's avatar
v2.1  
yan.yan committed
131
132
133
134
135
136
137
138
139
140
        # self.algo = ConvAlgo.Native
        if self.algo == ConvAlgo.Native:
            if FILTER_HWIO:
                # RSCK
                self.weight = Parameter(
                    torch.Tensor(*kernel_size, in_channels, out_channels))
            else:
                # RSKC
                self.weight = Parameter(
                    torch.Tensor(*kernel_size, out_channels, in_channels))
yan.yan's avatar
yan.yan committed
141
        else:
yan.yan's avatar
v2.1  
yan.yan committed
142
            # KRSC
yan.yan's avatar
yan.yan committed
143
            self.weight = Parameter(
yan.yan's avatar
v2.1  
yan.yan committed
144
145
                torch.Tensor(out_channels, *kernel_size, in_channels))

traveller59's avatar
traveller59 committed
146
147
148
149
150
151
        if bias:
            self.bias = Parameter(torch.Tensor(out_channels))
        else:
            self.register_parameter('bias', None)
        self.reset_parameters()

yan.yan's avatar
v2.1  
yan.yan committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    def extra_repr(self):
        s = ('{in_channels}, {out_channels}, kernel_size={kernel_size}'
             ', stride={stride}')
        if self.padding != (0, ) * len(self.padding):
            s += ', padding={padding}'
        if self.dilation != (1, ) * len(self.dilation):
            s += ', dilation={dilation}'
        if self.output_padding != (0, ) * len(self.output_padding):
            s += ', output_padding={output_padding}'
        if self.groups != 1:
            s += ', groups={groups}'
        if self.bias is None:
            s += ', bias=False'
        if self.algo is not None:
            s += f', algo={self.algo}'
        return s.format(**self.__dict__)

traveller59's avatar
traveller59 committed
169
170
    def reset_parameters(self):
        n = self.in_channels
yan.yan's avatar
v2.1  
yan.yan committed
171
172
173
174
175
176
177
178
        # following commented code is used to make weight different layout have same value
        # if self.algo != ConvAlgo.Native:
        #     weight2 = self.weight.data.permute(1, 2, 3, 0,
        #                                        4).contiguous().clone()
        #     init.uniform_(weight2, 0, 0.001)
        #     self.weight.data[:] = weight2.permute(3, 0, 1, 2, 4)
        # else:
        #     init.uniform_(self.weight, 0, 0.001)
yan.yan's avatar
yan.yan committed
179
        init.kaiming_uniform_(self.weight, a=math.sqrt(0.005))
traveller59's avatar
traveller59 committed
180
        if self.bias is not None:
yan.yan's avatar
v2.1  
yan.yan committed
181
182
            fan_in, _ = _calculate_fan_in_and_fan_out_hwio(
                self.weight, self.algo)
traveller59's avatar
traveller59 committed
183
184
185
            bound = 1 / math.sqrt(fan_in)
            init.uniform_(self.bias, -bound, bound)

yanyan's avatar
yanyan committed
186
187
    def forward(self, input: SparseConvTensor):
        assert isinstance(input, SparseConvTensor)
yan.yan's avatar
v2.1  
yan.yan committed
188
189
        assert input.features.shape[
            1] == self.in_channels, "channel size mismatch"
traveller59's avatar
traveller59 committed
190
191
192
193
194
195
196
197
        features = input.features
        device = features.device
        indices = input.indices
        spatial_shape = input.spatial_shape
        batch_size = input.batch_size
        if not self.subm:
            if self.transposed:
                out_spatial_shape = ops.get_deconv_output_size(
traveller59's avatar
traveller59 committed
198
199
                    spatial_shape, self.kernel_size, self.stride, self.padding,
                    self.dilation, self.output_padding)
traveller59's avatar
traveller59 committed
200
201
            else:
                out_spatial_shape = ops.get_conv_output_size(
traveller59's avatar
traveller59 committed
202
203
                    spatial_shape, self.kernel_size, self.stride, self.padding,
                    self.dilation)
traveller59's avatar
traveller59 committed
204
205
206
207
        else:
            out_spatial_shape = spatial_shape
        # input.update_grid(out_spatial_shape)
        # t = time.time()
yanyan's avatar
yanyan committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
        out_tensor = input.shadow_copy()
        if input.benchmark:
            if self.name is None:
                raise ValueError(
                    "you need to assign name to spmodules before benchmark (spconv.utils.bench.assign_name_to_spmod)"
                )
            if self.name not in input.benchmark_record:
                input.benchmark_record[self.name] = {
                    "type": "SparseConvolution",
                    "indice_gen_time": [],
                    "time": [],
                    "num_points": [],
                    "num_out_points": [],
                    "params": {
                        "kernel_size": self.kernel_size,
                        "stride": self.stride,
                        "padding": self.padding,
                        "dilation": self.dilation,
                        "output_padding": self.output_padding,
                        "subm": self.subm,
                        "transposed": self.transposed,
                        "input_channels": self.in_channels,
                        "out_channels": self.out_channels,
                    }
                }
traveller59's avatar
traveller59 committed
233
        if self.conv1x1:
yan.yan's avatar
yan.yan committed
234
235
236
237
238
239
240
            if FILTER_HWIO:
                features = torch.mm(
                    input.features,
                    self.weight.view(self.out_channels, self.in_channels).T)
            else:
                features = torch.mm(
                    input.features,
yan.yan's avatar
yan.yan committed
241
                    self.weight.view(self.in_channels, self.out_channels))
yan.yan's avatar
yan.yan committed
242

traveller59's avatar
fix #17  
traveller59 committed
243
            if self.bias is not None:
traveller59's avatar
traveller59 committed
244
                features += self.bias
yan.yan's avatar
yan.yan committed
245
            out_tensor = out_tensor.replace_feature(features)
traveller59's avatar
traveller59 committed
246
            return out_tensor
yan.yan's avatar
v2.1  
yan.yan committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
        indice_dict = input.indice_dict.copy()
        
        algo = self.algo
        if self.indice_key is not None :
            datas = input.find_indice_pair(self.indice_key)
            if datas is not None:
                msg = "due to limitation of pytorch, you must provide same algo to layers share same indice key."
                assert algo == datas.algo, msg
                # algo = datas.algo
        if algo == ConvAlgo.Native:
            datas = input.find_indice_pair(self.indice_key)
            if datas is not None:
                assert isinstance(datas, IndiceData)
            if self.inverse:
                assert datas is not None and self.indice_key is not None
                assert datas.is_subm is False, "inverse conv can only be used with standard conv and pool ops."

                outids = datas.indices
yanyan's avatar
yanyan committed
265
266
                indice_pairs = datas.indice_pairs
                indice_pair_num = datas.indice_pair_num
yan.yan's avatar
v2.1  
yan.yan committed
267
268
269
270
                out_spatial_shape = datas.out_spatial_shape
                assert indice_pair_num.shape[0] == np.prod(
                    self.kernel_size
                ), "inverse conv must have same kernel size as its couple conv"
traveller59's avatar
traveller59 committed
271
            else:
yan.yan's avatar
v2.1  
yan.yan committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
                if self.indice_key is not None and datas is not None:
                    outids = datas.out_indices
                    indice_pairs = datas.indice_pairs
                    indice_pair_num = datas.indice_pair_num
                else:
                    if input.benchmark:
                        torch.cuda.synchronize()
                        t = time.time()
                    outids, indice_pairs, indice_pair_num = ops.get_indice_pairs(
                        indices, batch_size, spatial_shape, algo,
                        self.kernel_size, self.stride, self.padding,
                        self.dilation, self.output_padding, self.subm,
                        self.transposed)
                    if input.benchmark:
                        torch.cuda.synchronize()
                        interval = time.time() - t
                        out_tensor.benchmark_record[
                            self.name]["indice_gen_time"].append(interval)

                    indice_data = IndiceData(outids,
                                             indices,
                                             indice_pairs,
                                             indice_pair_num,
                                             spatial_shape,
                                             is_subm=self.subm,
                                             algo=algo)
                    if self.indice_key is not None:
                        msg = f"your indice key {self.indice_key} already exists in this sparse tensor."
                        assert self.indice_key not in indice_dict, msg
                        indice_dict[self.indice_key] = indice_data
            if input.benchmark:
                torch.cuda.synchronize()
                t = time.time()
            indice_pairs_calc = indice_pairs
            if indice_pairs.device != features.device:
                indice_pairs_calc = indice_pairs.to(features.device)
traveller59's avatar
traveller59 committed
308
309
            if self.subm:
                out_features = Fsp.indice_subm_conv(features, self.weight,
yan.yan's avatar
v2.1  
yan.yan committed
310
                                                    indice_pairs_calc,
traveller59's avatar
traveller59 committed
311
                                                    indice_pair_num,
yan.yan's avatar
v2.1  
yan.yan committed
312
                                                    outids.shape[0], algo)
traveller59's avatar
traveller59 committed
313
            else:
traveller59's avatar
traveller59 committed
314
                if self.inverse:
315
                    out_features = Fsp.indice_inverse_conv(
yan.yan's avatar
v2.1  
yan.yan committed
316
317
                        features, self.weight, indice_pairs_calc,
                        indice_pair_num, outids.shape[0], algo)
traveller59's avatar
traveller59 committed
318
319
                else:
                    out_features = Fsp.indice_conv(features, self.weight,
yan.yan's avatar
v2.1  
yan.yan committed
320
                                                   indice_pairs_calc,
321
                                                   indice_pair_num,
yan.yan's avatar
v2.1  
yan.yan committed
322
                                                   outids.shape[0], algo)
traveller59's avatar
traveller59 committed
323

yan.yan's avatar
v2.1  
yan.yan committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
        else:
            datas = input.find_indice_pair(self.indice_key)
            if datas is not None:
                assert isinstance(datas, ImplicitGemmIndiceData)
            if self.inverse:
                assert datas is not None and self.indice_key is not None
                assert datas.is_subm is False, "inverse conv can only be used with standard conv and pool ops."
                outids = datas.indices
                pair_fwd = datas.pair_bwd
                pair_bwd = datas.pair_fwd
                pair_mask_fwd_splits = datas.pair_mask_bwd_splits
                pair_mask_bwd_splits = datas.pair_mask_fwd_splits
                mask_argsort_fwd_splits = datas.mask_argsort_bwd_splits
                mask_argsort_bwd_splits = datas.mask_argsort_fwd_splits
                masks = datas.masks

            else:
                if self.indice_key is not None and datas is not None:
                    outids = datas.out_indices
                    pair_fwd = datas.pair_fwd
                    pair_bwd = datas.pair_bwd
                    pair_mask_fwd_splits = datas.pair_mask_fwd_splits
                    pair_mask_bwd_splits = datas.pair_mask_bwd_splits
                    mask_argsort_fwd_splits = datas.mask_argsort_fwd_splits
                    mask_argsort_bwd_splits = datas.mask_argsort_bwd_splits
                    masks = datas.masks
                else:
                    res = ops.get_indice_pairs_implicit_gemm(
                        indices,
                        batch_size,
                        spatial_shape,
                        algo,
                        ksize=self.kernel_size,
                        stride=self.stride,
                        padding=self.padding,
                        dilation=self.dilation,
                        out_padding=self.output_padding,
                        subm=self.subm,
                        transpose=self.transposed,
                        is_train=self.training,
                        alloc=input.thrust_allocator)
                    outids = res[0]
                    num_inds_per_loc = res[1]
                    pair_fwd = res[2]
                    pair_bwd = res[3]
                    pair_mask_fwd_splits = res[4]
                    pair_mask_bwd_splits = res[5]
                    mask_argsort_fwd_splits = res[6]
                    mask_argsort_bwd_splits = res[7]
                    masks = res[8]
                    if self.indice_key is not None:
                        indice_data = ImplicitGemmIndiceData(
                            outids,
                            indices,
                            pair_fwd,
                            pair_bwd,
                            pair_mask_fwd_splits=pair_mask_fwd_splits,
                            pair_mask_bwd_splits=pair_mask_bwd_splits,
                            mask_argsort_fwd_splits=mask_argsort_fwd_splits,
                            mask_argsort_bwd_splits=mask_argsort_bwd_splits,
                            masks=masks,
                            is_subm=self.subm,
                            out_spatial_shape=out_spatial_shape,
                            algo=algo)
                        msg = f"your indice key {self.indice_key} already exists in this sparse tensor."
                        assert self.indice_key not in indice_dict, msg
                        indice_dict[self.indice_key] = indice_data
            if input.benchmark:
                torch.cuda.synchronize()
                t = time.time()
            num_activate_out = outids.shape[0]
            out_features = Fsp.implicit_gemm(
                features, self.weight, pair_fwd, pair_bwd,
                pair_mask_fwd_splits, pair_mask_bwd_splits,
                mask_argsort_fwd_splits, mask_argsort_bwd_splits,
                num_activate_out, masks, self.training, self.subm)
        if self.bias is not None:
            out_features += self.bias
yanyan's avatar
yanyan committed
402
403
404
405
406
407
408
409
        if input.benchmark:
            torch.cuda.synchronize()
            interval = time.time() - t
            out_tensor.benchmark_record[self.name]["time"].append(interval)
            out_tensor.benchmark_record[self.name]["num_points"].append(
                features.shape[0])
            out_tensor.benchmark_record[self.name]["num_out_points"].append(
                out_features.shape[0])
yan.yan's avatar
yan.yan committed
410
        out_tensor = out_tensor.replace_feature(out_features)
yanyan's avatar
yanyan committed
411
        out_tensor.indices = outids
yan.yan's avatar
v2.1  
yan.yan committed
412
        out_tensor.indice_dict = indice_dict
yanyan's avatar
yanyan committed
413
        out_tensor.spatial_shape = out_spatial_shape
traveller59's avatar
traveller59 committed
414
415
        return out_tensor

yan.yan's avatar
v2.1  
yan.yan committed
416

yan.yan's avatar
yan.yan committed
417
418
419
420
421
422
423
424
425
426
427
class SparseConv1d(SparseConvolution):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 groups=1,
                 bias=True,
                 indice_key=None,
yan.yan's avatar
v2.1  
yan.yan committed
428
                 algo: Optional[ConvAlgo] = None,
yan.yan's avatar
yan.yan committed
429
430
431
432
433
434
435
436
437
438
439
440
441
442
                 name=None):
        super(SparseConv1d, self).__init__(1,
                                           in_channels,
                                           out_channels,
                                           kernel_size,
                                           stride,
                                           padding,
                                           dilation,
                                           groups,
                                           bias,
                                           indice_key=indice_key,
                                           algo=algo,
                                           name=name)

traveller59's avatar
traveller59 committed
443
444
445
446
447
448
449
450
451
452
453

class SparseConv2d(SparseConvolution):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 groups=1,
                 bias=True,
454
                 indice_key=None,
yan.yan's avatar
v2.1  
yan.yan committed
455
                 algo: Optional[ConvAlgo] = None,
yanyan's avatar
yanyan committed
456
                 name=None):
457
458
459
460
461
462
463
464
465
466
        super(SparseConv2d, self).__init__(2,
                                           in_channels,
                                           out_channels,
                                           kernel_size,
                                           stride,
                                           padding,
                                           dilation,
                                           groups,
                                           bias,
                                           indice_key=indice_key,
yanyan's avatar
yanyan committed
467
468
                                           algo=algo,
                                           name=name)
traveller59's avatar
traveller59 committed
469
470
471
472
473
474
475
476
477
478
479
480


class SparseConv3d(SparseConvolution):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 groups=1,
                 bias=True,
481
                 indice_key=None,
yan.yan's avatar
v2.1  
yan.yan committed
482
                 algo: Optional[ConvAlgo] = None,
yanyan's avatar
yanyan committed
483
                 name=None):
484
485
486
487
488
489
490
491
492
493
        super(SparseConv3d, self).__init__(3,
                                           in_channels,
                                           out_channels,
                                           kernel_size,
                                           stride,
                                           padding,
                                           dilation,
                                           groups,
                                           bias,
                                           indice_key=indice_key,
yanyan's avatar
yanyan committed
494
495
                                           algo=algo,
                                           name=name)
496

traveller59's avatar
traveller59 committed
497

traveller59's avatar
traveller59 committed
498
499
500
501
502
503
504
505
506
507
class SparseConv4d(SparseConvolution):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 groups=1,
                 bias=True,
508
                 indice_key=None,
yan.yan's avatar
v2.1  
yan.yan committed
509
                 algo: Optional[ConvAlgo] = None,
yanyan's avatar
yanyan committed
510
                 name=None):
511
512
513
514
515
516
517
518
519
520
        super(SparseConv4d, self).__init__(4,
                                           in_channels,
                                           out_channels,
                                           kernel_size,
                                           stride,
                                           padding,
                                           dilation,
                                           groups,
                                           bias,
                                           indice_key=indice_key,
yanyan's avatar
yanyan committed
521
522
                                           algo=algo,
                                           name=name)
traveller59's avatar
traveller59 committed
523
524


yan.yan's avatar
bug fix  
yan.yan committed
525
526
527
528
529
530
531
532
533
534
535
class SparseConvTranspose1d(SparseConvolution):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 groups=1,
                 bias=True,
                 indice_key=None,
yan.yan's avatar
v2.1  
yan.yan committed
536
                 algo: Optional[ConvAlgo] = None,
yan.yan's avatar
bug fix  
yan.yan committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
                 name=None):
        super(SparseConvTranspose1d, self).__init__(1,
                                                    in_channels,
                                                    out_channels,
                                                    kernel_size,
                                                    stride,
                                                    padding,
                                                    dilation,
                                                    groups,
                                                    bias,
                                                    transposed=True,
                                                    indice_key=indice_key,
                                                    algo=algo,
                                                    name=name)


traveller59's avatar
traveller59 committed
553
554
555
556
557
558
559
560
561
562
class SparseConvTranspose2d(SparseConvolution):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 groups=1,
                 bias=True,
563
                 indice_key=None,
yan.yan's avatar
v2.1  
yan.yan committed
564
                 algo: Optional[ConvAlgo] = None,
yanyan's avatar
yanyan committed
565
                 name=None):
566
567
568
569
570
571
572
573
574
575
576
        super(SparseConvTranspose2d, self).__init__(2,
                                                    in_channels,
                                                    out_channels,
                                                    kernel_size,
                                                    stride,
                                                    padding,
                                                    dilation,
                                                    groups,
                                                    bias,
                                                    transposed=True,
                                                    indice_key=indice_key,
yanyan's avatar
yanyan committed
577
578
                                                    algo=algo,
                                                    name=name)
traveller59's avatar
traveller59 committed
579
580
581
582
583
584
585
586
587
588
589
590


class SparseConvTranspose3d(SparseConvolution):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 groups=1,
                 bias=True,
591
                 indice_key=None,
yan.yan's avatar
v2.1  
yan.yan committed
592
                 algo: Optional[ConvAlgo] = None,
yanyan's avatar
yanyan committed
593
                 name=None):
594
595
596
597
598
599
600
601
602
603
604
        super(SparseConvTranspose3d, self).__init__(3,
                                                    in_channels,
                                                    out_channels,
                                                    kernel_size,
                                                    stride,
                                                    padding,
                                                    dilation,
                                                    groups,
                                                    bias,
                                                    transposed=True,
                                                    indice_key=indice_key,
yanyan's avatar
yanyan committed
605
606
                                                    algo=algo,
                                                    name=name)
traveller59's avatar
traveller59 committed
607

yan.yan's avatar
v2.1  
yan.yan committed
608

yan.yan's avatar
bug fix  
yan.yan committed
609
610
611
612
613
614
615
616
617
618
619
class SparseConvTranspose4d(SparseConvolution):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 groups=1,
                 bias=True,
                 indice_key=None,
yan.yan's avatar
v2.1  
yan.yan committed
620
                 algo: Optional[ConvAlgo] = None,
yan.yan's avatar
bug fix  
yan.yan committed
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
                 name=None):
        super(SparseConvTranspose4d, self).__init__(4,
                                                    in_channels,
                                                    out_channels,
                                                    kernel_size,
                                                    stride,
                                                    padding,
                                                    dilation,
                                                    groups,
                                                    bias,
                                                    transposed=True,
                                                    indice_key=indice_key,
                                                    algo=algo,
                                                    name=name)


yan.yan's avatar
yan.yan committed
637
638
639
640
641
642
643
class SparseInverseConv1d(SparseConvolution):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 indice_key,
                 bias=True,
yan.yan's avatar
v2.1  
yan.yan committed
644
                 algo: Optional[ConvAlgo] = None,
yan.yan's avatar
yan.yan committed
645
646
647
648
649
650
651
652
653
654
655
                 name=None):
        super(SparseInverseConv1d, self).__init__(1,
                                                  in_channels,
                                                  out_channels,
                                                  kernel_size,
                                                  bias=bias,
                                                  inverse=True,
                                                  indice_key=indice_key,
                                                  algo=algo,
                                                  name=name)

traveller59's avatar
traveller59 committed
656

traveller59's avatar
traveller59 committed
657
658
659
660
class SparseInverseConv2d(SparseConvolution):
    def __init__(self,
                 in_channels,
                 out_channels,
661
                 kernel_size,
traveller59's avatar
traveller59 committed
662
                 indice_key,
Yan Yan's avatar
Yan Yan committed
663
                 bias=True,
yan.yan's avatar
v2.1  
yan.yan committed
664
                 algo: Optional[ConvAlgo] = None,
yanyan's avatar
yanyan committed
665
                 name=None):
666
667
668
669
670
671
        super(SparseInverseConv2d, self).__init__(2,
                                                  in_channels,
                                                  out_channels,
                                                  kernel_size,
                                                  bias=bias,
                                                  inverse=True,
Yan Yan's avatar
Yan Yan committed
672
                                                  indice_key=indice_key,
yanyan's avatar
yanyan committed
673
674
                                                  algo=algo,
                                                  name=name)
traveller59's avatar
traveller59 committed
675
676
677
678
679
680


class SparseInverseConv3d(SparseConvolution):
    def __init__(self,
                 in_channels,
                 out_channels,
681
                 kernel_size,
traveller59's avatar
traveller59 committed
682
                 indice_key,
Yan Yan's avatar
Yan Yan committed
683
                 bias=True,
yan.yan's avatar
v2.1  
yan.yan committed
684
                 algo: Optional[ConvAlgo] = None,
yanyan's avatar
yanyan committed
685
                 name=None):
686
687
688
689
690
691
        super(SparseInverseConv3d, self).__init__(3,
                                                  in_channels,
                                                  out_channels,
                                                  kernel_size,
                                                  bias=bias,
                                                  inverse=True,
Yan Yan's avatar
Yan Yan committed
692
                                                  indice_key=indice_key,
yanyan's avatar
yanyan committed
693
694
                                                  algo=algo,
                                                  name=name)
traveller59's avatar
traveller59 committed
695

yan.yan's avatar
v2.1  
yan.yan committed
696

yan.yan's avatar
yan.yan committed
697
698
699
700
701
702
703
class SparseInverseConv4d(SparseConvolution):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 indice_key,
                 bias=True,
yan.yan's avatar
v2.1  
yan.yan committed
704
                 algo: Optional[ConvAlgo] = None,
yan.yan's avatar
yan.yan committed
705
706
707
708
709
710
711
712
713
714
715
                 name=None):
        super(SparseInverseConv4d, self).__init__(4,
                                                  in_channels,
                                                  out_channels,
                                                  kernel_size,
                                                  bias=bias,
                                                  inverse=True,
                                                  indice_key=indice_key,
                                                  algo=algo,
                                                  name=name)

yan.yan's avatar
v2.1  
yan.yan committed
716

yan.yan's avatar
yan.yan committed
717
718
719
720
721
722
723
724
725
726
727
class SubMConv1d(SparseConvolution):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 groups=1,
                 bias=True,
                 indice_key=None,
yan.yan's avatar
v2.1  
yan.yan committed
728
                 algo: Optional[ConvAlgo] = None,
yan.yan's avatar
yan.yan committed
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
                 name=None):
        super(SubMConv1d, self).__init__(1,
                                         in_channels,
                                         out_channels,
                                         kernel_size,
                                         stride,
                                         padding,
                                         dilation,
                                         groups,
                                         bias,
                                         True,
                                         indice_key=indice_key,
                                         algo=algo,
                                         name=name)

traveller59's avatar
traveller59 committed
744
745
746
747
748
749
750
751
752
753
754

class SubMConv2d(SparseConvolution):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 groups=1,
                 bias=True,
755
                 indice_key=None,
yan.yan's avatar
v2.1  
yan.yan committed
756
                 algo: Optional[ConvAlgo] = None,
yanyan's avatar
yanyan committed
757
                 name=None):
758
759
760
761
762
763
764
765
766
767
768
        super(SubMConv2d, self).__init__(2,
                                         in_channels,
                                         out_channels,
                                         kernel_size,
                                         stride,
                                         padding,
                                         dilation,
                                         groups,
                                         bias,
                                         True,
                                         indice_key=indice_key,
yanyan's avatar
yanyan committed
769
770
                                         algo=algo,
                                         name=name)
traveller59's avatar
traveller59 committed
771
772
773
774
775
776
777
778
779
780
781
782


class SubMConv3d(SparseConvolution):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 groups=1,
                 bias=True,
783
                 indice_key=None,
yan.yan's avatar
v2.1  
yan.yan committed
784
                 algo: Optional[ConvAlgo] = None,
yanyan's avatar
yanyan committed
785
                 name=None):
786
787
788
789
790
791
792
793
794
795
796
        super(SubMConv3d, self).__init__(3,
                                         in_channels,
                                         out_channels,
                                         kernel_size,
                                         stride,
                                         padding,
                                         dilation,
                                         groups,
                                         bias,
                                         True,
                                         indice_key=indice_key,
yanyan's avatar
yanyan committed
797
798
                                         algo=algo,
                                         name=name)
799

traveller59's avatar
traveller59 committed
800
801
802
803
804
805
806
807
808
809
810

class SubMConv4d(SparseConvolution):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 groups=1,
                 bias=True,
811
                 indice_key=None,
yan.yan's avatar
v2.1  
yan.yan committed
812
                 algo: Optional[ConvAlgo] = None,
yanyan's avatar
yanyan committed
813
                 name=None):
814
815
816
817
818
819
820
821
822
823
824
        super(SubMConv4d, self).__init__(4,
                                         in_channels,
                                         out_channels,
                                         kernel_size,
                                         stride,
                                         padding,
                                         dilation,
                                         groups,
                                         bias,
                                         True,
                                         indice_key=indice_key,
yanyan's avatar
yanyan committed
825
826
                                         algo=algo,
                                         name=name)