Optimize topk operation in llama4 (#5128)

This commit is contained in:
fzyzcjy
2025-04-09 17:50:22 +08:00
committed by GitHub
parent 92823069c4
commit 86a876d883
4 changed files with 18 additions and 15 deletions

View File

@@ -1819,3 +1819,12 @@ class DeepEPMode(Enum):
return DeepEPMode.low_latency
else:
return DeepEPMode.normal
def fast_topk(values, topk, dim):
if topk == 1:
# Use max along the specified dimension to get both value and index
return torch.max(values, dim=dim, keepdim=True)
else:
# Use topk for efficiency with larger k values
return torch.topk(values, topk, dim=dim)