mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #4019] [Engine] Mac: calibrate the decode kernels' tiles on the device (setup.sh --calibrate) #4878
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#4878
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/4019
Author: @rasa04
Created: 10/24/2026
Status: 🔄 Open
Base:
main← Head:metal-calibrate-tiles📝 Commits (1)
6a68544[Engine] Mac: calibrate the decode kernels' tiles on the device (setup.sh --calibrate)📊 Changes
8 files changed (+533 additions, -8 deletions)
View changed files
📝
.gitignore(+1 -0)📝
docs/MAC.md(+9 -1)📝
metal/setup_mac.py(+40 -1)📝
metal/src/engine/window.cpp(+359 -4)📝
strata_control/calibration.py(+2 -0)📝
tests/terminal/test_calibration.py(+8 -0)📝
tools/calibrate.py(+59 -1)📝
tools/test_calibrate.py(+55 -1)📄 Description
Related discussion: #4025.
Problem. The Metal decode kernels use tiles tuned on the M4 and M5. On an M1 Max (24-core GPU) the fp32 dense GEMVs (
STRATA_METAL_F16unset) of a Qwen3.8-Flash-Next Q2_0 decode window take 33.1 ms on the default tiles and 20.7 ms on each shape's best one (gemv_parity --one … --ntok 2).setup.sh --calibrateis not ported to the Mac.Change (opt-in; with neither variable set the dispatches are unchanged):
STRATA_METAL_CALIBRATE=<file>: the engine times every instantiated tile of the dense GEMVs,gr_downand the routed experts on the device, outputs into a scratch buffer, and keeps a tile only if it is ≥ 3% faster.STRATA_METAL_TILES=<file>: use the tiles, never measure. A shape the file lacks, a file from another GPU, or an expert tile that does not divide the model's rows keeps the default.setup.sh --calibrateon a Mac (tools/calibrate.py, like the HIPSTRATA_SH_STREAMstep): measures, races the tiles against the defaults over 3 interleaved rounds, and writesSTRATA_METAL_TILESinto the config only aboveMIN_GAIN. A round whose engine fails to start or dies is dropped, as in the other steps (measure_candidate,41188ec7). One file per model.STRATA_METAL_F16=1, the default of a resident Q2_0 / IQ3_S pack): the fp16 GEMV and expert kernels keep their tiles - the calibration neither measures them nor gives them a tile measured on the fp32 kernels. The fp32 dispatches of its windows (the formats without an fp16 kernel,gr_down, the experts off the fp16 path) are measured and tiled.cbef1a9f) are among the calibrated down formats (DN_SET(T_Q5_0)has every candidate tile).STRATA_METAL_HCLD=1(off by default)gr_downkeeps itsgr_downp_*_8_8kernels: no tile from the file, none measured.setup.sh --calibratedoes not start beside a running engine;strata engine calibraterefuses a Mac config (its tiles would replace the file the config names).Result (M1 Max, 64 GB, Q2_0 resident, MTP, before the fp16 decode existed;
tools/ab_engine.py, 3 rounds, medians, tok/s):Quality (
tools/quant_quality.py, 4995 positions): same top token 98.4%, mean KL 0.003, perplexity 4.439 → 4.431. Near-ties can flip, since a tile changes the order of a sum.With the fp16 decode on (
STRATA_METAL_F16=1;setup.sh --calibrate's race, 3 interleaved rounds, medians of 6 rates, tok/s): run 1 (7ed042b4onffa80d73) default 40.6, tiles 43.35 (+6.8%, kept); run 2 (3c7874fbonffa80d73) default 40.6, tiles 41.7 (+2.7%, underMIN_GAIN: the defaults stayed); run 3 (7580f99don 0.2.12, its config with--spec 3 --spec-min-p 0.5and a 128K context) default 35.85, tiles 36.85 (+2.8%, underMIN_GAIN). Quality of run 1's tiles (quant_quality, 4995 positions) against the fp16 default: same top token 97.7%, mean KL 0.008, perplexity 4.415 → 4.433; against fp32, mean KL 0.0079 with the tiles and 0.0091 without.Validation (
6a685443on 0.2.12146df3c4, M1 Max, macOS 27):146df3c4withSTRATA_METAL_F16unset and set to 1 (quant_quality, 4995/4995 rows each);expert_parity --dn Q5_036/36 (every down tile × ntok 1, 2, 4; on9f7d977d);python -m unittest tools/test_calibrate.py tools/test_setup_mac.py tests/terminal/test_calibration.py(108 of 109, incl. the newtest_metal_tiles,test_engine_death_during_a_metal_round_keeps_the_runandtest_a_mac_config_is_refused);setup.sh --calibrate's measure and race (calibrate.runon the Q2_0 config,STRATA_METAL_F16=1) twice onffa80d73and once on 0.2.12.test_failed_calibration_says_the_engines_reason, which fails on a Mac on146df3c4without this change too (metal/setup_mac.pyis missing from the test's temp tree).146df3c4:gemv_parity,gr_parity(6 variants),expert_parityfor the other formats (no kernel changed; 507/507 and all-pass on 0.2.75e8a4cca). Thegr_downlimit check from a tiles file: built, not run (no Mac here has a variant over the limit).--parallel, the IQ3 / Unsloth / Gyro packs, CUDA / HIP / SYCL (the engines not touched).Limits. The gain is confirmed only on the Mac and
--specthe calibration ran on. With the fp16 decode on, the decode gain comes from the fp32 dispatches of a decode window (the formats without an fp16 kernel) andgr_down; the prompt's windows are tiled too but do not count in decode tok/s. Window sizes it did not see and the MTP layer keep the defaults; with SSD streaming the experts do too. Re-run after an update that changes the kernels.Individual runs (tok/s)
setup.sh --calibrate: default 25.9 / 26.1 / 27.8, tiles 35.0 / 35.1 / 37.3.setup.sh --calibratewithSTRATA_METAL_F16=1(the report's rates): run 1 default 40.8 / 40.7 / 40.5 / 40.6 / 40.5 / 40.6, tiles 43.5 / 43.4 / 43.2 / 43.1 / 43.3 / 43.4; run 2 default 40.6 / 40.7 / 40.6 / 40.5 / 40.7 / 32.0, tiles 41.8 / 41.8 / 41.7 / 41.7 / 41.7 / 41.7; run 3 default 35.6 / 35.4 / 36.0 / 35.9 / 36.0 / 35.8, tiles 37.0 / 36.7 / 37.2 / 37.0 / 36.6 / 36.3.Local changes to tools/ab_engine.py (whole answers kept, the die cooled before each start)
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.