Skip to content

Llama and Qwen3 MLP: use compiled SwiGLU - #854

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
Augustas11:upstream/swiglu-compiled
Dec 2, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
Augustas11:upstream/swiglu-compiled

Conversation

@Augustas11

Copy link
Copy Markdown
Contributor

Proposed changes

Adds a package func swiGLU(_ gate: MLXArray, _ x: MLXArray) -> MLXArray helper in Libraries/MLXLMCommon/SwiGLU.swift that reuses the existing compiledSiluProduct (SwitchLayers.swift) for same-dtype inputs, and wires it into LlamaMLP (Libraries/MLXLLM/Models/Llama.swift) and Qwen3MLP (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) * x so no type promotion happens inside the compiled trace.

Scope: only LlamaMLP and Qwen3MLP are 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)

  • Unit tests (Tests/MLXLMTests/SwiGLUFusionTests.swift, release test build with compiled Metal library, --filter SwiGLUFusionTests): 4 tests executed, 0 failures (0.25 s) — checks arrayEqual across .float16, .bfloat16, and .float32 for standalone swiGLU (shapes [1, 1, 14336], [1, 7, 1024], [4, 1, 3072]), mixed-dtype fallback, and 4-bit quantized LlamaMLP and Qwen3MLP.
  • End-to-end A/B benchmark (upstream/main f88d37e vs this branch, median of 6 alternating control/candidate rounds, prompt 512 tokens, decode 256 tokens, greedy temperature = 0; every candidate decode round beat every control round):
Model (mlx-community) Control decode (range) Candidate decode (range) Decode delta Control prefill Candidate prefill Prefill delta Greedy tokens identical (6/6)
Llama-3.2-3B-Instruct-4bit 214.54 tok/s (213.80–214.72) 216.71 tok/s (216.37–217.07) +1.01% 2407.63 tok/s 2422.51 tok/s +0.62% Yes
Qwen3-4B-4bit 162.57 tok/s (162.33–162.85) 164.36 tok/s (163.90–164.99) +1.10% 2025.46 tok/s 2037.48 tok/s +0.59% Yes

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

AI usage

  • I have read this PR description in full and approve it as my own, and it
    accurately describes the code changes.
  • AI usage disclosure: the code, tests, benchmark harness, and this description were drafted with an AI coding assistant; I reviewed the diff and ran the tests and benchmarks on my own machine.

Augustas from Malibu

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

Change looks good, thank you!

@davidkoski
davidkoski merged commit d5d4b3f into ml-explore:main Dec 2, 2026
3 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants