Skip to content

fix(sampling): scope the lang-bias counter arming to the test thread - #2188

Merged
inureyes merged 3 commits into
mainfrom
fix/issue-2187-lang-bias-counter-leak
Oct 6, 2026
Merged

inureyes merged 3 commits into
mainfrom
fix/issue-2187-lang-bias-counter-leak

Conversation

@inureyes

@inureyes inureyes commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Summary

The five Gemma 3 lookahead tests from #2182 failed in the full single-threaded -p mlxcel --lib run because sampling_observability_tests armed the opt-in lang-bias suppression counters with set_var("MLXCEL_LANG_BIAS_COUNTERS", "1"). The gate caches that variable in a process-wide OnceLock, so from then on every decode with a token bias (the victims use ignore_eos, which installs one) failed row_supports_fused_batch_except_bias, lookahead_params returned None, and the lookahead never primed.

How it was found

A binary search over the 5236 tests that sort before the victims (each step ran a half plus lookahead_is_gated_on_the_rewind_capability with --exact --test-threads=1 on the test-fast binary) ended at sampling_observability_tests::byte_fragment_suppression_counter_tracks_opt_in_entries. Every test in that file arms the gate through counter_lock(). Running a victim alone with MLXCEL_LANG_BIAS_COUNTERS=1 reproduces the same failure, which confirms the mechanism.

Fix

  • The gate moves to a new mlxcel_core::lang_bias_counters module with a thread-local scoped_override(enabled) RAII guard. A live guard wins over the cached environment value on its own thread only, guards nest, and drop restores the previous setting.
  • The observability tests hold that guard (together with their existing counter lock) for the whole test instead of mutating the environment. Their sampling calls run on the test thread, so the override covers them; with the variable unset they would fail if the override did not take effect.
  • config_supports_fused_batch_false_for_token_bias in mlxcel-core pins the counters off, so an exported variable no longer flips it, and now also asserts that a biased row leaves the fused path while the counters are on.
  • MLXCEL_LANG_BIAS_COUNTERS is now documented in docs/environment-variables.md, including that it takes biased requests off the lookahead pipeline.

Production impact: none. Nothing outside the tests changes the variable after startup, so a server that serves one family and then another sees the same setting throughout, and the override is never set in production code. The leak is backend-independent (pure Rust process state), so a single-threaded Metal or CUDA lib run, or a parallel run where an observability test happened to arm the gate first, could hit it too.

Verification

On gfx1151 (--features rocm, test-fast profile, --test-threads=1):

  • Minimal pair (byte_fragment_suppression_counter_tracks_opt_in_entries plus each of model_owned_sequence_primes_the_decode_lookahead_only_with_a_rewind and lookahead_is_gated_on_the_rewind_capability): 1 failed before, 2 passed after.
  • cargo test -p mlxcel --lib --profile test-fast --features rocm --no-fail-fast -- --test-threads=1: 8849 passed, 0 failed, 156 ignored (before: 8844 passed, 5 failed).
  • sampling_observability_tests: 6 passed; new lang_bias_counters::tests::override_is_scoped_nested_and_thread_local passes; config_supports_fused_batch_false_for_token_bias passes with and without MLXCEL_LANG_BIAS_COUNTERS=1 exported.
  • make verify-rocm with MLXCEL_ROCM_SMOKE_MODEL=models/mlx/Qwen3-0.6B-4bit on the final head: OK. Script gates, fmt, clippy -D warnings and the smoke (32 tokens) pass; the workspace test run reports 11987 passed, 0 failed, 382 ignored across 152 test binaries (-p mlxcel --lib: 8849 passed, 0 failed, 156 ignored).

Not verified: Metal and CUDA (not available on this host). The change is backend-independent Rust; no Metal or CUDA path is touched.

Closes #2187

@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:inference Generation, sampling, decoding (incl. speculative, DRY) status:done Completed and removed status:review Under review labels Oct 6, 2026
The B9 observability tests armed the opt-in suppression counters with `set_var("MLXCEL_LANG_BIAS_COUNTERS", "1")`. The gate caches the environment in a process-wide `OnceLock`, so once any of those tests ran, every later decode with a token bias in the same test binary (`ignore_eos` installs one) left the batched fused sampler and with it the decode lookahead. In the single-threaded ROCm gate the observability tests sort before the scheduler tests, which made the five Gemma 3 lookahead tests from #2182 fail. A binary search over the 5236 preceding tests isolated `sampling_observability_tests::byte_fragment_suppression_counter_tracks_opt_in_entries`; any test in that file arms the gate.

The gate moves to `mlxcel_core::lang_bias_counters`, which adds a thread-local `scoped_override` RAII guard that wins over the cached environment value on the calling thread only and restores the previous setting on drop. The observability tests hold that guard for their whole body instead of mutating the environment. Production is unchanged: nothing outside tests changes the variable after startup. `MLXCEL_LANG_BIAS_COUNTERS` is now documented in docs/environment-variables.md.

Validated on gfx1151: the minimal pair fails before and passes after, and `cargo test -p mlxcel --lib --profile test-fast --features rocm -- --test-threads=1` reports 8849 passed, 0 failed.

Closes #2187
Review follow-ups for #2187. `config_supports_fused_batch_false_for_token_bias` now pins the lang-bias counters off with `scoped_override(false)`, so an exported `MLXCEL_LANG_BIAS_COUNTERS` no longer changes its answer, and asserts that a biased row leaves the fused path while the counters are on. The `scoped_override` docs state that guards must drop in reverse creation order, and the observability test header names the real helpers and the byte-fragment tests.

Refs #2187
@inureyes
inureyes force-pushed the fix/issue-2187-lang-bias-counter-leak branch from b9b4d8a to c0a70f6 Compare October 6, 2026 22:38
@inureyes
inureyes merged commit fc21784 into main Oct 6, 2026
26 checks passed
@inureyes
inureyes deleted the fix/issue-2187-lang-bias-counter-leak branch October 6, 2026 23:07
@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

area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:high High priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test(scheduler): Gemma 3 lookahead tests fail in the full mlxcel lib run on ROCm

1 participant