Run Flux.1 on M3 Mac with Diffusers

0𝕏koji
1 min readAug 6, 2024

--

What is Diffusers?

1. Create a virtual env

python3 -m venv fluxtest
source fluxtest/bin/activate

2. Login to Hugging Face via CLI
https://huggingface.co/docs/huggingface_hub/main/en/guides/cli

pip install -U "huggingface_hub[cli]"
huggingface-cli login

3. Install packages

pip install torch==2.3.1
pip install git+https://github.com/huggingface/diffusers.git
pip install transformers==4.43.3 sentencepiece==0.2.0 accelerate==0.33.0 protobuf==5

4. Run a python script

import torch
from diffusers import FluxPipeline
import diffusers

_flux_rope = diffusers.models.transformers.transformer_flux.rope
def new_flux_rope(pos: torch.Tensor, dim: int, theta: int) -> torch.Tensor:
assert dim % 2 == 0, "The dimension must be even."
if pos.device.type == "mps":
return _flux_rope(pos.to("cpu"), dim, theta).to(device=pos.device)
else:
return _flux_rope(pos, dim, theta)

diffusers.models.transformers.transformer_flux.rope = new_flux_rope

pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", revision='refs/pr/1', torch_dtype=torch.bfloat16).to("mps")

prompt = "japanese girl, photo-realistic"
out = pipe(
prompt=prompt,
guidance_scale=0.,
height=1024,
width=1024,
num_inference_steps=4,
max_sequence_length=256,
).images[0]
out.save("image.png")
python image.py

output

got the script from lucataco!

Run Flux.1 on M3 Mac with ComfyUI

--

--

0𝕏koji

software engineer works for a Biotechnology Research startup in Brooklyn. #CreativeCoding #Art #IoT #MachineLearning #python #typescript #javascript #reactjs