Skip to content

Commit d898157

Browse files
committed
[Reduce] Move guard for zero-size inside custom_op
1 parent 66d8016 commit d898157

3 files changed

Lines changed: 17 additions & 10 deletions

File tree

quack/rmsnorm.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ def _rmsnorm_fwd(
351351
assert weight.dtype in supported_types, "Weight must be float32, float16 or bfloat16"
352352
if residual is not None:
353353
assert residual.dtype in supported_types, "Residual must be float16, bfloat16, or float32"
354+
if x.numel() == 0:
355+
return
354356

355357
N = x.size(-1)
356358
per_head = (weight is not None and weight.dim() == 2) or (bias is not None and bias.dim() == 2)
@@ -494,8 +496,7 @@ def rmsnorm_fwd(
494496
)
495497
else:
496498
residual_out = None
497-
if x.numel() > 0:
498-
_rmsnorm_fwd(x, weight, out, bias, rstd, None, residual, residual_out, eps, False)
499+
_rmsnorm_fwd(x, weight, out, bias, rstd, None, residual, residual_out, eps, False)
499500
# residual_out is None if residual is None and residual_dtype == input_dtype and dropout_p == 0.0
500501
if residual_out is None:
501502
residual_out = x
@@ -964,6 +965,8 @@ def _rmsnorm_bwd(
964965
if dresidual is not None:
965966
assert dresidual.shape == x.shape
966967
assert dresidual.dtype in supported_types, "Residual must be float16, bfloat16, or float32"
968+
if x.numel() == 0:
969+
return
967970

968971
N = x.size(-1)
969972
if dw_partial is None and db_partial is None:

quack/softmax.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ def _softmax_fwd(x: torch.Tensor, out: torch.Tensor) -> None:
200200
"""
201201
assert x.dim() == 2, "Input must be 2D"
202202
assert x.dtype in [torch.float16, torch.bfloat16, torch.float32], "Unsupported dtype"
203+
if x.numel() == 0:
204+
return
203205
N = x.size(1)
204206
dtype, out_dtype = [torch2cute_dtype_map[t.dtype] for t in [x, out]]
205207
_compile_softmax_fwd(dtype, out_dtype, N)(x, out)
@@ -224,8 +226,7 @@ def _softmax_fwd_fake(x: torch.Tensor, out: torch.Tensor) -> None:
224226

225227
def softmax_fwd(x: torch.Tensor) -> torch.Tensor:
226228
out = torch.empty_like(x)
227-
if x.numel() > 0:
228-
_softmax_fwd(x, out)
229+
_softmax_fwd(x, out)
229230
return out
230231

231232

@@ -401,6 +402,8 @@ def _softmax_backward(dy: torch.Tensor, y: torch.Tensor, dx: torch.Tensor) -> No
401402
assert dy.shape == y.shape, "dy and y must have same shape"
402403
assert dy.dtype in [torch.float16, torch.bfloat16, torch.float32], "Unsupported dtype"
403404
assert y.dtype == dy.dtype, "dy and y must have same dtype"
405+
if dy.numel() == 0:
406+
return
404407
N = dy.size(1)
405408
dtype, y_dtype, dx_dtype = [torch2cute_dtype_map[t.dtype] for t in [dy, y, dx]]
406409
_compile_softmax_backward(dtype, y_dtype, dx_dtype, N)(dy, y, dx)
@@ -419,8 +422,7 @@ def _softmax_backward_fake(dy: torch.Tensor, y: torch.Tensor, dx: torch.Tensor)
419422

420423
def softmax_bwd(dy: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
421424
dx = torch.empty_like(dy)
422-
if dy.numel() > 0:
423-
_softmax_backward(dy, y, dx)
425+
_softmax_backward(dy, y, dx)
424426
return dx
425427

426428

quack/topk.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ def _topk_fwd(
230230
assert x.dim() == 2, "Input must be 2D"
231231
assert x.dtype in [torch.float16, torch.bfloat16, torch.float32], "Unsupported dtype"
232232
assert k > 0 and k <= x.shape[1], "k must be positive and <= N"
233+
if x.numel() == 0:
234+
return
233235

234236
N = x.size(1)
235237
dtype = torch2cute_dtype_map[x.dtype]
@@ -284,8 +286,7 @@ def topk_fwd(x: torch.Tensor, k: int, softmax: bool = False):
284286
M = x.size(0)
285287
values = torch.empty((M, k), dtype=x.dtype, device=x.device)
286288
indices = torch.empty((M, k), dtype=torch.int32, device=x.device)
287-
if x.numel() > 0:
288-
_topk_fwd(x, k, softmax, values, indices)
289+
_topk_fwd(x, k, softmax, values, indices)
289290
return values, indices
290291

291292

@@ -479,6 +480,8 @@ def _topk_bwd(
479480
assert values.dim() == 2, "values must be 2D"
480481
assert indices.dim() == 2, "indices must be 2D"
481482
assert dvalues.dtype in [torch.float16, torch.bfloat16, torch.float32], "Unsupported dtype"
483+
if dvalues.numel() == 0:
484+
return
482485

483486
N = dx.size(1)
484487
dtype = torch2cute_dtype_map[dvalues.dtype]
@@ -548,8 +551,7 @@ def topk_bwd(
548551
"""
549552
M, k = dvalues.shape
550553
dx = torch.zeros((M, N), dtype=dvalues.dtype, device=dvalues.device)
551-
if dvalues.numel() > 0:
552-
_topk_bwd(dvalues, values, indices, k, softmax, dx)
554+
_topk_bwd(dvalues, values, indices, k, softmax, dx)
553555
return dx
554556

555557

0 commit comments

Comments
 (0)