@@ -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