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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//
|
| 2 |
+
// VibeVoicePipeline.swift
|
| 3 |
+
// VibeVoice CoreML Pipeline
|
| 4 |
+
//
|
| 5 |
+
// Auto-generated interface for VibeVoice TTS model
|
| 6 |
+
//
|
| 7 |
+
|
| 8 |
+
import Foundation
|
| 9 |
+
import CoreML
|
| 10 |
+
import Accelerate
|
| 11 |
+
|
| 12 |
+
/// VibeVoice TTS Pipeline for CoreML
|
| 13 |
+
public class VibeVoicePipeline {
|
| 14 |
+
|
| 15 |
+
// MARK: - Models
|
| 16 |
+
private var acousticEncoder: MLModel?
|
| 17 |
+
private var acousticDecoder: MLModel?
|
| 18 |
+
private var semanticEncoder: MLModel?
|
| 19 |
+
private var acousticConnector: MLModel?
|
| 20 |
+
private var semanticConnector: MLModel?
|
| 21 |
+
private var llm: MLModel?
|
| 22 |
+
private var diffusionHead: MLModel?
|
| 23 |
+
|
| 24 |
+
// MARK: - Configuration
|
| 25 |
+
public let sampleRate: Int = 24000
|
| 26 |
+
public let downsampleFactor: Int = 3200
|
| 27 |
+
public let latentDim: Int = 64
|
| 28 |
+
public let semanticDim: Int = 128
|
| 29 |
+
public let hiddenDim: Int = 1536
|
| 30 |
+
public let diffusionSteps: Int = 20
|
| 31 |
+
|
| 32 |
+
// MARK: - Initialization
|
| 33 |
+
|
| 34 |
+
public init(modelDirectory: URL, configuration: MLModelConfiguration = .init()) throws {
|
| 35 |
+
let config = configuration
|
| 36 |
+
config.computeUnits = .all
|
| 37 |
+
|
| 38 |
+
// Load models
|
| 39 |
+
acousticEncoder = try? MLModel(contentsOf: modelDirectory.appendingPathComponent("vibevoice_acoustic_encoder.mlpackage"), configuration: config)
|
| 40 |
+
acousticDecoder = try? MLModel(contentsOf: modelDirectory.appendingPathComponent("vibevoice_acoustic_decoder.mlpackage"), configuration: config)
|
| 41 |
+
semanticEncoder = try? MLModel(contentsOf: modelDirectory.appendingPathComponent("vibevoice_semantic_encoder.mlpackage"), configuration: config)
|
| 42 |
+
acousticConnector = try? MLModel(contentsOf: modelDirectory.appendingPathComponent("vibevoice_acoustic_connector.mlpackage"), configuration: config)
|
| 43 |
+
semanticConnector = try? MLModel(contentsOf: modelDirectory.appendingPathComponent("vibevoice_semantic_connector.mlpackage"), configuration: config)
|
| 44 |
+
llm = try? MLModel(contentsOf: modelDirectory.appendingPathComponent("vibevoice_llm.mlpackage"), configuration: config)
|
| 45 |
+
diffusionHead = try? MLModel(contentsOf: modelDirectory.appendingPathComponent("vibevoice_diffusion_head.mlpackage"), configuration: config)
|
| 46 |
+
|
| 47 |
+
guard acousticDecoder != nil, llm != nil, diffusionHead != nil else {
|
| 48 |
+
throw PipelineError.modelLoadFailed
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
// MARK: - Inference
|
| 53 |
+
|
| 54 |
+
/// Encode audio to acoustic latent representation.
|
| 55 |
+
/// Input must be fixed length: (1, 1, 24000) — 1 sec at 24 kHz; trim or pad before calling.
|
| 56 |
+
public func encodeAcoustic(_ audio: MLMultiArray) throws -> MLMultiArray {
|
| 57 |
+
guard let encoder = acousticEncoder else {
|
| 58 |
+
throw PipelineError.modelNotLoaded("acousticEncoder")
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
let input = try MLDictionaryFeatureProvider(dictionary: ["audio": audio])
|
| 62 |
+
let output = try encoder.prediction(from: input)
|
| 63 |
+
|
| 64 |
+
guard let latent = output.featureValue(for: "acoustic_latent")?.multiArrayValue else {
|
| 65 |
+
throw PipelineError.outputMissing("acoustic_latent")
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
return latent
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
/// Decode acoustic latent to audio waveform
|
| 72 |
+
public func decodeAcoustic(_ latent: MLMultiArray) throws -> MLMultiArray {
|
| 73 |
+
guard let decoder = acousticDecoder else {
|
| 74 |
+
throw PipelineError.modelNotLoaded("acousticDecoder")
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
let input = try MLDictionaryFeatureProvider(dictionary: ["acoustic_latent": latent])
|
| 78 |
+
let output = try decoder.prediction(from: input)
|
| 79 |
+
|
| 80 |
+
guard let audio = output.featureValue(for: "audio")?.multiArrayValue else {
|
| 81 |
+
throw PipelineError.outputMissing("audio")
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
return audio
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
/// Run LLM forward pass
|
| 88 |
+
public func runLLM(inputIds: MLMultiArray, attentionMask: MLMultiArray) throws -> (hiddenStates: MLMultiArray, logits: MLMultiArray) {
|
| 89 |
+
guard let model = llm else {
|
| 90 |
+
throw PipelineError.modelNotLoaded("llm")
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
let input = try MLDictionaryFeatureProvider(dictionary: [
|
| 94 |
+
"input_ids": inputIds,
|
| 95 |
+
"attention_mask": attentionMask
|
| 96 |
+
])
|
| 97 |
+
let output = try model.prediction(from: input)
|
| 98 |
+
|
| 99 |
+
guard let hiddenStates = output.featureValue(for: "hidden_states")?.multiArrayValue,
|
| 100 |
+
let logits = output.featureValue(for: "logits")?.multiArrayValue else {
|
| 101 |
+
throw PipelineError.outputMissing("hidden_states or logits")
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
return (hiddenStates, logits)
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
/// Single diffusion denoising step
|
| 108 |
+
public func diffusionStep(noisyLatent: MLMultiArray, timestep: Float, condition: MLMultiArray) throws -> MLMultiArray {
|
| 109 |
+
guard let head = diffusionHead else {
|
| 110 |
+
throw PipelineError.modelNotLoaded("diffusionHead")
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
let timestepArray = try MLMultiArray(shape: [1], dataType: .float32)
|
| 114 |
+
timestepArray[0] = NSNumber(value: timestep)
|
| 115 |
+
|
| 116 |
+
let input = try MLDictionaryFeatureProvider(dictionary: [
|
| 117 |
+
"noisy_latent": noisyLatent,
|
| 118 |
+
"timestep": timestepArray,
|
| 119 |
+
"condition": condition
|
| 120 |
+
])
|
| 121 |
+
let output = try head.prediction(from: input)
|
| 122 |
+
|
| 123 |
+
guard let prediction = output.featureValue(for: "prediction")?.multiArrayValue else {
|
| 124 |
+
throw PipelineError.outputMissing("prediction")
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
return prediction
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
// MARK: - Errors
|
| 131 |
+
|
| 132 |
+
public enum PipelineError: Error {
|
| 133 |
+
case modelLoadFailed
|
| 134 |
+
case modelNotLoaded(String)
|
| 135 |
+
case outputMissing(String)
|
| 136 |
+
case invalidInput(String)
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
// MARK: - DPM-Solver Scheduler
|
| 141 |
+
|
| 142 |
+
/// Simple DPM-Solver scheduler for diffusion inference
|
| 143 |
+
public class DPMSolverScheduler {
|
| 144 |
+
|
| 145 |
+
public let numTrainTimesteps: Int
|
| 146 |
+
public let numInferenceSteps: Int
|
| 147 |
+
public let betaSchedule: String
|
| 148 |
+
|
| 149 |
+
private var timesteps: [Float] = []
|
| 150 |
+
private var alphasCumprod: [Float] = []
|
| 151 |
+
|
| 152 |
+
public init(numTrainTimesteps: Int = 1000, numInferenceSteps: Int = 20, betaSchedule: String = "cosine") {
|
| 153 |
+
self.numTrainTimesteps = numTrainTimesteps
|
| 154 |
+
self.numInferenceSteps = numInferenceSteps
|
| 155 |
+
self.betaSchedule = betaSchedule
|
| 156 |
+
|
| 157 |
+
setupScheduler()
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
private func setupScheduler() {
|
| 161 |
+
// Compute betas based on schedule
|
| 162 |
+
var betas: [Float] = []
|
| 163 |
+
|
| 164 |
+
if betaSchedule == "cosine" {
|
| 165 |
+
let steps = numTrainTimesteps + 1
|
| 166 |
+
for i in 0..<steps {
|
| 167 |
+
let t = Float(i) / Float(numTrainTimesteps)
|
| 168 |
+
let alphaBar = cos((t + 0.008) / 1.008 * Float.pi / 2).pow(2)
|
| 169 |
+
betas.append(min(1 - alphaBar, 0.999))
|
| 170 |
+
}
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
// Compute alphas_cumprod
|
| 174 |
+
var alphaCumprod: Float = 1.0
|
| 175 |
+
for beta in betas {
|
| 176 |
+
alphaCumprod *= (1 - beta)
|
| 177 |
+
alphasCumprod.append(alphaCumprod)
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
// Compute timesteps for inference
|
| 181 |
+
let stepRatio = Float(numTrainTimesteps) / Float(numInferenceSteps)
|
| 182 |
+
timesteps = (0..<numInferenceSteps).map { Float(numTrainTimesteps - 1) - Float($0) * stepRatio }
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
public func getTimesteps() -> [Float] {
|
| 186 |
+
return timesteps
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
public func step(modelOutput: MLMultiArray, timestep: Float, sample: MLMultiArray) -> MLMultiArray {
|
| 190 |
+
// Simplified DDPM step - full implementation would use DPM-Solver++
|
| 191 |
+
let timestepIdx = Int(timestep)
|
| 192 |
+
let alphaProd = alphasCumprod[min(timestepIdx, alphasCumprod.count - 1)]
|
| 193 |
+
let alphaProdPrev = timestepIdx > 0 ? alphasCumprod[timestepIdx - 1] : 1.0
|
| 194 |
+
|
| 195 |
+
// v_prediction to epsilon
|
| 196 |
+
let sqrtAlphaProd = sqrt(alphaProd)
|
| 197 |
+
let sqrtOneMinusAlphaProd = sqrt(1 - alphaProd)
|
| 198 |
+
|
| 199 |
+
// Compute predicted original sample
|
| 200 |
+
// For v_prediction: x0 = sqrt(alpha) * sample - sqrt(1-alpha) * v
|
| 201 |
+
// Then get previous sample
|
| 202 |
+
|
| 203 |
+
// Simplified: just return model output scaled (placeholder)
|
| 204 |
+
return modelOutput
|
| 205 |
+
}
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
extension Float {
|
| 209 |
+
func pow(_ exponent: Float) -> Float {
|
| 210 |
+
return Foundation.pow(self, exponent)
|
| 211 |
+
}
|
| 212 |
+
}
|