Repository navigation
[Fix] topk_per_row_argmax: return early on zero rows - #6471
Conversation
Rows ride a grid dimension, so an empty batch launched a zero-sized grid. HIP rejects that with hipErrorInvalidValue, which the FlyDSL runtime only prints; the next kernel on the stream then raises. topk_select(k=1) routes here, and ATOM's rejection sampler hit it on a P/D producer step that had no drafts to verify. torch.argmax returns an empty result for zero rows. test_topk_select gains test_zero_rows: fp32/bf16/fp16 at widths 2048 (single-pass launch) and 129280 (split launch), each followed by a probe kernel. It fails on every case without this change. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
The previous commit guarded only the k=1 argmax. small_k, radix stream and
decode also put rows on a grid dimension and launch a zero-sized grid on an
empty batch (hipErrorInvalidValue), e.g. the k=10 sorted+values call a
relaxed-acceptance rejection sampler makes. Guard once in _dispatch; the
argmax guard stays for direct callers.
test_zero_rows now covers argmax at fp32/bf16/fp16 x {2048, 129280}, small_k
(k=16), stream (k=512), decode (k=10, sorted + tie=low + return_value) and a
caller passing output_idx, output_idx_offset, end and sorted_index. Each shape
is warmed at one row first, so the empty call takes the cached launch. With
the _dispatch guard removed, the four non-argmax cases fail.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
topk_select._dispatch is the only caller and now returns on zero rows for every backend, so the per-kernel guard is redundant. With only the _dispatch guard all ten test_zero_rows cases pass; with neither guard all ten fail. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Motivation
topk_selecton a zero-row input launches its FlyDSL kernels with a zero-sized grid: every backend puts rows on a grid dimension (observed: argmax(31, 0, 1)/(0, 1, 1), small_k and radix stream(0, 1, 1), decode(0, 4, 1)). HIP rejects it withhipErrorInvalidValue; the FlyDSL runtime only prints the failure, so the next kernel on the stream raisesHIP error: invalid argumentfar from the cause.Where it hits. ATOM's rejection sampler calls
topk_select(target_probs, 1), and with relaxed acceptancetopk_select(probs, 10, sorted=True, tie="low", return_value=True). On a step with no drafts to verify,target_probshas 0 rows and the server dies.torch.argmax, whichtopk_selectreplaced there, returns an empty result for zero rows.Technical Details
aiter/ops/topk_select.py_dispatch: return whenrows == 0. It is the single entry to every backend, so one guard covers all of them. Validation runs before it and post-processing (values gather, sort, offset) after it, both fine on empty tensors;idxstays[0, topk].Test Plan
op_tests/test_topk_select.pygainstest_zero_rows(also run frommain()). Each case is warmed at one row, so the empty call takes the cached launch, then followed by a probe kernel and a sync:n = 2048(single-pass) andn = 129280(split);sorted+tie="low"+return_value);output_idx,output_idx_offset,endandsorted_index.Test Result
MI355X (gfx950), 10 cases:
hipErrorInvalidValue)Submission Checklist