apple / ml-stable-diffusion

Stable Diffusion with Core ML on Apple Silicon
MIT License
16.74k stars 934 forks source link

Terminated due to memory issue (when deploying on iOS) #22

Open martinlexow opened 1 year ago

martinlexow commented 1 year ago

I was able to get StableDiffusion up and running in an app for macOS. Thanks for your incredible work, CoreML team!

However the same setup does not work on my iPhone 14 with iOS 16.2 (20C5058d) (no Simulator involved, tried it on a physical device).

Execution seems to struggle at line:

let pipeline = try StableDiffusionPipeline(resourcesAt: resourceURL)

...after a short freeze the app crashes and I’m getting an error from Xcode saying Terminated due to memory issue.

As recommended I’ve added the Increased Memory Limit capability to my project but without luck.

I also did use --chunk-unet, resulting in two unet chunks of about 850 MB each.

I am working with Xcode 14.1 (14B47) on a MBP 16 M1 Pro, 16 GB, macOS 13.0.1 Ventura.

What am I missing?

martinlexow commented 1 year ago

In case it’s helpful, here’s the error log → errorlog.txt

martinlexow commented 1 year ago

And here’s how it looks in Instruments.

Screenshot 2022-12-03 at 21 15 34
3DTOPO commented 1 year ago

Terminated due to memory on an M1 iPad Pro too.

I don't see any way to set the image size to be generated which seems odd. Am I missing something?

3DTOPO commented 1 year ago

Ugh the model has a fixed output shape.

3DTOPO commented 1 year ago

I just built it with "--latent-w" and "--latent-h" set to 384 and still terminated do to memory.

I'm thinking it still uses the same amount of RAM since it looks like the Unet uses 64x64 regardless of the latent shape.

jiangdi0924 commented 1 year ago

Has anyone successfully run it on an iOS device? I found out not working properly , macOS 13.1 iPhone 14 simultor iOS 16.1

outcoldman commented 1 year ago

I believe it is a requirement to use iOS 16.2, but only iPad with M2 works for me, iPhone 13 Mini crashes as well :(

BIGPPWONG commented 1 year ago

I have this problem too, tested on an iPhone 13 pm

smocer commented 1 year ago

Same here, iPhone 12. Looks like it doesn't have enough RAM

outcoldman commented 1 year ago

I can run it on iPad Pro 12.9" M2 iOS 16.2. It fails on iPhone Mini 13 iOS 16.2 (compute units was set to cpuAndNL). And my primary iPhone 14 Pro is not updated yet to 16.2 (and it crashes on 16.1).

legolasW commented 1 year ago

I had it running on M1 iPad 16.2 Beta just fine, but crashed due to memory limit once updated to Today's 16.2 RC