Skip to content

HIP RDNA2: batched MoE rounds on GemmStridedBatchedEx - #233

Merged
Vasyl-Trefilov merged 11 commits into
RustingStudio:mainfrom
I-AM-ENGINEER:hip-rdna2-moe
Dec 6, 2026
Merged

Vasyl-Trefilov merged 11 commits into
RustingStudio:mainfrom
I-AM-ENGINEER:hip-rdna2-moe

Conversation

@I-AM-ENGINEER

@I-AM-ENGINEER I-AM-ENGINEER commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #232 (attention) and #231 (gfx10), which this builds on.

What

The MoE of a prefill chunk hits hipBLAS through gemm_grouped: the experts of a call dequantize to f16, their x rows gather as f16, and the products run as strided-batched GemmEx (hipblasGemmStridedBatchedEx_v2, fp16 in/out, f32 accumulate). The per-expert shapes of this model are small (512 x 2048), where one GEMM an expert costs more in rocBLAS host time than the products themselves - the batched entry runs a whole round with one call.

  • The slots of a call split into items of the sizes Tensile runs fast on this silicon (a cost table per shape: 512 x 2048 takes 47 us an item from 384 slots to 512 but 123 at 1536, and 314-385 from 896 to 1152; 2048 x 512 takes 21 us at 192/256 but 42 at 448) - a small DP over slot counts picks the cheapest cover.
  • A round dequants and gathers in one launch each, runs one batched GEMM, and scatters the f16 results into y with the activation (blas_scatter; a call's lone expert keeps a single GEMM, act-fused through gemm_splitk_sum).
  • STRATA_MOE_BATCH=0 puts every expert back on its own GEMM; STRATA_MOE_STATS prints the slot counts and the estimated time of the first calls.
  • Same gate as the attention (this is the same Blas handle): RDNA2 only by default, STRATA_HIPBLAS=1/0 overrides, and never for a weight type the backend's int8_gemm takes (an int8 WMMA card keeps those on its int8 GEMM). CUDA and Vulkan get the trait's default, which forwards gemm_grouped to gemm with the same tile count - their behavior is unchanged.

Series numbers

All four RDNA2 PRs together, Qwen4-35B-A3B-Uncensored Q4_K_M on an RX 6800 XT (16 GB), ctx 65536, kv q8, a fresh prefix each run (only the 13-token template prefix is cached, so every prompt is fully prefilled):

prompt upstream + gfx10 series, chunk 8192 series, chunk 2048 series decode (chunk 2048)
4K (4.2k tokens) 494 667 719 (1.5x) 60.6
8K (8.4k tokens) - 785 852 59.5
16K (16.8k tokens) 239 1107 848 (3.5x) 60.1
32K (33.4k tokens) 134 542 554 (4.1x) 58.4

Prefill and decode in tok/s. chunk 8192 ran with --vram-margin 1536 (5210 of 10240 expert slots on the card, 14.6 GB resident), chunk 2048 with --vram-margin 1600 (6000 slots, 14.6 GB); decode at chunk 8192 was 47-52 tok/s. The baseline column is upstream main + #231 at its own defaults (chunk 8192, 15 GB budget, 3.7k / 14.8k / 29.7k-token prompts of the same generator, so the ratios are against slightly shorter prompts); its decode was not measured in that sweep. Per-PR splits were not measured - the four PRs were benchmarked as one.

Chunk size trades experts for scratch: the prefill slab holds chunk rows of every scratch region (276 KB a token), so 8192 costs 2214 MB and 2048 costs 624 MB, and the freed space becomes expert slots. 8192 prefills faster, 2048 decodes ~20% faster (the same prompts on both).

Long prompts stay slower than short ones because the attention of a chunk is dense over the whole prefix: the QSA mask masks the scores, it does not skip the work, so prefill falls roughly as 1/depth. With chunk 8192 and a 15 GB budget the 8192-token chunks of a 64k prompt ran at ~1100 tok/s at an 8k prefix, ~1000 at 16k, ~600 at 24k, ~300 at 32k and ~150 at 64k. A 132k-context setting keeps 4750 slots instead of 5210 and prefills a 33k prompt at ~440 tok/s.

Verified

  • A 700-token multi-topic probe answers all four planted facts on this build; a 5.9k-token dialogue and the 5.7k decoy recall match llama.cpp.
  • The gate check: with STRATA_HIPBLAS=0 the backend logs no hipblas line and runs the dot2 kernels; on gfx1030 the line appears by default.
  • cargo test --release --lib: green (kernel_names counts 475/403/451 with blas_scatter added).

Tooling note

The diagnosis, the patch and the verification runs above were prepared with the help of the deepseek-v4.1-flash-1017 model.

@aletheore

aletheore Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

🔍 Aletheore evidence diff

graph LR
    n0["src/backend.rs"]
    n1["src/cuda/mod.rs"]
    n2["prefill.rs"]
    n3["src/hip/mod.rs"]
    n4["cpu_worker.rs"]
    n5["src/cuda/host_share.rs"]
    n6["chat.rs"]
    n7["mat.rs"]
    n8["expert_cache.rs"]
    n9["src/gpu_model.rs"]
    n10["src/gpu_model/host_share.rs"]
    n11["dspark.rs"]
    n12["forward.rs"]
    n13["device.rs"]
    n14["pool.rs"]
    n15["worker.rs"]
    n16["lib.rs"]
    n17["main.rs"]
    n18["model.rs"]
    n19["projector.rs"]
    n20["src/vulkan/mod.rs"]
    n21["preflight.rs"]
    n22["tests/backend.rs"]
    n23["tests/common/mod.rs"]
    n24["tests/gpu_model.rs"]
    n25["inkling2_attn_kv.rs"]
    n26["inkling2_q8_attention.rs"]
    n27["kernels.rs"]
    n28["kv_q8.rs"]
    n29["kv_turbo.rs"]
    n30["llama_numerics.rs"]
    n31["tq_probe.rs"]
    n32["vision_gpu.rs"]
    n33["vulkan_split_kv.rs"]
    n0 --> n4
    n0 --> n7
    n0 --> n8
    n0 --> n9
    n0 --> n10
    n0 --> n11
    n0 --> n12
    n0 --> n13
    n0 --> n14
    n0 --> n15
    n0 --> n16
    n0 --> n17
    n0 --> n18
    n0 --> n19
    n0 --> n20
    n0 --> n22
    n0 --> n23
    n0 --> n24
    n0 --> n25
    n0 --> n26
    n0 --> n27
    n0 --> n28
    n0 --> n29
    n0 --> n30
    n0 --> n31
    n0 --> n32
    n0 --> n33
    n1 --> n5
    n1 --> n6
    n1 --> n13
    n1 --> n16
    n1 --> n17
    n1 --> n20
    n1 --> n21
    n1 --> n23
    n1 --> n24
    n1 --> n25
    n1 --> n28
    n1 --> n29
    n1 --> n30
    n1 --> n33
    n2 --> n9
    n3 --> n16
    n3 --> n23
    classDef changed fill:#d9772c,color:#fff,stroke:#b85f1c;
    classDef changedFinding fill:#c0392b,color:#fff,stroke:#922b21;
    classDef dependent fill:#f2f0ec,color:#18140f,stroke:#d6d0c5;
    class n0 changed
    class n1 changed
    class n2 changed
    class n3 changed
    class n4 dependent
    class n5 dependent
    class n6 dependent
    class n7 dependent
    class n8 dependent
    class n9 dependent
    class n10 dependent
    class n11 dependent
    class n12 dependent
    class n13 dependent
    class n14 dependent
    class n15 dependent
    class n16 dependent
    class n17 dependent
    class n18 dependent
    class n19 dependent
    class n20 dependent
    class n21 dependent
    class n22 dependent
    class n23 dependent
    class n24 dependent
    class n25 dependent
    class n26 dependent
    class n27 dependent
    class n28 dependent
    class n29 dependent
    class n30 dependent
    class n31 dependent
    class n32 dependent
    class n33 dependent
Loading

What changed
Deterministic, computed from this commit's real scan and import graph - posted on every run, whether or not Flash Review found anything.

  • src/backend.rs · modified · +9/-0 · +2 functions · 27 other dependents
  • src/cuda/kernels.cu · modified · +181/-0
  • src/cuda/mod.rs · modified · +5/-4 · 14 other dependents
  • src/gpu_model/prefill.rs · modified · +8/-4 · 1 other dependent
  • src/hip/ffi.rs · modified · +118/-1 · +7 functions, +1 class
  • src/hip/mod.rs · modified · +285/-1 · +9 functions · 2 other dependents

Aggregate deltas

  • Modules: +0
  • Dependency graph edges: +0
  • Commits: 6

Comment thread src/cuda/kernels.cu Resolved
Comment thread src/hip/mod.rs Resolved
@aletheore

aletheore Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Aletheore Flash Lite review

What this PR does. This pull request adds a grouped GEMM path for mixture-of-experts prefill on HIP, using hipblasGemmStridedBatchedEx_v2 with batched rounds, dequantization and gather/scatter kernels, and a fallback to the existing kernel on error or unsupported shapes. It also adds corresponding CUDA kernels and kernel-name registrations and updates the backend trait and prefill call sites to use the new grouped interface. The changes affect the HIP and CUDA GPU backends and the GPU model prefill path for expert matrix multiplication.

Merge confidence Review effort Merge risk
2 / 5 3 / 5 Medium
Area Files Lines
src/cuda 2 +65 −5
src/hip 2 +224 −3
src 1 +4 −0
src/gpu_model 1 +3 −3

3 finding(s) posted as inline review comment(s). 4 earlier finding(s) from previous pushes still open. 1 earlier finding(s) no longer detected in this push.

# Severity Category Where
1 🟠 High Correctness mod.rs:245 moe_idx mailbox allocation is undersized for 3n u64 words per slot host_mailbox(WORDSSLOTS*2) with…
2 🟠 High Correctness kernels.cu:6245 x_gather_hg does float4 load without checking x_stride alignment Wrapper checks cols%8 and rows%4 but not…
3 🔵 Low Performance mod.rs:340 moe_rounds x_cap/c_cap can allocate hundreds of MB per call x_cap=bpcols*2 with b up to 32 and p up to 1536…
- 🟠 High Correctness mod.rs:591 Earlier finding, still open
- 🟠 High Correctness ffi.rs:104 Earlier finding, still open
- 🟠 High Correctness mod.rs:805 Earlier finding, still open
- 🟡 Medium Correctness mod.rs:195 Earlier finding, still open
Review details

Reviewed 6 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 8 of 8 proposed finding(s).

Fix all with your agent
File: src/hip/mod.rs
Line: 245
Symbol: gemm_grouped_blas
Issue: moe_idx mailbox allocation is undersized for 3*n u64 words per slot host_mailbox(WORDS*SLOTS*2) with WORDS=3*2048 u64 words/slot needs WORDS*SLOTS*8 bytes (or WORDS*SLOTS u64s); at slot 127 writes at host+127*WORDS u64s (~6.2MB) far exceed a 1.57MB byte allocation, corrupting heap or truncating the device buffer read by dequant_hg/x_gather_hg/blas_scatter.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: src/cuda/kernels.cu
Line: 6245
Issue: x_gather_hg does float4 load without checking x_stride alignment Wrapper checks cols%8 and rows%4 but not x_stride%4. If x_stride (e.g. hidden size ne) is not a multiple of 4, row*x_stride+c is not 16-byte aligned for some rows, so *(float4*)(x+row*x_stride+c) faults/misaligns on GPU, crashing or corrupting the gather.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: src/hip/mod.rs
Line: 340
Symbol: moe_rounds
Issue: moe_rounds x_cap/c_cap can allocate hundreds of MB per call x_cap=b*p*cols*2 with b up to 32 and p up to 1536 (rows<cols table) gives ~200MB for 512x2048, cached in blas_x and never shrunk. A call with many large experts can OOM a small RDNA2 card or force a huge allocation on the hot prefill path, where row_cap only limits the C buffer, not X.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Blast radius: 79 other file(s) depend on what this PR changes (30 directly, 49 indirectly)

Computed from the import graph of this exact commit, not guessed by a model. It sees imports only, so dynamic imports, reflection and non-code references are not counted.

  • src/backend.rs is imported by src/cpu_worker.rs, src/deepseek/mat.rs, src/expert_cache.rs, src/gpu_model.rs (+23 more)
  • src/cuda/mod.rs is imported by src/cuda/host_share.rs, src/deepseek/chat.rs, src/inkling2/forward/device.rs, src/lib.rs (+10 more)
  • src/hip/mod.rs is imported by src/lib.rs, tests/common/mod.rs
  • src/gpu_model/prefill.rs is imported by src/gpu_model.rs

Indirectly affected: examples/dotn_bench.rs, examples/ds_cpu_bench.rs, examples/moe_cpu_layer_bench.rs, src/calibrate.rs, src/cpu_moe.rs, src/deepseek/forward.rs (+43 more)

The graph is large; this list is capped and not exhaustive.

@I-AM-ENGINEER

Copy link
Copy Markdown
Contributor Author

Both findings checked on the card:

  1. att_exp returns exp(x)+1 - you are right, and this was the real bug. A probe kernel on gfx1030: v_exp_f32(x * log2e) returns 2^x exactly (x = -3 -> 0.049787 == expf(-3), all sampled points match) - there is no - 1 in the MUFU result, so our + 1.0f turned every weight into exp(x) + 1. Fixed in the base PR (HIP RDNA2: prefill attention on hipBLAS #232, head bdc37c2): att_exp returns the raw result; this branch is rebased on it.
    Why it looked fine: the softmax unit test compared the kernel against a reference that used the same ATT_EXP, so the shift cancelled in the comparison - a tautology, and the flattened weights only cost retrieval margin. With the fix the decoy recall that previously missed a needle answers 3 of 3 needles on 3 consecutive runs, matching llama.cpp, and the multi-topic probe answers all four planted facts.

  2. gemm_grouped overflow guard is a tautology - fixed in this branch: rows.checked_mul(cols) into a let Some(total) = ... else { fall back to gemm }, then total.checked_mul(2) against BLAS_W_BYTES; nothing multiplies on the unchecked path any more.

Both branches are pushed (heads bdc37c2 and this one). The 700-token probe answers 4/4 facts on the updated build.

Comment thread src/cuda/kernels.cu Resolved
Comment thread src/hip/mod.rs Resolved
Comment thread src/hip/mod.rs Resolved
@I-AM-ENGINEER

Copy link
Copy Markdown
Contributor Author

Three more, checked:

  1. att_softmax_wave over the padded L / masked vectors not zeroed - not a bug, and the guards are in the kernel:

    • A full vector (k + 8 <= n) covers keys k..k+7, all < n; the last partial vector and the tail loop (for (k = (nv << 3) + lane; k < n; ...)) both test k + j < n, so keys past the causal n reach neither the max nor the sum. L is the padded pitch, and [n, L) sits inside those partial vectors, which write e = 0 there.
    • The max pass only reads. The exp pass zeroes a fully masked vector before skipping it: *(uint4*)(row + k) = make_uint4(0u, 0u, 0u, 0u); continue; - no raw scores reach the P V GEMM.
    • Independent check: the softmax unit test compares this kernel against a separate reference kernel (its own vis(k) = k < n && mask bit summation) at L = 5680 and L = 16620, masked and unmasked rows, tolerance 1e-4 - 0 mismatches.
  2. SAME_X dropped in the overflow fallback - real, fixed: the fallback now passes act | if same { SAME_X } else { 0 }, same as the later path.

  3. Padded MoE items: gather count vs batched stride - not a bug: x_gather_hg zero-fills the padding (uint2 z = make_uint2(0u, 0u); if ((u64)r < cnt[e]) { ... } *(uint2*)(out + i) = z;), so the padded rows the batched GEMM reads are zeros, not stale data; the same p is the stride everywhere (gather b * p * cols, GEMM sa/sb/sc = p*cols / p*cols / p*rows, scatter b * p * rows), and the scatter skips r >= cnt[e], so padded rows land in the block and never in y.

While checking this I also found the branch was missing the dequant_h/dequant_hg and x_gather_h/x_gather_hg kernels entirely - the dense path logged dequant_h_q6_k is not loaded; the dot2 kernel takes over, i.e. the library paths were falling back and not actually running on this branch. Added (the DEF_DEQUANT_H macro pair and the two gather kernels); the smoke now runs with no fallback lines.

Comment thread src/hip/mod.rs Resolved
Comment thread src/hip/mod.rs Resolved
Comment thread src/hip/mod.rs Resolved
Comment thread src/hip/mod.rs Resolved
@I-AM-ENGINEER

Copy link
Copy Markdown
Contributor Author

Real inconsistency - the guard bounded a single slot by BLAS_W_BYTES while the round buffer asked for up to MOE_BATCH slots. Fixed:

  • moe_rounds derives b_max = MOE_BATCH.min(n).min(BLAS_W_BYTES / (rows * cols * 2)) and caps both the round size (b < b_max in the round closure) and the w_buf allocation with it, so the weight buffer cannot exceed 384 MiB whatever the expert shape.
  • For the measured model (512 x 2048 experts, 2 MB a slot) b_max stays 32 and the numbers are unchanged; for a 100 MB slot it becomes 3 rounds of one slot instead of a 3.2 GB allocation.

Covered by the existing fallback: a slot larger than BLAS_W_BYTES still leaves the library path for the dot2 kernel entirely.

Comment thread src/cuda/mod.rs Resolved
Comment thread src/hip/mod.rs Resolved
@I-AM-ENGINEER

Copy link
Copy Markdown
Contributor Author

All three checked, all three real:

  1. x_gather_h/x_gather_hg never registered - right, the kernels were there but the loader's name list never asked for them (hipModuleGetFunction never ran for those names, so every grouped call would have failed at launch). Added to kernel_names_with; the counts are now 537/465/513 and kernel_names passes.

  2. moe_rounds hard-fails past MOE_ITEMS_MAX - fixed: gemm_grouped now takes the worst-case item count (Σ ceil(cnt / 16) over the experts, 16 being the smallest plan size) and skips the batched path for the per-expert GEMM when it would exceed MOE_ITEMS_MAX; moe_rounds keeps its ensure! as a last guard. A pathological split degrades to more GEMMs, not a failed prefill.

  3. act masked to two bits, flags lost in the fallbacks - fixed: the original act is kept as full_act and passed unchanged to both fallback paths; the library path still uses act & 3 for the activation, the other bits are the dot2 kernel's hints.

Smoke on the updated head: the 700-token probe answers 4 of 4 planted facts and the server log has no is not loaded line.

Comment thread src/hip/mod.rs Resolved
@I-AM-ENGINEER

Copy link
Copy Markdown
Contributor Author

Two notes:

  1. x_gather_h/x_gather_hg registration - already fixed in the head this review ran against (3f51016): kernel_names_with carries them in the chain

    .chain(["kv_to_h", "q_to_h", "att_softmax", "att_scale", "blas_scatter", "x_gather_h", "x_gather_hg", "x_to_h", "blas_scatter_d"].map(String::from))

    cargo test --release --lib -- kernel_names passes (537/465/513), and the runtime smoke on the same build logs no is not loaded line. The comment above may be from an earlier head - please re-check against 3f51016.

  2. SAME_X "re-gathers x for the up GEMM" - not a bug: SAME_X is documented in backend.rs as "x is unchanged since the previous gemm call, which read the same x, so its int8 copy is reused" - it is a hint about the int8 copy of x, not a statement that x holds some gathered scratch with another layout. The f16 path has no int8 copy to reuse; re-reading the same x with the same x_stride/xrow is exactly the correct data.

    For completeness, the up call passes act = 1 | MOE_GATE_X | SAME_X; act & 3 == 1 is the silu-mul activation, and the higher bits are hints for the dot2 kernel (which the fallbacks now receive unchanged).

@Vasyl-Trefilov

Copy link
Copy Markdown
Member

Hi, ty. Same CUDA build break as 232 bc it sits on it. Also gemm_grouped is in impl Gpu, not in impl Backend for Gpu, so prefill never calls it and the MoE BLAS path does not run (rustc warns it is unused). Pls move it into the trait impl, and pls add a fallback to the normal kernel when hipBLAS fails, like attention and dense have.

I-AM-ENGINEER and others added 4 commits December 6, 2026 03:39
RDNA2 has no matrix cores: the flash prefill kernel and the dot2 GEMMs run
at 0.5-12 TFLOPS, and as the context grows the attention of a prefill chunk
dominates its time. This runs the attention of a chunk on the library's
Tensile kernels instead (hipBLAS, fp16 in, f16 scores, f32 out):

- ffi: GemmEx and GemmEx_v2 bindings (the legacy entry takes
  hipblasDatatype_t, the _v2 entry hipDataType; C = x W^T needs OP_N for B).
- hip: attn_prefill_blas - K/V and Q as f16, Q K^T a head into an f16 score
  block, a wave-a-row softmax with the QSA mask, P V a head straight into
  out, then 1/sum and the query gate applied in att_scale. Past 32k keys, or
  on a chunk the kernels do not cover, the flash kernel keeps running
  (attn_prefill_m_at falls through to attn_prefill_m).
- On by default for RDNA2 only (gfx103*); STRATA_HIPBLAS=1 turns the
  library path on on any HIP card, =0 off anywhere.
- kernels.cu: kv_to_h, q_to_h, att_softmax, att_scale, and att_exp for
  RDNA2's v_exp_f32. The CUDA build compiles them and never launches them.

Verified on Qwen4-35B-A3B Q4_K_M, RX 6800 XT (gfx1030): a 5.9k-token
multi-topic dialogue answers all four planted facts; a 5.7k decoy recall
matches llama.cpp. Series numbers (all four RDNA2 PRs): warm prefill
4K 494 -> 1007, 16K 239 -> 1264, 32K 134 -> 1052 tok/s.
…ange

The raw NUL byte made git treat the file as binary, so it was committed with CRLF
and every later diff of it showed the whole file. ASCII again, LF like the rest of
the tree.
I-AM-ENGINEER and others added 5 commits December 6, 2026 04:18
The MoE of a prefill chunk hits hipBLAS through gemm_grouped: the experts of
a call dequantize to f16, their x rows gather as f16, and the products run
as strided-batched GemmEx (fp16 in/out, f32 accumulate). The per-expert
shapes here are small (512x2048), where one GEMM an expert costs more in
rocBLAS host time than the products themselves - a whole round runs with
one call.

- The slots of a call split into items of the sizes Tensile runs fast on
  this silicon (a cost table per shape; the sizes at which the same GEMM
  costs 4-8x more are avoided), the cheapest cover picked by a small DP
  over slot counts.
- A round dequants and gathers in one launch each, runs one batched GEMM,
  and scatters the f16 results into y with the activation (blas_scatter); a
  call's lone expert keeps a single act-fused GEMM through gemm_splitk_sum.
- STRATA_MOE_BATCH=0 puts every expert back on its own GEMM;
  STRATA_MOE_STATS prints the slot counts of the first calls.
- Same gate as the attention (one Blas handle): RDNA2 only by default,
  STRATA_HIPBLAS=1/0 overrides. CUDA and Vulkan take the trait's default,
  which forwards gemm_grouped to gemm with the same tile count.

Verified on Qwen4-35B-A3B Q4_K_M, RX 6800 XT: the 700-token probe answers
all four planted facts (34.7 tok/s decode); the 5.9k dialogue and 5.7k
decoy recall match llama.cpp. Series numbers (all four RDNA2 PRs): warm
prefill 4K 494 -> 1007, 16K 239 -> 1264, 32K 134 -> 1052 tok/s.
@I-AM-ENGINEER

Copy link
Copy Markdown
Contributor Author

Review fixes, all in the head:

  • gemm_grouped is in impl Backend for Gpu now; it sat in impl Gpu, so the prefill never reached the MoE path.
  • a hipBLAS failure before the first expert or round retries on the dot2 kernel, as the attention and dense paths do; a failure after part of y is written sets MOE_PARTIAL, returns the error, and leaves the rest of the run on dot2.
  • u16 tiles instead of __half (same CUDA build as HIP RDNA2: prefill attention on hipBLAS #232), and f2h for their stores: dst[i] = __float2half(x) into a u16 tile converts the value instead of writing the f16 bits, so every dequanted expert was garbage. Wiring the path in was not enough - the first real launch produced nonsense until the stores took f2h.

Smoke (gfx1030, mainline + #232 + this PR): a 6k-token recall probe answers its four planted facts, prefill 473 tok/s.

Comment thread src/hip/mod.rs Resolved
Comment thread src/hip/mod.rs Resolved
Comment thread src/hip/mod.rs Resolved
…l latch per call

host_mailbox counts u32 words, so WORDS * SLOTS u64 slots need twice the words;
the slot writes ran past the pinned allocation. MOE_PARTIAL now starts false on
every gemm_grouped call, so a later failure that wrote no y still falls back to
the dot2 kernel instead of aborting.
@I-AM-ENGINEER

Copy link
Copy Markdown
Contributor Author

Two fixes in the head, one dismissal.

  • moe_idx sized the pinned mailbox in u32 words while it writes u64 slots: host_mailbox(WORDS * SLOTS) is 4 * WORDS * SLOTS bytes for 8 * WORDS * SLOTS bytes of stores, so slots past the first walked off the end of the allocation. Now host_mailbox(WORDS * SLOTS * 2).
  • MOE_PARTIAL is cleared at the start of every gemm_grouped call: the latch only says this call cannot be repeated on dot2 (part of y is written), it must not make every later failure fatal.
  • blas_buf freeing a block a queued kernel still reads: dismissed - the buffer is dropped through Buf -> hipFree, not hipFreeAsync, and hipFree synchronizes the device, so the block cannot be reissued under a launch on that stream. The buffers only grow, and only a bigger chunk or prefix grows them.

Smoke (gfx1030, mainline + #232 + this PR, chunk 2048, 6000/10240 experts on the GPU): a 6k-token recall probe answers its four planted facts; an 8296-token prompt prefills at 740 tok/s (per chunk 483 -> 649 -> 734 -> 785); no kernel fallbacks in the log, so the MoE BLAS path is still the one running.

@I-AM-ENGINEER

Copy link
Copy Markdown
Contributor Author

Branch rebuilt on the new #232 head. One change and one cleanup:

  • gemm_grouped's gate takes self.int8_gemm(ty), the counterpart of the take clause in HIP RDNA2: dense prefill projections on GemmEx #235: where the backend has an int8 GEMM for a type, the experts' tiles stay on gemm_raw instead of going to the batched GemmEx. int8_gemm is false on RDNA2, so nothing changes on the card this was measured on.
  • The comment lines the fix commits had added are gone; no other code change. The two threads above without an answer are the fixes of the previous head: moe_idx sizes the pinned mailbox in u64 slots (host_mailbox(WORDS * SLOTS * 2)), and MOE_PARTIAL is cleared at the start of every gemm_grouped call.

Checked on gfx1030 (HIP backend only) on the top of the stack, 4bf89b52: host_mailbox_is_shared_with_kernels and batched_quantized_gemm_uses_prepared_x pass, the hip:: unit tests pass except the upstream Linux-only drm_free_finds_the_device_by_its_pci_address, and a 6k-token recall probe answers its four facts with no fallback line.

@Aletheore please re-run the review on the current head.

Comment thread src/hip/ffi.rs Resolved
Comment thread src/hip/mod.rs Resolved
Comment thread src/cuda/kernels.cu Resolved
Comment thread src/hip/mod.rs Resolved
Comment thread src/cuda/kernels.cu Resolved
Comment thread src/hip/mod.rs Resolved
@I-AM-ENGINEER

Copy link
Copy Markdown
Contributor Author

Branch rebuilt on the same #232 head with one line changed. Blas::open takes the library path from STRATA_HIPBLAS_LIB again: the first MoE commit had rewritten that line from a snapshot taken before #232's fix, so on this branch STRATA_HIPBLAS was the switch and the path at once, and STRATA_HIPBLAS=1 turned the path off. STRATA_HIPBLAS is the switch only now.

Checked on gfx1030 on the top of the stack (edd130a2): with STRATA_HIPBLAS=1 the log carries the hipblas line and a 6k-token recall probe answers its four facts; with STRATA_HIPBLAS_LIB naming hipblas.dll the line is there too; with STRATA_HIPBLAS=0 it is gone.

@Aletheore please re-run the review on the current head.

@Vasyl-Trefilov
Vasyl-Trefilov merged commit abf6f90 into RustingStudio:main Dec 6, 2026
5 checks passed
@Vasyl-Trefilov

Copy link
Copy Markdown
Member

Hi, ty. The batched expert rounds look good to me. I checked the three new bot comments:

  1. The mailbox size is fine. host_mailbox counts u32 words, so WORDSSLOTS2 is exactly WORDS*SLOTS u64 slots, the bot was wrong.
  2. The x_stride alignment one was real at this head, but your later commit in the dense PR adds the x_stride/y_stride checks to gemm_grouped, so in the stack it is covered.
  3. The x_cap size is bounded by the chunk size, so it is fine for now.
    Small nit: in gemm_grouped "same" and n_x are not used, the let _ line can go.
    I will merge it together with the attention and dense PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants