Skip to content

Commit 427bcf8

Browse files
authored
Split benchmarks for convolution operators (#2670)
1 parent 917238b commit 427bcf8

5 files changed

Lines changed: 239 additions & 222 deletions

File tree

benchmark/test_conv1d.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import pytest
2+
import torch
3+
4+
import flag_gems
5+
from benchmark.performance_utils import GenericBenchmark
6+
7+
8+
class Conv1DBenchmark(GenericBenchmark):
9+
def set_more_shapes(self):
10+
return [
11+
(32, 64, 512, 64, 3, 1, 0, 1),
12+
(64, 48, 1024, 128, 5, 2, 2, 1),
13+
(16, 24, 2048, 96, 7, 1, 3, 2),
14+
(8, 8, 8192, 16, 11, 4, 5, 1),
15+
(4, 4, 16384, 4, 15, 2, 7, 1),
16+
(32, 64, 512, 64, 3, 1, "valid", 1),
17+
(64, 48, 1024, 128, 5, 2, "valid", 1),
18+
(16, 24, 2048, 96, 7, 1, "same", 2),
19+
(8, 8, 8192, 16, 11, 1, "same", 1),
20+
]
21+
22+
23+
def conv1d_input_fn(shape, dtype, device):
24+
(
25+
batch,
26+
input_c,
27+
input_l,
28+
out_c,
29+
kernel,
30+
stride,
31+
padding,
32+
groups,
33+
) = shape
34+
input_shape = (batch, input_c, input_l)
35+
weight_shape = (out_c, input_c // groups, kernel)
36+
input = torch.randn(size=input_shape, device=device, dtype=dtype)
37+
weight = torch.randn(size=weight_shape, device=device, dtype=dtype)
38+
39+
yield {
40+
"input": input,
41+
"weight": weight,
42+
"bias": None,
43+
"groups": groups,
44+
"stride": stride,
45+
"padding": padding,
46+
},
47+
48+
49+
@pytest.mark.conv1d
50+
def test_conv1d():
51+
torch.backends.cudnn.allow_tf32 = False
52+
bench = Conv1DBenchmark(
53+
input_fn=conv1d_input_fn,
54+
op_name="conv1d",
55+
torch_op=torch.nn.functional.conv1d,
56+
dtypes=[
57+
torch.float16,
58+
torch.float32,
59+
], # Exclude bfloat16 due to cuDNN limitations
60+
)
61+
bench.set_gems(flag_gems.conv1d)
62+
bench.run()

benchmark/test_conv2d.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import pytest
2+
import torch
3+
4+
import flag_gems
5+
from benchmark.attri_util import FLOAT_DTYPES
6+
from benchmark.performance_utils import GenericBenchmark
7+
8+
9+
class Conv2DBenchmark(GenericBenchmark):
10+
def set_more_shapes(self):
11+
return [
12+
(32, 64, 128, 128, 32, 3, 3, 1, 2, 1),
13+
(32, 64, 210, 210, 16, 5, 5, 2, 1, 1),
14+
(16, 32, 12, 12, 24, 3, 3, 2, 1, 1),
15+
(16, 32, 24, 24, 24, 3, 3, 2, 2, 2),
16+
(16, 32, 24, 24, 24, 3, 3, 1, 2, 2),
17+
(16, 32, 12, 12, 24, 3, 3, 2, "valid", 1),
18+
(32, 64, 128, 128, 32, 3, 3, 1, "valid", 1),
19+
(16, 32, 24, 24, 24, 3, 3, 1, "same", 2),
20+
(32, 64, 210, 210, 16, 5, 5, 1, "same", 1),
21+
]
22+
23+
24+
def _input_fn(shape, dtype, device):
25+
(
26+
batch,
27+
input_c,
28+
input_h,
29+
input_w,
30+
out_c,
31+
kernel_h,
32+
kernel_w,
33+
stride,
34+
padding,
35+
groups,
36+
) = shape
37+
input_shape = (batch, input_c, input_h, input_w)
38+
weight_shape = (out_c, input_c // groups, kernel_h, kernel_w)
39+
input = torch.randn(size=input_shape, device=device, dtype=dtype)
40+
weight = torch.randn(size=weight_shape, device=device, dtype=dtype)
41+
42+
yield {
43+
"input": input,
44+
"weight": weight,
45+
"bias": None,
46+
"groups": groups,
47+
"stride": stride,
48+
"padding": padding,
49+
},
50+
51+
52+
@pytest.mark.conv2d
53+
def test_conv2d(monkeypatch):
54+
if flag_gems.vendor_name == "hygon":
55+
monkeypatch.setenv("TRITON_HIP_USE_NEW_STREAM_PIPELINE", "0")
56+
57+
torch.backends.cudnn.allow_tf32 = False
58+
bench = Conv2DBenchmark(
59+
input_fn=_input_fn,
60+
op_name="conv2d",
61+
torch_op=torch.nn.functional.conv2d,
62+
dtypes=FLOAT_DTYPES,
63+
)
64+
bench.set_gems(flag_gems.conv2d)
65+
66+
bench.run()

benchmark/test_conv3d.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import pytest
2+
import torch
3+
4+
import flag_gems
5+
from benchmark.attri_util import FLOAT_DTYPES
6+
from benchmark.performance_utils import GenericBenchmark
7+
8+
9+
class Conv3DBenchmark(GenericBenchmark):
10+
def set_more_shapes(self):
11+
return None
12+
13+
14+
@pytest.mark.conv3d
15+
def test_conv3d():
16+
def conv3d_input_fn(shape, dtype, device):
17+
(
18+
batch,
19+
input_c,
20+
input_d,
21+
input_h,
22+
input_w,
23+
out_c,
24+
kernel_d,
25+
kernel_h,
26+
kernel_w,
27+
stride,
28+
padding,
29+
groups,
30+
) = shape
31+
input_shape = (batch, input_c, input_d, input_h, input_w)
32+
weight_shape = (out_c, input_c // groups, kernel_d, kernel_h, kernel_w)
33+
input = torch.randn(size=input_shape, device=device, dtype=dtype)
34+
weight = torch.randn(size=weight_shape, device=device, dtype=dtype)
35+
36+
yield {
37+
"input": input,
38+
"weight": weight,
39+
"bias": None,
40+
"groups": groups,
41+
"stride": stride,
42+
"padding": padding,
43+
},
44+
45+
torch.backends.cudnn.allow_tf32 = False
46+
bench = Conv3DBenchmark(
47+
op_name="conv3d",
48+
input_fn=conv3d_input_fn,
49+
torch_op=torch.nn.functional.conv3d,
50+
dtypes=FLOAT_DTYPES,
51+
)
52+
bench.set_gems(flag_gems.conv3d)
53+
bench.run()

benchmark/test_conv_depthwise2d.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pytest
2+
import torch
3+
4+
import flag_gems
5+
from benchmark.performance_utils import GenericBenchmark
6+
7+
8+
class ConvDepthwise2DBenchmark(GenericBenchmark):
9+
def set_more_shapes(self):
10+
# Additional shapes for COMPREHENSIVE mode
11+
return [
12+
(1, 64, 224, 224, 3, 3, 1, 1, 1),
13+
(1, 128, 112, 112, 5, 5, 2, 2, 1),
14+
]
15+
16+
17+
def _input_fn(shape, dtype, device):
18+
(
19+
batch,
20+
channels,
21+
input_h,
22+
input_w,
23+
kernel_h,
24+
kernel_w,
25+
stride,
26+
padding,
27+
dilation,
28+
) = shape
29+
input_shape = (batch, channels, input_h, input_w)
30+
weight_shape = (channels, 1, kernel_h, kernel_w)
31+
input_tensor = torch.randn(size=input_shape, device=device, dtype=dtype)
32+
weight = torch.randn(size=weight_shape, device=device, dtype=dtype)
33+
34+
# Pass as positional args since the first arg is named 'self' in aten op
35+
yield (
36+
input_tensor,
37+
weight,
38+
[kernel_h, kernel_w],
39+
None, # bias
40+
[stride, stride],
41+
[padding, padding],
42+
[dilation, dilation],
43+
)
44+
45+
46+
@pytest.mark.conv_depthwise2d
47+
def test_conv_depthwise2d():
48+
torch.backends.cudnn.allow_tf32 = False
49+
50+
bench = ConvDepthwise2DBenchmark(
51+
op_name="_conv_depthwise2d",
52+
input_fn=_input_fn,
53+
torch_op=torch.ops.aten._conv_depthwise2d,
54+
gems_op=flag_gems._conv_depthwise2d,
55+
dtypes=[torch.float16, torch.float32],
56+
)
57+
58+
bench.run()

0 commit comments

Comments
 (0)