Sequence-wise generators (MiniMax-H3 Ref2VA, MiniMax-H3-Image, Qwen-Image 2.1, FLUX 3 Image Klein) append every reference to the sequence, where computation cost grows dramatically. We propose CodecOPSD to prune the redundant condition tokens with a video codec, and we train a small LoRA on the kept tokens via on-policy self-distillation.
On average, CodecOPSD drops consumer GPUs (12 to 32 GB): a 5 s 480p video in Consumer GPUs).
On MiniMax-H3-Image, it drops project page.
π¬ Visualization | π₯ News | π§ Installation | β‘ Quickstart | ComfyUI | π Inference | π§© Codec Pruning | ποΈ Train
β If you like CodecOPSD, please help ββstarββ this repo. Thanks! π€
Teacher = the released model, every condition token.
+ CodecOPSD (student) = the same model with codec pruning and the CodecOPSD LoRA.
Everything else is the same; only the number of condition tokens differs. Times are denoising times.
Silver wolf in a dusk desert: 2 pictures + 3 videos β 768p, 243 frames, Pika H3 Flex 6 steps, B200.
<sub>Extreme setting: 90% of the three videos' tokens pruned, in the DiT and in the VLM.</sub>
| Teacher (all tokens) <sub>1013 s</sub> | + CodecOPSD :8.4x faster<sub>121 s Β· tokens 232k β 24k (β90%) Β· VLM 47.7k β 11.0k (β77%)</sub> |
|---|
Anime duel in two shots: 3 pictures + 3 videos β 768p, 124 frames, Pika H3 Flex 6 steps, H200.
<sub>Extreme setting: 90% of the two effect videos' tokens pruned, in the DiT and in the VLM.</sub>
| Teacher (all tokens) <sub>731 s</sub> | + CodecOPSD :6.8x faster<sub>108 s Β· tokens 133k β 22k (β83%)</sub> |
|---|
H3 Ref2VA Gallery (click to open it)
6 references -> 1 desk scene: 6 pictures -> image (distilled 6 steps), 1664 x 2496, 1x H200. Put the "laptop", "lamp", "plant", "mug", and "headphones" onto the "desk" in the evening, in the same camera view, with a "CodecOPSD" logo printed on the mug.
| Teacher (all tokens) <sub>29.7 s</sub> | + CodecOPSD :3.1x faster<sub>9.5 s Β· tokens 17.3k β 4.0k (β77%)</sub> |
|---|
16 references -> 1 group photo: 16 pictures -> image (distilled 6 steps), 2048 x 3072, 1x H200
| Teacher (all tokens) <sub>302.9 s</sub> | + CodecOPSD :7.6x faster<sub>39.6 s Β· tokens 98.6k β 17.1k (β83%)</sub> |
|---|
H3-Image Gallery (click to open it)
Codec pruning drops the redundant condition tokens of every reference before the DiT, and the student LoRA is distilled on its own rollouts from the teacher, which sees every condition token:
- 2026-11-22 : Code release: inference and training of CodecOPSD for MiniMax-H3 Ref2VA and MiniMax-H3-Image, one-line inference, and Gradio demos.
- 2026-11-23 :Project page with the Ref2VA and image galleries.
- 2026-11-24 : H3-Ref2VA on Hugging Face:model andSpace .
- 2026-11-27 : H3-Image on Hugging Face:model andSpace .
- 2026-11-30 : Fully optimized MiniMax-H3 Ref2VA for edge GPUs (12 to 32 GB): seeConsumer GPUs .
- 2026-12-02 : CodecOPSD in ComfyUI on its native MiniMax-H3 Ref2VA, with example workflows: seeComfyUI .
- ComfyUI version
- Check the training code
- Extend to Cosmos 3 Nano
- Extend to Qwen-Image 2.1
- Extend to FLUX 3 Image Klein
Linux x86_64, Python 3.12, and an NVIDIA GPU with 80 GB or more (141 GB or more keeps every model on the GPU). On a 12 to 32 GB GPU with 64 GB of CPU memory, optimized for the RTX 3090, 4090 and 5090, video runs at 480p: see Consumer GPUs.
git clone https://github.com/Kiteretsu77/CodecOPSD.git && cd CodecOPSD
conda create -n codecopsd python=3.12 -y && conda activate codecopsd
pip install -e ".[app]" # PyTorch 2.8, diffusers 0.43, transformers 5.14.1; [app] adds Gradio
transformers5.14.1 andimageio-ffmpeg0.7.0 are pinned: the LoRAs were trained on that text encoder's vision features, and the x264 build is part of codec pruning.- The models download on first use (see Quick Start );
python -m codecopsd.downloaddownloads them ahead (--dry-runlists them and their sizes).
We also built a demo optimized for Hugging Face Spaces: CodecOPSD-H3-Ref2VA generates a 5-second 480p video from multiple reference pictures and videos in about 30 seconds end to end.
Video: MiniMax-H3 Pika H3 Flex, 6 steps
python app.py ref2va
Image: MiniMax-H3
python app.py ref2image
: MiniMax-H3 Ref2VA, optimized for RTX 3090, 4090 and 5090
python app.py ref2va_extreme
Supports 12 to 32 GB GPUs: tested on the RTX 4070 (12 GB), RTX 5060 Ti (16 GB), RTX 3090 and 4090 (24 GB), and RTX 5090 (32 GB).
On an
Every demo prints a public link when it starts: open it and click an example. The first launch downloads the models.
What the extreme app has (docs/consumer_gpus.md):
- INT8 MiniMax-H3 Ref2VA (Comfy-Org ) with the 4-steppi-H3 adapter
- TAEH3 encoder and Sol-H3-VAE decoder
- Automatic GPU memory plans with per-request block streaming
- Page-locked CPU offload
- SageAttention2 (when installed), fused Triton kernels, INT8 and FP8 kernels per RTX generation
- Optional audio
Model weights and storage, demo options #
| Demo | GPU | CPU memory | Disk |
|---|---|---|---|
| Video | 80 GB or more | 128 GB | 115 GB |
| Image | 80 GB or more | 128 GB | 104 GB |
| Consumer GPUs | 12 to 32 GB | 64 GB | 71 GB |
- Weights go to
models/(a symlink to a larger disk works): the released models at pinned revisions, our LoRAs and examples fromCodecOPSD-H3-Ref2VA andCodecOPSD-H3-Image . All three demos share the FP8 VLM and take 212 GB together;python -m codecopsd.download --dry-runlists every file. --no-share: a local page only;--no-download: no downloads.- "Where the time went" under each result lists the stages; outputs/app/stats.jsonl keeps one line per run.
CodecOPSD also runs in ComfyUI on its native MiniMax-H3 Ref2VA, as sample code for Linux. It changes only
$\color{#1a7f37}{\boldsymbol{\mathsf{MiniMax\ H3\ Reference\ to\ Video\ with\ CodecOPSD}}}$ : our reference encode, with codec pruning on the references. 2. $\color{#1a7f37}{\boldsymbol{\mathsf{CodecOPSD\ LoRA}}}$ : our LoRA on the reference rows, and the drop of the pruned tokens before the DiT.
- Works on: RTX 3080 Ti, RTX 4070 / 4070 SUPER / 4070 Ti / 4070 Ti SUPER, RTX 4080 / 4080 SUPER, RTX 4090, RTX 5060 Ti 16 GB, RTX 5070 / 5070 Ti, RTX 5080, RTX 5090 (12-32 GB GPU memory)
- Min CPU memory requirement: 32 GB
Instructions are in comfyui/README.md.
Video: MiniMax-H3
python inference/generate.py minimax_h3_ref2va --example dog_frisbee
python inference/generate.py minimax_h3_ref2va --prompt-file prompt.txt \
--reference man.jpg --reference street.jpg --reference camera.mp4 --out out.mp4
On a 12 to 32 GB GPU: python inference/generate.py minimax_h3_ref2va_extreme (Consumer GPUs).
Python, settings, bundled examples #
import torch
from diffusers.utils import export_to_video
from codecopsd import MiniMaxH3CodecOPSDPipeline
from codecopsd.pipelines.minimax_h3_ref2va import load_reference
pipe = MiniMaxH3CodecOPSDPipeline.from_minimax_h3(
"models/MiniMax-H3-Ref2VA", text_encoder_fp8="fp8",
text_encoder_path="models/MiniMax-H3-ModelOpt-Mixed9-Dynamic-FP8/Ref2VA/text_encoder")
pipe.load_condition_lora("HikariDawn/CodecOPSD-H3-Ref2VA", weight_name="codecopsd_minimax_h3_ref2va.safetensors")
pipe.enable_model_cpu_offload()
video = pipe(
prompt="The golden retriever from <Picture 1> chases and catches a flying red frisbee on the park lawn of "
"<Picture 2>, running like the dog in <Video 1>. The camera tracks the dog.",
references=[load_reference(f) for f in ["dog.jpg", "park.jpg", "dog_running.mp4"]],
generator=torch.Generator().manual_seed(42),
).frames[0]
export_to_video(video, "output.mp4", fps=24)
| Setting | Flag | Default (as the demo) |
|---|---|---|
| Output size, height x width | --height ,--width |
480 x 832 (480p); 768 x 1344 = 768p |
| Frames, 24 fps, 17n + 5 | --frames |
124 (5.2 s), up to 243 |
| Seed | --seed |
42 |
| Sampler | --sampler |
pika : Pika H3 Flex, 6 steps (base : 50 steps) |
| Video references | --video-drop |
0.7 : drop 70 % (recommended) |
| Codec thresholds, videos | --video-thresholds MOTION_PX RESIDUAL |
5.0 1.0 |
| Codec thresholds, pictures | --picture-thresholds MOTION_PX RESIDUAL |
4.0 1.5 |
| Text encoder pruning | --vlm-drop ,--vlm-scale |
pictures: the thresholds x 0.25; videos: the transformer's tokens |
| Text encoder weights | --text-encoder |
fp8 |
| Attention | --attention |
auto : cuDNN on H100 / H200 / B200, SDPA on A100 |
| GPU placement | --offload |
auto : every model on the GPU from 150 GB, else one at a time |
| GPU memory | --gpu-memory |
the GPU's own; a smaller number caps the process to it, to check that a run fits |
| Time per stage | --stats |
off: prints where the seconds went, stage by stage |
| Mode | --mode |
compress : CodecOPSD (baseline : the teacher, every token) |
Bundled video examples: every folder in assets/examples/minimax_h3_ref2va/ (dog_frisbee, dolly_zoom_desert, wolf,
candy, deep_sea, qinglong, blanket, ...).
Image: MiniMax-H3
python inference/generate.py minimax_h3_image --example desk_setup_edit
python inference/generate.py minimax_h3_image --prompt "Put the lamp <Picture 1> on the desk in <Picture 2>." \
--image lamp.jpg --image desk.jpg --out out.png
Python, settings, bundled examples #
import torch
from PIL import Image
from codecopsd import MiniMaxH3ImageCodecOPSDPipeline
pipe = MiniMaxH3ImageCodecOPSDPipeline.from_minimax_h3_image(
"models/MiniMax-H3-Image", text_encoder_fp8="fp8",
text_encoder_path="models/MiniMax-H3-ModelOpt-Mixed9-Dynamic-FP8/Ref2VA/text_encoder")
pipe.load_condition_lora("HikariDawn/CodecOPSD-H3-Image", weight_name="codecopsd_minimax_h3_image.safetensors")
pipe.enable_model_cpu_offload()
image = pipe(
prompt="Put the lamp <Picture 1> on the desk in <Picture 2>.",
images=[Image.open("lamp.jpg"), Image.open("desk.jpg")],
generator=torch.Generator().manual_seed(42),
).images[0]
image.save("output.png")
| Setting | Flag | Default (as the demo) |
|---|---|---|
| Output size, height x width | --height ,--width |
the first picture's aspect ratio on the 2048 x 2048 area |
| Steps | the released 6-step grid | |
| Seed | --seed |
42 |
| Codec thresholds, pictures | --picture-thresholds MOTION_PX RESIDUAL |
4.0 1.5 (the residual threshold falls with the short side above 1024 px) |
| Text encoder pruning | --vlm-drop ,--vlm-scale |
the picture thresholds x 0.25 |
| Text encoder weights | --text-encoder |
fp8 |
| Attention | --attention |
auto : cuDNN on H100 / H200 / B200, SDPA on A100 |
| GPU placement | --offload |
auto : every model on the GPU from 150 GB, else one at a time |
| Mode | --mode |
compress : CodecOPSD (baseline : the teacher, every token) |
Bundled image examples: every folder in assets/examples/minimax_h3_image/ (desk_setup_edit, product_poster,
farm_barn, lineart_to_photo, ...).
Every extra acceleration (on by default), few-step samplers #
On top of CodecOPSD (codec pruning and the CodecOPSD LoRA), every item below is merged into this repo and on by default:
- Text encoder (VLM) pruning: it reads the kept vision tokens
- Few-step sampler: Pika H3 Flex, 6 steps
- FP8 text encoder
- Video VAE decoder in bf16
- AdaLN as per-timestep tables: less GPU memory, same output
- Attention kernel per GPU: cuDNN on H100 / H200 / B200, SDPA on A100
- GPU placement: every model resident from 150 GB, else model offload or block streaming (
--offload) - 12 to 32 GB GPUs: the extreme path with its own transformer choices (Consumer GPUs )
--sampler |
Files (next to MiniMax-H3-Ref2VA/ ) |
Steps |
|---|---|---|
pika (default) |
Pika H3 FlexPika-H3-Flex/ref2va/adapter.safetensors |
6 |
base (baseline) |
none | 50 |
Pika H3 Flex reproduces its release bit for bit. Codec pruning runs on the CPU while the GPU encodes the same references with the VAE, so it always finishes first; latents, masks, and text embeddings are bit-identical in every GPU placement.
Codec pruning decides, before the model runs, which condition tokens to drop: it re-encodes each reference with H.264 and drops the tokens the encoder spends neither motion nor residual on. It reads pixels alone (no model, no GPU) and runs on the CPU while the VAE encodes, so it adds no time.
It is also a training-free design: on the released models without the LoRA, codec pruning already provides good results in many cases, especially on images. The CodecOPSD LoRA keeps the teacher's quality at larger drops.
The mask decides which condition tokens the model reads. Two modes: threshold keeps a token when the codec spends motion or residual on it, so the drop follows the content; priority drop drops a fixed share of a video's tokens, the least informative first. Everything below is already the default, so nothing needs tuning.
Pictures and general videos (MiniMax-H3 Ref2VA and MiniMax-H3-Image): the settings we ship and find stable.
| Condition | Mode | Setting |
|---|---|---|
| Pictures | threshold | a fixed residual threshold (default --picture-thresholds 4.0 1.5 ); the VLM uses 0.25 x the thresholds and keeps more of each picture, where we observed closer facial similarity (default--vlm-scale 0.25 ) |
| General videos (RGB footage) | priority drop | drop 70 %, the most stable (default --video-drop 0.7 ); 66 to 75 % also work, lower for strong camera motion or busy scenes; the VLM reuses the same mask (default) |
Condition videos (sketch, surface normal, depth, edge, ...): the threshold, for an almost lossless drop (the
output stays as close to the full-token one as a change of seed). Regular conditions share one setting; sparse marks
get lower thresholds so that their small moving parts stay. Pass the kind to drop_mask (all in
codec_pruning.yaml, conditions):
| Condition | kind |
Motion px / residual | Why |
|---|---|---|---|
| Regular: sketch, surface normal, depth, edge, segmentation, human pose, dense tracks, grayscale, inpainting, outpainting, given frames | "video" (default) |
5 / 1.0 | dense maps |
| Motion tracks | "motion_track" |
1 / 0.5 | small markers that move a little |
| Boxes | "bbox" |
1 / 0.1 | flat shapes, only the corners move |
| Blur, low resolution | "degradation" |
5 / 1.0, x264 preset veryfast | a finer motion search on degraded footage |
Motion px at 480 x 640, scaled with the size. Flags and Python calls: docs/mask_settings.md.
Prune the tokens of any condition in a few lines (python inference/test_drop_mask.py checks it):
import torch
from codecopsd.codec_pruning import drop_mask
video = torch.rand(124, 3, 480, 832) * 2 - 1 # a condition video [T, 3, H, W], float in [-1, 1] or uint8
picture = torch.rand(3, 1536, 1536) * 2 - 1 # a condition picture [3, H, W]
dropped = drop_mask(video, drop=0.7) # bool [37, 15, 26] = [T', H / 32, W / 32], True = drop
dropped = drop_mask(video) # no rate: the threshold decision
dropped_picture = drop_mask(picture) # bool [1, 48, 48]; pictures use the threshold decision
tokens = torch.randn(*dropped.shape, 64) # your condition tokens on the same grid [T', H / 32, W / 32, C]
kept = tokens[~dropped] # [N_kept, C]: give these to the model
positions = (~dropped).nonzero() # [N_kept, 3]: their (t, h, w) positions, for the rotary embedding
The token grid follows MiniMax-H3's VAE (32 x 32 pixels per token); for another VAE, see docs/codec_pruning.md.
π Documents: codec pruning in your own model | mask settings | training | consumer GPUs
Repo layout #
app.py the Gradio demos: python app.py ref2va | ref2va_extreme (the default) | ref2image
inference/
generate.py one-line inference of every model (bundled examples or your own inputs)
cli/ each model's command line: minimax_h3_ref2va.py, minimax_h3_ref2va_extreme.py,
minimax_h3_image.py, cosmos3.py
demo/ the Gradio demo, one module per part (demo/__init__.py lists them)
load.py the models, building a pipeline, offload (shared)
extreme/ the consumer GPU path only (app.py ref2va_extreme): transformer choices (pi-H3,
Pika H3 Flex, HyperFlow H3 Flash), memory plans, INT8 and fused kernels,
SageAttention2, TAEH3, the Sol-H3-VAE decoder
hardware.py the GPU, CPU memory and kernels of this machine (python -m inference.hardware)
examples.py, utils.py the bundled examples' flags; dropped-token counts, run summary lines
vlm_fp8.py saves the FP8 VLM once from the bf16 release (python -m inference.vlm_fp8)
test_drop_mask.py drop_mask on synthetic inputs (CPU, about 20 s)
assets/examples/ bundled examples: <model>/<name>/example.json + prompt.txt (media downloaded on first use)
docs/ codec_pruning.md, mask_settings.md, training.md, consumer_gpus.md
src/codecopsd/
codec_pruning/ codec pruning: drop_mask, submit_prune, README
configs/ every setting, commented (codec_pruning.yaml: the thresholds and the priority drop)
download.py the first-use download into models/ and assets/examples/refs/
attention.py attention kernels of the transformer
offload.py pinned model offload, layer streaming
stats.py where the seconds of a request go, stage by stage
models/ condition-row LoRA transformers, FP8 linears
pipelines/minimax_h3_ref2va/ Ref2VA (reference to video): MiniMaxH3CodecOPSDPipeline, few-step samplers, VLM drop
pipelines/minimax_h3_image/ reference to image: MiniMaxH3ImageCodecOPSDPipeline
scripts/
train_minimax_h3_ref2va.py training for MiniMax-H3 Ref2VA (encode, train)
train_minimax_h3_image.py training for MiniMax-H3-Image (encode, train)
convert_minimax_h3_lora.py training checkpoint -> LoRA (.safetensors), both models
download_examples.py the bundled examples' media into assets/examples/refs/
Warning
Need more check. The training code and its guide are still being verified.
Sample training code for 4 GPUs, for MiniMax-H3-Image and MiniMax-H3 Ref2VA: docs/training.md.
The CodecOPSD code is free for research: anyone, companies included, may use, modify and share it for research and publish papers and results built on it. Commercial use needs a license from us: please contact TODO. Full terms: LICENSE.
The models keep their own licenses: MiniMax-H3, MiniMax-H3-Image, and the weights built on them (the CodecOPSD LoRAs,
Pika H3 Flex, pi-H3, Comfy-Org's INT8 MiniMax-H3, HyperFlow H3 Flash) are under the MiniMax Community License
(MiniMax-H3,
MiniMax-H3-Image).
inference/extreme/models/hyperflow_flash/ and inference/extreme/models/int8.py are HyperFlow H3 Flash's model
code, under its own Apache 2.0 License (Video Rebirth, hyperflow-h3-flash).
Users are responsible for their use of the code, the models, and the generated content, and should follow the applicable laws and the licenses above. The authors are not responsible for any misuse.
@article{wang2026codecopsd,
title = {On-Policy Codec Distillation for Efficient All-in-One Video Generation},
author = {Wang, Boyang and Blackburn-Matzen, Kevin and Saha, Oindrila and Gadelha, Matheus and Cheng, Zezhou},
journal = {arXiv preprint arXiv:2611.31184},
year = {2026}
}
We thank MiniMax for MiniMax-H3 and MiniMax-H3-Image, Pika for Pika H3 Flex, Lakonik for pi-H3, Comfy-Org for the INT8 MiniMax-H3, and Video Rebirth for HyperFlow H3 Flash. Our code builds on diffusers, and codec pruning uses FFmpeg and x264.