Repository navigation
[Engine] Inkling 2 on AMD: faster loop-controller and small products - #331
Open
okada-shogo wants to merge 2 commits into
Open
okada-shogo wants to merge 2 commits into
okada-shogo wants to merge 2 commits into
Conversation
… in one launch on HIP A prompt's loop controller ran its gain and forget products as two hipblasSgemm calls (64 values in, 2,304 out a token) and then ink_gate(): three launches and a round trip of two 2,304-wide buffers, about 290 us of every controller at 1,024 rows on an RX 9070 XT, 96 controllers a call. ink_ctrl_up_gate() does the two products and the gate in one launch, a thread an output with its two weight rows in registers. Each product is one fmaf after the other over the rank from 0, which is what hipBLAS's kernel for this shape does: byte for byte the same on gfx1201 under ROCm 7.2 and 10.2 at every row count tried (32 to 8,192), and poly_inkling2_test's ctrl_up_gate case checks it against gemm_f32() and ink_gate(). CUDA keeps cuBLAS. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gemm_f32() gives a prompt's products of up to 256 outputs to gemm_tile_kernel (or gemm_small_kernel), with tiles sized on NVIDIA cards. On an RX 9070 XT they left the 64 multiprocessors few blocks: Inkling 2 Mini's controllers' down products (4,608 values into 64 outputs, 1,024 tokens) took 243 us, its router (2,304 into 128) 146 us. On HIP the tile now follows the product's size: <32, 32, 2, 2> up to 65,536 outputs, <64, 64, 4, 4> up to 262,144 (or at 64 outputs or fewer), <64, 128, 4, 4> past that; 156 and 120 us for the two. Every tile sums an output one fmaf after the other in the inputs' order, so the bits are the same; poly_gemm_small_test checks Inkling's shapes too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Problem and result
Inkling 2 Mini's prompt on AMD spent about 13% of its GPU time on small fp32 products that left the card mostly idle. Two causes:
hipblasSgemmcalls (64 values in, 2,304 out a token), thenink_gate(). That is about 290 us a controller at 1,024 rows, 96 controllers a call. On HIP,ink_ctrl_up_gate()now does both products and the gate in one launch (75 us).gemm_f32()'s small products (the controllers' down products, 4,608 into 64; the router, 2,304 into 128). Theirgemm_tile_kerneltiles were sized on NVIDIA cards and gave the RX 9070 XT's 64 multiprocessors few blocks. On HIP the tile now follows the product's size: down 204 -> 135 us, router 123 -> 107 us.The output is bit-identical to main.
RX 9070 XT (gfx1201), native Fedora, ROCm 7.2.4. Prompt tok/s, mean of 3 rotated runs, one process a run:
Decode is unchanged: a token's controllers already have their own fused path, and it does not use
gemm_f32(). CUDA and Metal are unchanged: both changes are compiled for HIP only.Each run (tok/s)
Related discussion
None.
Approach
inkling2.cu:ink_ctrl_up_gate_kernel.chrows in shared memory.fmafafter the other over the rank, starting from 0. That is the order hipBLAS's kernel for this shape uses (TensileMT8x8x8). The gate isink_gate_kernel's expression.--loop-bf16) are widened asbf16_to_f32()widens them.inkling2.cpp:control(). It takes the new kernel only wherelin_pair()would have calledhipblasSgemmthroughgemm_f32(): 32 rows or more, F32 (or BF16 thatlin()widens), scale 1, rank 64.kernels.cu:gemm_f32(). On HIP, where it picked agemm_tile_kernel, the tile now depends on the output count (rows x outputs):<32, 32, 2, 2><64, 64, 4, 4><64, 128, 4, 4>Every tile sums an output in the same order, so the bits do not depend on it.
gemm_small_kernel's cases are unchanged.Kernel times (rocprofv3, 2,048-token IQ4_NL prompt, 2 calls of 1,024 rows)
hipblasSgemm+ink_gate_kernel), per controllergemm_tile_kernelgemm_tile_kernelTile sweep: gemm_tile_kernel on the RX 9070 XT (us)
Main's pick against this PR's pick, from a sweep of 11 tile shapes. Every shape gave the same bits as
<16, 64, 2, 4>.Over K 2,048, 2,304 and 4,608, n_out 64, 100, 128 and 256, and 64 to 8,192 rows, the new pick was never slower than main's.
Same bits: hipblasSgemm against an fmaf loop
A probe with K=64, N=2,304,
HIPBLAS_OP_T, HIPBLAS_OP_N, alpha 1, beta 0, random data, gfx1201. It compared every output with a plainfmafloop from 0 in k order, at 32, 33, 64, 100, 128, 255, 256, 300, 512, 777, 1,000, 1,023, 1,024, 1,500, 2,048, 3,000, 4,096 and 8,192 rows:ROCBLAS_USE_HIPBLASLT=1ROCBLAS_USE_HIPBLASLT=0Validation
Passed:
poly_inkling2_test ctrl_up_gate controllerson gfx1201 (ROCm 7.2.4): 72 checks, 0 failed. The newctrl_up_gatecase comparesink_ctrl_up_gate()byte for byte withgemm_f32()x2 +ink_gate():poly_gemm_small_teston gfx1201: all passed. Each output is compared bit for bit with a hostfmaloop, now also at Inkling's shapes (4,608 -> 64 and 2,304 -> 128 at 37, 1,024, 2,048 and 8,192 rows).STRATA_FREEZE_EXPERTS=1, same placement on both sides (2,325 of 3,072 experts for IQ4_NL, 3,058 for UD-Q2_K_XL).--top-probs 20.--kv fp16 --threads 11 --vram-reserve-mib 700, a 302-token warm-up in the same process.Failed:
Not run:
Limits
ink_ctrl_up_gate()with main depends on hipBLAS keeping its order for this shape.ctrl_up_gatechecks it on the card it runs on, so a ROCm that changes it fails the test instead of passing silently.ink_ctrl_up_gate()takes rank 64 only (Inkling 2 Mini'sloop.gate_rank). Any other rank keeps the old path.🤖 Generated with Claude Code