Repository navigation
perf(marlin): make the small-grid tile override K-aware (#5281) - #5292
Merged
Merged
Conversation
#5216 switched to the narrow {thread_k=128, thread_n=64, threads=128} tile whenever the auto-selected tile left fewer than four output tiles per SM. That test counted output tiles only: it ignored K and the 256-thread tile it replaced. On 128-SM Ada parts N=4096 sits exactly on the boundary, so every large-K down projection took the narrow tile even though the wide tile already had enough split-K work per SM (1.07-1.31x slower at M=4/16/128). Keep #5216's tile-count gate, but on parts with at most 128 SMs require the narrow tile to still split K over at least two threadblocks per output tile. A 16-row wide tile is kept for long K unless the narrow tile still splits K at least four ways. The 170-SM RTX 5090 keeps #5216's measured choice; the narrow tile was faster there for every 16-row shape, including large K. Also forward optional thread_k/thread_n/num_threads through the existing manual branch so tests and tuning can compare both tiles on identical operands; the defaults keep the 18-argument ABI (and the 20-argument scratch ABI). Selection is unchanged numerically: both tiles are bitwise deterministic, agree to one output ulp, and auto is bitwise identical to the forced tile the rule selects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #5281. The #5216 small-grid override in
marlin_mmcounted only outputtiles (
m_tiles * n_tiles * 4 <= sms) and ignored K and the 256-thread widetile it replaced. On 128-SM Ada parts
N=4096sits exactly on that boundary(M=128: 32 tiles x 4 == 128; M<=16 is far below it), so large-K down
projections (K=11008/14336) launched the 128-thread
{128,64,128}tile eventhough the wide tile already had 8 split-K blocks per output tile. Decode and
small-batch calls were 1.07-1.31x slower than before #5216.
The rule now requires the narrow tile to pay for its halved warp count: on
128-SM parts it must still split K over at least two threadblocks per output
tile, and a 16-row wide tile is kept for long K unless the narrow tile still
splits K at least four ways. The 170-SM RTX 5090 keeps #5216's measured
choice (the narrow tile was faster for every 16-row shape there).
What Changed
gptqmodel_ext/marlin/gptq_marlin.cu: factor the small-grid decision intomarlin_prefers_narrow_small_grid(). Keep perf(marlin): use 128x64 tiles when the output grid is small #5216's tile-count gate, keep theperf(marlin): use 128x64 tiles when the output grid is small #5216 choice on parts with more than 128 SMs, and on <=128-SM parts require
a >=2-way narrow K split (plus short K or a >=4-way split for 16-row wide
tiles). No kernel arithmetic changes.
gptqmodel/utils/marlin.py,marlin_torch_{fp16,bf16}.cpp: optionalthread_k,thread_n,num_threadsarguments forward an explicit tilethrough the existing manual branch in
marlin_mm. Defaults keep thehistorical 18-argument ABI (and the 20-argument scratch ABI); validation
rejects a partial tile or
num_threadswithout a pair.tests/kernels/test_marlin_small_grid_tiles.py: selection-contract tests forthe large-K wide cases, the narrow cases that still split K, and a
numerics test that forces both tiles on identical operands.
tests/test_marlin_scratch_interface.py: CPU forwarding test for the newoptional arguments.
docs/marlin-tile-schedule.md: updated selection flow and measured table.Logic Flow Documentation
Before (main @ 2665486):
flowchart TD A["auto tile from the small/large table"] --> B{"m_tiles * n_tiles * 4 <= sms?"} B -- "no" --> C["wide table tile"] B -- "yes" --> D["narrow {128, 64, 128}"]After:
flowchart TD A["auto tile from the small/large table"] --> B{"m_tiles * n_tiles * 4 <= sms?"} B -- "no" --> C["wide table tile"] B -- "yes" --> D{"sms > 128?"} D -- "yes (RTX 5090)" --> E["narrow {128, 64, 128}"] D -- "no (128-SM Ada)" --> F{"narrow splits K over >= 2 blocks per tile?"} F -- "no" --> C F -- "yes" --> G{"thread_m_blocks == 1 and prob_k > 4096 and narrow split < 4?"} G -- "yes" --> C G -- "no" --> Em_tiles = ceil(prob_m_split / (16 * thread_m_blocks)); the wide tile is theone
determine_exec_config()picked before the override. The full-SM sm_80tables and the Ada narrow FP32 AWQ schedule keep their own choice, as before.
Measurements
Method: GPTQ 4-bit g128 MarlinLinear fp16, CUDA-graph replay of 100 calls,
minimum of 7 interleaved rounds per variant in ONE process (the host is shared
with other tenants; variants were alternated so drift hits both).
wideisthe pre-#5216 tile,
narrowis the #5216 tile,fixedis the tile the newrule selects. Timings in microseconds per call.
sm_89 (RTX 4090, 128 SMs):
The #5216 wins stay visible: 4096x1024 (0.15-0.55x), 3584x512 (0.29x),
4096x4096 at M<=64 (0.49-0.91x), 8192x2048 (0.77x). BF16 spot check on the
same part: 14336x4096 M=16 15.22 -> 19.78 us (1.30x, fixed wide), M=128
64.90 -> 77.61 us (1.20x, fixed wide), 4096x1024 M=64 92.00 -> 14.04 us
(0.15x, fixed narrow).
sm_120 (RTX 5090, 170 SMs): the new rule is a no-op there by design; every
measured shape keeps #5216's narrow tile.
wide -> narrow:The last two rows are the pre-existing #5216 behaviour (narrow with no K
split); they are not changed by this PR and are called out as a follow-up.
Numerics
The change is selection-only. Marlin splits K across warps and threadblocks
and reduces the partials in a fixed order, so two different tiles cannot be
bitwise equal: changing
thread_k/thread_nchanges the FP32 reductiontree. Forced-tile evidence on the measured shapes (GPTQ builder, both dtypes):
Both tiles are bitwise deterministic across repeats, both have the same
maximum FP64-oracle error, and
autois bitwise identical to the forced tilethe rule selects (asserted in the new test). The literal "bitwise equality
between the two tiles" requested by the issue is not achievable for this
kernel; the 1-ulp agreement above is the strongest true property.
Tests
Red on pristine main (worktree at 2665486, new test copied in):
PYTHONPATH=/root/work/issue-5281-pristine HF_HUB_OFFLINE=1 \ CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=0 \ python -m pytest tests/kernels/test_marlin_small_grid_tiles.py -q -ra -k large_k_keeps_wide # 8 failed (expected (256,256,64)/(256,128,128), got (128,64,128)), 22 deselectedControl on the same pristine tree:
... -k large_k_keeps_narrow # 5 passed, 25 deselectedGreen after the fix (RTX 4090):
test_marlin_jit.py::test_marlin_cuda_smoke_build_and_forwardfails when runafter other marlin tests ("already loaded in this Python process"); the same
failure reproduces on pristine 2665486, so it is a pre-existing test
isolation issue, not this change.
Lint:
Unverified
The
sms > 128split and the 4096 K cutoff are tuned on sm_89 (128 SMs) vssm_120 (170 SMs) only.
K/N combinations around them were not exhaustively measured.
microseconds move a few percent between runs. All comparisons were
interleaved in one process.
behaviour and is left unchanged; it needs its own retune.