> ## Documentation Index
> Fetch the complete documentation index at: https://docs.m-star.org/llms.txt
> Use this file to discover all available pages before exploring further.

# FLUX.2 klein and Z-Image

> Generate images with klein or Z-Image, and edit a reference image with klein.

Use FLUX.2 klein for text-to-image or a reference-image edit. Choose `flux2_klein` (4B) or `flux2_klein_9b`. Z-Image-Turbo serves text-to-image only.

## Install and start

Every default fits its stages on one GPU. Access to the 9B weights is gated. Check the [9B checkpoint terms](https://huggingface.co/black-forest-labs/FLUX.2-klein-9B) and obtain download access before starting it.

### `flux2_klein`

Text, image → image. The default `flux2_klein.yaml` uses 1 GPU. The [weights](https://huggingface.co/black-forest-labs/FLUX.2-klein-4B) use Apache-2.0.

```bash theme={null}
uv pip install --torch-backend=auto "mstar-ai[flux2_klein]"
mstar serve flux2_klein
```

[Send the request](#generate-image).

### `flux2_klein_9b`

Text, image → image. The default `flux2_klein_9b.yaml` uses 1 GPU. The [weights](https://huggingface.co/black-forest-labs/FLUX.2-klein-9B) use FLUX Non-Commercial.

```bash theme={null}
uv pip install --torch-backend=auto "mstar-ai[flux2_klein]"
mstar serve flux2_klein_9b
```

[Send the request](#generate-image).

### `z_image_turbo`

Text → image. The default `z_image_turbo.yaml` uses 1 GPU. The [weights](https://huggingface.co/Tongyi-MAI/Z-Image-Turbo) use Apache-2.0.

```bash theme={null}
uv pip install --torch-backend=auto "mstar-ai[flux2_klein]"
mstar serve z_image_turbo
```

[Send the request](#generate-image).

## Send a request

Run either client example in a second terminal after `/health` returns 200.

### Generate an image

Save the returned PNG.

**Python SDK**

```python theme={null}
from pathlib import Path
from mstar import MStarClient

client = MStarClient("http://localhost:8000")
image = client.generate_image("An orange cat watching snow fall.")
Path("image.png").write_bytes(image)
```

**curl**

```bash theme={null}
curl -fsS http://localhost:8000/v1/images/generations \
  -H 'Content-Type: application/json' \
  -d '{"prompt":"An orange cat watching snow fall."}' \
  | python3 -c 'import json, sys; print(json.load(sys.stdin)["data"][0]["b64_json"])' \
  | base64 --decode > image.png
```

## Deployment configs

| File | GPUs | Parallelism | When to use |
| - | - | - | - |
| `configs/flux2_klein.yaml` | 1 | none | Default; up to 8 requests per step. |
| `configs/flux2_klein_9b.yaml` | 1 | none | Default; up to 4 requests per step. |
| `configs/z_image_turbo.yaml` | 1 | none | Default; up to 8 requests per step. |

Each captures the denoise step at 1024x1024 and sets `resources: dit_attn: backend: sdpa`.

To use an alternative, pass `--config configs/<file>` after the launcher name. From a wheel installation, [copy the shipped YAML](https://docs.m-star.org/serving.md#custom-configs) and pass its local path.

## Settings and defaults

Set these under `model_kwargs` in the config.

| Knob | Default | Effect |
| - | - | - |
| `compile` | `true` | `torch.compile` of the transformer, one trace per shape. |
| `compile_eager_rounding` | `true` | Inductor rounds intermediates where eager PyTorch does and fuses no FMAs. |
| `compile_exact_ops` | `false` | Set `true` or list `norms` or `activations` to keep those operations’ eager results inside the compiled forward. The klein configs set `[norms]`. Z-Image uses `false`. |
| `accelerator_graph` | `true` | Capture the denoise step for each `capture_sizes` entry (`[[1024, 1024]]`) and `capture_batch_sizes` entry (`[1, 2, 4, 8]`). Other shapes run eager. |
| `capture_edit_sizes` | `capture_sizes` | klein only: edit shapes with one reference image of the output size. `[]` captures none. |
| `capture_caption_lengths` | `[32, 64]` | Z-Image only: caption lengths captured per batch size. |
| `max_batch_size` | `8` | Rows per denoise step. |
| `max_image_area` | `4194304` (2048x2048) | Larger requests are a 400. |
| `vae_compile` | `false` | Compiled VAE decode. The shipped configs turn it on; set `false` for bit-exact, repeatable output. |
| `async_scheduling` | `true` | Build the next denoise step while the current one runs. `false` is lockstep. |
| `linear_dtype` | `bf16` | `mxfp8` or `nvfp4` runs the block linears through cuBLASLt's block-scaled GEMM. Needs compute capability 10.0 or higher; other GPUs stay on bf16 with a warning. Images change against bf16, so it is off by default. |
| `linear_keep_bf16` | none | Module-name patterns kept in bf16 under a low-precision `linear_dtype`, for example `single_blocks.0-3.*`. |
| `linear_dtype_steps` | none | Run some denoise steps in bf16: a count of leading steps, or a dtype per step. Holds a second copy of the weights. |
| `lora` | none | klein only: adapters merged into the weights at load, `[{path: ..., scale: ...}]` (diffusers, PEFT or BFL-layout safetensors). |

With the earlier compiled VAE, plain compilation with eager rounding gave median image PSNR against eager of 37.6 dB for klein-4B and 35.6 dB for klein-9B over 100 prompts. Keeping the norms made both transformers bit-exact with eager in tests before q/k norm fusion. Z-Image measured 34 dB median before fusion. Keeping its 180 norms per step raised every tested prompt to at least 55.9 dB and cost 45% of batch-one latency. [Measurement details](https://github.com/mstar-project/mstar/blob/69ee254d14d1a47e09600164c305acda7fe11641/docs/models.rst#L2542-L2566)

`backend` on the `dit_attn` resource picks the attention kernel: `sdpa` runs torch's SDPA, as the configs ship, and a ragged-attention backend such as `flashinfer` runs the engine's ragged attention resource. Remove the block to let the GPU's kernel table choose.

## Limits

* Z-Image refuses reference images and sides over 8192 px.
* `/v1/images/edits` takes one image and no mask. Send several reference images through the SDK or `/generate`.
* klein's `lora` is a merge at load, not a per-request adapter, and a DoRA adapter is refused. Neither model serves [per-request LoRA](https://docs.m-star.org/models/lora.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.