|
| 1 | +"""Test FLUX.2 Turbo LoRA model serving. |
| 2 | +
|
| 3 | +Server setup: |
| 4 | + CUDA_VISIBLE_DEVICES=4,5,6,7 torchrun --nproc_per_node=4 \ |
| 5 | + -m cache_dit.serve.serve \ |
| 6 | + --model-path black-forest-labs/FLUX.2-dev \ |
| 7 | + --lora-path fal/FLUX.2-dev-Turbo \ |
| 8 | + --lora-name flux.2-turbo-lora.safetensors \ |
| 9 | + --parallel-type ulysses \ |
| 10 | + --parallel-text-encoder \ |
| 11 | + --quantize-type float8_wo \ |
| 12 | + --attn _flash_3 \ |
| 13 | + --cache \ |
| 14 | + --compile \ |
| 15 | + --ulysses-anything |
| 16 | +
|
| 17 | +This test calls /generate with a custom sigma schedule (TURBO_SIGMAS) for 8-step turbo inference. |
| 18 | +
|
| 19 | +Reference LoRA: https://huggingface.co/fal/FLUX.2-dev-Turbo |
| 20 | +Base model: https://huggingface.co/black-forest-labs/FLUX.2-dev |
| 21 | +""" |
| 22 | + |
| 23 | +import os |
| 24 | +import requests |
| 25 | +import base64 |
| 26 | +from PIL import Image |
| 27 | +from io import BytesIO |
| 28 | + |
| 29 | + |
| 30 | +# Pre-shifted custom sigmas for 8-step turbo inference |
| 31 | +TURBO_SIGMAS = [1.0, 0.6509, 0.4374, 0.2932, 0.1893, 0.1108, 0.0495, 0.00031] |
| 32 | + |
| 33 | + |
| 34 | +def call_api(prompt, name="flux2_turbo", **kwargs): |
| 35 | + host = os.environ.get("CACHE_DIT_HOST", "localhost") |
| 36 | + port = int(os.environ.get("CACHE_DIT_PORT", 8000)) |
| 37 | + url = f"http://{host}:{port}/generate" |
| 38 | + |
| 39 | + payload = { |
| 40 | + "prompt": prompt, |
| 41 | + "width": kwargs.get("width", 1024), |
| 42 | + "height": kwargs.get("height", 1024), |
| 43 | + "num_inference_steps": kwargs.get("num_inference_steps", 8), |
| 44 | + "guidance_scale": kwargs.get("guidance_scale", 2.5), |
| 45 | + "sigmas": kwargs.get("sigmas", TURBO_SIGMAS), |
| 46 | + "seed": kwargs.get("seed", 42), |
| 47 | + "num_images": kwargs.get("num_images", 1), |
| 48 | + } |
| 49 | + |
| 50 | + if "output_format" in kwargs: |
| 51 | + payload["output_format"] = kwargs["output_format"] |
| 52 | + if "output_dir" in kwargs: |
| 53 | + payload["output_dir"] = kwargs["output_dir"] |
| 54 | + |
| 55 | + response = requests.post(url, json=payload, timeout=600) |
| 56 | + response.raise_for_status() |
| 57 | + result = response.json() |
| 58 | + |
| 59 | + assert "images" in result and result["images"], "No images in response" |
| 60 | + |
| 61 | + if payload.get("output_format", "base64") == "path": |
| 62 | + filename = result["images"][0] |
| 63 | + assert os.path.exists(filename) |
| 64 | + img = Image.open(filename) |
| 65 | + print(f"Saved: {filename} ({img.size[0]}x{img.size[1]})") |
| 66 | + return filename |
| 67 | + |
| 68 | + img_data = base64.b64decode(result["images"][0]) |
| 69 | + img = Image.open(BytesIO(img_data)) |
| 70 | + |
| 71 | + filename = f"{name}.png" |
| 72 | + img.save(filename) |
| 73 | + print(f"Saved: {filename} ({img.size[0]}x{img.size[1]})") |
| 74 | + return filename |
| 75 | + |
| 76 | + |
| 77 | +def test_flux2_turbo_lora(): |
| 78 | + prompt = ( |
| 79 | + "Industrial product shot of a chrome turbocharger with glowing hot exhaust manifold, " |
| 80 | + "engraved text 'FLUX.2 [dev] Turbo by fal' on the compressor housing and 'fal' on the turbine wheel, " |
| 81 | + "gradient heat glow from orange to electric blue , studio lighting with dramatic shadows, " |
| 82 | + "shallow depth of field, engineering blueprint pattern in background." |
| 83 | + ) |
| 84 | + |
| 85 | + return call_api( |
| 86 | + prompt=prompt, |
| 87 | + name="flux2_turbo_lora", |
| 88 | + num_inference_steps=8, |
| 89 | + guidance_scale=2.5, |
| 90 | + sigmas=TURBO_SIGMAS, |
| 91 | + width=1024, |
| 92 | + height=1024, |
| 93 | + seed=42, |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + print("=" * 80) |
| 99 | + print("Testing FLUX.2 Turbo LoRA Model Serving") |
| 100 | + print("=" * 80) |
| 101 | + test_flux2_turbo_lora() |
| 102 | + print("=" * 80) |
| 103 | + print("Done") |
| 104 | + print("=" * 80) |
0 commit comments