diff --git a/.gitmodules b/.gitmodules index 9eb8d89..755d835 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +1,5 @@ [submodule "third_party/llama.cpp"] path = third_party/llama.cpp - url = https://github.com/ggml-org/llama.cpp.git + url = https://github.com/robertkarl/llama.cpp.git + branch = UnbiasedStrata shallow = true diff --git a/CMakeLists.txt b/CMakeLists.txt index 7b51e42..5269053 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1459,23 +1459,8 @@ if(STRATA_NATIVE_EXPERTS) if(NOT EXISTS ${STRATA_GGML_DIR}/ggml/CMakeLists.txt) message(FATAL_ERROR "ggml was not found in ${STRATA_GGML_DIR}: run `git submodule update --init` first") endif() - # UnbiasedStrata: patches/*.patch are applied to the submodule here, at configure time, so a tree built with cmake - # directly gets them too. Each is checked in reverse first, so a configure is idempotent. See UPSTREAM.md. - file(GLOB _strata_patches ${CMAKE_CURRENT_SOURCE_DIR}/patches/*.patch) - list(SORT _strata_patches) - foreach(_strata_patch IN LISTS _strata_patches) - execute_process(COMMAND patch -p1 -N -R --dry-run -s -i ${_strata_patch} WORKING_DIRECTORY ${STRATA_GGML_DIR} - RESULT_VARIABLE _strata_patched OUTPUT_QUIET ERROR_QUIET) - if(NOT _strata_patched EQUAL 0) - execute_process(COMMAND patch -p1 -N -s -i ${_strata_patch} WORKING_DIRECTORY ${STRATA_GGML_DIR} - RESULT_VARIABLE _strata_patch_rc OUTPUT_VARIABLE _strata_patch_out ERROR_VARIABLE _strata_patch_out) - if(NOT _strata_patch_rc EQUAL 0) - message(FATAL_ERROR "could not apply ${_strata_patch} to ${STRATA_GGML_DIR}:\n${_strata_patch_out}") - endif() - get_filename_component(_strata_patch_name ${_strata_patch} NAME) - message(STATUS "applied patches/${_strata_patch_name} to ${STRATA_GGML_DIR}") - endif() - endforeach() + # UnbiasedStrata: the submodule is the fork robertkarl/llama.cpp, branch UnbiasedStrata (upstream's pin plus the + # tool-call parser commits UPSTREAM.md lists). Nothing is patched at configure; the submodule's tree is what is built. add_subdirectory(${STRATA_GGML_DIR}/ggml ${CMAKE_BINARY_DIR}/ggml EXCLUDE_FROM_ALL) # part of strata_kernels_cpu (the pool calls these and they call its CPU probe: one library, so GNU ld's # one-pass static linking has no cycle to resolve) diff --git a/PLAN.md b/PLAN.md index 85fe79a..3bf276b 100644 --- a/PLAN.md +++ b/PLAN.md @@ -190,7 +190,7 @@ Facts that shape the implementation either way: - The parser patch `qwen3-coder any argument order` from the old fork (`patches/`) is still needed at this pin: Qwen3.6 and Qwen3.8 route to that parser and the upstream grammar silently drops a call whose optional argument comes first. Applied once, at configure time, idempotently (the old - fork had to apply it in two places; we apply it in one). + fork had to apply it in two places; we apply it in one). (2026-10-19: the patch and its DeepSeek/GLM counterpart are commits on robertkarl/llama.cpp, branch `UnbiasedStrata`, which the submodule pins; nothing is applied at configure any more.) - Prompt caching stays in the engine. The front end sends the full id list every request. - Encode images on CPU by default to leave VRAM for the expert cache; `--vision-gpu` to move it. @@ -227,9 +227,9 @@ between `generate.cpp` and `src/poly/main.cpp`. Fallbacks, in order: one binary ``` Makefile make build | make test | make bench ; CUDA_ARCHS=, PORTABLE=, JOBS= -CMakeLists.txt upstream's, plus: submodule default, patch step, front end, merged MMQ lib -.gitmodules third_party/llama.cpp -> ggml-org/llama.cpp @ 3cf03257, shallow -patches/ *.patch with header: file, reason, measurement; applied at configure +CMakeLists.txt upstream's, plus: submodule default, front end, merged MMQ lib +.gitmodules third_party/llama.cpp -> robertkarl/llama.cpp branch UnbiasedStrata (3cf03257 + parser commits), shallow + (patches/ applied at configure until 2026-10-19; now commits on that branch, see UPSTREAM.md) frontend/ strata-context.cpp, engine.hpp, vision.hpp, ui_stub.cpp, main.cpp, tests src/ include/ upstream engine, unchanged except what UPSTREAM.md lists tools/ upstream's offline Python, minus installers (ยง8) @@ -346,7 +346,7 @@ present-unmeasured, the exact build lines, the exact run line per measured model limits (no constrained decoding, one request at a time, CPU image encode time). No superlatives. `UPSTREAM.md`: the upstream commit; the llama.cpp pin; each engine or build hunk with file, reason, -and the measurement that justified it; each patch under `patches/`; the list of features dropped +and the measurement that justified it; each commit on the llama.cpp fork's branch; the list of features dropped relative to upstream with the rule. Updated in the same commit as any engine or build change. ## 11. Measurements diff --git a/README.md b/README.md index 390c691..38e8094 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ and curl for the tests. Nothing is downloaded after the clone: ggml, llama, comm ``` git clone && cd UnbiasedStrata -git submodule update --init # shallow, third_party/llama.cpp @ 3cf03257 +git submodule update --init # shallow, third_party/llama.cpp @ robertkarl/llama.cpp UnbiasedStrata (a6de9af3) make build # build/bin/{strata,strata-poly,libstrata_kq.so,unbiased-strata} make test # the front end against a fake engine; no GPU, no model ``` @@ -40,8 +40,9 @@ make test # the front end against a fake engine; no G The two `git` lines are the only ones that reach the network. For a box without one, run them on a machine that has it and copy the tree over (`rsync -a UnbiasedStrata/ box:UnbiasedStrata/`; about 700 MB with `.git`), then `make build` there; that is how the A6000 VM below was done (2026-10-19, fresh clone: 5 s clone, 27 s submodule, 20.6 min `make build` -at the default four archs on 12 cores and CUDA 12.4, 9 s `make test`). Configuring applies `patches/` to the submodule -in place, so `git status` shows `third_party/llama.cpp` modified from then on; that is expected. +at the default four archs on 12 cores and CUDA 12.4, 9 s `make test`). The submodule is a fork of llama.cpp +(robertkarl/llama.cpp, branch `UnbiasedStrata`: upstream's pin plus the tool-call parser commits in UPSTREAM.md); nothing is +patched at configure, and `git status` is clean after a build. `make build` configures once and builds only the four targets above, not upstream's `all`. Options, as `make` variables: @@ -57,8 +58,8 @@ variables: `make frontend` builds `unbiased-strata` alone, without CUDA, into `build-frontend/`: for a laptop or a CI box. `make test` builds that and runs `frontend/tests/`: seven adapter cases against `fake-engine.py` (cancel, drain, -backpressure, child exit, `ERR`) and `e2e.sh`, the HTTP surface a coding agent touches. The `patches/` directory is -applied to the submodule at configure time (one patch, the Qwen3-Coder tool-call grammar; see UPSTREAM.md). +backpressure, child exit, `ERR`) and `e2e.sh`, the HTTP surface a coding agent touches. The llama.cpp changes the front +end needs (the tool-call grammars) are commits on the submodule's branch; see UPSTREAM.md. Boxes this has been built and served on: a RunPod RTX 3090 (24 GB, driver 580, CUDA 12.x) and an RTX A6000 (48 GB) in a Proxmox VM with no route to the internet. The front end's tests also run on macOS. @@ -205,8 +206,8 @@ the 3090 (Flash-Next with the installer's `--pool-workers 15`; 24 costs 14 %) an - No constrained decoding: a request's `response_format` or tool schema is parsed, not enforced at the sampler; the server logs `a grammar was requested; this engine does not constrain sampling` once per such request. One consequence is handled: llama.cpp's tool-call grammars expect required arguments in the schema's order, which - only a constrained sampler guarantees, so `patches/llama.cpp-any-argument-order.patch` accepts any order (DeepSeek - V4 Flash and GLM 5.3 Flash write `content` before `path`; without the patch their finished calls came back as parse + only a constrained sampler guarantees, so the submodule's branch accepts any order (DeepSeek + V4 Flash and GLM 5.3 Flash write `content` before `path`; at upstream's pin their finished calls came back as parse errors). A tool call that is malformed in other ways is still an error to the client, not a retry. - GLM 5.3 Flash's template honours `reasoning_effort` only as `low` or `high` and treats anything else as `max`, at which it thinks to the token cap on a coding task; send one of the two, or set `--chat-template-kwargs diff --git a/UPSTREAM.md b/UPSTREAM.md index 7279ac1..dd999ca 100644 --- a/UPSTREAM.md +++ b/UPSTREAM.md @@ -5,7 +5,7 @@ | What | Where | Version | | --- | --- | --- | | Engines (`strata`, `strata-poly`), tools, docs | https://github.com/VecSzn/PolyStrata | tag `v0.1.45-poly.1`, commit `d0cd84dc6bd009d1d9e2191e4e28cc76b80f1ee3` | -| ggml, llama, common, mtmd, cpp-httplib | https://github.com/ggml-org/llama.cpp, as the submodule `third_party/llama.cpp` | commit `3cf03257f219afbe7334045ff7c6a06ac68c627d`, the one upstream pins | +| ggml, llama, common, mtmd, cpp-httplib | https://github.com/robertkarl/llama.cpp, branch `UnbiasedStrata`, as the submodule `third_party/llama.cpp` | commit `a6de9af3b19b5e3a5397f2c835a29a13c37664bd`: ggml-org `3cf03257f219afbe7334045ff7c6a06ac68c627d` (the commit upstream pins) plus the two parser commits below | Commit `0197ae86` ("Import PolyStrata v0.1.45-poly.1 (d0cd84dc) unmodified") is the upstream tree, every tracked file, with the same tree hash as upstream's commit. `git diff 0197ae86 HEAD` is the whole story of this fork; @@ -21,17 +21,18 @@ between a request and a token, (3) breaks or complicates the one-command build, Everything else stays, built or not. Engine changes are bug fixes for the target (Linux x86-64, one NVIDIA GPU, Pascal or newer, CUDA 12.x), each recorded below with the file, the reason and the measurement. -## Build (`CMakeLists.txt`, `Makefile`, `.gitmodules`, `.gitignore`, `patches/`) +## Build (`CMakeLists.txt`, `Makefile`, `.gitmodules`, `.gitignore`) - **ggml from the submodule, not a fetch.** `STRATA_GGML_DIR` defaults to `third_party/llama.cpp` and the configure stops with the `git submodule update --init` line if `ggml/CMakeLists.txt` is missing. Upstream's `FetchContent` of llama.cpp (the only network access of its build) is deleted. The three later `if(STRATA_GGML_DIR)` blocks are unchanged; their `else()` branches are now dead. `.gitignore` no longer ignores `/third_party/llama.cpp*`. -- **`patches/*.patch` applied at configure time.** Each patch is checked in reverse first, so configuring twice - applies it once. The submodule's working tree is therefore modified after a configure; `git -C - third_party/llama.cpp diff` shows exactly the patches. UnbiasedStrata 1 applied its patch in `make` and in - CMake both; here it is CMake only, so a tree configured directly gets it too. +- **llama.cpp changes are commits on a fork, not configure-time patches.** The submodule points at + robertkarl/llama.cpp, branch `UnbiasedStrata`: ggml-org `3cf03257` plus the two commits under "Changes to llama.cpp" + below. Until 2026-10-19 the same changes were `patches/*.patch`, applied to the submodule's working tree by CMake at + configure (UnbiasedStrata 1 did it in `make` and in CMake both), which left the submodule dirty after every build and + meant the pinned commit alone did not name the sources compiled. The pin now does; `git status` is clean after a build. - **llama.cpp's libraries for the front end.** A second `add_subdirectory` of the submodule with `LLAMA_BUILD_COMMON` and `LLAMA_BUILD_MTMD` on and tests, tools, examples, server, app, UI, prebuilt UI, OpenSSL and llguidance off. It reuses the ggml targets configured above. Gated by `STRATA_FRONTEND` (default on), @@ -85,15 +86,17 @@ Recorded here because a client written for one of the two may notice. end reports it as an engine death with an empty message. Open; matters only to a service manager. - **Default `min_p`** is llama-server's 0.05 when a request omits it; upstream's server sends none. pi sends 0. -## Patches to llama.cpp (`patches/`) +## Changes to llama.cpp (robertkarl/llama.cpp, branch `UnbiasedStrata`) -- `llama.cpp-qwen3-coder-any-argument-order.patch` (`common/parsers/qwen3-coder.cpp`): the Qwen3-Coder +Two commits on top of ggml-org `3cf03257`; `git -C third_party/llama.cpp diff 3cf03257 HEAD` is exactly these. + +- `9547d8d` (`common/parsers/qwen3-coder.cpp`): the Qwen3-Coder tool-call grammar accepts a call's arguments in any order. Upstream takes the required ones first and the optional ones after; Qwen3.8 Flash-Next often writes the optional ones first (`limit`, `offset`, `path`), and because the grammar's tool-call group is optional and has no end anchor, such a call was dropped silently: the client got `finish_reason=stop` and an empty answer (UnbiasedStrata 1, 2026-10-08, REGINA-104). Qwen3.6's - template routes to the same parser. Carried over unchanged; it applies cleanly at this pin. -- `llama.cpp-any-argument-order.patch` (`common/chat-auto-parser-generator.cpp`, `common/parsers/deepseek.cpp`): the same for every + template routes to the same parser. Carried over from UnbiasedStrata 1 unchanged. +- `a6de9af` (`common/chat-auto-parser-generator.cpp`, `common/parsers/deepseek.cpp`): the same for every template the auto-generated PEG grammar serves (DeepSeek V4 Flash's DSML, GLM 5.3 Flash's `` form, and the rest). Upstream accepts the required arguments only in the schema's order, which holds when its own sampler is constrained by the grammar; this engine samples unconstrained, and both models wrote `content` before `path` diff --git a/docs/CORRECTNESS-PLAN.md b/docs/CORRECTNESS-PLAN.md index 3adf8c4..e42f8b2 100644 --- a/docs/CORRECTNESS-PLAN.md +++ b/docs/CORRECTNESS-PLAN.md @@ -82,9 +82,9 @@ The native kernels are compiled with `--use_fast_math` to "match the pinned llam contract" (`CMakeLists.txt:506-519`); ggml-cuda itself uses `-use_fast_math` (`third_party/llama.cpp/ggml/src/ggml-cuda/CMakeLists.txt:186`). -One patch is applied to the submodule at configure time (`CMakeLists.txt:1462-1478`, `UPSTREAM.md:62-64`): -`patches/llama.cpp-qwen3-coder-any-argument-order.patch`, a tool-call parser change. It touches nothing the -engines or the reference arithmetic use. +The submodule's branch (robertkarl/llama.cpp `UnbiasedStrata`; at the time of these checks the same change was a +configure-time patch) carries tool-call parser changes only (`common/parsers/*`, `common/chat-auto-parser-generator.cpp`; +UPSTREAM.md "Changes to llama.cpp"). They touch nothing the engines or the reference arithmetic use. Both model architectures load at the pin: `qwen35moe` (`src/llama-arch.h:47`, graph `src/models/qwen35moe.cpp`) and `qwen4exp` (`src/llama-arch.h:48`, graph `src/models/qwen4exp.cpp`; diff --git a/patches/llama.cpp-any-argument-order.patch b/patches/llama.cpp-any-argument-order.patch deleted file mode 100644 index 0e513ed..0000000 --- a/patches/llama.cpp-any-argument-order.patch +++ /dev/null @@ -1,77 +0,0 @@ -diff --git a/common/chat-auto-parser-generator.cpp b/common/chat-auto-parser-generator.cpp -index b78789d..bfd762c 100644 ---- a/common/chat-auto-parser-generator.cpp -+++ b/common/chat-auto-parser-generator.cpp -@@ -391,22 +391,21 @@ common_peg_parser analyze_tools::build_tool_parser_tag_tagged(parser_build_conte - } - }); - -- // Build required arg sequence in definition order -- common_peg_parser args_seq = p.eps(); -- for (size_t i = 0; i < required_parsers.size(); i++) { -- if (i > 0) { -- args_seq = args_seq + p.space(); -- } -- args_seq = args_seq + required_parsers[i]; -+ // UnbiasedStrata 2: every argument, required or optional, in any order. Upstream requires the required -+ // arguments in the schema's order, which holds when its sampler is constrained by this grammar; here the -+ // engine samples unconstrained and DeepSeek V4 Flash and GLM 5.3 Flash write `content` before `path` for a -+ // `write` tool that declares `path` first, so the finished, well-formed call failed to parse. A missing -+ // required argument is left for the client to report, as it is for optional-only tools. -+ common_peg_parser any_arg = p.choice(); -+ for (const auto & req : required_parsers) { -+ any_arg |= req; - } -- -- // Build optional args with flexible ordering -- if (!optional_parsers.empty()) { -- common_peg_parser any_opt = p.choice(); -- for (const auto & opt : optional_parsers) { -- any_opt |= opt; -- } -- args_seq = args_seq + p.repeat(p.space() + any_opt, 0, -1); -+ for (const auto & opt : optional_parsers) { -+ any_arg |= opt; -+ } -+ common_peg_parser args_seq = p.eps(); -+ if (!required_parsers.empty() || !optional_parsers.empty()) { -+ args_seq = p.repeat(p.space() + any_arg, required_parsers.empty() ? 0 : 1, -1); - } - - if (!arguments.start.empty()) { -diff --git a/common/parsers/deepseek.cpp b/common/parsers/deepseek.cpp -index 9ca4bb3..ae538f0 100644 ---- a/common/parsers/deepseek.cpp -+++ b/common/parsers/deepseek.cpp -@@ -178,20 +178,18 @@ common_chat_params common_chat_params_init_deepseek_v3_2(const common_chat_templ - } - }); - -- common_peg_parser args_seq = p.eps(); -- for (size_t i = 0; i < required_parsers.size(); i++) { -- if (i > 0) { -- args_seq = args_seq + p.space(); -- } -- args_seq = args_seq + required_parsers[i]; -+ // UnbiasedStrata 2: every argument in any order (see common/chat-auto-parser-generator.cpp for the reason: -+ // this engine samples unconstrained, and DeepSeek V4 Flash writes `content` before `path`). -+ common_peg_parser any_arg = p.choice(); -+ for (const auto & req : required_parsers) { -+ any_arg |= req; - } -- -- if (!optional_parsers.empty()) { -- common_peg_parser any_opt = p.choice(); -- for (const auto & opt : optional_parsers) { -- any_opt |= opt; -- } -- args_seq = args_seq + p.repeat(p.space() + any_opt, 0, -1); -+ for (const auto & opt : optional_parsers) { -+ any_arg |= opt; -+ } -+ common_peg_parser args_seq = p.eps(); -+ if (!required_parsers.empty() || !optional_parsers.empty()) { -+ args_seq = p.repeat(p.space() + any_arg, required_parsers.empty() ? 0 : 1, -1); - } - - common_peg_parser invoke_body = args_seq; diff --git a/patches/llama.cpp-qwen3-coder-any-argument-order.patch b/patches/llama.cpp-qwen3-coder-any-argument-order.patch deleted file mode 100644 index 683bd32..0000000 --- a/patches/llama.cpp-qwen3-coder-any-argument-order.patch +++ /dev/null @@ -1,26 +0,0 @@ -diff --git a/common/parsers/qwen3-coder.cpp b/common/parsers/qwen3-coder.cpp -index 208f551..2d7c80f 100644 ---- a/common/parsers/qwen3-coder.cpp -+++ b/common/parsers/qwen3-coder.cpp -@@ -139,12 +139,15 @@ common_chat_params common_chat_params_init_qwen3_coder(const common_chat_templat - (param.required ? required_args : optional_args).push_back(arg_rule); - }); - -- // Accept required arguments in any order, as Qwen does not always adhere to the -- // order provided. -- auto args = p.permute("tool-" + name + "-args", required_args); -- if (!optional_args.empty()) { -- args = args + p.zero_or_more(p.choice(optional_args)); -- } -+ // Accept the arguments in any order, required and optional interleaved. The upstream rule -+ // took the required ones first and the optional ones after; Qwen3.8 Flash-Next often writes -+ // the optional ones first (limit, offset, path), and because the tool-call group below is -+ // optional and the grammar has no end anchor, such a call was dropped without any error: -+ // the client got finish_reason=stop and an empty answer. Whether every required argument -+ // is present is for the tool to report; a dropped call reports nothing. -+ std::vector all_args = required_args; -+ all_args.insert(all_args.end(), optional_args.begin(), optional_args.end()); -+ auto args = all_args.empty() ? p.eps() : p.zero_or_more(p.choice(all_args)); - - auto func = p.tool(p.tool_open("\n") + - p.tool_args(args) + diff --git a/third_party/llama.cpp b/third_party/llama.cpp index 3cf0325..a6de9af 160000 --- a/third_party/llama.cpp +++ b/third_party/llama.cpp @@ -1 +1 @@ -Subproject commit 3cf03257f219afbe7334045ff7c6a06ac68c627d +Subproject commit a6de9af3b19b5e3a5397f2c835a29a13c37664bd