Skip to content

feat: Port Z-Image (z_image) text-to-image and img2img #2425

Closed

Description

@inureyes

Part of #2348

Problem / Background

Z-Image (Tongyi-MAI, Z-Image-Turbo and Z-Image base) is a 6B single-stream DiT text-to-image model with image-to-image editing. #2341 already ports its DiT blocks as shared code for LLaDA-Image and Ming-Image, but Z-Image itself has no issue and does not load: its Diffusers directory has no top-level model_type, and after #2341 the probe recognizes it as z_image with no registered family. This issue adds the family on top of the #2341 blocks.

Upstream reference

Current Behavior

Proposed Solution

New module src/imagegen/z_image/ (config.rs, transformer.rs, vae.rs, pipeline.rs, mod.rs) implementing ImageGenerationModel with supports(Generate) and supports(Edit) true, family id z_image.

  • Direct load, no conversion step. Upstream asks users to run convert.py first, but its load path already sanitizes raw Diffusers weights on the fly (weights.py#L85-L117: transformer key remap, VAE conv transpose when source layout, text encoder model. prefix strip and lm_head drop); conversion only casts and optionally quantizes. mlxcel loads the raw tree directly, per the project's direct-checkpoint rule. Also accept an mlx-vlm converted directory: when vae/model.safetensors.index.json metadata has mlx_vlm_format: "z_image", skip the VAE transpose; when weights carry .scales, load quantized through UnifiedLinear with mode, bits and group size from that metadata (affine, mxfp4 32, mxfp8 32, nvfp4 16; anything else errors, as upstream _apply_weights).
  • transformer.rs: the top-level ZImageTransformer composed from imagegen::dit_blocks (do not copy the blocks), plus sanitize_transformer_weights.
  • vae.rs: build the feat: Add image generation runtime: generate-image CLI, /v1/images routes, shared VAE and scheduler #2341 FLUX.2 encoder and decoder from vae/config.json. If feat: Add image generation runtime: generate-image CLI, /v1/images routes, shared VAE and scheduler #2341's Flux2Vae hard-codes 32 latent channels, the batch-norm stats or post_quant_conv, make those config-driven in src/imagegen/vae/flux2.rs here (latent_channels, block_out_channels, layers_per_block, use_post_quant_conv, optional bn.*) and update its // Used by: comment to add ZImage. ERNIE-Image and LLaDA-Image behavior must not change.
  • Text conditioning: the shared Qwen3 conditioner's hidden_states(...)[num_layers - 1], trimmed to the valid token count.
  • Scheduler: FlowMatchEulerDiscrete::static_shift(steps - 1, shift) from src/imagegen/scheduler.rs (contract below).
  • pipeline.rs: generate and edit as upstream, including cfg_truncation and negative_prompt (via --negative-prompt or --gen-kwargs), strength (default 0.6, edit only, exactly one reference image).
  • Register z_image in the feat: Add image generation runtime: generate-image CLI, /v1/images routes, shared VAE and scheduler #2341 FAMILIES table and the registry standalone family table.

Scope

In scope: the module above, the VAE generalization if needed, docs (docs/image-generation.md row with turbo 8 steps / guidance 0 and base 50 / 4, docs/supported-models.md).

Out of scope: a conversion or quantization command, ControlNet and LoRA checkpoints, multi-reference editing (upstream accepts exactly one image).

Implementation Notes

Acceptance Criteria

  • mlxcel generate-image -m models/Z-Image-Turbo-bf16 -p "A red panda serving tea in a moonlit bamboo forest" --size 1024x1024 --seed 42 -o out.png writes a PNG with defaults 8 steps and guidance 0, loading the raw Diffusers tree with no conversion.
  • Seed-fixed parity against mlx-vlm on mlx-community/Z-Image-Turbo-bf16 at 512x512, 8 steps, seed 42: initial noise bit-identical, PNG PSNR at least 30 dB, and final latents within the per-case bound documented in the test, which must reject a wrong port (committed fixture). It fails on current main. Revised during epic epic: image generation runtime and model families #2348: the original final-latents relative RMS below 1e-2 bound is not reachable in bf16. Upstream moves its own latents by 2.7e-2 to 1.0e-1 when a single precision setting changes, so measured rel-RMS is recorded in the PR as drift and does not gate the merge.
  • Base model: mlx-community/Z-Image-bf16 with --negative-prompt "blurry, low quality" --gen-kwargs '{"cfg_truncation":1.0}' runs 50 steps at guidance 4 and matches upstream under the same thresholds at 512x512.
  • Img2img: --task edit --image fox.png --gen-kwargs '{"strength":0.6}' matches upstream; strength 0 and 1.5 are rejected.
  • Unit tests, each failing on current main: static_shift sigmas for shift 3.0 and 6.0 at 8 and 49 steps against upstream literals; _img2img_start_index table; transformer key sanitize on a synthetic raw key set; VAE sanitize is a no-op on an mlx_vlm_format: "z_image" directory.
  • If the VAE was generalized: the ERNIE-Image or LLaDA-Image VAE parity test from feat: Add image generation runtime: generate-image CLI, /v1/images routes, shared VAE and scheduler #2341 still passes.
  • mlxcel arch --json lists z_image; docs updated.

Verification

cargo build --release --features metal,accelerate
cargo test --workspace --profile test-fast --features metal,accelerate z_image
cargo test --workspace --profile test-fast --features metal,accelerate imagegen
cargo test --workspace --profile test-fast --features metal,accelerate
cargo clippy --workspace --all-targets -- -D warnings
cargo fmt --check
./target/release/mlxcel generate-image -m models/Z-Image-Turbo-bf16 -p "A red panda serving tea in a moonlit bamboo forest" --size 1024x1024 --seed 42 -o /tmp/z-image.png

Dependencies

Blocked by #2341 (runtime, DiT blocks, FLUX.2 VAE, probe, shared Qwen3 conditioner and static-shift scheduler).

Activity

  1. added
    status:blocked
    type:enhancement
    priority:low
    area:models
    modelsize:small
    modeltype:vlm
    on Oct 16, 2026
  2. added
    status:ready
    status:in-progress
    status:review
    and removed
    status:blocked
    status:ready
    status:in-progress
    on Oct 27, 2026
  3. added
    status:done
    and removed
    status:review
    on Oct 27, 2026
  4. added a commit that references this issue on Nov 3, 2026
    a29a269
  5. added this to the 0.8 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:modelsmodelsize:smallmodeltype:vlmpriority:lowstatus:donetype:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions