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
6 changes: 3 additions & 3 deletions Libraries/IntegrationTestHelpers/IntegrationTestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -905,10 +905,10 @@ public enum RerankerIntegrationTests {
response.scoreKind == .cosineSimilarity,
"\(modelID) must return cosine-similarity scores")
try check(response.results.count == 2, "\(modelID) returned an unexpected score count")
// Generated by rerank.py from the pinned MLX checkpoint revision above. The upstream
// checkpoint holds the same bfloat16 weights, so it has to reproduce them too.
// Generated by rerank.py from the pinned MLX checkpoint revision above.
// The source projector is bfloat16; the converted MLX projector is float32.
let expectedScores = [-0.156_369_954_347_610_47, 0.402_552_098_035_812_4]
// Quantized kernels can vary slightly with backend evaluation order.
// Allow for projector precision and backend evaluation order.
let tolerance = 3e-3
for index in expectedScores.indices {
let result = response.results[index]
Expand Down
3 changes: 3 additions & 0 deletions Libraries/MLXLLM/Models/JinaReranker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ public final class JinaRerankerModel: Module, LanguageModel, KVCacheDimensionPro
}

func jinaCosineSimilarity(_ documents: MLXArray, _ query: MLXArray) -> MLXArray {
// The reference reranker computes final cosine scores from float32 embeddings.
let documents = documents.asType(.float32)
let query = query.asType(.float32)
let numerator = MLX.sum(documents * query, axis: -1)
let documentNorm = MLX.sqrt(MLX.sum(documents * documents, axis: -1))
let queryNorm = MLX.sqrt(MLX.sum(query * query, axis: -1))
Expand Down
24 changes: 24 additions & 0 deletions Tests/MLXLMTests/RerankerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,30 @@ struct RerankerTests {
#expect(scores[0] <= 1)
}

@Test(arguments: [DType.bfloat16, .float16, .float32])
func jinaCosineSimilarityUsesFloat32Reductions(dtype: DType) {
let query = MLXArray([Float(1), 2, 3]).reshaped(1, 3).asType(dtype)
let documents = MLXArray([Float(3), 2, 1, -3, -2, -1, 0, 0, 0])
.reshaped(3, 3).asType(dtype)

let output = jinaCosineSimilarity(documents, query)
let scores = output.asArray(Float.self)

#expect(output.dtype == .float32)
#expect(abs(scores[0] - 10.0 / 14.0) < 1e-6)
#expect(abs(scores[1] + 10.0 / 14.0) < 1e-6)
#expect(scores[2] == 0)
}

@Test(arguments: [Float(1e-5), 1e3])
func jinaCosineSimilarityAvoidsFloat16UnderflowAndOverflow(scale: Float) {
let vector = (MLXArray([Float(1), 2, 3]) * scale).reshaped(1, 3).asType(.float16)
let score = jinaCosineSimilarity(vector, vector).item(Float.self)

#expect(score.isFinite)
#expect(abs(score - 1) < 1e-6)
}

@Test func causalScoringUsesMicroBatchesAndReturnsNormalizedRelevance() async throws {
let tokenizer = ByteRerankerTokenizer()
let model = TestCausalRerankerModel(
Expand Down
Loading