Issue with Optimizing Stable Diffusion XL Model for iOS 18

Hi everyone,

I’m currently in the process of converting and optimizing the Stable Diffusion XL model for iOS 18. I followed the steps from the WWDC 2024 session on model optimization, specifically the one titled "Bring your machine learning and AI models to Apple Silicon."

I utilized the Stable Diffusion XL model and the tools available in the ml-stable-diffusion GitHub repository and ran the following script to convert the model into an .mlpackage:

python3 -m python_coreml_stable_diffusion.torch2coreml \
    --convert-unet \
    --convert-vae-decoder \
    --convert-text-encoder \
    --xl-version \
    --model-version stabilityai/stable-diffusion-xl-base-1.0 \
    --bundle-resources-for-swift-cli \
    --refiner-version stabilityai/stable-diffusion-xl-refiner-1.0 \
    --attention-implementation SPLIT_EINSUM \
    -o ../PotraitModel/ \
    --custom-vae-version madebyollin/sdxl-vae-fp16-fix \
    --latent-h 128 \
    --latent-w 96 \
    --chunk-unet

The model conversion worked without any issues. However, when I proceeded to optimize the model in a Jupyter notebook, following the same process shown in the WWDC session, I encountered an error during the post-training quantization step. Here’s the code I used for that:

op_config = cto_coreml.0pPalettizerConfig(
    nbits=4,
    mode="kmeans",
    granularity="per_grouped_channel",
    group_size=16,
)

config = cto_coreml.OptimizationConfig(op_config)
compressed_model = cto_coreml.palettize_weights(mlmodel, config)

Unfortunately, I received the following error:

AssertionError: The IOS16 only supports per-tensor LUT, but got more than one lut on 0th axis. LUT shape: (80, 1, 1, 1, 16, 1)

It appears that the minimum deployment target of the MLModel is set to iOS 16, which might be causing compatibility issues. How can I update the minimum deployment target to iOS 18? If anyone has encountered this issue or knows a workaround, I would greatly appreciate your guidance!

Thanks in advance for any help!

Answered by jbenavidesv in 806803022

This is because the conversion is set by default from iOS 16/macOS13 here. You need to either set it to ct.target.macOS15 or ct.target.iOS18.

Accepted Answer

This is because the conversion is set by default from iOS 16/macOS13 here. You need to either set it to ct.target.macOS15 or ct.target.iOS18.

Hi @pushpinder

It @jbenavidesv's answer appears correct. Try setting the minimum deployment target to iOS18 and let us know if that unblocks you. If it does not please update the post with the error you receive so we can further assist you.

Issue with Optimizing Stable Diffusion XL Model for iOS 18
 
 
Q