Views
No views yet
transformers checkpoint with custom code (trust_remote_code=True).pip install "transformers>=5.7.0" "torch>=2.4" pillow requests decordvideo_backend="codec") that replaces
uniform frame sampling with codec-aware canvas packing driven by motion
vectors and bit-cost — typically yielding stronger long-video accuracy at the
same token budget. To enable it you need two extra pieces:1# 1. The cv-preinfer CLI (PyPI: codec-video-prep) drives canvas extraction.
2pip install codec-video-prep opencv-python
3
4# 2. A working `ffmpeg` binary must be on PATH.
5# Verify with: ffmpeg -versionflock (already present on
Linux/macOS) for the on-disk result cache, and roughly 2 GB free disk under
$ONLINE_CODEC_CACHE_DIR (defaults to $HF_HOME/online_codec) per
processed video.demo_inference.py that covers both image and video paths.1# Image (default sample image; no auth required)
2python demo_inference.py
3
4# Image, custom file + prompt
5python demo_inference.py --mode image --media /path/to/cat.jpg \
6 --prompt "What is the cat doing?"
7
8# Video (16 uniformly-sampled frames; max-pixels caps per-frame resolution for memory)
9python demo_inference.py --mode video --media /path/to/clip.mp4 \
10 --num-frames 16 --max-pixels 200704 \
11 --prompt "Describe what happens in this video."1import torch
2from transformers import AutoProcessor, AutoModelForImageTextToText
3from PIL import Image
4
5MODEL_ID = "lmms-lab-encoder/LLaVA-OneVision-2-8B-Instruct"
6
7processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
8model = AutoModelForImageTextToText.from_pretrained(
9 MODEL_ID, trust_remote_code=True, dtype=torch.bfloat16, device_map="cuda",
10).eval()
11
12# ----- Image -----
13image = Image.open("cat.jpg").convert("RGB")
14messages = [{"role": "user", "content": [
15 {"type": "image"},
16 {"type": "text", "text": "Describe this image in detail."},
17]}]
18text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
19inputs = processor(text=[text], images=[image], return_tensors="pt", padding=True)
20inputs = {k: v.to("cuda") if hasattr(v, "to") else v for k, v in inputs.items()}
21
22out = model.generate(**inputs, max_new_tokens=256, do_sample=False)
23print(processor.tokenizer.decode(out[0, inputs["input_ids"].shape[-1]:], skip_special_tokens=True))
24
25# ----- Video -----
26# Lower max_pixels if you hit OOM on long videos.
27processor.video_processor.max_pixels = 200704
28
29messages = [{"role": "user", "content": [
30 {"type": "video"},
31 {"type": "text", "text": "Describe what happens in this video."},
32]}]
33text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
34inputs = processor(
35 text=[text], videos=["clip.mp4"], return_tensors="pt", padding=True,
36 num_frames=16, # exact frame count; or use target_fps / max_frames
37)
38inputs = {k: v.to("cuda") if hasattr(v, "to") else v for k, v in inputs.items()}
39out = model.generate(**inputs, max_new_tokens=256, do_sample=False)
40print(processor.tokenizer.decode(out[0, inputs["input_ids"].shape[-1]:], skip_special_tokens=True))video_backend="codec"). Everything else — canvas extraction via
cv-preinfer, on-disk caching, patch-position bookkeeping, chat-template
rewriting — happens inside processor(...):1# Make sure: `pip install codec-video-prep opencv-python` and ffmpeg on PATH.
2messages = [{"role": "user", "content": [
3 {"type": "video"},
4 {"type": "text", "text": "Describe what happens in this long video."},
5]}]
6text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
7
8inputs = processor(
9 text=[text],
10 videos=["long_clip.mp4"],
11 video_backend="codec",
12 max_pixels=150000, # per-canvas pixel budget; lower if OOM
13 return_tensors="pt",
14 padding=True,
15 # Optional: override codec defaults from preprocessor_config.json
16 # codec_config={"target_canvas": 32, "group_size": 32, "images_per_group": 4},
17)
18inputs = {k: v.to("cuda") if hasattr(v, "to") else v for k, v in inputs.items()}
19
20out = model.generate(**inputs, max_new_tokens=256, do_sample=False)
21print(processor.tokenizer.decode(out[0, inputs["input_ids"].shape[-1]:], skip_special_tokens=True))preprocessor_config.json under the
"codec" key (target_canvas=32, group_size=32, images_per_group=4,
patch=14, min_group_frames=8, max_group_frames=64); they can be
overridden per call via codec_config={...}.target_canvas requires (or fewer than min_group_frames), a UserWarning
is emitted and inference proceeds with however many canvases cv-preinfer
can actually form. For very short clips, falling back to the frame-sampling
backend is usually a better choice.chat_template.jinja follows the Qwen3 chat format and emits <|vision_start|>...<|vision_end|> placeholders; the processor expands them per-frame (frames backend) or per-canvas-patch-run (codec backend).processor(..., video_backend=...): "frames" (default, uniform sampling) and "codec" (canvas packing via cv-preinfer, requires codec-video-prep + ffmpeg).1@misc{an2026llavaonevision2nextgenerationperceptualintelligence,
2 title = {LLaVA-OneVision-2: Towards Next-Generation Perceptual Intelligence},
3 author = {Xiang An and Yin Xie and Feilong Tang and Yunyao Yan and Huajie Tan and Didi Zhu and Changrui Chen and Xiuwei Zhao and Bin Qin and Kaicheng Yang and Yifei Shen and Yuanhan Zhang and Kaichen Zhang and Wenkang Zhang and Zheng Cheng and Nansen Zhang and Chunsheng Wu and Chunjiang Ge and Zimin Ran and Dehua Song and Chunyuan Li and Shikun Feng and Ming Hu and Zhangquan Chen and Junbo Niu and Bo Li and Ziyong Feng and Ziwei Liu and Zongyuan Ge and Jiankang Deng},
4 year = {2026},
5 eprint = {2605.25979},
6 archivePrefix = {arXiv},
7 primaryClass = {cs.CV},
8 url = {https://arxiv.org/abs/2605.25979}
9}1@misc{an2025llavaonevision15fullyopenframework,
2 title = {LLaVA-OneVision-1.5: Fully Open Framework for Democratized Multimodal Training},
3 author = {Xiang An and Yin Xie and Kaicheng Yang and Wenkang Zhang and Xiuwei Zhao and Zheng Cheng and Yirui Wang and Songcen Xu and Changrui Chen and Didi Zhu and Chunsheng Wu and Huajie Tan and Chunyuan Li and Jing Yang and Jie Yu and Xiyao Wang and Bin Qin and Yumeng Wang and Zizhen Yan and Ziyong Feng and Ziwei Liu and Bo Li and Jiankang Deng},
4 year = {2025},
5 eprint = {2509.23661},
6 archivePrefix = {arXiv},
7 primaryClass = {cs.CV},
8 url = {https://arxiv.org/abs/2509.23661}
9}1@misc{tang2026onevisionencodercodecalignedsparsityfoundational,
2 title = {OneVision-Encoder: Codec-Aligned Sparsity as a Foundational Principle for Multimodal Intelligence},
3 author = {Feilong Tang and Xiang An and Yunyao Yan and Yin Xie and Bin Qin and Kaicheng Yang and Yifei Shen and Yuanhan Zhang and Chunyuan Li and Shikun Feng and Changrui Chen and Huajie Tan and Ming Hu and Manyuan Zhang and Bo Li and Ziyong Feng and Ziwei Liu and Zongyuan Ge and Jiankang Deng},
4 year = {2026},
5 eprint = {2602.08683},
6 archivePrefix = {arXiv},
7 primaryClass = {cs.CV},
8 url = {https://arxiv.org/abs/2602.08683}
9}