MagCache4HunyuanVideo

June 11, 2025 · View on GitHub

MagCache can speedup HunyuanVideo 2.8x without much visual quality degradation, in a training-free manner. The following video shows the results generated by MagCache-HunyuanVideo.


Prompt: The video shows two astronauts in bulky suits walking slowly on the moon’s surface.... The video shows two astronauts in bulky suits walking slowly on the moon’s surface, against a vast starry universe. Their steps are heavy and slow, kicking up dust in the low-gravity environment. The scene is silent, mysterious, and evokes the courage and dreams of space exploration.

📈 Inference Latency Comparisons on a Single A800 GPU

ResolutionHunyuanVideoTeaCache (0.15)MagCache (E012K04R02)MagCache (E024K06R02)
540p~20min10s~8min53s~8min50s~7min17s
720p~54min05s~23 min 49s~23min50s~19min33s

Usage

Follow HunyuanVideo to clone the repo and finish the installation, then copy 'magcache_sample_video.py' in this repo to the HunyuanVideo repo. You can modify the 'magcache_thresh', 'magcache_K', and 'retention_ratio' in lines 294-296 to obtain your desired trade-off between latency and visul quality.

For single-gpu inference, you can use the following command:

cd HunyuanVideo

# 480P T2V
python3 magcache_sample_video.py \
    --video-size 544 960 \
    --video-length 129 \
    --infer-steps 50 \
    --seed 0 \
    --prompt "A couple in formal evening attire is caught in heavy rain on their way home, holding a black umbrella. In the flat shot, the man is wearing a black suit and the woman is wearing a white long dress. They walk slowly in the rain, and the rain drips down the umbrella. The camera moves smoothly with their steps, showing their elegant posture in the rain." \
    --flow-reverse \
    --use-cpu-offload \
    --save-path ./magcache_results

# 720P T2V
python3 magcache_sample_video.py \
    --video-size 720 1280 \
    --video-length 129 \
    --infer-steps 50 \
    --seed 0 \
    --prompt "The video shows two astronauts in bulky suits walking slowly on the moon’s surface, against a vast starry universe. Their steps are heavy and slow, kicking up dust in the low-gravity environment. The scene is silent, mysterious, and evokes the courage and dreams of space exploration." \
    --flow-reverse \
    --use-cpu-offload \
    --save-path ./magcache_results

To generate a video with 8 GPUs, you can use the following command:

cd HunyuanVideo

torchrun --nproc_per_node=8 magcache_sample_video.py \
    --video-size 1280 720 \
    --video-length 129 \
    --infer-steps 50 \
    --prompt "A cat walks on the grass, realistic style." \
    --flow-reverse \
    --seed 42 \
    --ulysses-degree 8 \
    --ring-degree 1 \
    --save-path ./teacache_results

For FP8 inference, you must explicitly specify the FP8 weight path. For example, to generate a video with fp8 weights, you can use the following command:

cd HunyuanVideo

DIT_CKPT_PATH={PATH_TO_FP8_WEIGHTS}/{WEIGHT_NAME}_fp8.pt

python3 magcache_sample_video.py \
    --dit-weight ${DIT_CKPT_PATH} \
    --video-size 1280 720 \
    --video-length 129 \
    --infer-steps 50 \
    --prompt "A cat walks on the grass, realistic style." \
    --seed 42 \
    --embedded-cfg-scale 6.0 \
    --flow-shift 7.0 \
    --flow-reverse \
    --use-cpu-offload \
    --use-fp8 \
    --save-path ./teacache_fp8_results

Citation

If you find MagCache is useful in your research or applications, please consider giving us a star 🌟 and citing it by the following BibTeX entry.

@misc{ma2025magcachefastvideogeneration,
      title={MagCache: Fast Video Generation with Magnitude-Aware Cache}, 
      author={Zehong Ma and Longhui Wei and Feng Wang and Shiliang Zhang and Qi Tian},
      year={2025},
      eprint={2506.09045},
      archivePrefix={arXiv},
      primaryClass={cs.CV},
      url={https://arxiv.org/abs/2506.09045}, 
}

Acknowledgements

We would like to thank the contributors to the HunyuanVideo and TeaCache.