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
39 changes: 27 additions & 12 deletions Tests/MLXLMTests/Gemma4ChunkedPrefillTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ struct Gemma4ChunkedPrefillTests {
"""
let config = try JSONDecoder().decode(
Gemma4Configuration.self, from: Data(json.utf8))
return Gemma4(config)
// Pin the initializer weights. Task-local rather than MLXRandom.seed:
// tests in this suite run concurrently and share the global RNG.
let model = withRandomState(MLXRandom.RandomState(seed: 42)) {
Gemma4(config)
}
// Run in f16, as real models do.
model.apply { $0.dtype.isFloatingPoint ? $0.asType(.float16) : $0 }
return model
}

/// Token ids well inside the tiny vocabulary.
Expand Down Expand Up @@ -94,15 +101,18 @@ struct Gemma4ChunkedPrefillTests {
#expect(chunked.cacheOffsets == singlePass.cacheOffsets)
#expect(chunked.cacheOffsets.allSatisfy { $0 == tokens.count })

let close = allClose(
chunked.logits, singlePass.logits, rtol: 1e-4, atol: 1e-5
).item(Bool.self)
// Max absolute difference, not allClose: relative error is meaningless
// here because logits pass through zero.
let drift = abs(
chunked.logits.asType(.float32) - singlePass.logits.asType(.float32)
).max()
eval(drift)
#expect(
close,
drift.item(Float.self) < 5e-2,
"""
Final-position logits differ between windowSize=\(chunkSize) and \
single-pass prefill — chunked prefill must be numerically \
equivalent under causal masking.
single-pass prefill by \(drift.item(Float.self)) — chunked prefill \
must be numerically equivalent under causal masking.
""")
}

Expand Down Expand Up @@ -131,11 +141,16 @@ struct Gemma4ChunkedPrefillTests {
eval(output.logits)
#expect(cache.allSatisfy { $0.offset == tokens.count })

// And it matches the batched run.
// And it matches the batched run. Same shapes and same chunking, so the
// two runs are bitwise equal; the tolerance only covers one f16 ulp,
// which is ~2e-3 at these logit magnitudes.
let batched = try Self.prefillLogits(model: model, tokens: tokens, windowSize: 6)
let close = allClose(
output.logits[0..., -1, 0...], batched.logits, rtol: 1e-4, atol: 1e-5
).item(Bool.self)
#expect(close, "1-D and [1, N] token inputs must produce identical logits.")
let drift = abs(
output.logits[0..., -1, 0...].asType(.float32) - batched.logits.asType(.float32)
).max()
eval(drift)
#expect(
drift.item(Float.self) < 5e-3,
"1-D and [1, N] token inputs must produce identical logits.")
}
}
13 changes: 11 additions & 2 deletions Tests/MLXLMTests/Gemma4UnifiedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,14 @@ struct Gemma4UnifiedTests {

@Test("Gemma4 Unified text-only prefill is chunk-size invariant")
func textOnlyPrefillChunkSizeInvariant() throws {
let model = Gemma4Unified(try tinyTextConfig())
// Pin the initializer weights. Task-local rather than MLXRandom.seed:
// tests in this suite run concurrently and share the global RNG.
let config = try tinyTextConfig()
let model = withRandomState(MLXRandom.RandomState(seed: 42)) {
Gemma4Unified(config)
}
// Run in f16, as real models do.
model.apply { $0.dtype.isFloatingPoint ? $0.asType(.float16) : $0 }
let input = LMInput(tokens: MLXArray([0, 2, 3, 4, 5, 1]).expandedDimensions(axis: 0))

func prefill(windowSize: Int) throws -> (logits: MLXArray, cacheOffsets: [Int]) {
Expand All @@ -147,7 +154,9 @@ struct Gemma4UnifiedTests {
let diff = abs(full.logits.asType(.float32) - chunked.logits.asType(.float32)).max()
eval(diff)

#expect(diff.item(Float.self) < 1e-4)
#expect(
diff.item(Float.self) < 1e-4,
"Chunked and single-pass prefill logits differ by \(diff.item(Float.self)).")
}

@Test("Gemma4 Unified processor emits model patches and position ids")
Expand Down
Loading