File size: 9,785 Bytes
765ab6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"""Original VideoLLaMA3 baseline with the official evaluation protocol
(VideoLLaMA3/evaluation): load_video(fps=1, max_frames=180), the image processor's
total-token cap = --budget (official --max_visual_tokens), no compression.

    python -m vbe.baseline_vl3 --model_path <VideoLLaMA3-7B> --benchmark longvideobench \
        --budget 8192 [--subtitles] --output results/<run>/longvideobench/shard0of1.jsonl

--subtitles (LongVideoBench only) interleaves the subtitles between the frames exactly
as the official longvideobench.py does; without it the prompt is the no-subtitle one
used by vbe.infer. Same answer parsing (vbe.benchmarks.extract_choice) and output
format as vbe.infer, so vbe.score reads it unchanged.
"""
import argparse
import json
import os
import time
from collections import OrderedDict

os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")

import numpy as np
import torch
from torch.utils.data import DataLoader, Dataset

from videollama3.mm_utils import load_video
from videollama3.model import Videollama3Qwen2ForCausalLM
from videollama3.model.processor import Videollama3Processor

from vbe.benchmarks import DEFAULT_ROOT, build_prompt, extract_choice, load_benchmark

MAX_FRAMES = 180


def parse_args():
    p = argparse.ArgumentParser()
    p.add_argument("--model_path", required=True)
    p.add_argument("--benchmark", required=True, choices=["videomme", "mlvu", "longvideobench"])
    p.add_argument("--data_root", default=DEFAULT_ROOT)
    p.add_argument("--output", required=True)
    p.add_argument("--budget", type=int, default=16384, help="official --max_visual_tokens")
    p.add_argument("--subtitles", action="store_true", help="LongVideoBench: official interleaved subtitles")
    p.add_argument("--shard_id", type=int, default=0)
    p.add_argument("--num_shards", type=int, default=1)
    p.add_argument("--max_new_tokens", type=int, default=16)
    p.add_argument("--num_workers", type=int, default=6)
    p.add_argument("--limit", type=int, default=0)
    return p.parse_args()


# --- copied from VideoLLaMA3/evaluation/benchmarks/longvideobench.py -------------------

def timestamp_to_seconds(timestamp):
    h, m, s = timestamp.split(':')
    return int(h) * 3600 + int(m) * 60 + float(s)


def insert_subtitles_into_frames(frames, frame_timestamps, subtitles, starting_timestamp_for_subtitles, duration):
    interleaved_list = []
    cur_i = 0
    for subtitle in subtitles:
        if "timestamp" in subtitle:
            start, end = subtitle["timestamp"]
            if not isinstance(end, float):
                end = duration
            start -= starting_timestamp_for_subtitles
            end -= starting_timestamp_for_subtitles
            subtitle_timestamp = (start + end) / 2
            subtitle_text = subtitle["text"]
        else:
            start, end = subtitle["start"], subtitle["end"]
            start = timestamp_to_seconds(start)
            end = timestamp_to_seconds(end)
            start -= starting_timestamp_for_subtitles
            end -= starting_timestamp_for_subtitles
            subtitle_timestamp = (start + end) / 2
            subtitle_text = subtitle["line"]
        for i, (frame, frame_timestamp) in enumerate(zip(frames[cur_i:], frame_timestamps[cur_i:])):
            if frame_timestamp <= subtitle_timestamp:
                interleaved_list.append(frame)
                cur_i += 1
            else:
                break
        if end - start < 1:
            end = subtitle_timestamp + 0.5
            start = subtitle_timestamp - 0.5
        covering_frames = False
        for frame, frame_timestamp in zip(frames, frame_timestamps):
            if frame_timestamp < end and frame_timestamp > start:
                covering_frames = True
                break
        if covering_frames:
            interleaved_list.append(subtitle_text)
    for i, (frame, frame_timestamp) in enumerate(zip(frames[cur_i:], frame_timestamps[cur_i:])):
        interleaved_list.append(frame)
    return interleaved_list


def interleaved_content(frames, timestamps, subtitle, start_ts, duration, instruction):
    """official generate_instruction: video chunks split by subtitle strings."""
    items = insert_subtitles_into_frames(frames, timestamps, subtitle, start_ts, duration) + [instruction]
    content, n_cur, idx = [], 0, 0
    for ele in items:
        if isinstance(ele, str):
            if n_cur:
                content.append({"type": "video", "num_frames": n_cur, "timestamps": timestamps[idx - n_cur:idx]})
                n_cur = 0
            content.append({"type": "text", "text": ele})
        else:
            n_cur += 1
            idx += 1
    return content

# ----------------------------------------------------------------------------------------


