diff --git a/IntegrationTesting/IntegrationTestingTests/Gemma4AudioIntegrationTests.swift b/IntegrationTesting/IntegrationTestingTests/Gemma4AudioIntegrationTests.swift new file mode 100644 index 0000000000..8a6e86a3d8 --- /dev/null +++ b/IntegrationTesting/IntegrationTestingTests/Gemma4AudioIntegrationTests.swift @@ -0,0 +1,100 @@ +// Copyright © 2026 Apple Inc. +// +// Real end-to-end Gemma 4 audio inference. Downloads an audio-capable Gemma 4 +// VLM and asks it to transcribe real speech clips, exercising the full audio +// path: AVAssetReader PCM -> mel feature extractor -> Conformer audio tower -> +// begin/end-of-audio prompt splice -> text. +// +// Speech clips are committed under Tests/MLXLMTests/Resources/. +// +// Run: +// xcodebuild test -project IntegrationTesting.xcodeproj \ +// -scheme IntegrationTesting -destination 'platform=macOS' \ +// -only-testing:IntegrationTestingTests/Gemma4AudioIntegrationTests + +import Foundation +import HuggingFace +import IntegrationTestHelpers +import MLXHuggingFace +import MLXLMCommon +import Testing +import Tokenizers + +private let models = IntegrationTestModels( + downloader: #hubDownloader(), + tokenizerLoader: #huggingFaceTokenizerLoader() +) + +private let resources = URL(fileURLWithPath: #filePath) + .deletingLastPathComponent() + .deletingLastPathComponent() + .deletingLastPathComponent() + .appendingPathComponent("Tests/MLXLMTests/Resources") + .path + +struct SpeechCase: Sendable, CustomStringConvertible { + let file: String + let expected: [String] + var description: String { file } +} + +private let speechCases: [SpeechCase] = [ + .init( + file: "gemma_speech_test.wav", + expected: ["quick", "brown", "fox", "lazy", "dog", "river"]), + .init( + file: "gemma_speech_long.wav", + expected: ["weather", "rain", "forecast", "afternoon", "breeze", "evening", "sky"]), +] + +@Suite(.serialized) +struct Gemma4AudioIntegrationTests { + + private func transcribe(model: String, clip: SpeechCase) async throws -> String { + let container = try await models.vlmContainer(for: ModelConfiguration(id: model)) + let session = ChatSession( + container, generateParameters: GenerateParameters(maxTokens: 120, temperature: 0)) + let url = URL(fileURLWithPath: "\(resources)/\(clip.file)") + return try await session.respond( + to: "Transcribe the speech in this audio clip.", + images: [], videos: [], audios: [.url(url)]) + } + + private func assertRecovered(_ answer: String, _ clip: SpeechCase) { + let lower = answer.lowercased() + #expect(!lower.contains(""), "audio path regressed to a wall: \(answer)") + let hits = clip.expected.filter { lower.contains($0) } + #expect( + hits.count >= 3, + "[\(clip.file)] did not recover the spoken words (matched \(hits) in: \(answer))") + } + + @Test(arguments: speechCases) + func gemma4_e4b_transcribes(_ clip: SpeechCase) async throws { + let answer = try await transcribe(model: "mlx-community/gemma-4-e4b-it-4bit", clip: clip) + print("[e4b/\(clip.file)] \(answer)") + assertRecovered(answer, clip) + } + + @Test func gemma4_e4b_perceivesRealSpeech() async throws { + let clip = SpeechCase(file: "gemma_audio_librispeech.wav", expected: []) + let answer = try await transcribe(model: "mlx-community/gemma-4-e4b-it-4bit", clip: clip) + print("[e4b/librispeech] \(answer)") + let lower = answer.lowercased() + #expect(!lower.contains(""), "audio path regressed to a wall") + #expect( + !lower.contains("not provided") && !lower.contains("no audio") + && !lower.contains("haven't provided") && !lower.contains("have not provided"), + "model claims no audio; audio not reaching the model: \(answer)") + // One content-word hit is the perception bar, not an ASR-quality bar: + // the 4-bit QAT checkpoint paraphrases real (non-TTS) speech loosely, + // and verbatim transcription quality is a model property rather than an + // integration requirement — this test only guards that real audio + // reaches the tower and influences the answer. + let contentWords = ["middle", "class", "welcome", "mr", "mister", "gospel", "apostle"] + let hits = contentWords.filter { lower.contains($0) } + #expect( + hits.count >= 1, + "did not perceive the real-speech content (matched \(hits) in: \(answer))") + } +} diff --git a/IntegrationTesting/IntegrationTestingTests/Gemma4VideoIntegrationTests.swift b/IntegrationTesting/IntegrationTestingTests/Gemma4VideoIntegrationTests.swift new file mode 100644 index 0000000000..81e66f7279 --- /dev/null +++ b/IntegrationTesting/IntegrationTestingTests/Gemma4VideoIntegrationTests.swift @@ -0,0 +1,102 @@ +// Copyright © 2026 Apple Inc. +// +// Real end-to-end Gemma 4 VIDEO inference. Downloads gemma-4-e4b-it-4bit and +// asks it to describe a real video clip, exercising the PR #256 video tower +// end to end (frame sampling via MediaProcessing → vision tower → text). +// +// Run: +// xcodebuild test -project IntegrationTesting.xcodeproj \ +// -scheme IntegrationTesting -destination 'platform=macOS' \ +// -only-testing:IntegrationTestingTests/Gemma4VideoIntegrationTests + +import Foundation +import HuggingFace +import IntegrationTestHelpers +import MLXHuggingFace +import MLXLMCommon +import Testing +import Tokenizers + +private let models = IntegrationTestModels( + downloader: #hubDownloader(), + tokenizerLoader: #huggingFaceTokenizerLoader() +) + +// Resolve Tests/MLXLMTests/Resources relative to this source file so the suite +// runs on any clone / CI checkout, not just one developer's machine. +private let videoResources = URL(fileURLWithPath: #filePath) + .deletingLastPathComponent() // IntegrationTestingTests/ + .deletingLastPathComponent() // IntegrationTesting/ + .deletingLastPathComponent() // repo root + .appendingPathComponent("Tests/MLXLMTests/Resources") + +@Suite(.serialized) +struct Gemma4VideoIntegrationTests { + + // The repo already ships a small real clip for VLM tests. + private static let videoURL = videoResources.appendingPathComponent("1080p_30.mov") + + @Test func gemma4_e4b_describesVideo() async throws { + let container = try await models.vlmContainer( + for: ModelConfiguration(id: "mlx-community/gemma-4-e4b-it-4bit") + ) + + let session = ChatSession( + container, + generateParameters: GenerateParameters(maxTokens: 120, temperature: 0) + ) + + let answer = try await session.respond( + to: "Describe what happens in this video in one or two sentences.", + images: [], + videos: [.url(Self.videoURL)], + audios: [] + ) + + print("[e4b/video] \(answer)") + + let lower = answer.lowercased() + // Reject the degenerate /special-token wall failure mode. + #expect(!lower.contains(""), "description is a wall — video tower not producing usable embeddings") + // Must be a substantive natural-language description, not a token fragment. + let wordCount = answer.split(whereSeparator: { $0 == " " || $0 == "\n" }).count + #expect(wordCount >= 8, "description too short to be a real video understanding: \(answer)") + // The clip is a sequence of solid colour blocks; a correct description + // should reference colour/blocks/frames. Require at least one such cue. + let visualCues = ["color", "colour", "block", "frame", "screen", "background", "blue", "green", "yellow", "magenta", "red"] + #expect( + visualCues.contains(where: { lower.contains($0) }), + "description lacks any visual cue from the clip: \(answer)" + ) + } + + // Big Buck Bunny (Blender Foundation, CC-BY-3.0) — a real animated outdoor + // scene. See Resources/FIXTURES_LICENSES.md. + private static let bbbURL = videoResources.appendingPathComponent("gemma_video_bbb.mp4") + + @Test func gemma4_e4b_describesVideo_bbb() async throws { + let container = try await models.vlmContainer( + for: ModelConfiguration(id: "mlx-community/gemma-4-e4b-it-4bit")) + let session = ChatSession( + container, generateParameters: GenerateParameters(maxTokens: 150, temperature: 0)) + + let answer = try await session.respond( + to: "Describe what happens in this video in one or two sentences.", + images: [], videos: [.url(Self.bbbURL)], audios: []) + + print("[e4b/video-bbb] \(answer)") + let lower = answer.lowercased() + #expect(!lower.contains(""), "BBB description is a wall") + #expect( + answer.split(whereSeparator: { $0 == " " || $0 == "\n" }).count >= 8, + "BBB description too short: \(answer)") + // Animated outdoor nature scene (rabbit/animal, grass/trees, sky, cartoon). + let cues = [ + "rabbit", "bunny", "animal", "creature", "animat", "cartoon", "character", + "grass", "tree", "forest", "field", "nature", "sky", "green", "outdoor", + ] + #expect( + cues.contains(where: { lower.contains($0) }), + "BBB description lacks any scene cue: \(answer)") + } +} diff --git a/Libraries/MLXLMCommon/LanguageModel.swift b/Libraries/MLXLMCommon/LanguageModel.swift index 4c85ac1c05..188e52d1a1 100644 --- a/Libraries/MLXLMCommon/LanguageModel.swift +++ b/Libraries/MLXLMCommon/LanguageModel.swift @@ -126,10 +126,17 @@ public struct LMInput { public let samples: MLXArray + /// Optional padding mask over the time axis of `samples`, where `true` + /// marks padding positions (frames the audio encoder should ignore) + /// and `false` marks valid audio. `nil` means all positions are valid. + public let mask: MLXArray? + public init( - samples: MLXArray + samples: MLXArray, + mask: MLXArray? = nil ) { self.samples = samples + self.mask = mask } } diff --git a/Libraries/MLXLMCommon/UserInput.swift b/Libraries/MLXLMCommon/UserInput.swift index d1be58b93d..f7d6d83c9d 100644 --- a/Libraries/MLXLMCommon/UserInput.swift +++ b/Libraries/MLXLMCommon/UserInput.swift @@ -159,10 +159,21 @@ public struct UserInput { public var minPixels: Int? public var maxPixels: Int? - public init(resize: CGSize? = nil, minPixels: Int? = nil, maxPixels: Int? = nil) { + /// Optional per-call cap on the number of frames sampled from each + /// video, for processors that support it. When set, the effective cap + /// is `min(model configured maximum, videoMaxFrames)`; when `nil` the + /// model configuration is used. Memory-constrained callers (e.g. apps + /// near a per-process memory limit) can lower this per request. + public var videoMaxFrames: Int? + + public init( + resize: CGSize? = nil, minPixels: Int? = nil, maxPixels: Int? = nil, + videoMaxFrames: Int? = nil + ) { self.resize = resize self.minPixels = minPixels self.maxPixels = maxPixels + self.videoMaxFrames = videoMaxFrames } } diff --git a/Libraries/MLXVLM/Models/Gemma4.swift b/Libraries/MLXVLM/Models/Gemma4.swift index 8ad788ee33..6b5039dcb7 100644 --- a/Libraries/MLXVLM/Models/Gemma4.swift +++ b/Libraries/MLXVLM/Models/Gemma4.swift @@ -8,12 +8,28 @@ import MLXNN private enum Gemma4Error: LocalizedError { case imageTokenCountMismatch(expectedVisionTokens: Int, actualPromptTokens: Int) + case videoTokenCountMismatch(expectedVisionTokens: Int, actualPromptTokens: Int) + case audioTokenCountMismatch(expectedAudioTokens: Int, actualPromptTokens: Int) + case missingVideoTokenId + case multipleAudioInputsUnsupported var errorDescription: String? { switch self { case .imageTokenCountMismatch(let expectedVisionTokens, let actualPromptTokens): return "Gemma4 image token count mismatch: vision encoder produced \(expectedVisionTokens) soft tokens, but the prompt contains \(actualPromptTokens) image tokens." + case .videoTokenCountMismatch(let expectedVisionTokens, let actualPromptTokens): + return + "Gemma4 video token count mismatch: vision encoder produced \(expectedVisionTokens) soft tokens, but the prompt contains \(actualPromptTokens) video tokens." + case .audioTokenCountMismatch(let expectedAudioTokens, let actualPromptTokens): + return + "Gemma4 audio token count mismatch: audio encoder produced \(expectedAudioTokens) soft tokens, but the prompt contains \(actualPromptTokens) audio tokens." + case .missingVideoTokenId: + return + "Gemma4 video input provided but the configuration does not declare a video_token_id." + case .multipleAudioInputsUnsupported: + return + "Gemma4 supports a single audio input per request; multiple audio clips were provided." } } } @@ -405,16 +421,79 @@ public struct Gemma4VisionConfiguration: Codable, Sendable { } } +public struct Gemma4AudioConfiguration: Codable, Sendable { + public let hiddenSize: Int + public let numHiddenLayers: Int + public let numAttentionHeads: Int + public let subsamplingConvChannels: [Int] + public let convKernelSize: Int + public let residualWeight: Float + public let attentionChunkSize: Int + public let attentionContextLeft: Int + public let attentionContextRight: Int + public let attentionLogitCap: Float + public let attentionInvalidLogitsValue: Float + public let useClippedLinears: Bool + public let rmsNormEps: Float + public let gradientClipping: Float + public let outputProjDims: Int? + + enum CodingKeys: String, CodingKey { + case hiddenSize = "hidden_size" + case numHiddenLayers = "num_hidden_layers" + case numAttentionHeads = "num_attention_heads" + case subsamplingConvChannels = "subsampling_conv_channels" + case convKernelSize = "conv_kernel_size" + case residualWeight = "residual_weight" + case attentionChunkSize = "attention_chunk_size" + case attentionContextLeft = "attention_context_left" + case attentionContextRight = "attention_context_right" + case attentionLogitCap = "attention_logit_cap" + case attentionInvalidLogitsValue = "attention_invalid_logits_value" + case useClippedLinears = "use_clipped_linears" + case rmsNormEps = "rms_norm_eps" + case gradientClipping = "gradient_clipping" + case outputProjDims = "output_proj_dims" + } + + public init(from decoder: any Swift.Decoder) throws { + let c = try decoder.container(keyedBy: CodingKeys.self) + hiddenSize = try c.decodeIfPresent(Int.self, forKey: .hiddenSize) ?? 1024 + numHiddenLayers = try c.decodeIfPresent(Int.self, forKey: .numHiddenLayers) ?? 12 + numAttentionHeads = try c.decodeIfPresent(Int.self, forKey: .numAttentionHeads) ?? 8 + subsamplingConvChannels = + try c.decodeIfPresent([Int].self, forKey: .subsamplingConvChannels) ?? [128, 32] + convKernelSize = try c.decodeIfPresent(Int.self, forKey: .convKernelSize) ?? 5 + residualWeight = try c.decodeIfPresent(Float.self, forKey: .residualWeight) ?? 0.5 + attentionChunkSize = try c.decodeIfPresent(Int.self, forKey: .attentionChunkSize) ?? 12 + attentionContextLeft = try c.decodeIfPresent(Int.self, forKey: .attentionContextLeft) ?? 13 + attentionContextRight = try c.decodeIfPresent(Int.self, forKey: .attentionContextRight) ?? 0 + attentionLogitCap = try c.decodeIfPresent(Float.self, forKey: .attentionLogitCap) ?? 50.0 + attentionInvalidLogitsValue = + try c.decodeIfPresent(Float.self, forKey: .attentionInvalidLogitsValue) ?? -1e9 + useClippedLinears = + try c.decodeIfPresent(Bool.self, forKey: .useClippedLinears) ?? true + rmsNormEps = try c.decodeIfPresent(Float.self, forKey: .rmsNormEps) ?? 1e-6 + gradientClipping = + try c.decodeIfPresent(Float.self, forKey: .gradientClipping) ?? 1e10 + outputProjDims = try c.decodeIfPresent(Int.self, forKey: .outputProjDims) + } +} + public struct Gemma4Configuration: Codable, Sendable { public let textConfiguration: Gemma4TextConfiguration public let visionConfiguration: Gemma4VisionConfiguration + public let audioConfiguration: Gemma4AudioConfiguration? public let modelType: String public let quantization: BaseConfiguration.Quantization? public let imageTokenId: Int public let audioTokenId: Int? + public let videoTokenId: Int? public let boiTokenId: Int public let eoiTokenId: Int? public let visionSoftTokensPerImage: Int + public let visionSoftTokensPerVideoFrame: Int + public let videoFrameChunkSize: Int public let tieWordEmbeddings: Bool private let _vocabularySize: Int? @@ -428,13 +507,17 @@ public struct Gemma4Configuration: Codable, Sendable { enum CodingKeys: String, CodingKey { case textConfiguration = "text_config" case visionConfiguration = "vision_config" + case audioConfiguration = "audio_config" case modelType = "model_type" case quantization case imageTokenId = "image_token_id" case audioTokenId = "audio_token_id" + case videoTokenId = "video_token_id" case boiTokenId = "boi_token_id" case eoiTokenId = "eoi_token_id" case visionSoftTokensPerImage = "vision_soft_tokens_per_image" + case visionSoftTokensPerVideoFrame = "vision_soft_tokens_per_video_frame" + case videoFrameChunkSize = "video_frame_chunk_size" case tieWordEmbeddings = "tie_word_embeddings" case _vocabularySize = "vocab_size" case _hiddenSize = "hidden_size" @@ -447,16 +530,32 @@ public struct Gemma4Configuration: Codable, Sendable { Gemma4TextConfiguration.self, forKey: CodingKeys.textConfiguration) visionConfiguration = try c.decode( Gemma4VisionConfiguration.self, forKey: CodingKeys.visionConfiguration) + audioConfiguration = try c.decodeIfPresent( + Gemma4AudioConfiguration.self, forKey: CodingKeys.audioConfiguration) modelType = try c.decodeIfPresent(String.self, forKey: CodingKeys.modelType) ?? "gemma4" quantization = try c.decodeIfPresent( BaseConfiguration.Quantization.self, forKey: CodingKeys.quantization) imageTokenId = try c.decodeIfPresent(Int.self, forKey: CodingKeys.imageTokenId) ?? 258_880 audioTokenId = try c.decodeIfPresent(Int.self, forKey: CodingKeys.audioTokenId) + videoTokenId = try c.decodeIfPresent(Int.self, forKey: CodingKeys.videoTokenId) boiTokenId = try c.decodeIfPresent(Int.self, forKey: CodingKeys.boiTokenId) ?? 255_999 eoiTokenId = try c.decodeIfPresent(Int.self, forKey: CodingKeys.eoiTokenId) visionSoftTokensPerImage = try c.decodeIfPresent(Int.self, forKey: CodingKeys.visionSoftTokensPerImage) ?? visionConfiguration.defaultOutputLength + // Default 64 = (24 patches / 3 pool kernel)^2 for the Swift port's + // 384x384 fixed video frame size, matching the processor's + // `video_seq_length` default. Keep these two values in sync when + // overriding either. + visionSoftTokensPerVideoFrame = + try c.decodeIfPresent(Int.self, forKey: CodingKeys.visionSoftTokensPerVideoFrame) + ?? 64 + // Default 4 caps the vision tower batch size when running video so + // peak Metal memory stays manageable on iPhone-class devices. Set + // higher (or to the full frame count) on M-series Macs for slightly + // faster prefill. + videoFrameChunkSize = + try c.decodeIfPresent(Int.self, forKey: CodingKeys.videoFrameChunkSize) ?? 4 tieWordEmbeddings = try c.decodeIfPresent(Bool.self, forKey: CodingKeys.tieWordEmbeddings) ?? textConfiguration.tieWordEmbeddings @@ -916,9 +1015,15 @@ final class Gemma4TextBackbone: Module { @ModuleInfo(key: "per_layer_projection_norm") var perLayerProjectionNorm: Gemma4RMSNormZeroShift? + static func isKVSharedOnlyLayer(_ layerIdx: Int, textConfig: Gemma4TextConfiguration) -> Bool { + guard textConfig.numKVSharedLayers > 0 else { return false } + return layerIdx >= textConfig.hiddenLayers - textConfig.numKVSharedLayers + } + init(_ config: Gemma4TextConfiguration) { self.config = config - self.firstKVSharedLayerIdx = config.hiddenLayers - config.numKVSharedLayers + let firstKVSharedLayerIdx = config.hiddenLayers - config.numKVSharedLayers + self.firstKVSharedLayerIdx = firstKVSharedLayerIdx self.embedScale = pow(Float(config.hiddenSize), 0.5) self.embedTokensPerLayerScale = pow(Float(max(config.hiddenSizePerLayerInput, 1)), 0.5) self._perLayerInputScale = rsqrt(MLXArray(2.0)) @@ -943,7 +1048,10 @@ final class Gemma4TextBackbone: Module { self._embedTokens.wrappedValue = Embedding( embeddingCount: config.vocabularySize, dimensions: config.hiddenSize) self._layers.wrappedValue = (0 ..< config.hiddenLayers).map { - Gemma4TextDecoderLayer(config: config, layerIdx: $0) + Gemma4TextDecoderLayer( + config: config, + layerIdx: $0, + kvSharedOnly: Self.isKVSharedOnlyLayer($0, textConfig: config)) } self._norm.wrappedValue = Gemma4RMSNormZeroShift( dimensions: config.hiddenSize, eps: config.rmsNormEps) @@ -1049,7 +1157,6 @@ final class Gemma4TextBackbone: Module { } let finalPerLayerInputs = projectPerLayerInputs(h0, perLayerInputs: processedPerLayerInputs) - let hasExplicitCache = cache != nil let localCache = cache ?? Array(repeating: nil as KVCache?, count: max(firstKVSharedLayerIdx, 1)) let fullMask: MLXFast.ScaledDotProductAttentionMaskMode @@ -1680,7 +1787,7 @@ private final class Gemma4VisionModel: Module { return (MLXArray(values, [batch, count, 2]), realCount) } - func callAsFunction(_ pixelValues: MLXArray) -> MLXArray { + func callAsFunction(_ pixelValues: MLXArray, outputLength: Int? = nil) -> MLXArray { let pixels = if pixelValues.ndim == 3 { expandedDimensions(pixelValues, axis: 0) @@ -1713,7 +1820,9 @@ private final class Gemma4VisionModel: Module { attentionMask = expandedDimensions(attentionMask, axis: 1) hiddenStates = encoder(hiddenStates, positions: patchPositions, mask: attentionMask) - hiddenStates = pooler(hiddenStates, patchPositions: patchPositions, validCount: realCount) + hiddenStates = pooler( + hiddenStates, patchPositions: patchPositions, validCount: realCount, + outputLength: outputLength) if let standardizationBias, let standardizationScale { hiddenStates = (hiddenStates - standardizationBias) * standardizationScale @@ -1738,6 +1847,622 @@ private final class Gemma4MultimodalEmbedder: Module, UnaryLayer { } } +// MARK: - Audio + +private final class Gemma4AudioRMSNorm: Module, UnaryLayer { + let eps: Float + @ModuleInfo var weight: MLXArray + + init(dimensions: Int, eps: Float = 1e-6) { + self.eps = eps + self._weight.wrappedValue = MLXArray.ones([dimensions]) + super.init() + } + + func callAsFunction(_ x: MLXArray) -> MLXArray { + MLXFast.rmsNorm(x, weight: weight, eps: eps) + } +} + +private final class Gemma4AudioClippableLinear: Module, UnaryLayer { + @ModuleInfo(key: "linear") var linear: Linear + @ModuleInfo(key: "input_min") var inputMin: MLXArray? + @ModuleInfo(key: "input_max") var inputMax: MLXArray? + @ModuleInfo(key: "output_min") var outputMin: MLXArray? + @ModuleInfo(key: "output_max") var outputMax: MLXArray? + let useClipping: Bool + + init(inFeatures: Int, outFeatures: Int, bias: Bool = false, useClipping: Bool = true) { + self.useClipping = useClipping + self._linear.wrappedValue = Linear(inFeatures, outFeatures, bias: bias) + if useClipping { + self._inputMin.wrappedValue = MLXArray(-Float.infinity) + self._inputMax.wrappedValue = MLXArray(Float.infinity) + self._outputMin.wrappedValue = MLXArray(-Float.infinity) + self._outputMax.wrappedValue = MLXArray(Float.infinity) + } + super.init() + } + + func callAsFunction(_ x: MLXArray) -> MLXArray { + let clippedInput: MLXArray + if let inputMin, let inputMax { + clippedInput = clip(x, min: inputMin, max: inputMax) + } else { + clippedInput = x + } + let projected = linear(clippedInput) + if let outputMin, let outputMax { + return clip(projected, min: outputMin, max: outputMax) + } + return projected + } +} + +/// LayerNorm without bias, matching `nn.LayerNorm(dims, bias=False)` in the Python model. +/// The checkpoint stores a single `weight` parameter at the `norm` key. +private final class Gemma4AudioLayerNorm: Module, UnaryLayer { + @ModuleInfo var weight: MLXArray + let eps: Float + + init(dimensions: Int, eps: Float = 1e-6) { + self.eps = eps + self._weight.wrappedValue = MLXArray.ones([dimensions]) + super.init() + } + + func callAsFunction(_ x: MLXArray) -> MLXArray { + let xFloat = x.asType(.float32) + let meanVal = MLX.mean(xFloat, axis: -1, keepDims: true) + let variance = MLX.mean((xFloat - meanVal).square(), axis: -1, keepDims: true) + let normalized = (xFloat - meanVal) * rsqrt(variance + eps) + return (normalized * weight.asType(.float32)).asType(x.dtype) + } +} + +private final class Gemma4SSCPConvBlock: Module { + let timeStride: Int = 2 + let padding: (Int, Int, Int, Int) = (1, 1, 1, 1) + + @ModuleInfo(key: "conv") var conv: Conv2d + @ModuleInfo(key: "norm") var norm: Gemma4AudioLayerNorm + + init(config: Gemma4AudioConfiguration, idx: Int) { + let inChannels = idx == 0 ? 1 : config.subsamplingConvChannels[idx - 1] + let outChannels = config.subsamplingConvChannels[idx] + + // Conv2d: MLX expects [B, H, W, C], weight [C_out, kH, kW, C_in] + self._conv.wrappedValue = Conv2d( + inputChannels: inChannels, + outputChannels: outChannels, + kernelSize: 3, + stride: 2, + padding: 0, + bias: false + ) + + self._norm.wrappedValue = Gemma4AudioLayerNorm( + dimensions: outChannels, eps: config.rmsNormEps) + super.init() + } + + func callAsFunction(_ x: MLXArray, mask: MLXArray) -> (MLXArray, MLXArray) { + // x: [B, T, F, C] (MLX channel-last) + // mask: [B, T] (True = invalid/padding) + + // Zero out invalid positions + var x = MLX.where( + expandedDimensions(expandedDimensions(mask, axis: -1), axis: -1), + MLXArray(0.0, dtype: x.dtype), x) + + // Manual padding on T and F dims + x = MLX.padded( + x, + widths: [ + .init((0, 0)), .init((padding.0, padding.1)), + .init((padding.2, padding.3)), .init((0, 0)), + ]) + + x = conv(x) // [B, T_out, F_out, C_out] + + // Downsample mask by time stride + let tOut = x.dim(1) + let downsampled = mask[0..., .stride(by: timeStride)] + let outputMask = downsampled[0..., .. (MLXArray, MLXArray) { + // audioMel: [B, T, F_in] + // Add channel dim: [B, T, F, 1] + var x = expandedDimensions(audioMel, axis: -1) + + var currentMask = mask + (x, currentMask) = layer0(x, mask: currentMask) + (x, currentMask) = layer1(x, mask: currentMask) + + // Flatten F*C -> [B, T, F*C] + let batchSize = x.dim(0) + let timeSteps = x.dim(1) + let freqBins = x.dim(2) + let channels = x.dim(3) + x = x.reshaped(batchSize, timeSteps, freqBins * channels) + + // Project to hidden_size + x = inputProjLinear(x) + + return (x, currentMask) + } +} + +private final class Gemma4ConformerFeedForward: Module { + let gradientClipping: Float + let residualWeight: Float + + @ModuleInfo(key: "pre_layer_norm") var preLayerNorm: Gemma4AudioRMSNorm + @ModuleInfo(key: "ffw_layer_1") var ffwLayer1: Gemma4AudioClippableLinear + @ModuleInfo(key: "ffw_layer_2") var ffwLayer2: Gemma4AudioClippableLinear + @ModuleInfo(key: "post_layer_norm") var postLayerNorm: Gemma4AudioRMSNorm + + init(config: Gemma4AudioConfiguration) { + self.gradientClipping = config.gradientClipping + self.residualWeight = config.residualWeight + + self._preLayerNorm.wrappedValue = Gemma4AudioRMSNorm(dimensions: config.hiddenSize) + self._ffwLayer1.wrappedValue = Gemma4AudioClippableLinear( + inFeatures: config.hiddenSize, outFeatures: config.hiddenSize * 4, + useClipping: config.useClippedLinears) + self._ffwLayer2.wrappedValue = Gemma4AudioClippableLinear( + inFeatures: config.hiddenSize * 4, outFeatures: config.hiddenSize, + useClipping: config.useClippedLinears) + self._postLayerNorm.wrappedValue = Gemma4AudioRMSNorm(dimensions: config.hiddenSize) + super.init() + } + + func callAsFunction(_ x: MLXArray) -> MLXArray { + let residual = x + var h = clip(x, min: -gradientClipping, max: gradientClipping) + h = preLayerNorm(h) + h = ffwLayer1(h) + h = silu(h) + h = ffwLayer2(h) + h = clip(h, min: -gradientClipping, max: gradientClipping) + h = postLayerNorm(h) + return residual + h * residualWeight + } +} + +private final class Gemma4AudioAttention: Module { + let numHeads: Int + let hiddenSize: Int + let headDim: Int + let chunkSize: Int + let maxFutureHorizon: Int + let maxPastHorizon: Int + let contextSize: Int + let invalidLogitsValue: Float + let softcap: Float + let qScale: Float + let kScale: Float + + @ModuleInfo(key: "relative_k_proj") var relativeKProj: Linear + @ParameterInfo(key: "per_dim_scale") var perDimScale: MLXArray + @ModuleInfo(key: "q_proj") var qProj: Gemma4AudioClippableLinear + @ModuleInfo(key: "k_proj") var kProj: Gemma4AudioClippableLinear + @ModuleInfo(key: "v_proj") var vProj: Gemma4AudioClippableLinear + @ModuleInfo(key: "post") var post: Gemma4AudioClippableLinear + + // Relative position embedding (inline) + // Note: relPosInvTimescales is NOT a model parameter — it's a computed constant. + // Store as [Float] to avoid MLX Module treating it as a loadable weight. + private let relPosNumHeads: Int + private let relPosHeadDim: Int + private let relPosMaxBackward: Int + private let relPosMaxForward: Int + private let relPosInvTimescalesData: [Float] + + init(config: Gemma4AudioConfiguration) { + self.numHeads = config.numAttentionHeads + self.hiddenSize = config.hiddenSize + self.headDim = config.hiddenSize / config.numAttentionHeads + self.chunkSize = config.attentionChunkSize + self.maxFutureHorizon = config.attentionContextRight + self.maxPastHorizon = max(0, config.attentionContextLeft - 1) + self.contextSize = chunkSize + maxPastHorizon + maxFutureHorizon + self.invalidLogitsValue = config.attentionInvalidLogitsValue + self.softcap = config.attentionLogitCap + + self.qScale = pow(Float(headDim), -0.5) / Foundation.log(2.0) + self.kScale = Foundation.log(1 + Foundation.exp(1.0)) / Foundation.log(2.0) + + self._relativeKProj.wrappedValue = Linear( + config.hiddenSize, config.numAttentionHeads * headDim, bias: false) + self._perDimScale.wrappedValue = MLXArray.zeros([headDim]) + self._qProj.wrappedValue = Gemma4AudioClippableLinear( + inFeatures: config.hiddenSize, outFeatures: numHeads * headDim, + useClipping: config.useClippedLinears) + self._kProj.wrappedValue = Gemma4AudioClippableLinear( + inFeatures: config.hiddenSize, outFeatures: numHeads * headDim, + useClipping: config.useClippedLinears) + self._vProj.wrappedValue = Gemma4AudioClippableLinear( + inFeatures: config.hiddenSize, outFeatures: numHeads * headDim, + useClipping: config.useClippedLinears) + self._post.wrappedValue = Gemma4AudioClippableLinear( + inFeatures: config.hiddenSize, outFeatures: config.hiddenSize, + useClipping: config.useClippedLinears) + + // Relative position embedding setup + self.relPosNumHeads = numHeads + self.relPosHeadDim = headDim + self.relPosMaxBackward = maxPastHorizon + self.relPosMaxForward = maxFutureHorizon + + let minTimescale: Float = 1.0 + let maxTimescale: Float = 10000.0 + let numTimescales = config.hiddenSize / 2 + let logTimescaleIncrement = + Foundation.log(maxTimescale / minTimescale) / Float(max(numTimescales - 1, 1)) + self.relPosInvTimescalesData = (0 ..< numTimescales).map { i in + minTimescale * Foundation.exp(Float(i) * (-logTimescaleIncrement)) + } + + super.init() + } + + private func padDim1(_ x: MLXArray, padLeft: Int, padRight: Int) -> MLXArray { + var widths = Array(repeating: IntOrPair((0, 0)), count: x.ndim) + widths[1] = IntOrPair((padLeft, padRight)) + return MLX.padded(x, widths: widths) + } + + private func convertToBlock(_ x: MLXArray) -> MLXArray { + // [B, T, ...] -> [B, num_blocks, chunk_size, ...] + let batchSize = x.dim(0) + let timeSteps = x.dim(1) + let rest = Array(x.shape.dropFirst(2)) + let numBlocks = (timeSteps + chunkSize - 1) / chunkSize + let padLen = numBlocks * chunkSize - timeSteps + var result = x + if padLen > 0 { + result = padDim1(result, padLeft: 0, padRight: padLen) + } + return result.reshaped([batchSize, numBlocks, chunkSize] + rest) + } + + private func extractBlockContext(_ x: MLXArray) -> MLXArray { + // [B, T, ...] -> [B, num_blocks, context_size, ...] + let padLeft = maxPastHorizon + let padRight = maxFutureHorizon + chunkSize - 1 + let padded = padDim1(x, padLeft: padLeft, padRight: padRight) + let tPadded = padded.dim(1) + let numBlocks = (tPadded - contextSize) / chunkSize + 1 + + // Build indices: starts[:, None] + offsets[None, :] + let starts = MLXArray( + stride(from: 0, to: numBlocks * chunkSize, by: chunkSize).map { + Int32($0) + }) + let offsets = MLXArray((0 ..< contextSize).map { Int32($0) }) + let indices = expandedDimensions(starts, axis: 1) + expandedDimensions(offsets, axis: 0) + // indices: [numBlocks, contextSize] + + // Gather using advanced indexing + // padded: [B, T_padded, ...rest] + // We need padded[:, indices] which gives [B, numBlocks, contextSize, ...rest] + return padded[0..., indices] + } + + private func relPosTimingSignal(_ position: MLXArray, dtype: DType) -> MLXArray { + let posFloat = position.asType(.float32) + let pos = expandedDimensions(posFloat, axis: -1) + let invTS = MLXArray(relPosInvTimescalesData).reshaped(1, 1, -1) + let scaledTime = pos * invTS + let signal = concatenated([sin(scaledTime), cos(scaledTime)], axis: -1) + return signal.asType(dtype) + } + + private func relPosRelativeShift( + _ termBD: MLXArray, batchSize: Int, numHeads: Int, numBlocks: Int, + blockSize: Int, contextSize: Int, maxSpanPlus1: Int + ) -> MLXArray { + let padAmount = (contextSize + 1) - maxSpanPlus1 + var shifted = MLX.padded( + termBD, + widths: [ + .init((0, 0)), .init((0, 0)), .init((0, 0)), .init((0, 0)), .init((0, padAmount)), + ]) + shifted = shifted.reshaped(batchSize, numHeads, numBlocks, blockSize * (contextSize + 1)) + shifted = shifted[0..., 0..., 0..., ..<(blockSize * contextSize)] + shifted = shifted.reshaped(batchSize, numHeads, numBlocks, blockSize, contextSize) + return shifted + } + + private func computeRelativePositionLogits(queries: MLXArray, keys: MLXArray) -> MLXArray { + // queries: [B, U, W, N, H], keys: [B, U, C, N, H] + let batchSize = queries.dim(0) + let numBlocks = queries.dim(1) + let blockSize = queries.dim(2) + let ctxSize = keys.dim(2) + + // Past-only relative positions [maxPastHorizon ... 0], matching Google's + // reference (`torch.arange(max_past_horizon, -1, -1)`). PR #192 originally + // used a symmetric [maxBackward ... -maxForward] span, which over-counts + // positions by maxFutureHorizon and misaligns relPosRelativeShift's pad + // math → semantically-wrong attention bias (audio not understood). + let posIndices = MLXArray( + stride(from: relPosMaxBackward, through: 0, by: -1).map { Int32($0) } + ).reshaped(1, -1) + let maxSpanPlus1 = posIndices.dim(1) + + var sinEmb = relPosTimingSignal(posIndices, dtype: queries.dtype) + sinEmb = relativeKProj(sinEmb.asType(relativeKProj.weight.dtype)) + sinEmb = sinEmb.reshaped(maxSpanPlus1, relPosNumHeads, relPosHeadDim) + sinEmb = sinEmb.asType(queries.dtype) + + let queriesP = queries.transposed(0, 3, 1, 2, 4) + let keysP = keys.transposed(0, 3, 1, 4, 2) + let termAC = queriesP.matmul(keysP) + + let sinEmbT = sinEmb.transposed(1, 2, 0) + let qReshaped = queriesP.reshaped( + batchSize, relPosNumHeads, numBlocks * blockSize, relPosHeadDim) + var termBD = qReshaped.matmul(sinEmbT).reshaped( + batchSize, relPosNumHeads, numBlocks, blockSize, maxSpanPlus1) + + termBD = relPosRelativeShift( + termBD, batchSize: batchSize, numHeads: relPosNumHeads, numBlocks: numBlocks, + blockSize: blockSize, contextSize: ctxSize, maxSpanPlus1: maxSpanPlus1) + + return termAC + termBD + } + + func callAsFunction( + _ hiddenStates: MLXArray, mask: MLXArray, causalValidMask: MLXArray + ) -> MLXArray { + let batchSize = hiddenStates.dim(0) + let timeSteps = hiddenStates.dim(1) + let qkvShape = [batchSize, timeSteps, numHeads, headDim] + + var q = qProj(hiddenStates).asType(.float32).reshaped(qkvShape) + var k = kProj(hiddenStates).asType(.float32).reshaped(qkvShape) + let v = vProj(hiddenStates).asType(.float32).reshaped(qkvShape) + + let pds = softplus(perDimScale) + q = q * (qScale * pds) + k = k * kScale + + let queryBlocks = convertToBlock(q) // [B, U, W, N, H] + let keyBlocks = extractBlockContext(k) // [B, U, C, N, H] + let valueBlocks = extractBlockContext(v) // [B, U, C, N, H] + let numBlocks = queryBlocks.dim(1) + + // Build validity condition + let validMask = logicalNot(mask) // True = valid + let extractedValid = extractBlockContext(validMask) // [B, U, C] + // condition: [B, 1, U, W, C] + let condition = + expandedDimensions(expandedDimensions(extractedValid, axis: 1), axis: 3) + * expandedDimensions( + expandedDimensions(expandedDimensions(causalValidMask, axis: 0), axis: 0), axis: 0) + + var logits = computeRelativePositionLogits(queries: queryBlocks, keys: keyBlocks) + logits = tanh(logits / softcap) * softcap + logits = MLX.where( + condition .> 0, logits, MLXArray(invalidLogitsValue, dtype: logits.dtype)) + + let probs = softmax(logits, axis: -1) + // context = einsum("bnuwc,bucnh->buwnh", probs, valueBlocks) + var context = einsum("bnuwc,bucnh->buwnh", probs, valueBlocks) + context = context.reshaped(batchSize, numBlocks * chunkSize, numHeads, headDim) + context = context[0..., .. [B, T, D] and post-project + context = context.reshaped(batchSize, timeSteps, numHeads * headDim) + return post(context) + } +} + +private final class Gemma4ConformerLightConv1d: Module { + let gradientClipping: Float + let causalPadding: Int + + @ModuleInfo(key: "pre_layer_norm") var preLayerNorm: Gemma4AudioRMSNorm + @ModuleInfo(key: "linear_start") var linearStart: Gemma4AudioClippableLinear + @ModuleInfo(key: "depthwise_conv1d") var depthwiseConv1d: Conv1d + @ModuleInfo(key: "conv_norm") var convNorm: Gemma4AudioRMSNorm + @ModuleInfo(key: "linear_end") var linearEnd: Gemma4AudioClippableLinear + + init(config: Gemma4AudioConfiguration) { + self.gradientClipping = config.gradientClipping + self.causalPadding = config.convKernelSize - 1 + + self._preLayerNorm.wrappedValue = Gemma4AudioRMSNorm( + dimensions: config.hiddenSize, eps: config.rmsNormEps) + self._linearStart.wrappedValue = Gemma4AudioClippableLinear( + inFeatures: config.hiddenSize, outFeatures: config.hiddenSize * 2, + useClipping: config.useClippedLinears) + // Depthwise conv1d: groups = hidden_size so weight shape is [out, kernel, 1] + self._depthwiseConv1d.wrappedValue = Conv1d( + inputChannels: config.hiddenSize, + outputChannels: config.hiddenSize, + kernelSize: config.convKernelSize, + stride: 1, + padding: 0, + groups: config.hiddenSize, + bias: false + ) + self._convNorm.wrappedValue = Gemma4AudioRMSNorm( + dimensions: config.hiddenSize, eps: config.rmsNormEps) + self._linearEnd.wrappedValue = Gemma4AudioClippableLinear( + inFeatures: config.hiddenSize, outFeatures: config.hiddenSize, + useClipping: config.useClippedLinears) + super.init() + } + + func callAsFunction(_ x: MLXArray) -> MLXArray { + let residual = x + + var h = preLayerNorm(x) + h = linearStart(h) + + // GLU: split in half along last dim and gate + let halfDim = h.dim(-1) / 2 + let x1 = h[0..., 0..., .. MLXArray { + var h = feedForward1(x) + + // Attention with pre/post norm and residual + let residual = h + h = clip(h, min: -gradientClipping, max: gradientClipping) + h = normPreAttn(h) + h = selfAttn(h, mask: mask, causalValidMask: causalValidMask) + h = clip(h, min: -gradientClipping, max: gradientClipping) + h = residual + normPostAttn(h) + + // Zero out invalid positions before lconv1d + let validityMask = expandedDimensions(logicalNot(mask), axis: -1).asType(h.dtype) + h = h * validityMask + + h = lconv1d(h) + h = feedForward2(h) + h = clip(h, min: -gradientClipping, max: gradientClipping) + return normOut(h) + } +} + +private final class Gemma4AudioEncoder: Module { + let config: Gemma4AudioConfiguration + + @ModuleInfo(key: "subsample_conv_projection") var subsampleConvProjection: + Gemma4SubSampleConvProjection + @ModuleInfo(key: "layers") var layers: [Gemma4ConformerBlock] + @ModuleInfo(key: "output_proj") var outputProj: Linear? + + init(config: Gemma4AudioConfiguration) { + self.config = config + self._subsampleConvProjection.wrappedValue = Gemma4SubSampleConvProjection(config: config) + self._layers.wrappedValue = (0 ..< config.numHiddenLayers).map { _ in + Gemma4ConformerBlock(config: config) + } + if let outputProjDims = config.outputProjDims { + self._outputProj.wrappedValue = Linear( + config.hiddenSize, outputProjDims, bias: true) + } + super.init() + } + + private func buildCausalValidMask() -> MLXArray { + let chunkSize = config.attentionChunkSize + let maxFutureHorizon = config.attentionContextRight + let maxPastHorizon = max(0, config.attentionContextLeft - 1) + let upperDiagonal = maxPastHorizon + maxFutureHorizon + let ctxSize = chunkSize + maxPastHorizon + maxFutureHorizon + + let lowerCausal = tril(MLXArray.ones([ctxSize, chunkSize])).transposed() + let upperCausal = tril( + MLXArray.ones([chunkSize, ctxSize]), + k: upperDiagonal) + let maskResult = (lowerCausal * upperCausal).asType(.bool) + return maskResult + } + + func callAsFunction(_ audioMel: MLXArray, audioMelMask: MLXArray) -> (MLXArray, MLXArray) { + var (audioEncodings, currentMask) = subsampleConvProjection(audioMel, mask: audioMelMask) + + let causalValidMask = buildCausalValidMask() + + for block in layers { + audioEncodings = block( + audioEncodings, mask: currentMask, causalValidMask: causalValidMask) + } + + if let outputProj { + audioEncodings = outputProj(audioEncodings) + } + + if currentMask.dim(1) != audioEncodings.dim(1) { + let targetLen = audioEncodings.dim(1) + currentMask = currentMask[0..., ..