Skip to content

spacemit: reuse Q4 decode activation operands in TCM - #100

Merged
alex-spacemit merged 1 commit into
spacemit-com:mtmd-backendfrom
co-seven:pr/nomtp-q4-20261203
Dec 4, 2026
Merged

alex-spacemit merged 1 commit into
spacemit-com:mtmd-backendfrom
co-seven:pr/nomtp-q4-20261203

Conversation

@co-seven

@co-seven co-seven commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Problem and behavior

Performance optimization following #91. Single-row Q4 decode reuses the same quantized activation across N32 weight tiles. Stage the compact activation in each worker's TCM and, for eligible shapes, expand its two IME2 nibble operands once across the K dimension instead of repeating that expansion for every output tile.

The existing weight representation, quantization scale/sum bits and Q4 zero-point behavior are retained. This is separate from #91's M=2–4 fused GEMM change.

Change and design

  • ime.cpp: shape/capacity-based dispatch, per-worker compact activation staging, and a disjoint TCM prefix for the optional expanded operand. The expanded path requires M=1, K<=2048, N>=1024 and sufficient TCM; compact staging has its own bounds checks.
  • ime2_kernels.cpp: expand each K32 activation block into the two operands and consume them from the existing fixed-zero-point or affine single-row kernel.
  • ime_kernels.h: internal expansion and expanded-GEMV declarations.

The compact staging remains enabled. Dispatch is based on operator type/shape and available TCM, not model names. No new environment controls are introduced.

Scope is limited to ggml/src/ggml-spacemit/. Impact classification: execution-path change. No changes to the retired CPU backend, model code, public API or test sources. Local validation fixtures are excluded. This independently based module commit preserves the target branch's common MTP graph/rollback paths; experimental noMTP-wide reversions are not submitted. No new external dependency is added.

Validation

Base: mtmd-backend at 3f5ce5fd84f4dc6e7e621642cfef0143d49e7c24. Candidate: 94afec93b4d8cf6f8d3e1de7e9ad490a19d9e94d; source tree b9cf21cc05535cf0b5c7a0ebc4286b6f000f90c5.

The exact submitted production source passed a RISC-V Release cross-build of ggml-spacemit using the SpacemiT v1.1.2 toolchain (GCC 14.3), cmake/riscv64-spacemit-linux-gnu-gcc.cmake, GGML_SPACEMIT=ON, GGML_RV_ZBA=ON, GGML_OPENMP=OFF, Spert 0.6.3 and SpacemiT ORT 2.0.6. Build target: cmake --build <build> --target ggml-spacemit --parallel 24. Backend DSO SHA-256: b65bf4ecf1621704c1a64f646bcc85230a7bcbfcf5720960dbb0e57ab6ae200e. Source hashes were checked before/after the build and against the committed files.

The historical Gemma comparison produced identical 128 output token IDs across all 12 formal requests. Qwen3.5-2B control requests themselves produced different token sequences (0/15 identical pairs among repeated controls), so its timing case does not establish output equivalence.

The final-source static check confirms that the compact and expanded activation regions are disjoint and capacity-bounded. New final-head board numerical checks were not run.

Check Status Evidence / scope
Exact-source RISC-V backend build PASS Configuration and DSO identity above
Diff / scope / formatting checks PASS Backend-only; no test changes or new environment controls; git diff --check, UTF-8/LF and added-line whitespace
Gemma model outputs PASS (historical) All 128 IDs match in 12 requests
Qwen output equivalence NOT ESTABLISHED The repeated baseline itself varies
Model performance PASS (historical measurement) +0.81% / +0.06%; robust gain not established
Final-head board / additional platform coverage NOT RUN Historical target results are carried forward; no new final-head K3 or K1 run

Performance

Case Baseline decode tok/s Candidate decode tok/s Samples per arm Change
Gemma 4 E2B Q4_0, 10K 12.287 ± 0.026 12.387 ± 0.054 6 +0.81%
Qwen3.5-2B Q4_0, 10K 15.127 ± 0.036 15.136 ± 0.027 6 +0.06%

Prefill: Gemma 250.88 → 250.81 tok/s; Qwen 364.93 → 366.28 tok/s. Historical comparison: 71a69aeb → 491189ab, labels gemma-q4 and qwen35-q4. These small changes do not establish a robust whole-model speedup.

Historical K3 A/B protocol: X100 2.2 GHz / A100 1.8 GHz, eight backend workers on cores 8–15; fixed 10,240 prompt token IDs and 128 generated IDs; MTP off, FA on, F16 KV. Server: -np 1 -c 12288 -b 2048 -ub 512 -t 8 --device SPACEMIT0 -ngl 99 -fa on -lm none -ctk f16 -ctv f16 --no-cache-prompt --cache-ram 0 --spec-type none. Nonstreaming /completion: n_predict=128, ignore_eos=true, temperature=0, samplers=["temperature"], seed=123, return_tokens=true, cache_prompt=false.

ABBA order, four fresh server processes, one 16-token warmup and three formal requests per process: six samples across two startups per arm. Only the backend DSO changed. Runtime identity, frequencies and interference monitoring passed the archived audit. Decode throughput is 127 / decode_seconds; values are mean ± sample standard deviation. Delta = 100 * (candidate / baseline - 1). Prefill uses the server's prompt timing.

A separate one-sample-per-arm K3-179 observation on Qwen3.6 with the other noMTP optimizations and GDN in-place writeback measured 9.228 tok/s with compact activation staging versus 9.191 without it. It used 10,240 prompt tokens and -ub 2048. This is a staging-only observation with insufficient replication, not an independent Q4-module benefit or a stable speedup claim.

Risks and limits

Whole-model gains were small; Qwen's baseline nondeterminism prevents an output-equivalence conclusion. The optional expansion uses additional TCM bandwidth/capacity and is gated by shape. Final-head operator/model target revalidation remains pending.

The historical module comparisons used the common noMTP experimental baseline on top of 3f5ce5fd; this PR instead preserves upstream common MTP behavior. Historical results are not claimed as new measurements of the final independent head. Raw build/test/benchmark archives are retained privately and are not publicly attached; the cases, results, artifact identities and limits above are the reviewable record here. Exact-input model replay is not provided by these summaries. Required final-head target evidence remains pending; this submission requests review, not a waiver or a claim of merge readiness. No maintainer exception has been granted. CI status is separate from the local results above.

AI assistance and contributor responsibility

Codex assisted with source preparation, static review, local build/check execution, historical evidence analysis and this PR description. The contributor discussed the module design and explicitly requested publication. No unrecorded personal review, maintainer approval or target execution is asserted. The contributor remains responsible for reviewing the submitted code and claims and addressing review feedback.

Copy link
Copy Markdown
Collaborator

Please fix the RVV clobber list before merging. expand_q8_gemv_a writes vector registers and register groups, but its inline asm declares only t0 and memory. Please declare all registers written and check the modified consumer asm for the same issue.

Please also separate compact staging from expansion in the benchmark. Staging happens before work is divided among workers, so at N=256 with eight workers there is only one N32 tile per worker and no reuse across tiles. An exact-head A/B/C of baseline, compact only, and compact plus expanded should show whether the extra path earns its keep. Please cover Q4_1/Q4_K parity as well as Q4_0, and verify which route ran on each side of the dispatch and scratch thresholds. The current historical +0.81%/+0.06% doesn't establish a repeatable gain.

@alex-spacemit
alex-spacemit merged commit 4fb140b into spacemit-com:mtmd-backend Dec 4, 2026
13 of 20 checks passed
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