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.

CI Python License


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_diffusion 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_diffusion import DiT, DiTConfig, DiffusionTrainer
from mlx_diffusion.schedulers import FlowMatchEulerScheduler
from mlx_diffusion.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_diffusion import DiT, inject_lora, save_lora
from mlx_diffusion.schedulers import FlowMatchEulerScheduler
from mlx_diffusion 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.0.tar.gz (123.0 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.0-py3-none-any.whl (46.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mlx_diffuser-0.1.0.tar.gz
  • Upload date:
  • Size: 123.0 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.0.tar.gz
Algorithm Hash digest
SHA256 cf502904bcbd9ecf7d46d9157c5443b0c272a29613e5c07cd8b1a5bef65470ea
MD5 dca94a1325af9f62e3aa7160db2e40f3
BLAKE2b-256 c46c3312860f2679420f1f305045b47d763ba70eb82ad98efca76b02f1ad5f2a

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_diffuser-0.1.0.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.0-py3-none-any.whl.

File metadata

  • Download URL: mlx_diffuser-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b5c6b7de7260722f071d55df2094374f84172024561c4500ec86cd175d8c9a74
MD5 a49f162bf60a2ce1ae9a9a900343b492
BLAKE2b-256 901f9f20d5fc05e2883e7486c2be71a6cbe1298393f6ae27876d2999b7a14a71

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_diffuser-0.1.0-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