Skip to content

Commit cb0ed3d

Browse files
committed
add addcdiv.out, addcmul.out, baddbmm.out, cat.out etc; refactor op name according to native_functions.yaml
1 parent 4f024d6 commit cb0ed3d

15 files changed

Lines changed: 604 additions & 144 deletions

src/flag_gems/__init__.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ def torch_ge(v):
4242
("_grouped_mm", group_mm),
4343
("_is_all_true", _is_all_true),
4444
("_log_softmax", log_softmax),
45+
("_log_softmax.out", log_softmax_out),
4546
("_log_softmax_backward_data", log_softmax_backward),
47+
("_log_softmax_backward_data.out", log_softmax_backward_out),
4648
("_safe_softmax", _safe_softmax),
4749
("_softmax", softmax),
50+
("_softmax.out", softmax_out),
4851
("_softmax_backward_data", softmax_backward),
52+
("_softmax_backward_data.out", softmax_backward_out),
4953
(
5054
"_to_copy",
5155
to_copy,
@@ -64,11 +68,15 @@ def torch_ge(v):
6468
("add.Tensor", add),
6569
("add_.Tensor", add_),
6670
("addcdiv", addcdiv),
71+
("addcdiv.out", addcdiv_out),
6772
("addcmul", addcmul),
73+
("addcmul.out", addcmul_out),
6874
("addmv", addmv),
6975
("addmv.out", addmv_out),
7076
("addmm", addmm),
7177
("addmm.out", addmm_out),
78+
("addmm.dtype", addmm_dtype),
79+
("addmm.dtype_out", addmm_dtype_out),
7280
("addr", addr),
7381
("alias_copy", alias_copy),
7482
("all", all),
@@ -117,6 +125,7 @@ def torch_ge(v):
117125
("bmm", bmm),
118126
("bmm.out", bmm_out),
119127
("cat", cat),
128+
("cat.out", cat_out),
120129
("celu", celu),
121130
("celu_", celu_),
122131
("ceil", ceil),
@@ -378,8 +387,8 @@ def torch_ge(v):
378387
("rms_norm", rms_norm),
379388
("roll", roll),
380389
("round", round),
381-
("round.out", round_out),
382390
("round_", round_),
391+
("round.out", round_out),
383392
("rrelu_with_noise_backward", rrelu_with_noise_backward),
384393
("rsqrt", rsqrt),
385394
("rsqrt_", rsqrt_),
@@ -481,6 +490,16 @@ def torch_ge(v):
481490
func_name = fn.__name__ if hasattr(fn, "__name__") else str(fn)
482491
FULL_CONFIG_BY_FUNC.setdefault(func_name, []).append(_item)
483492

493+
# Friendly names for only_enable(include=[...]) when the registered impl is *.out
494+
for _alias, _target in (
495+
("softmax", "softmax_out"),
496+
("softmax_backward", "softmax_backward_out"),
497+
("log_softmax", "log_softmax_out"),
498+
("log_softmax_backward", "log_softmax_backward_out"),
499+
):
500+
if _target in FULL_CONFIG_BY_FUNC:
501+
FULL_CONFIG_BY_FUNC.setdefault(_alias, []).extend(FULL_CONFIG_BY_FUNC[_target])
502+
484503

485504
def enable(
486505
lib=aten_lib,

src/flag_gems/ops/__init__.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
from flag_gems.ops.absolute import absolute
99
from flag_gems.ops.acos import acos
1010
from flag_gems.ops.add import add, add_
11-
from flag_gems.ops.addcdiv import addcdiv
12-
from flag_gems.ops.addcmul import addcmul
13-
from flag_gems.ops.addmm import addmm, addmm_out
11+
from flag_gems.ops.addcdiv import addcdiv, addcdiv_out
12+
from flag_gems.ops.addcmul import addcmul, addcmul_out
13+
from flag_gems.ops.addmm import addmm, addmm_dtype, addmm_dtype_out, addmm_out
1414
from flag_gems.ops.addmv import addmv, addmv_out
1515
from flag_gems.ops.addr import addr
1616
from flag_gems.ops.alias_copy import alias_copy, alias_copy_out
@@ -39,7 +39,7 @@
3939
scaled_dot_product_attention_forward,
4040
)
4141
from flag_gems.ops.avg_pool2d import avg_pool2d, avg_pool2d_backward
42-
from flag_gems.ops.baddbmm import baddbmm
42+
from flag_gems.ops.baddbmm import baddbmm, baddbmm_out
4343
from flag_gems.ops.batch_norm import batch_norm, batch_norm_backward
4444
from flag_gems.ops.bernoulli_ import bernoulli_
4545
from flag_gems.ops.bitwise_and import (
@@ -60,7 +60,7 @@
6060
)
6161
from flag_gems.ops.bitwise_right_shift import bitwise_right_shift
6262
from flag_gems.ops.bmm import bmm, bmm_out
63-
from flag_gems.ops.cat import cat
63+
from flag_gems.ops.cat import cat, cat_out
6464
from flag_gems.ops.ceil import ceil, ceil_, ceil_out
6565
from flag_gems.ops.celu import celu, celu_
6666
from flag_gems.ops.clamp import (
@@ -167,7 +167,12 @@
167167
from flag_gems.ops.log1p_ import log1p_
168168
from flag_gems.ops.log10 import log10, log10_, log10_out
169169
from flag_gems.ops.log_sigmoid import log_sigmoid
170-
from flag_gems.ops.log_softmax import log_softmax, log_softmax_backward
170+
from flag_gems.ops.log_softmax import (
171+
log_softmax,
172+
log_softmax_backward,
173+
log_softmax_backward_out,
174+
log_softmax_out,
175+
)
171176
from flag_gems.ops.logaddexp import logaddexp, logaddexp_out
172177
from flag_gems.ops.logical_and import logical_and, logical_and_
173178
from flag_gems.ops.logical_not import logical_not
@@ -274,7 +279,12 @@
274279
from flag_gems.ops.slice_backward import slice_backward
275280
from flag_gems.ops.slice_scatter import slice_scatter
276281
from flag_gems.ops.soft_margin_loss import soft_margin_loss, soft_margin_loss_out
277-
from flag_gems.ops.softmax import softmax, softmax_backward
282+
from flag_gems.ops.softmax import (
283+
softmax,
284+
softmax_backward,
285+
softmax_backward_out,
286+
softmax_out,
287+
)
278288
from flag_gems.ops.softplus import softplus
279289
from flag_gems.ops.softshrink import softshrink, softshrink_out
280290
from flag_gems.ops.sort import sort, sort_stable
@@ -344,8 +354,12 @@
344354
"add",
345355
"add_",
346356
"addcdiv",
357+
"addcdiv_out",
347358
"addcmul",
359+
"addcmul_out",
348360
"addmm",
361+
"addmm_dtype",
362+
"addmm_dtype_out",
349363
"addmm_out",
350364
"addmv",
351365
"addmv_out",
@@ -378,6 +392,7 @@
378392
"avg_pool2d",
379393
"avg_pool2d_backward",
380394
"baddbmm",
395+
"baddbmm_out",
381396
"batch_norm",
382397
"batch_norm_backward",
383398
"bernoulli_",
@@ -398,6 +413,7 @@
398413
"bmm",
399414
"bmm_out",
400415
"cat",
416+
"cat_out",
401417
"ceil",
402418
"ceil_",
403419
"ceil_out",
@@ -538,6 +554,8 @@
538554
"log_sigmoid",
539555
"log_softmax",
540556
"log_softmax_backward",
557+
"log_softmax_backward_out",
558+
"log_softmax_out",
541559
"log1p_",
542560
"logaddexp",
543561
"logaddexp_out",
@@ -676,6 +694,8 @@
676694
"soft_margin_loss_out",
677695
"softmax",
678696
"softmax_backward",
697+
"softmax_backward_out",
698+
"softmax_out",
679699
"softplus",
680700
"softshrink",
681701
"softshrink_out",

src/flag_gems/ops/addcdiv.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ def addcdiv_kernel(x, t1, t2, value):
1616
return x + value * (t1 / t2)
1717

1818

19-
def addcdiv(inp, tensor1, tensor2, value=1.0, out=None):
20-
logger.debug("GEMS ADDCDIV FORWARD")
21-
22-
if out is None:
23-
out = torch.empty_like(inp)
24-
19+
def addcdiv_out(inp, tensor1, tensor2, *, value=1.0, out):
20+
logger.debug("GEMS ADDCDIV_OUT")
2521
addcdiv_kernel(inp, tensor1, tensor2, value, out0=out)
26-
2722
return out
23+
24+
25+
def addcdiv(inp, tensor1, tensor2, value=1.0):
26+
"""Functional entry; CUDA may dispatch here without hitting ``addcdiv.out``."""
27+
logger.debug("GEMS ADDCDIV")
28+
out = torch.empty_like(inp)
29+
return addcdiv_kernel(inp, tensor1, tensor2, value, out0=out)

src/flag_gems/ops/addcmul.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@ def addcmul_forward(x, t1, t2, value):
1616
return x + value * t1 * t2
1717

1818

19-
def addcmul(inp, tensor1, tensor2, *, value=1.0, out=None):
20-
logger.debug("GEMS ADDCMUL FORWARD")
21-
if out is not None:
22-
broadcast_shape = torch.broadcast_shapes(
23-
inp.shape, tensor1.shape, tensor2.shape
24-
)
25-
if list(out.shape) != list(broadcast_shape):
26-
out.resize_(broadcast_shape)
27-
addcmul_forward(inp, tensor1, tensor2, value, out0=out)
28-
return out
29-
else:
30-
return addcmul_forward(inp, tensor1, tensor2, value)
19+
def addcmul_out(inp, tensor1, tensor2, *, value=1.0, out):
20+
logger.debug("GEMS ADDCMUL_OUT")
21+
broadcast_shape = torch.broadcast_shapes(inp.shape, tensor1.shape, tensor2.shape)
22+
if list(out.shape) != list(broadcast_shape):
23+
out.resize_(broadcast_shape)
24+
addcmul_forward(inp, tensor1, tensor2, value, out0=out)
25+
return out
26+
27+
28+
def addcmul(inp, tensor1, tensor2, *, value=1.0):
29+
"""Functional entry; keep alongside ``addcmul.out`` for dispatch coverage."""
30+
logger.debug("GEMS ADDCMUL")
31+
broadcast_shape = torch.broadcast_shapes(inp.shape, tensor1.shape, tensor2.shape)
32+
dtype = torch.promote_types(
33+
inp.dtype, torch.promote_types(tensor1.dtype, tensor2.dtype)
34+
)
35+
out = torch.empty(broadcast_shape, device=inp.device, dtype=dtype)
36+
return addcmul_out(inp, tensor1, tensor2, value=value, out=out)

src/flag_gems/ops/addmm.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,39 @@ def addmm_out(bias, mat1, mat2, *, beta=1, alpha=1, out=None):
184184
out.stride(1),
185185
)
186186
return out
187+
188+
189+
def addmm_dtype(bias, mat1, mat2, out_dtype, *, beta=1, alpha=1):
190+
logger.debug("GEMS ADDMM_DTYPE")
191+
out = torch.empty(
192+
(mat1.shape[0], mat2.shape[1]),
193+
device=mat1.device,
194+
dtype=out_dtype,
195+
)
196+
return addmm_dtype_out(bias, mat1, mat2, out_dtype, beta=beta, alpha=alpha, out=out)
197+
198+
199+
def addmm_dtype_out(bias, mat1, mat2, out_dtype, *, beta=1, alpha=1, out):
200+
logger.debug("GEMS ADDMM_DTYPE_OUT")
201+
if mat1.dtype != mat2.dtype:
202+
raise RuntimeError(
203+
f"mat1 and mat2 must have the same dtype, but got {mat1.dtype} and {mat2.dtype}"
204+
)
205+
if out.dtype != out_dtype:
206+
raise RuntimeError(
207+
"out_dtype must be the same as the dtype of the provided out tensor"
208+
)
209+
if not (
210+
out_dtype == mat1.dtype
211+
or (
212+
out_dtype == torch.float32 and mat1.dtype in (torch.float16, torch.bfloat16)
213+
)
214+
):
215+
raise RuntimeError(
216+
"out_dtype must be the same as input dtype or fp32 for fp16/bf16 inputs"
217+
)
218+
if bias.dtype != out_dtype and bias.dtype != mat1.dtype:
219+
raise RuntimeError("self dtype must match either out_dtype or mat1 dtype")
220+
221+
bias_c = bias.to(out_dtype)
222+
return addmm_out(bias_c, mat1, mat2, beta=beta, alpha=alpha, out=out)

src/flag_gems/ops/baddbmm.py

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,38 @@ def baddbmm_kernel(
134134
tl.store(o_ptrs, o, mask=mask_c)
135135

136136

137+
def _baddbmm_launch(bias, A, B, beta, alpha, out):
138+
batch, M, K = A.shape
139+
_, _, N = B.shape
140+
A = A.contiguous()
141+
B = B.contiguous()
142+
bbias = torch.broadcast_to(bias, (batch, M, N)).contiguous()
143+
bias_batch_stride = bbias.stride(0)
144+
bias_M_stride = bbias.stride(1)
145+
bias_N_stride = bbias.stride(-1)
146+
147+
grid = lambda meta: (
148+
triton.cdiv(meta["M"], meta["TILE_M"]),
149+
triton.cdiv(meta["N"], meta["TILE_N"]),
150+
batch,
151+
)
152+
with torch_device_fn.device(A.device):
153+
baddbmm_kernel[grid](
154+
A,
155+
B,
156+
out,
157+
bbias,
158+
alpha,
159+
beta,
160+
M,
161+
N,
162+
K,
163+
bias_batch_stride=bias_batch_stride,
164+
bias_M_stride=bias_M_stride,
165+
bias_N_stride=bias_N_stride,
166+
)
167+
168+
137169
class BaddbmmFunction(torch.autograd.Function):
138170
@staticmethod
139171
def forward(ctx, bias, A, B, beta, alpha):
@@ -145,35 +177,8 @@ def forward(ctx, bias, A, B, beta, alpha):
145177

146178
batch, M, K = A.shape
147179
_, _, N = B.shape
148-
A = A.contiguous()
149-
B = B.contiguous()
150180
out = torch.empty((batch, M, N), dtype=A.dtype, device=A.device)
151-
152-
bbias = torch.broadcast_to(bias, (batch, M, N)).contiguous()
153-
bias_batch_stride = bbias.stride(0)
154-
bias_M_stride = bbias.stride(1)
155-
bias_N_stride = bbias.stride(-1)
156-
157-
grid = lambda meta: (
158-
triton.cdiv(meta["M"], meta["TILE_M"]),
159-
triton.cdiv(meta["N"], meta["TILE_N"]),
160-
batch,
161-
)
162-
with torch_device_fn.device(A.device):
163-
baddbmm_kernel[grid](
164-
A,
165-
B,
166-
out,
167-
bbias,
168-
alpha,
169-
beta,
170-
M,
171-
N,
172-
K,
173-
bias_batch_stride=bias_batch_stride,
174-
bias_M_stride=bias_M_stride,
175-
bias_N_stride=bias_N_stride,
176-
)
181+
_baddbmm_launch(bias, A, B, beta, alpha, out)
177182
return out
178183

179184
@staticmethod
@@ -234,6 +239,24 @@ def compute_B_grad(A, d_output, alpha):
234239
return grad_B
235240

236241

242+
def baddbmm_out(bias, A, B, *, beta=1.0, alpha=1.0, out):
243+
logger.debug("GEMS BADDBMM_OUT")
244+
batch, M, K = A.shape
245+
_, _, N = B.shape
246+
assert (
247+
out.shape == (batch, M, N) and out.dtype == A.dtype
248+
), "Incompatible output shape or dtype for baddbmm.out"
249+
_baddbmm_launch(
250+
bias.contiguous(),
251+
A.contiguous(),
252+
B.contiguous(),
253+
beta,
254+
alpha,
255+
out,
256+
)
257+
return out
258+
259+
237260
def baddbmm(bias, A, B, beta=1.0, alpha=1.0):
238261
return BaddbmmFunction.apply(
239262
bias.contiguous(),

0 commit comments

Comments
 (0)