Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Availability Annotations #18

Merged
merged 1 commit into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import PackageDescription
let package = Package(
name: "stable-diffusion",
platforms: [
.macOS(.v13),
.iOS(.v16),
],
.macOS(.v11),
.iOS(.v14),
],
products: [
.library(
name: "StableDiffusion",
Expand Down
1 change: 1 addition & 0 deletions swift/StableDiffusion/pipeline/Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import CoreML
import Accelerate

/// A decoder model which produces RGB images from latent samples
@available(iOS 16.2, macOS 13.1, *)
public struct Decoder: ResourceManaging {

/// VAE decoder model
Expand Down
1 change: 1 addition & 0 deletions swift/StableDiffusion/pipeline/ManagedMLModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import CoreML
///
/// It will automatically load a model into memory when needed or requested
/// It allows one to request to unload the model from memory
@available(iOS 16.2, macOS 13.1, *)
public final class ManagedMLModel: ResourceManaging {

/// The location of the model
Expand Down
1 change: 1 addition & 0 deletions swift/StableDiffusion/pipeline/Random.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import CoreML
/// This implementation matches:
/// [NumPy's older randomkit.c](https://github.com/numpy/numpy/blob/v1.0/numpy/random/mtrand/randomkit.c)
///
@available(iOS 16.2, macOS 13.1, *)
struct NumPyRandomSource: RandomNumberGenerator {

struct State {
Expand Down
1 change: 1 addition & 0 deletions swift/StableDiffusion/pipeline/SafetyChecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import CoreML
import Accelerate

/// Image safety checking model
@available(iOS 16.2, macOS 13.1, *)
public struct SafetyChecker: ResourceManaging {

/// Safety checking Core ML model
Expand Down
1 change: 1 addition & 0 deletions swift/StableDiffusion/pipeline/SampleTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Foundation
/// print(String(format: "mean: %.2f, var: %.2f",
/// timer.mean, timer.variance))
/// ```
@available(iOS 16.2, macOS 13.1, *)
public final class SampleTimer: Codable {
var startTime: CFAbsoluteTime?
var sum: Double = 0.0
Expand Down
2 changes: 2 additions & 0 deletions swift/StableDiffusion/pipeline/Scheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import CoreML
/// [Hugging Face Diffusers PNDMScheduler](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_pndm.py)
///
/// It uses the pseudo linear multi-step (PLMS) method only, skipping pseudo Runge-Kutta (PRK) steps
@available(iOS 16.2, macOS 13.1, *)
public final class Scheduler {
/// Number of diffusion steps performed during training
public let trainStepCount: Int
Expand Down Expand Up @@ -224,6 +225,7 @@ public final class Scheduler {
}
}

@available(iOS 16.2, macOS 13.1, *)
extension Scheduler {
/// How to map a beta range to a sequence of betas to step over
public enum BetaSchedule {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import Foundation
import CoreML

@available(iOS 16.2, macOS 13.1, *)
public extension StableDiffusionPipeline {

struct ResourceURLs {
Expand Down
2 changes: 2 additions & 0 deletions swift/StableDiffusion/pipeline/StableDiffusionPipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import CoreGraphics
///
/// This implementation matches:
/// [Hugging Face Diffusers Pipeline](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py)
@available(iOS 16.2, macOS 13.1, *)
public struct StableDiffusionPipeline: ResourceManaging {

/// Model to generate embeddings for tokenized input text
Expand Down Expand Up @@ -271,6 +272,7 @@ public struct StableDiffusionPipeline: ResourceManaging {

}

@available(iOS 16.2, macOS 13.1, *)
extension StableDiffusionPipeline {
/// Sampling progress details
public struct Progress {
Expand Down
1 change: 1 addition & 0 deletions swift/StableDiffusion/pipeline/TextEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Foundation
import CoreML

/// A model for encoding text
@available(iOS 16.2, macOS 13.1, *)
public struct TextEncoder: ResourceManaging {

/// Text tokenizer
Expand Down
1 change: 1 addition & 0 deletions swift/StableDiffusion/pipeline/Unet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Foundation
import CoreML

/// U-Net noise prediction model for stable diffusion
@available(iOS 16.2, macOS 13.1, *)
public struct Unet: ResourceManaging {

/// Model used to predict noise residuals given an input, diffusion time step, and conditional embedding
Expand Down
1 change: 1 addition & 0 deletions swift/StableDiffusion/tokenizer/BPETokenizer+Reading.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import Foundation

@available(iOS 16.2, macOS 13.1, *)
extension BPETokenizer {
enum FileReadError: Error {
case invalidMergeFileLine(Int)
Expand Down
2 changes: 2 additions & 0 deletions swift/StableDiffusion/tokenizer/BPETokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import Foundation

/// A tokenizer based on byte pair encoding.
@available(iOS 16.2, macOS 13.1, *)
public struct BPETokenizer {
/// A dictionary that maps pairs of tokens to the rank/order of the merge.
let merges: [TokenPair : Int]
Expand Down Expand Up @@ -166,6 +167,7 @@ public struct BPETokenizer {
}
}

@available(iOS 16.2, macOS 13.1, *)
extension BPETokenizer {

/// A hashable tuple of strings
Expand Down
8 changes: 7 additions & 1 deletion swift/StableDiffusionCLI/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Foundation
import StableDiffusion
import UniformTypeIdentifiers

@available(iOS 16.2, macOS 13.1, *)
struct StableDiffusionSample: ParsableCommand {

static let configuration = CommandConfiguration(
Expand Down Expand Up @@ -176,6 +177,7 @@ enum RunError: Error {
case saving(String)
}

@available(iOS 16.2, macOS 13.1, *)
enum ComputeUnits: String, ExpressibleByArgument, CaseIterable {
case all, cpuAndGPU, cpuOnly, cpuAndNeuralEngine
var asMLComputeUnits: MLComputeUnits {
Expand All @@ -188,4 +190,8 @@ enum ComputeUnits: String, ExpressibleByArgument, CaseIterable {
}
}

StableDiffusionSample.main()
if #available(iOS 16.2, macOS 13.1, *) {
StableDiffusionSample.main()
} else {
print("Unsupported OS")
}
1 change: 1 addition & 0 deletions swift/StableDiffusionTests/StableDiffusionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import XCTest
import CoreML
@testable import StableDiffusion

@available(iOS 16.2, macOS 13.1, *)
final class StableDiffusionTests: XCTestCase {

var vocabFileInBundleURL: URL {
Expand Down