Skip to main content

Diffusion training and sampling on Apple silicon with MLX.

Project description

mlx-diffuser

Diffusion & flow models on Apple silicon, powered by MLX. Train from scratch, fine-tune, or run inference — for image, video, and discrete modalities — from one small, readable codebase.

PyPI version PyPI downloads Python License CI Docs


If you know PyTorch and 🤗 diffusers, you already know this library: Model.from_pretrained(...), pipe(...), nn.Module everywhere. The difference is underneath — unified memory, mx.compile, fused Metal kernels, and built-in weight quantization, so large models fit and run fast on a Mac.

Install

pip install mlx-diffuser          # core
pip install "mlx-diffuser[hub]"   # + Hugging Face Hub loading

Requires Apple silicon (M-series) and Python 3.11+.

Generate

from mlx_diffuser import DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained("path/or/hub-id", dtype="bf16", quantize=4)
images = pipe([1, 2, 3], num_inference_steps=50, guidance_scale=4.0, seed=0)

…or from the terminal:

mlx-diffuser generate path/or/hub-id --labels 1,2,3 --steps 50 --out samples/

Train from scratch

from mlx_diffuser import DiT, DiTConfig, DiffusionTrainer
from mlx_diffuser.schedulers import FlowMatchEulerScheduler
from mlx_diffuser.training import batch_iterator

model = DiT(DiTConfig(in_channels=3, hidden_size=384, depth=12, num_heads=6))
trainer = DiffusionTrainer(model, FlowMatchEulerScheduler(), lr=1e-4, ema_decay=0.999)
trainer.fit(batch_iterator(data, batch_size=32), steps=10_000)
model.save_pretrained("my-model")

Fine-tune with LoRA (on your laptop)

from mlx_diffuser import DiT, inject_lora, save_lora
from mlx_diffuser.schedulers import FlowMatchEulerScheduler
from mlx_diffuser import DiffusionTrainer

model = DiT.from_pretrained("my-model")
inject_lora(model, rank=8)                 # base frozen; only adapters train
trainer = DiffusionTrainer(model, FlowMatchEulerScheduler(), lr=5e-3)
trainer.fit(batch_iterator(data, batch_size=8), steps=1000)
save_lora(model, "my-lora", rank=8, alpha=16)
mlx-diffuser train --data ./photos --base my-model --lora --out my-lora --steps 1000

What's inside

  • ModelsDiT (image/video/text via config), UNet2D (Stable-Diffusion style, text-conditionable), AutoencoderKL (VAE for latent diffusion).
  • SchedulersDDPM, DDIM, EulerDiscrete, FlowMatchEuler; one object covers both training (add_noise/get_target) and sampling (step).
  • TrainingDiffusionTrainer (compiled step, EMA, grad clip, min-SNR weighting), full fine-tune, and LoRA.
  • Apple-silicon speed — fused attention, mx.compile, lazy evaluation, unified-memory controls, and 4/8-bit weight quantization.
  • CLIgenerate, train, convert.

Design

The library rests on three orthogonal pieces — a process (the noise/flow math), a network (a plain nn.Module), and a pipeline (inference glue). Keeping them independent is what lets the same network train under DDPM today and flow-matching tomorrow. See DESIGN.md and the documentation.

License

Apache-2.0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mlx_diffuser-0.1.1.tar.gz (123.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mlx_diffuser-0.1.1-py3-none-any.whl (46.6 kB view details)

Uploaded Python 3

File details

Details for the file mlx_diffuser-0.1.1.tar.gz.

File metadata

  • Download URL: mlx_diffuser-0.1.1.tar.gz
  • Upload date:
  • Size: 123.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mlx_diffuser-0.1.1.tar.gz
Algorithm Hash digest
SHA256 660faf8a3ff9664ac6b26974a35eb8728dc1656461a3712256689ba2d7965c48
MD5 4147c254c3ae6071e068893c36646005
BLAKE2b-256 546781ad59da52723fbc5a986a212729ec5d739ac25c130843ab9571708e71de

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_diffuser-0.1.1.tar.gz:

Publisher: release.yml on amirhossein-razlighi/mlx_diffuser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mlx_diffuser-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: mlx_diffuser-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 46.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mlx_diffuser-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e9f3281ddda49f9e4c616f16b06b05357030b0083171dc2ef36228729f3971a7
MD5 4bd1be9352eb2730fe75539a2e27f8a0
BLAKE2b-256 b4eeac143b73f83a3d047a159738257664c93bf89f11f926c30b5556255be078

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_diffuser-0.1.1-py3-none-any.whl:

Publisher: release.yml on amirhossein-razlighi/mlx_diffuser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page