Skip to content

Commit

Permalink
ImageController: avoid creating pipeline twice
Browse files Browse the repository at this point in the history
Fixes #274, Fixes #282
  • Loading branch information
godly-devotion committed Jul 20, 2023
1 parent 0d232e1 commit 4fe125a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
13 changes: 5 additions & 8 deletions Mochi Diffusion/Support/ImageController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,22 @@ final class ImageController: ObservableObject {
controlNet = model.controlNet
currentControlNets = []

loadModel()
loadPipeline()
}
}

@Published
var currentControlNets: [SDControlNet] = [] {
didSet {
loadModel()
}
}
var currentControlNets: [SDControlNet] = []

private func loadModel() {
private func loadPipeline() {
guard let model = currentModel else {
return
}

Task {
logger.info("Started loading model: \"\(self.modelName)\"")
do {
try await ImageGenerator.shared.load(
try await ImageGenerator.shared.loadPipeline(
model: model,
controlNet: currentControlNets.filter { $0.image != nil }.compactMap(\.name),
computeUnit: mlComputeUnitPreference.computeUnits(forModel: model),
Expand Down Expand Up @@ -381,6 +377,7 @@ final class ImageController: ObservableObject {
currentControlNets[index].image = image
}
}
loadPipeline()
}

func selectImage() async -> CGImage? {
Expand Down
2 changes: 1 addition & 1 deletion Mochi Diffusion/Support/ImageGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class ImageGenerator: ObservableObject {
return models
}

func load(model: SDModel, controlNet: [String] = [], computeUnit: MLComputeUnits, reduceMemory: Bool) async throws {
func loadPipeline(model: SDModel, controlNet: [String] = [], computeUnit: MLComputeUnits, reduceMemory: Bool) async throws {
let fm = FileManager.default
if !fm.fileExists(atPath: model.url.path) {
await updateState(.error("Couldn't load \(model.name) because it doesn't exist."))
Expand Down

0 comments on commit 4fe125a

Please sign in to comment.