Skip to content

fix speculative decode tests - #326

Merged
davidkoski merged 2 commits into
mainfrom
speculative-fix
Jun 15, 2026
Merged

davidkoski merged 2 commits into
mainfrom
speculative-fix

Conversation

@davidkoski

Copy link
Copy Markdown
Member

Proposed changes

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

- fix #315
- run models in float16 (more typical) and avoid the float32/tf32 mismatch
@davidkoski
davidkoski requested a review from angeloskath June 3, 2026 15:48
@davidkoski

Copy link
Copy Markdown
Member Author

@petrukha-ivan FYI -- this works as expected

@angeloskath angeloskath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@davidkoski
davidkoski merged commit e6a753a into main Jun 15, 2026
2 checks passed
@davidkoski
davidkoski deleted the speculative-fix branch July 10, 2026 17:16
dkgv pushed a commit to dkgv/mlx-swift-lm that referenced this pull request Jul 30, 2026
* fix speculative decode tests

- fix ml-explore#315
- run models in float16 (more typical) and avoid the float32/tf32 mismatch
davidkoski pushed a commit that referenced this pull request Jul 31, 2026
…485)

On devices with a NAX these two tests fail (#357). Both build a synthetic
model from config, leaving the MLXNN weights at their random float32
defaults, then assert that chunked and single-pass prefill agree to a
tolerance calibrated for exact float32. The two paths are mathematically
equivalent, but TF32 can select different kernels and reduction orders by
shape, so the logits drift past that tolerance. The chunking logic is
correct; the oracle is not.

- Cast both synthetic models to f16, as merged #326 did for
  SpeculativeDecodingTests. Real models never run float32.
- Pin the initializer weights with withRandomState. The tests in these
  suites run concurrently, so MLXRandom.seed would not work here: it
  mutates process-global state that the other tests draw from.
- In Gemma4ChunkedPrefillTests, compare max absolute difference rather
  than allClose, since these logits pass through zero where relative
  error is meaningless, and widen the bound to 5e-2.

Gemma4UnifiedTests keeps its 1e-4 bound: with the model in f16 that test
is bitwise equal, so only the cast was needed.

unbatchedTokensAccepted moves to the same max-abs-diff form. Its old
rtol 1e-4 / atol 1e-5 was finer than one f16 ulp (~2e-3 at these
magnitudes) once the shared factory started returning an f16 model.

Exact cache-offset assertions are unchanged throughout; those catch the
chunk-boundary bugs these tests exist for and are not precision
sensitive.

With the weights pinned the drift is deterministic: 6.2e-3, 8.4e-3,
6.4e-3, 6.2e-3 for chunk sizes 16/8/5/3, identical across runs and
unchanged with MLX_ENABLE_TF32=0. 5e-2 leaves ~6x margin, while an
injected cache/mask defect moves these logits by 1.0-2.4.
div0-space pushed a commit to LibraxisAI/mlx-swift-lm that referenced this pull request Nov 29, 2026
…l-explore#326)

* style(gptoss): format GPTOSS+Experts.swift for the Lint gate

Format-only: line breaks and indentation, no token changes. The Lint
workflow formats every Swift file that a pull request changes with
.swift-format and fails on any resulting diff. This file is not
formatted on main, and the next commit edits it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(gptoss): keep bf16 activations through the expert SwiGLU clamp

`swiglu` clamped with `MLXArray(limit)` bounds. They are float32 0-d
arrays, so `clip` promoted bf16 expert activations to float32, and the
residual add carried float32 into every later layer: layers 1-23 of a
bf16 gpt-oss-20b checkpoint ran their projections and stored K/V in
float32. Swift scalar bounds take the input dtype through
`ScalarOrArray`, as the Python float bounds of mlx-lm's `swiglu_oai` do.
bf16 checkpoints now stay bf16 and float32 models stay float32. The
bounds came from the upstream port.

The opt-in compiled single-token expert graph wraps the same function,
so both expert routes change. gpt-oss-20b native full-attention K/V
falls from 49,152 to 24,576 bytes per token; consumers that size from
the observed native types (`CBv2NativeKVTypeProbe`) follow.

`GPTOSSActivationDTypeTests` (kernel) builds bf16 and float32 tiny
models with MXFP4 experts and checks the eager (sorted and unsorted)
and compiled expert outputs, every layer's output, the logits, and the
probe's per-layer K/V dtypes. Three comments that described GPT-OSS K/V
as float32 now match, and fork.yaml describes the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(gptoss): price catalog window checkpoints with bf16 K/V

`HistoricalWindowCheckpointTests.catalogWindowReservation` still sized
gpt-oss-20b windows with float32 K/V ("float32 for 23 of its 24
layers") and expected ~7 MB per checkpoint. With scalar clamp bounds
every layer of the bf16 checkpoint stores bf16 K/V, so the catalog row
uses 2-byte elements: 262,144 logical bytes per window layer and
4,462,452 bytes reserved per checkpoint (~4.5 MB; three retained
checkpoints are ~13 MB).

The staged byte budget comment in `CompleteCheckpointCapture.swift`
quotes reserved bytes, as its gemma-4-26b figure does, so gpt-oss-20b
is ~4.5 MB there rather than the 3.15 MB logical window size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
div0-space pushed a commit to LibraxisAI/mlx-swift-lm that referenced this pull request Nov 29, 2026
Brings ml-explore#322 (Flint), ml-explore#324, ml-explore#326-ml-explore#330 (CBv2 paged/MTP/SDPA, GPT-OSS), ml-explore#325 and the
Nemotron Super fused MTP verify window: SDK main up to the commit d-inference
master pins (889fe3c). Main's later ml-explore#331/ml-explore#332 are left out until the provider
adopts ml-explore#332's streamed tool-call type. Package.swift pins the cluster
mlx-swift 9fe34405 instead of main's 67dc7528; that was the only conflict.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

[BUG] speculative decode test sometimes fails (SpeculativeDecodingTests)

2 participants