Skip to content

test(cli): forbid clap env bindings on overrides_with flag pairs - #2622

Merged
inureyes merged 3 commits into
mainfrom
test/issue-2618-clap-env-override-guard
Oct 26, 2026
Merged

inureyes merged 3 commits into
mainfrom
test/issue-2618-clap-env-override-guard

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Follow-up to #2609 / #2612, both items of #2618 in one PR.

1. Guard against env + overrides_with

clap 4.6.7 counts an env-sourced value as present and treats overrides as direct conflicts, so an env binding on either half of a --x / --no-x pair makes VAR=... --other-half a parse error. Nothing stopped a new arg from reintroducing the binding #2612 removed.

  • mlxcel::cli::env_bound_override_args (src/cli/mod.rs) walks a Command and every subcommand and returns each arg whose get_env() is Some and that overrides, or is overridden by, another arg in the same command. clap has no public override getter, so it reads the overrides: [...] field from the Arg Debug output and panics when that field is missing or unterminated, so a clap format change fails loudly instead of turning the guard into a no-op.
  • Self-tests in cli::env_override_guard_tests build synthetic env + overrides_with commands and check env on the overriding half, on the overridden half, no false positives (pair without env, env without pair), and subcommand walking.
  • no_arg_combines_clap_env_with_overrides_with in src/main_tests.rs (Cli::command() of mlxcel) and in the tests module of src/bin/mlx_server.rs assert an empty result. No allowlist.

2. Autoload invalid values

env_fallback_models_autoload now goes through apply_boolish_pair_env and returns anyhow::Result<()>. LLAMA_ARG_MODELS_AUTOLOAD=maybe is a startup error with the same message as the other five pairs (LLAMA_ARG_MODELS_AUTOLOAD has an invalid boolean value "maybe"), and the y/t/n/f spellings are accepted. Both startup call sites (build_startup_input in mlxcel-server, run_serve_async in mlxcel serve) propagate it with ?. The error formatting is shared with apply_bool_pair_env_bindings through one helper.

Guard demonstration

With env = "LLAMA_ARG_MODELS_AUTOLOAD" temporarily re-added to mlxcel serve --models-autoload (src/main.rs) and env = "LLAMA_ARG_CACHE_PROMPT" re-added to --cache-prompt (src/cli/cache_args.rs), both guard tests fail and name the offenders:

mlxcel serve models_autoload (env LLAMA_ARG_MODELS_AUTOLOAD) <-> _no_models_autoload
mlxcel serve cache_prompt (env LLAMA_ARG_CACHE_PROMPT) <-> no_cache_prompt
mlxcel-server cache_prompt (env LLAMA_ARG_CACHE_PROMPT) <-> no_cache_prompt

With the bindings removed again (this PR), both pass.

Test plan

  • cargo test --profile test-fast --lib cli:: (291 passed, including the 4 guard self-tests)
  • cargo test --profile test-fast --lib server::cli_input (155 passed; models_autoload_env_rejects_an_unparseable_value, models_autoload_env_accepts_boolish_spellings)
  • cargo test --profile test-fast --bin mlxcel --bin mlxcel-server (316 + 39 passed)
  • cargo test --profile test-fast --test cli_help_consistency (27 passed)
  • cargo clippy --profile test-fast --bin mlxcel --bin mlxcel-server --lib --tests -- -D warnings, cargo fmt --all -- --check
  • make verify-llama-compat, make verify-env-var-docs
  • Manual: LLAMA_ARG_MODELS_AUTOLOAD=maybe mlxcel-server --models-dir <dir> and mlxcel serve --models-dir <dir> both exit with Error: LLAMA_ARG_MODELS_AUTOLOAD has an invalid boolean value "maybe"
  • Full make verify not run in this unit (CPU-only worktree on a shared host); left to the merge gate.

Closes #2618

