import Foundation import NNC public enum ModelVersion: String, Codable { case v1 = "v1" case v2 = "v2" case kandinsky21 = "kandinsky2.1" case sdxlBase = "sdxl_base_v0.9" case sdxlRefiner = "sdxl_refiner_v0.9" case ssd1b = "ssd_1b" case svdI2v = "svd_i2v" case wurstchenStageC = "wurstchen_v3.0_stage_c" case wurstchenStageB = "wurstchen_v3.0_stage_b" case sd3 = "sd3" case pixart = "pixart" case auraflow = "auraflow" case flux1 = "flux1" case sd3Large = "sd3_large" case hunyuanVideo = "hunyuan_video" case wan21_1_3b = "wan_v2.1_1.3b" case wan21_14b = "wan_v2.1_14b" case hiDreamI1 = "hidream_i1" case hiDreamO1 = "hidream_o1" case qwenImage = "qwen_image" case qwenImage2_1 = "qwen_image_2.1" case wan22_5b = "wan_v2.2_5b" case zImage = "z_image" case ernieImage = "ernie_image" case flux2 = "flux2" case flux2_9b = "flux2_9b" case flux2_4b = "flux2_4b" case cosmos2_5_2b = "cosmos2.5_2b" case ideogram4 = "ideogram_4" case krea2 = "krea_2" case ltx2 = "ltx2" case ltx2_3 = "ltx2.3" case seedvr2_3b = "seedvr2_3b" case seedvr2_7b = "seedvr2_7b" case longcatVideoAvatar1_5 = "longcat_video_avatar_v1.5" case minimaxH3 = "minimax_h3" } public enum TextEncoderVersion: String, Codable { case chatglm3_6b = "chatglm3_6b" } public enum ImageEncoderVersion: String, Codable { case clipL14_336 = "clip_l14_336" case openClipH14 = "open_clip_h14" case eva02L14_336 = "eva02_l14_336" case siglipL27_384 = "siglip_l27_384" case siglip2L27_512 = "siglip2_l27_512" } public enum AlternativeDecoderVersion: String, Codable { case transparent = "transparent" } public enum SamplerModifier: String, Codable { case none = "none" case inpainting = "inpainting" // More technically, this means the reference image is concatenated by the channel. case depth = "depth" case editing = "editing" case double = "double" case canny = "canny" case kontext = "kontext" // More technically, this means we concatenate tokens. case qwenimageEditPlus = "qwenimage_edit_plus" case qwenimageLayered = "qwenimage_layered" case qwenimageEdit2511 = "qwenimage_edit_2511" case kontextKv = "kontext_kv" // KV cache enabled. case fl2va = "fl2va" case ref2va = "ref2va" } public struct LoRAConfiguration: Equatable, Codable { public enum Mode: String, Codable { case all case base case refiner } public var file: String public var weight: Float public var version: ModelVersion public var isLoHa: Bool public var modifier: SamplerModifier public var mode: Mode public init( file: String, weight: Float, version: ModelVersion, isLoHa: Bool, modifier: SamplerModifier, mode: Mode ) { self.file = file self.weight = weight self.version = version self.isLoHa = isLoHa self.modifier = modifier self.mode = mode } } public struct DeviceProperties { public var isFreadPreferred: Bool public var memoryCapacity: MemoryCapacity public var isNHWCPreferred: Bool public var cacheUri: URL public var isPartialOffloadPreferred: Bool public init( isFreadPreferred: Bool, memoryCapacity: MemoryCapacity, isNHWCPreferred: Bool, cacheUri: URL, isPartialOffloadPreferred: Bool ) { self.isFreadPreferred = isFreadPreferred self.memoryCapacity = memoryCapacity self.isNHWCPreferred = isNHWCPreferred self.cacheUri = cacheUri self.isPartialOffloadPreferred = isPartialOffloadPreferred } } public struct SamplerOutput { public var x: DynamicGraph.Tensor public var unets: [UNet?] init(x: DynamicGraph.Tensor, unets: [UNet?]) { self.x = x self.unets = unets } } @inlinable public func isNaN(_ x: Tensor) -> Bool { return x.isNaN } func clipDenoised(_ x: DynamicGraph.Tensor) -> DynamicGraph.Tensor { let x = x.clamped(-2...2) let shape = x.shape let y = x.rawValue.toCPU() var values = [Float]() for i in 0..( filters: Int, sigma: Float, size: Int, input: DynamicGraph.Tensor ) -> Model { let model = Convolution( groups: filters, filters: filters, filterSize: [size, size], noBias: true, hint: Hint( stride: [1, 1], border: Hint.Border(begin: [size / 2, size / 2], end: [size / 2, size / 2])), format: .OIHW) model.compile(inputs: input) var weight = Tensor(.CPU, .NCHW(filters, 1, size, size)) var sum: Float = 0 for i in 0.. { associatedtype FloatType: TensorNumeric & BinaryFloatingPoint associatedtype UNet: UNetProtocol where UNet.FloatType == FloatType var filePath: String { get } var modifier: SamplerModifier { get } var version: ModelVersion { get } var audioShiftRatio: Float { get } var upcastAttention: Bool { get } var usesFlashAttention: UseFlashAttention { get } var usesSolAttention: Bool { get } var externalOnDemand: Bool { get } var injectControls: Bool { get } var injectT2IAdapters: Bool { get } var injectIPAdapterLengths: [Int] { get } var injectAttentionKV: Bool { get } var lora: [LoRAConfiguration] { get } var tiledDiffusion: TiledConfiguration { get } var teaCache: TeaCacheConfiguration { get } var isGuidanceEmbedEnabled: Bool { get } func sample( _ x_T: DynamicGraph.Tensor, unets: [UNet?], sample: DynamicGraph.Tensor?, conditionImage: DynamicGraph.Tensor?, referenceImages: [DynamicGraph.Tensor], referenceAudios: [DynamicGraph.Tensor], mask: DynamicGraph.Tensor?, negMask: DynamicGraph.Tensor?, conditioning c: [DynamicGraph.Tensor], tokenLengthUncond: Int, tokenLengthCond: Int, extraProjection: DynamicGraph.Tensor?, injectedControls: [( model: ControlModel, hints: [([DynamicGraph.Tensor], Float)] )], textGuidanceScale: Float, imageGuidanceScale: Float, guidanceEmbed: Float, startStep: (integral: Int, fractional: Float), endStep: (integral: Int, fractional: Float), originalSize: (width: Int, height: Int), cropTopLeft: (top: Int, left: Int), targetSize: (width: Int, height: Int), aestheticScore: Float, negativeOriginalSize: (width: Int, height: Int), negativeAestheticScore: Float, zeroNegativePrompt: Bool, refiner: Refiner?, fpsId: Int, motionBucketId: Int, condAug: Float, startFrameCfg: Float, sharpness: Float, sampling: Sampling, cancellation: (@escaping () -> Void) -> Void, feedback: (Int, Tensor?) -> Bool ) -> Result, Error> // For most, this is straightforward, just multiple the steps by the strength. But for Euler A // or DPM++ 2M Karras, it is not as obvious. Particularly for Karras' schedule, the best we // can do is to find the closest timestep and startStep, and alphaCumprod, and go from there. func timestep(for strength: Float, sampling: Sampling) -> ( timestep: Float, startStep: Float, roundedDownStartStep: Int, roundedUpStartStep: Int ) // Whether we need to scale the noise at a particular intermediate step. func noiseScaleFactor(at step: Float, sampling: Sampling) -> Float // Whether we need to scale the sample at a particular intermediate step. func sampleScaleFactor(at step: Float, sampling: Sampling) -> Float } extension Sampler { public var suffix: String { teaCache.suffix(for: version) } public func sampleAdd( _ sample: DynamicGraph.Tensor, noise: DynamicGraph.Tensor, scale: (sample: Float, noise: Float), version: ModelVersion ) -> DynamicGraph.Tensor { Diffusion.sampleAdd( sample, noise: noise, scale: scale, version: version, audioShiftRatio: audioShiftRatio) } public func sampleScaledAdd( _ left: DynamicGraph.Tensor, _ right: DynamicGraph.Tensor, sigma: (now: Float, next: Float), version: ModelVersion, scale: (_ sigma: (now: Float, next: Float)) -> (left: Float, right: Float) ) -> DynamicGraph.Tensor { Diffusion.sampleScaledAdd( left, right, sigma: sigma, version: version, audioShiftRatio: audioShiftRatio, scale: scale) } } // Add noise to a clean sample. H3's RF audio and video use different noise levels. public func sampleAdd( _ sample: DynamicGraph.Tensor, noise: DynamicGraph.Tensor, scale: (sample: Float, noise: Float), version: ModelVersion, audioShiftRatio: Float ) -> DynamicGraph.Tensor { var result = Functional.add( left: sample, right: noise, leftScalar: scale.sample, rightScalar: scale.noise) guard version == .minimaxH3 else { return result } let shape = sample.shape precondition(shape.count == 4 && shape[3] == 24 && shape == noise.shape) let audioHeight = MiniMaxH3AudioHeight(videoLatentFrames: shape[0], latentWidth: shape[2]) let videoHeight = shape[1] - audioHeight precondition(videoHeight > 0) let audioSigma = MiniMaxH3AudioSigma(forVideoSigma: scale.noise, audioShiftRatio: audioShiftRatio) result[0..( _ left: DynamicGraph.Tensor, _ right: DynamicGraph.Tensor, sigma: (now: Float, next: Float), version: ModelVersion, audioShiftRatio: Float, scale: (_ sigma: (now: Float, next: Float)) -> (left: Float, right: Float) ) -> DynamicGraph.Tensor { let videoScale = scale(sigma) var result = Functional.add( left: left, right: right, leftScalar: videoScale.left, rightScalar: videoScale.right) guard version == .minimaxH3 else { return result } let shape = left.shape precondition(shape.count == 4 && shape[3] == 24 && shape == right.shape) let audioHeight = MiniMaxH3AudioHeight(videoLatentFrames: shape[0], latentWidth: shape[2]) let videoHeight = shape[1] - audioHeight precondition(videoHeight > 0) let audioScale = scale( ( now: MiniMaxH3AudioSigma(forVideoSigma: sigma.now, audioShiftRatio: audioShiftRatio), next: MiniMaxH3AudioSigma(forVideoSigma: sigma.next, audioShiftRatio: audioShiftRatio) )) result[0.. Bool { guard version != .svdI2v else { return (textGuidanceScale - 1).magnitude > 1e-3 || (startFrameCfg - 1).magnitude > 1e-3 } guard version != .longcatVideoAvatar1_5 else { // Only the distilled no-CFG path is wired; the triple-guidance base path is not implemented. return false } guard modifier != .editing else { // If both are 1, no cfg. if (textGuidanceScale - 1).magnitude <= 1e-3 && (imageGuidanceScale - 1).magnitude <= 1e-3 { return false } return true } return (textGuidanceScale - 1).magnitude > 1e-3 } public func cfgChannelsAndInputChannels( channels: Int, conditionShape: TensorShape?, isCfgEnabled: Bool, textGuidanceScale: Float, imageGuidanceScale: Float, version: ModelVersion, modifier: SamplerModifier ) -> (Int, Int) { let cfgChannels: Int let inChannels: Int if version == .svdI2v { cfgChannels = 1 inChannels = channels * 2 } else { switch modifier { case .inpainting, .depth, .canny: cfgChannels = isCfgEnabled ? 2 : 1 inChannels = channels + (conditionShape?[3] ?? 0) case .editing: if isCfgEnabled { // If they are the same, it degrades to guidance * (etCond - etAllUncond) + etAllUncond if (textGuidanceScale - imageGuidanceScale).magnitude <= 1e-3 { cfgChannels = 2 } else { cfgChannels = 3 } } else { cfgChannels = 1 } inChannels = channels * 2 case .double: cfgChannels = isCfgEnabled ? 2 : 1 inChannels = channels * 2 case .none, .kontext, .kontextKv, .qwenimageEditPlus, .qwenimageEdit2511, .fl2va, .ref2va, .qwenimageLayered: cfgChannels = isCfgEnabled ? 2 : 1 inChannels = channels } } return (cfgChannels, inChannels) } func updateCfgInputAndConditions( xIn: inout DynamicGraph.Tensor, conditions c: inout [DynamicGraph.Tensor], conditionImage: DynamicGraph.Tensor?, batchSize: Int, startHeight: Int, startWidth: Int, channels: Int, isCfgEnabled: Bool, textGuidanceScale: Float, modifier: SamplerModifier ) { let graph = xIn.graph switch modifier { case .inpainting, .depth, .canny: if let conditionImage = conditionImage { let shape = conditionImage.shape for i in stride(from: 0, to: batchSize, by: shape[0]) { xIn[ i..<(i + shape[0]), 0.. Bool { switch version { case .auraflow, .flux1, .hiDreamI1, .hiDreamO1, .kandinsky21, .pixart, .sd3, .sd3Large, .sdxlBase, .sdxlRefiner, .ssd1b, .v1, .v2, .wurstchenStageB, .wurstchenStageC, .qwenImage, .qwenImage2_1, .zImage, .ernieImage, .flux2, .flux2_9b, .flux2_4b, .cosmos2_5_2b, .ideogram4, .krea2: return true case .hunyuanVideo, .svdI2v, .wan21_14b, .wan21_1_3b, .wan22_5b, .ltx2, .ltx2_3, .seedvr2_3b, .seedvr2_7b, .longcatVideoAvatar1_5, .minimaxH3: return false } } func applyCfg( etOut: DynamicGraph.Tensor, blur: Model?, batchSize: Int, startHeight: Int, startWidth: Int, channels: Int, isCfgEnabled: Bool, textGuidanceScale: Float, imageGuidanceScale: Float, alpha: Float, modifier: SamplerModifier, step: Int, isBatchEnabled: Bool, cfgZeroStar: CfgZeroStarConfiguration ) -> DynamicGraph.Tensor { let et: DynamicGraph.Tensor if isCfgEnabled { var etUncond = etOut[0.. let squaredSum: DynamicGraph.Tensor let etUncondF32 = DynamicGraph.Tensor(from: etUncond) let etCondF32 = DynamicGraph.Tensor(from: etCond) if isBatchEnabled { dotProduct = (etUncondF32 .* etCondF32).reduced( .sum, axis: [1, 2, 3]) squaredSum = (etUncondF32 .* etUncondF32).reduced(.sum, axis: [1, 2, 3]) + 1e-8 } else { dotProduct = (etUncondF32 .* etCondF32).reduced( .sum, axis: [0, 1, 2, 3]) squaredSum = (etUncondF32 .* etUncondF32).reduced( .sum, axis: [0, 1, 2, 3]) + 1e-8 } let stStar = dotProduct ./ squaredSum etUncond = DynamicGraph.Tensor(from: etUncondF32 .* stStar) } // This handles the case text guidance scale == 0 and text guidance == image guidance. et = etUncond + imageGuidanceScale * (etCond - etUncond) } else { let etAllUncond = etOut[ (batchSize * 2)..<(batchSize * 3), 0..(from: etUncond) let etCondF32 = DynamicGraph.Tensor(from: etCond) var etAllUncondF32 = DynamicGraph.Tensor(from: etAllUncond) let dotProduct = (etAllUncondF32 .* etUncondF32).reduced(.sum, axis: [1, 2, 3]) let squaredSum = (etAllUncondF32 .* etAllUncondF32).reduced( .sum, axis: [1, 2, 3]) let stStar = dotProduct ./ squaredSum etAllUncondF32 = etAllUncondF32 .* stStar et = DynamicGraph.Tensor( from: etAllUncondF32 + textGuidanceScale * (etCondF32 - etUncondF32) + imageGuidanceScale * (etUncondF32 - etAllUncondF32)) } else { et = etAllUncond + textGuidanceScale * (etCond - etUncond) + imageGuidanceScale * (etUncond - etAllUncond) } /* let etUncondF32 = DynamicGraph.Tensor(from: etUncond) let etCondF32 = DynamicGraph.Tensor(from: etCond) let etAllUncondF32 = DynamicGraph.Tensor(from: etOut[ (batchSize * 2)..<(batchSize * 3), 0..(from: etAllUncondF32 + imageGuidanceScale * (predText - etAllUncondF32)) */ } } else { if cfgZeroStar.isEnabled { let dotProduct: DynamicGraph.Tensor let squaredSum: DynamicGraph.Tensor let etUncondF32 = DynamicGraph.Tensor(from: etUncond) let etCondF32 = DynamicGraph.Tensor(from: etCond) if isBatchEnabled { // Batch should be independent to the batch dimension. dotProduct = (etUncondF32 .* etCondF32).reduced( .sum, axis: [1, 2, 3]) squaredSum = (etUncondF32 .* etUncondF32).reduced(.sum, axis: [1, 2, 3]) + 1e-8 } else { dotProduct = (etUncondF32 .* etCondF32).reduced( .sum, axis: [0, 1, 2, 3]) squaredSum = (etUncondF32 .* etUncondF32).reduced( .sum, axis: [0, 1, 2, 3]) + 1e-8 } let stStar = dotProduct ./ squaredSum etUncond = DynamicGraph.Tensor(from: etUncondF32 .* stStar) } et = etUncond + textGuidanceScale * (etCond - etUncond) /* let etNorm = et.reduced(.norm2, axis: [3]) let etCondNorm = etCond.reduced(.norm2, axis: [3]) et = (etCondNorm ./ etNorm) .* et */ } } else { var etOut = etOut if channels < etOut.shape[3] { etOut = etOut[0..