Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions Libraries/MLXLLM/Models/Jamba.swift
Original file line number Diff line number Diff line change
Expand Up @@ -533,29 +533,19 @@ public class JambaModel: Module, LLMModel, KVCacheDimensionProvider {
sanitizedWeights["lm_head.weight"] = nil
}

// Handle MoE expert weights
if sanitizedWeights["model.layers.0.block_sparse_moe.experts.0.w1.weight"] == nil {
return sanitizedWeights
}

// Stack the per-expert tensors of a Hugging Face checkpoint,
// `feed_forward.experts.N.<proj>`, into `feed_forward.switch_mlp.<proj>`,
// as in mlx-lm jamba.py.
for l in 0 ..< config.numHiddenLayers {
let prefix = "model.layers.\(l)"
let mapping: [(String, String)] = [
("w1", "gate_proj"),
("w2", "down_proj"),
("w3", "up_proj"),
]

for (n, m) in mapping {
for k in ["weight", "scales", "biases"] {
if sanitizedWeights["\(prefix).block_sparse_moe.experts.0.\(n).\(k)"] != nil {
let toJoin = (0 ..< config.numExperts).map { e in
sanitizedWeights.removeValue(
forKey: "\(prefix).block_sparse_moe.experts.\(e).\(n).\(k)"
)!
}
sanitizedWeights["\(prefix).block_sparse_moe.switch_mlp.\(m).\(k)"] =
MLX.stacked(toJoin)
let base = "model.layers.\(l).feed_forward"
for proj in ["gate_proj", "down_proj", "up_proj"] {
for name in ["weight", "bias", "scales", "biases"] {
let experts = (0 ..< config.numExperts).compactMap { e in
sanitizedWeights.removeValue(forKey: "\(base).experts.\(e).\(proj).\(name)")
}
if !experts.isEmpty {
sanitizedWeights["\(base).switch_mlp.\(proj).\(name)"] = MLX.stacked(
experts)
}
}
}
Expand Down
133 changes: 133 additions & 0 deletions Tests/MLXLMTests/JambaSanitizeExpertsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import Foundation
import MLX
import MLXLMCommon
import MLXNN
import Testing

@testable import MLXLLM

/// Regression test for issue #200: `JambaModel.sanitize(weights:)` wrote
/// the stacked experts under `block_sparse_moe`, but the module path is
/// `feed_forward`, so a checkpoint with one tensor per expert did not load.
///
/// The test is copied from `jambaLoaderStacksPerExpertWeights` in the
/// kernel forward-pass tests of PR #185, without the known issue.
@Suite
struct JambaSanitizeExpertsTests {

static func model(seed: UInt64) throws -> JambaModel {
let configuration = try TinyModel.configuration(
JambaConfiguration.self,
[
"model_type": "jamba", "hidden_size": 32, "intermediate_size": 48,
"num_hidden_layers": 2, "num_attention_heads": 4, "num_key_value_heads": 2,
"attn_layer_offset": 1, "attn_layer_period": 2, "expert_layer_offset": 1,
"expert_layer_period": 2, "mamba_d_conv": 4, "mamba_d_state": 8,
"mamba_expand": 2, "num_experts": 4, "num_experts_per_tok": 2,
"rms_norm_eps": 1e-6, "max_position_embeddings": 256, "vocab_size": 64,
"tie_word_embeddings": false,
])
let model = JambaModel(configuration)
TinyModel.randomize(model, seed: seed)
return model
}

/// A Hugging Face Jamba checkpoint stores each expert as
/// `feed_forward.experts.N.{gate,up,down}_proj`. The loader must stack
/// them into `feed_forward.switch_mlp`, and the loaded model must give
/// the logits of the model the checkpoint came from.
@Test func loaderStacksPerExpertWeights() throws {
let reference = try Self.model(seed: 5)
var checkpoint: [String: MLXArray] = [:]
for (key, value) in reference.parameters().flattened() {
if key.contains(".switch_mlp.") {
for expert in 0 ..< value.dim(0) {
let perExpert = key.replacingOccurrences(
of: ".switch_mlp.", with: ".experts.\(expert).")
checkpoint[perExpert] = value[expert]
}
} else {
checkpoint[key] = value
}
}
#expect(checkpoint["model.layers.1.feed_forward.experts.3.down_proj.weight"] != nil)
#expect(checkpoint["model.layers.1.feed_forward.switch_mlp.down_proj.weight"] == nil)

let loaded = try Self.model(seed: 6)
let folder = FileManager.default.temporaryDirectory
.appendingPathComponent("jamba-sanitize-\(UUID().uuidString)", isDirectory: true)
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
defer { try? FileManager.default.removeItem(at: folder) }
try MLX.save(arrays: checkpoint, url: folder.appendingPathComponent("model.safetensors"))
// loadWeights calls sanitize(weights:) and then a strict update, so
// a key under the wrong module path makes it throw.
try loadWeights(modelDirectory: folder, model: loaded)

let rows = [TinyModel.tokens(count: 11, vocabularySize: 64, seed: 3)]
#expect(
TinyModel.maxAbsDifference(
TinyModel.logits(reference, rows), TinyModel.logits(loaded, rows)) == 0,
"loaded logits")
}
}

/// Tiny-model helpers for this file. They are copied from the kernel test
/// support of PR #183 (`Tests/MLXLMTests/Kernel/Support/SyntheticModel.swift`)
/// and are private, so that this file does not depend on that PR.
private enum TinyModel {

/// Decodes a model configuration from a JSON dictionary.
static func configuration<C: Decodable>(_ type: C.Type, _ values: [String: Any]) throws -> C {
try JSONDecoder().decode(C.self, from: JSONSerialization.data(withJSONObject: values))
}

/// Replaces every floating-point parameter with seeded random values:
/// norm scales near 1, other 1-D values near 0, and matrices with a
/// standard deviation of `1 / sqrt(fan-in)`.
static func randomize(_ model: Module, seed: UInt64) {
let parameters = model.parameters().flattened().sorted { $0.0 < $1.0 }
var updated: [(String, MLXArray)] = []
for (index, (name, value)) in parameters.enumerated() where value.dtype.isFloatingPoint {
let noise = MLXRandom.normal(
value.shape, key: MLXRandom.key(seed &* 1_000_003 &+ UInt64(index)))
let random: MLXArray
if value.ndim <= 1 {
random = name.hasSuffix("weight") ? 1 + 0.1 * noise : 0.1 * noise
} else {
let fanIn =
name.contains("conv") ? value.shape.dropFirst().reduce(1, *) : value.dim(-1)
random = noise * (1 / Float(fanIn).squareRoot())
}
updated.append((name, random.asType(value.dtype)))
}
model.update(parameters: ModuleParameters.unflattened(updated))
eval(model)
}

/// Token IDs from a fixed linear congruential generator.
static func tokens(count: Int, vocabularySize: Int, seed: Int) -> [Int] {
var state = UInt64(truncatingIfNeeded: seed) &+ 0x9E37_79B9
return (0 ..< count).map { _ in
state = state &* 6_364_136_223_846_793_005 &+ 1_442_695_040_888_963_407
return Int((state >> 33) % UInt64(vocabularySize))
}
}

/// A `[rows, length]` int32 array of token IDs.
static func batch(_ rows: [[Int]]) -> MLXArray {
MLXArray(rows.flatMap { $0.map { Int32($0) } }).reshaped(rows.count, rows[0].count)
}

static func maxAbsDifference(_ a: MLXArray, _ b: MLXArray) -> Float {
abs(a.asType(.float32) - b.asType(.float32)).max().item(Float.self)
}

/// Runs the model on `rows` and returns the logits.
static func logits(
_ model: any LanguageModel, _ rows: [[Int]], cache: [KVCache]? = nil
) -> MLXArray {
let output = model(batch(rows), cache: cache)
eval(output)
return output
}
}
23 changes: 5 additions & 18 deletions Tests/MLXLMTests/Kernel/LLM/TextModelForwardPassTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -450,24 +450,11 @@ extension KernelTests {
JambaModel($0)
}
let rows = [SyntheticModel.tokens(count: 11, vocabularySize: 64, seed: 3)]
// The load throws, so a thrown error is the known issue, as in
// ModelCaseChecks.loading. Once it loads, the logits must match.
try withKnownIssue(
"""
JambaModel.sanitize(weights:) stacks experts only under \
`block_sparse_moe.experts.N.w1` and writes `block_sparse_moe.switch_mlp` \
(Jamba.swift:521-565), but the module path is `feed_forward.switch_mlp`, \
so a per-expert checkpoint does not load.
"""
) {
try SyntheticModel.load(checkpoint, into: loaded)
#expect(
SyntheticModel.maxAbsDifference(
ForwardPassChecks.logits(reference, rows),
ForwardPassChecks.logits(loaded, rows)) == 0, "loaded logits")
} matching: {
$0.error != nil || $0.isFailedExpectation(["loaded logits"])
}
try SyntheticModel.load(checkpoint, into: loaded)
#expect(
SyntheticModel.maxAbsDifference(
ForwardPassChecks.logits(reference, rows),
ForwardPassChecks.logits(loaded, rows)) == 0, "loaded logits")
}

/// With tied embeddings the model must still return logits over the
Expand Down
8 changes: 8 additions & 0 deletions fork.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ def:
- "Tests/MLXLMTests/QwenImageBudgetTests.swift"
- "Tests/MLXLMTests/GatedDelta*.swift"
- "Tests/MLXLMTests/SSMDecodeBoundsTests.swift"
- title: "Jamba per-expert checkpoint loading"
description: |
`JambaModel.sanitize(weights:)` stacks the per-expert tensors `feed_forward.experts.N.*` into `feed_forward.switch_mlp.*`.
Upstream wrote them under `block_sparse_moe`, a path the model does not have, so a per-expert checkpoint did not load.
`JambaSanitizeExpertsTests` loads a per-expert checkpoint of a tiny model and compares the logits.
globs:
- "Libraries/MLXLLM/Models/Jamba.swift"
- "Tests/MLXLMTests/JambaSanitizeExpertsTests.swift"
- title: "Ternary Bonsai 2 (packed dense Qwen)"
description: |
Native dense Qwen text and vision wrappers for Prism's schema-2
Expand Down
Loading