Skip to content

Commit 9b3136b

Browse files
committed
Revert "Fix mixed-dtype matmul: correct output type conversion in kernels"
This reverts commit 635a742.
1 parent 3199f4d commit 9b3136b

8 files changed

Lines changed: 50 additions & 182 deletions

File tree

src/flag_gems/ops/addmm.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,6 @@
1010
from flag_gems.utils import broadcastable_to, libentry, libtuner
1111
from flag_gems.utils import triton_lang_extension as tle
1212

13-
_ordered_datatypes = [torch.float16, torch.bfloat16, torch.float32, torch.float64]
14-
15-
16-
def get_higher_dtype(a, b):
17-
if a is b:
18-
return a
19-
assert a in _ordered_datatypes
20-
assert b in _ordered_datatypes
21-
for d in _ordered_datatypes:
22-
if a is d:
23-
return b
24-
if b is d:
25-
return a
26-
27-
2813
logger = logging.getLogger(__name__)
2914

3015

@@ -91,9 +76,6 @@ def addmm_kernel(
9176
if IS_FP64:
9277
a = a.to(tl.float32)
9378
b = b.to(tl.float32)
94-
if a.dtype != b.dtype:
95-
a = a.to(tl.float32)
96-
b = b.to(tl.float32)
9779
accumulator += tl.dot(a, b, allow_tf32=False)
9880
a_ptrs += BLOCK_SIZE_K * stride_ak
9981
b_ptrs += BLOCK_SIZE_K * stride_bk
@@ -106,7 +88,7 @@ def addmm_kernel(
10688
bias = tl.load(i_ptrs, mask=c_mask, other=0.0)
10789

10890
accumulator = accumulator * alpha + bias * beta
109-
c = accumulator.to(c_ptr.dtype.element_ty)
91+
c = accumulator.to(bias.dtype)
11092
tl.store(c_ptrs, c, mask=c_mask)
11193

11294

@@ -130,8 +112,7 @@ def addmm(bias, mat1, mat2, *, beta=1, alpha=1):
130112
)
131113
mat1 = mat1.contiguous()
132114
# mat2 = mat2.contiguous()
133-
c_dtype = get_higher_dtype(mat1.dtype, mat2.dtype)
134-
out = torch.empty((M, N), device=mat1.device, dtype=c_dtype)
115+
out = torch.empty((M, N), device=mat1.device, dtype=mat1.dtype)
135116
bias = bias.broadcast_to(out.shape)
136117

137118
grid = lambda META: (
@@ -170,8 +151,7 @@ def addmm_out(bias, mat1, mat2, *, beta=1, alpha=1, out=None):
170151
M, K = mat1.shape
171152
_, N = mat2.shape
172153
if out is None:
173-
c_dtype = get_higher_dtype(mat1.dtype, mat2.dtype)
174-
out = torch.empty((M, N), device=mat1.device, dtype=c_dtype)
154+
out = torch.empty((M, N), device=mat1.device, dtype=mat1.dtype)
175155
else:
176156
assert out.shape == (M, N), "Incompatible output shape"
177157
logger.debug(

src/flag_gems/ops/baddbmm.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,6 @@
1212
from .bmm import bmm
1313
from .mul import mul
1414

15-
_ordered_datatypes = [torch.float16, torch.bfloat16, torch.float32, torch.float64]
16-
17-
18-
def get_higher_dtype(a, b):
19-
if a is b:
20-
return a
21-
assert a in _ordered_datatypes
22-
assert b in _ordered_datatypes
23-
for d in _ordered_datatypes:
24-
if a is d:
25-
return b
26-
if b is d:
27-
return a
28-
29-
3015
logger = logging.getLogger(__name__)
3116

3217

@@ -131,9 +116,6 @@ def baddbmm_kernel(
131116
mask_b = mask_k[:, None] & mask_n[None, :]
132117
a = tl.load(a_ptrs, mask=mask_a)
133118
b = tl.load(b_ptrs, mask=mask_b)
134-
if a.dtype != b.dtype:
135-
a = a.to(tl.float32)
136-
b = b.to(tl.float32)
137119
accumulator += tl.dot(a, b, allow_tf32=False)
138120
offs_k += TILE_K
139121
a_ptrs += TILE_K
@@ -152,7 +134,7 @@ def baddbmm_kernel(
152134

153135
bi = tl.load(bias_ptrs, mask=mask_c)
154136
out = accumulator * alpha + bi * beta
155-
o = out.to(o_ptrs.dtype.element_ty)
137+
o = out.to(bi.dtype)
156138
tl.store(o_ptrs, o, mask=mask_c)
157139

158140

@@ -169,8 +151,7 @@ def forward(ctx, bias, A, B, beta, alpha):
169151
_, _, N = B.shape
170152
A = A.contiguous()
171153
B = B.contiguous()
172-
c_dtype = get_higher_dtype(A.dtype, B.dtype)
173-
out = torch.empty((batch, M, N), dtype=c_dtype, device=A.device)
154+
out = torch.empty((batch, M, N), dtype=A.dtype, device=A.device)
174155

175156
bbias = torch.broadcast_to(bias, (batch, M, N)).contiguous()
176157
bias_batch_stride = bbias.stride(0)

src/flag_gems/ops/bmm.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,6 @@
1010
from flag_gems.utils import libentry, libtuner
1111
from flag_gems.utils import triton_lang_extension as tle
1212

13-
_ordered_datatypes = [torch.float16, torch.bfloat16, torch.float32, torch.float64]
14-
15-
16-
def get_higher_dtype(a, b):
17-
if a is b:
18-
return a
19-
assert a in _ordered_datatypes
20-
assert b in _ordered_datatypes
21-
for d in _ordered_datatypes:
22-
if a is d:
23-
return b
24-
if b is d:
25-
return a
26-
27-
2813
logger = logging.getLogger(__name__)
2914

3015

@@ -144,9 +129,6 @@ def bmm_kernel(
144129
a_ptrs += TILE_K * stride_ak
145130
b_ptrs += TILE_K * stride_bk
146131

147-
if a.dtype != b.dtype:
148-
a = a.to(tl.float32)
149-
b = b.to(tl.float32)
150132
o += tl.dot(a, b, allow_tf32=False)
151133

152134
if DIVISIBLE_M and DIVISIBLE_N:
@@ -166,8 +148,7 @@ def bmm(A, B):
166148
assert A.shape[2] == B.shape[1], "K dim mismatch"
167149
batch, M, K = A.shape
168150
_, _, N = B.shape
169-
c_dtype = get_higher_dtype(A.dtype, B.dtype)
170-
out = torch.empty((batch, M, N), dtype=c_dtype, device=A.device)
151+
out = torch.empty((batch, M, N), dtype=A.dtype, device=A.device)
171152

172153
grid_fn = lambda meta: (
173154
triton.cdiv(meta["M"], meta["TILE_M"]),

src/flag_gems/runtime/backend/_nvidia/hopper/ops/mm.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,13 @@ def mm_kernel_general(
175175
for k in range(0, tl.cdiv(K, BLOCK_K)):
176176
a = a_desc.load([offset_am.to(tl.int32), offset_k.to(tl.int32)])
177177
b = b_desc.load([offset_k.to(tl.int32), offset_bn.to(tl.int32)])
178-
if a.dtype != b.dtype:
179-
a = a.to(tl.float32)
180-
b = b.to(tl.float32)
181178
if IS_FP64:
182179
acc += tl.dot(a, b, allow_tf32=False)
183180
else:
184181
acc += tl.dot(a, b, out_dtype=tl.float32, allow_tf32=False)
185182
offset_k += BLOCK_K
186183

187-
acc = acc.to(C.dtype.element_ty)
184+
acc = acc.to(a_desc.dtype)
188185
c_desc.store([offset_am.to(tl.int32), offset_bn.to(tl.int32)], acc)
189186

190187
else:

tests/test_addmm.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
(15, 160, 1024),
1818
(495, 5333, 71),
1919
]
20-
FLOAT_DTYPES = utils.ALL_FLOAT_DTYPES
20+
FLOAT_DTYPES = utils.FLOAT_DTYPES
2121

2222

2323
@pytest.mark.addmm
@@ -28,8 +28,6 @@
2828
def test_addmm(monkeypatch, M, N, K, scalar, dtype, b_column_major):
2929
if flag_gems.vendor_name == "tsingmicro" and dtype == torch.float32:
3030
pytest.skip("Skiping fp32 addmm test on tsingmicro platform")
31-
if dtype == torch.float64 and torch.cuda.get_device_capability()[0] < 9:
32-
pytest.skip("tl.dot does not support fp64 on compute capability < 9.0")
3331

3432
if flag_gems.vendor_name == "mthreads":
3533
monkeypatch.env("MUSA_ENABLE_SQMMA", "1")
@@ -69,8 +67,6 @@ def test_addmm(monkeypatch, M, N, K, scalar, dtype, b_column_major):
6967
def test_addmm_out(M, N, K, scalar, dtype):
7068
if flag_gems.vendor_name == "tsingmicro" and dtype == torch.float32:
7169
pytest.skip("Skiping fp32 addmm_out test on tsingmicro platform")
72-
if dtype == torch.float64 and torch.cuda.get_device_capability()[0] < 9:
73-
pytest.skip("tl.dot does not support fp64 on compute capability < 9.0")
7470

7571
mat1 = torch.randn((M, K), dtype=dtype, device=flag_gems.device)
7672
mat2 = torch.randn((K, N), dtype=dtype, device=flag_gems.device)
@@ -97,27 +93,3 @@ def test_addmm_out(M, N, K, scalar, dtype):
9793
torch.addmm(bias2, mat1, mat2, alpha=alpha, beta=beta, out=out)
9894

9995
utils.gems_assert_close(out, ref_out, dtype, reduce_dim=K)
100-
101-
102-
MIXED_DTYPE_PAIRS = [
103-
(torch.float16, torch.float32),
104-
(torch.float32, torch.float16),
105-
]
106-
107-
108-
@pytest.mark.addmm
109-
@pytest.mark.parametrize("M, N, K", MNK_SHAPES)
110-
@pytest.mark.parametrize("dtype_a, dtype_b", MIXED_DTYPE_PAIRS)
111-
def test_addmm_mixed_dtype(M, N, K, dtype_a, dtype_b):
112-
mat1 = torch.randn((M, K), dtype=dtype_a, device=flag_gems.device)
113-
mat2 = torch.randn((K, N), dtype=dtype_b, device=flag_gems.device)
114-
bias = torch.randn((N,), dtype=dtype_a, device=flag_gems.device)
115-
ref_mat1 = utils.to_reference(mat1, True)
116-
ref_mat2 = utils.to_reference(mat2, True)
117-
ref_bias = utils.to_reference(bias, True)
118-
119-
ref_out = torch.addmm(ref_bias, ref_mat1, ref_mat2)
120-
with flag_gems.use_gems():
121-
res_out = torch.addmm(bias, mat1, mat2)
122-
123-
utils.gems_assert_close(res_out, ref_out, torch.float32, reduce_dim=K)

tests/test_baddbmm.py

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import flag_gems
55

6-
from .accuracy_utils import ALL_FLOAT_DTYPES as ORIG_ALL_FLOAT_DTYPES
6+
from .accuracy_utils import ALL_FLOAT_DTYPES as ORIG_FLOAT_DTYPES
77
from .accuracy_utils import SCALARS, gems_assert_close, to_reference
88
from .conftest import QUICK_MODE
99

@@ -18,7 +18,7 @@
1818
(15, 160, 1024),
1919
(495, 5333, 71),
2020
]
21-
FLOAT_DTYPES = ORIG_ALL_FLOAT_DTYPES
21+
FLOAT_DTYPES = ORIG_FLOAT_DTYPES
2222

2323
GNK_SHAPES = [(16, 512, 2048), (16, 2560, 2048), (64, 2048, 128)]
2424

@@ -37,8 +37,6 @@
3737
@pytest.mark.parametrize("scalar", SCALARS)
3838
@pytest.mark.parametrize("dtype", FLOAT_DTYPES)
3939
def test_baddbmm(monkeypatch, M, N, K, scalar, dtype):
40-
if dtype == torch.float64 and torch.cuda.get_device_capability()[0] < 9:
41-
pytest.skip("tl.dot does not support fp64 on compute capability < 9.0")
4240
if flag_gems.vendor_name == "mthreads" and dtype in [torch.float16, torch.bfloat16]:
4341
monkeypatch.setenv("MUSA_ENABLE_SQMMA", "1")
4442
batch = 4
@@ -62,8 +60,6 @@ def test_baddbmm(monkeypatch, M, N, K, scalar, dtype):
6260
@pytest.mark.parametrize("scalar", SCALARS)
6361
@pytest.mark.parametrize("dtype", FLOAT_DTYPES)
6462
def test_baddbmm_backward(M, N, K, scalar, dtype):
65-
if dtype == torch.float64 and torch.cuda.get_device_capability()[0] < 9:
66-
pytest.skip("tl.dot does not support fp64 on compute capability < 9.0")
6763
batch = 2
6864
mat1 = torch.randn(
6965
(batch, M, K), dtype=dtype, device=flag_gems.device, requires_grad=True
@@ -95,27 +91,3 @@ def test_baddbmm_backward(M, N, K, scalar, dtype):
9591
gems_assert_close(res_in_bias, ref_in_bias, dtype, reduce_dim=K)
9692
gems_assert_close(res_in_grad1, ref_in_grad1, dtype, reduce_dim=N)
9793
gems_assert_close(res_in_grad2, ref_in_grad2, dtype, reduce_dim=M)
98-
99-
100-
MIXED_DTYPE_PAIRS = [
101-
(torch.float16, torch.float32),
102-
(torch.float32, torch.float16),
103-
]
104-
105-
106-
@pytest.mark.baddbmm
107-
@pytest.mark.parametrize("M, N, K", MNK_SHAPES)
108-
@pytest.mark.parametrize("dtype_a, dtype_b", MIXED_DTYPE_PAIRS)
109-
def test_baddbmm_mixed_dtype(M, N, K, dtype_a, dtype_b):
110-
batch = 4
111-
mat1 = torch.randn((batch, M, K), dtype=dtype_a, device=flag_gems.device)
112-
mat2 = torch.randn((batch, K, N), dtype=dtype_b, device=flag_gems.device)
113-
bias = torch.randn((N,), dtype=dtype_a, device=flag_gems.device)
114-
ref_mat1 = to_reference(mat1, True)
115-
ref_mat2 = to_reference(mat2, True)
116-
ref_bias = to_reference(bias, True)
117-
118-
ref_out = torch.baddbmm(ref_bias, ref_mat1, ref_mat2)
119-
res_out = flag_gems.baddbmm(bias, mat1, mat2)
120-
121-
gems_assert_close(res_out, ref_out, torch.float32, reduce_dim=K)

tests/test_bmm.py

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
(15, 160, 1024),
2222
(495, 5333, 71),
2323
]
24-
FLOAT_DTYPES = utils.ALL_FLOAT_DTYPES
24+
FLOAT_DTYPES = utils.FLOAT_DTYPES
2525

2626

2727
@pytest.mark.bmm
@@ -30,8 +30,6 @@
3030
def test_bmm(M, N, K, dtype):
3131
if flag_gems.vendor_name == "tsingmicro" and dtype == torch.float32:
3232
pytest.skip("Skiping fp32 bmm test on tsingmicro platform")
33-
if dtype == torch.float64 and torch.cuda.get_device_capability()[0] < 9:
34-
pytest.skip("tl.dot does not support fp64 on compute capability < 9.0")
3533

3634
if flag_gems.vendor_name == "mthreads":
3735
os.environ["MUSA_ENABLE_SQMMA"] = "1"
@@ -64,8 +62,6 @@ def test_bmm(M, N, K, dtype):
6462
def test_bmm_non_contiguous(M, N, K, dtype):
6563
if flag_gems.vendor_name == "tsingmicro" and dtype == torch.float32:
6664
pytest.skip("Skiping fp32 bmm test on tsingmicro platform")
67-
if dtype == torch.float64 and torch.cuda.get_device_capability()[0] < 9:
68-
pytest.skip("tl.dot does not support fp64 on compute capability < 9.0")
6965

7066
if flag_gems.vendor_name == "kunlunxin":
7167
torch.manual_seed(0)
@@ -98,8 +94,6 @@ def test_bmm_non_contiguous(M, N, K, dtype):
9894
def test_bmm_out(M, N, K, dtype):
9995
if flag_gems.vendor_name == "tsingmicro" and dtype == torch.float32:
10096
pytest.skip("Skiping fp32 bmm test on tsingmicro platform")
101-
if dtype == torch.float64 and torch.cuda.get_device_capability()[0] < 9:
102-
pytest.skip("tl.dot does not support fp64 on compute capability < 9.0")
10397

10498
if flag_gems.vendor_name == "kunlunxin":
10599
torch.manual_seed(0)
@@ -119,30 +113,3 @@ def test_bmm_out(M, N, K, dtype):
119113
torch.bmm(mat1, mat2, out=out)
120114

121115
utils.gems_assert_close(out, ref_out, dtype, reduce_dim=K)
122-
123-
124-
MIXED_DTYPE_PAIRS = [
125-
(torch.float16, torch.float32),
126-
(torch.float32, torch.float16),
127-
]
128-
129-
130-
@pytest.mark.bmm
131-
@pytest.mark.parametrize("M, N, K", MNK_SHAPES)
132-
@pytest.mark.parametrize("dtype_a, dtype_b", MIXED_DTYPE_PAIRS)
133-
def test_bmm_mixed_dtype(M, N, K, dtype_a, dtype_b):
134-
if flag_gems.vendor_name == "tsingmicro" and (
135-
dtype_a == torch.float32 or dtype_b == torch.float32
136-
):
137-
pytest.skip("Skiping fp32 bmm test on tsingmicro platform")
138-
batch = 4
139-
mat1 = torch.randn((batch, M, K), dtype=dtype_a, device=flag_gems.device)
140-
mat2 = torch.randn((batch, K, N), dtype=dtype_b, device=flag_gems.device)
141-
ref_mat1 = utils.to_reference(mat1, True)
142-
ref_mat2 = utils.to_reference(mat2, True)
143-
144-
ref_out = torch.bmm(ref_mat1, ref_mat2)
145-
with flag_gems.use_gems():
146-
res_out = torch.bmm(mat1, mat2)
147-
148-
utils.gems_assert_close(res_out, ref_out, torch.float32, reduce_dim=K)

0 commit comments

Comments
 (0)