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

# MAGI-2-preview

> Run native MAGI-2-preview joint audio-video generation with SGLang Diffusion.

## 1. Quick start

Follow the [SGLang Diffusion installation guide](https://docs.sglang.io/docs/sglang-diffusion/installation.md) for your GPU platform. On CUDA:

```bash Command theme={null}
uv pip install "sglang[diffusion]" --prerelease=allow
```

The 8-GPU recipe reads its expert-parallel degree from a pipeline configuration
file, since `ep_size` has no CLI flag. Create it before launching:

```bash Command theme={null}
echo '{"ep_size": 4}' > magi2-ep4.json
```

## 2. Model capabilities

[MAGI-2-preview](https://huggingface.co/sand-ai/MAGI-2-preview) is a 114B-parameter
mixture-of-experts diffusion model from SandAI that generates a video and its
synchronized stereo soundtrack in one denoising trajectory. Roughly 6B parameters
are active per token, so serving cost tracks the active path far more closely than
the 114B total.

Generation is text-to-video-and-audio, or image-plus-text-to-video-and-audio
(TI2V) when a conditioning image is supplied. Audio is not a second pass over a
finished video: the video and audio latents share one packed sequence and one
denoise loop.

> [!WARNING]
>
> Review the license and usage terms on the MAGI-2-preview model card before
> production or commercial use. SGLang support does not grant additional model
> usage rights.

One root checkpoint holds two DiT partitions, which SGLang loads as separate
components rather than as separate model IDs:

<table>
  <colgroup>
    <col />

    <col />

    <col />

    <col />

    <col />
  </colgroup>

  <thead>
    <tr>
      <th>Partition</th>
      <th>Layers</th>
      <th>Hidden</th>
      <th>Attention</th>
      <th>MLP</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>Preview DiT</td>
      <td>40</td>
      <td>3072</td>
      <td>24 heads with attention sinks, 4-stream hyper-connections</td>
      <td>12-head MoE over 256 experts, top-6</td>
    </tr>

    <tr>
      <td>Refiner DiT</td>
      <td>30</td>
      <td>4096</td>
      <td>32 heads, GQA with 8 query groups, block-window local attention in every layer</td>
      <td>Dense</td>
    </tr>
  </tbody>
</table>

Use the root model ID `sand-ai/MAGI-2-preview`. SGLang owns the mapping from that
root to the preview weights, refiner weights, video VAE, distilled turbo decoder,
and audio VAE. Do not point `--model-path` at a manually downloaded
subdirectory.

### Quality tiers

<table>
  <colgroup>
    <col />

    <col />

    <col />

    <col />
  </colgroup>

  <thead>
    <tr>
      <th>Tier</th>
      <th>Delivered geometry</th>
      <th>Delivered rate</th>
      <th>Passes</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>Preview only</td>
      <td>896x512</td>
      <td>12.5 fps</td>
      <td>Preview DiT</td>
    </tr>

    <tr>
      <td>Two-stage 1080p (default)</td>
      <td>1920x1088</td>
      <td>25 fps</td>
      <td>Preview DiT, then refiner</td>
    </tr>
  </tbody>
</table>

Both tiers produce a 10-second clip with joint stereo audio at 44100 Hz. The
default two-stage tier delivers 249 frames: the preview pass generates at
896x512, and the refiner upsamples to 1920x1088 while halving the temporal VAE
stride, which doubles the delivered frame rate.

The output canvas is 1088 tall rather than 1080 because the VAE stride forces
every spatial dimension to a multiple of 16. Only the two resolutions above are
accepted; any other `width`/`height` pair is rejected before denoising.

## 3. Parallelism

MAGI-2 layers two parallel decompositions over the same ranks. Ulysses sequence
parallelism spans every rank and shards the packed sequence, and expert
parallelism shares those same ranks to split the preview MoE by head. Expert
parallelism defaults to the world size, but can be sized below it — which is what
makes 8 ranks work, since 8 does not divide the 12 MoE heads.

`--num-gpus` must divide every Ulysses head axis, because a rank owns whole heads
on all of them. The MoE heads are sharded by `ep_size` instead, so they are checked
against that. All four are checked at launch rather than deep inside a collective:

<table>
  <colgroup>
    <col />

    <col />

    <col />
  </colgroup>

  <thead>
    <tr>
      <th>Axis</th>
      <th>Count</th>
      <th>Divided by</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>Preview attention heads</td>
      <td>24</td>
      <td>`--num-gpus`</td>
    </tr>

    <tr>
      <td>Refiner attention heads</td>
      <td>32</td>
      <td>`--num-gpus`</td>
    </tr>

    <tr>
      <td>Refiner key/value heads (GQA)</td>
      <td>8</td>
      <td>`--num-gpus`</td>
    </tr>

    <tr>
      <td>Preview MoE heads</td>
      <td>12</td>
      <td>`ep_size`</td>
    </tr>
  </tbody>
</table>

Divisibility alone does not make a count usable, because the expert weights also
have to fit. The 114B headline is the preview partition — 113.9B parameters, 228 GB
in BF16 — and only its expert tensors shard; the 6.7B refiner is separate and is
replicated on every rank. Measured on 143 GB H200 cards, two-stage 1080p tier, 100
preview plus 5 refiner steps, steady-state clip:

<table>
  <colgroup>
    <col />

    <col />

    <col />
  </colgroup>

  <thead>
    <tr>
      <th>GPUs</th>
      <th>`ep_size`</th>
      <th>Result</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>8</td>
      <td>4</td>
      <td>**253s**, run eager — see below</td>
    </tr>

    <tr>
      <td>4</td>
      <td>4 (default)</td>
      <td>**348s** compiled, the recommended default</td>
    </tr>

    <tr>
      <td>2</td>
      <td>2</td>
      <td>**689s**, requires `--dit-layerwise-offload`</td>
    </tr>

    <tr>
      <td>1</td>
      <td>1</td>
      <td>out of memory while loading weights</td>
    </tr>
  </tbody>
</table>

**Use 4 GPUs unless you have a reason not to.** 8 is 1.37x faster per clip but
spends 46% more GPU-time to get there, so it buys latency rather than throughput;
2 and 4 cost the same GPU-seconds per clip. Two concurrent 4-GPU deployments beat
one 8-GPU deployment on total output.

> [!WARNING]
>
> 6 and 12 GPUs satisfy the preview head axes but fail the refiner's 8 key/value
> heads, so they are preview-only counts.
>
> 8 GPUs divides every Ulysses axis but not the preview's 12 MoE heads, so it needs
> an explicit `ep_size` of 4 — two expert-parallel groups of 4 ranks, each holding
> all 12 heads. It also wants **eager mode**: `torch.compile` regresses the preview
> pass badly at this rank count (335s compiled against 184s eager). The reference
> implementation instead pads the head axis to 16 and gives the two spare ranks
> zero-weight experts; SGLang does not need that padding.

The denoise loop runs both CFG branches itself, so SGLang must not also split CFG
across ranks. Passing `--enable-cfg-parallel` is rejected rather than silently
double-applied.

Data parallelism (`--dp-size` greater than 1) and every attention backend other
than FlashAttention are also rejected at startup.

Tensor parallelism is rejected too. No layer in either DiT is column- or
row-parallel, so `--tp-size` shards nothing and only takes ranks away from
Ulysses. Measured at 896x512, 20 steps on 4x H200. These are whole-stage times
including per-process warmup, so do not divide them into a per-step cost and
compare against the 100-step figures in section 8:

<table>
  <colgroup>
    <col />

    <col />

    <col />
  </colgroup>

  <thead>
    <tr>
      <th>Configuration</th>
      <th>Time</th>
      <th>Per-rank memory</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`--ulysses-degree 4`</td>
      <td>**124s**</td>
      <td>85 GB</td>
    </tr>

    <tr>
      <td>`--tp-size 2 --ulysses-degree 2`</td>
      <td>206s</td>
      <td>120-133 GB</td>
    </tr>

    <tr>
      <td>`--tp-size 4 --ulysses-degree 1`</td>
      <td>608s</td>
      <td>135 GB</td>
    </tr>
  </tbody>
</table>

The memory column is per-rank device memory observed during denoise, which is a
different metric from the runtime's own `Max peak` quoted in section 8; read the
rows against each other, not across sections. The `--tp-size` rows predate the
startup rejection and cannot be reproduced without removing that check.

Slower *and* larger, so put the whole degree in `--ulysses-degree`.

## 4. Serve MAGI-2-preview

The recommended four-card launch. Ulysses spans all four ranks, and expert
parallelism defaults to that same world size, which divides the 12 MoE heads:

```bash 4 GPUs recommended theme={null}
sglang serve \
  --model-path sand-ai/MAGI-2-preview \
  --num-gpus 4 \
  --ulysses-degree 4 \
  --performance-mode speed \
  --port 30010
```

`--performance-mode speed` keeps components GPU-resident for lowest latency.
Use it only after confirming the workload fits; see the offload section below
when it does not.

Both DiTs use attention sinks and a packed variable-length attention path that
are implemented only on the FlashAttention backend. Sink logits are silently
dropped on any other backend, so MAGI-2 restricts itself to FlashAttention and
does not accept `--attention-backend sage_attn`.

The first launch downloads the checkpoint. If the repository requires
authentication, export a Hugging Face token in the server environment.

## 5. Generate video and audio

> [tab] T2VA
>
> Text only. The defaults already describe the two-stage 1080p tier, so a minimal
> request changes nothing but the prompt and seed.
>
> Pass long prompts inline with `--prompt`. Do not use `--prompt-file-path` or
> `--prompt-path` for a single long caption: both read a file and split it into one
> request per non-empty line, which turns a wrapped paragraph into many unrelated
> short prompts.
>
> ```bash Command theme={null}
> sglang generate \
>   --model-path sand-ai/MAGI-2-preview \
>   --num-gpus 4 \
>   --ulysses-degree 4 \
>   --performance-mode speed \
>   --prompt "A cellist tunes up alone in a wood-panelled rehearsal room at dusk, then draws one long low note; dust turns in the last of the window light and the room's natural reverb settles around the sound." \
>   --seed 1101 \
>   --save-output
> ```

> [tab] TI2V
>
> Supply a conditioning image with `--image-path`. Prefer it over the shorter
> spellings: `--image-path` and its `--image-paths` alias are the flags that
> actually feed image-to-video conditioning, and several other flags begin with
> `--image`.
>
> MAGI-2 VAE-encodes the original conditioning image rather than a resized and
> centre-cropped copy, so the generic TI2V preprocessing path is deliberately
> skipped.
>
> ```bash Command theme={null}
> sglang generate \
>   --model-path sand-ai/MAGI-2-preview \
>   --num-gpus 4 \
>   --ulysses-degree 4 \
>   --performance-mode speed \
>   --prompt "The camera pushes in slowly on the supplied frame as steam rises from the cup and quiet kitchen sounds settle in." \
>   --image-path "<YOUR-IMAGE-PATH>" \
>   --seed 2101 \
>   --save-output
> ```

> [tab] Preview-only tier
>
> Request the preview geometry to skip the refiner entirely. A request delivered
> at the preview size leaves the refiner nothing to do, so the second pass is
> skipped even when its weights are loaded:
>
> ```bash Command theme={null}
> sglang generate \
>   --model-path sand-ai/MAGI-2-preview \
>   --num-gpus 4 \
>   --ulysses-degree 4 \
>   --performance-mode speed \
>   --width 896 \
>   --height 512 \
>   --prompt "Rain moves across a quiet street at night while a single neon sign buzzes overhead." \
>   --seed 3101 \
>   --save-output
> ```
>
> > [!NOTE]
> >
> > `--fps` is validated as an integer, so the preview tier's 12.5 fps delivered
> > rate cannot be named exactly on the command line. Leave `--num-frames` and
> > `--fps` at their defaults and select the tier through `--width`/`--height`
> > alone.

To skip loading the refiner weights altogether rather than skipping the pass,
turn the refiner off in the pipeline configuration. There is no CLI flag for it;
see the configuration notes below.

## 6. Offload

The preview DiT's expert weights are the memory driver. There are 256 experts
per head across 12 heads in each of 36 MoE layers, and they dominate resident
footprint even though only 6 experts per head are active for a given token. When
a topology cannot hold them, offload the DiT rather than shrinking the topology.
On two cards offload is not optional — the expert weights do not fit without it.

```bash 4 GPUs with DiT layerwise offload theme={null}
sglang serve \
  --model-path sand-ai/MAGI-2-preview \
  --num-gpus 4 \
  --ulysses-degree 4 \
  --performance-mode memory \
  --dit-layerwise-offload \
  --dit-offload-prefetch-size 1 \
  --port 30010
```

`--dit-layerwise-offload` streams DiT blocks with asynchronous host-to-device
prefetch overlap and selects only the DiT layerwise group. Measured at 896x512
over 20 steps, it moves peak from 96202 MiB to 83594 MiB and costs 1.5 s — 13% of
peak memory for 1.2% of latency, because the prefetch overlaps transfers with
compute. This is the lever to reach for first.

Raise `--dit-layerwise-resident-layers` to pin leading blocks on GPU across
denoise steps when VRAM allows; resident layers are transferred once instead of
being re-streamed every step, which trades memory for latency more efficiently
than raising the prefetch size.

`--cpu-offload-components dit` did not reduce peak on the same measurement (96202
MiB either way): the DiT has to be resident through denoise, so whole-component
offload has nothing to move. `--performance-mode memory` likewise lands within a
couple of percent of `speed` on both time and peak. Neither is a substitute for
the layerwise flag here.

The Qwen3.5 text encoder is 52 GiB and stays on the host regardless of these
flags, and its forward runs there too. Staging it onto a card costs 42-84 s for a
3 s GPU forward against 24 s on the CPU, and the transfer spikes 52 GiB on top of
the resident DiTs, which is enough to fail the second request of a run. Only one
rank encodes; the embedding is broadcast to the others.

> [!WARNING]
>
> Set `PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True` when running the
> two-stage 1080p tier. Both DiTs stay resident through decode, and the 1080p
> window decode requests a single \~9 GiB block; without expandable segments that
> allocation fails on fragmentation while tens of GiB sit reserved but unallocated.

> [!WARNING]
>
> DiT layerwise offload and cache-dit are mutually exclusive. With
> `SGLANG_CACHE_DIT_ENABLED` set and DiT selected for layerwise offload, SGLang
> raises rather than running: cache-dit may reuse a skipped block whose weights
> layerwise offload has already released, which surfaces as a shape mismatch.
> Disable `--dit-layerwise-offload`, remove `dit` from
> `--layerwise-offload-components`, or unset `SGLANG_CACHE_DIT_ENABLED`. DiT
> layerwise offload also disables FSDP inference, with a warning rather than an
> error.

## 7. Configuration notes

* Only 1920x1088 and 896x512 are accepted. The tier is selected by the
  requested geometry, not by a separate flag.
* The clip length is fixed at 10 seconds. `num_frames` and `fps` must agree with
  that duration to within one frame, which is why the default is 249 frames at
  25 fps rather than a round 250.
* 249 is `(63 - 1) * 4 + 1`. The refiner latent has 63 frames and the turbo
  decoder is causal, so the first latent yields one frame and each later one
  yields four.
* Video and audio carry separate guidance scales, and the refiner carries its own
  pair. The request defaults are 5.0 and 7.0 for the preview pass, and 2.0 and
  5.0 for the refiner. These are request fields, not CLI flags;
  `--guidance-scale` sets the video scale only.
* `--num-inference-steps` defaults to 100 for the preview pass. Neither DiT is
  step-distilled, so this dominates wall-clock time. The refiner runs 5 steps.
* The refiner, the expert-parallel degree, and the distilled turbo decoder are
  pipeline-configuration fields (`enable_refiner`, `ep_size`, `use_turbo_vae`)
  with no dedicated CLI flags. Set them by passing a JSON file to
  `--pipeline-config-path`, for example `{"ep_size": 4}` for the 8-card launch.
  Turning off `use_turbo_vae` falls back to the video VAE decode path and is a
  configuration-only change.
* Audio is delivered as stereo at 44100 Hz. The audio VAE decodes at 51200 Hz
  and is resampled for the container.
* The text encoder is a Qwen3.5 hybrid backbone loaded through transformers.
  Prompts are padded to the longest in the batch and truncated at 7000 tokens.

## 8. Performance

MAGI-2's cost is dominated by expert-weight movement and by the packed joint
audio-video sequence, both of which depend heavily on GPU model, interconnect,
offload policy, and the selected quality tier. Benchmark your own topology before
sizing a deployment; the figures below are one data point, not a target.

Measured on H200 (143771 MiB each), two-stage 1080p tier, 100 preview steps plus 5
refiner steps, one 10-second clip, seed and prompt held fixed. Each column is that
topology's best configuration, and all are steady state, so the first clip of a
process costs more while its graphs build:

<table>
  <colgroup>
    <col />

    <col />

    <col />

    <col />
  </colgroup>

  <thead>
    <tr>
      <th>Phase</th>
      <th>2 GPUs</th>
      <th>4 GPUs</th>
      <th>8 GPUs</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`ep_size`</td>
      <td>2</td>
      <td>4</td>
      <td>4</td>
    </tr>

    <tr>
      <td>Compile</td>
      <td>regional</td>
      <td>regional</td>
      <td>**none**</td>
    </tr>

    <tr>
      <td>Offload</td>
      <td>layerwise</td>
      <td>none</td>
      <td>none</td>
    </tr>

    <tr>
      <td>Preview denoise (100 steps)</td>
      <td>590.4 s</td>
      <td>286.9 s</td>
      <td>183.9 s</td>
    </tr>

    <tr>
      <td>Preview-to-refiner handoff</td>
      <td>\<0.2 s</td>
      <td>\<0.2 s</td>
      <td>\<0.2 s</td>
    </tr>

    <tr>
      <td>Refiner denoise (5 steps)</td>
      <td>79.4 s</td>
      <td>41.3 s</td>
      <td>31.6 s</td>
    </tr>

    <tr>
      <td>Decode (video and audio)</td>
      <td>7.1 s</td>
      <td>8.8 s</td>
      <td>12.2 s</td>
    </tr>

    <tr>
      <td>**Total**</td>
      <td>**689 s**</td>
      <td>**348 s**</td>
      <td>**253 s**</td>
    </tr>

    <tr>
      <td>Peak memory</td>
      <td>132208 MiB</td>
      <td>85274 MiB</td>
      <td>126572 MiB</td>
    </tr>

    <tr>
      <td>GPU-seconds per clip</td>
      <td>1378</td>
      <td>1393</td>
      <td>2028</td>
    </tr>
  </tbody>
</table>

Read the last row before choosing 8 GPUs: it is 1.37x faster per clip but spends 46%
more GPU-time, while 2 and 4 GPUs cost the same GPU-seconds. Decode also moves the
wrong way with scale, so latency gains come entirely from denoise.

**Compile is not a free win at every rank count.** At 4 GPUs and below, regional
compile is worth roughly a quarter of the clip and 46 GiB of peak, so reach for it
first. At 8 GPUs it costs more than it saves — the preview pass measures 335 s
compiled against 184 s eager — so leave it off there. Plain `--enable-torch-compile`
without `--regional-compile` compiles the whole transformer and fails on sm90:
inductor autotunes the multi-head MoE GEMMs and asks 262144 bytes of shared memory
against the 232448 available. Pair the two flags or use neither.

Expect a couple of percent of drift between processes on a shared machine; compare
configurations within one run rather than against this table.

At the preview-only tier the refiner and the larger decode both drop out, which is
most of the clip's second half. Supplying a conditioning image costs a further
17-24 s at either tier, for the extra 1680 patch tokens.

Weight loading is separate and paid once per process: roughly 5 minutes for both
DiTs, the expert tensors dominating.

Text encoding is not in the table because it tracks prompt length rather than rank
count. The encoder runs on the host and its weights are memory-mapped from storage, so
a short caption costs about a second once those weights are in the page cache, while a
1900-token caption takes about 24 s.

The preview pass — 2.9 s/step on four cards, 1.8 s/step on eight — dominates, so step
count is the first knob for trading quality against latency; the refiner's 5 steps are
not worth tuning first.

## 9. Run in ComfyUI

MAGI-2 has no ComfyUI route. The
[SGLDiffusion plugin](https://github.com/sgl-project/sglang/tree/main/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion)
ships no MAGI-2 node and no integrated-mode executor for it. The generic
`SGLDiffusion Generate Video` node has not been validated against this model,
and its default request of 1280x720 and 120 frames at 24 fps is rejected by the
geometry and clip-length checks in section 7. Use `sglang serve` or
`sglang generate` directly.


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