mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3756] [Engine] Metal: the control vector (experimental speed projection) on the Mac engine #4765
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#4765
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/3756
Author: @khaterdev
Created: 10/22/2026
Status: 🔄 Open
Base:
main← Head:metal-control-vector📝 Commits (1)
f1a0905[Engine] Metal: the control vector (experimental speed projection) on the Mac engine📊 Changes
13 files changed (+375 additions, -48 deletions)
View changed files
📝
docs/DETAILS.md(+2 -1)📝
docs/MAC.md(+9 -1)📝
metal/setup_mac.py(+6 -0)➕
metal/src/engine/cvec.cpp(+73 -0)📝
metal/src/engine/missing.cpp(+0 -10)➕
metal/src/engine/msl/cvec.metal(+65 -0)📝
metal/src/engine/prompt_chain.inc(+7 -2)📝
metal/src/engine/stubs.cpp(+0 -3)📝
metal/src/engine/window.cpp(+60 -4)➕
metal/tests/cvec_parity.cpp(+112 -0)📝
setup.py(+37 -23)📝
strata_cli/launch/runner.py(+2 -2)📝
tests/terminal/test_launch.py(+2 -2)📄 Description
Problem and result
On a Mac the experimental speed projection (the control vector in
data/experimental-speed-projection,--control-vector-scaled) did nothing:metal/src/engine/missing.cppreturned an empty vector anddocs/MAC.mdlisted it as not ported, while the CUDA, HIP and SYCL engines apply it. A Mac user who turned it on got the stock model.After this change the Metal engine applies the vector where the PC engines do: after each steered layer's FFN write, in the decode window and in both prompt paths. The per-request switch (
"experimental_speed_projection": falsein a request, the web app's drawer) works as on a PC, and a request with it off decodes exactly what the stock engine decodes../setup.shon a Mac asks the same question as the PC installers (or takes--experimental-speed-projection on|off|GGUF); the app's Mac setup takes the flag;strata launchkeeps it in its Mac argument list.Off by default, as everywhere else. Nothing changes for a Mac that does not load a vector:
cv_covers()is false, so no dispatch is added and every read keeps itsapplyflag.Related discussion
None. Opened directly; if the scope should go through an issue first, say so and I will move the discussion there.
Approach
metal/src/engine/msl/cvec.metal: one kernel per (stream, row),cvec.cu's math: the layer's pending FFN write withgr_norm's arithmetic (h += bo * 2 sigmoid(inj / hc)), thenh -= s (h . v) v(project) orh += v(add). With the request flag 0 or no direction for the layer it applies only the write, so the next read (apply = 0) sees bitwise what the fused read would have produced.window.cpp:pend_write(l)decides whether layerl's first read applies the pending write (not after a steered layer: the kernel did), and the final read likewise.cv_ready()uploads the loader's tables into window buffers once and compiles the kernel;cv_dispatch()is pushed aftermoe_combine_sh(decode window),moe_out(both prompt chains). The dispatch cache is unaffected: a vector is loaded before the first window, and the per-request switch is a flag buffer the kernel reads.cvec.cpp: the Metal side ofcvec.hpp:cvec_uploadkeeps the host tables andCvec,cvec_set_enabledwrites the window's flag between requests (nothing is in flight on Metal then).cvec_apply/cvec_tablesstay stubs: they serve the CUDA graphs and the CUDA prefill, which the Metal engine does not run. The three resolved stubs were removed from the generatedstubs.cppby hand (gen_stubs.pywould drop the same lines).setup.py: the PC installers' choice becameesp_choose()/esp_engine_args(), used unchanged by the PC path and now bymac_control_setup(flag only, the app asks nothing) andmetal/setup_mac.py(asks, default no).strata_cli/launch/runner.py: the Mac filter keeps--experimental-speed-projection(it passesoff, as for the PC).MAC.md(the limit removed, what was measured),DETAILS.md(the Mac setup line).Compatibility: no engine flag, config key or protocol line changes. PC engines are untouched (
metal/and the shared Python only).Validation
Environment: MacBook Pro M1 Max, 64 GB, macOS 26.5.2; Apple clang 21.0.0, metal-cpp_26, llama.cpp 3cf0325; Qwen3.8-Flash-Next GSQ-RCO Q2_0, 256K context,
--spec 2 --mtp, the picture reader on. Revisionf1a0905d,cmake -S metal -B build-metal -G Ninja -DCMAKE_BUILD_TYPE=Release -DMETAL_CPP_DIR=... -DSTRATA_GGML_DIR=...; ninja -C build-metal strata cvec_parity. The stock reference is the released engine component 0.1.47 (76f90e58436d+metal, the parent of this branch).Passed:
./build-metal/cvec_parity->control vector parity, 24 windows (project/add, with/without the write, 1..8 rows): worst rel err 1.18e-07, off = gr_norm's write bitwise: 0 failures PASSreasoning_effort: none, 9 prompts (a short question, code, a 434-token and an 11,295-token prompt, five requests the model refuses), served byserve.serverwith the vector loaded (--control-vector-scaled <esp>.gguf:1.0 --control-vector-layer-range 4 44 --cvec-mode project --cvec-dir per-layer):speed.py: 300-token story decode, median of 4 runs after a warm-up; an 8.5K-token prompt read twice), alternating on/off twice: decode on 31.9 and 32.5 tok/s, off 33.3 and 33.2 tok/s (about 3% lower with the vector on); prompt read on 251-345, off 285-345 tok/s (within the run-to-run spread).python3 -m unittest tools.test_setup_macOK;tests.terminal.test_own_filesOK;tests.terminal.test_launch107 of 108 (the Mac argument-list test updated for the kept flag).Failed:
tests.terminal.test_launch.SetupArgs.test_the_apps_own_installer_argumentsfails before and after this change on this Mac (on011c9e60with nothing changed): it expects--kvin the app's installer arguments, which the Mac filter drops. Not touched here.Not run:
metal/only;setup.py's PC path calls the same code it did, through the new helper, but was not executed on a PC.user_argsitself); the Swift, Coder, Unsloth and Gyro families with a vector (the loader's family checks are the PC ones).Limits
gr_norm's read as the CUDA prefill does (gr_write_cvec_norm_rs), not done here.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.