Image editing with Qwen-Image 2.1 on an RTX 5090: architecture, profiling, and Blackwell kernels
magpie is a lean PyTorch runtime and custom
CUDA/Triton kernel stack for Qwen-Image 2.1 Turbo on
Blackwell (sm_120) GPUs. On a single 32 GB RTX
5090, it edits a 1024² image in 1.26 s and a
2048² image in 5.00 s end-to-end (request to PNG reply,
text encoder included) using its fast mode, or 1.49
s and 6.19 s at the full eight steps—both
passing a 16-image perceptual quality gate against the bf16 baseline.
The next-fastest engine measured on the same card (sglang
FP8 + SageAttention) takes 2.54 s and 12.07
s; the official diffusers pipeline with CPU
offload takes 28.0 s at 1024² and runs out of memory at
2048².
Contents. 1. The problem and model architecture · 2. Baseline profile and roofline · 3. Quality gating · 4. The optimisation ladder · 5. Core engineering decisions · 6. Discarded experiments · 7. RTX 5090 and cross-GPU results · 8. Showcase · 9. Using it and serving lessons · 10. Predictions, lessons, and what is left
1. The problem and model architecture
Out of the box, Qwen-Image 2.1 does not fit on a 32 GB consumer GPU.
Its three networks total 30.2 GiB in bf16 weights
alone, and the official diffusers pipeline peaks at
38.9 GiB for a 1024² edit and 64.6 GiB
for a 2048² edit:
| component | role | parameters | weights on disk (bf16) |
|---|---|---|---|
| Qwen3-VL | encodes prompt + reference images | 8.77 B | 16.3 GiB |
| DiT | single-stream denoising transformer (32 blocks) | 7.12 B | 13.3 GiB |
| VAE | 2D image ↔︎ latent codec (16× spatial, 64 ch) | 0.34 B | 0.6 GiB |
Single-stream sequence and prefix caching
There is no separate text stream or cross-attention. Text hidden
states (from Qwen3-VL's final decoder layer, before the final norm),
reference image VAE latents, and the noisy target image sit in
one concatenated sequence processed by 32 identical
blocks (width 4096, 32 heads × 128, SwiGLU MLP ratio 3, no biases, and
one Linear(4096 → 16384) modulation layer shared across all
blocks):
[ text ... <image slots> ... text ][ target image ]
└────────── prefix ────────────┘ └── redrawn each step ──┘
Reference images enter twice: Qwen3-VL reserves one
<|image_pad|> slot per 32×32 pixel patch, and the DiT
replaces each slot with four 16×16 VAE latent tokens (4,096 DiT tokens
at 1024², 16,384 at 2048²). Each block applies RMS QK-norm per head and
3-axis RoPE (sequence index, centred row, centred column):
x = x + tanh(gate1) * attention( norm(x) * (1 + scale1) )
x = x + tanh(gate2) * swiglu( norm(x) * (1 + scale2) )
Two architectural rules make the prefix invariant across denoising steps:
- Block-causal attention: prefix tokens attend to earlier tokens and all tokens of their own image, never to the target image.
causal_condition: prefix tokens are modulated as if timestept = 0; only target tokens receive the actual noise levelσ.
Step 1 runs prefill over the full sequence and
caches every layer's prefix keys and values; steps 2 onward run
decode on target tokens only against
[cached prefix, target] without an attention mask. Both
checkpoints use flow-matching Euler steps
(x ← x + (σ_next − σ) · v): base uses 40
shifted steps, while Turbo uses 8 fixed sigmas
(1.0, 0.978, 0.954, 0.927, 0.895, 0.845, 0.705, 0.415 → 0).
The lean PyTorch port
(src/magpie)
Before optimising anything, we reimplemented the DiT, 2D VAE,
sampler, and loop in PyTorch using the checkpoint's exact parameter
names so load_state_dict works directly. Replacing
diffusers's video-shaped 3D VAE decoder with a pure 2D
decoder (dropping unused time_conv weights) cut peak memory
by 5–21 GiB immediately (port DiT + VAE peaks:
17.2 GiB at 1024² and 26.8 GiB at
2048²) and shaved 3–5 % off latency. On tiny random weights the DiT
matches diffusers bit-for-bit (0.0 diff); on
real bf16 weights it reaches 39.9 dB (against
diffusers's own 41.4 dB cache-on/off
floor).
Three subtle parity bugs surfaced only because we checked tiny random weights and free-running trajectories:
- Equal-dimension aliasing: slot padding used the
text encoder's hidden size instead of the DiT's (both are
4096, so only a tiny-weight test with unequal dims caught it). - CPU vs GPU constants: computing the 128 timestep
embedding frequencies via
expon GPU vs CPU differed in the last float32 ULP, which flipped rounded bf16 values after multiplying by 1000 and cost 26 dB. - Excess precision: multiplying
(σ_next − σ) · vin fp32 instead of bf16 before adding to the fp32 latent drifted the free-running loop from 54 dB down to 32–47 dB.
2. Baseline profile and roofline
Profiled on a 96 GB RTX PRO 6000 Blackwell (where the unoptimised bf16 pipeline fits) to locate bottlenecks before writing kernels.
The DiT takes 86 to 98 percent of every edit request. Percent of each request's total time, official pipeline, bf16, batch 1. Total seconds at right. One NVIDIA RTX PRO 6000 Blackwell (95 GiB), PyTorch 2.14.1, CUDA 13.0. Warm p50 of 5 Turbo and 3 base requests. The unfilled end of each bar is other overhead, under 3 %. VAE encode and decode are summed.
| official pipeline (RTX PRO 6000) | text encoder | VAE encode | DiT | VAE decode | other | total |
|---|---|---|---|---|---|---|
| Turbo, 1024² | 98 ms | 47 ms | 2,333 ms | 171 ms | 71 ms | 2,717 ms |
| Turbo, 2048² | 464 ms | 223 ms | 16,226 ms | 725 ms | 247 ms | 17,877 ms |
| base (40 steps), 1024² | 98 ms | 47 ms | 10,688 ms | 172 ms | 69 ms | 11,078 ms |
| base (40 steps), 2048² | 457 ms | 222 ms | 69,136 ms | 711 ms | 261 ms | 70,784 ms |
Turbo is 4.0 to 4.1 x faster than base, end to end. Seconds per edit request, warm p50, official pipeline, batch 1. At 2048 an edit costs 6.4 to 6.6 times its 1024 time. Each request includes the VAE encode of the input image. One RTX PRO 6000 Blackwell, bf16.
After the first step, the prefix cache halves each DiT step: 523 ms to 259 ms. DiT milliseconds per denoising step, edit at 1024, Turbo (8 steps), official pipeline. Step 1 computes the prefix either way. Steps 2 to 8 are averaged. Whole request: 2.72 s with the cache, 4.57 s without.
Dropping the text encoder drops peak memory below 32 GB, and the lean 2D VAE saves another 5 to 21 GiB. Peak GPU memory allocated, Turbo edit, bf16, batch 1. Official pipeline is diffusers. Lean port (DiT + VAE only) takes text states pre-computed. Dashed line: 32 GB (RTX 5090).
Inside the GPU trace
Perfetto traces of the lean port (DiT + VAE) revealed four facts that set the optimisation plan:
| GPU time inside labelled spans (port) | edit 1024² | edit 2048² (profiled, +8%) |
|---|---|---|
| MLP (SwiGLU) | 1,005 ms | 4,531 ms |
| QKV + QK-norm + RoPE | 542 ms | 2,846 ms |
| attention | 467 ms | 7,341 ms |
| attention output | 103 ms | 449 ms |
| VAE decode | 149 ms | 645 ms |
| VAE encode | 39 ms | 170 ms |
| all kernels | 2,406 ms | 16,660 ms |
At 1024², the MLP is 42 % of kernel time; at 2048², quadratic attention takes over at 44 % (16,384 queries over 32,768 keys).
DiT steps already run at 73–80 % of the card's measured model-shaped bf16 matmul (378.5 TFLOP/s; 8192³ square matmul 422 TFLOP/s; copy bandwidth 1,462 GB/s). Multiples of speedup require lower-precision tensor-core formats, not bf16 tuning.
Elementwise work (23 %: norms, RoPE, modulation, casts, copies) exceeds attention (18 %) at 1024².
Inside QKV, three matmuls take 306 ms while 22 small elementwise/copy kernels for QK-norm + RoPE take 226 ms (9 % of the request, 98 ms plain copies). Inside VAE decode (149 ms), convolutions are only 56 ms; norms/activations take 49 ms, copies 24 ms, and NCHW↔︎NHWC conversions 19 ms.
- No launch-bound gap: GPU kernels cover 98.6 % of a 1024² edit and 99.6 % at 2048² across ~12,700 launches (median gap 1.4 µs on the 1,711 ms build). CUDA graphs can win < 2 %.
- Ampere-era stock kernels: stock matmuls/convs ran
cutlass_80_tensorop_bf16andsm80_xmmaonsm_120. - Roofline floor: pricing FLOPs at datasheet peaks (1 / 2 / 4 PFLOPS dense-or-sparse for bf16 / FP8 / FP4, 1,597 GB/s) gives a bf16 floor of 876 ms (FP8 linears 618 ms, FP4 linears 489 ms) at 1024²; DiT small kernels drop from 423 ms → 139 ms when fused to single read/write passes.
Kernel time per span of a 1024² edit: measured port, at fastest card rates (422 TFLOP/s, 1,462 GB/s), and at datasheet rates.
3. Quality gating
Early runs gated on worst-of-40 per-step relative L2 error and PSNR.
Both proved noisy: a rounding-only fusion (qk_prep) moved
PSNR by −3.5 dB despite 99.9997 % bit-identical intermediates, while
diffusers itself only scores 41.4 dB between cache-on and
cache-off. Worse, worst-of-N grows with N (5 images showed 1.83 % worst
step error while 16 exposed 6.45 %).
The shipped gate evaluates 16 frozen edits (ink/pencil drawings + item 15, a letterpress poster whose edit recolours it and replaces two lines of text) at both 1024² and 2048² against the unoptimised bf16 port:
- Pass rule: every item has DINOv2 CLS cosine ≥ 0.98 and DreamSim distance ≤ 0.05 (provisional).
- Text check: item 15's six poster lines are transcribed back by Qwen3-VL and checked by eye.
- Triage: flagged items are inspected by eye and shown to Qwen3-VL in both A/B orders (since the VLM judge exhibits strong first-image position bias, its verdicts only confirm "no visible defect", never proof).
- Text-to-image: stays in the gate as a regression
check, judged on quality rather than layout match because NVFP4 MLP
weights shift composition (
DINOv2~0.90) without hurting fidelity.
4. The optimisation ladder
Measured sequentially on the RTX PRO 6000 (DiT + VAE, Turbo, 1024²
edit) against each preceding build, followed by later refinements
(1,013 → 995.6 ms measured as one span) and opt-in
modes:
Latency ladder on an RTX PRO 6000 (1024² edit, DiT + VAE). Four
core groups take the eager port from 2,484 ms to 1,013 ms; later
fusion/resize refinements reach 995.6 ms; fast reaches
812.9 ms.
| # | change | 1024² DiT+VAE | Δ (1024²) | 2048² / memory / other effect |
|---|---|---|---|---|
| 0 | lean PyTorch port (2D VAE, prefix cache) | 2,484 ms | — | peak 38.9→17.2 GiB (1024²), 64.6→26.8 GiB (2048²) |
| 1 | fused QK-norm + RoPE Triton kernel (qk_prep) |
2,279 ms | −205 ms | 2048²: 15,298→14,032 ms (−1,266 ms) |
| 2 | max-free Triton flash attention (29/32 blocks) | 2,245 ms | −34 ms | later replaced by SageAttention 2 |
| 3 | Triton FP8 MLP (mlp_fp8) |
2,023 ms | −222 ms | — |
| 4 | fused VAE (pixel_norm, NHWC, grow_add) +
K/V buffer + DiT glue |
1,832 ms | −191 ms | VAE decode 149→77 ms, encode 39→21 ms |
| 5 | FP8 row alignment (drop inner 128-col scaling) + FP8 K, V | 1,711 ms | −121 ms | ~62 ms align + ~58 ms K/V |
| 6 | SageAttention 2 + FP8 on all 4 attention linears (Q, K, V, Out) | 1,508 ms | −203 ms | 2048² stages 11.60→9.11 s |
| 7 | CUTLASS sm_120 block-scaled MXFP8 matmuls
(magpie_gemm) |
1,316 ms | −192 ms | matmuls 515→650–727 TFLOP/s |
| 8 | NVFP4 on MLP output matmul | 1,218 ms | −98 ms | — |
| 9 | NVFP4 on both MLP matmuls + MXFP8 ping-pong schedule | 1,048 ms | −170 ms | 2048²: 11.2→6.8 s (steps 8+9) |
| 10 | quantised KV cache kept across steps (SageKV,
tile-ordered write) |
1,013 ms | −35 ms | 2048²: −150 ms (6,689→6,538 ms) |
| 11 | native sm_120 4-bit attention
(magpie_attn4, 2048-class only) |
— | — | 2048²: 6,607→5,785 ms (−822 ms, −12 %) |
| 12 | residual-in-norm (Stream), shared KV scratch tail,
8k-row MLP, magpie_attn8, GPU resize |
995.6 ms | −17.4 ms | 5090 2048² peak 26.4→18.2 GiB (−7.75 GiB tail + −0.47 GiB MLP); resize 30→0.1 ms |
| 13 | 1448² default reference + 1024² encoder copy (2048² edits) | — | — | 5090 2048² DiT+VAE 7,247→6,106 ms; encode 471→158 ms |
| 14 | opt-in fast (skip DiT steps 3 &
6) |
812.9 ms | −183 ms | PRO 6000 2048²: −1,162 ms (5,702→4,540); 5090: 1,158→935 / 5,474→4,306 ms |
| 15 | opt-in keep (reuse reference KV across
requests) |
863 ms | −116 ms | PRO 6000 2048²: −236 ms; 5090: 1,158→1,023 / 5,474→5,247 ms (+4.6 / +11.3 GiB) |
Additional memory and serving gains (measured on RTX 5090 unless noted):
| change | effect |
|---|---|
drop bf16 originals (slim=True) |
DiT resident 13.9 → 8.4 GiB |
| calibrated AutoRound NVFP4 text encoder | encoder 15.2 → 5.9 GiB (101 → 72 ms; fits 11 / 6 refs resident on 5090 vs 4 / 2) |
| prepacked hybrid DiT checkpoint | load time 10.0 → 1.6 s, load peak 19.1 → 6.1 GiB |
single GPU executor thread (ThreadPoolExecutor(1)) |
5090 HTTP round trip 2.7 → 1.1 s (removes ~1.6 s per-thread CUDA warm-up) |
server prompt memory (MAGPIE_PROMPTS=8) |
repeated prompt+image skips text encode (142–175 ms → 0 ms) & encoder unpark |
PNG zlib level 1 |
2048² CPU encode ~430 → ~90 ms (~4.7× faster, identical pixels) |
| startup 6-edit probe + quadratic memory planner | 0 OOMs inside plan; parks encoder when needed, returns 507 otherwise |
5. Core engineering decisions
1. Native Blackwell tensor-core formats (MXFP8 & NVFP4)
Plain Triton FP8 (e4m3) kept one scale per token row and
one per weight column, applying them in software after the matmul;
across 54 tile configs it plateaued at 515–518 TFLOP/s.
Removing inner 128-column rescaling via row alignment recovered ~62 ms,
but the real jump came from switching to MXFP8 via
CUTLASS (native/gemm.cu): one power-of-two scale byte per
32 values, read and applied directly inside the sm_120
tensor-core instruction (640–727 TFLOP/s, ~30 %
faster). Triton fused norm and SwiGLU kernels emit activations straight
into CUTLASS's scale-slot layout.
NVFP4 packs two e2m1 nibbles per byte
with one FP8 scale per 16 values (~13 % per-matmul rounding error vs 3.8
% for FP8). Tolerance depends on which tensor is rounded:
| MLP matmuls in NVFP4 (PRO 6000) | 1024² DiT+VAE | DINOv2 min | verdict |
|---|---|---|---|
| none (all MXFP8) | 1,316 ms | 0.990 | pass |
| output matmul only | 1,218 ms | 0.993 | pass |
| hidden matmul only | 1,165 ms | 0.956 | fail |
| both (shipped default) | 1,054 ms | 0.977 | 16/16 pass on 5090; 2 flagged on PRO 6000 |
The output matmul reads spiky post-SwiGLU activations where large
values carry the signal; the hidden matmul reads dense normalised
tokens. Pre-scaling weights by 64 and SwiGLU outputs by
0.25 (undone via alpha) keeps scales inside
FP8 range. Attention linears stay MXFP8 (emulated NVFP4 scored
0.967).
Why we stopped tuning GEMMs
(native/peak.cu): microbenchmarking single
sm_120 instructions on the RTX 5090 showed bf16 reaches
93 % of its instruction ceiling (240 vs 258 TFLOP/s),
whereas MXFP8 (557–708 vs 1,024 TFLOP/s) and NVFP4 (1,104–1,209 vs 2,030
TFLOP/s) hit the exact same ceiling as a hand-written streaming matmul
(590–624 and 1,108–1,243 TFLOP/s) once
operands stream from global memory rather than staying in shared
memory/registers. Only CUTLASS's ping-pong FP8 schedule yielded a
further 4 % matmul gain.
Quantised GEMMs on the 5090 are bound by operand streaming from global memory, not the tensor-core instruction or shared memory.
2. Kernel fusion and memory layout
qk_prep(−205 ms): fuses RMSNorm, weight multiply, and 3-axis RoPE into one Triton pass (1 read, 1 write) using explicit bit-level bf16 rounding andfmato preserve 99.9997 % bit-identity. Because it is bandwidth-bound, tile sweeps changed runtime by < 1 %.- VAE (
−191 mscombined): keeps tensors channels-last (NHWC), fuses GroupNorm + SiLU intopixel_norm(one pass instead of seven), and fusesconv + dup_up(x)intogrow_add, halving VAE decode (149 → 77 ms). - Pre-allocated K/V buffer &
Stream: step 1 allocates full[prefix, target]K/V buffers; decode steps write target rows in place instead of concatenating.Stream(branch, skip, gate)defers each residual add into the next norm kernel (saving ~9.5 ms at 1024², 38 ms at 2048²).
3.
Attention: SageAttention 2, SageKV, and native 4-bit
sm_120
- SageAttention 2 (−203 ms): replaces PyTorch SDPA/Triton flash attention with INT8 Q×K (subtracting per-head mean K, which is softmax-invariant and shrinks dynamic range) and FP8 P×V (0.90 ms kernel + 0.29 ms quantisation vs 1.64 ms Triton).
- Quantised KV cache (
SageKV, −35 ms at 1024², −150 ms at 2048²): instead of re-quantising all prefix K and V on every step,sage_packquantises the full sequence once after step 1 (freezing key means and value scales with 2× headroom) and subsequent steps quantise only target rows. Writing values in output-tile order rather than token order eliminated 1 MB strided stores and cut the helper kernel from 1.23 ms → 0.13 ms. - Native 4-bit attention (
native/attn_sm120.cuh, −822 ms at 2048²): Blackwell addsmma.sync ... kind::mxf4nvf4 ... m16n8k64(64 elements/instruction vs 32 for 8-bit). Ablating SageAttention showed 81 % of a 2048² call sat in the two matmuls. Forking SageAttention's kernel to run both Q×K and P×V in 4-bit cut a 2048² call from 12.40 ms (712 TFLOP/s) → 8.80 ms (1,004 TFLOP/s) using three exact transforms beforecvt.rn.satfinite.e2m1x2: (1) channel-mean centering on V, (2) orthogonal Hadamard rotation on Q, K, and V (spreading outliers while leaving dot products invariant and inverting linearly on output), and (3) error-fitted per-16 scales. - Shape-gated switch: 4-bit attention broke
multi-reference 1024² edits (
DINOv20.76–0.96 at 7–10 refs) and flagged 1024² item 0 (0.969), so it activates only for 2048-class outputs (≥ 15,360target tokens and≥ 24,000keys). Everything smaller usesmagpie_attn8(unit-scale block-scaled FP8 P×V). - 64-bit row offsets: at 5+ 2048² references,
row × 24,576overflowedint32past row 87,382; switching to 64-bit offsets fixed the illegal memory access.
4. Doing less work and fitting 32 GB
fast(−183 / −1,162 ms): skips DiT steps 3 and 6 (TURBO_FAST), reusing the previous velocity. At step 3 (σ: 0.927 → 0.895),ΔσandΔvare both small; at step 6 (0.705 → 0.415), the flow-matching trajectory is nearly straight (vchanges little), and steps 4 and 7 recompute fresh velocities that correct drift. Step 7 cannot be skipped.keep(−134 / −227 ms on 5090): because images precede the prompt in causal order,keepretains reference K/V rows in bf16 across requests of the same images (skipping VAE encode and step 0 image rows). Keeping bf16 rather than dequantised rows is essential: dequantised reference reuse drifted (DINOv20.78–0.897).- Smaller inputs where full resolution isn't needed:
for 2048² outputs, conditioning the DiT on a 1448²
reference passes the gate (
−1.1 svs 2048²) while 1024² fails; giving Qwen3-VL a 1024² copy cuts text encoding from 471 → 158 ms with zero gate loss. Condition images resize on GPU via antialiased bicubic interpolation (30 ms → 0.1 ms). - Fitting 32 GB without OOMs:
- Dropping Qwen3-VL's unused LM head and KV cache trimmed it from
16.3 → 15.2 GiB. Switching to a community calibrated
AutoRound NVFP4 checkpoint
(
--encoder nvfp4, default on Blackwell) cut it to 5.9 GiB, letting a 2048² edit with up to 6 references stay resident on the 5090 (vs 2 with bf16). Trade-off: it occasionally misspells requested lettering (2 of 6 test renders where bf16 succeeded);--encoder bf16restores the original. - Sharing one bf16 KV scratch tail across all 32
layers (each layer holds only its small
heldboundary slice) plus running the MLP in 8,192-row blocks freed 7.75 GiB + 0.47 GiB at 2048² (peak 26.4 → 18.2 GiB on 5090). - Prepacked hybrid DiT checkpoint: stores the final NVFP4/MXFP8/bf16 mix directly, cutting load time from 10.0 → 1.6 s and load peak from 19.1 → 6.1 GiB.
- Startup probe & planner: runs 6 staged probe edits at startup (44.8 s), fits a quadratic memory curve per resolution, and classifies each incoming request as resident, staged (parks encoder in host RAM), or 507 refused—zero OOMs inside the plan.
- Dropping Qwen3-VL's unused LM head and KV cache trimmed it from
16.3 → 15.2 GiB. Switching to a community calibrated
AutoRound NVFP4 checkpoint
(
6. Discarded experiments
Every change had to pass the 16-image gate and pay for its code complexity:
| experiment | result | why discarded |
|---|---|---|
| FP8 attention linears before SageAttention | 1,832 → 1,723 ms | failed the old step-error gate (later passed perceptual gate) |
| SageAttention 3 (FP4) | 1.53 vs 1.20 ms/call; item 4 0.940 |
failed gate and slower at 1024² (0.64 ms quant overhead) |
| NVFP4 on MLP hidden matmul only | 1,165 ms, 2 flagged | failed gate (0.956) |
| NVFP4 on attention linears (emulated) | DINOv2 0.967 |
failed gate |
| 4-bit attention at 1024² or 7+ 1024² refs | −15 ms; item 0 0.969; 7+ refs
0.76–0.96 |
lost subjects / content |
| merging reference keys 2×2 | 17 % mean step error | broke edits (with or without RoPE correction) |
| 1024² reference for 2048² output | DINOv2 0.972–0.977 |
failed gate (1448² kept) |
keep with dequantised reference rows |
DINOv2 0.78–0.92 |
resumed edits drifted; reverted to bf16 rows |
fused 12,288-wide QKV matmul (fused-qkv branch) |
−5.5 ms (1024²), −6.8 ms (2048²) | too much code for ~0.5 % gain |
| 28 CUTLASS tile/stage configs | only 2 compiled, identical | operand-streaming bound |
VAE torch.compile |
+127 ms (1024²), +494 ms & +3.4 GiB (2048²) | Inductor typed our Triton pixel_norm Python-float
scale as fp64, running norm+SiLU at 1/64
rate (35 → 757 ms on L4) |
cudnn.benchmark |
−10 ms | card-dependent conv algorithm flips changed VAE encode conditioning
and pushed items 0 & 4 under 0.98 on 5090 |
| round-to-nearest NVFP4 / MXFP8 text encoder | DINOv2 0.956–0.971 |
superseded by calibrated NVFP4 (5.9 GiB) |
| Triton FP8 path on Blackwell / B200 / 4090 | B200 2,089 ms, 4090 7,508 ms | moved to ada branch (sm_89);
master is sm_120 native |
7. RTX 5090 and cross-GPU results
All RTX 5090 comparisons ran sequentially on one RunPod RTX 5090 (575
W, driver 580.126.09, 31.4 GiB usable, bf16
8192³ measured at 235.6 TFLOP/s) on
2026-10-15.
Next-fastest engine (sglang FP8 + SageAttention)
takes 2.0× as long as magpie fast at 1024² and 2.4× at
2048². Median of 5 warm requests after 1 warm-up. magpie
reports torch peak allocated; others report nvidia-smi
used.
| RTX 5090, edit (request → PNG) | 1024² | 2048² | 2048² peak |
|---|---|---|---|
magpie, fast |
1,263 ms | 5,000 ms | 18.4 GiB |
| magpie, 8 steps | 1,488 ms | 6,189 ms | 18.4 GiB |
| sglang 0.5.21 (FP8 + SageAttention) | 2,543 ms | 12,066 ms | 31.1 GiB |
| sglang 0.5.21 (default) | 4,487 ms | 23,585 ms | 27.8 GiB |
| LightX2V (encoder offloaded) | 4,539 ms | 20,072 ms | 30.7 GiB |
| vLLM-Omni (encoder offloaded) | out of memory | out of memory | – |
official diffusers (CPU offload, in-process) |
28,047 ms | out of memory | – |
Real-time replay of 1024² completions over 16 seconds.
fast
and quality gates on 5090 vs PRO 6000
fast cuts 19–21 % off DiT + VAE on both cards (RTX
5090: 1,158 → 935 ms at 1024², 5,474 → 4,306 ms at 2048²; PRO 6000: 979
→ 787 ms, 4,679 → 3,690 ms).
On RTX 5090, all 32 edits (16 × 1024², 16 × 2048²) pass the 0.98
gate in both 8-step and fast modes (lowest
0.9861).
Same kernels on RTX PRO 6000: items 0 (0.975) and 4
(0.964) drop below 0.98 at 1024² (both 8-step and
fast) because differing SM counts change CUTLASS/attention
reduction order.
Memory and multi-reference scaling
1024² memory vs latency on RTX 5090 (magpie peaks at
14.9 GiB).
2048² memory vs latency on RTX 5090 (magpie is both
fastest and lowest peak at 18.4 GiB).
Multi-reference limits on RTX 5090 (29.6 GiB probe usable). Each extra reference adds ~1.4 GiB / 0.4 s at 1024² and ~1.9 GiB / 1.2 s at 2048².
| RTX 5090 | resident | encoder parked | refused from |
|---|---|---|---|
| 1024² edit, 1024² refs | 11 (29.2 GiB) | 15 planned; 14 measured (27.7 GiB) | 16 |
| 2048² edit, 1448² refs | 6 (27.9 GiB) | 8 (25.8 GiB) | 9 |
Other cards and 40-step base
Gate DiT + VAE latency across four GPUs. PRO 6000 round-trip PNG:
1,023 / 4,757 ms (vs LightX2V 2,512 / 13,518 ms, vLLM-Omni 2,347 /
10,340 ms). B200 (sm_100) and RTX 4090 (sm_89)
run the portable Triton FP8 ada branch.
| 40-step base (DiT + VAE) | 1024² edit | 2048² edit |
|---|---|---|
| port, eager PyTorch, PRO 6000 | 10.78 s | 68.97 s |
| magpie, PRO 6000 | 4.08 s (DINOv2 ≥ 0.988) |
24.43 s (≥ 0.990) |
| magpie, RTX 5090 | 4.76 s (DINOv2 ≥ 0.987) |
27.90 s (≥ 0.988) |
8. Showcase
111 requests rendered on the RTX 5090 with default settings (8 steps, whole request including text encoder): 21 text-to-image at 1024² (median 0.93 s), 6 at 2048² (4.94 s), 58 one-reference edits at 1024²-class (1.25 s), 13 at 2048²-class (5.57 s), and 13 multi-reference edits. Tip: always use a fresh seed when editing a generated image; reusing the source seed starts from the exact same noise and yields harsh, over-sharpened output.
Several references (2–4 inputs)
| RTX 5090, whole request | 2 references | 3 references | 4 references |
|---|---|---|---|
| 1024²-class output | 1.45 s, 16.2 GiB | 1.76 s, 17.6 GiB | 2.17 s, 19.2 GiB |
| 2048²-class output | 6.54 s, 19.9 GiB | 7.77 s, 21.9 GiB | 8.89 s, 23.9 GiB |
fast vs 8 steps
Showcase sources
All from Wikimedia Commons. Public domain unless marked: Dorothea Lange, Lange-MigrantMother02; Johannes Vermeer, 1665 Girl with a Pearl Earring; Michelangelo, Sistine Chapel ceiling, Creation of Adam; Michelangelo, Tondo Doni 2021; Michelangelo, Michelangelo - Delphic Sibyl; Original: Ernst Haeckel. Scan: Ragesoss. Cleanup: Ilmari Karonen., Haeckel Discomedusae 8; Katsushika Hokusai, Tsunami by hokusai 19th century; The Strobridge Litho. Co., Cincinnati & New York., Barnum & Bailey clowns and geese2; Utagawa Hiroshige, Hiroshige, Sudden shower over Shin-Ōhashi bridge and Atake, 1857; Albrecht Dürer, The Rhinoceros (NGA 1964.8.697) enhanced; Frank R. Paul, Amazing Stories 1927 08; Livioandronico2013, Michelangelo's David 2015, CC BY-SA 4.0; Szilas, Michelangelo - Moses - San Pietro in Vincoli-3; Jebulon, Michelangelo's Pietà Saint Peter's Basilica Vatican City, CC0; NASA, Apollo 11 Lunar Module Eagle in landing configuration in lunar orbit from the Command and Service Module Columbia; NASA Hubble Space Telescope, View of Hubble after Being Released from the Shuttle Atlantis (28223588012); NASA, STS-97 International Space Station seen from Space Shuttle Endeavour; Cygni_18, Launch of the Saturn V with Apollo 11 (48300001196); NASA/JPL, Voyager spacecraft; Leonardo da Vinci, Da Vinci Vitruve Luc Viatour (cropped).
9. Using it and serving lessons
uv pip install -e .
bash tools/build_native.sh # builds CUTLASS GEMM + SageAttention + attn4/8 (~404 s on 5090)
python -m magpie.server --port 8188
POST /generate accepts prompt,
images, resolution
(1024/2048), fast,
keep, reference, and seed; server
flags include --encoder nvfp4|bf16 and
--max-memory.
Four operational surprises from deploying on Colab, RunPod pods, and RunPod serverless:
- Check the GPU before benchmarking: our first rented 5090 was power-capped at 400 W and ran bf16 at 16.5 TFLOP/s (14× slow) without raising an error.
- One GPU worker thread: Python's
ThreadingHTTPServerspawns a thread per request, paying ~1.6 s of CUDA context warm-up each time (2.7 s → 1.1 sonce pinned to a single executor thread). - Reply encoding & tunnel transport: default PNG
compression took 305 ms at 1024² and ~430 ms at 2048² (
zliblevel 1 cuts it ~4.7×); Cloudflare QUIC tunnels throttled Colab to 6–70 KB/s vs 0.8 MB/s over HTTP/2. - Cold starts: with page cache dropped, the server loads in 10.0 s (11.3 GiB) and probes for 44.8 s; in serverless, 260 s went to GPU provisioning and 230 s to network-volume reads out of 545 s total.
10. Predictions, lessons, and what is left
Every optimisation started with a written prediction; the misses taught the most:
- Price a lever by its budget or bytes moved:
qk_prepcould at most return 236 ms; fusing residual adds into norms only stopped 17 GB of reads at ~1.8 TB/s (~9 msceiling vs9.5 msmeasured—predicted 50–100 ms);fastskips a 92 ms decode step, not an average step (183 msmeasured vs220 mspredicted). - Measure before blaming:
exp2was 2.7 % of attention (guessed 25 %);pixel_normslowed 21× undertorch.compilebecause of an inferred fp64 scalar, not graph breaks. - Native instructions beat software scaling: hardware
block scaling beat our Triton row/col scaling by 30 %;
cvt.rn.satfinite.e2m1x2turned a regression into a speedup. - Lay out data-movement kernels by write address:
1.23 → 0.13 mswith identical bytes. - Gate shapes, not just total sequence length: 4-bit attention passed 2048² with 1 ref but broke 1024² with 7 refs.
Method, limits, and open work
- Software & timing: PyTorch
2.14.1+cu130,diffusers85c9fa4,transformers5.19, CUTLASSsm_120, SageAttention 2,sglang[diffusion]0.5.21, LightX2V and vLLM-Omni (main2026-10-15). Engine benchmarks report 1 discarded warm-up + median of 5;magpiereports torch peak allocated, others reportnvidia-smimemory used. Weights are under the Qwen Research License (non-commercial); the prepacked transformer (tensorkelechi/magpie-qwen-image-2.1-turbo-hybrid-nvfp4-mxfp8-bf16) carries the required licence, notice, and "Built with Qwen" attribution. - Open items: (1) cache the 44.8 s startup probe per
card/build; (2) refit
keepcaches in place (2048²keeppeaks at 29.48 of 29.6 GiB on 5090); (3) add a tolerance/multi-seed gate for card-dependent reductions (PRO 6000 items 0 & 4); (4)magpie_attn4at 1024² (worth ~15 ms if item 0 clears0.98); (5) nativesm_100kernels for B200.



