Skip to content

Commit ae8f78a

Browse files
committed
Remove implicit scorer runtime dependencies
1 parent 00d5548 commit ae8f78a

5 files changed

Lines changed: 144 additions & 43 deletions

File tree

‎src/wrbench/eval/scoring/run_local_qwen35_probe_logprob_scorer.py‎

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@
3838
question_for_probe,
3939
validate_prompt_mode,
4040
)
41-
from .runtime_common import META_KEYS
41+
from .runtime_common import (
42+
META_KEYS,
43+
explicit_cuda_placement_metadata,
44+
load_model_explicit_cuda,
45+
)
4246
except ImportError:
4347
from wrbench.eval.scoring.prompts_v2_probe import (
4448
CAMERA_MOTION_CONTEXT_KEY,
@@ -55,7 +59,11 @@
5559
question_for_probe,
5660
validate_prompt_mode,
5761
)
58-
from wrbench.eval.scoring.runtime_common import META_KEYS
62+
from wrbench.eval.scoring.runtime_common import (
63+
META_KEYS,
64+
explicit_cuda_placement_metadata,
65+
load_model_explicit_cuda,
66+
)
5967

6068

6169
SCHEMA_VERSION = "runtime_v2_probe_logprob_v2"
@@ -961,24 +969,13 @@ def fetch_videos_with_backend(video_url_or_urls, sample_indices_fn=None):
961969
video_processor.fetch_videos = fetch_videos_with_backend
962970

963971
def load_with(model_cls: Any) -> Any:
964-
try:
965-
return model_cls.from_pretrained(
966-
str(model_path),
967-
trust_remote_code=True,
968-
local_files_only=True,
969-
dtype=dtype_obj,
970-
device_map={"": local_rank},
971-
attn_implementation=attn_implementation,
972-
)
973-
except TypeError:
974-
return model_cls.from_pretrained(
975-
str(model_path),
976-
trust_remote_code=True,
977-
local_files_only=True,
978-
torch_dtype=dtype_obj,
979-
device_map={"": local_rank},
980-
attn_implementation=attn_implementation,
981-
)
972+
return load_model_explicit_cuda(
973+
model_cls,
974+
model_path=str(model_path),
975+
dtype=dtype_obj,
976+
attn_implementation=attn_implementation,
977+
local_rank=local_rank,
978+
)
982979

983980
if loader_family == "qwen25vl":
984981
from transformers import Qwen2_5_VLForConditionalGeneration # type: ignore
@@ -1257,7 +1254,7 @@ def build_run_config(
12571254
else "processor_kwargs_fps"
12581255
),
12591256
**scorer_metadata,
1260-
"device_map": {"": int(local_rank)},
1257+
**explicit_cuda_placement_metadata(local_rank),
12611258
"local_rank": int(local_rank),
12621259
"num_shards": int(args.num_shards),
12631260
"shard_id": int(args.shard_id),

‎src/wrbench/eval/scoring/run_local_qwen3vl_video_evidence.py‎

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,17 @@
1919

2020

2121
try:
22-
from .runtime_common import scoring_video_path
22+
from .runtime_common import (
23+
explicit_cuda_placement_metadata,
24+
load_model_explicit_cuda,
25+
scoring_video_path,
26+
)
2327
except ImportError:
24-
from wrbench.eval.scoring.runtime_common import scoring_video_path
28+
from wrbench.eval.scoring.runtime_common import (
29+
explicit_cuda_placement_metadata,
30+
load_model_explicit_cuda,
31+
scoring_video_path,
32+
)
2533

2634

2735
SUBJECT_SCHEMA_VERSION = "qwen3vl_subject_judgeability_v1"
@@ -3353,24 +3361,13 @@ def fetch_videos_with_backend(video_url_or_urls, sample_indices_fn=None):
33533361
video_processor.fetch_videos = fetch_videos_with_backend
33543362

33553363
def load_with(model_cls: Any) -> Any:
3356-
try:
3357-
return model_cls.from_pretrained(
3358-
str(model_path),
3359-
trust_remote_code=True,
3360-
local_files_only=True,
3361-
dtype=dtype_obj,
3362-
device_map={"": local_rank},
3363-
attn_implementation=attn_implementation,
3364-
)
3365-
except TypeError:
3366-
return model_cls.from_pretrained(
3367-
str(model_path),
3368-
trust_remote_code=True,
3369-
local_files_only=True,
3370-
torch_dtype=dtype_obj,
3371-
device_map={"": local_rank},
3372-
attn_implementation=attn_implementation,
3373-
)
3364+
return load_model_explicit_cuda(
3365+
model_cls,
3366+
model_path=str(model_path),
3367+
dtype=dtype_obj,
3368+
attn_implementation=attn_implementation,
3369+
local_rank=local_rank,
3370+
)
33743371

33753372
try:
33763373
from transformers import Qwen3VLForConditionalGeneration # type: ignore
@@ -3572,7 +3569,7 @@ def build_run_config(
35723569
if str(args.fps) == "full"
35733570
else "processor_kwargs_fps"
35743571
),
3575-
"device_map": {"": int(local_rank)},
3572+
**explicit_cuda_placement_metadata(local_rank),
35763573
"local_rank": int(local_rank),
35773574
"num_shards": int(args.num_shards),
35783575
"shard_id": int(args.shard_id),

‎src/wrbench/eval/scoring/runtime_common.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,40 @@
55
from typing import Any, Final
66

77

