Repository navigation
spacemit: retain compact Q6_K weights with aligned IME2 tiles - #101
Merged
alex-spacemit merged 1 commit intoDec 4, 2026
Merged
Conversation
Keep the original six-bit codes and signed subgroup scales for the K2048/N248320 output head instead of expanding weights to Q8. Add a lossless repack and inverse with 128-byte-aligned payloads, matched physical allocation, and the paired TCM staging/IME2 decode path. Preserve compact storage for complete zero-offset aliases and reject unsupported views before they can enter the legacy weight path. Assisted-by: Codex
alex-spacemit
merged commit Dec 4, 2026
fc01796
into
spacemit-com:mtmd-backend
11 of 20 checks passed
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.
Overview
The IME2 Q6_K output-head path currently expands weights to Q8 at load time. For the contiguous K2048/N248320 head used by Qwen3.6-35B-A3B, this increases the weight buffer and the bytes copied for each decode step. This change retains the original six-bit codes and scales in a compact, 128-byte-aligned layout and consumes it with a matching IME2 kernel.
The final head allocation decreases from 540,344,320 to 421,150,720 bytes (-22.06%). Earlier same-board experiments measured decode improvements of 8.526776 -> 8.934479 token/s for compact repacking on K3-235, followed by 9.683554 -> 9.817370 token/s for alignment on K3-175. These are two separate comparisons, not a cross-board speedup. The measurements belong to the experimental packages described below; this standalone commit has not been rerun end-to-end on a board.
Related context: #98 covers existing dense Q8_0/Q6_K tail correctness. The fixed-input operator/model comparisons below document the performance problem addressed here.
Scope and design
One commit,
4d60c576e230afc7a1f5b79f51c05a073a4840c2, directly abovemtmd-backendat75b1485aaad93fbb44d1b5f517a79a7bf0457dd3: seven files, 375 added lines.repack.cpp,repack.hime2_kernels.cpp,ime_kernels.hime.cppime.h,ggml-spacemit.cpp[2048, 248320, 1, 1]. Other shapes retain their existing path. This is an execution-path optimization affecting weight upload/readback, allocation, matmul dispatch and the model output head.ggml/src/ggml-spacemit/. No changes to the retired CPU backend, public APIs, build dependencies, other optimization modules, test sources or experiment controls are included. Temporary validation fixtures remain outside the submitted diff.Validation
Base:
75b1485aaad93fbb44d1b5f517a79a7bf0457dd3. Candidate:4d60c576e230afc7a1f5b79f51c05a073a4840c2.ggml-spacemittarget built successfully.git diff --check, added-line EditorConfig checks and the code-style workflow's model-naming script (177 mappings).editorconfig-checkeris unavailable locally; the added-line check is not claimed as full-workflow coverage.The layout fixture extracts the exact committed repack functions and layout constants and uses real
block_q6_K/GGML types. An independent raw-Q6 decoder checks the represented codes/scales. Cases cover row counts 1/7/8/24/31/32, K256 block counts 1/2/8, random/extreme/zero/subnormal/NaN metadata, all 64 six-bit codes, all 256 signed-scale bit patterns and all 65,536 FP16 scale bit patterns. Guard bytes, unchanged input, zero padding, aligned payloads and rejected short uploads are checked. These helper-boundary cases do not expand production dispatch beyond the stated shape. The extracted repack code and fixture are sanitizer-instrumented; the linked hostggml-baseis not.The view fixture covers root allocation before/after
bufferassignment, full and nested aliases, noncompact parents, neighboring shapes, and rejection of shape/offset/stride/type/transpose changes. It checks the initialization-order defect found during extraction, where compact storage could otherwise acquire the legacy Q8 trait. A full on-board view matmul regression comparison remains unverified.Build environment: SpacemiT GNU toolchain package 1.1.2, spine-runtime 0.6.3, ORT package 2.0.6, RISC-V Release,
GGML_SPACEMIT=ON,GGML_RV_ZBA=ON, OpenMP off. The executed configuration can be reproduced with the SDK paths set locally:For the IME1-only build, the same configuration additionally disables
SPACEMIT_RISCV_COMPILER_SUPPORT_VMADOT_S4,SPACEMIT_RISCV_COMPILER_SUPPORT_VPACKandSPACEMIT_RISCV_COMPILER_SUPPORT_VNPACK.Performance
Historical experimental-package measurements, collected before standalone extraction. Model:
Qwen3.6-35B-A3B-Q4_0.gguf, 20,836,243,072 bytes, with the Q6_K output head. All model runs use the same 10,240 prompt token IDs, 128 generated tokens, no prompt cache, no MTP/speculation, temperature 0, seed 123 andignore_eos=true. Native timing has tracing disabled and excludes model loading. CPU threads/batch threads are 8/8; the backend uses eight A100 workers pinned to CPUs 8-15.Common server options:
--device SPACEMIT0 -ngl 99 -np 1 -c 32768 -b 2048 -ub 2048 -t 8 -tb 8 -fa on -lm none -ctk f16 -ctv f16 --no-cache-prompt --cache-ram 0 --reasoning off --warmup --spec-type none. Each formal model sample is from a separate server process with the same request. K3-235 ran compact then baseline; K3-175 ran compact then aligned. Server-reported prompt/decode token rates are used, not wall-clock loading or trace-derived rates. The response contains 128 generated IDs; decode throughput is127000 / predicted_msbecause the first token comes from prefill. Prefill throughput is10240000 / prompt_ms.Full-model results
One native sample per version; these are individual observations, not repeated-run means. Throughput change is
(after / before - 1) * 100; higher is better.All 128 generated token IDs match across these four runs. Formal timing windows have no detected competing workload: 20 clean observations per K3-235 run and 19 per K3-175 run. All servers exit 0 and release all eight TCM allocations. The measured aligned K3-175 result remains below 10 token/s; the single-sample model comparisons do not establish a statistically stable gain.
Output-head operator results
M1/K2048/N248320 Q6_K head. Each launch uses one untimed warmup and an adaptive performance loop; every recorded sample reports 99 timed runs. Reported values are arithmetic means of the listed per-launch native samples. Latency change is
(after / before - 1) * 100; lower is better. These percentages are operator improvements, not model-throughput improvements.Raw per-run operator timings, in microseconds:
The two K3-175 aligned samples differ by 480.20 us, so the -3.71% result is an observed mean improvement with material variation. A separate same-board direct-weight-read experiment regressed head latency by 15.60%; retaining compact-weight TCM staging is intentional.
The operator command used a local adaptation of
test-backend-opsthat adds this full-head case; it is not an unmodified upstream test executable:Weight allocation
Evidence identities and applicability
c6bf421ca21e3107386861b31ce70b604a281b1c52343a49cbf42791ef0f349e801812eedc52dc5c5f485d7cbeb709bbc8b8ad47988e456b7c1093ac1b0200ab2dfa42f730d457958b820807e2a83915851cbf5df73d1f620079fe21c71f95291f1c848d004248c485de3aa05f734eaf8561fde4162a843321114294d3dfd50eRecorded run labels are
baselinecompact2,q6compactmodel1,compact175r2,q6alignedmodel175r1,q6compactnative1andq6alignednative175r1. Full-model comparisons come fromsame-round-model-results.jsonandmodel175-results.json; operator results come fromoperator-results.jsonandnative175-results.json. The final 95-instruction leaf matches the historical aligned package, but standalone integration and the new view guard were not included in that package's model run.The identical formal request files have SHA256
08331861b9ff37c44c497576a45f96def7c143cfc59d871d7ef512d935a6c615; the recorded prompt-file SHA256 isae8948cf4a76edf42c09ba63ae321ba8b396bfef6b5e06c24d620f2d292f6630. The operator fixture executable is7ed4a53b8b55e2e68c6f0d4457284672a572c3f86fb64d1c235cc71d3085fc09. Model identity was recorded by path, size and modification time; a model-content SHA256 was not recorded.Risks and limits
_Float16declaration; the corrected extraction excludes that declaration while preserving Q6 code verbatim. Neither attempt is counted as a passing result.AI assistance and responsibility
Codex assisted with implementation, experiments, local validation, code review, evidence checking and this PR description. The contributor reviewed the file-by-file changes and compact data layout before explicitly requesting publication. Contributor
co-sevenowns the submitted code and claims, including the stated validation limits.