December 23, 2024|5 min reading

How to Run Flux Schnell Locally on Mac - A Step-by-Step Guide

How to Run Flux Schnell Locally on Mac
Author Merlio

published by

@Merlio

How to Run Flux Schnell on Mac Locally

Flux Schnell is an open-source text-to-image model designed for efficient and high-quality image generation. This guide walks you through setting up and running Flux Schnell locally on a MacBook Pro with an M3 Max chip. With detailed steps and optimization tips, you’ll be generating stunning images in no time.

Requirements for Running Flux Schnell Locally

Before starting, ensure your system meets the following requirements:

  • MacBook Pro with M3 Max chip
  • RAM: At least 40 GB available
  • OS: macOS Sonoma or later
  • Installed Tools:
    • Xcode Command Line Tools
    • Homebrew package manager

Step-by-Step Installation Guide

Step 1: Install Miniconda

Miniconda simplifies Python environment management. Follow these steps:

Open Terminal.

Run the following command to download the Miniconda installer:

curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh

Install Miniconda:

sh Miniconda3-latest-MacOSX-arm64.sh

Follow the prompts to complete the installation.

Close and reopen Terminal to apply changes.

Step 2: Create and Activate a Conda Environment

Create a dedicated environment for Flux Schnell:

conda create -n flux python=3.11 -y conda activate flux

Step 3: Install PyTorch

Install PyTorch with MPS (Metal Performance Shaders) support:

pip install torch==2.3.1

Step 4: Install Diffusers and Dependencies

Install the necessary libraries for Flux Schnell:

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.27.3

Running Flux Schnell Locally

Step 1: Prepare the Script

Create a new Python file named flux_generate.py.

Copy the following code into the file:

import torch from diffusers import FluxPipeline import diffusers # Modify the rope function to handle MPS device _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 # Load the Flux Schnell model pipe = FluxPipeline.from_pretrained( "black-forest-labs/FLUX.1-schnell", revision='refs/pr/1', torch_dtype=torch.bfloat16 ).to("mps") # Set the prompt for image generation prompt = "A cat holding a sign that says hello world" # Generate the image out = pipe( prompt=prompt, guidance_scale=0., height=1024, width=1024, num_inference_steps=4, max_sequence_length=256, ).images[0] # Save the generated image out.save("flux_image.png")

Step 2: Execute the Script

Navigate to the directory containing flux_generate.py in Terminal.

Run the script:

python flux_generate.py

The process will take around 30 seconds, utilizing up to 40 GB of RAM.

Optimizing Performance

To maximize the efficiency of Flux Schnell on your Mac:

Close Unnecessary Applications: Free up RAM and CPU resources.

Ensure Proper Ventilation: Prevent thermal throttling by maintaining good airflow.

Adjust Parameters:

  • Increase num_inference_steps for better image quality.
  • Modify max_sequence_length for more detailed prompts.

Troubleshooting Common Issues

  • Memory Errors: Reduce image dimensions or sequence length.
  • Module Not Found: Ensure all dependencies are installed within your conda environment.
  • CUDA Errors: Confirm the use of the MPS backend (to("mps")).

Conclusion

Setting up and running Flux Schnell on your MacBook Pro M3 Max allows for unparalleled image generation capabilities. Experiment with prompts, parameters, and advanced features to unlock the full potential of this powerful tool.

FAQs

What is Flux Schnell?

Flux Schnell is an open-source text-to-image model designed for efficient and high-quality image generation.

Do I need an internet connection to use Flux Schnell locally?

No, once installed, Flux Schnell runs entirely offline on your local machine.

Can I use Flux Schnell on devices other than a Mac?

Yes, Flux Schnell supports various platforms. However, installation steps may vary based on your hardware and operating system.

How can I improve image quality?

Increase the num_inference_steps and adjust the guidance_scale parameters in the script for finer results.

What should I do if I encounter memory errors?

Reduce the image size or max_sequence_length in the script to conserve memory.