Repository navigation
spacemit: reuse Q4 decode activation operands in TCM - #100
Conversation
Assisted-by: Codex
|
Please fix the RVV clobber list before merging. 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. |
4fb140b
into
spacemit-com:mtmd-backend
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-backendat3f5ce5fd84f4dc6e7e621642cfef0143d49e7c24. Candidate:94afec93b4d8cf6f8d3e1de7e9ad490a19d9e94d; source treeb9cf21cc05535cf0b5c7a0ebc4286b6f000f90c5.The exact submitted production source passed a RISC-V Release cross-build of
ggml-spacemitusing 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.
git diff --check, UTF-8/LF and added-line whitespacePerformance
Prefill: Gemma 250.88 → 250.81 tok/s; Qwen 364.93 → 366.28 tok/s. Historical comparison:
71a69aeb→491189ab, labelsgemma-q4andqwen35-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.