Repository navigation
Allow downstream specialization of the Qwen3.5 GDN/MoE blocks and SwitchGLU - #511
Conversation
davidkoski
left a comment
There was a problem hiding this comment.
The change generally looks good, but there are a few issues:
- types like
SwitchGLUstill have apublicinit -- maybe this should also beopen? - several types are
openbut haveinternalinit: GatedDeltaNet, SparseMoeBlock, DecoderLayer, Model
The lack of init has two effects: you can't override the init (maybe fine) and you also won't be able to instantiate it (probably defeats the purpose of the PR).
|
Looking at this I think the intent is right. |
`Qwen35Language` has no access modifier, so everything nested in it is internal regardless of its own modifier, and `GatedDeltaNet`, `SparseMoeBlock`, `DecoderLayer` and `Model` are additionally `final`. `SwitchGLU` and `SwitchLinear` are `public` rather than `open`. Together this makes the Qwen3.5 language stack impossible to specialize from another module. Serving this model with routed experts streamed from SSD instead of resident needs exactly one substitution — our own expert MLP in place of `SparseMoeBlock`'s — while keeping the rest of the vendored model and its weight keys intact. Without subclassing that becomes a reimplementation: ~445 lines re-deriving the `@ModuleInfo(key:)` bindings, the gather/scatter sort and the quantized-vs-dense dispatch that `SwitchGLU` already gets right, and every upstream fix has to be hand-ported. Widens access only: Qwen35Language -> public GatedDeltaNet, SparseMoeBlock, DecoderLayer, Model -> open SwitchGLU, SwitchLinear -> open SwitchGLU/SwitchLinear/SparseMoeBlock callAsFunction -> open `Module` is already `open`, so nothing changes in mlx-swift. `SparseMoeBlock.callAsFunction` had to widen as a consequence, not a choice: it satisfies a requirement in the public `UnaryLayer` protocol, so an `open` class cannot leave it internal. Made `open` rather than `public` since overriding it is the point. Leaves FusedGateUpSwitchGLU alone and omits the GDN conv-fold port, which is a behaviour change and belongs in its own PR. Full suite: 296 tests, 0 failures.
257b88a to
44b3b47
Compare
|
Rebased for conflicts & adjusted public inits. Note: I thought init required |
davidkoski
left a comment
There was a problem hiding this comment.
Change looks good. Just waiting for CI. Thanks!
…tchGLU (ml-explore#511) * Allow downstream specialization of Qwen3.5 GDN/MoE blocks and SwitchGLU `Qwen35Language` has no access modifier, so everything nested in it is internal regardless of its own modifier, and `GatedDeltaNet`, `SparseMoeBlock`, `DecoderLayer` and `Model` are additionally `final`. `SwitchGLU` and `SwitchLinear` are `public` rather than `open`. Together this makes the Qwen3.5 language stack impossible to specialize from another module. Serving this model with routed experts streamed from SSD instead of resident needs exactly one substitution — our own expert MLP in place of `SparseMoeBlock`'s — while keeping the rest of the vendored model and its weight keys intact. Without subclassing that becomes a reimplementation: ~445 lines re-deriving the `@ModuleInfo(key:)` bindings, the gather/scatter sort and the quantized-vs-dense dispatch that `SwitchGLU` already gets right, and every upstream fix has to be hand-ported. Widens access only: Qwen35Language -> public GatedDeltaNet, SparseMoeBlock, DecoderLayer, Model -> open SwitchGLU, SwitchLinear -> open SwitchGLU/SwitchLinear/SparseMoeBlock callAsFunction -> open `Module` is already `open`, so nothing changes in mlx-swift. `SparseMoeBlock.callAsFunction` had to widen as a consequence, not a choice: it satisfies a requirement in the public `UnaryLayer` protocol, so an `open` class cannot leave it internal. Made `open` rather than `public` since overriding it is the point. Leaves FusedGateUpSwitchGLU alone and omits the GDN conv-fold port, which is a behaviour change and belongs in its own PR. Full suite: 296 tests, 0 failures. * public/open updates --------- Co-authored-by: Fred Jura <digital@afya.ai> Co-authored-by: David Koski <dkoski@apple.com>
What is currently unreachable
The Qwen3.5 language stack in
Libraries/MLXVLM/Models/Qwen35.swiftcannot be specialized from another module:221enum Qwen35Language {441final class GatedDeltaNet: Module {finaland internal574final class SparseMoeBlock: Module, UnaryLayer {finaland internal626final class DecoderLayer: Module {finaland internal681final class Model: Module {finaland internalAnd in
Libraries/MLXLMCommon/SwitchLayers.swift,SwitchGLU(:42) andSwitchLinear(:222) arepublic, notopen— subclassable only inside this module.Why I care
I serve Qwen3.6-35B-A3B on a 24 GB Apple M4 with the routed experts streamed from SSD into a bounded RAM cache rather than held resident. That needs one thing: substitute my own expert-MLP implementation for
SparseMoeBlock's, keeping the rest of the vendored model — and critically its weight keys — untouched.Because none of the above is subclassable, I reimplemented instead. That is ~445 lines of
StreamingSwitchGLUplus aResidentSwitchGLUand aResidentMoeBlock, and the large majority of it re-derives whatSwitchGLUalready does correctly: the three@ModuleInfo(key:)weight bindings, the gather/scatter sort, the quantized-vs-dense dispatch. Every upstream fix toSwitchGLUis a fix I have to notice and hand-port. Being able to subclass would delete most of that and keep me on your code path.The change
Mechanical access widening, no behaviour change:
Qwen35Language→publicGatedDeltaNet,SparseMoeBlock,DecoderLayer,Model→open(droppingfinal)SwitchGLU,SwitchLinear→openSwitchGLU.callAsFunction,SwitchLinear.callAsFunction,SparseMoeBlock.callAsFunction→openModuleis alreadyopen(MLXNN/Module.swift:97), so nothing needed to change on your side of the dependency.One cascade worth flagging
SparseMoeBlockconforms toUnaryLayer, which is a public protocol, so once the class becameopenthe compiler required itscallAsFunctionto be public too:I made it
openrather thanpublic, since overriding that method is precisely the point of the change. Mentioning it because it is the one place where "just widen the class" was not sufficient, and it is not obvious from reading.Deliberately not included
The internal notes this came from bundled a second, unrelated change: porting the merged GDN decode conv-fold from the MLXLLM copy of this model to the MLXVLM twin. That is a real optimization with its own bitwise test, but it is a behaviour change and it belongs in its own PR against its own review. I left it out to keep this one purely mechanical. Happy to send it separately if you want it.
I also left
FusedGateUpSwitchGLUalone — it ispubliclike the others, but I have no use case for subclassing it and did not want to widen API surface speculatively. Say the word if you would rather these move together.Evidence
swift build --target MLXVLM→ clean, exit 0.swift test→ 296 tests, 0 failures, unchanged.swift-formatapplied with the repo's.swift-format.mainat3d292ec.Access control is the whole diff — 9 insertions, 9 deletions — so "no behaviour change" is checkable by reading it.