Skip to content

Add coherence integration tests - #235

Merged
davidkoski merged 2 commits into
ml-explore:mainfrom
DePasqualeOrg:coherence-integration-tests
May 7, 2026
Merged

davidkoski merged 2 commits into
ml-explore:mainfrom
DePasqualeOrg:coherence-integration-tests

Conversation

@DePasqualeOrg

@DePasqualeOrg DePasqualeOrg commented Apr 23, 2026 •

Copy link
Copy Markdown
Contributor

I've added an integration test suite that checks for coherent responses from a representative sample of models. The suite can be run whenever wide-ranging changes such as #178 are made to catch regressions such as the one noted in #233.

The test suite uses smaller models (≤9B) so that they can run on machines with 16 GB of RAM.

The new suite already caught a latent bug: LLMTypeRegistry had Jamba registered under the key "jamba_3b", but actual Jamba configs specify model_type: "jamba". I fixed that here as well.

It also surfaced a second issue: loading OLMoE (or any model using GPTNeoXTokenizer) fails because Swift Transformers doesn't register that class. My Swift Tokenizers package already works correctly, because the class is registered there.

There are also two failing tool-calling integration tests that are outside the scope of this PR.

Changes

  • Added new CoherenceIntegrationTests suite
  • Refactored IntegrationTestModels around generic llmContainer(for:) / vlmContainer(for:) accessors and reduced boilerplate code
  • Added LLMRegistry.qwen3_5_2b_4bit (used by the new suite) and qwen3_6_27b_4bit
  • Renamed LLMRegistry.jamba_3b to jamba_3b_4bit and pointed it at the 4-bit variant (1.7 GB vs 6.1 GB bf16)
  • Fixed LLMTypeRegistry Jamba creator key: "jamba_3b" → "jamba"

"nemotron_h": create(NemotronHConfiguration.self, NemotronHModel.init),
"afmoe": create(AfMoEConfiguration.self, AfMoEModel.init),
"jamba_3b": create(JambaConfiguration.self, JambaModel.init),
"jamba": create(JambaConfiguration.self, JambaModel.init),

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.

I wonder if we need both? Perhaps not:

That is from #8 where it was added and it uses jamba as you have.

@davidkoski davidkoski 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.

Changes look good and very useful. Thank you!

@davidkoski
davidkoski merged commit 38fff58 into ml-explore:main May 7, 2026
3 checks passed
NivDvir added a commit to NivDvir/mlx-swift-lm that referenced this pull request May 27, 2026
Per davidkoski's ml-explore#238 standard ("people can always go back to the PR
the lines came from to see the discussion") applied harder: the
two comment blocks I'd kept were both restatements of what the
variable names and call structure already say.

Brings the comment-line ratio on this PR from ~40% to 0%, in line
with the maintainer's own recent PRs (ml-explore#283 2%, ml-explore#235 3%, ml-explore#228 0%).
davidtai added a commit to davidtai/mlx-swift-lm that referenced this pull request Oct 6, 2026
… NanoChat, Granite, OpenELM and Gemma 2 (ml-explore#235)

* style: format DiffusionGemmaVisionOracleTests.swift

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

* style: format DiffusionGemmaFactoryTests.swift

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

* test: run bit-exact reference tests only on reference hardware

Nine DiffusionGemma tests compare float bits with frozen references.
On the hosted macos-26 runner (Apple M1 VM) they differ in the low bits,
with no change in shape, dtype or sign. Add a `.referenceHardware`
Swift Testing trait that runs a test only when MLX_REFERENCE_HARDWARE=1,
and mark the nine tests with it. Elsewhere they report as skipped with
the reason. No assertion, tolerance or reference changes.

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

* test: gate only the bit comparisons of mixed DiffusionGemma tests

Three gated tests also held checks that pass on any hardware. Split them
so only the bit-for-bit comparison is gated:

- prefixIdentityTokensOwnerAndEmptyStateFailClosed() runs its
  fail-closed probes on all hardware. The donor-logits bit comparison
  moves to failClosedProbesLeaveDonorLogitsBitExact().
- nativeFactoryLoadsExactStateAndReleasesItsModel() keeps the load and
  model-release checks on all hardware. The logits bit comparison moves
  to nativeFactoryDenoiseMatchesReferenceBits().
- The two parameterized vision tests run their bf16 case on all
  hardware. The fp32 case moves to a separate gated test.

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

* test: gate only the reference comparisons of three more DiffusionGemma tests

Split the three tests that were still gated whole, so their checks that
pass on any hardware run everywhere:

- chunkedSessionBoundsPrefillAndCanCancelAfterPrefill() keeps the
  prefill bounds, the complete flag, the counters, cancel, the emitted
  checkpoint and the released cache for widths 1, 3 and 7. The KV and
  logits bit comparisons move to chunkedSessionMatchesIndependentNativeReference().
- sharedWeightsConditioningKeepsCommittedCacheState() keeps the fixture
  checks, both cache positions and the unchanged encoder KV after the
  provisional canvas. The reference comparisons stay in
  sharedWeightsConditioningAndCommittedCacheMatchReference().
- committedPrefixRestoreKeepsPositionsAfterDonorRetirement() keeps the
  token count, retained bytes, donor release and both positions. The
  reference comparisons stay in
  committedPrefixRestoreIsExactAfterDonorAppendAndRetirement().

Each pair shares one helper. The fixture reference, case-count and
parameter-count checks of the block and fp32 vision fixtures also run
everywhere now, in blockOracleFixtureIsPinned() and fp32FixtureIsPinned().
CONTRIBUTING.md says which comparisons the gate covers.

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

* style: format HistoricalWindowCheckpointTests.swift

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

* test: size the slot-cap request from what the ledger accepts

slotCapLeavesRoomForServing() sized its request from bytesReserved.
bytesReserved includes the paged pool's physical-floor overhead, which a
request's target KV replaces when it reserves. The overhead depends on
the buffer sizes the MLX allocator returns: a 36,864-byte segment gets a
49,152-byte buffer, or a 65,536-byte buffer reused from the process-wide
cache. When earlier work left 64 KiB buffers in the cache, the capped
arm left more than 5.5 windows of real slack instead of half a window,
and the uncapped arm's reservation fit.

Find the largest request the ledger accepts beside the capped set with
trial reservations, then take half a window off it. The assertions do
not change.

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

* test: add test types and a unit test step without the Metal library

Add the Swift Testing tags unit, kernel, integration and reference in
Tests/MLXLMTests/TestTypeTags.swift, and the UnitTests parent suite
that gives the unit tag to the suites under Tests/MLXLMTests/Unit.

Fork Tests runs the unit tests before it builds the Metal library.
swift test cannot select tests by tag, so the step selects the
UnitTests suite by name. CONTRIBUTING.md describes the four types.

Add two example unit suites for JSONValue and ToolCall.execute.

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

* test: select the UnitTests suite of every test target

Other test targets, such as MLXLMServerTests, can then add unit tests
with their own TestTypeTags.swift and no change to the workflow.

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

* test: apply review fixes to the test type convention

- Delete every cached *.metallib, not only mlx.metallib, before the
  build and before the cache is saved, so the unit step guard sees the
  same set of files.
- The guard prints the found libraries, then one ::error:: line.
- Add scripts/check-unit-test-files.sh: it fails when a Swift file
  under Tests/<Target>/Unit/ has no extension UnitTests. The unit step
  runs it, and scripts/test-run-nested-suite.sh proves it red on a bad
  file.
- CONTRIBUTING.md: folders for each test target, the sentence on files
  without a type tag, and the section Xcode integration tests.
- fork.yaml: unit tests are under Tests/<Target>/Unit/ for each test
  target; describe the new script.

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

* test: add kernel forward-pass tests with tiny random models

Add the KernelTests suite and a shared helper for kernel tests.
SyntheticModel builds a tiny model from a configuration dictionary,
gives it seeded random weights and evaluates them before use. It also
loads a synthetic checkpoint through loadWeights. ForwardPassChecks
checks the logits shape, dtype and finite values, determinism, cache
consistency, batch invariance and causality.

Add forward-pass tests for Laguna, Qwen3-Next and Qwen3-MoE, behavior
tests for the caches, masks and quantized attention in KVCache.swift,
and tests of the RoPE scaling types against reference rotations.

Two tests show defects and use withKnownIssue: ChunkedKVCache.copy()
after maybeTrimFront() and ArraysCache.copy() with an empty slot.

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

* test: add a table-driven model check to the kernel test helper

ModelCase describes one tiny model: how to build it, the checkpoint keys
and shapes it must have, the keys that sanitize(weights:) must drop, and
a conversion to the original checkpoint layout. ModelCaseChecks runs the
standard checks on a table of such models. A check that fails because of
a known production defect runs inside withKnownIssue, with the defect
named.

SyntheticModel.randomize now also handles 0-D parameters and float64
initial values. checkCacheConsistency takes an optional cache for models
whose newCache(parameters:) is not usable.

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

* test: match each known issue to its failing expectation

Every withKnownIssue block took any failure inside it as the known
issue, so a new failure in the block would hide. Each block now passes
a matching predicate on the comment of the expectation that the defect
fails. Another failed expectation, or a thrown error, fails the test.

ModelCase.run matches only the expectations in Check.failingExpectations.
For the loading check, a thrown error of the load is also the known
issue, because a wrong sanitize(weights:) makes the strict update throw.
The loading check runs the shape checks and the wrong-shape rejection
outside the known issue.

The two KVCacheBehaviorTests blocks give each expectation a comment and
match on it.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* test: add kernel forward-pass tests for ten more text models

Add a ModelCase table for SmolLM3, Exaone 4, OLMoE, OLMo 3, PhiMoE,
Phi-3, NanoChat, Granite, OpenELM and Gemma 2. Each model has a case for
each configuration that changes the code path: tied or untied
embeddings, layers without RoPE, sliding-window layers, the RoPE scaling
types, the expert routing and the logit soft caps. The table runs the
shape, determinism, cache, batch, causality and loading checks, and
checks the default LoRA keys.

More tests check the NoPE layer of SmolLM3, the sliding-window caches of
Exaone 4 and OLMo 3, the soft caps of NanoChat and Gemma 2, the logits
scaling of Granite, the Gemma 2 message generator and the OpenELM layer
shapes.

Known issues: the Gemma 2 mask is boolean and is added to the scores, so
the prompt pass is not causal. The full pass of Exaone 4 has no window
mask. OLMo 3 newCache is not the protocol requirement. The OpenELM head
has the wrong input size.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Gajesh Naik <26431906+Gajesh2007@users.noreply.github.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.

2 participants