mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #2819] [CLOSED] metal: --rope-scaling, --rope-scale, --rope-freq-base, --rope-freq-scale and --yarn-* reach the decode window's RoPE table #4281
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#4281
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/2819
Author: @oinani0721
Created: 10/16/2026
Status: ❌ Closed
Base:
main← Head:metal-rope-scaling📝 Commits (1)
b8bc481metal: --rope-scaling, --rope-scale, --rope-freq-base, --rope-freq-scale and --yarn-* reach the decode window's RoPE table📊 Changes
5 files changed (+287 additions, -11 deletions)
View changed files
📝
metal/CMakeLists.txt(+16 -0)➕
metal/src/engine/rope_window.h(+29 -0)📝
metal/src/engine/setup_host.cpp(+9 -3)📝
metal/src/engine/window.cpp(+4 -8)➕
metal/tests/engine/rope_table_parity.cpp(+229 -0)📄 Description
Title
Issue: Resolves #2767. No other issue or pull request covers RoPE scaling on the Mac engine (search in the first comment).
Every file:line is at
296b59d(v0.1.44.1, the last release) unless marked "branch". Short names are the files under src/, include/ or metal/, not their copies under sycl/; CMakeLists.txt is the root one. On the branch, window.cpp's lines 18-467 move 1 down and its lines after 475 move 4 up, setup_host.cpp's lines after 125 move 6 down, and metal/CMakeLists.txt's lines 106-123 move 9 down and its lines after 123 move 16 down.Summary
On a Mac,
--rope-scaling yarn --rope-scale 4is parsed, resolved and logged as active (strata generate: rope scaling yarn, factor 4 ..., generate.cpp:3002-3008), but, by the code, no rotation changes:rope_table_setdoes nothing on Metal (metal/src/engine/setup_host.cpp:151, "the window keeps its own table");qsa_prep, which reads only the decode window's own cos/sin table (qsa.metal:74);Window::initbuilds that table without scaling, from the model file'srope.freq_base(window.cpp:468-475).This PR makes the window build the table with
build_rope_table(64, rope_scaling(), ...), the builder the PC's session table uses (layer.cpp:837-840). setup_host.cpp:116-150 keeps rope.cu:48-86: the same 417 tokens once comments, whitespace and braces are ignored. By the code, linear and YaRN scaling, the magnitude factor (YaRN's mscale, or--yarn-attn-factor),--rope-freq-baseand--rope-freq-scalethen reach every rotation on a Mac without a kernel change (the reading is in the first comment). On an M5 Max (On-model results), yarn x4 changes the decode windows' logits on this branch and changes nothing on main (#2767); for each prompt path (the resident prompt chain, the streaming prompt GEMM) the test compares how close the path stays under yarn x4 to the same prompts read through decode windows, against a threshold derived in the same sitting, and for both paths that threshold did not separate the measured numbers, so they stay a code reading, as the MTP draft layer does.This is one engine part needed to run past the trained 262,144 tokens on a Mac, not all of them: Mac setup does not write the flags, and nothing checks the memory a long context takes on a resident pack.
What changed
metal/src/engine/window.cpp: the table loop inWindow::initbecomes onewindow_rope_table(strata::kernels::rope_scaling(), MAXC + 16, cs_.as<float>())call. The table keeps its size (MAXC + 16 rows) and layout (float2 [pos][32]).metal/src/engine/rope_window.h(new):window_rope_tablebuilds the table withbuild_rope_tableinto two temporary vectors of (MAXC + 16) x 32 floats each (branch rope_window.h:21), freed when it returns, and interleaves them into the window's rows; nothing it allocates stays. The window and the test run this same code.metal/src/engine/setup_host.cpp: a declaration of the scaledbuild_rope_tablewith__attribute__((flatten))(branch :126-131), soRopeScaling::mscale()is inlined into it under the flag below; the function's body does not change. The file's opening comment now says thatrope_scaling()andbuild_rope_tablefill the window's table.metal/CMakeLists.txt:set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/src/engine/setup_host.cpp PROPERTIES COMPILE_OPTIONS "-ffp-contract=off"), the form CMakeLists.txt:450-453 uses for two HIP files, with a comment that gives the by-hand check for the flatten (below); and therope_table_paritytarget withadd_test. The test has its own folder becausetests/*.cppare globbed into mtl_rt-only programs (metal/CMakeLists.txt:41-47). Setup builds onlystrata(setup_mac.py:280).metal/tests/engine/rope_table_parity.cpp(new; CPU only, no GPU or model; 5.7 s and 0.54 GB in ctest on the M5 Max). At the allocation of the 1M probe (bench/results/2026-10-09-rope-yarn-long-context/probe.py, whose default capacity is--max-context 1048832, probe.py:32), 1,048,976 rows, it checks:--rope-freq-base 5e6, against that loop given the same base (a copy of the test whose table ignores the base fails here);--yarn-attn-factor 0.9, linear x4 with--yarn-attn-factor 1.25and yarn with--rope-freq-scale 0.3: bit for bit against a float64 transcription of ggml'srope_yarn(llama.cpp 3cf03257, ggml-cpu/ops.cpp:5958-5973) with every operation rounded on its own. As in src/kernels/rope_parity.cpp:233-236, the correction dims and the ramp come from the shared helpers; an oracle written from ggml's own formulas checks those too (first comment, with its code);What it leaves alone:
.metalfile is touched, the spare indexer key included (Extra Notes).metal/setup_mac.pybefore its own parser runs (setup.py:5706-5707, 5446), so the--rope-scalingand--rope-scaleit defines (setup.py:5715-5719) never reach that parser; setup_mac.py still drops them with "not used on a Mac" (setup_mac.py:508-510) and writes no rope flags. Setup support is a later PR, which has to count a long context's memory first.metal/. The root CMakeLists.txt and sycl/CMakeLists.txt do not referencemetal/, so no CUDA, HIP or SYCL build compiles a changed file. They were not built.How the default path was checked
All on an M5 Max (128 GB), AppleClang 21.0.0, against the Metal build of
296b59d; the commands and their output lines are in the first comment.rope_scaling()isRopeScaling{}withfreq_basefrom the model file'sqwen4exp.rope.freq_base(generate.cpp:2940, 2969, 2978-2990), the key the window read before.qwen4exp.rope.freq_base = 10000000.0and norope.scaling.*key.otool -X -s __TEXT __text <object> | shasumdiffers only forsetup_host.cpp.o(97573d5fa328b5c1 -> fd4ef0c308696cb7) andwindow.cpp.o(afaf2e316677789d -> fc9d170bb55b5ea8). In setup_host.cpp.o 36 of 37 functions are identical; onlybuild_rope_table(int, const RopeScaling&, int, float*, float*)changes. In window.cpp.o, with the checkout path normalized, onlyWindow::initchanges;window_rope_tableis new and thestd::maplookup the old loop made is gone.runtime_hello,rope_table_parity).-Wall -Wextragives no warning on setup_host.cpp, the test or a file that includes only rope_window.h.tools.test_setup_macandtools.test_setup_ropepass (38 tests); no setup file is changed.Why
-ffp-contract=off, and whyflattenThe project holds this table bit for bit to a float64 reference (rope_scaling.hpp:24-26, 122-123). Without the flag Apple clang 21 fuses YaRN's blend and mscale (4 fused multiply-adds), and at 1M 2 of the 67,134,464 floats of the yarn x2 table differ from the reference (sin at position 589,449 pair 19 and at 910,519 pair 17); at
--max-context262,144 and 524,544 none do, which is why ctest runs the 1M allocation. With the flag all seven scaled configurations match.generate.cpp.okeeps an out-of-lineRopeScaling::mscale()with a fused multiply-add. Compiled with-fno-inlineas a stand-in for a compiler that stops inlining it, the builder withoutflattencallsmscale(), and a strata link binds that call to generate.cpp.o's copy; the flattened builder makes no such call. Today's build inlines it either way, and the flattened build gives the same eight tables (hashes in the first comment). ctest cannot see a lostflatten, since the test does not link generate.cpp.o; the CMake comment gives annmcheck, for the default Release build. With only mscale fused, the 1M yarn table would change at 1 of the 252 factors from 1.25 to 64 in steps of 0.25 (36.25, in 1 float).Inferred from the build files, not checked on a PC: rope.cu gets no
-march,-mfmaor/arch:(CMakeLists.txt:354, 1219-1225, 1326-1352), so a PC whose host compiler targets baseline x86-64 rounds it the same way. A host compiler that can fuse by default (an ARM64 Linux host, or-march=nativein the flags) may not.On-model results
Apple M5 Max (128 GB, macOS 27.0.1), 2026-10-16; Qwen3.8-Flash-Next-GSQ-RCO-abliterated-IQ3_S (revision eec4e10a, hashes in the first comment), greedy, thinking off; main is 296b59d's Metal build. B0-B2 and B6 ran resident at
--max-context 65536, except B2's two streaming runs; B3-B5 streamed with--kv int8. Desktop programs stayed open (30-42 GB in use beside the engine), so every streaming start ran with its cap lowered bySTRATA_METAL_LEAVE_SHARE(recorded per run) and with the model's pages dropped from the file cache first; the first comment says why and has every table.STRATA_METAL_LOGPOSrows byte-identical. Setup's default config, 6 requests (decided: the control, this branch run twice, was identical): tokens and DONE fields identical, 6 of 6 logit files byte-identical.--rope-scaling yarn --rope-scale 4 --yarn-orig-ctx 262144) against no flag gives the same results in every comparison of B1, while the log saysrope scaling yarn, factor 4.--kv int8against fp16 at 32K: identical tokens and byte-identical logits, as #2707 reads the code (qsa_prepstores K and V as int8 whatever--kvsays).STRATA_WATCHDOG_S=300, the harness's setting when the arena is smaller than B4's): without scaling 1 of 3 (CEDAR-482|MARBLE-956|QUARTZ-956), prefill 589 tok/s (1,781 s), decode 20.0 tok/s; yarn x4 3 of 3, 580 tok/s (1,807 s), 18.0 tok/s. The cap was lowered to 54.6 GB (a 13.0 GB arena). The PC run in bench/results/2026-10-09-rope-yarn-long-context (README.md:146-148; RTX PRO 6000, IQ3_XXS, engine 0.1.41) found 0 of 3 and 3 of 3.Extra Notes
--rope-freq-baseand a model file'srope.scaling.*keys now take effect on Metal, and--rope-freq-scaleand the--yarn-*knobs do under linear or yarn, as on the PC (generate.cpp:2941-2944, 2957-2990); before, Metal ignored them. None of the six files Mac setup offers hasrope.scaling.*keys.296b59dno SAVE or RESTORE gets past the RAM check, as the engine says when it starts (memory guard: the available RAM cannot be read; parking and session save/restore are refused, generate.cpp:2327-2331; conversation_memory.cpp:280-292; conversation_file.cpp:1153, 1216-1222). With #2503's macOS reading (its diff, read) a RESTORE still fails while the file is parsed: the window writes 4 indexer tail rows a QSA layer (window.cpp:1384-1397) where the read limit allows 3 (conversation_state.cpp:117-122, 338; conversation_file.cpp:733, 760-761).🤖 Generated with Claude Code
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.