Skip to content

fix(serve): refuse --lean on --tree flex before default_mode resolves the model - #30

Merged
rkinas merged 3 commits into
rkinas:mainfrom
Butterski:fix/lean-flex-refused-before-default-mode
Oct 21, 2026
Merged

rkinas merged 3 commits into
rkinas:mainfrom
Butterski:fix/lean-flex-refused-before-default-mode

Conversation

@Butterski

@Butterski Butterski commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Problem

tests/test_lean.py::test_flex_with_lean_on_refused_before_loading fails on any CUDA GPU without FlashAttention 3,
also on main (found on an RTX 4060 Ti, see #29):

basal/server.py: in main
    a.mode = a.mode or default_mode(a.model, a.revision)
basal/server.py: in default_mode
    if model is not None and attention_available("fa3") is not None and _gemma4(model, revision):
basal/server.py: in _gemma4
    return is_gemma4_dir(resolve(model, revision, metadata_only=True))
E   Failed: must reject before loading a model

main() chooses the default mode before anything else, and on CUDA without FA3 default_mode() resolves the model
(to see whether it is Gemma 4). The --lean on / --tree flex refusal comes later, in Server.__init__. So the
refusal needs the model's metadata first: offline, basal-serve --lean on --tree flex ends in a download error
instead of the refusal. On Hopper with FA3 (and on CPU) default_mode() does not resolve, which is why it passes
there.

Change

  • refuse_lean_flex(lean, tree): the existing check and message, shared by main() (now before default_mode()),
    basal-calibrate with the engine in process (before its default_mode()), ThinkServer.__init__ (before it
    resolves the model and starts its sidecar) and Server.__init__ (unchanged position, for callers that build a
    Server directly). Both values are explicit (lean_tree never resolves auto to on), so checking the raw
    arguments refuses exactly the same cases.
  • The test runs the three entry points (basal-serve, basal-calibrate, a ThinkServer built directly) with a CUDA GPU
    without FA3 simulated (torch.cuda.is_available and attention_available patched) and resolve failing, so it
    fails without the fix on any machine, CPU included.

Checks

  • Simulated GPU without FA3, without the fix: the serve case fails as on the RTX 4060 Ti, and with only the server
    part fixed the calibrate and think cases still fail; with the whole fix all three pass.
  • basal-serve --model Remek/never-downloaded-repo --lean on --tree flex: exits with the refusal (no resolve).
  • tests/ (without test_gguf.py): 617 passed, 73 skipped (CPU).

Summary by Sourcery

Reject incompatible lean and flex tree settings before selecting a mode or resolving the model.

Bug Fixes:

  • Reject --lean on --tree flex before model resolution across serve, calibrate, and think entry points, avoiding unintended downloads or model-loading failures.

Enhancements:

  • Centralize the lean/flex incompatibility check while retaining validation for callers that construct servers directly.

Documentation:

  • Document that --lean on --tree flex is refused before resolving the model, including on CUDA systems without FlashAttention 3.

Tests:

  • Expand coverage across serve, calibrate, and think paths, including simulated CUDA without FlashAttention 3 and assertions that model resolution is not attempted.

… the model

On a CUDA GPU without FlashAttention 3, main() chose the default mode
first, and default_mode() resolves the model to see whether it is Gemma 4;
the --lean on / --tree flex refusal came after it, in Server.__init__. So
the refusal needed the model metadata (offline: a download error instead)
and tests/test_lean.py::test_flex_with_lean_on_refused_before_loading
failed on such GPUs (found on an RTX 4060 Ti; it also fails on main
there). Both values are explicit, so the check runs before default_mode
now (refuse_lean_flex, shared with Server). The test also simulates a GPU
without FA3, so it fails without the fix on any machine.
@sourcery-ai

sourcery-ai Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

The PR prevents --lean on --tree flex from triggering default-mode model resolution by validating the raw CLI arguments immediately in main(), while preserving direct Server construction protection. It also strengthens the regression test to reproduce the CUDA-without-FA3 path on any machine and documents the behavior.

Sequence diagram for early lean-flex refusal

sequenceDiagram
    actor User
    participant Main as main()
    participant Refuse as refuse_lean_flex()
    participant Default as default_mode()
    participant Server as Server.__init__()

    User->>Main: basal-serve --lean on --tree flex
    Main->>Refuse: refuse_lean_flex(a.lean, a.tree)
    Refuse-->>Main: SystemExit refusal
    Note over Main,Default: default_mode() and model resolution are not reached

    User->>Server: direct Server construction
    Server->>Refuse: refuse_lean_flex(a.lean, a.tree)
    Refuse-->>Server: SystemExit refusal
Loading

File-Level Changes

Change Details Files
Centralize and move the invalid --lean on --tree flex validation ahead of default-mode selection and model resolution.
  • Add a shared refusal helper that checks the explicit CLI values and preserves the existing error message.
  • Invoke the helper in main() before default_mode() and retain the validation in Server.__init__ for direct callers.
  • Document that refusal now occurs before model resolution.
basal/server.py
docs/WHATS_NEW.md
Make the regression test cover both native and CUDA-without-FlashAttention-3 environments.
  • Patch model resolution to fail the test if loading is attempted.
  • Run the refusal assertion for the current environment and a simulated CUDA/no-FA3 environment.
tests/test_lean.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai 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.

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="basal/server.py" line_range="1432" />
<code_context>

 def main():
     a = parser().parse_args()
+    refuse_lean_flex(a.lean, a.tree)  # before default_mode, which resolves the model on CUDA GPUs without FA3
     a.mode = a.mode or default_mode(a.model, a.revision)
     validate_quant(a.mode, a.quant)
</code_context>
<issue_to_address>
**Model errors mask refusal**

When basal-calibrate runs in-process on CUDA without FA3, with no explicit mode and `--lean on --tree flex`, and the model is unavailable offline, `default_mode()` attempts model resolution before `Server.__init__` reaches `refuse_lean_flex`, so calibration performs an avoidable lookup and reports a model-resolution failure instead of the lean/tree refusal.

Call `refuse_lean_flex` before `default_mode()` in basal-calibrate’s in-process path.
</issue_to_address>

### Comment 2
<location path="basal/server.py" line_range="638" />
<code_context>
-        if a.lean == "on" and a.tree == "flex":
-            raise SystemExit("--tree flex runs with --lean pass2; with --lean on its FlexAttention falls back to an "
-                             "uncompiled kernel during CUDA graph capture and the engine fails to start")
+        refuse_lean_flex(a.lean, a.tree)
         if getattr(a, "batches", "default") not in ("default", "auto") and kind not in ("graph", "exit"):
             raise SystemExit("--batches needs the CUDA graph engine (fast, fast-nocompile, fast-exit, fp8, nvfp4)")
</code_context>
<issue_to_address>
**Direct think startup resolves first**

When a caller directly constructs `ThinkServer` with `--lean on --tree flex` and model resolution fails, `ThinkServer.__init__` resolves the model before calling `Server.__init__`, so a direct construction with `--lean on --tree flex` can fail during resolution before this guard rejects the options; the caller sees a resolution error instead of the refusal.

Call `refuse_lean_flex` at the start of `ThinkServer.__init__`, before resolving the model or starting the sidecar.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread basal/server.py Resolved
Comment thread basal/server.py Resolved
… and ThinkServer too

basal-calibrate (engine in process) chose the default mode before
building the Server, and a ThinkServer resolves the model and starts its
sidecar before Server.__init__: both reached the refusal only after the
model lookup. Both call refuse_lean_flex first now. The test runs the
three entry points (basal-serve, basal-calibrate, ThinkServer) on a
simulated CUDA GPU without FA3; without this change the calibrate and
think cases fail.
@rkinas
rkinas merged commit a305d03 into rkinas:main Oct 21, 2026
1 check passed
rkinas added a commit that referenced this pull request Oct 21, 2026
…ast_kernels, as 2.3.1's default_mode asks (#30)

In 2.3.1 default_mode() resolves the model when gemma_fast_kernels() is None (no FA3 and not sm_120), not through
attention_available directly; the test now patches that, and still fails without the fix (serve case checked).
rkinas added a commit that referenced this pull request Oct 21, 2026
@rkinas

rkinas commented Oct 21, 2026

Copy link
Copy Markdown
Owner

Merged and released in basal 2.3.1 (https://github.com/rkinas/basal/releases/tag/v2.3.1). One follow-up commit: 2.3.1 picks the default mode through gemma_fast_kernels(), so the test now patches that (it still fails without your fix). Thanks!

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.

2 participants