madebyollin / maple-diffusion

Stable Diffusion inference on iOS / macOS using MPSGraph
https://madebyoll.in/posts/maple_diffusion/
MIT License
793 stars 51 forks source link

If there are too many tokens in the Prompt the app crashes. #15

Closed jackpal closed 1 year ago

jackpal commented 1 year ago

Repro steps:

  1. Paste a long prompt into the Prompt string.
  2. Tap Generate Image.

Expected:

Image gets generated, possibly with a warning about ignoring the trailing tokens of the prompt.

Actual:

The app crashes.

The following prompt causes the crash, but I don't think there's anything special about it, it just has a lot of tokens and happened to be the prompt for the first image on lexica.art when I visited it to grab an example prompt:

a photorealistic dramatic fantasy render of a beautiful woman wearing a beautiful intricately detailed japanese crow kitsune mask and clasical japanese kimono by wlop, artgerm, greg rutkowski, alphonse mucha, beautiful dynamic dramatic dark moody lighting, shadows, cinematic atmosphere, artstation, concept design art, octane render, 8 k the seeds for each individual image are : [ 7 7 9 9 7 6 1 8 1, 3 2 5 6 4 1 8 5 7 5, 4 0 0 8 6 1 3 6 7 8, 3 1 5 5 8 2 9 4 2 4, 1 7 0 9 5 4 0 5 8 2, 9 3 3 7 4 3 2 0 7, 3 3 3 0 9 2 2 3 1 3, 4 7 7 1 8 2 1 7, 4 1 7 2 0 7 6 9 5 ] 

The crash is:

Swift/Array.swift:915: Fatal error: Can't construct Array with count < 0

#0  0x00000001bf2380f8 in _swift_runtime_on_report ()
#1  0x00000001bf2d5b50 in _swift_stdlib_reportFatalErrorInFile ()
#2  0x00000001bee99ac4 in closure #1 in closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:) ()
#3  0x00000001bee99828 in closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:) ()
#4  0x00000001bee99630 in closure #1 in _assertionFailure(_:_:file:line:flags:) ()
#5  0x00000001bee99188 in _assertionFailure(_:_:file:line:flags:) ()
#6  0x00000001bee7c944 in static Array._allocateUninitialized(_:) ()
#7  0x00000001bf1d17ac in specialized Array.init(repeating:count:) ()
#8  0x00000001beef0da8 in Array.init(repeating:count:) ()
#9  0x00000001048408b0 in BPETokenizer.encode(s:) at /Users/jackpal/Developer/maple-diffusion/maple-diffusion/MapleDiffusion.swift:545
#10 0x000000010484996c in MapleDiffusion.generateLatent(prompt:negativePrompt:seed:steps:guidanceScale:completion:) at /Users/jackpal/Developer/maple-diffusion/maple-diffusion/MapleDiffusion.swift:828
#11 0x000000010484bdf0 in MapleDiffusion.generate(prompt:negativePrompt:seed:steps:guidanceScale:completion:) at /Users/jackpal/Developer/maple-diffusion/maple-diffusion/MapleDiffusion.swift:879
#12 0x000000010481f0e4 in closure #1 in ContentView.generate() at /Users/jackpal/Developer/maple-diffusion/maple-diffusion/ContentView.swift:35
madebyollin commented 1 year ago

Oops! Should be fixed now?

jackpal commented 1 year ago

Yes, it's fixed now. Thanks!