How apple-aiml-research/ml-stable-diffusion works
Core ML Stable Diffusion has two complementary products: a Python toolchain that converts Diffusers/PyTorch components into Core ML resources, and a Swift runtime that uses those resources to generate images on Apple platforms. The principal runtime flow is prompt tokenization and text conditioning, latent initialization, iterative scheduler/U-Net (or SD3 transformer) denoising, then VAE decoding and optional safety checking; image-to-image adds encoding. README also documents ControlNet, multilingual text encoding, SDXL/SD3, and conversion optimizations. Edges follow excerpts or README claims; unsampled feature wiring is omitted where not established.
Main entry points: StableDiffusionPipeline.swift Stable Diffusion · StableDiffusionPipeline+Resources.swift Model resources · main.swift Generation CLI · StableDiffusion3Pipeline.swift SD3 pipeline
Components 29
Model conversion
- PyTorch conversion
python_coreml_stable_diffusion/torch2coreml.py - U-Net conversion
python_coreml_stable_diffusion/unet.py - ControlNet conversion
python_coreml_stable_diffusion/controlnet.py - Conversion optimizations
python_coreml_stable_diffusion/attention.py - Activation quantization
python_coreml_stable_diffusion/activation_quantization.py - Mixed-bit analysis
python_coreml_stable_diffusion/mixed_bit_compression_pre_analysis.py
Swift generation runtime
- Generation CLI
swift/StableDiffusionCLI/main.swift - Stable Diffusion
swift/StableDiffusion/pipeline/StableDiffusionPipeline.swift - SDXL pipeline
swift/StableDiffusion/pipeline/StableDiffusionXLPipeline.swift - SD3 pipeline
swift/StableDiffusion/pipeline/StableDiffusion3Pipeline.swift
Prompt conditioning
- BPE tokenizer
swift/StableDiffusion/tokenizer/BPETokenizer.swift - Text encoder
swift/StableDiffusion/pipeline/TextEncoder.swift - XL text encoders
swift/StableDiffusion/pipeline/TextEncoderXL.swift - T5 text encoder
swift/StableDiffusion/pipeline/TextEncoderT5.swift - Multilingual encoder
swift/StableDiffusion/pipeline/MultilingualTextEncoder.swift
Diffusion and sampling
- U-Net predictor
swift/StableDiffusion/pipeline/Unet.swift - ControlNet
swift/StableDiffusion/pipeline/ControlNet.swift - MMDiT predictor
swift/StableDiffusion/pipeline/MultiModalDiffusionTransformer.swift - PNDM scheduler
swift/StableDiffusion/pipeline/Scheduler.swift - DPM solver
swift/StableDiffusion/pipeline/DPMSolverMultistepScheduler.swift - Flow scheduler
swift/StableDiffusion/pipeline/DiscreteFlowScheduler.swift - Random sources
swift/StableDiffusion/pipeline/RandomSource.swift
Image and resources
- Model resources
swift/StableDiffusion/pipeline/StableDiffusionPipeline+Resources.swift - VAE encoder
swift/StableDiffusion/pipeline/Encoder.swift - VAE decoder
swift/StableDiffusion/pipeline/Decoder.swift - Safety checker
swift/StableDiffusion/pipeline/SafetyChecker.swift - Managed Core ML
swift/StableDiffusion/pipeline/ManagedMLModel.swift - Resource lifecycle
swift/StableDiffusion/pipeline/ResourceManaging.swift
Other components
- App or developer
Connections 39
- App or developer → PyTorch conversion: converts models
- PyTorch conversion → U-Net conversion: uses
python_coreml_stable_diffusion/torch2coreml.py - PyTorch conversion → ControlNet conversion: uses
python_coreml_stable_diffusion/torch2coreml.py - PyTorch conversion → Model resources: produces models
- App or developer → Generation CLI: runs generation
swift/StableDiffusionCLI/main.swift - Generation CLI → Model resources: loads resources
swift/StableDiffusionCLI/main.swift - Generation CLI → Stable Diffusion: selects pipeline
swift/StableDiffusionCLI/main.swift - Generation CLI → SDXL pipeline: selects pipeline
swift/StableDiffusionCLI/main.swift - Generation CLI → SD3 pipeline: selects pipeline
swift/StableDiffusionCLI/main.swift - Model resources → Text encoder: constructs
swift/StableDiffusion/pipeline/StableDiffusionPipeline+Resources.swift - Model resources → BPE tokenizer: constructs
swift/StableDiffusion/pipeline/StableDiffusionPipeline+Resources.swift - Model resources → U-Net predictor: constructs
swift/StableDiffusion/pipeline/StableDiffusionPipeline+Resources.swift - Model resources → ControlNet: constructs
swift/StableDiffusion/pipeline/StableDiffusionPipeline+Resources.swift - Model resources → VAE encoder: constructs
swift/StableDiffusion/pipeline/StableDiffusionPipeline+Resources.swift - Model resources → VAE decoder: constructs
swift/StableDiffusion/pipeline/StableDiffusionPipeline+Resources.swift - Stable Diffusion → ControlNet: executes
swift/StableDiffusion/pipeline/StableDiffusionPipeline.swift - Stable Diffusion → U-Net predictor: predicts noise
swift/StableDiffusion/pipeline/StableDiffusionPipeline.swift - Stable Diffusion → PNDM scheduler: steps latents
swift/StableDiffusion/pipeline/StableDiffusionPipeline.swift - Stable Diffusion → VAE encoder: encodes input
swift/StableDiffusion/pipeline/StableDiffusionPipeline.swift - Stable Diffusion → VAE decoder: decodes latents
swift/StableDiffusion/pipeline/StableDiffusionPipeline.swift - Stable Diffusion → Safety checker: checks output
swift/StableDiffusion/pipeline/StableDiffusionPipeline.swift - SDXL pipeline → U-Net predictor: predicts noise
swift/StableDiffusion/pipeline/StableDiffusionXLPipeline.swift - SDXL pipeline → VAE decoder: decodes latents
swift/StableDiffusion/pipeline/StableDiffusionXLPipeline.swift - SD3 pipeline → MMDiT predictor: predicts noise
swift/StableDiffusion/pipeline/StableDiffusion3Pipeline.swift - SD3 pipeline → PNDM scheduler: calculates steps
swift/StableDiffusion/pipeline/StableDiffusion3Pipeline.swift - SD3 pipeline → VAE decoder: decodes latents
swift/StableDiffusion/pipeline/StableDiffusion3Pipeline.swift - Stable Diffusion → Text encoder: encodes prompt
- Stable Diffusion → BPE tokenizer: tokenizes prompt
- Text encoder → BPE tokenizer: tokenizes text
swift/StableDiffusion/pipeline/TextEncoder.swift - Managed Core ML → Resource lifecycle: manages lifecycle
- U-Net predictor → Managed Core ML: runs inference
swift/StableDiffusion/pipeline/Unet.swift - VAE decoder → Managed Core ML: runs inference
swift/StableDiffusion/pipeline/Decoder.swift - VAE encoder → Managed Core ML: runs inference
swift/StableDiffusion/pipeline/Encoder.swift - Stable Diffusion → DPM solver: may select
swift/StableDiffusion/pipeline/StableDiffusionPipeline.swift - Stable Diffusion → Flow scheduler: may select
swift/StableDiffusion/pipeline/StableDiffusionPipeline.swift - VAE encoder → Random sources: samples latents
swift/StableDiffusion/pipeline/Encoder.swift - SD3 pipeline → T5 text encoder: conditions input
swift/StableDiffusion/pipeline/StableDiffusion3Pipeline.swift - SDXL pipeline → XL text encoders: conditions input
- Stable Diffusion → Multilingual encoder: optional conditioning