8+
def load_model_explicit_cuda(
9+
model_cls: Any,
10+
*,
11+
model_path: str,
12+
dtype: Any,
13+
attn_implementation: str,
14+
local_rank: int,
15+
) -> Any:
16+
"""Load on CPU, then place one complete model on the selected CUDA device.
17+
18+
Avoiding ``device_map`` keeps the single-GPU path independent of Accelerate.
19+
The dtype/torch_dtype retry preserves compatibility across Transformers
20+
model classes.
21+
"""
22+
common_kwargs = {
23+
"trust_remote_code": True,
24+
"local_files_only": True,
25+
"attn_implementation": attn_implementation,
26+
}
27+
try:
28+
model = model_cls.from_pretrained(model_path, dtype=dtype, **common_kwargs)
29+
except TypeError:
30+
model = model_cls.from_pretrained(model_path, torch_dtype=dtype, **common_kwargs)
31+
return model.to(f"cuda:{int(local_rank)}")
32+
33+
34+
def explicit_cuda_placement_metadata(local_rank: int) -> dict[str, Any]:
35+
return {
36+
"device_map": None,
37+
"device_placement": "explicit_model_to",
38+
"model_device": f"cuda:{int(local_rank)}",
39+
}
40+
41+
842
def require_section(mapping: dict[str, Any], key: str, scope: str) -> dict[str, Any]:
943
value = mapping.get(key)
1044
if not isinstance(value, dict):

‎tests/test_eval_scoring.py‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,78 @@ def write_jsonl(path: Path, rows: list[dict[str, object]]) -> None:
8585
)
8686

8787

88+
def test_single_gpu_model_loading_does_not_require_accelerate_device_map() -> None:
89+
class FakeModel:
90+
calls: list[tuple[str, dict[str, object]]] = []
91+
92+
@classmethod
93+
def from_pretrained(cls, path: str, **kwargs: object) -> "FakeModel":
94+
cls.calls.append((path, kwargs))
95+
return cls()
96+
97+
def to(self, device: str) -> "FakeModel":
98+
self.device = device
99+
return self
100+
101+
model = runtime_common.load_model_explicit_cuda(
102+
FakeModel,
103+
model_path="/models/qwen",
104+
dtype="bf16-sentinel",
105+
attn_implementation="flash_attention_2",
106+
local_rank=3,
107+
)
108+
109+
assert model.device == "cuda:3"
110+
assert FakeModel.calls == [
111+
(
112+
"/models/qwen",
113+
{
114+
"dtype": "bf16-sentinel",
115+
"trust_remote_code": True,
116+
"local_files_only": True,
117+
"attn_implementation": "flash_attention_2",
118+
},
119+
)
120+
]
121+
assert "device_map" not in FakeModel.calls[0][1]
122+
assert qwen35_mod.load_model_explicit_cuda is runtime_common.load_model_explicit_cuda
123+
assert qwen3vl_mod.load_model_explicit_cuda is runtime_common.load_model_explicit_cuda
124+
assert runtime_common.explicit_cuda_placement_metadata(3) == {
125+
"device_map": None,
126+
"device_placement": "explicit_model_to",
127+
"model_device": "cuda:3",
128+
}
129+
130+
131+
def test_single_gpu_model_loading_keeps_torch_dtype_compatibility_retry() -> None:
132+
class LegacyModel:
133+
calls: list[dict[str, object]] = []
134+
135+
@classmethod
136+
def from_pretrained(cls, _path: str, **kwargs: object) -> "LegacyModel":
137+
cls.calls.append(kwargs)
138+
if "dtype" in kwargs:
139+
raise TypeError("legacy model expects torch_dtype")
140+
return cls()
141+
142+
def to(self, device: str) -> "LegacyModel":
143+
self.device = device
144+
return self
145+
146+
model = runtime_common.load_model_explicit_cuda(
147+
LegacyModel,
148+
model_path="/models/legacy-qwen",
149+
dtype="bf16-sentinel",
150+
attn_implementation="flash_attention_2",
151+
local_rank=0,
152+
)
153+
154+
assert model.device == "cuda:0"
155+
assert "device_map" not in LegacyModel.calls[0]
156+
assert "device_map" not in LegacyModel.calls[1]
157+
assert LegacyModel.calls[1]["torch_dtype"] == "bf16-sentinel"
158+
159+
88160
def test_scoring_video_path_prefers_eval_video_path_then_path_then_video_path() -> None:
89161
assert (
90162
runtime_common.scoring_video_path(
@@ -1104,6 +1176,7 @@ def test_runtime_example_matches_wrbench_default_d3d6_profile() -> None:
11041176

11051177
assert env["PROMPT_MODE"] == p25.P25_PROMPT_MODE
11061178
assert env["TASK_CONTEXT_MODE"] == "none"
1179+
assert int(env["QWEN3VL_MAX_NEW_TOKENS"]) == qwen3vl_mod.DEFAULT_MAX_NEW_TOKENS
11071180

11081181

11091182
def test_current_qwen35_profile_is_declared_without_overlay_install() -> None:

‎wrbench.runtime.example.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
"QWEN35_EVIDENCE_CONTEXT_MODE": "visibility_v1",
112112
"QWEN3VL_DTYPE": "bfloat16",
113113
"QWEN3VL_ATTN_IMPLEMENTATION": "flash_attention_2",
114-
"QWEN3VL_MAX_NEW_TOKENS": "48",
114+
"QWEN3VL_MAX_NEW_TOKENS": "768",
115115
"QWEN3VL_MAX_VIDEOS": "0",
116116
"QWEN3VL_BINARY_PROMPT_SCHEMA": "subject_judgeability_v1",
117117
"QWEN3VL_RESCUE_PROMPT_SCHEMA": "guarded_teacher_gate_v3"

0 commit comments

Comments
 (0)