SCR-VL 中段上下文 KV 缓存复用与可编辑视频上下文

SCR-VL 把长视频表示为带时间戳的分段列表,模型先看稀疏版本并选出需要的片段,再在上下文中段重新解码这些片段并删除其余片段。该工作复用编辑点之后的 KV 缓存,避免从头重算,并在 Qwen2.5-VL-7B 上以 K=6 与完整重算对比答案一致性和预填充开销。

# SCR- VL : 可编辑带中文本的视频背景 KV- Cache 重用 [![arXiv](https://img.shields.io/badge/arXiv-2611.18718-b31b1b.svg)](https://arxiv.org/abs/2611.18718) [![许可证](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](LICENSE) 编辑长视频上下文的中间并重建其 KV 缓存 **1.7 — 1. 8x 速度更快** 超过完全重算(最多 2.3x,每个幸存部分迁移时,答案相同。 **96–97%** 关于Qwen2 5 - VL-7B的问题. 还在Qwen3-VL-8B指令和思考上进行了测试. ![SCR- VL 结果](<https://raw.githubusercontent.com/dayuwang401/SCR-VL/main/docs/fig_teaser.png>) *(a) 在Qwen2 5-VL-7B上进行中文本编辑后重建空档,完全重排对SCR-VL;"同意"是多频繁的. 两者给出相同的答案。 (b) 将更多幸存部分重新定位,在比满部分更差的情况下节省更多的计算。 重算(90 - 提问解答研究).* 守则和论文的汇总结果 [*可编辑视频上下文:中文本 KV- Cache 用于上下文管理 视频 LLMS*](https://arxiv.org/abs/2611.18718) 由王大尧主演\* (NTU,Baidu)和沙沙徐.\* (PKU), \*等号 贡献 。 从此寄存器中创建的 PDF 文件是 [`paper/main.pdf`](<https://github.com/dayuwang401/SCR-VL/blob/main/paper/main.pdf>). ## 如何运作 在上下文中,一个长长的视频作为时间戳片段的列表而保存。该模型观看一个稀疏的版本,命名为: 区段需要,上下文在中间编辑:这些区段按4x帧速率重新解码,其他 删除。在编辑后重建 KV 缓存,通常意味着在第一次修改后重新计算所有信息 符号 。 **SCR - VL (英语).** 避免使用M-RoPE的视频 LLMS : - 当某一段整体移动时,它的时空、高度和宽度位置都由相同的偏移 \\\\\所以一个 缓存密钥的旋转会移动它。 这在 0 层上是精确的( 相对错误 3e- 3 in bf16, 1 bf16 step) 并且是 。 运行时为每个段。 - 一个包含内容的缓存的视觉编码器输出可以移除每个幸存部分的ViT工作. - 它的工作是Qwen2 5 - VL-7B,Qwen3-VL-8B 教导和思考,并通过同样的正确性测试. Qwen3-VL-30B-A3B (MoE). ![概览](<https://raw.githubusercontent.com/dayuwang401/SCR-VL/main/docs/fig_overview.png>) ## 结果 Qwen2 5- VL-7B, 模型选择编辑, SCR- VL 带有 K= 6 对编辑上下文的完全重构( HuggingFace) 变压器,SDPA,一个A800-80GB: | 基准 | 问 题 | 完全答复协议 | 已保存的预补令牌 | 重建延迟 | |---|---|---|---|---| | V- NIAH( 20 – 60 分钟合成针头) | 720 | 97.4% | 30% | 7.20 s → 4.26 s (1.7x) | | V-NIAH, FlashAttention-2 | 720 | 97.4% | 30% | 3.43 s → 1.88 s (1.8x) | | 视频 -- -- MME长 | 180 | 96.1% | 32% | 6.30 s → 3.46 s (1.8x) | | LongVideoBench,视频 20分钟 | 276 | 97.1% | 32% | 6.23 s → 3.49 s (1.8x) | 将每个幸存部分重新定位,而不是最大的 6 保存了 63% 的预填符,运行速度比 1.98x 更快 full recomputing (2.29x with FlashAttention-2),与full recomputing的区别较大(probe KL 0.024 vs. 0.008),这个来源于90 - 问题V - NIAH样本上的无解带(论文,附录D). 编辑的上下文本身很有价值: 将包含针头的片段密度提高 V - NIAH 精度来自 52.2%到96.9%. 模型自己的片段选择没有达到(49.6%),并附加变形片段. 在32K预算中,没有进行编辑,而是一个强有力、廉价的基准。 所有号码都来自 [`results/summary.csv`](<https://github.com/dayuwang401/SCR-VL/blob/main/results/summary.csv>) 结束 `scripts/make_tables.py`; 无 以手键入. ## 安装 Python 3.10, PyTorch 2.7(CUDA 12.6轮), 一个80 GB GPU用于7 – 8B 模型. 需要两种环境是因为 Qwen2 5 - VL 和 Quen3 - VL 需要不同的变压器版本. ```bash git clone https://github.com/dayuwang401/SCR-VL && cd SCR-VL python3.10 -m venv .venvs/vctx && .venvs/vctx/bin/pip install -r env/requirements_vctx.txt # Qwen2.5-VL, transformers 4.51.3 python3.10 -m venv .venvs/vctx3 && .venvs/vctx3/bin/pip install -r env/requirements_vctx3.txt # Qwen3-VL, transformers 4.57.1 # optional, for the FlashAttention-2 runs: pip install flash-attn==2.8.0.post2 --no-build-isolation ``` 将权重下载到 `models/` (或点) `VCTX_MODELS` 其他地方: ```bash huggingface-cli download Qwen/Qwen2.5-VL-7B-Instruct --revision cc594898137f460bfe9f0759e9844b3ce807cfb5 --local-dir models/Qwen2.5-VL-7B-Instruct huggingface-cli download Qwen/Qwen3-VL-8B-Instruct --revision 0c351dd01ed87e9c1b53cbc748cba10e6187ff3b --local-dir models/Qwen3-VL-8B-Instruct huggingface-cli download Qwen/Qwen3-VL-8B-Thinking --revision 92f3c4b4feadd3a016ef468d103bb5f58b2a2c6b --local-dir models/Qwen3-VL-8B-Thinking ``` 总是 `source` 首选一个嵌入脚本。 `$PY`,模型和 venv 自己的 cuDNN 打开 `LD_LIBRARY_PATH`. | 脚本 | 型号 | |---|---| | `env.sh` | Qwen2 5 - VL-7B - 指导 | | `env3.sh` | Qwen3-VL-8B - 指示 | | `env3t.sh` | Qwen3-VL-8B - 思考(用空思维块回答) | | `env3moe.sh` | Qwen3-VL-30B - A3B - 思维 | ## 先检查正确性 ```bash source env.sh; CUDA_VISIBLE_DEVICES=0 $PY -m pytest -q tests # GPU tests need the model; CPU-only tests run anywhere source env3.sh; CUDA_VISIBLE_DEVICES=0 $PY -m pytest -q tests/test_correctness.py ``` `tests/test_correctness.py` 检查每个区块位置的一致性, layer-0 旋转后的密钥精确度, 无编辑等同,前缀精确,删除后一段即可使 SCR- VL 等前缀再利用比特 位数。所测量的数字被写到 `results/test_numbers*.json`. ## 数据 视频没有被重新分配。 `data/` 从原始来源, 之后 `source env.sh`. **V - NIAH 干草堆.** 三部公共领域电影 互联网档案馆的存檔,存档日期2013-03-02. (Public Domain Mark 1.0): `anna-christie-1930-greta-garbo-english`, `disraeli_1929_202506` 财务报告和财务报告 `bulldog-drummond_1929_202506`保存为 `data/haystack/{anna_christie_1930,disraeli_1929,bulldog_drummond_1929}.mp4`,然后解码 2 fps 帧库: ```bash for f in anna_christie_1930 disraeli_1929 bulldog_drummond_1929; do $PY scripts/build_bank.py data/haystack/$f.mp4 data/bank/$f.npy; done # 448x308 ``` 针头卡和问题由种子0产生 `vctx/tasks/vniah.py`. **视频 -- -- MME长** (首部60长视频,由YouTube id,HF制作). `lmms-lab/Video-MME`,学术研究用途: ```bash $PY scripts/fetch_videomme.py 60 mkdir -p data/videomme/bank; for v in data/videomme/videos/*.mp4; do $PY scripts/build_bank.py $v data/videomme/bank/$(basename $v .mp4).npy 1.0 252 448; done ``` **LongVideoBench** (HF `longvideobench/LongVideoBench`门,门,CCBY -NC - 电话SA4.0)接受条件。HuggingFace财务报告和财务报告 导出 `HF_TOKEN`。我们使用所有 276 验证问题,其视频至少长 20 分钟( 92 视频), 不 字幕 : ```bash mkdir -p data/lvb && cd data/lvb huggingface-cli download longvideobench/LongVideoBench lvb_val.json --repo-type dataset --local-dir . python -c "import json; d=[x for x in json.load(open('lvb_val.json')) if x['duration']>=1200] json.dump([x['id'] for x in d], open('selected_ids.json','w')) open('patterns.txt','w').write(''.join(f'*{v}\n' for v in sorted({x['video_path'] for x in d})))" huggingface-cli download longvideobench/LongVideoBench --repo-type dataset --include "videos.tar.part.*" --local-dir . cat videos.tar.part.* | tar -x --wildcards -T patterns.txt && cd ../.. mkdir -p data/lvb/bank; for v in data/lvb/videos/*.mp4; do $PY scripts/build_bank.py $v data/lvb/bank/$(basename $v .mp4).npy 1.0 252 448; done ``` ## 运行实验 每个脚本恢复: 已经存在的问题 `results/<tag>_shard*.jsonl` 跳过。 `--shard i --num-shards N`. ```bash source env.sh $PY scripts/run_vniah.py --shard 0 --num-shards 1 --n-per-cell 80 --tag vniah # V-NIAH, 720 questions $PY scripts/run_vniah.py --shard 0 --num-shards 1 --init-frames 16 --dense-frames 64 --ablate-every 1000000 --tag vniah64k VCTX_ATTN=flash_attention_2 $PY scripts/run_vniah.py --shard 0 --num-shards 1 --tag vniah_fa2 $PY scripts/run_append.py --shard 0 --num-shards 1 --tag append # edit-append control $PY scripts/run_longqa.py --shard 0 --num-shards 1 --tag videomme # Video-MME $PY scripts/run_longqa.py --dataset lvb --shard 0 --num-shards 1 --tag lvb # LongVideoBench source env3.sh # Qwen3-VL-8B: same scripts with tags q3vl_vniah / q3vl_videomme / q3vl_lvb source env3t.sh # Qwen3-VL-8B-Thinking: tags q3vlt_* ``` 然后再生出来 `results/summary.csv`,每个表格,每个数字和PDF: ```bash bash scripts/build_paper.sh # uses tectonic if on PATH, otherwise pdflatex + bibtex SKIP_RESULTS=1 bash scripts/build_paper.sh # compile only, from the committed paper/numbers.tex and paper/tables/ ``` ## 基准利用 `python -m vctx.cli` 运行与双轨牵引电路相同的管道,有冷冻的裂缝(`--mode sample` 或 时 间 `full`), 每问题轨迹和领导板。见 [`AGENTS.md`](<https://github.com/dayuwang401/SCR-VL/blob/main/AGENTS.md>) 为了规则。 ```bash python -m vctx.cli list # tasks, sample sizes, registered edit policies and rebuilders # edit track: better context-edit rules, rebuild frozen to the exact prefix path, accuracy only python -m vctx.cli edit --task vniah --mode sample --policy v0 --save-traj # infra track: better KV rebuilds, edit frozen, answer-free fidelity vs. full recomputation plus cost python -m vctx.cli trace --task vniah --mode sample --pattern v0 python -m vctx.cli infra-eval --task vniah --mode sample --pattern v0 --rebuilder scr:K=6,r=0 --rebuilder scr_all:r=0 --rebuilder prefix --repeats 2 python -m vctx.cli summarize ``` - **新编辑策略 :** 在下方添加模块 `vctx/edit/policies/` 与 `@register`. `example_neighbors.py` 财务报告和财务报告 `tests/edit/test_example_policy.py` 是一个完整的模板。包含的政策 : `v0` (报纸的规则), `v0_think`, `noop`, `example_neighbors`, `scan`, `coarse2fine`, `propose_verify`, `think_vote`. - **新建算法 :** 注册 `f(engine, old_state, new_blocks, **params) -> (state, logits, stats)` 输入 `vctx/infra/contrib.py` 并按顺序排列 `infra-eval` ([`docs/infra/EVAL.md`](<https://github.com/dayuwang401/SCR-VL/blob/main/docs/infra/EVAL.md>)。包括: `full`, `prefix`, `scr`, `scr_all`,以及边界修复和订购的变体(`scr_tailrepair`, `scr_visrepair`, `scr_vistail`, `scr_protect`, `scr_early`, `scr_late`, `scr_small`). ## 版式 ``` vctx/ engine.py (Qwen2.5-VL rebuild paths), engine3.py (Qwen3-VL / MoE), kvops.py (M-RoPE key shift), positions.py, segments.py, vitcache.py, policy.py, runtime.py, tasks/, bench/, edit/, infra/, splits/, cli.py tests/ correctness tests (GPU) and harness tests (CPU) scripts/ experiment entry points, summary/table/figure generators, build_paper.sh results/ summary.csv, infra-eval leaderboard, correctness-test numbers, environment record paper/ LaTeX source; every number is a macro in paper/numbers.tex generated from results/ docs/infra/ rebuild-track guide and the answer-free evaluation protocol ``` 问题记录不发布,因为它们包含Video-MME和LongVideoBench问答. ## 引用 ```bibtex @article{wang2026editable, title = {Editable Video Context: Mid-Context KV-Cache Reuse for Context-Managing Video LLMs}, author = {Wang, Dayu and Xu, Shasha}, journal = {arXiv preprint arXiv:2611.18718}, year = {2026} } ``` ## 许可证 代码 : [Apache-2.0](<https://github.com/dayuwang401/SCR-VL/blob/main/LICENSE>)数据集和模型权重保留自己的许可证(纸张,表11 )。

查看原始来源