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
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
API Reference
DiffusionConfiguration
DiffusionModelVariant
DiffusionGenerationOptions
Model Operations
generateImage
DiffusionProgressUpdate
DiffusionResult
Examples
Complete SwiftUI Image Generator
Prompt Engineering Helpers
Error Handling
Best Practices
Expect slow first load
Expect slow first load
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.
Enable reduceMemory on constrained devices
Enable reduceMemory on constrained devices
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.Match resolution to model variant
Match resolution to model variant
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.
Provide a cancel mechanism
Provide a cancel mechanism
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.Use 20–25 steps for most prompts
Use 20–25 steps for most prompts
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.
Keep guidance scale between 7 and 9
Keep guidance scale between 7 and 9
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
Related
VLM
Vision Language Models
LLM Generation
Text generation
Configuration
SDK configuration options
Best Practices
Performance optimization