Repository navigation
fix(sampling): scope the lang-bias counter arming to the test thread - #2188
Merged
Merged
Conversation
2 tasks done
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
force-pushed
the
fix/issue-2187-lang-bias-counter-leak
branch
from
October 6, 2026 22:38
b9b4d8a to
c0a70f6
Compare
This was referenced Oct 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The five Gemma 3 lookahead tests from #2182 failed in the full single-threaded
-p mlxcel --librun becausesampling_observability_testsarmed the opt-in lang-bias suppression counters withset_var("MLXCEL_LANG_BIAS_COUNTERS", "1"). The gate caches that variable in a process-wideOnceLock, so from then on every decode with a token bias (the victims useignore_eos, which installs one) failedrow_supports_fused_batch_except_bias,lookahead_paramsreturnedNone, 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_capabilitywith--exact --test-threads=1on the test-fast binary) ended atsampling_observability_tests::byte_fragment_suppression_counter_tracks_opt_in_entries. Every test in that file arms the gate throughcounter_lock(). Running a victim alone withMLXCEL_LANG_BIAS_COUNTERS=1reproduces the same failure, which confirms the mechanism.Fix
mlxcel_core::lang_bias_countersmodule with a thread-localscoped_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.config_supports_fused_batch_false_for_token_biasin 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_COUNTERSis now documented indocs/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):byte_fragment_suppression_counter_tracks_opt_in_entriesplus each ofmodel_owned_sequence_primes_the_decode_lookahead_only_with_a_rewindandlookahead_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; newlang_bias_counters::tests::override_is_scoped_nested_and_thread_localpasses;config_supports_fused_batch_false_for_token_biaspasses with and withoutMLXCEL_LANG_BIAS_COUNTERS=1exported.make verify-rocmwithMLXCEL_ROCM_SMOKE_MODEL=models/mlx/Qwen3-0.6B-4biton the final head: OK. Script gates, fmt, clippy-D warningsand 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