class Videos(Dataset):
    def __init__(self, groups):
        self.groups = groups

    def __len__(self):
        return len(self.groups)

    def __getitem__(self, i):
        video, samples = self.groups[i]
        try:
            frames, ts = load_video(video, fps=1, max_frames=MAX_FRAMES, precise_time=True)
            return dict(video=video, samples=samples, frames=frames, ts=[float(t) for t in ts], error=None)
        except Exception as e:  # noqa: BLE001
            return dict(video=video, samples=samples, error=repr(e))


def main():
    args = parse_args()
    samples = load_benchmark(args.benchmark, args.data_root)
    lvb_raw = {}
    if args.subtitles:
        assert args.benchmark == "longvideobench", "--subtitles is implemented for LongVideoBench only"
        d = os.path.join(args.data_root, "longvideobench")
        for r in json.load(open(os.path.join(d, "lvb_val.json"))):
            lvb_raw[r["id"]] = (json.load(open(os.path.join(d, "subtitles", r["subtitle_path"]))),
                                r["starting_timestamp_for_subtitles"], r["duration"])
    done = set()
    if os.path.exists(args.output):
        done = {json.loads(l)["qid"] for l in open(args.output) if l.strip()}
    groups = OrderedDict()
    for s in samples:
        groups.setdefault(s["video"], []).append(s)
    groups = sorted(groups.items())[args.shard_id::args.num_shards]
    if args.limit:
        groups = groups[: args.limit]
    todo = [(v, [s for s in ss if s["qid"] not in done]) for v, ss in groups]
    todo = [(v, ss) for v, ss in todo if ss]

    model = Videollama3Qwen2ForCausalLM.from_pretrained(
        args.model_path, torch_dtype=torch.bfloat16, attn_implementation="flash_attention_2").cuda().eval()
    processor = Videollama3Processor.from_pretrained(args.model_path)
    processor.image_processor.max_tokens = args.budget
    tok = processor.tokenizer

    os.makedirs(os.path.dirname(args.output), exist_ok=True)
    fout = open(args.output, "a")
    loader = DataLoader(Videos(todo), batch_size=None, num_workers=args.num_workers, collate_fn=lambda x: x)
    n_q = sum(len(ss) for _, ss in todo)
    n, t0 = 0, time.time()
    for item in loader:
        if item["error"] is None:
            image_inputs = processor.process_images([item["frames"]], merge_size=2, return_tensors="pt")
            n_vis = int(sum(g.prod() // 4 for g in image_inputs["grid_sizes"]))
        for s in item["samples"]:
            rec = dict(qid=s["qid"], video=s["video"], answer=s["answer"], meta=s["meta"], pred="", response="",
                       mode="vl3_raw", vision_tokens=0, n_frames=0)
            if item["error"] is not None:
                rec["error"] = item["error"]
            else:
                try:
                    instruction = build_prompt(args.benchmark, s)
                    if args.subtitles:
                        sub, st, dur = lvb_raw[s["qid"]]
                        content = interleaved_content(item["frames"], item["ts"], sub, st, dur, instruction)
                    else:
                        content = [{"type": "video", "num_frames": len(item["frames"]), "timestamps": item["ts"]},
                                   {"type": "text", "text": instruction}]
                    prompt = processor.apply_chat_template([{"role": "user", "content": content}],
                                                           tokenize=False, add_generation_prompt=True)
                    x = processor.process_text(prompt, image_inputs, return_tensors="pt")
                    with torch.inference_mode():
                        out = model.generate(
                            input_ids=x["input_ids"].cuda(), attention_mask=x["attention_mask"].cuda(),
                            pixel_values=image_inputs["pixel_values"].cuda().to(torch.bfloat16),
                            grid_sizes=image_inputs["grid_sizes"].cuda(), merge_sizes=image_inputs["merge_sizes"].cuda(),
                            modals=["video"], do_sample=False, max_new_tokens=args.max_new_tokens,
                            pad_token_id=tok.eos_token_id)
                    resp = tok.batch_decode(out, skip_special_tokens=True)[0].strip()
                    rec.update(response=resp, pred=extract_choice(args.benchmark, resp, s["options"]),
                               seq_len=int(x["input_ids"].shape[1]), vision_tokens=n_vis, n_frames=len(item["frames"]))
                except torch.cuda.OutOfMemoryError as e:
                    torch.cuda.empty_cache()
                    rec["error"] = repr(e)
            fout.write(json.dumps(rec, ensure_ascii=False) + "\n")
            fout.flush()
            n += 1
        if n % 20 < len(item["samples"]):
            el = time.time() - t0
            print(f"  {n}/{n_q} questions, {el / n:.2f}s/q, eta {el / n * (n_q - n) / 60:.0f} min", flush=True)
    print(f"[{args.benchmark} shard {args.shard_id}] done: {n} questions in {(time.time() - t0) / 60:.1f} min")


if __name__ == "__main__":
    main()