Skip to content

test(losses): de-flake test_cross_entropy_qwen3_vocab (batch 2 → 64) - #79

Merged
Brooooooklyn merged 1 commit into
mainfrom
fix/deflake-cross-entropy-qwen3-vocab-test
Jun 26, 2026
Merged

Brooooooklyn merged 1 commit into
mainfrom
fix/deflake-cross-entropy-qwen3-vocab-test

Conversation

@Brooooooklyn

@Brooooooklyn Brooooooklyn commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Main CI intermittently fails on a single Rust unit test (most recently on the PR #78 merge commit, run 28209793148):

FAILED: nn::losses_test::tests::test_cross_entropy_qwen3_vocab
        crates/mlx-core/src/nn/losses_test.rs:630
        "Loss should be > 10.0, got 9.881153"
        (1868 passed, 1 failed)

It is unrelated to whatever PR happens to be merging — the test was last touched in #12 and only convert.rs changed in #78. It is a pre-existing flaky test.

Root cause

The test builds random logits/targets with no seed and asserts a tight band on the mean-reduced loss:

logits  = random_normal([2, 151936], 0,1, None)   // None is the DTYPE arg, not a seed
targets = randint([2], 0, 151936)
loss    = mean_over_batch( logsumexp(logits) − logits[target] )   // ≈ log(151936) ≈ 11.93
assert!(10.0 < loss < 15.0)

With batch_size = 2, the loss is ≈ 12.43 − N(0, 0.71) — a high random target logit occasionally drags the mean below 10.0 (the observed 9.88 is a ~3.5σ draw).

Fix

Bump batch_size 2 → 64. The mean-reduction then concentrates the loss at log(V), shrinking the spread from ±0.71 to ±0.13. Vocab is unchanged, so the large-vocab chunking path is still exercised.

Verification (2,000,000-draw simulation of the exact loss model)

batch loss min P(loss<10) P(loss>15)
2 12.43 ± 0.71 9.01 0.030% 0.015%
64 12.43 ± 0.13 11.82 0 0

At batch=64 the loss stays in [11.8, 13.0] across 2M draws — ~15σ from the 10.0 bound. (logsumexp constant measured = 12.4313, matching ln(V)+0.5.)

🤖 Generated with Claude Code


Note

Low Risk
Test-only change with no production or loss-implementation edits; slightly more work per test run from a larger batch.

Overview
Stabilizes test_cross_entropy_qwen3_vocab in losses_test.rs by raising batch_size from 2 to 64 while keeping the Qwen3-sized vocab (151936) and the same (10.0, 15.0) loss band.

The test still uses unseeded random logits/targets; with a tiny batch, a lucky target logit could drag the mean cross-entropy below 10.0 and fail CI. A larger batch tightens the mean-reduced loss around log(vocab) so the assertion is reliable without changing what the test exercises (large-vocab / chunked cross-entropy path).

Reviewed by Cursor Bugbot for commit 0225f73. Bugbot is set up for automated code reviews on this repo. Configure here.

random_normal/randint are unseeded, so the mean-reduced cross-entropy
loss varied run-to-run. At batch_size=2 the spread around log(V)≈12.43
is ±0.71, so a high random target logit could push the batch mean below
the 10.0 lower bound (or above 15.0) — surfaced on CI as
"Loss should be > 10.0, got 9.881153". batch_size=64 shrinks the spread
to ±0.13; a 2M-draw simulation keeps the loss in [11.8, 13.0] and never
leaves the (10, 15) band. Vocab unchanged, so the large-vocab chunking
path is still exercised.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3fa1310f-6c89-4298-887e-2e8826ca8b1b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/deflake-cross-entropy-qwen3-vocab-test

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Brooooooklyn
Brooooooklyn merged commit ed672d1 into main Jun 26, 2026
7 checks passed
@Brooooooklyn
Brooooooklyn deleted the fix/deflake-cross-entropy-qwen3-vocab-test branch June 26, 2026 01:55
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