From 616255f8e3b4e1cda055fe1fbd065e99c7889df0 Mon Sep 17 00:00:00 2001 From: Pedro Cuenca Date: Sun, 11 Dec 2022 16:36:39 +0100 Subject: [PATCH] Unify seed type to UInt32. Otherwise the app may crash when attempting to use a random Int as a seed. --- .../StableDiffusion/pipeline/StableDiffusionPipeline.swift | 6 +++--- swift/StableDiffusionCLI/main.swift | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/swift/StableDiffusion/pipeline/StableDiffusionPipeline.swift b/swift/StableDiffusion/pipeline/StableDiffusionPipeline.swift index bc888cc..e33f57b 100644 --- a/swift/StableDiffusion/pipeline/StableDiffusionPipeline.swift +++ b/swift/StableDiffusion/pipeline/StableDiffusionPipeline.swift @@ -68,7 +68,7 @@ public struct StableDiffusionPipeline { prompt: String, imageCount: Int = 1, stepCount: Int = 50, - seed: Int = 0, + seed: UInt32 = 0, disableSafety: Bool = false, progressHandler: (Progress) -> Bool = { _ in true } ) throws -> [CGImage?] { @@ -140,11 +140,11 @@ public struct StableDiffusionPipeline { return try decodeToImages(latents, disableSafety: disableSafety) } - func generateLatentSamples(_ count: Int, stdev: Float, seed: Int) -> [MLShapedArray] { + func generateLatentSamples(_ count: Int, stdev: Float, seed: UInt32) -> [MLShapedArray] { var sampleShape = unet.latentSampleShape sampleShape[0] = 1 - var random = NumPyRandomSource(seed: UInt32(seed)) + var random = NumPyRandomSource(seed: seed) let samples = (0..( converting: random.normalShapedArray(sampleShape, mean: 0.0, stdev: Double(stdev))) diff --git a/swift/StableDiffusionCLI/main.swift b/swift/StableDiffusionCLI/main.swift index 6b09af6..6175978 100644 --- a/swift/StableDiffusionCLI/main.swift +++ b/swift/StableDiffusionCLI/main.swift @@ -47,7 +47,7 @@ struct StableDiffusionSample: ParsableCommand { var outputPath: String = "./" @Option(help: "Random seed") - var seed: Int = 93 + var seed: UInt32 = 93 @Option(help: "Compute units to load model with {all,cpuOnly,cpuAndGPU,cpuAndNeuralEngine}") var computeUnits: ComputeUnits = .all