forked from flagos-ai/FlagGems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcudnn_convolution.py
More file actions
92 lines (82 loc) · 2.64 KB
/
Copy pathcudnn_convolution.py
File metadata and controls
92 lines (82 loc) · 2.64 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
import logging
from flag_gems.ops.conv1d import conv1d
from flag_gems.ops.conv2d import conv2d
from flag_gems.ops.conv3d import conv3d
logger = logging.getLogger(__name__)
def cudnn_convolution(
input,
weight,
padding,
stride,
dilation,
groups,
benchmark,
deterministic,
allow_tf32,
):
"""
CUDNN convolution operation.
This is a lower-level convolution operation that does not include bias.
It supports 1D, 2D, and 3D convolutions based on the input dimensionality.
Args:
input: Input tensor of shape (N, C_in, *spatial_dims)
weight: Weight tensor of shape (C_out, C_in/groups, *kernel_dims)
padding: Padding for each spatial dimension
stride: Stride for each spatial dimension
dilation: Dilation for each spatial dimension
groups: Number of groups for grouped convolution
benchmark: cuDNN benchmark flag (ignored in Triton implementation)
deterministic: cuDNN deterministic flag (ignored in Triton implementation)
allow_tf32: Allow TF32 computation flag (ignored in Triton implementation)
Returns:
Output tensor after convolution
"""
logger.debug("GEMS CUDNN_CONVOLUTION")
ndim = input.ndim - 2
# Extract values from lists if they are lists (cudnn_convolution receives lists)
def extract_param(param, expected_len):
if isinstance(param, (list, tuple)):
if len(param) == expected_len:
return param if expected_len > 1 else param[0]
elif len(param) == 1:
return param[0]
return param
if ndim == 1:
# For 1D convolution, extract single values from lists
stride_val = extract_param(stride, 1)
padding_val = extract_param(padding, 1)
dilation_val = extract_param(dilation, 1)
return conv1d(
input,
weight,
bias=None,
stride=stride_val,
padding=padding_val,
dilation=dilation_val,
groups=groups,
)
elif ndim == 2:
return conv2d(
input,
weight,
bias=None,
stride=stride,
padding=padding,
dilation=dilation,
groups=groups,
)
elif ndim == 3:
return conv3d(
input,
weight,
bias=None,
stride=stride,
padding=padding,
dilation=dilation,
groups=groups,
)
else:
raise ValueError(
f"cudnn_convolution only supports 1D, 2D, and 3D convolutions, "
f"got input with {ndim} spatial dimensions"
)