|
| 1 | +// Copyright 2026 Apple Inc. |
| 2 | +// |
| 3 | +// Use of this source code is governed by a BSD-3-clause license that can |
| 4 | +// be found in the LICENSE file or at https://opensource.org/licenses/BSD-3-Clause |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import Testing |
| 8 | + |
| 9 | +@testable import CoreAISpeech |
| 10 | + |
| 11 | +@Suite("GenerationConfig") |
| 12 | +struct GenerationConfigTests { |
| 13 | + @Test("Parses forced_decoder_ids from HuggingFace format") |
| 14 | + func parseForcedDecoderIds() throws { |
| 15 | + let json: [String: Any] = [ |
| 16 | + "forced_decoder_ids": [[1, 50258], [2, 50259], [3, 50360], [4, 50364]], |
| 17 | + "eos_token_id": 50257, |
| 18 | + "max_new_tokens": 100, |
| 19 | + ] |
| 20 | + let data = try JSONSerialization.data(withJSONObject: json) |
| 21 | + let url = FileManager.default.temporaryDirectory.appendingPathComponent("gen_config.json") |
| 22 | + try data.write(to: url) |
| 23 | + defer { try? FileManager.default.removeItem(at: url) } |
| 24 | + |
| 25 | + let config = try GenerationConfig(from: url) |
| 26 | + #expect(config.forcedPrefix == [50258, 50259, 50360, 50364]) |
| 27 | + #expect(config.eotToken == 50257) |
| 28 | + #expect(config.maxDecodeSteps == 100) |
| 29 | + } |
| 30 | + |
| 31 | + @Test("Falls back to Whisper defaults when forced_decoder_ids is missing") |
| 32 | + func fallbackOnMissingField() throws { |
| 33 | + let json: [String: Any] = ["eos_token_id": 50257] |
| 34 | + let data = try JSONSerialization.data(withJSONObject: json) |
| 35 | + let url = FileManager.default.temporaryDirectory.appendingPathComponent("gen_config2.json") |
| 36 | + try data.write(to: url) |
| 37 | + defer { try? FileManager.default.removeItem(at: url) } |
| 38 | + |
| 39 | + let config = try GenerationConfig(from: url) |
| 40 | + #expect(config.forcedPrefix == GenerationConfig.whisper.forcedPrefix) |
| 41 | + } |
| 42 | + |
| 43 | + @Test("Falls back to Whisper defaults when forced_decoder_ids has wrong format") |
| 44 | + func fallbackOnWrongFormat() throws { |
| 45 | + let json: [String: Any] = [ |
| 46 | + "forced_decoder_ids": [50258, 50259, 50360], // flat array, not pairs |
| 47 | + ] |
| 48 | + let data = try JSONSerialization.data(withJSONObject: json) |
| 49 | + let url = FileManager.default.temporaryDirectory.appendingPathComponent("gen_config3.json") |
| 50 | + try data.write(to: url) |
| 51 | + defer { try? FileManager.default.removeItem(at: url) } |
| 52 | + |
| 53 | + let config = try GenerationConfig(from: url) |
| 54 | + #expect(config.forcedPrefix == GenerationConfig.whisper.forcedPrefix) |
| 55 | + } |
| 56 | + |
| 57 | + @Test("Skips null token IDs in forced_decoder_ids pairs") |
| 58 | + func skipsNullTokenIds() throws { |
| 59 | + let json: [String: Any] = [ |
| 60 | + "forced_decoder_ids": [[1, NSNull()], [2, 50360], [3, 50364]], |
| 61 | + ] |
| 62 | + let data = try JSONSerialization.data(withJSONObject: json) |
| 63 | + let url = FileManager.default.temporaryDirectory.appendingPathComponent("gen_config4.json") |
| 64 | + try data.write(to: url) |
| 65 | + defer { try? FileManager.default.removeItem(at: url) } |
| 66 | + |
| 67 | + let config = try GenerationConfig(from: url) |
| 68 | + #expect(config.forcedPrefix == [50360, 50364]) |
| 69 | + } |
| 70 | + |
| 71 | + @Test("Handles out-of-order positions") |
| 72 | + func outOfOrderPositions() throws { |
| 73 | + let json: [String: Any] = [ |
| 74 | + "forced_decoder_ids": [[3, 50360], [1, 50258], [2, 50259]], |
| 75 | + ] |
| 76 | + let data = try JSONSerialization.data(withJSONObject: json) |
| 77 | + let url = FileManager.default.temporaryDirectory.appendingPathComponent("gen_config5.json") |
| 78 | + try data.write(to: url) |
| 79 | + defer { try? FileManager.default.removeItem(at: url) } |
| 80 | + |
| 81 | + let config = try GenerationConfig(from: url) |
| 82 | + #expect(config.forcedPrefix == [50258, 50259, 50360]) |
| 83 | + } |
| 84 | +} |
0 commit comments