Developer docs / AI Fabric tutorials
SDXL image generation
- GPU
- Volume
- Image DSL
Image models are heavy in a very specific place: their weights. Stable Diffusion XL is about 7 GB of parameters, and downloading that on every run would make generation feel glacial. The fix is to download once and keep the bytes around, so the slow part happens a single time and every run afterwards starts almost instantly.
In this tutorial you'll generate images with Stable Diffusion XL on a GPU, and cache the weights on an AI Fabric Volume so the cold start only bites once. You'll write plain Python: no Dockerfile, no GPU driver, no model server to babysit.
Before you start
You need the AI Fabric CLI installed and authenticated: run blade setup once. The function asks for a single RTX A4500 and builds on fabric/torch, the catalog image that already ships CUDA and PyTorch, so there's nothing to build by hand.
Create the app and a weights Volume
Create a file called sdxl.py. Start with the App and a Volume, a piece of storage that survives across runs. The weights download into it on the first run and are read straight back on every run after that.
import bladekit
import io
app = bladekit.app("sdxl")
# A Volume persists across runs: weights are downloaded once, reused forever.
hf_cache = bladekit.Volume.from_name("hf-cache", create_if_missing=True)create_if_missing=True means the first run makes the Volume and later runs reuse it. The name hf-cache is shared on purpose. Other Hugging Face tutorials point at the same Volume, so weights you've already pulled are there waiting.
Define the image
Start from the fabric/torch catalog base and layer the diffusion stack on top with .pip_install(...). The .env(...) call is the glue that makes caching work: it points HF_HOME at the Volume mount, so Hugging Face writes the downloaded weights into /hf-cache instead of an ephemeral container directory.
image = (
bladekit.Image.from_catalog("fabric/torch")
.pip_install("torch", "diffusers", "transformers", "accelerate", "safetensors")
.env({"HF_HOME": "/hf-cache"})
)Write the paint function
Now the function that does the work. The @app.function decorator is where you ask for hardware: request a single RTX A4500, the image you just defined, and mount the Volume at /hf-cache, the same path HF_HOME points at. The _pipe global keeps the model resident across calls in a warm container.
_pipe = None
@app.function(gpu="RTX-A4500", image=image, timeout=1800, volumes={"/hf-cache": hf_cache})
def paint(prompt: str, steps: int = 30) -> bytes:
global _pipe
import torch
from diffusers import StableDiffusionXLPipeline
if _pipe is None:
_pipe = StableDiffusionXLPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float16, variant="fp16", use_safetensors=True,
).to("cuda")The first call finds _pipe set to None, so it loads the SDXL pipeline in half precision (float16) and moves it onto the GPU. Every later call in the same warm container sees _pipe already populated and skips straight to generation, which is exactly why the first image is slow and the rest are quick.
Render and return the image
With the pipeline loaded, generating is a single call. Run the prompt for steps denoising steps, encode the result as a PNG into an in-memory buffer, and return the raw bytes, so the caller gets a file it can write straight to disk.
image = _pipe(prompt, num_inference_steps=steps).images[0]
buf = io.BytesIO()
image.save(buf, format="PNG")
return buf.getvalue()Call it from your machine
A local_entrypoint is the part that runs on your laptop. Calling paint.remote(...) ships the prompt to the GPU container, runs generation there, and returns the PNG bytes to you, which the entrypoint writes into a local sdxl-out/ folder.
@app.local_entrypoint()
def main():
import pathlib
out = pathlib.Path("sdxl-out")
out.mkdir(exist_ok=True)
png = paint.remote("a tiny blacksmith forge floating in space, dramatic lighting")
(out / "image.png").write_bytes(png)
print(f"Saved {len(png) / 1e6:.1f} MB PNG")Run it
Run the program with the AI Fabric CLI:
blade run sdxl.pyThe first run downloads the SDXL weights (~7 GB) into the Volume. That's the cold start, and it takes a few minutes. The second run finds the weights already in the Volume and starts much faster. When it finishes, your image lands at sdxl-out/image.png locally.
To turn this into an HTTP service, add @app.web_endpoint(methods=["POST"]) above paint and blade deploy it: same function, now reachable as a URL.
Take it for a spin
Everything above in one file, ready to run.
import bladekit
import io
app = bladekit.app("sdxl")
# A Volume persists across runs: weights are downloaded once, reused forever.
hf_cache = bladekit.Volume.from_name("hf-cache", create_if_missing=True)
image = (
bladekit.Image.from_catalog("fabric/torch")
.pip_install("torch", "diffusers", "transformers", "accelerate", "safetensors")
.env({"HF_HOME": "/hf-cache"})
)
_pipe = None
@app.function(gpu="RTX-A4500", image=image, timeout=1800, volumes={"/hf-cache": hf_cache})
def paint(prompt: str, steps: int = 30) -> bytes:
global _pipe
import torch
from diffusers import StableDiffusionXLPipeline
if _pipe is None:
_pipe = StableDiffusionXLPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float16, variant="fp16", use_safetensors=True,
).to("cuda")
image = _pipe(prompt, num_inference_steps=steps).images[0]
buf = io.BytesIO()
image.save(buf, format="PNG")
return buf.getvalue()
@app.local_entrypoint()
def main():
import pathlib
out = pathlib.Path("sdxl-out")
out.mkdir(exist_ok=True)
png = paint.remote("a tiny blacksmith forge floating in space, dramatic lighting")
(out / "image.png").write_bytes(png)
print(f"Saved {len(png) / 1e6:.1f} MB PNG")Concepts used: Cold start, Image build, GPU