|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | + |
| 4 | +import flag_gems |
| 5 | + |
| 6 | +from .accuracy_utils import gems_assert_close |
| 7 | + |
| 8 | +SHAPE_CUDNN_CONV2D = [ |
| 9 | + ((1, 2, 5, 5), (1, 2, 3, 3), 1), |
| 10 | + ((2, 3, 9, 9), (1, 3, 3, 3), 1), |
| 11 | + ((32, 8, 8, 8), (32, 8, 2, 2), 1), |
| 12 | +] |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.cudnn_convolution |
| 16 | +@pytest.mark.parametrize("shape, kernel, groups", SHAPE_CUDNN_CONV2D) |
| 17 | +@pytest.mark.parametrize("stride", [1, 2]) |
| 18 | +@pytest.mark.parametrize("padding", [0, 1]) |
| 19 | +@pytest.mark.parametrize("dtype", [torch.float16, torch.float32]) |
| 20 | +@pytest.mark.parametrize("dilation", [1, 2]) |
| 21 | +def test_cudnn_convolution_2d( |
| 22 | + shape, kernel, stride, padding, groups, dtype, dilation, monkeypatch |
| 23 | +): |
| 24 | + if flag_gems.vendor_name == "mthreads" and dtype == torch.float16: |
| 25 | + monkeypatch.setenv("MUSA_ENABLE_SQMMA", "1") |
| 26 | + |
| 27 | + inp = torch.randn(shape, dtype=dtype, device=flag_gems.device) |
| 28 | + weight = torch.randn(kernel, dtype=dtype, device=flag_gems.device) |
| 29 | + |
| 30 | + ref_out = torch.cudnn_convolution( |
| 31 | + inp, |
| 32 | + weight, |
| 33 | + padding=[padding, padding], |
| 34 | + stride=[stride, stride], |
| 35 | + dilation=[dilation, dilation], |
| 36 | + groups=groups, |
| 37 | + benchmark=False, |
| 38 | + deterministic=False, |
| 39 | + allow_tf32=False, |
| 40 | + ) |
| 41 | + |
| 42 | + with flag_gems.use_gems(): |
| 43 | + res_out = torch.cudnn_convolution( |
| 44 | + inp, |
| 45 | + weight, |
| 46 | + padding=[padding, padding], |
| 47 | + stride=[stride, stride], |
| 48 | + dilation=[dilation, dilation], |
| 49 | + groups=groups, |
| 50 | + benchmark=False, |
| 51 | + deterministic=False, |
| 52 | + allow_tf32=False, |
| 53 | + ) |
| 54 | + |
| 55 | + gems_assert_close(res_out.cpu(), ref_out.cpu(), dtype) |
| 56 | + |
| 57 | + |
| 58 | +SHAPE_CUDNN_CONV1D = [ |
| 59 | + ((32, 2, 4), (17, 2, 2)), |
| 60 | + ((32, 15, 6), (17, 15, 2)), |
| 61 | + ((64, 64, 64), (128, 64, 7)), |
| 62 | +] |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.cudnn_convolution |
| 66 | +@pytest.mark.parametrize("shape, kernel", SHAPE_CUDNN_CONV1D) |
| 67 | +@pytest.mark.parametrize("stride", [1, 2]) |
| 68 | +@pytest.mark.parametrize("padding", [0, 1]) |
| 69 | +@pytest.mark.parametrize("dtype", [torch.float16, torch.float32]) |
| 70 | +def test_cudnn_convolution_1d(shape, kernel, stride, padding, dtype, monkeypatch): |
| 71 | + if flag_gems.vendor_name == "mthreads" and dtype == torch.float16: |
| 72 | + monkeypatch.setenv("MUSA_ENABLE_SQMMA", "1") |
| 73 | + |
| 74 | + inp = torch.randn(shape, dtype=dtype, device=flag_gems.device) |
| 75 | + weight = torch.randn(kernel, dtype=dtype, device=flag_gems.device) |
| 76 | + |
| 77 | + ref_out = torch.cudnn_convolution( |
| 78 | + inp, |
| 79 | + weight, |
| 80 | + padding=[padding], |
| 81 | + stride=[stride], |
| 82 | + dilation=[1], |
| 83 | + groups=1, |
| 84 | + benchmark=False, |
| 85 | + deterministic=False, |
| 86 | + allow_tf32=False, |
| 87 | + ) |
| 88 | + |
| 89 | + with flag_gems.use_gems(): |
| 90 | + res_out = torch.cudnn_convolution( |
| 91 | + inp, |
| 92 | + weight, |
| 93 | + padding=[padding], |
| 94 | + stride=[stride], |
| 95 | + dilation=[1], |
| 96 | + groups=1, |
| 97 | + benchmark=False, |
| 98 | + deterministic=False, |
| 99 | + allow_tf32=False, |
| 100 | + ) |
| 101 | + |
| 102 | + gems_assert_close(res_out.cpu(), ref_out.cpu(), dtype) |
| 103 | + |
| 104 | + |
| 105 | +SHAPE_CUDNN_CONV3D = [ |
| 106 | + ((1, 2, 5, 5, 5), (1, 2, 3, 3, 3), 1), |
| 107 | + ((2, 3, 9, 9, 9), (1, 3, 3, 3, 3), 1), |
| 108 | +] |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.cudnn_convolution |
| 112 | +@pytest.mark.parametrize("shape, kernel, groups", SHAPE_CUDNN_CONV3D) |
| 113 | +@pytest.mark.parametrize("stride", [1, 2]) |
| 114 | +@pytest.mark.parametrize("padding", [0, 1]) |
| 115 | +@pytest.mark.parametrize("dtype", [torch.float16, torch.float32]) |
| 116 | +@pytest.mark.parametrize("dilation", [1, 2]) |
| 117 | +def test_cudnn_convolution_3d( |
| 118 | + shape, kernel, stride, padding, groups, dtype, dilation, monkeypatch |
| 119 | +): |
| 120 | + if flag_gems.vendor_name == "mthreads" and dtype == torch.float16: |
| 121 | + monkeypatch.setenv("MUSA_ENABLE_SQMMA", "1") |
| 122 | + |
| 123 | + inp = torch.randn(shape, dtype=dtype, device=flag_gems.device) |
| 124 | + weight = torch.randn(kernel, dtype=dtype, device=flag_gems.device) |
| 125 | + |
| 126 | + ref_out = torch.cudnn_convolution( |
| 127 | + inp, |
| 128 | + weight, |
| 129 | + padding=[padding, padding, padding], |
| 130 | + stride=[stride, stride, stride], |
| 131 | + dilation=[dilation, dilation, dilation], |
| 132 | + groups=groups, |
| 133 | + benchmark=False, |
| 134 | + deterministic=False, |
| 135 | + allow_tf32=False, |
| 136 | + ) |
| 137 | + |
| 138 | + with flag_gems.use_gems(): |
| 139 | + res_out = torch.cudnn_convolution( |
| 140 | + inp, |
| 141 | + weight, |
| 142 | + padding=[padding, padding, padding], |
| 143 | + stride=[stride, stride, stride], |
| 144 | + dilation=[dilation, dilation, dilation], |
| 145 | + groups=groups, |
| 146 | + benchmark=False, |
| 147 | + deterministic=False, |
| 148 | + allow_tf32=False, |
| 149 | + ) |
| 150 | + |
| 151 | + gems_assert_close(res_out.cpu(), ref_out.cpu(), dtype) |
0 commit comments