Skip to content

Add z-image support with cfg-parallel#666

Open
kTorp wants to merge 3 commits intoxdit-project:mainfrom
kTorp:main
Open

Add z-image support with cfg-parallel#666
kTorp wants to merge 3 commits intoxdit-project:mainfrom
kTorp:main

Conversation

@kTorp
Copy link
Contributor

@kTorp kTorp commented Mar 6, 2026

What

Enables Z-image as an xDiT runner model and modifiers the transformer to allow for cfg-parallel

Why

Having the original Z-image model makes comparisons with the distilled Z-image-turbo easier.

How

Registers Tongyi-MAI/Z-Image as a new model in z_image.py.

In the original diffusers pipeline, cfg is active when guidance_scale > 1. It doubles the batch by stacking
conditional and unconditional inputs before passing them into the transformer.
xFuserZImageTransformer2DWrapper.forward() makes it parallelizable by splitting the work across CFG ranks.

Tests

xdit --model Tongyi-MAI/Z-Image \
    --seed 42 \
    --prompt "A cute cat" \
    --height 1088 \
    --width 1920 \
    --num_inference_steps 50 \
    --ulysses_degree 2 \
    --use_torch_compile \
    --guidance_scale 4.0 \
    --num_iterations 4 \
    --attention_backend aiter \
    --use_cfg_parallel \
    --output_directory results

Running the above command produces identical outputs when compared to running without --use_cfg_parallel and with --ulysses_degree 1 --use_cfg_parallel and improves performance over both by several seconds.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces support for the original Z-Image model, enhancing the system's capability to run and evaluate different image generation models. A significant improvement is the implementation of parallel processing for Classifier-Free Guidance within the Z-Image transformer, which optimizes performance by distributing computational load. These changes enable more efficient and flexible image generation workflows, particularly when using guidance scales greater than one.

Highlights

  • Z-Image Model Integration: The original Z-Image model (Tongyi-MAI/Z-Image) has been integrated as a new runnable model, allowing for direct comparisons with the distilled Z-Image-Turbo.
  • CFG-Parallel Support: The Z-Image transformer has been modified to support Classifier-Free Guidance (CFG) in parallel, distributing the conditional and unconditional inputs across CFG ranks for improved performance.
  • Distributed Operations Enhancement: Updates to the base model and Z-Image transformer now incorporate classifier-free guidance ranks into distributed operations, ensuring correct data handling during parallel execution.
Changelog
  • xfuser/model_executor/models/runner_models/base_model.py
    • Imported get_classifier_free_guidance_rank for distributed operations.
    • Updated the _gather_dp_outputs method to consider the classifier-free guidance rank when determining the representative process for gathering outputs.
  • xfuser/model_executor/models/runner_models/z_image.py
    • Introduced xFuserZImageModel to support the Z-Image model.
    • Registered "Tongyi-MAI/Z-Image" and "Z-Image" as runnable models.
    • Configured default input values and enabled use_cfg_parallel capability for the Z-Image model.
    • Implemented model loading and pipeline execution logic for Z-Image.
  • xfuser/model_executor/models/transformers/transformer_z_image.py
    • Imported necessary functions for classifier-free guidance distributed operations.
    • Modified the forward method to enable parallel processing of inputs (x, cap_feats, t) across CFG ranks.
    • Implemented an all_gather step in the forward method to collect and reassemble outputs from parallel CFG processes.
Activity
  • The author has provided detailed test commands demonstrating the functionality of the new Z-Image model with CFG-parallel.
  • Performance improvements were noted, with identical outputs produced compared to non-parallel runs, and several seconds of performance gain.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for the Z-Image model and enables CFG-parallel for it, which is a great enhancement for performance. The implementation of CFG-parallel by splitting the batch across ranks in the transformer and gathering the results is well done. I've found one potential issue in the prompt handling for the new model which could lead to incorrect behavior when a single string prompt is used. My specific comment provides a fix for this, suggesting a more readable if/else structure as per repository guidelines.

return pipe

def _run_pipe(self, input_args: dict) -> DiffusionOutput:
prompt = list(input_args["prompt"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation of converting prompt to a list can lead to incorrect behavior when a single prompt is provided as a string. If input_args["prompt"] is a string like "A cute cat", list(input_args["prompt"]) will produce a list of characters ['A', ' ', 'c', 'u', 't', 'e', ' ', 'c', 'a', 't'], which is not the intended behavior for the diffusion pipeline.

To handle both single string prompts and lists/tuples of prompts correctly, it's clearer to use an explicit if/else block rather than a conditional expression, as per the guideline to avoid complex one-liners for readability.

        if isinstance(input_args["prompt"], str):
            prompt = [input_args["prompt"]]
        else:
            prompt = list(input_args["prompt"])
References
  1. Avoid using a conditional expression to select and call a function in a single line if it results in a long or complex statement. A more verbose if/else block can be clearer and more readable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant