forked from modular/modular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconv.py
More file actions
785 lines (663 loc) · 26.4 KB
/
conv.py
File metadata and controls
785 lines (663 loc) · 26.4 KB
1
2
3
4
5
6
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2025, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# 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.
# ===----------------------------------------------------------------------=== #
from dataclasses import dataclass
from typing import Optional, Union
import max.driver as md
from max.dtype import DType
from max.graph import DeviceRef, TensorValue, TensorValueLike, Weight, ops
from max.graph.type import FilterLayout
from .layer import Layer, Module
class Conv2D(Module):
"""A 2D convolution over an input signal composed of several input
planes.
Example:
.. code-block:: python
conv = nn.Conv2D(
kernel_size=3,
in_channels=64,
out_channels=128,
dtype=DType.float32,
stride=1,
padding=0,
has_bias=False,
name="conv2d_weight",
device=DeviceRef.GPU(),
)
"""
device: Union[DeviceRef, None]
"""The device where matrix operations are performed."""
filter: Weight
"""The weight matrix stored on CPU with shape (height, width, in_channels / num_groups, out_channels).
Model init moves the weight to :obj:`device`."""
stride: tuple[int, int]
"""Controls the stride for the cross-correlation."""
padding: tuple[int, int, int, int]
"""Controls the amount of padding applied before and after the input for height and width dimensions."""
dilation: tuple[int, int]
"""Controls the dilation rate."""
num_groups: int
"""Number of blocked connections from input channels to output channels."""
bias: Union[Weight, None] = None
"""The optional bias vector stored on CPU with shape (out_channels,).
Model init moves the bias to :obj:`device` if present."""
permute: bool = False
"""bool controls whether self.filter is permuted from PyTorch order to max order.
PyTorch order is: (out_channels, in_channels / num_groups, height, width)
Max API order: (height, width, in_channels / num_groups, out_channels)."""
def __init__(
self,
kernel_size: Union[int, tuple[int, int]],
in_channels: int,
out_channels: int,
dtype: DType,
stride: Union[int, tuple[int, int]] = 1,
padding: Union[int, tuple[int, int], tuple[int, int, int, int]] = 0,
dilation: Union[int, tuple[int, int]] = 1,
num_groups: int = 1,
device: Union[DeviceRef, None] = None,
has_bias: bool = False,
permute: bool = False,
name: Union[str, None] = None,
) -> None:
"""Initializes the Conv2D layer with weights and optional bias.
Args:
kernel_size: Size of the convolving kernel. Can be a single int or tuple (height, width).
in_channels: Number of channels in the input image.
out_channels: Number of channels produced by the convolution.
dtype: The data type for both weights and bias.
stride: Stride of the convolution. Default: 1
padding: Padding added to input. Can be int or tuple (pad_top, pad_bottom, pad_left, pad_right). Default: 0
dilation: Spacing between kernel elements. Default: 1
num_groups: Number of blocked connections from input channels to output channels. Default: 1
device: The target device for computation.
Weights remain on CPU until moved during computation.
name: Base name for weights (appended with ``.weight`` and
``.bias`` if applicable).
has_bias: When :obj:`True`, adds a bias vector to the layer.
Defaults to :obj:`False`.
permute: When :obj:`True`, permutes weights from PyTorch format to Max format.
Defaults to :obj:`False`.
"""
super().__init__()
self.device = device
self.permute = permute
# Handle kernel_size as int or tuple
if isinstance(kernel_size, int):
kernel_height = kernel_width = kernel_size
else:
kernel_height, kernel_width = kernel_size
self.filter = Weight(
name=f"{name}.weight" if name else "weight",
dtype=dtype,
shape=(
[
out_channels,
in_channels // num_groups,
kernel_height,
kernel_width,
]
if self.permute
else [
kernel_height,
kernel_width,
in_channels // num_groups,
out_channels,
]
),
device=self.device or DeviceRef.CPU(),
)
if has_bias:
self.bias = Weight(
name=f"{name}.bias" if name else "bias",
dtype=dtype,
shape=(out_channels,),
device=self.device or DeviceRef.CPU(),
)
# Convert scalar parameters to tuples as needed
self.stride = (stride, stride) if isinstance(stride, int) else stride
if isinstance(padding, int):
padding = (padding, padding, padding, padding)
elif len(padding) == 2:
# Convert (pad_h, pad_w) to (pad_top, pad_bottom, pad_left, pad_right)
pad_h, pad_w = padding
padding = (pad_h, pad_h, pad_w, pad_w)
self.padding = padding
if isinstance(dilation, int):
dilation = (dilation, dilation)
self.dilation = dilation
self.num_groups = num_groups
if (
isinstance(self.filter, Weight)
and self.filter.quantization_encoding is not None
):
raise ValueError("Conv2D not implemented with weight quantization.")
def __call__(self, x: TensorValue) -> TensorValue:
"""Apply 2D convolution to input `x`. Permutes pytorch weights to match max API if permute=True.
Args:
x: a tensor of shape [batch_size, height, width, in_channels]
if self.permute, then input is of shape: [batch_size, in_channels, height, width]
and will be permuted to match max's expected input shape.
Returns:
a tensor of shape [batch_size, new_height, new_width, out_channels]
if self.permute, then output shape will be [batch_size, out_channels, new_height, new_width]
"""
weight: TensorValue = self.filter
is_nvidia_gpu = (
isinstance(self.device, DeviceRef)
and self.device.is_gpu()
and md.accelerator_api() == "cuda"
)
if self.permute:
# Input: [batch_size, in_channels, height, width] -> [batch_size, height, width, in_channels]
x = ops.permute(x, [0, 2, 3, 1])
# GPU supports FCRS but CPU doesn't. On CPU, permute from
# FCRS to RSCF format.
if not is_nvidia_gpu:
# Permute weight from [out_channels, in_channels // num_groups, height, width]
# to [height, width, in_channels // num_groups, out_channels] (RSCF)
weight = ops.permute(weight, [2, 3, 1, 0])
output = ops.conv2d(
x,
weight,
self.stride,
self.dilation,
self.padding,
self.num_groups,
self.bias,
filter_layout=FilterLayout.FCRS
if (self.permute and is_nvidia_gpu)
else FilterLayout.RSCF,
)
if self.permute:
# Output: [batch_size, new_height, new_width, out_channels] -> [batch_size, out_channels, new_height, new_width]
output = ops.permute(output, [0, 3, 1, 2])
return output
@dataclass
class Conv2DV1(Layer):
"""A 2D convolution over an input signal composed of several input
planes.
Example:
.. code-block:: python
conv = nn.Conv2DV1(
filter=filter_2d,
bias=bias_2d,
stride=2,
padding=1
)
output = conv(x)
"""
filter: TensorValueLike
bias: Optional[TensorValueLike] = None
stride: Union[int, tuple[int, int]] = (1, 1)
padding: Union[int, tuple[int, int, int, int]] = (0, 0, 0, 0)
dilation: Union[int, tuple[int, int]] = (1, 1)
groups: int = 1
def __call__(self, x: TensorValue) -> TensorValue:
# These need to be casted as the underlying ops.conv2d call
# expects them to only be tuple types.
if isinstance(self.stride, int):
self.stride = (self.stride, self.stride)
if isinstance(self.padding, int):
self.padding = (
self.padding,
self.padding,
self.padding,
self.padding,
)
if isinstance(self.dilation, int):
self.dilation = (self.dilation, self.dilation)
if (
isinstance(self.filter, Weight)
and self.filter.quantization_encoding is not None
):
raise ValueError(
"Conv2DV1 not implemented with weight quantization."
)
return ops.conv2d(
x,
self.filter,
self.stride,
self.dilation,
self.padding,
self.groups,
self.bias,
)
@dataclass
class Conv1DV1(Layer):
"""A 1D convolution over an input signal composed of several input
planes.
Deprecated: Use `Conv1D` instead.
Example:
.. code-block:: python
conv = nn.Conv1DV1(
filter=filter_1d,
bias=bias_1d,
stride=1,
padding=1
)
"""
filter: TensorValueLike # [kernel_size, in_channels, out_channels]
bias: Optional[TensorValueLike] = None
stride: int = 1
padding: int = 0
dilation: int = 1
groups: int = 1
def __call__(self, x: TensorValueLike) -> TensorValue:
"""
Args:
x: a tensor of shape [batch_size, length, in_channels]
Returns:
a tensor of shape [batch_size, new_length, out_channels]
new_length = ((length + 2 * padding - (kernel_size - 1) - 1) / stride) + 1
"""
# TODO(GEX-327): Support Conv1D in mo rather than implementing it using Conv2DV1.
# Reshape [batch_size, length, in_channels] to [batch_size, height=1, length, in_channels].
x = ops.unsqueeze(x, 1)
# Reshape [kernel_size, in_channels, out_channels] to [height=1, kernel_size, in_channels, out_channels].
filter = ops.unsqueeze(self.filter, 0)
if (
isinstance(self.filter, Weight)
and self.filter.quantization_encoding is not None
):
raise ValueError("Conv1D not implemented with weight quantization.")
else:
output = ops.conv2d(
x,
filter,
(1, self.stride),
(1, self.dilation),
(0, 0, self.padding, self.padding),
self.groups,
self.bias,
)
# Reshape [batch_size, height=1, new_length, out_channels] to [batch_size, new_length, out_channels].
return ops.squeeze(output, 1)
class Conv1D(Module):
"""A 1D convolution over an input signal composed of several input
planes.
Example:
.. code-block:: python
conv = nn.Conv1D(
kernel_size=3,
in_channels=64,
out_channels=128,
dtype=DType.float32,
stride=1,
padding=0,
has_bias=False,
name="conv1d_weight",
device=DeviceRef.GPU(),
)
"""
device: Union[DeviceRef, None]
"""The device where matrix operations are performed."""
filter: Weight
"""The weight matrix stored on CPU with shape (kernel_size, in_channels / num_groups, out_channels).
Model init moves the weight to :obj:`device`."""
stride: int
"""Controls the stride for the cross-correlation."""
padding: int
"""Controls the amount of padding applied before and after the input."""
dilation: int
"""Controls the dilation rate."""
num_groups: int
"""Number of blocked connections from input channels to output channels."""
bias: Union[Weight, None] = None
"""The optional bias vector stored on CPU with shape (out_channels,).
Model init moves the bias to :obj:`device` if present."""
permute: bool = False
"""bool controls whether self.filter is permuted from PyTorch order to max order.
PyTorch order is: (out_channels, in_channels / num_groups, kernel_size)
Max API order: (kernel_size, in_channels / num_groups, out_channels)."""
def __init__(
self,
kernel_size: int,
in_channels: int,
out_channels: int,
dtype: DType,
stride: int = 1,
padding: int = 0,
dilation: int = 1,
num_groups: int = 1,
device: Union[DeviceRef, None] = None,
has_bias: bool = False,
permute: bool = False,
name: Union[str, None] = None,
) -> None:
"""Initializes the Conv1D layer with weights and optional bias.
Args:
kernel_size: Size of the convolving kernel.
in_channels: Number of channels in the input signal.
out_channels: Number of channels produced by the convolution.
dtype: The data type for both weights and bias.
stride: Stride of the convolution. Default: 1
padding: Padding added to both sides of the input. Default: 0
dilation: Spacing between kernel elements. Default: 1
num_groups: Number of blocked connections from input channels to output channels. Default: 1
device: The target device for computation.
Weights remain on CPU until moved during computation.
name: Base name for weights (appended with ``.weight`` and
``.bias`` if applicable).
has_bias: When :obj:`True`, adds a bias vector to the layer.
Defaults to :obj:`False`.
"""
super().__init__()
self.device = device
self.permute = permute
if self.permute:
self.filter = Weight(
name=f"{name}.weight" if name else "weight",
dtype=dtype,
shape=[out_channels, in_channels // num_groups, kernel_size],
device=self.device or DeviceRef.CPU(),
)
else:
self.filter = Weight(
name=f"{name}.weight" if name else "weight",
dtype=dtype,
shape=[kernel_size, in_channels // num_groups, out_channels],
device=self.device or DeviceRef.CPU(),
)
if has_bias:
self.bias = Weight(
name=f"{name}.bias" if name else "bias",
dtype=dtype,
shape=(out_channels,),
device=self.device or DeviceRef.CPU(),
)
self.stride = stride
self.padding = padding
self.dilation = dilation
self.num_groups = num_groups
if (
isinstance(self.filter, Weight)
and self.filter.quantization_encoding is not None
):
raise ValueError("Conv1D not implemented with weight quantization.")
def __call__(self, x: TensorValue) -> TensorValue:
"""Applied 1D convolution to input `x`. Permutes pytorch weights to match max API if permute=True.
Args:
x: a tensor of shape [batch_size, length, in_channels]
if self.permute, then input is of shape: [batch_size, in_channels, length]
and will be permuted to match max's expected input shape.
Returns:
a tensor of shape [batch_size, new_length, out_channels]
if self.permute, then output shape will be [batch_size, out_channels, new_length]
new_length = ((length + 2 * padding - (kernel_size - 1) - 1) / stride) + 1
"""
weight: TensorValue = self.filter
is_nvidia_gpu = (
isinstance(self.device, DeviceRef)
and self.device.is_gpu()
and md.accelerator_api() == "cuda"
)
if self.permute:
x = ops.permute(x, [0, 2, 1]) # [batch_size, length, in_channels]
# GPU supports FCRS but CPU doesn't. On CPU, permute from
# FCS to SCF, then add dummy dim to become RSCF.
if not is_nvidia_gpu:
weight = ops.unsqueeze(ops.permute(weight, [2, 1, 0]), 0)
# on GPU, unsqueeze FCS to FCRS
else:
weight = ops.unsqueeze(weight, 2)
# No permute, filer is SCF and unsqueeze to RSCF.
else:
weight = ops.unsqueeze(weight, 0)
# Reshape for Conv2DV1
x = ops.unsqueeze(x, 1) # [batch_size, height=1, length, in_channels]
output = ops.conv2d(
x,
weight,
(1, self.stride),
(1, self.dilation),
(0, 0, self.padding, self.padding),
self.num_groups,
self.bias,
filter_layout=FilterLayout.FCRS
if (self.permute and is_nvidia_gpu)
else FilterLayout.RSCF,
)
# Reshape back from Conv2DV1
output = ops.squeeze(
output, 1
) # [batch_size, new_length, out_channels]
if self.permute:
output = ops.permute(
output, [0, 2, 1]
) # [batch_size, out_channels, new_length]
return output
@dataclass
class Conv3DV1(Layer):
"""A 3D convolution over an input signal composed of several input
planes.
Deprecated: Use `Conv3D` instead.
Example:
.. code-block:: python
conv = nn.Conv3DV1(
filter=filter_3d,
bias=bias_3d,
stride=1,
padding=1
)
"""
filter: TensorValueLike # [depth, height, width, in_channels / num_groups, out_channels]
bias: Optional[TensorValueLike] = None # [out_channels]
stride: Union[int, tuple[int, int, int]] = (1, 1, 1)
padding: Union[int, tuple[int, int, int, int, int, int]] = (
0,
0,
0,
0,
0,
0,
)
dilation: Union[int, tuple[int, int, int]] = (1, 1, 1)
groups: int = 1
def __call__(self, x: TensorValueLike) -> TensorValue:
"""
Args:
x: a tensor of shape (batch_size, depth, height, width, in_channels)
Returns:
a tensor of shape (batch_size, new_depth, new_height, new_width, out_channels)
"""
# These need to be casted as the underlying ops.conv3d call
# expects them to only be tuple types.
if isinstance(self.stride, int):
self.stride = (self.stride, self.stride, self.stride)
if isinstance(self.padding, int):
self.padding = (
self.padding,
self.padding,
self.padding,
self.padding,
self.padding,
self.padding,
)
if isinstance(self.dilation, int):
self.dilation = (self.dilation, self.dilation, self.dilation)
if (
isinstance(self.filter, Weight)
and self.filter.quantization_encoding is not None
):
raise ValueError("Conv3D not implemented with weight quantization.")
return ops.conv3d(
x,
self.filter,
self.stride,
self.dilation,
self.padding,
self.groups,
self.bias,
)
class Conv3D(Module):
"""A 3D convolution over an input signal composed of several input
planes.
Example:
.. code-block:: python
conv = nn.Conv3D(
depth=,
height=,
width=,
in_channels=,
out_channels=,
dtype=DType.float32,
stride=1,
padding=0,
has_bias=False,
name="conv3d_weight",
device=DeviceRef.GPU(),
)
"""
device: Union[DeviceRef, None]
"""The device where matrix operations are performed."""
filter: Weight
"""The weight matrix stored on CPU with shape (depth, height, width, in_channels / num_groups, out_channels).
Model init moves the weight to :obj:`device`."""
stride: tuple[int, int, int]
"""Controls the stride for the cross-correlation. """
padding: tuple[int, int, int, int, int, int]
"""Controls the amount of padding applied before and after the input for depth, height, and width dimensions."""
dilation: tuple[int, int, int]
"""Not implemented yet. Assuming dilation = 1 for now."""
num_groups: int
"""Not implemented yet. Assuming num_groups = 1 for now."""
bias: Union[Weight, None] = None
"""The optional bias vector stored on CPU with shape (out_channels,).
Model init moves the bias to :obj:`device` if present."""
permute: bool = False
"""bool controls whether self.filter is permuted from PyTorch order to max order.
PyTorch order is: (out_channels, in_channels / num_groups, depth, height, width)
Max API order: (depth, height, width, in_channels / num_groups, out_channels). """
def __init__(
self,
depth: int,
height: int,
width: int,
in_channels: int,
out_channels: int,
dtype: DType,
stride: Union[int, tuple[int, int, int]] = 1,
padding: Union[int, tuple[int, int, int, int, int, int]] = 0,
dilation: Union[int, tuple[int, int, int]] = 1,
num_groups: int = 1,
device: Union[DeviceRef, None] = None,
has_bias: bool = False,
permute: bool = False,
name: Union[str, None] = None,
) -> None:
"""Initializes the Conv3D layer with weights and optional bias.
Args:
depth: kernel_size[0]
height: kernel_size[1]
width: kernel_size[2]
in_channels: number of channels in the input image.
out_channels: dimensionality of the output.
dtype: The data type for both weights and bias.
stride: Stride of the convolution. Default: 1
padding: Padding added to all six sides of the input. Default: 0
dilation: Spacing between kernel elements. Default: 1
num_groups: Number of blocked connections from input channels to output channels. Default: 1.
device: The target device for computation.
Weights remain on CPU until moved during computation.
name: Base name for weights (appended with ``.weight`` and
``.bias`` if applicable).
has_bias: When :obj:`True`, adds a bias vector to the layer.
Defaults to :obj:`False`.
"""
super().__init__()
self.device = device
self.permute = permute
if self.permute:
self.filter = Weight(
name=f"{name}.weight" if name else "weight",
dtype=dtype,
shape=[
out_channels,
in_channels // num_groups,
depth,
height,
width,
],
device=self.device or DeviceRef.CPU(),
)
else:
self.filter = Weight(
name=f"{name}.weight" if name else "weight",
dtype=dtype,
shape=[
depth,
height,
width,
in_channels // num_groups,
out_channels,
],
device=self.device or DeviceRef.CPU(),
)
if has_bias:
self.bias = Weight(
name=f"{name}.bias" if name else "bias",
dtype=dtype,
shape=(out_channels,),
device=self.device or DeviceRef.CPU(),
)
# These need to be casted as the underlying ops.conv3d call
# expects them to only be tuple types.
if isinstance(stride, int):
stride = (stride, stride, stride)
self.stride = stride
if isinstance(padding, int):
padding = (
padding,
padding,
padding,
padding,
padding,
padding,
)
self.padding = padding
if isinstance(dilation, int):
dilation = (dilation, dilation, dilation)
self.dilation = dilation
self.num_groups = num_groups
if (
isinstance(self.filter, Weight)
and self.filter.quantization_encoding is not None
):
raise ValueError("Conv3D not implemented with weight quantization.")
def __call__(self, x: TensorValue) -> TensorValue:
"""Applied 3D convolution to input `x`. Permutes pytorch weights to match max API if permute=True.
Args:
x: a tensor of shape (batch_size, depth, height, width, in_channels)
if self.permute, then input is of shape: (batch_size, in_channels, depth, height, width)
and will be permuted to match max's expected input shape.
Returns:
a tensor of shape (batch_size, new_depth, new_height, new_width, out_channels).
if self.permute, then the output shape will be (batch_size, out_channels, new_depth, new_height, new_width)
"""
weight: TensorValue = self.filter
if self.permute:
weight = ops.permute(self.filter, [2, 3, 4, 1, 0])
x = ops.permute(x, [0, 2, 3, 4, 1])
res = ops.conv3d(
x,
weight,
self.stride,
self.dilation,
self.padding,
self.num_groups,
self.bias,
)
# permute output from (batch_size, depth, height, width, out_channels) (batch_size, out_channels, depth, height, width).
if self.permute:
res = ops.permute(res, [0, 4, 1, 2, 3])
return res