@inureyes inureyes added type:test Test related changes priority:low Low priority status:review Under review status:done Completed and removed status:review Under review labels Oct 26, 2026
@inureyes
inureyes force-pushed the test/issue-2618-clap-env-override-guard branch from 6c022b9 to d34249a Compare October 26, 2026 09:15
inureyes added a commit that referenced this pull request Oct 26, 2026
clap 4.6.7 counts an env-sourced value as present and registers every overrides_with partner as a direct conflict, so an env binding on either half of a --x / --no-x pair turns VAR=... plus the other half into a parse error (#2609). #2612 removed env from six pairs; nothing stopped a new arg from reintroducing it.

- Add mlxcel::cli::env_bound_override_args, which walks a clap Command and its subcommands and lists every arg that binds an env variable while it overrides, or is overridden by, another arg. clap has no public override getter, so the helper reads the overrides field from the Arg Debug output and panics if the field disappears. A self-test on synthetic env + overrides_with args pins the parse.
- Assert an empty result for Cli::command() in src/main_tests.rs and in the mlxcel-server tests module.
- Route LLAMA_ARG_MODELS_AUTOLOAD through apply_boolish_pair_env. An invalid value is now a startup error with the same message as the other five pairs (b10621 throws on invalid pair values), and the y/t/n/f spellings the old parser rejected are accepted. Both startup call sites (mlxcel-server and mlxcel serve) return the error.

Closes #2618
clap 4.6.7 prints `help` and `long_help` before `settings` and `overrides` in an Arg's Debug output, so scanning from the first `overrides: [` substring could read help text that happened to contain it as the override list, and the guard would miss a real env + overrides_with pair without panicking. The scan now starts after `, settings: ArgFlags(` and looks for `, overrides: [`. The new self-test puts that lookalike string in help text; it fails with the previous scan and passes now. The technical report's gap entry is updated to match.

Refs #2618
@inureyes
inureyes force-pushed the test/issue-2618-clap-env-override-guard branch from 1210624 to b06f867 Compare October 26, 2026 09:26
@inureyes

Copy link
Copy Markdown
Member Author

Pre-merge follow-up from the orchestrator review:

  • The override scan read the first overrides: [ in the Arg's Debug output, but clap 4.6.7 prints help and long_help before it, so help text containing that string would have been misread without a panic. The scan is now anchored after , settings: ArgFlags(, and help_text_that_looks_like_the_overrides_field_is_ignored covers it (it fails with the previous scan). Guard self-tests 5/5, both binary guards, cargo clippy --profile test-fast --lib --tests -- -D warnings and cargo fmt --check pass.
  • Behavior note for release notes: LLAMA_ARG_MODELS_AUTOLOAD values are no longer trimmed, so " true" or "true\n" is now a startup error, the same as the other five toggle variables and clap's boolish parser.
  • Rebased on main; verify-llama-compat, verify-env-var-docs, verify-kernel-port-dispatch and verify-rocm-overlay pass. The full make verify-rocm-held gate runs on main after the wave.

@inureyes
inureyes marked this pull request as ready for review October 26, 2026 09:26
@inureyes
inureyes merged commit e8eb431 into main Oct 26, 2026
13 checks passed
@inureyes
inureyes deleted the test/issue-2618-clap-env-override-guard branch October 26, 2026 09:27
inureyes added a commit that referenced this pull request Oct 26, 2026
## Summary

Widens the remaining 32-bit offsets in the ROCm gather QMV paths to `int64_t` (follow-up to ml-explore/mlx#4738) and settles the suspected grid-z launch limit: **z cap ruled out** on gfx1151, so launch geometry is unchanged.

## Grid-z result

The decisive test (unsorted `gather_qmm`, bf16, affine 4-bit, group 64, E=128, M=1, N=K=64, B=65,537, every element against a host reference) passes with the launch geometry unchanged, as does the same shape through the bf16 8-bit warp-shared, bf16 mxfp4 and f32 per-row kernels. The first pre-fix run of the bf16 affine cases quantized the weights in f32, so `gather_qmm` promoted them to the f32 per-row kernel; review fixed the weights to quantize in the activation dtype and each case now asserts the output dtype, and the corrected bf16 cases pass. The widening touches none of the bf16 kernels those cases reach, so they exercise the same batch-in-z launch the code had before. A standalone `hipLaunchKernel` probe (ROCm 7.15, HIP runtime 71526333) ran grids of 65,537, 262,144 and 16,777,216 blocks in z and 65,537 and 262,144 in y, every block executing, although `hipDeviceProp_t::maxGridSize` and `rocminfo` report 65535 for y and z. The limit that applies is 2^32 - 1 threads per dimension (LOCAL_FIXES item 11). The B = 65,537 tests stay as a regression guard.

## Changes

- `gather_qmv_kernel` (`qmm.hip`), `gather_qmv_tiled_kernel` and `gather_qmv_tiled_6bit_kernel` (`qmv_tiled_kernel.hip`) compute the output index in `int64_t`; the opt-in 4/8-bit tiled kernel also widens `rhs_idx * N * w_stride` and `lhs_idx * M * K`.
- `gemv_gather_impl` (`gemms/gemv.hip`) offsets its output by `int64_t(indices_idx) * rows`.
- The dead `quantized/qmv_kernel.hip` is dropped, and the stale comment in `qdequant.hpp` now names its real user. README backend count is 110.
- `LOCAL_FIXES.md` item 61 (60 was taken by #2623). No core file changed, so `CORE_RESIDUAL.diff` is untouched.
- New `tests/rocm_hip_grid_clamp/gather_qmv.rs` module: the four B = 65,537 cases, plus an `#[ignore]` f32 test (N=65,536, K=64, B=32,769, 2^31 + 2^16 output elements) checking rows 0, 32,767 and 32,768 against the host.

## Test plan

- [x] `rocm_hip_grid_clamp`, all 13 fast tests pass under `rocm_gpu_guard.sh --hold`; after the review fix and the rebase onto main, the `gather` filter (5 fast tests) passes again
- [x] The ignored >2^31 test passes with the fix (5.8 s). Negative control: with `gather_qmv_kernel`'s 32-bit index restored, it fails with `hipErrorIllegalAddress` in `gather_qmv_kernel<float, float, 4, 64, true>` (dispatch grid `[256, 256, 32769]`)
- [x] mlxcel-core lib `gather` (41) and `quantized` (42) filters, and `rocm_gather_qmm_expert_batched` (2), pass
- [x] `make verify-llama-compat verify-env-var-docs verify-kernel-port-dispatch verify-kernel-dtype-keys verify-rocm-overlay` and `rocm_overlay.py drift` pass
- [x] `cargo clippy --profile test-fast --features rocm -p mlxcel -p mlxcel-core --lib --tests -- -D warnings` is clean

The full fast set and the ignored test ran on the pre-rebase base (1cb18cc); the rebase onto main added #2622 and #2623, which do not touch these kernels. The overlay and contract checks were re-run after the rebase. The item 46 record says HIP refuses y grids past 65535 blocks; this runtime did not reproduce that, and item 61 and the test header now say so while leaving the item 46 caps in place.

Follow-ups not in scope: `gemv_batched` and `gemv_batched_inline` in `gemms/gemv.hip` still form `batch_idx * rows` in `int` (not gather paths); no B = 65,537 case covers the bf16 6-bit tiled gather kernel; the tiled gather kernels clamp an out-of-range expert to 0 where the other gather kernels write zeros.

Closes #2620
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:low Low priority status:done Completed type:test Test related changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test(cli): forbid clap env bindings on overrides_with flag pairs, unify invalid-value handling

1 participant