Skip to content

[MAX] Add Qwen image VAE for Qwen-Image#8

Draft
jglee-sqbits wants to merge 5 commits intomainfrom
add/qwen-image/vae
Draft

[MAX] Add Qwen image VAE for Qwen-Image#8
jglee-sqbits wants to merge 5 commits intomainfrom
add/qwen-image/vae

Conversation

@jglee-sqbits
Copy link
Collaborator

@jglee-sqbits jglee-sqbits commented Mar 10, 2026

Summary

  • add the Qwen image VAE implementation
  • add the corresponding autoencoder config wiring
  • keep the implementation on the module-v2 path

Testing

  • ./bazelw run format
  • ./bazelw run lint

Checklist

  • The PR is small and focused on one thing.
  • The code was formatted.
  • The code was tested.

@jglee-sqbits jglee-sqbits changed the title [MAX] Add Qwen image VAE [MAX] Add Qwen image VAE for Qwen-Image Mar 10, 2026
@gemini-code-assist
Copy link

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 the Qwen Image VAE, a crucial component for single-image generation within the MAX framework. It adapts the QwenImage VAE's 3D causal convolution architecture to 2D for efficient T=1 image generation, providing both encoder and decoder modules. The changes also include the necessary configuration and a robust weight transformation mechanism to correctly load and utilize pre-trained 3D weights in a 2D context.

Highlights

  • Qwen Image VAE Implementation: Added the full implementation for the Qwen Image Variational Autoencoder (VAE), including its encoder and decoder components.
  • Configuration Wiring: Integrated the necessary configuration classes to support the new Qwen Image VAE.
  • 3D to 2D Weight Transformation: Implemented logic to transform 3D causal convolution weights to 2D for single-image generation (T=1), adapting the Wan-2.1 architecture.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • max/python/max/pipelines/architectures/autoencoders/init.py
    • Imported the new AutoencoderKLQwenImageModel.
  • max/python/max/pipelines/architectures/autoencoders/autoencoder_kl_qwen_image.py
    • Implemented the AutoencoderKLQwenImage VAE, including NCHWRMSNorm, ResBlock, Attention, Interpolate2D, ZeroPadBottomRight2D, Upsampler, Downsampler, MidBlock, UpBlock, DownBlock modules, and the _transform_decoder_weights utility for adapting 3D weights to 2D.
  • max/python/max/pipelines/architectures/autoencoders/model_config.py
    • Defined AutoencoderKLQwenImageConfigBase and AutoencoderKLQwenImageConfig for configuring the QwenImage VAE.
Activity
  • The author confirmed the PR is small and focused on a single objective.
  • Code formatting and linting checks were successfully performed.
  • Testing is pending as indicated by the checklist.
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

@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 the VAE implementation for Qwen-Image. The implementation looks solid overall. I've left a few comments for minor improvements: one is a typo in a config name, and two others are suggestions to improve code clarity and reduce duplication in the weight transformation logic. Please take a look.

Comment on lines +690 to +693
def _transform_decoder_weights(
raw_weights: dict[str, Any],
target_dtype: DType,
) -> dict[str, Any]:

Choose a reason for hiding this comment

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

medium

The function _transform_decoder_weights is also used to transform encoder weights in load_model (lines 848-852). The name is a bit misleading. Consider renaming it to something more generic, like _transform_3d_vae_weights, to better reflect its usage for both encoder and decoder components. You will need to update the call sites as well.

Suggested change
def _transform_decoder_weights(
raw_weights: dict[str, Any],
target_dtype: DType,
) -> dict[str, Any]:
def _transform_3d_vae_weights(
raw_weights: dict[str, Any],
target_dtype: DType,
) -> dict[str, Any]:

Comment on lines +751 to +782
if ".to_qkv.weight" in key:
if data.ndim == 5:
data = (
data[:, :, -1, :, :]
if data.shape[2] > 1
else data[:, :, 0, :, :]
)
C = data.shape[0] // 3
prefix = key.replace(".to_qkv.weight", "")
result[f"{prefix}.to_q.weight"] = _to_weight_data(
data[:C], f"{prefix}.to_q.weight", target_dtype
)
result[f"{prefix}.to_k.weight"] = _to_weight_data(
data[C : 2 * C], f"{prefix}.to_k.weight", target_dtype
)
result[f"{prefix}.to_v.weight"] = _to_weight_data(
data[2 * C :], f"{prefix}.to_v.weight", target_dtype
)
continue
if ".to_qkv.bias" in key:
C = data.shape[0] // 3
prefix = key.replace(".to_qkv.bias", "")
result[f"{prefix}.to_q.bias"] = _to_weight_data(
data[:C], f"{prefix}.to_q.bias", target_dtype
)
result[f"{prefix}.to_k.bias"] = _to_weight_data(
data[C : 2 * C], f"{prefix}.to_k.bias", target_dtype
)
result[f"{prefix}.to_v.bias"] = _to_weight_data(
data[2 * C :], f"{prefix}.to_v.bias", target_dtype
)
continue

Choose a reason for hiding this comment

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

medium

The logic for splitting fused QKV weights and biases is very similar. You can refactor this to combine the handling for weights and biases, which would reduce code duplication and improve maintainability.

        if ".to_qkv." in key:
            is_weight = ".weight" in key
            suffix = "weight" if is_weight else "bias"

            if is_weight and data.ndim == 5:
                data = (
                    data[:, :, -1, :, :]
                    if data.shape[2] > 1
                    else data[:, :, 0, :, :]
                )

            C = data.shape[0] // 3
            prefix = key.replace(f".to_qkv.{suffix}", "")

            result[f"{prefix}.to_q.{suffix}"] = _to_weight_data(
                data[:C], f"{prefix}.to_q.{suffix}", target_dtype
            )
            result[f"{prefix}.to_k.{suffix}"] = _to_weight_data(
                data[C : 2 * C], f"{prefix}.to_k.{suffix}", target_dtype
            )
            result[f"{prefix}.to_v.{suffix}"] = _to_weight_data(
                data[2 * C :], f"{prefix}.to_v.{suffix}", target_dtype
            )
            continue

Comment on lines +75 to +77
temperal_downsample: list[bool] = Field(
default_factory=lambda: [False, True, True]
)

Choose a reason for hiding this comment

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

medium

There is a typo in the config field name temperal_downsample. It should be temporal_downsample.

Suggested change
temperal_downsample: list[bool] = Field(
default_factory=lambda: [False, True, True]
)
temporal_downsample: list[bool] = Field(
default_factory=lambda: [False, True, True]
)

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant