Repository navigation
feat(qwen2): consolidate text decoder into MLXLMCommon namespace (WS-C #4) - #175
Merged
Merged
Conversation
…#4, issue #168) Lifts the Qwen 2 text-decoder layer stack into a public `Qwen2` namespace under `MLXLMCommon/Models/Qwen2.swift`. Four targets consume the shared types: - MLXLLM/Models/Qwen2.swift - MLXVLM/Models/Qwen2VL.swift - MLXVLM/Models/Qwen25VL.swift - MLXVLM/Models/FastVLM.swift The four targets had FOUR independent copies of essentially the same code (Attention, MLP, *DecoderLayer, *Model). Highest-leverage consolidation in the sprint. Adapter pattern (same as Mistral 3): each target's TextConfiguration adds a computed `var layerArgs: Qwen2.LayerArgs` accessor. Configs stay separate (different field-name conventions and Qwen-2.5-specific fields like slidingWindow); shared layer classes take the adapter. Shared: - `Qwen2.LayerArgs` (adapter) - `Qwen2.Attention` (uses applyRotaryPosition; supports linear-scaling RoPE — the dead `mropeSection` field that 2 of the 3 VLMs computed but never used is dropped) - `Qwen2.MLP` - `Qwen2.DecoderLayer` - `Qwen2.ModelInner` (supports `inputEmbedding` for VLM vision-fusion) Per-target wrappers: - LLM `Qwen2Model: LLMModel` (optional Linear lm_head or tied embed) - VLM `Language.LanguageModel` (3 instances; same pattern as LLM but returns `LMOutput`); preemptive prefill-sync `eval()` barriers added to all three VLM `prepare` methods. Net diff: +352 / -704 (~352-line reduction across 5 files). Verification: - swift build clean across MLXLMCommon, MLXLLM, MLXVLM - swift test --configuration release: 203/204 (1 pre-existing benchmark scaffold failure) - LLM smoke `mlx-community/Qwen2.5-7B-Instruct-8bit --method simple` with explicit "What is your name?" prompt: "I am Qwen, a large language model from Alibaba Cloud." (35 tok/s) - VLM smoke deferred (no cached Qwen2VL / Qwen25VL / FastVLM checkpoints locally); build-pass + LLM smoke confirm the shared- type wiring is correct. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
4 tasks done
Closed
3 of 4 tasks
div0-space
pushed a commit
to LibraxisAI/mlx-swift-lm
that referenced
this pull request
Nov 29, 2026
…#175) * 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> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fourth of 8 model-family consolidation PRs from issue #168. Highest-
leverage consolidation in the sprint — 4 targets were carrying
independent copies of essentially the same Qwen 2 layer stack:
MLXLLM/Models/Qwen2.swift— the canonical Qwen 2 LLMMLXVLM/Models/Qwen2VL.swift— Qwen 2 Vision-LanguageMLXVLM/Models/Qwen25VL.swift— Qwen 2.5 Vision-LanguageMLXVLM/Models/FastVLM.swift— Apple's FastVLM (uses Qwen 2 backbone)What's shared
Qwen2.LayerArgs— adapter struct (same pattern as Mistral 3 feat(mistral3): consolidate text decoder into MLXLMCommon namespace (WS-C #3) #174)Qwen2.Attention— usesapplyRotaryPosition; supports linear-scaling RoPE. The deadmropeSectionfield that Qwen2VL / Qwen25VL computed but never read in the language-side attention is dropped.Qwen2.MLPQwen2.DecoderLayerQwen2.ModelInner— supportsinputEmbeddingfor VLM vision-fusionPer-target wrappers
MLXLLM/Models/Qwen2.swift:Qwen2Model: LLMModel, optional Linear lm_head or tied embedding. Configuration retains all original fields + newvar layerArgsaccessor.MLXVLM/Models/Qwen2VL.swift,Qwen25VL.swift,FastVLM.swift:Language.LanguageModelshrinks to wrapQwen2.ModelInner. Each TextConfiguration adds avar layerArgs: Qwen2.LayerArgsadapter. Preemptive prefill-synceval()barriers added to all three VLMpreparemethods (mirroring PR #172).Net diff: +352 / -704 lines (~352-line reduction across 5 files).
Verification
swift buildclean across MLXLMCommon, MLXLLM, MLXVLMswift test --configuration release: 203/204 (the 1 "failure" is the env-drivenInferenceBenchmark.benchmark()scaffold; pre-existing).mlx-community/Qwen2.5-7B-Instruct-8bit --method simple(MLX_BENCH_PROMPT="What is your name?"):"I am Qwen, a large language model from Alibaba Cloud."(35 tok/s)LanguageModelpatterns now). Reviewer should run a Qwen 2 / 2.5 VL / FastVLM smoke before merge if available.