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
12 changes: 8 additions & 4 deletions Tests/MLXLMTests/EmbeddingGemmaMaskTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private func hasNaN(_ a: MLXArray) -> Bool {

/// Batch rows against the same texts run one at a time: real positions and pooled output.
private func expectBatchMatchesSingles(
_ m: EmbeddingGemma, lengths: [Int], left: Bool = false,
_ m: EmbeddingGemma, lengths: [Int], left: Bool = false, tolerance: Double = 1e-5,
sourceLocation: SourceLocation = #_sourceLocation
) {
let (ids, mask) = padded(lengths, left: left)
Expand All @@ -98,9 +98,11 @@ private func expectBatchMatchesSingles(
let (single, singlePooled) = run(
m, [Array(ids[row][real])], mask: [[Int32](repeating: 1, count: n)])
let rowStates = states[row, real.lowerBound ..< real.upperBound]
#expect(relNorm(rowStates, single[0]) <= 1e-5, "row \(row)", sourceLocation: sourceLocation)
#expect(
relNorm(pooled[row], singlePooled[0]) <= 1e-5, "row \(row)",
relNorm(rowStates, single[0]) <= tolerance, "row \(row)",
sourceLocation: sourceLocation)
#expect(
relNorm(pooled[row], singlePooled[0]) <= tolerance, "row \(row)",
sourceLocation: sourceLocation)
}
}
Expand Down Expand Up @@ -208,8 +210,10 @@ struct EmbeddingGemmaMaskTests {

@Test("G7: a left-padded batch matches the texts run one at a time")
func leftPadding() throws {
// TF32 matmuls (neural accelerators) leave ~1.1e-3 here; see MatmulPrecision.
expectBatchMatchesSingles(
model(try config(bidirectional: true)), lengths: [20, 3], left: true)
model(try config(bidirectional: true)), lengths: [20, 3], left: true,
tolerance: MatmulPrecision.tolerance(float32: 1e-5, reduced: 5e-3))
}

@Test("G8: a 1-D input with a 1-D mask equals the 2-D version")
Expand Down
9 changes: 9 additions & 0 deletions Tests/MLXLMTests/EvalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ public class EvalTests: XCTestCase {
}

func testSpeculativeLogProbabilitiesMatchTheMainModelDecodingAlone() throws {
// Two decodes of the same model are compared value for value and by top-k order, which
// TF32 matmuls (neural accelerators) perturb by up to ~3e-3 and reorder near-ties. CPU
// pins IEEE FP32; see MatmulPrecision.
try Device.withDefaultDevice(.cpu) {
try speculativeLogProbabilitiesMatchTheMainModelDecodingAlone()
}
}

private func speculativeLogProbabilitiesMatchTheMainModelDecodingAlone() throws {
let main = makeQualityGateLlamaModel()
let stranger = makeQualityGateLlamaModel(seed: 19)
let prompt = LMInput(tokens: MLXArray([1, 2, 3]))
Expand Down
12 changes: 10 additions & 2 deletions Tests/MLXLMTests/HadamardQuantizedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ final class HadamardQuantizedTests: XCTestCase {
let h = TurboQuantRotation.hadamardMatrix(dim: block) / sqrt(Float(block))
let expected = matmul((x * signs).reshaped(-1, block), h).reshaped(3, 16)

assertClose(rotation.forward(x, signs: signs), expected)
// TF32 matmuls (neural accelerators) need atol ~6e-4 here (max abs error 7.2e-4); see
// MatmulPrecision.
assertClose(
rotation.forward(x, signs: signs), expected,
atol: MatmulPrecision.tolerance(float32: 1e-4, reduced: 2e-3))
}

func testInverseUndoesForwardAndKeepsDType() {
Expand Down Expand Up @@ -98,7 +102,11 @@ final class HadamardQuantizedTests: XCTestCase {

let dequantizedWeight = dequantized(
weight, scales: scales, biases: biases, groupSize: groupSize, bits: bits)
assertClose(layer(x), matmul(rotated, dequantizedWeight.T), rtol: 1e-3, atol: 1e-3)
// TF32 matmuls (neural accelerators) need atol ~1.4e-3 here (max abs error 2.7e-3); see
// MatmulPrecision.
assertClose(
layer(x), matmul(rotated, dequantizedWeight.T), rtol: 1e-3,
atol: MatmulPrecision.tolerance(float32: 1e-3, reduced: 5e-3))

let unrotated = quantizedMM(
x, weight, scales: scales, biases: biases, transpose: true,
Expand Down
3 changes: 2 additions & 1 deletion Tests/MLXLMTests/LFM2DSparkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ struct LFM2DSparkMetalTests {
let actualLogits = target(next, cache: speculativeCache, state: nil).logits
let expectedLogits = target(next, cache: baselineCache, state: nil).logits
let difference = abs(actualLogits - expectedLogits).max().item(Float.self)
#expect(difference < 1e-4)
// TF32 matmuls (neural accelerators) leave ~2e-4 here; see MatmulPrecision.
#expect(difference < MatmulPrecision.tolerance(float32: 1e-4, reduced: 1e-3))
}

@Test
Expand Down