Developer docs / AI Fabric tutorials

Flux image generation

  • GPU
  • Volume
  • Image DSL

Take it for a spin

The whole program in one file, ready to run.

flux_gen.py
import bladekit
import io

app = bladekit.app("flux-gen")

hf_cache = bladekit.Volume.from_name("hf-cache", create_if_missing=True)

image = (
    # fabric/torch already bakes CUDA + PyTorch, so do NOT re-pip torch on top.
    bladekit.Image.from_catalog("fabric/torch")
    .apt_install("ffmpeg")
    .pip_install(
        "diffusers", "transformers",
        "accelerate", "safetensors", "sentencepiece",
        # hf_transfer parallelizes the ~23 GB weight download (the plain
        # downloader can stall on files this large).
        "hf_transfer",
    )
    .env({"HF_HOME": "/hf-cache", "HF_HUB_ENABLE_HF_TRANSFER": "1"})
)

_pipe = None

@app.function(
    gpu="RTX-A4500",
    image=image,
    timeout=1800,
    volumes={"/hf-cache": hf_cache},
    # FLUX.1-schnell is a gated HF repo; inject a token via a Secret.
    secrets=[bladekit.Secret.from_name("hf-token")],
)
def generate(prompt: str, width: int = 1024, height: int = 1024) -> bytes:
    global _pipe
    import torch
    from diffusers import FluxPipeline

    if _pipe is None:
        _pipe = FluxPipeline.from_pretrained(
            "black-forest-labs/FLUX.1-schnell",
            torch_dtype=torch.bfloat16,
        )
        # The 12B transformer alone is ~24 GB in bf16, over the A4500's ~20 GB,
        # so enable_model_cpu_offload still OOMs at generation. Sequential offload
        # streams each submodule through the GPU (a few GB of VRAM, slower). Do
        # NOT also call .to("cuda"); the two are mutually exclusive.
        _pipe.enable_sequential_cpu_offload()

    result = _pipe(prompt, width=width, height=height, num_inference_steps=4)
    buf = io.BytesIO()
    result.images[0].save(buf, format="PNG")
    return buf.getvalue()

@app.local_entrypoint()
def main():
    import pathlib
    png = generate.remote("a photorealistic GPU data center at night, cinematic")
    pathlib.Path("flux-out.png").write_bytes(png)
    print("Saved flux-out.png")

Run it

  1. Save as flux_gen.py. Run blade run flux_gen.py.
  2. Flux Schnell uses 4 inference steps, much faster than SDXL's 30. Under sequential CPU offload on the A4500 each step takes ~60 s, so expect ~4-5 min of generation.
  3. Weights (~23 GB) are cached in the hf-cache Volume: the first run spends ~6 min downloading, later runs skip it entirely.
  4. Change width/height to 768/1344 for portrait images.

Concepts used: Cold start, Image build, GPU

Contact the team

Loading the form…