feat(examples): Qwen-Image 20B MMDiT text-to-image on Trainium - #85
Open
ymwangg wants to merge 3 commits into
Open
feat(examples): Qwen-Image 20B MMDiT text-to-image on Trainium#85ymwangg wants to merge 3 commits into
ymwangg wants to merge 3 commits into
Conversation
Add a device-only, TP-mandatory Qwen-Image text-to-image pipeline (MMDiT denoiser + Qwen2.5 text-encoder + VAE decoder), with fused on-device CFG + FlowMatchEuler sampling and an end-to-end demo.sh. Includes the RoPE interleave-scatter perf fix (denoiser 14.1s -> 0.30s/step), 512px default, in-memory weights, and device-wrapper refactor onto a _DeviceModule base.
Cut Qwen-Image launch and per-image cost. Cold launch was dominated by host-side weight prep (~350s extract/shard), not compile or generation. - Shard cache (--weight-cache-dir, default ./weight_cache): extract+shard once, then reload per-rank safetensors shards on later launches (bf16 for denoiser/text-encoder = what uploads, fp32 for VAE). ~350s -> mmap read, peak RAM ~200GB -> ~45GB. --no-weight-cache disables. Default _select_weight_keys now sorts, so the safetensors key re-sort can't change the NEFF's HLO parameter order (avoids a spurious VAE recompile on the first cached run). - Resident mode (--prompts-file / --interactive): keep the process and all TP ranks alive, generating one image per prompt so the weight upload (~25s) + NEFF load (~17s) that no disk cache can remove are paid once. Rank 0 broadcasts (prompt, seed) so ranks run generate() in lockstep. Additional images then cost only generate() exec (~2.8s at 8 steps). - Text-length bucketing (--text-bucket, default 64): round the encoder seq and denoiser txt length up to a bucket so varying prompts reuse one compiled kernel per bucket instead of recompiling per exact length. Exact: the encoder is causal (real tokens never attend to the right-pad tail, sliced off after) and the denoiser masks the pad text tokens; only bf16 GEMM reduction order shifts with the kernel shape. --text-bucket 0 restores per-exact-length (bitwise-stable) behavior. CPU tests pass; validated end-to-end on trn2 (TP=4, 512px).
512px/TP4/50-step generation for the coffee-shop prompt, shown in the README and used as the PR's sample image.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a device-only, TP-mandatory Qwen-Image 20B MMDiT text-to-image pipeline to
examples/models/qwen_image/. The full pipeline runs on trn2 (TP=4):Produces correct 512px images. End-to-end
demo.shreuses the compile cache, runs CPU correctness tests, then generates viatorchrun.Sample output
512px, TP=4, 50 steps, guidance 4.0 — prompt: "a coffee shop entrance with a chalkboard sign".
Notable details
(2i, 2i+1)pair convention, not qwen3's(i, i+half)split.latents_stdas its reciprocal (latents / std_recip); dividing by raw std instead is off by ~std².Startup & serving perf
A cold launch was dominated by host-side weight prep (~350s extract/shard), not compile or generation. Three additions cut that:
--weight-cache-dir, default on): extract+shard once, then reload per-rank safetensors shards on later launches (bf16 = what uploads; fp32 for the VAE). ~350s → memory-mapped read; peak RAM ~200GB → ~45GB.--no-weight-cachedisables.--prompts-file/--interactive): keep the process and all TP ranks alive, generating one image per prompt so the weight upload (~25s) + NEFF load (~17s) that no disk cache can remove are paid once — additional images then cost onlygenerate()(~2.8s at 8 steps).--text-bucket, default 64): round the encoder seq and denoiser text length up to a bucket so varying-length prompts reuse one compiled kernel per bucket instead of recompiling per exact length. Exact (causal encoder + masked pad text tokens);--text-bucket 0restores per-exact-length, bitwise-stable behavior.Test plan
uv run pytest examples/models/qwen_image/tests/QWEN_IMAGE_TP_DEVICE_TEST=1 uv run pytest tests/test_tp_device.pySee
examples/models/qwen_image/README.mdfor full run instructions and the durable lessons/limits.