Skip to main content
The Diffusion module enables on-device image generation using Apple’s ml-stable-diffusion framework. Models run as CoreML packages, leveraging the Apple Neural Engine for hardware-accelerated inference.

Overview

The image generation pipeline:
  • CoreML backend — Uses Apple’s ml-stable-diffusion for optimized on-device inference
  • Progress callbacks — Step-by-step progress updates during generation
  • Cancellation — Interrupt generation at any step
  • Safety checker — Optional NSFW content filtering
First-time model loading triggers CoreML compilation, which can take 5–15 minutes depending on the device. Subsequent loads use the compiled cache and are significantly faster.

Basic Usage

Setup

Register a Diffusion Model

Diffusion models use CoreML and are distributed as .zip archives. Use registerModel with the .coreml framework:
Use .coreml framework and .imageGeneration modality — not .llamaCpp. The model is a .zip archive containing compiled CoreML model packages.

Download the Model

Configure and Load

Set reduceMemory: true on devices with limited RAM. This trades some speed for significantly lower peak memory usage during generation.

API Reference

DiffusionConfiguration

DiffusionModelVariant

DiffusionGenerationOptions

Model Operations

generateImage

DiffusionProgressUpdate

DiffusionResult

Examples

Complete SwiftUI Image Generator

Prompt Engineering Helpers

Error Handling

Best Practices

The first time a CoreML diffusion model loads, it compiles the model for the target device’s Neural Engine. This takes 5–15 minutes. Show a clear progress indicator and explain the wait to users. Subsequent loads use the compiled cache and are fast.
Stable Diffusion models require ~2GB of RAM. Set reduceMemory: true in DiffusionConfiguration to lower peak memory usage at the cost of some speed. This prevents OOM crashes on older devices.
Always use the native resolution for your model variant: 512×512 for SD 1.5/2.0 and 1024×1024 for SDXL. Non-native resolutions produce distorted or low-quality results.
Image generation can take 30–120 seconds on mobile. Always return false from the progress handler or call cancelImageGeneration() to let users abort without waiting.
Below 15 steps, images are noticeably noisy. Above 30 steps, quality improvement plateaus. 20–25 steps gives the best quality-to-latency ratio on Apple Silicon.
A guidance scale of 7.0–8.5 produces coherent images that follow the prompt. Values above 12 often produce oversaturated, artifact-heavy results.

Supported Models

VLM

Vision Language Models

LLM Generation

Text generation

Configuration

SDK configuration options

Best Practices

Performance optimization