Skip to content

Keep the drafterless retry intact when the arch gate narrows the argv - #8667

Merged
danielhanchen merged 3 commits into
mainfrom
fix/spec-start-index-after-argv-narrowing
Aug 13, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
fix/spec-start-index-after-argv-narrowing

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

_spec_start is a positional index into cmd, captured immediately before the speculative flags are appended. #7670 (merged as 5a3e9fc7a) added five rebinds that remove tokens from the prefix ahead of it, so the stored index now points past the block it is supposed to mark.

Its two consumers slice on that index:

_fb_tail = cmd[_spec_start + len(spec_flags) :]
fallback_cmd = cmd[:_spec_start] + ["--spec-default"] + _fb_tail

With N tokens removed below the index, the drafterless retry keeps spec_flags[:N] and loses N tokens out of the middle of the tail.

Why this could not happen before #7670

The only rebind in that window was cmd = _fa_cmd, and _with_flash_attn_off is length-preserving by design. The file states the invariant twice, at _with_flash_attn_off ("every form is flipped in place (length preserved for downstream slices)") and again at its helper ("rewritten in place so the list length is preserved for downstream slices"). Everything else in the window is cmd.extend, which appends and cannot shift an index below _spec_start. #7670 is the first code to remove tokens from the prefix.

Impact

Two things go wrong, and the second is the reason this is not just a correctness bug.

The retry re-runs the drafter that just failed. spec_flags begins with --spec-type mtp or --model-draft <path>, so a surviving prefix of it means the fallback carries the mode that just failed. llama.cpp accumulates spec types, so appending --spec-default cannot undo it. The code's own comment at the fallback site says exactly this must not happen.

Tokens are cut out of the middle of the tail. In the four-token narrowing, the deleted run is:

['--chat-template-file', 't.jinja', '--api-key', 'SECRET']

--api-key lands in the tail, so the fallback would start llama-server with no key at all.

Observed against main at 5a3e9fc7a, driving the real _without_flags and the verbatim slice arithmetic:

fallback_cmd = [... '--spec-type','mtp','--draft-max','16','--spec-default','--cache-ram','0', ...]
retains the failed spec mode: True
tail tokens silently deleted: ['--chat-template-file','t.jinja','--api-key','SECRET']

Reachability

Manual mode, multi-GPU ROCm host, a mixed-arch gate that narrows, a user ratio --tensor-split 3,1, and an MTP drafter requested. The survivors arm drops 2 tokens, and any later drafter-load failure builds the corrupted argv. The auto path via the arch-crash retry is equally reachable: --split-mode tensor / --tensor-split are emitted outside the manual block, so they reach argv in auto mode too. Note the comment near the retry ("No ratio can be live where this runs") is about the _tensor_split attribute, while _without_tensor_split scans argv, so it does not protect this.

The fix

Re-locate the block rather than trust the index. _subsequence_index searches back from the hint first, since narrowing can only move the block left, then forward as a defensive fallback. A future narrowing site is safe by construction instead of relying on every author remembering to adjust an index.

Adjusting the index at each of the five removal sites would also work, but it puts the burden on the next person to add a sixth site, and this defect is what that burden looks like when it is missed once.

Tests

studio/backend/tests/test_spec_start_after_arch_narrowing.py, six cases: the un-narrowed control, --api-key survival under the four-token narrowing, the failed drafter not being re-run under all three narrowings, no token lost under any narrowing, a block that moved right, and an absent block not raising.

Mutation check, reverting _subsequence_index to the stale index:

3 failed, 3 passed
  test_the_api_key_survives_the_four_token_narrowing
  test_the_failed_drafter_is_not_re_run_after_narrowing
  test_no_token_is_lost_by_any_narrowing

Counts, serial:

  • arch gate suites plus the new file: 317 passed
  • test_mtp_drafter_companion.py: 217 passed

There is no AMD hardware on any runner, so this drives the real helpers over a fabricated argv rather than a live host.

Follows up #7670.

_spec_start is a positional index captured just before the speculative flags
are appended. #7670's arch gate rebinds cmd with tokens REMOVED from the
prefix (--split-mode / --tensor-split), which slides the spec block left and
leaves the index pointing past it.

The fallback then kept spec_flags[:N], re-running the drafter that had just
failed (llama.cpp accumulates spec types, so --spec-default cannot undo it),
and sliced N tokens out of the middle of the tail. With the four-token
narrowing that included --api-key, so the retry launched llama-server with no
key at all.

Re-locate the block instead of trusting the index, searching back from the
hint first since narrowing can only move it left. A future narrowing site is
then safe by construction rather than by remembering to adjust the index.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4ac2d4cf9f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

# narrowed cmd since, which slides the block left and would
# otherwise slice --api-key and friends out of the tail.
_spec_at = _subsequence_index(cmd, spec_flags, _spec_start)
_fb_tail = cmd[_spec_at + len(spec_flags) :]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Update the startup-retry guard for the relocated slice

The existing test_the_startup_retry_drops_the_mtp_the_extras_and_the_env_carry in studio/backend/tests/test_metal_paravirtual_guard.py still searches _load_model_source() for the literal _fb_tail = cmd[_spec_start and fallback_cmd = cmd[:_spec_start]. With this change neither substring exists, so str.index() deterministically raises ValueError before the test reaches its assertions, causing the backend test suite to fail. Update that guard to locate the new _spec_at-based reconstruction.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch, that guard would have failed the suite. Fixed in 498cc64: the slice anchors now read _fb_tail = cmd[_spec_at and fallback_cmd = cmd[:_spec_at], which is the same region as before, so the strip_spec, env.pop and MTP/DSpark assertions all still bind. Confirmed by reverting each of them in turn against the updated anchors: dropping the _SPEC_ENV_VARS pop, flipping strip_spec off, and removing the env = _launch_spec_env kwarg each fail the test, and reverting the call sites back to _spec_start fails it too. tests/test_metal_paravirtual_guard.py is 119 passed; the three arch gate suites plus the new file are 317 passed; tests/test_mtp_drafter_companion.py is 217 passed. It was the only place in the tree still naming _spec_start.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

danielhanchen added a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Aug 13, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 498cc644a9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit f4a7458 into main Aug 13, 2026
47 of 54 checks passed
@danielhanchen
danielhanchen deleted the fix/spec-start-index-after-argv-narrowing branch August 13, 2026 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant