Skip to content

Open KVCacheSimple and ArraysCache so a track repo can carry the Qwen 3.8 model files - #143

Merged
davidtai merged 1 commit into
feat/qwen38-flash-next-runnerfrom
feat/qwen4exp-model-access
Sep 9, 2026
Merged

davidtai merged 1 commit into
feat/qwen38-flash-next-runnerfrom
feat/qwen4exp-model-access

Conversation

@davidtai

@davidtai davidtai commented Sep 9, 2026

Copy link
Copy Markdown

Stacks on #140 (feat/qwen38-flash-next-runner). Base of this PR is that branch, not main.

What changed

Access levels only, in two files. No behaviour change, no logic touched.

symbol file today now
qwen35MTPTopTwoRows(_:) Libraries/MLXLLM/Models/Qwen35MTPTopTwo.swift:190 internal public
KVCacheSimple (class) Libraries/MLXLMCommon/KVCache.swift:327 public class open class
KVCacheSimple.innerState() KVCache.swift:336 public override func open override func
KVCacheSimple.state KVCache.swift:385 public override var open override var
KVCacheSimple.trim(_:) KVCache.swift:410 public override func open override func
KVCacheSimple.copy() KVCache.swift:458 public override func open override func
ArraysCache (class) KVCache.swift:1173 public class open class
ArraysCache.copy() KVCache.swift:1259 public override func open override func

Seven declarations (KVCacheSimple counts once for the class and four times
for its members). No initializer had to become open: Qwen4ExpAttentionCache
compiles its public override init() against the inherited public override init() unchanged. ArraysCache.extract(_:) was already open.

Why

The Qwen 3.8 Flash-Next model files — Libraries/MLXLLM/Models/Qwen4Exp*.swift
(8 files) and Libraries/MLXLMCommon/Qwen4ExpCaches.swift — are to be compiled
OUTSIDE this fork, as an editable copy carried by a track repository. Exactly
these seven declarations blocked that:

  • Qwen4ExpAttentionCache subclasses KVCacheSimple and overrides
    innerState(), state, trim(_:) and copy().
  • Qwen4ExpLayerCache subclasses ArraysCache and overrides copy().
  • Qwen4ExpCBv2 calls qwen35MTPTopTwoRows.

A class or member that is public but not open can only be subclassed or
overridden inside its own defining module. That is why Qwen4ExpCaches.swift
currently lives in MLXLMCommon rather than beside the model — the file says so
in its own header comment.

Verification

1. The fork still builds.

swift build -c release --scratch-path .build-access \
  --target MLXLMCommon --target MLXLLM --target MLXRunners

→ Build of target: 'MLXRunners' complete! (329.24s). Warnings only, all
pre-existing.

2. The goal is proved: the nine files compile OUTSIDE the fork.

A throwaway SwiftPM package was created outside the fork tree, depending on this
worktree by local path. The nine Qwen4Exp files were copied into it verbatim
(only import MLXLLM / import MLXLMCommon added) and built as a separate
module Qwen4ExpTrack:

swift build -c release   # in the scratch package

→ Build complete! (673.48s), with all nine object files emitted
(Qwen4Exp.swift.o … Qwen4ExpText.swift.o) and Qwen4ExpTrack.swiftmodule.
The package was deleted afterwards and is not committed.

Negative control. The same scratch package was rebuilt with these access
changes reverted (git stash), everything else identical. It failed with
exactly the eight blockers this PR removes and nothing else:

Qwen4ExpCBv2.swift:996:22:  cannot find 'qwen35MTPTopTwoRows' in scope
Qwen4ExpCaches.swift:32:20: cannot inherit from non-open class 'KVCacheSimple' outside of its defining module
Qwen4ExpCaches.swift:54:26: overriding non-open instance method outside of its defining module
Qwen4ExpCaches.swift:64:25: overriding non-open property outside of its defining module
Qwen4ExpCaches.swift:83:26: overriding non-open instance method outside of its defining module
Qwen4ExpCaches.swift:91:26: overriding non-open instance method outside of its defining module
Qwen4ExpCaches.swift:115:20: cannot inherit from non-open class 'ArraysCache' outside of its defining module
Qwen4ExpCaches.swift:126:26: overriding non-open instance method outside of its defining module

That is the proof the seven declarations are necessary AND sufficient.

3. Tests for the touched modules: 19 tests, all pass.

xcodebuild test-without-building -scheme mlx-swift-lm-Package \
  -destination 'platform=macOS' \
  -only-testing:MLXLMTests/CompilableKVCacheTests \
  -only-testing:MLXLMTests/Qwen35MTPTopTwoTests
→ ✔ Test run with 8 tests in 2 suites passed after 0.911 seconds.

xcodebuild test-without-building ... (the 11 KVCacheTests free-function tests)
→ ✔ Test run with 11 tests in 0 suites passed after 0.526 seconds.

Suites: CompilableKVCacheTests (3), Qwen35 MTP hierarchical top-2 (5),
KVCacheTests (11). GPU-gated and model-on-disk suites were skipped.

Two environment obstacles, both pre-existing and unrelated

Recording these because they cost time and will cost the next person time.

  • swift test --scratch-path … --filter KVCache cannot run in this repo.
    It builds, then every MLX-touching case aborts at runtime with
    MLX error: Failed to load the default metallib — SwiftPM does not produce
    or place the Metal library for the pinned mlx-swift. The tests above were
    therefore run the way .github/workflows/pull_request.yml runs them, through
    xcodebuild build-for-testing, which does build the Metal library.

  • xcodebuild build-for-testing -scheme mlx-swift-lm-Package fails on this
    base branch
    , before any test bundle is linked:

    Sources/BenchLoad/main.swift:43:1: error: 'main' attribute cannot be used
                                       in a module that contains top-level code
    

    That file is untouched by this PR; the defect arrived in 4f2971f. It is not
    caught today because only the CodeQL workflow runs on PRs here — the
    mac_build_and_test job did not run on feat: Qwen 3.8 Flash-Next runner stack (CBv2 runners, bench-worker, model port, n-gram LRU) #140. To get a linked test bundle the
    BenchLoad executable target was removed from Package.swift locally and
    temporarily
    ; Package.swift was restored before committing and is not part
    of this diff. Worth a separate fix.

Also note xcodebuild needs -skipPackagePluginValidation -skipMacroValidation
non-interactively, because mlx-swift ships a CudaBuild plugin that otherwise
waits on a trust prompt.

Follow-up, not done here

Qwen4ExpCaches.swift still carries a header comment saying the caches live in
MLXLMCommon because KVCacheSimple and ArraysCache are public, not open.
That reason is now gone. The comment was left alone deliberately — this PR
changes access levels and nothing else — and should be updated by whichever
change actually moves the file.

🤖 Generated with Claude Code

The Qwen 3.8 Flash-Next model files (Libraries/MLXLLM/Models/Qwen4Exp*.swift
and Libraries/MLXLMCommon/Qwen4ExpCaches.swift) must compile OUTSIDE this
fork, as an editable copy in a track repository. Seven declarations blocked
that. This changes their access level only. No behaviour changes.

- Libraries/MLXLLM/Models/Qwen35MTPTopTwo.swift
  qwen35MTPTopTwoRows(_:)          internal -> public
    Qwen4ExpCBv2 calls it for the MTP top-2 reduction.

- Libraries/MLXLMCommon/KVCache.swift
  KVCacheSimple (class)            public -> open
  KVCacheSimple.innerState()       public override -> open override
  KVCacheSimple.state              public override -> open override
  KVCacheSimple.trim(_:)           public override -> open override
  KVCacheSimple.copy()             public override -> open override
  ArraysCache (class)              public -> open
  ArraysCache.copy()               public override -> open override
    Qwen4ExpAttentionCache subclasses KVCacheSimple and overrides those
    four members. Qwen4ExpLayerCache subclasses ArraysCache and overrides
    copy(). ArraysCache.extract(_:) was already open.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@davidtai
davidtai marked this pull request as ready for review September 9, 2026 10:08
@davidtai
davidtai merged commit d087c30 into feat/qwen38-flash-next-runner Sep 9, 2026
davidtai added a commit that referenced this pull request Sep 16, 2026
CBv2MTPRectangularSerializing is public on this branch (PR #143), so main's new
requirement must be public on LayerCacheV2 and PagedLayerCache too. Found by
the first real build of the merge (ai-server).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
javiech1 pushed a commit to javiech1/mlx-swift-lm that referenced this pull request Oct 24, 2026
Layr-Labs#254)

**Fix Qwen35 VLM sanitize for bare `model.*` weight keys (Layr-Labs#143)**

Unified Qwen 3.5 checkpoints ship language-model tensors at bare `model.*` paths instead of `model.language_model.*`, causing a `keyNotFound` failure at load time. Adds the same `else if key.hasPrefix("model.")` fallback already present in the LLM path, prepending `language_model.` to route tensors correctly.

Also adds a regression test covering all four key shapes: bare `model.*`, `model.language_model.*`, and `lm_head.*`.
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