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
15 changes: 11 additions & 4 deletions Libraries/MLXEmbedders/Models/Bert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -783,10 +783,17 @@ public struct BertConfiguration: Decodable, Sendable {
architectures = try container.decodeIfPresent([String].self, forKey: .architectures) ?? []
let stringIDToLabel =
try container.decodeIfPresent([String: String].self, forKey: .idToLabel) ?? [:]
idToLabel = Dictionary(
uniqueKeysWithValues: stringIDToLabel.compactMap { key, value in
Int(key).map { ($0, value) }
})
var parsedIDToLabel: [Int: String] = [:]
for (key, label) in stringIDToLabel {
guard let id = Int(key) else { continue }
guard parsedIDToLabel[id] == nil else {
throw DecodingError.dataCorruptedError(
forKey: .idToLabel, in: container,
debugDescription: "id2label key \(key) repeats integer id \(id).")
}
parsedIDToLabel[id] = label
}
idToLabel = parsedIDToLabel
let labelToID =
try container.decodeIfPresent([String: Int].self, forKey: .labelToID) ?? [:]
for (label, id) in labelToID where idToLabel[id] == nil {
Expand Down
33 changes: 30 additions & 3 deletions Tests/MLXLMTests/RerankerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,28 @@ struct RerankerTests {
#expect(configuration.encoderRerankerConfiguration.scoreKind == .normalizedRelevance)
}

@Test func bertConfigurationRejectsDuplicateIDToLabelKeys() throws {
#expect(throws: DecodingError.self) {
try decodeBertConfiguration(
modelType: "bert",
architecture: "BertModel",
labels: 2,
padTokenID: 0,
maxPositionEmbeddings: 32,
stringIDToLabel: ["1": "pos", "01": "also-pos"])
}

let configuration = try decodeBertConfiguration(
modelType: "bert",
architecture: "BertModel",
labels: 2,
padTokenID: 0,
maxPositionEmbeddings: 32,
idToLabel: [0: "irrelevant", 1: "relevant"])

#expect(configuration.idToLabel == [0: "irrelevant", 1: "relevant"])
}

@Test func encoderConfigurationUsesDeclaredPositiveLabel() throws {
let configuration = try decodeBertConfiguration(
modelType: "xlm-roberta",
Expand Down Expand Up @@ -806,6 +828,7 @@ private func bertConfigurationData(
padTokenID: Int,
maxPositionEmbeddings: Int,
idToLabel: [Int: String] = [:],
stringIDToLabel: [String: String] = [:],
labelToID: [String: Int] = [:],
includeNumLabels: Bool = true,
numLayers: Int = 1
Expand All @@ -825,7 +848,9 @@ private func bertConfigurationData(
if includeNumLabels {
configuration["num_labels"] = labels
}
if !idToLabel.isEmpty {
if !stringIDToLabel.isEmpty {
configuration["id2label"] = stringIDToLabel
} else if !idToLabel.isEmpty {
configuration["id2label"] = Dictionary(
uniqueKeysWithValues: idToLabel.map { (String($0.key), $0.value) })
}
Expand All @@ -841,7 +866,8 @@ private func decodeBertConfiguration(
labels: Int,
padTokenID: Int,
maxPositionEmbeddings: Int,
idToLabel: [Int: String] = [:]
idToLabel: [Int: String] = [:],
stringIDToLabel: [String: String] = [:]
) throws -> BertConfiguration {
try JSONDecoder().decode(
BertConfiguration.self,
Expand All @@ -851,7 +877,8 @@ private func decodeBertConfiguration(
labels: labels,
padTokenID: padTokenID,
maxPositionEmbeddings: maxPositionEmbeddings,
idToLabel: idToLabel))
idToLabel: idToLabel,
stringIDToLabel: stringIDToLabel))
}

private func decodeQwenConfiguration() throws -> MLXLLM.Qwen3Configuration {
Expand Down
Loading