Skip to content

Commit 4a638f8

Browse files
committed
make it work with Instruments
1 parent 5817dbe commit 4a638f8

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

Example/maple-diffusion/ContentView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ struct ContentView: View {
132132
case .ready:
133133
print("model ready")
134134
running = false
135+
case .notStarted:
136+
break
135137
}
136138
}
137139
}

Sources/MapleDiffusion/Functions.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,20 @@ func makeAuxUpsampler(graph: MPSGraph, xIn: MPSGraphTensor) -> MPSGraphTensor {
555555
}
556556

557557

558-
559558
func tensorToCGImage(data: MPSGraphTensorData) -> CGImage {
560559
let shape = data.shape.map{$0.intValue}
561560
var imageArrayCPUBytes = [UInt8](repeating: 0, count: shape.reduce(1, *))
562561
data.mpsndarray().readBytes(&imageArrayCPUBytes, strideBytes: nil)
563-
return CGImage(width: shape[2], height: shape[1], bitsPerComponent: 8, bitsPerPixel: 32, bytesPerRow: shape[2]*shape[3], space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGBitmapInfo(rawValue: CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.noneSkipLast.rawValue), provider: CGDataProvider(data: NSData(bytes: &imageArrayCPUBytes, length: imageArrayCPUBytes.count))!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)!
562+
563+
return CGImage(
564+
width: shape[2], height: shape[1],
565+
bitsPerComponent: 8, bitsPerPixel: 32,
566+
bytesPerRow: shape[2]*shape[3],
567+
space: CGColorSpaceCreateDeviceRGB(),
568+
bitmapInfo: CGBitmapInfo(rawValue: CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.noneSkipLast.rawValue),
569+
provider: CGDataProvider(data: NSData(bytes: &imageArrayCPUBytes,
570+
length: imageArrayCPUBytes.count))!,
571+
decode: nil,
572+
shouldInterpolate: true,
573+
intent: CGColorRenderingIntent.defaultIntent)!
564574
}

Sources/MapleDiffusion/MapleDiffusion.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ public class MapleDiffusion : ObservableObject {
358358

359359
// 3. Noise generation
360360
var latent = randomLatent(seed: seed)
361+
361362
let timesteps = Array<Int>(stride(from: 1, to: 1000, by: Int(1000 / steps)))
362363
completion(nil, 0.75 * 1 / Float(steps), "Starting diffusion...")
363364

@@ -371,7 +372,8 @@ public class MapleDiffusion : ObservableObject {
371372
let tMPSData = MPSGraphTensorData(device: graphDevice!, data: tData, shape: [1], dataType: MPSDataType.int32)
372373
let tPrevData = [Int32(tsPrev)].withUnsafeBufferPointer {Data(buffer: $0)}
373374
let tPrevMPSData = MPSGraphTensorData(device: graphDevice!, data: tPrevData, shape: [1], dataType: MPSDataType.int32)
374-
let guidanceScaleData = [Float16(guidanceScale)].withUnsafeBufferPointer {Data(buffer: $0)}
375+
let guidanceScaleData = [Float32(guidanceScale)].withUnsafeBufferPointer {Data(buffer: $0)}
376+
375377
let guidanceScaleMPSData = MPSGraphTensorData(device: graphDevice!, data: guidanceScaleData, shape: [1], dataType: MPSDataType.float16)
376378
let temb = tembGraph!.run(with: commandQueue!, feeds: [tembTIn!: tMPSData], targetTensors: [tembOut!], targetOperations: nil)[tembOut!]!
377379
let etaUncond: MPSGraphTensorData
@@ -473,3 +475,6 @@ public class MapleDiffusion : ObservableObject {
473475
}
474476
}
475477
}
478+
479+
480+

0 commit comments

Comments
 (0)