Repository navigation
[Triton/Gluon] Clamp the split-K heuristic so it cannot divide by zero - #5136
Conversation
get_splitk() / _get_splitk() shrink NUM_KSPLIT and BLOCK_SIZE_K by floor-dividing by 4 (2 in some copies) while hunting for EVEN_K. Neither decrement was clamped: NUM_KSPLIT = NUM_KSPLIT // NUM_KSPLIT_STEP # 2 // 4 == 0 BLOCK_SIZE_K = BLOCK_SIZE_K // BLOCK_SIZE_K_STEP # 32 // 4 == 8 A NUM_KSPLIT of 2 or 3 lands on 0, and the next line in the same loop is triton.cdiv(K, NUM_KSPLIT) -- ZeroDivisionError. BLOCK_SIZE_K can fall to 8, under the 16 that compute_splitk_params() clamps to everywhere else. This is reachable from shipped configs, not theoretical: 45 GEMM config entries carry NUM_KSPLIT of 2 or 3 (34 twos, 11 threes) across gfx942 and gfx950. The helper is copy-pasted into seven modules; this clamps the six that the gemm-basic wrapper-layer PR does not already cover: gemm/basic/gemm_afp4wfp4.py gemm/fused/fused_gemm_afp4wfp4_mul_add.py gemm/fused/fused_gemm_a8w8_blockscale_mul_add.py _triton_kernels/gemm/basic/gemm_a16wfp4.py _triton_kernels/gemm/batched/batched_gemm_a16wfp4.py _triton_kernels/gemm/batched/batched_gemm_afp4wfp4.py (_triton_kernels/gemm/basic/gemm_a8wfp4.py is the seventh; it moves to the wrapper layer in satya/triton-config-in-wrapper-gemm-basic and is clamped there.) Seven copies of one heuristic is its own problem -- worth collapsing into gemm_config_utils once these are all in the same layer.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
PR title tags & labels: |
There was a problem hiding this comment.
Pull request overview
This PR hardens the host-side split-K “EVEN_K” heuristic used by several Triton GEMM wrappers/kernels by clamping heuristic decrements so they cannot produce invalid parameters (notably NUM_KSPLIT == 0, which would trigger a divide-by-zero on the next triton.cdiv(K, NUM_KSPLIT)).
Changes:
- Clamp
NUM_KSPLITfloor-division decrements to a minimum of1to prevent divide-by-zero during heuristic iteration. - Clamp
BLOCK_SIZE_Kfloor-division decrements to a minimum of16to align with the minimum enforced bycompute_splitk_params()elsewhere. - Add inline comments explaining why these clamps are required.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| aiter/ops/triton/gemm/fused/fused_gemm_afp4wfp4_mul_add.py | Clamp split-K heuristic decrements to prevent NUM_KSPLIT going to 0 and BLOCK_SIZE_K falling below 16. |
| aiter/ops/triton/gemm/fused/fused_gemm_a8w8_blockscale_mul_add.py | Same split-K clamp hardening in fused A8W8 blockscale mul-add wrapper. |
| aiter/ops/triton/gemm/basic/gemm_afp4wfp4.py | Same split-K clamp hardening in basic AFP4WFP4 wrapper. |
| aiter/ops/triton/_triton_kernels/gemm/batched/batched_gemm_afp4wfp4.py | Same split-K clamp hardening in batched AFP4WFP4 kernel-side helper. |
| aiter/ops/triton/_triton_kernels/gemm/batched/batched_gemm_a16wfp4.py | Same split-K clamp hardening in batched A16WFP4 kernel-side helper. |
| aiter/ops/triton/_triton_kernels/gemm/basic/gemm_a16wfp4.py | Same split-K clamp hardening in basic A16WFP4 kernel-side helper. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
brunomazzottiamd
left a comment
There was a problem hiding this comment.
LGTM, we can merge it! However, I have a request for the scope of another PR...
get_splitk function is duplicated across all six GEMM and fused-GEMM files!
The implementation is almost identical, with two minor differences:
gemm_afp4wfp4.pyrecomputesNUM_KSPLITbefore returning.batched_gemm_afp4wfp4.pycontains leftover debug comments.
Notably, the copies in _triton_kernels/gemm/basic/gemm_a16wfp4.py and _triton_kernels/gemm/batched/batched_gemm_a16wfp4.py appear unused. Their wrappers already import get_splitk from another module.
Code duplication makes fixes to the split-K heuristic error-prone and has already allowed the implementations to drift slightly. Please extract the shared split-K calculation into a common utility module, update all callers to import it, and remove the local copies.
No description provided.