| """ |
| 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() |
|
|