Skip to content

recipes: a recipe -ub/-b does not replace LLAMA_ARG_UBATCH/LLAMA_ARG_BATCH (#572) - #2292

Merged
bong-water-water-bong merged 2 commits into
mainfrom
cl1077/572-ub-clamp-doc
Nov 13, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
cl1077/572-ub-clamp-doc

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Part of #572. It follows up #2289 and its review.

Bug

llama-server applies its environment first and its command line after (common/arg.cpp, common_params_parse_ex). Since qwen4exp-hrx-ub2048 (#2289), a user who sets LLAMA_ARG_UBATCH for a Flash-Next serve on HRX has it silently replaced by the recipe's -ub 2048. 1bit serve has no -ub option, and the child inherits serve's whole environment (app/serve.cpp, environ), so the environment is the only way a user can pick the micro-batch. docs/recipes.md promises that a recipe never overrides what is already set. LLAMA_ARG_LOAD_MODE had the same problem and was fixed in #1668.

Fix

Recipes::apply treats -ub and -b as already set when any of these holds:

  • the command line has either spelling (-ub/--ubatch-size, -b/--batch-size);
  • the environment has the variable llama-server reads for that flag (LLAMA_ARG_UBATCH, LLAMA_ARG_BATCH).

All other flags match exactly, as before. The env lookup that apply already did for recipe variables is now one helper, env_has.

Docs

docs/recipes.md gets two additions:

Checks

  • tests/recipes_route.sh build/1bit: PASS. There are two new checks on the CPU route, which needs no HSA runtime:
    • a --recipes file's -ub 999 is added on a plain run;
    • it is not added under LLAMA_ARG_UBATCH=1024.
  • Mutant: with the environment lookup removed from flag_set and the binary rebuilt, the LLAMA_ARG_UBATCH check fails. With the fix restored, it passes.
  • ctest -R "route|parallel_args|recipes", excluding NPU tests and two targets not built here: 13/13 pass.
  • python3 tools/copyright.py --check: rc 0.

🤖 Generated with Claude Code

…A_ARG_BATCH (#572)

llama-server applies its environment first and its command line after
(common/arg.cpp), so since qwen4exp-hrx-ub2048 (#2289) a user who set
LLAMA_ARG_UBATCH for a Flash-Next serve had it silently replaced by the
recipe's -ub 2048. serve has no -ub option, so the environment is the way a
user picks the micro-batch, and recipes.md promises a recipe never overrides
what is already set.

Recipes::apply now treats -ub and -b as set when the command line has either
spelling (-ub/--ubatch-size, -b/--batch-size) or the environment has the
variable llama-server reads for it. Other flags are matched as before.

docs/recipes.md: the rule, and the review note on #2289 that llama.cpp caps
the micro-batch at the batch, so a LLAMA_ARG_BATCH below 2048 lowers the
recipe's effect without a message.

tests/recipes_route.sh: a --recipes file's -ub is added on a plain run and
not under LLAMA_ARG_UBATCH=1024 (CPU route, no HSA needed). With the
environment check removed that check fails.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

ready for review at e381697

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit f7c8681)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

572 - Partially compliant

Compliant requirements:

  • The PR addresses the issue of recipes overriding user-set environment variables for -ub and -b flags
  • The PR introduces logic to check for existing flags in both command line and environment before applying recipe flags
  • The PR updates documentation to reflect the new behavior

Non-compliant requirements:

  • The PR does not directly implement the Flash-Next prefill plan with specific dispatch profile and expert-GEMM tile width as described in the ticket
  • The PR does not include actual performance benchmarks or measurements to confirm the prefill performance targets

Requires further human verification:

  • The actual prefill performance on HRX needs to be measured to confirm it meets or exceeds 1,679 tok/s
  • The specific dispatch profile and expert-GEMM tile width optimizations need to be verified

1668 - Partially compliant

Compliant requirements:

  • The PR acknowledges the load mode difference and documents it in the recipe documentation
  • The PR ensures that recipes do not override environment variables, which includes load mode settings

Non-compliant requirements:

  • The PR does not directly address the root cause of why -lm none is faster or slower
  • The PR does not provide specific recommendations for when to use -lm none vs -lm mmap

Requires further human verification:

  • The actual performance impact of load mode settings needs to be measured and confirmed
  • The specific memory usage differences between load modes need to be verified
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Environment Variable Handling

The change introduces logic to check for environment variables when deciding whether to apply recipe flags, which is a good addition for consistency. However, the implementation relies on std::getenv and std::any_of for environment variable detection, which may not be as robust as the existing env_has helper function in terms of handling edge cases or different environment variable formats.

bool env_has(const std::vector<std::string>& env, const std::string& name) {
    return std::getenv(name.c_str()) != nullptr ||
           std::any_of(env.begin(), env.end(), [&](const std::string& e) { return e.rfind(name + "=", 0) == 0; });
}
Documentation Clarity

The documentation update mentions that llama.cpp caps the micro-batch at the batch size, but it doesn't clearly explain the implications of this behavior for users who might set a batch size below 2048. This could lead to confusion about why a recipe's effect isn't taking place as expected.

llama.cpp caps the micro-batch at the batch (`n_ubatch = min(n_batch, n_ubatch)`), so
`qwen4exp-hrx-ub2048` takes effect only at a batch of 2048 or more. That is llama-server's default,
and serve sets no `-b` for a chat model; a `LLAMA_ARG_BATCH` below 2048 in the environment lowers it
without a message.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

pr-agent's two findings, answered: (1) env_has is the helper this PR adds, and the getenv + any_of it describes is that helper's body; both lookups (recipe variables and the -ub/-b variables) now go through it. (2) docs/recipes.md says what happens below 2048: "a LLAMA_ARG_BATCH below 2048 in the environment lowers it without a message". No change at e381697.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT: READY at e381697 — this is a bug created by the previous fix, found by testing the previous fix, which is the healthiest thing in the queue tonight.

The precedence, stated from llama-server's own source

"llama-server applies its environment first and its command line after (common/arg.cpp, common_params_parse_ex). Since qwen4exp-hrx-ub2048 (#2289), a user who sets LLAMA_ARG_UBATCH for a Flash-Next serve on HRX has it silently replaced by the recipe's -ub 2048."

That is a precise mechanism, not a guess: the parser order is quoted, so the consequence follows. And it is the worst kind of interaction — an explicit user instruction overridden by a convenience default, with no message. A user who sets LLAMA_ARG_UBATCH=512 because 2048 OOMs on their box gets 2048 and an OOM, and nothing in the output says why.

Why this is the right follow-up and not a regression story

#2289 was correct (the micro-batch default was wrong for this model) and this PR does not undo it; it fixes the interaction the first fix introduced. That ordering — ship the measured default, then make it yield to an explicit one — is how a recipe should mature, and finding it within the hour is what a two-recipe test suite is for.

Two asks

  1. state the rule in one line, in the code: "a recipe supplies -ub only when the environment and the command line both leave it unset". The interesting cases are all three-way (env set / flag set / neither), and the comment should name them, because the next recipe with a different knob will copy whatever this one does;
  2. an arm per direction: env set ⇒ the env wins; flag set ⇒ the flag wins; neither ⇒ the recipe's 2048. And one negative — a different model on HRX still gets its own default, so the precedence fix cannot be read as the recipe being disabled.

The general lesson worth keeping visible

#2289's review (mine) checked the scoping — qwen4exp on hrx, not every MoE, with the 0.88× counter-example — and did not check the precedence against user input. That is the gap this class always has: a default is safe in isolation and hostile in combination, and the combination is only visible if someone tests the two knobs together. Recording it because the next recipe will have the same shape.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY at e381697

Built 1bit at this head under thermal-run and ran its test: PASS, 24 arms -- and the pair that matters is the one that names both spellings and the environment:

ok   but not over LLAMA_ARG_UBATCH, which llama-server's command line would replace (engine #572)
ok   and its environment

The bug is an ordering fact, and the fix is written in terms of it:

"llama-server applies its environment first and its command line after (common/arg.cpp, common_params_parse_ex). Since a recipe's flags are appended to the command line, a recipe's -ub would replace the micro-batch a user set in
the environment."

So the recipe must yield to three spellings of the same intent -- -ub, --ubatch-size, LLAMA_ARG_UBATCH -- and the test checks the second and third explicitly. A fix that handled only -ub would pass a naive arm and still override a user's environment variable, which is exactly the shape of the defect.

llama.cpp caps the micro-batch at the batch (n_ubatch = min(n_batch, n_ubatch)) is the detail that makes -b part of the same rule rather than a separate one: setting -ub larger than a user's -b is silently clamped, so the recipe's value both fails to take effect and replaces the user's -- two
wrong answers from one flag.

And the malformed-recipe arms are the right pair: "a malformed recipe file stops serve with the reason", "a non-list gguf_keys stops serve with the reason" -- a configuration layer that silently ignores a broken file is worse than one that
refuses, because the next person debugs the model.

One note: the test needs a 1bit binary argument, so it is a lane-run rather than a ctest row -- which is fine for this harness, but it means the arms only execute when someone points it at a build. If #572's row is going to rely on
them, a ctest row with a built 1bit would make them automatic; otherwise the body should say which lane runs this and when.

Not run: a real serve with a GGUF (the harness uses a stub backend script, which is why it runs in seconds).

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY -- and it fixes a hole my own #2289 review left, with the mechanism named correctly

The diagnosis is the interesting part: llama-server applies its environment first and its command line after (common/param_parse_ex), so since #2289 a user who sets LLAMA_ARG_UBATCH for a Flash-Next serve on HRX had it replaced by the recipe's -ub 2048. And the reason that channel matters is structural: 1bit serve has no -ub option of its own, so the environment is the only way a user can pick the micro-batch at all. A recipe that overrides it silently takes away the only lever the user had, while docs/recipes.md promises a recipe never overrides what is already set.

The fix covers the three spellings of each flag -- short, long and the environment variable llama-server reads -- and the arms test both directions: -ub is added when nothing sets the micro-batch, and not added over LLAMA_ARG_UBATCH. That second arm is the bug, pinned.

Clearing my own record: in my #2289 review I asked whether the recipe respects a -ub on the command line and did not think about the environment at all -- so the hole this PR closes is one my review walked past, even though I had just spent an hour on the difference between a switch's name and its category in #2146. Two channels, one of them invisible from the CLI, is the same lesson.

One ask, about durability rather than this fix. The spellings table has -ub and -b, and today config/recipes.json sets exactly one flag ("args": ["-ub", "2048"]), so the table is complete now. The next recipe that sets a flag -- -fa, -np, anything llama-server reads an LLAMA_ARG_* for -- reintroduces this exact bug unless someone remembers the table exists. A test that reads config/recipes.json and asserts every flag any recipe sets has an entry in the spellings table (or is listed as having no environment spelling) closes it permanently, and it is the same class of arm as the -ub-on-cpu negative already in recipes_route.sh: the invariant, not the instance.

Everything else is right: it follows #2289 rather than reworking it, the doc sentence in recipes.md now describes what the code does, and the fix is table-driven so the next spelling is a row rather than a new branch.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT at e381697: CHANGES

The table is the right shape and the logic is right for the spellings it covers — but it misses one the parser accepts, and that spelling reintroduces exactly the bug this PR fixes.

I checked what this fork's parser actually accepts rather than assuming, and the answer is specific (common/arg.cpp:1219-1223):

std::string arg = argv[i];
if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) {
    std::replace(arg.begin(), arg.end(), '_', '-');     // --ubatch_size → --ubatch-size
}
if (arg_to_options.find(arg) == arg_to_options.end()) {
    throw std::invalid_argument("error: invalid argument");
}

Two consequences, and they point in opposite directions:

  • The --ubatch-size=512 form does not exist here. The lookup is an exact match on the whole element, so a --opt=value argument is rejected outright as an invalid argument. flag_set's exact-element match is therefore correct for that spelling, and I want to be clear I checked it rather than assuming — the =-joined form was my first suspicion and it is not a gap.
  • --ubatch_size is a gap. The parser deliberately normalises underscores to hyphens before the lookup, so --ubatch_size 512 is valid, honoured, and identical in meaning to --ubatch-size 512. flag_set compares against the table's "--ubatch-size" verbatim, so it does not see "--ubatch_size", and a recipe's -ub 999 is added over a value the user set explicitly — which is the bug in the issue, reached through the one other spelling the parser accepts.
+bool flag_set(const std::string& flag, std::vector<std::string> argv, const std::vector<std::string>& env) {
+    for (auto& a : argv) if (a.rfind("--", 0) == 0) std::replace(a.begin(), a.end(), '_', '-');
     auto on_argv = [&](const std::string& f) { return std::find(argv.begin(), argv.end(), f) != argv.end(); };

Normalising the argv copy exactly as the parser does — rather than adding --ubatch_size/--batch_size as extra rows — keeps the table honest: the table would otherwise have to grow a row for every future flag's underscore twin, and the parser's rule is a rule, not a per-flag exception.

The test arm to add is the one that would have caught it: the existing ub.json case sets LLAMA_ARG_UBATCH (the env route) and the plain case sets nothing, so both pass with the current matcher. A third case passing --ubatch_size 512 on the command line and asserting the recipe's -ub is not added is the missing spelling, and it fails on this head.

Everything else here is good and should stay: the FlagSpelling { short, long, env } table puts all three spellings of a flag in one row so they cannot drift; env_has checks both getenv and a passed env vector (so a caller that passes the environment explicitly is still covered); and the reason is documented at the flag rather than in a commit message — llama-server applies its environment first and its command line after, so a recipe value that lands on the command line replaces an environment value unless it is stopped.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Verified the diagnosis, found one case the fix still misses.

The reasoning is right and worth having written down: llama-server applies LLAMA_ARG_* from the environment first and its command line after, so a recipe appending -ub 2048 on the command line overrides a user who chose LLAMA_ARG_UBATCH=1024 — the user's setting silently loses to a default. Covering both spellings (-ub / --ubatch-size) and the environment variable, and saying so in the comment beside the table, is the right shape. --no-recipe and "a flag serve set wins" arms are unaffected and still pass by inspection.

The case that still loses: the = form. flag_set() matches argv tokens exactly:

auto on_argv = [&](const std::string& f) { return std::find(argv.begin(), argv.end(), f) != argv.end(); };

so a user (or Lemonade) who writes --ubatch-size=512 — one argv token — is not seen, and the recipe still appends -ub 2048 over it. That is the same failure this PR is fixing, one spelling further out, and the new test has no arm for it (it covers LLAMA_ARG_UBATCH=1024 and, presumably, the bare -ub).

I could not confirm against the pin from here — third_party/llama.cpp is not checked out in my worktree (arg.cpp absent, which is what submodule_pin_check exists for) — so please check one thing: whether the pinned common/arg.cpp accepts --ubatch-size=512. Recent lines normalise --opt=value before dispatch, in which case this is live; if the pin rejects it, the finding is "the test does not pin the refusal" instead. Either way the predicate wants one line:

auto on_argv = [&](const std::string& f) {
    return std::any_of(argv.begin(), argv.end(),
                       [&](const std::string& a) { return a == f || a.rfind(f + "=", 0) == 0; });
};

Second question, not a defect: the table has two entries, -ub and -b. Are those the only flags any recipe sets that llama-server also reads from the environment? -c/LLAMA_ARG_CTX_SIZE, -t/LLAMA_ARG_THREADS and -ngl/LLAMA_ARG_N_GPU_LAYERS have the same asymmetry, and the engine's own recipes set --threads (there is a tests/recipes_route.sh arm for it). If the answer is "the two #572 named", a half-line above the table saying so would keep the next person from re-deriving it; if it is "all of them", the table is one row short of the bug.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Follow-up on my own second question — I checked, and the answer is "the two are enough today".

The engine's own config/recipes.json sets exactly three flags across every recipe: -ub, --spec-draft-n-max, --spec-draft-p-min. Of those only -ub has a LLAMA_ARG_* spelling that a user can set independently, so the table covers the shipped set — and -b is there for the same reason one step ahead. My --threads remark came from the test fixture (mine.json), not from a shipped recipe, so it does not argue for a third row.

That makes the --ubatch-size=512 finding the only one that matters, and a half-line above the table ("these are the flags our recipes set that llama-server also reads from the environment; add a row when that changes") would close the loop.

…--ubatch-size) (#572)

Review of #2292 at e381697: llama-server's parser turns '_' into '-' in a
"--" option before the lookup (common/arg.cpp, pin e6c80014e), so
--ubatch_size 512 is a valid micro-batch, and flag_set's verbatim compare
let a recipe add -ub over it. flag_set now compares both sides in the
parsed form. A "--opt=value" token is rejected by that parser, so it needs
no case (checked at the pin).

tests/recipes_route.sh: a recipe file whose first recipe sets
--ubatch_size 512 and second sets -ub 999 keeps 512 and adds no -ub.
docs/recipes.md: one sentence; recipes.cpp: which flags the table covers.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Answering the review at e381697 (CHANGES, --ubatch_size) and the =-form question, at f7c8681:

  • --ubatch_size: fixed as suggested. flag_set now compares both sides as llama-server parses them. as_parsed() turns _ into - in a -- token, the same rule as common/arg.cpp at the current pin e6c80014e (lines 745-748 and 1152-1155). The normalising is a rule, so the table needed no underscore rows.
  • --ubatch-size=512: no case needed. At e6c80014e the parser looks the whole token up exactly and throws invalid argument for an = form (arg.cpp:749-751), so llama-server would refuse that command line before any recipe mattered. Checked at the pin, not assumed.
  • New test case: a --recipes file whose first recipe sets --ubatch_size 512 and second sets -ub 999. Expected: 512 is kept and no -ub is added.
    • With the fix, tests/recipes_route.sh passes: ok nor over --ubatch_size, which llama-server's parser reads as --ubatch-size.
    • With app/recipes.cpp reverted to e381697 and rebuilt, it fails exactly that check (FAIL nor over --ubatch_size ...). With the fix restored, it passes.
  • The note above the table: added, as asked. These are the flags our recipes set that llama-server also reads from the environment; add a row when that changes.
  • Docs and checks: docs/recipes.md has one sentence on the comparison rule. The route ctests pass 13/13, and tools/copyright.py --check gives rc 0.
  • Also seen, not in this PR: the parser at e6c80014e now has a duplicate-argument check (seen_args, arg.cpp:752). A launch where two paths add -ub would be refused there. Serve's role launch appends -b/-ub after launch_for has applied recipes. No shipped recipe matches an embedding or rerank model today, so nothing hits it now.

ready for review at f7c8681

@github-actions

Copy link
Copy Markdown

Persistent review updated to latest commit f7c8681

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY at f7c8681

The parser claim is verified against llama-server's own source, and the suite passes with a rebuilt 1bit -- 25 arms.

// common/arg.cpp:745, in the option table build:
std::string arg = argv[i];
if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) {
    std::replace(arg.begin(), arg.end(), '_', '-');          // so --ubatch_size IS --ubatch-size
}
if (arg_to_options.find(arg) == arg_to_options.end()) {
    throw std::invalid_argument("error: invalid argument: " + arg);   // and --opt=value has no entry

So both of this commit's premises are true at the pin: an underscore in a -- option is rewritten before the lookup, and a --opt=value token is looked up whole, which is why it needs no case. as_parsed() mirrors the
rewrite and only for --, which is exactly the extent of it.

And the arms pass, with the caveat that made me almost report the opposite: with my worktree's stale 1bit binary (built 12:52, before this head) the new arm FAILs --
FAIL nor over --ubatch_size, which llama-server's parser reads as --ubatch-size. Rebuilding the target at this head (under ~/lb/thermal-run.sh, since it is CPU-heavy) gives:

rc=0   25 passing arms   PASS

So a reviewer who runs tests/recipes_route.sh against an old 1bit will see this arm fail and conclude the PR is broken. It is worth one line in the header, because the arm's whole subject is how the flag is spelled, and the test cannot tell "the fix is missing" from
"your binary is older than the fix". The other 24 arms have the same property but this one is the first that depends on the new code path.

What else I checked and it holds: the recipe-vs-environment precedence is unchanged in shape (--ubatch_size 512 from a recipe wins over LLAMA_ARG_UBATCH, and no -ub token is emitted, so serve sees one value rather than two); docs/recipes.md's new sentence
says the comparison happens the way the parser reads it, which is the mechanism rather than the symptom; and the earlier commit's three-spelling coverage (-ub, --ubatch-size, LLAMA_ARG_UBATCH) is still there.

Not run: llama-server itself (the arms drive 1bit's recipe routing, which is where the value is decided).

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT at f7c8681: READY — withdraws my CHANGES of 2026-11-12T17:02:45Z

VERDICT: READY at f7c8681 - withdraws my CHANGES of 2026-11-12T17:02:45Z

Fixed as asked, with the arm that would have caught it, and — the part I did not ask for — the comment records why the = form needs no case.

// --ubatch_size is --ubatch-size; compare in that form. A "--opt=value" token is rejected there, so it needs no case
if (a.rfind("--", 0) == 0) std::replace(a.begin(), a.end(), '_', '-');
  • The normalisation is exactly the one-line fix: normalise the argv copy the way the parser does, rather than adding --ubatch_size/--batch_size twin rows to the table — so the rule stays a rule instead of a per-flag exception.
  • And the second clause is the half I verified and did not expect to see written down: "a --opt=value token is rejected there, so it needs no case". That is the finding from my review — the parser's lookup is an exact match on the whole element, so --ubatch-size=512 is an invalid argument rather than a spelling flag_set must handle. Leaving that unstated would have invited the next reader to "fix" a gap that does not exist; naming it closes the question in the file.

The arm is the shape I asked for, and it asserts both halves at once:

recipe args ["--ubatch_size", "512"]  ->  after --ubatch_size = 512   (the user's value survives)
                                          after -ub         = None    (the recipe's flag was not added)

So the flag is seen as set under the underscore spelling, which is the whole defect, and it also proves the recipe's own -ub did not land on top — either half failing would have caught the original bug.

Everything from my earlier verdict still holds for the unchanged parts: the FlagSpelling { short, long, env } table keeps all three spellings of a flag in one row so they cannot drift, env_has checks both getenv and a passed env vector, and the reason (llama-server applies its environment first and its command line after) is documented at the flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant