Skip to content

Instantly share code, notes, and snippets.

@awni
Last active February 24, 2025 14:33
Show Gist options
  • Save awni/a67d16d50f0f492d94a10418e0592bde to your computer and use it in GitHub Desktop.
Save awni/a67d16d50f0f492d94a10418e0592bde to your computer and use it in GitHub Desktop.

Setup the repo

git clone [email protected]:filipstrand/mflux.git
cd mflux && pip install -r requirements.txt

Make a run script

Name this anything, maybe flux.py. Make sure to update the two paths marked below.

import sys
sys.path.append("/path/to/mflux/repo/src")  # <- Update

from flux_1_schnell.config.config import Config
from flux_1_schnell.models.flux import Flux1Schnell

flux = Flux1Schnell("black-forest-labs/FLUX.1-schnell")

image = flux.generate_image(
    seed=3,
    prompt="A rocketship exactly in the style of the cartoon rocket emoji. The rocket ship is flying in the sky. The logo 'MLX' is written on the side of the ship in black letters.",
    config=Config(num_inference_steps=2)
)

image.save("/path/to/save/image.png")  # <- Update

Run the script

python flux.py

NB The model weights will be downloaded on the first run. This can take a long time.

@filipstrand
Copy link

In terms of the size of the weights, we can get away with only using ~34GB of space since it only loads the following tensors:

https://github.com/filipstrand/mflux/blob/5023437e89b0a54232e0ecd05da6447ed2ea738d/src/flux_1_schnell/weights/weight_handler.py#L10

@awni
Copy link
Author

awni commented Aug 14, 2024

In terms of the size of the weights, we can get away with only using ~34GB of space since it only loads the following tensors:

It might be better to use snapshot_download directly to pull only the files you need.

That will also save needing to install and setup git-lfs and the entire first step of cloning the repo. It will also save needing specify the path to the model in the generation script.

Here's an example of how that works.

@filipstrand also please feel free to borrow any useful setup notes from this gist to put in your README directly.

@uogbuji
Copy link

uogbuji commented Aug 14, 2024

@filipstrand if you do follow awni's breadcrumbs & simplify the requirements, pls come mention that here, as well.

FWIW, my small variation on @awni's runner:

'''
Make sure you set `MFLUX_PATH` and `FLUX_SCHNELL_PATH` according to where you cloned these components.

Sample run: `time python flux.py fluxtest.png "Cartoon scene of the Boulder flatirons in winter"`
'''
import sys
from pathlib import Path

MFLUX_PATH = Path.home() / Path('src/mflux/src')
FLUX_SCHNELL_PATH = Path.home() / Path('src/FLUX.1-schnell')

sys.path.append(str(MFLUX_PATH))
output = sys.argv[1]
prompt = ' '.join(sys.argv[2:])

print('Output file (PNG):', output, '\n', 'Prompt:', prompt)

from flux_1_schnell.config.config import Config
from flux_1_schnell.models.flux import Flux1Schnell

flux = Flux1Schnell(str(FLUX_SCHNELL_PATH) + '/')

image = flux.generate_image(
    seed=3,
    prompt=prompt,
    config=Config(num_inference_steps=2)
)

image.save(output)

@filipstrand
Copy link

@awni Thanks a lot for the tip! This has now been updated in main.

@uogbuji Thanks for your provided example. I ended up going with what @awni suggested and used the snapshot_download feature. This also mirrors how a typical Huggingface Diffusers setup work, so I tried to stay as close to that as possible. If you want to have a look at the changes, they can be found in this commit: filipstrand/mflux@e3a85df

@uogbuji
Copy link

uogbuji commented Aug 14, 2024

@awni Thanks a lot for the tip! This has now been updated in main.

@uogbuji Thanks for your provided example. I ended up going with what @awni suggested and used the snapshot_download feature. This also mirrors how a typical Huggingface Diffusers setup work, so I tried to stay as close to that as possible. If you want to have a look at the changes, they can be found in this commit: filipstrand/mflux@e3a85df

Love it!!

@ivanfioravanti
Copy link

I'm adding M4 Max results: 18.7 seconds!

mflux-generate --prompt "Luxury food photograph" --model schnell --steps 2 2 1.75s user 10.71s system 66% cpu 18.721 total

@filipstrand
Copy link

@ivanfioravanti Added to the performance table!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment