Next Block Prediction: Video Generation via Semi-Autoregressive Modeling

February 12, 2025 ยท View on GitHub

arXivย  project pageย  Hugging Faceย 

Official pytorch implementation of the following paper:

Next Block Prediction: Video Generation via Semi-Autoregressive Modeling.

Shuhuai Ren1, Shuming Ma2, Xu Sun1, Furu Wei2
1Peking University
2Microsoft Research Asia

We introduce a semi-autoregressive (semi-AR) framework, called Next-Block Prediction (NBP), for video generation. This framework features the following properties:

  • ๐Ÿš€ The generation unit is shifted from individual tokens to blocks (e.g., rows or frames), where each token in the current block simultaneously predicts the corresponding token in the next block;
  • ๐Ÿ”ฅ By employing bidirectional attention within each block, enabling tokens to capture more robust spatial dependencies;
  • โšก By predicting multiple tokens in parallel, NBP models significantly reduce the number of generation steps, leading to 11x faster inference;
  • ๐Ÿฅ‡ State-of-the-art generation performance on video datasets compared to AR-based models;

Setup

Enviroment

Please setup the environment using the following commands:

conda create -n nbp python=3.9
conda activate nbp
sh setup.sh

Data & Model

Download the datasets from the official websites.

The file structure should look like:

NBP/
    data/
        |โ€“โ€“ UCF-101/
            |-- ApplyEyeMakeup/
                |-- v_ApplyEyeMakeup_g01_c01.avi
                |-- ...
            |-- ...
        |-- kinetics-dataset/
            |-- k600/
                |-- train/
                |-- val/
                |-- test/

    ckpt/
    |โ€“โ€“ NBP-ucf-base/
        |-- ucf_base_nbp16_hybrid.ckpt
    |-- NBP-k600-base/
        |-- k600_base_nbp16.ckpt
    |โ€“โ€“ NBP-tokenizer-ucf/
        |-- magvit2_ucf.pt
    |-- NBP-tokenizer-k600/
        |-- magvit2_k600.pt

Model Zoo for Video Tokenizer

We reproduce closed-source MAGVITv2 as our video tokenizer. In contrast to the official implementation, which utilizes LFQ as its quantizer, we adopt FSQ due to its simplicity and reduced number of loss functions and hyper-parameters. Following the original paper's recommendations, we set the FSQ levels to $[8, 8, 8, 5, 5, 5]$, and the size of the visual vocabulary is 64K. Moreover, we employ PatchGAN instead of StyleGAN to enhance training stability.
Training DatarFVD (128x128)ckpt
UCF-10115.50magvit2_ucf.pt
K6006.73magvit2_k600.pt

You can easily incorporate our tokenizer into your language model with:

from nbp.download import load_magvit2

# load tokenizer
tokenizer = load_magvit2('/path/to/tokenizer.pt', resolution=128, device="cuda")
tokenizer.eval()

# encode
_, tokens = tokenizer.encode(raw_video, quantize=True)
# decode
pred_video = tokenizer.decode_from_code_indices(tokens)
pred_video = torch.clamp(pred_video, 0, 1)

For the evaluation of our tokenizer, please refer to scripts/recons/eval_tokenizer.sh.

Model Zoo for Video Generator

Training DataModel Size#Token#stepgFVD (128x128)ckpt
UCF-101700M128095103.3ucf_base_nbp16_hybrid.ckpt
UCF-1013B12809555.3ucf_3b_nbp16_hybrid.ckpt
K600700M7684825.5k600_base_nbp16.ckpt
K6003B7684819.5k600_3b_nbp16.ckpt

Training

Please refer to scripts/lm_train for model training.

If you use deepspeed, after training, run

python zero_to_fp32.py /path/to/checkpoint-folder /path/to/output-file

to convert the model to fp32.

Evaluation

Please refer to scripts/lm_eval for model evaluation.

Acknowledgments

Our code is partially built upon OmniTokenizer and FSQ-pytorch.

License

This project is licensed under the MIT license, as found in the LICENSE file.