pytorch / pytorch/TensorRT

🐛 [Bug] WAN2.1 graph break with Dynamic shapes

Open
#3,721 2 comments 0 reactions 1 assignee View on GitHub

@apbose is already working on this.

Since Jul 25, 2025.

bug story: Dynamic Shapes & Symbolic Tracing
Dominant language
Python
Stars
3k
Forks
410
Avg merge
3d 18h
Merged PRs (30d)
78

Description

Bug Description

While compiling WAN2.1 with dynamic shapes, I found that there is graph break. Such operators are not supported:

  • torch.ops.aten.empty.memory_format + Operator Count: 2
  • torch.ops.aten.scatter.src + Operator Count: 4

You can use this script to reproduce:

pip install git+https://github.com/huggingface/diffusers.git
import os
import numpy as np
import torch
import torch_tensorrt as torch_trt
from diffusers.utils import export_to_video
from diffusers import AutoencoderKLWan, WanPipeline
from diffusers.schedulers.scheduling_unipc_multistep import UniPCMultistepScheduler
from diffusers.models.transformers.transformer_wan import WanTransformer3DModel
import argparse
import sys


def main(args):
        

    model_name = f"wan21_frames{args.num_frames}"


    dtype = torch.float16

    settings = {
        # "use_python_runtime": True,
        "enabled_precisions": {torch.float16},
        "immutable_weights": False,
        "min_block_size": 1,
        "offload_module_to_cpu": True,
        "optimization_level": 3,
    }





    # Available models: Wan-AI/Wan2.1-T2V-14B-Diffusers, Wan-AI/Wan2.1-T2V-1.3B-Diffusers
    model_id = "Wan-AI/Wan2.1-T2V-14B-Diffusers"
    vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float16)
    flow_shift = 5.0 # 5.0 for 720P, 3.0 for 480P
    scheduler = UniPCMultistepScheduler(prediction_type='flow_prediction', use_flow_sigmas=True, num_train_timesteps=1000, flow_shift=flow_shift)
    pipe = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.float16, transformer=WanTransformer3DModel(num_layers=1))
    pipe.scheduler = scheduler
    pipe.to(dtype).to("cuda")

    prompt = "A cat and a dog baking a cake together in a kitchen. The cat is carefully measuring flour, while the dog is stirring the batter with a wooden spoon. The kitchen is cozy, with sunlight streaming through the window."
    negative_prompt = "Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards"

    model = pipe.transformer

    FRAMES = torch.export.Dim("num_frames", min=1, max=11)
    dynamic_shapes = {
        "hidden_states": {2: FRAMES},
        "timestep": {},
        "encoder_hidden_states": {},
        "return_dict": None,
    }


    with torch_trt.dynamo.Debugger(log_level="debug", 
                                    profile_format="trex", 
                                    save_engine_profile=True, 
                                    # capture_fx_graph_before=["remove_detach"], 
                                    # capture_fx_graph_after=["remove_num_users_is_0_nodes"],
                                    logging_dir="/home/profile/logging/torchtrt", engine_builder_monitor=False,):
        trt_mod = torch_trt.MutableTorchTensorRTModule(model, **settings)
        trt_mod.set_expected_dynamic_shape_range((), dynamic_shapes)
        pipe.transformer = trt_mod
        output = pipe(
            prompt=prompt,
            negative_prompt=negative_prompt,
            height=720,
            width=1280,
            num_frames=args.num_frames,
            guidance_scale=5.0,
        ).frames[0]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.