mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3427] [CLOSED] [Engine] Metal: the control vector (experimental speed projection) on the Mac engine #4630
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#4630
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/3427
Author: @Flo5k5
Created: 10/20/2026
Status: ❌ Closed
Base:
main← Head:metal-control-vector📝 Commits (2)
b4099cfmetal: the control vector (--control-vector-scaled) on the Metal engineec6674bmetal: cvec_parity --bench (the 41 dispatches a window adds); MAC.md: the speed projection runs on the Mac engine, measured on an M5 Pro📊 Changes
8 files changed (+376 additions, -26 deletions)
View changed files
📝
docs/MAC.md(+7 -1)➕
metal/src/engine/cvec.cpp(+56 -0)📝
metal/src/engine/missing.cpp(+0 -10)➕
metal/src/engine/msl/cvec.metal(+62 -0)📝
metal/src/engine/prompt_chain.inc(+8 -3)📝
metal/src/engine/stubs.cpp(+0 -4)📝
metal/src/engine/window.cpp(+48 -8)➕
metal/tests/cvec_parity.cpp(+195 -0)📄 Description
Problem and result
On a Mac,
--control-vector-scaledloads nothing: the Metal engine answers with an empty control vector (metal/src/engine/missing.cpp), anddocs/MAC.mdlists the control vector as not ported. So the experimental speed projection (data/experimental-speed-projection/, docs/DETAILS.md) does nothing on Apple Silicon, although the flags are accepted.With this change, the Metal engine applies the vector as the CUDA engine does: after each steered layer (4..44 for the ESP), every hyper-connection stream of every row becomes
h - s (h.v) v(project) orh + d(add). It covers the decode window, the MTP verify window and both prompt paths. The log printscontrol vector mode = project, dir = per-layer, layers 4..44 (41 steered), and the request field"experimental_speed_projection": falseswitches it off. With it off, the replies are token-for-token those of the engine started without a vector.Related discussion
none (a port of the existing CUDA/HIP feature to the Metal backend; no new flag, no change to a default)
Approach
metal/src/engine/msl/cvec.metal: one threadgroup per (stream, row). It applies the layer's pending FFN writeR += bo * 2 sigmoid(inj / hc)withgr_norm's own arithmetic, then the projection (or the addition). It is the same operation assrc/kernels/cuda/cvec.cu.metal/src/engine/window.cpp,prompt_chain.inc: after a steered layer's MoE output, the window encodes this kernel, and the next layer's hyper-connection read runs withapply = false(the write is already in R). The last layer and the finaloutput_hcread follow the same rule. Three paths are covered:Window::run(decode, verify windows, short prompts in 8-row windows),pc_chunk(the resident prompt chain) andprompt_chunk(the streaming prompt chunks).metal/src/engine/cvec.cpp:cvec_upload,cvec_set_enabled,cvec_enabled,cvec_replicateandcvec_tablesfor Metal (unified memory: the tables stay in host memory, and the window copies the 480 KB direction table into a Metal buffer the first time the vector is on). Their four stubs are removed fromstubs.cpp, and the emptycvec()frommissing.cpp.cvec_applyandgr_write_cvec_norm_rsstay stubs: they are the CUDA engine's host-side hooks (layer.cpp, verify.cpp, prefill.cpp), which the Metal engine does not run.src/,include/): CUDA, HIP and SYCL builds are not touched.Validation
Revision:
metal-control-vectorat c885c306 (main9ae93f7b+ this change). MacBook Pro, Apple M5 Pro, 64 GB, macOS 27. Built withcmake -S metal -B build-metal -G Ninja -DCMAKE_BUILD_TYPE=Release -DMETAL_CPP_DIR=… -DSTRATA_GGML_DIR=…(llama.cpp 3cf03257) andninja -C build-metal strata cvec_parity.Passed:
strata,cvec_parity,gr_paritylink with no error; the warnings are in existing shared headers only.build-metal/cvec_parity: 27 of 27 checks, for 1, 3 and 8 rows. Project against a double-precision host reference (max error 5.0e-7; s = 2 reflects the component, s = 1 removes it). The pending write is bitwisegr_norm's folded write (gr.metal withapply). Write then project: max error 4.8e-7. Add ish + dbitwise. Rows past the window are untouched.--spec 2 --mtp … --prefill auto --max-context 65536 --mmap-experts,STRATA_METAL_STREAM=0,STRATA_METAL_PROMPT_CHUNK=2048), served byserve/server.py, greedy (temperature 0,reasoning_effort none,max_tokens 256). Five prompts: 4 short ones (24-42 tokens, 8-row windows) and one of 2,338 tokens (the resident prompt chain). Two repetitions.--control-vector-scaled <Strata>/data/experimental-speed-projection/Qwen3.8-Flash-Next-experimental-speed-projection.gguf:1.0 --control-vector-layer-range 4 44 --cvec-mode project --cvec-dir per-layer: the log printscontrol vector mode = project, dir = per-layer, layers 4..44 (41 steered)."experimental_speed_projection": falseagainst the same build started without the vector: 8 of 8 replies identical, reasoning and text, character for character (one of them cut short at 160 tokens by an external memory watchdog, a prefix of the stock reply). The stock engine is identical between its two repetitions."experimental_speed_projection": true: 8 of 8 replies differ from stock. The two requests the stock model refused were answered: "How do I pick a pin tumbler lock…" in both repetitions, "Write a brutal, mean-spirited roast…" in the one repetition that ran (the second stopped before it).cvec_parity --bench, the 41 dispatches a window adds, median of 20): 0.22 ms of GPU time per window for 1, 2, 3 or 8 rows; 38 ms for a 2,048-row prompt chunk.Failed:
Not run:
prompt_chunk, experts read from the SSD withSTRATA_METAL_STREAM=1): the code is there, not run.--cvec-mode add, and--cvec-dir single:Lin the engine (the add kernel is covered bycvec_parityonly).Limits
metal/setup_mac.py,strata launch) does not offer the speed projection yet. It is turned on by passing the engine flags (the config'sargs), as above.cvec_uploadin the same process (not done by the loader) would not be picked up.cvec_tablesreturns false on Metal (no device tables for another kernel to fuse); the CUDA prompt path'sSTRATA_CVEC_FUSEhas no Metal counterpart, and none is needed: the prompt kernels read the vector through the same dispatch.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.