File size: 3,766 Bytes
4c42b0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Pluggable model adapters for run_custom_model.py (VLMEvalKit-free).

An adapter is any class with:

    class MyModel:
        def __init__(self, model_name="...", **kwargs): ...
        def generate(self, text: str, images: list[PIL.Image.Image]) -> str: ...

`images` is a list of PIL images already sampled by the runner (16 video frames,
or a single image, or frames + an observation image for interleaved tasks).
Return the model's raw text answer; eval.py then grades it.

Select an adapter on the CLI:  --model_impl bear_models:CosmosReason1
"""


class EchoModel:
    """Dependency-free adapter for smoke-testing the pipeline (no real model)."""

    def __init__(self, model_name="echo", **kwargs):
        self.model_name = model_name

    def generate(self, text, images):
        return f"[echo] got {len(images)} image(s). I choose option A."


class CosmosReason1:
    """
    NVIDIA Cosmos-Reason1-7B (built on Qwen2.5-VL-7B), via Hugging Face transformers.

        pip install "transformers>=4.51" accelerate torchvision

    Notes:
      - Recommend max_new_tokens=4096 so chain-of-thought isn't truncated.
      - The runner passes already-sampled video frames as a list of images.
    """

    def __init__(self, model_name="nvidia/Cosmos-Reason1-7B", max_new_tokens=4096, device_map="auto", **kwargs):
        import torch
        from transformers import AutoProcessor, AutoModelForMultimodalLM
        self.model_name = model_name
        self.max_new_tokens = max_new_tokens
        self.processor = AutoProcessor.from_pretrained(model_name)
        self.model = AutoModelForMultimodalLM.from_pretrained(
            model_name, torch_dtype=torch.bfloat16, device_map=device_map
        )

    def generate(self, text, images):
        content = [{"type": "image", "image": im} for im in images]
        content.append({"type": "text", "text": text})
        messages = [{"role": "user", "content": content}]
        inputs = self.processor.apply_chat_template(
            messages,
            add_generation_prompt=True,
            tokenize=True,
            return_dict=True,
            return_tensors="pt",
        ).to(self.model.device)
        out = self.model.generate(**inputs, max_new_tokens=self.max_new_tokens)
        gen = out[0][inputs["input_ids"].shape[-1]:]
        return self.processor.decode(gen, skip_special_tokens=True).strip()


class QwenVL:
    """
    Generic adapter for Qwen2.5-VL-style image-text-to-text models
    (e.g. Qwen/Qwen2.5-VL-7B-Instruct). Copy & tweak for your own HF model.

        pip install "transformers>=4.49" accelerate torchvision
    """

    def __init__(self, model_name="Qwen/Qwen2.5-VL-7B-Instruct", max_new_tokens=1024, device_map="auto", **kwargs):
        import torch
        from transformers import AutoProcessor, AutoModelForImageTextToText
        self.model_name = model_name
        self.max_new_tokens = max_new_tokens
        self.processor = AutoProcessor.from_pretrained(model_name)
        self.model = AutoModelForImageTextToText.from_pretrained(
            model_name, torch_dtype=torch.bfloat16, device_map=device_map
        )

    def generate(self, text, images):
        content = [{"type": "image", "image": im} for im in images]
        content.append({"type": "text", "text": text})
        messages = [{"role": "user", "content": content}]
        inputs = self.processor.apply_chat_template(
            messages, add_generation_prompt=True, tokenize=True,
            return_dict=True, return_tensors="pt",
        ).to(self.model.device)
        out = self.model.generate(**inputs, max_new_tokens=self.max_new_tokens)
        gen = out[0][inputs["input_ids"].shape[-1]:]
        return self.processor.decode(gen, skip_special_tokens=True).strip()