Skip to content

VideoProcessor allocates full-video CUDA buffers for NumPy and PIL outputs #15472

Backlog

Description

@gorkadabo1

Describe the bug

VideoProcessor.postprocess_video denormalizes decoded video on the accelerator before returning NumPy or PIL frames on the CPU. With the public Wan2.1 1.3B checkpoint at 832 × 480 resolution and 81 frames, the default NumPy output requires an additional 740.4 MiB of CUDA memory during postprocessing, while the decoded video tensor itself already occupies 370.2 MiB.

Moving the decoded video to CPU before denormalization avoids these additional CUDA allocations.

Source code · Wan2.1 model card

I have implemented and validated a local fix and would like to submit a PR if the maintainers agree with this approach.

Reproduction

import torch

from diffusers import AutoencoderKLWan, WanPipeline

model_id = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
revision = "0fad780a534b6463e45facd96134c9f345acfa5b"
vae = AutoencoderKLWan.from_pretrained(
    model_id, subfolder="vae", revision=revision, dtype=torch.float32
)
pipe = WanPipeline.from_pretrained(
    model_id, revision=revision, vae=vae, torch_dtype=torch.bfloat16
).to("cuda")
postprocess_video = pipe.video_processor.postprocess_video


def measure_postprocessing(video, output_type):
    torch.cuda.synchronize()
    start = torch.cuda.memory_allocated()
    torch.cuda.reset_peak_memory_stats()
    frames = postprocess_video(video, output_type=output_type)
    extra = torch.cuda.max_memory_allocated() - start
    print(f"Additional CUDA allocation in postprocessing: {extra / 2**20:.1f} MiB")
    return frames


pipe.video_processor.postprocess_video = measure_postprocessing
frames = pipe(
    prompt="A cat walking through grass",
    height=480,
    width=832,
    num_frames=81,
    num_inference_steps=20,
    guidance_scale=5.0,
    generator=torch.Generator("cpu").manual_seed(2490),
).frames
print(frames.shape, frames.dtype)

Context

A Wan2.2 5B deployment on 24 GB GPUs reported a CUDA OOM during Diffusers' denormalization step after video generation. vLLM-Omni addressed this by moving the decoded video to CPU before calling postprocessing. This report addresses the shared postprocessing path used directly by Diffusers.

Related:

Logs

Additional CUDA allocation in postprocessing on upstream main: 740.4 MiB
Additional CUDA allocation in postprocessing with the patch: 0.0 MiB
Returned frames: (1, 81, 480, 832, 3) float32

System Info

• Diffusers 0.44.0.dev0, upstream main 12d5c7a
• Python 3.11.15, PyTorch 2.7.1+cu126, Transformers 5.23.0, Accelerate 1.11.0
• Hugging Face Hub 1.33.0, Safetensors 0.8.0
• Linux x86-64, one NVIDIA A100-SXM4-80GB, NVIDIA driver 615.71.10

Who can help?

@DN6 @a-r-r-o-w

Activity

  1. added
    bug
    on Dec 3, 2026
  2. cloudgpuapp commented on Dec 3, 2026

    @cloudgpuapp

    Great catch — this is a very common OOM trigger on 24 GB GPUs right after Wan2.1 720p VAE decode, because the VAE output tensor is still resident on CUDA while denormalize() allocates another full-video intermediate buffer on the same device.

    For anyone hitting this on current diffusers before a fix lands in VideoProcessor.postprocess_video, a clean workaround is to request output_type="pt" from the pipeline and move the tensor to CPU before postprocessing:

    video_pt = pipe(..., output_type="pt").frames
    video_cpu = video_pt.cpu()
    del video_pt
    frames = pipe.video_processor.postprocess_video(video_cpu, output_type="pil")

    For the upstream fix itself, moving to CPU (or processing in small frame chunks on CPU) when output_type in ("np", "pil") and video.device.type != "cpu" avoids the extra GPU peak without changing output_type="pt" behavior.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions