dflow 2B for Apple Silicon: MPS port with an int8 engine for M5

An Apple Silicon port of data-archetype/dflow_2b. The whole pipeline runs in PyTorch on MPS. On an M5 Mac the denoiser's matrix multiplications run on the GPU Neural Accelerators through our own int8 Metal kernel. This cuts generation time by about 40% against the plain bf16 path, and the picture stays the same for the same seed. The original model weights are unchanged. The repository also keeps the earlier MLX denoiser experiment; despite the repository name, the PyTorch/MPS path is the recommended one.

Sample images

Six noncommercial samples generated with the original dflow 2B weights on an Apple M5 MacBook via PyTorch/MPS. Each image was generated at 768 Γ— 768 with 50 ER-SDE steps. From left to right, top to bottom: noir film still, frog macro, maritime oil painting, tram watercolor, science fiction, and studio still life.

Six dflow 2B samples in different genres

Run

Requires Apple Silicon with MPS support, Python 3.12 and sufficient unified memory. The fast and turbo modes need an M5 or newer GPU and macOS 26.2 or newer. The package includes the original weights and dependencies. Download the complete repository, including safetensors files, then from its root:

python3.12 -m venv .venv
source .venv/bin/activate
pip install -r requirements-mps.txt
bash gen.sh "A lighthouse on a cliff at dusk, a photograph."

gen.sh "<caption>" [size] [steps] [seed] [out.png] defaults to 512 Γ— 512, 50 steps and seed 7, writes the PNG to out/ and prints its path. DFLOW_MODE picks the engine:

Mode What runs Use
fast (default) torch.compile and int8 matrix multiplications on the M5 GPU Neural Accelerators Everyday generation on an M5
turbo fast, plus guidance reuse and a middle-layer cache on alternate steps Drafts: same composition, small detail changes
exact The original bf16 path Any Apple Silicon Mac, reference output
DFLOW_MODE=turbo bash gen.sh "A lighthouse on a cliff at dusk, a photograph."
DFLOW_MODE=exact bash gen.sh "A lighthouse on a cliff at dusk, a photograph." 1024

The first fast or turbo run writes an int8 weight cache to models/dflow_2b_int8/ (1.9 GB, about 10 s, once). On first use the int8 kernel checks itself against a CPU reference. On a GPU without Neural Accelerators that check stops the run with an error; use DFLOW_MODE=exact there. The full command line (solver, schedule, PDG/CFG scales, negative prompt, JSON layouts, --compile, --int8, --turbo) is python port/generate.py --help.

The dependency versions in requirements-mps.txt are those recorded in the original test environment. Availability may depend on your Python and macOS version.

Speed and quality on an M5 MacBook

Tested on a MacBook Pro M5 (10-core GPU) with 16 GB of unified memory, macOS 27.2 and PyTorch 2.15.0, at 50 ER-SDE steps.

Whole gen.sh run, from start to saved PNG, with a 162-word caption:

Image size exact fast turbo
512 Γ— 512 48.1 s 28.9–30.1 s (βˆ’38 to βˆ’40%) 22.5 s (βˆ’53%)
1024 Γ— 1024 181.5 s 109.1 s (βˆ’40%) not measured

Peak MPS memory in fast mode is 3.1 GB: the stages load one after another, and the int8 copy of the image layers takes 1.9 GB instead of 3.8 GB in bf16.

Each image was compared with the exact image for the same caption and seed (6 captions at 512, 2 at 1024). LPIPS 0 means identical images; visible differences start around 0.05.

Mode LPIPS at 512, mean / max PSNR at 512 LPIPS at 1024, mean / max PSNR at 1024
fast 0.0056 / 0.0096 40.5 dB 0.020 / 0.029 37.6 dB
turbo 0.030 / 0.056 31.3 dB not measured β€”

The same six captions and seeds in exact, fast and turbo modes at 512 Γ— 512

Row labels give the whole-run time for one 512 Γ— 512 image. Per-image numbers are in speedup/results, and the full report with profiles and rejected ideas is speedup/REPORT.md (in Russian).

How the int8 engine works

  • On the M5, PyTorch runs bf16 matrix multiplications at 13–15 TFLOP/s and MLX at 12.4–13 TFLOP/s, the limit of that path. The M5 GPU Neural Accelerators also multiply int8 Γ— int8 β†’ int32 at about twice that rate, and neither framework uses it.
  • port/nax_int8.py is a Metal 4 mpp::tensor_ops::matmul2d kernel compiled at run time with torch.mps.compile_shader. It reaches 21–24 TOPS, 1.4–1.6Γ— faster than bf16. It is registered as a torch.library custom op, so torch.compile keeps each block as one graph and fuses the activation quantization with neighbouring operations.
  • The 120 linear layers of the image path (qkv, attention output, MLP up and down in all 30 blocks) run as W8A8: per-channel int8 weights and per-token int8 activations. The text refiner, AdaLN, patch embedding and velocity head stay in bf16.
  • Calibration on real activations (6 captions Γ— 50 steps) sets SmoothQuant channel scales for every layer and keeps 32 outlier channels of each MLP down projection in bf16. It is stored in port/int8_calibration.safetensors and made by speedup/calibrate_int8.py.
  • Two smaller fixes help every mode: the attention query scale is no longer read back from the GPU in every forward pass, and torch.compile skips the text refiner, which runs once per caption.
  • turbo (port/turbo.py) repeats the previous guidance correction on alternate steps, which skips the weak path-drop pass, and, on staggered steps, reuses the previous contribution of the 18 middle blocks. The first steps and the last three run in full.

MLX denoiser experiment

mlx_port/ runs the denoiser in MLX, with the text encoder and autoencoder in PyTorch on MPS. It produces correct images but is slower per step than the compiled PyTorch path at working sizes; the MLX report (in Russian) has the measurements. To try it:

PYTHONPATH=port:. python mlx_port/sample_mlx.py --prompt "A lighthouse on a cliff at dusk, a photograph." --size 512 --steps 50 --seed 7 --out out/lighthouse.png

Origin, changes and licences

The code was modified for Apple Silicon and MLX on 21 November 2026 and sped up on 22 November 2026. The original dflow model and source are by data-archetype. Original model weights are unchanged. See PATCHES.md for every change to the original code, speedup/REPORT.md for the int8 engine and turbo mode, and the MLX report for the MLX denoiser. The dflow weights and code are licensed under ModelGo Attribution-NonCommercial 2.0; use and distribution are limited to noncommercial purposes. Preserve the original notice and attribution.

The bundled text encoder is derived from T5Gemma 2 and is subject to the Gemma Terms of Use, its notice and use restrictions. The bundled dinac3_96 autoencoder is distributed under its own licence and attribution notices. The bundled upstream ComfyUI code is subject to GPL-3.0. Consult the component notices for details.

Downloads last month

-

Downloads are not tracked for this model. How to track
MLX
Hardware compatibility
Log In to add your hardware

Quantized

Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Model tree for ungurenko/dflow-2b-mlx

Finetuned
(1)
this model