Repository navigation
Llama and Qwen3 MLP: use compiled SwiGLU - #854
Merged
Merged
Conversation
The Llama and Qwen3 MLP ran a compiled silu and then a separate multiply, two kernel launches per layer. swiGLU traces gate * sigmoid(gate) * x into one kernel; mixed input dtypes keep the two-kernel form. Tests check the fused activation and both MLPs (4-bit quantized) are bit-equal to the unfused form for float16, bfloat16 and float32.
div0-space
added a commit
to LibraxisAI/mlx-swift-lm
that referenced
this pull request
Dec 2, 2026
…20cb09) into unified LibraxisAI core - Integrates MLXWhisper Swift 6 library, POST /v1/audio/transcriptions (--whisper-model), ml-explore#854 compiled SwiGLU, ml-explore#436 PromptTrie/LRUPromptCache, ml-explore#643 bounded Prompt-Lookup Decoding, and ml-explore#392/ml-explore#400 Gemma 4 audio/video - Reconciles Package.swift (both MLXGuidedGeneration and MLXWhisper linked into MLXLMServer), ServerConfiguration (--batch-v2/--max-concurrency/--kv-cache-mb/--served-model-name + --whisper-model), and MLXServerRunner (CBv2 + Whisper + Embeddings in one mlx-server process) Authored-By: gemini <agents@vetcoders.io>
davidkoski
approved these changes
Dec 2, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
Change looks good, thank you!
4 tasks done
hkay-dev
pushed a commit
to hkay-dev/mlx-swift-lm
that referenced
this pull request
Dec 6, 2026
The Llama and Qwen3 MLP ran a compiled silu and then a separate multiply, two kernel launches per layer. swiGLU traces gate * sigmoid(gate) * x into one kernel; mixed input dtypes keep the two-kernel form. Tests check the fused activation and both MLPs (4-bit quantized) are bit-equal to the unfused form for float16, bfloat16 and float32.
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.
Proposed changes
Adds a
package func swiGLU(_ gate: MLXArray, _ x: MLXArray) -> MLXArrayhelper inLibraries/MLXLMCommon/SwiGLU.swiftthat reuses the existingcompiledSiluProduct(SwitchLayers.swift) for same-dtype inputs, and wires it intoLlamaMLP(Libraries/MLXLLM/Models/Llama.swift) andQwen3MLP(Libraries/MLXLLM/Models/Qwen3.swift) so MLX compiles the SiLU and elementwise multiply together instead of dispatching them separately.Mixed-dtype inputs fall back to
silu(gate) * xso no type promotion happens inside the compiled trace.Scope: only
LlamaMLPandQwen3MLPare updated here (the two architectures benchmarked below); other SwiGLU models (e.g.Qwen2MLP) are left unchanged to keep the diff and verification bounded and can adopt the same helper in a follow-up.Verification (M3 Ultra, release build)
Tests/MLXLMTests/SwiGLUFusionTests.swift, release test build with compiled Metal library,--filter SwiGLUFusionTests): 4 tests executed, 0 failures (0.25 s) — checksarrayEqualacross.float16,.bfloat16, and.float32for standaloneswiGLU(shapes[1, 1, 14336],[1, 7, 1024],[4, 1, 3072]), mixed-dtype fallback, and 4-bit quantizedLlamaMLPandQwen3MLP.upstream/mainf88d37evs this branch, median of 6 alternating control/candidate rounds, prompt 512 tokens, decode 256 tokens, greedytemperature = 0; every candidate decode round beat every control round):mlx-community)Llama-3.2-3B-Instruct-4bitQwen3-4B-4bitChecklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI usage
accurately describes the code changes.
Augustas from Malibu