ml-stable-diffusion icon indicating copy to clipboard operation
ml-stable-diffusion copied to clipboard

Encoder.swift fatal error with image2image (possible .mlmodelc compatibility breaks)

Open keijiro opened this issue 1 year ago • 2 comments

When I try image2image with ready-made Core ML models, it throws the following error.

StableDiffusion/Encoder.swift:96: Fatal error: Unexpectedly found nil while unwrapping an Optional value

You can reproduce this issue with the following steps:

  1. Download coreml-stable-diffusion-2-base.
  2. Create a 512x512 input image.
  3. swift run StableDiffusionSample --resource-path <resource dir> --image <input image> "test"

It doesn’t reproduce when I reconvert the model using the latest version of torch2coreml.

I also confirmed that it doesn’t reproduce when I revert the changes in https://github.com/apple/ml-stable-diffusion/commit/1147e87b790895feeddc1b146c8fddf6f8b9f8a5

keijiro avatar Apr 29 '23 01:04 keijiro

Thanks for the report and good catch! We will need to update the VAEEncoder.mlmodelc files on Hugging Face. cc: @pcuenca

atiorh avatar Apr 29 '23 04:04 atiorh

Looking into it. Thanks, I didn't realize the encoder inputs had changed.

pcuenca avatar Apr 30 '23 11:04 pcuenca

Submitted these PRs after converting the VAE encoder again:

https://huggingface.co/apple/coreml-stable-diffusion-v1-4/discussions/4/files https://huggingface.co/apple/coreml-stable-diffusion-v1-5/discussions/5/files https://huggingface.co/apple/coreml-stable-diffusion-2-base/discussions/6/files https://huggingface.co/apple/coreml-stable-diffusion-2-1-base/discussions/2/files

Tested locally with this script:

declare -a repos=(
    coreml-stable-diffusion-v1-4
    coreml-stable-diffusion-v1-5
    coreml-stable-diffusion-2-base
    coreml-stable-diffusion-2-1-base
)

for repo in "${repos[@]}"
do
    for variant in "original" "split_einsum"
    do
        units="cpuAndNeuralEngine"
        if [ "$variant" == "original" ]
        then
            units="cpuAndGPU"
        fi
        echo swift run StableDiffusionSample --compute-units $units --resource-path models/apple/$repo/$variant/compiled --image image.jpg "test"
        swift run StableDiffusionSample --compute-units $units --resource-path models/apple/$repo/$variant/compiled --image image.jpg "test"
        echo
    done
done

I updated the .mlpackage directories too, but the Python inference script does not seem to have a way to test image-to-image yet.

pcuenca avatar Apr 30 '23 18:04 pcuenca

@pcuenca Thank you very much!

atiorh avatar May 01 '23 01:05 atiorh

@keijiro Could you please verify that the issue is not longer present for you as well?

atiorh avatar May 01 '23 01:05 atiorh

I ran the following shell script with the updated models in those PRs, and verified that it works correctly.

swift run StableDiffusionSample --resource-path models/coreml-stable-diffusion-v1-4/original/compiled         --compute-units cpuAndGPU          --image source.png --strength 0.9 "painting of a cat"
swift run StableDiffusionSample --resource-path models/coreml-stable-diffusion-v1-4/split_einsum/compiled     --compute-units cpuAndNeuralEngine --image source.png --strength 0.9 "painting of a cat"
swift run StableDiffusionSample --resource-path models/coreml-stable-diffusion-v1-5/original/compiled         --compute-units cpuAndGPU          --image source.png --strength 0.9 "painting of a cat"
swift run StableDiffusionSample --resource-path models/coreml-stable-diffusion-v1-5/split_einsum/compiled     --compute-units cpuAndNeuralEngine --image source.png --strength 0.9 "painting of a cat"
swift run StableDiffusionSample --resource-path models/coreml-stable-diffusion-2-base/original/compiled       --compute-units cpuAndGPU          --image source.png --strength 0.9 "painting of a cat"
swift run StableDiffusionSample --resource-path models/coreml-stable-diffusion-2-base/split_einsum/compiled   --compute-units cpuAndNeuralEngine --image source.png --strength 0.9 "painting of a cat"
swift run StableDiffusionSample --resource-path models/coreml-stable-diffusion-2-1-base/original/compiled     --compute-units cpuAndGPU          --image source.png --strength 0.9 "painting of a cat"
swift run StableDiffusionSample --resource-path models/coreml-stable-diffusion-2-1-base/split_einsum/compiled --compute-units cpuAndNeuralEngine --image source.png --strength 0.9 "painting of a cat"

Thanks!

keijiro avatar May 01 '23 04:05 keijiro

Thanks for the confirmation @keijiro! I just merged those PRs so I think this issue can be closed now :)

pcuenca avatar May 01 '23 07:05 pcuenca

Thanks for the report and the fix both!

atiorh avatar May 01 '23 17:05 atiorh