Repository navigation
Expand file tree
/
Copy pathvl_example.py
More file actions
139 lines (126 loc) · 3.49 KB
/
Copy pathvl_example.py
File metadata and controls
139 lines (126 loc) · 3.49 KB
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
import argparse
import os
import sys
import torch
from transformers import AutoProcessor
from moe_infinity import MoE
parser = argparse.ArgumentParser(
description="MoE-Infinity vision-language inference example"
)
parser.add_argument(
"--checkpoint",
default="Qwen/Qwen3.5-35B-A3B",
help="HuggingFace VL model checkpoint",
)
parser.add_argument(
"--image",
required=True,
help="Path to the input image",
)
parser.add_argument(
"--prompt",
default="What animal is in the picture?",
help="Question to ask about the image",
)
parser.add_argument(
"--offload_dir",
default=os.path.join(os.path.expanduser("~"), "moe-infinity-vl"),
help="Directory for offloading expert weights",
)
parser.add_argument(
"--device_memory_ratio",
type=float,
default=0.5,
help="Fraction of device memory used for expert caching",
)
parser.add_argument(
"--max_new_tokens",
type=int,
default=256,
help="Maximum tokens to generate",
)
parser.add_argument(
"--expect",
default=None,
help="Fail unless this substring appears in the answer (case-insensitive)",
)
parser.add_argument(
"--enable-minimax-m3-indexer-kernel",
dest="enable_minimax_m3_indexer_kernel",
action="store_true",
help="Enable the MiniMax-M3 Lightning Indexer block-sparse attention kernel (default off; eager is the reference)",
)
args = parser.parse_args()
processor = AutoProcessor.from_pretrained(
args.checkpoint, trust_remote_code=True
)
model = MoE(
args.checkpoint,
{
"offload_path": args.offload_dir,
"device_memory_ratio": args.device_memory_ratio,
"enable_minimax_m3_indexer_kernel": args.enable_minimax_m3_indexer_kernel,
},
)
messages = [
{
"role": "user",
"content": [
{"type": "image", "path": args.image},
{"type": "text", "text": args.prompt},
],
}
]
inputs = processor.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_dict=True,
return_tensors="pt",
enable_thinking=False,
)
input_ids = inputs["input_ids"].to("cuda:0")
generate_kwargs = {
key: (value.to("cuda:0") if isinstance(value, torch.Tensor) else value)
for key, value in inputs.items()
if key != "input_ids"
}
if "pixel_values" in generate_kwargs:
generate_kwargs["pixel_values"] = generate_kwargs["pixel_values"].to(
torch.bfloat16
)
with torch.no_grad():
output_ids = model.generate(
input_ids,
max_new_tokens=args.max_new_tokens,
do_sample=False,
pad_token_id=processor.tokenizer.eos_token_id,
**generate_kwargs,
)
answer = processor.tokenizer.decode(
output_ids[0][input_ids.shape[1] :], skip_special_tokens=True
).strip()
print(f"ANSWER: {answer}")
if not answer:
print("FAIL: empty answer", file=sys.stderr)
sys.exit(1)
routing_stats = model.engine.expert_executor.get_gpu_routing_stats()
print(f"ROUTING_STATS: {routing_stats}")
dispatch_evidence = max(
int(routing_stats.get("route_batches", 0)),
int(routing_stats.get("fallback_count", 0)),
int(routing_stats.get("completion_events_retired", 0)),
)
if dispatch_evidence <= 0:
print(
f"FAIL: no routed-expert dispatches recorded ({routing_stats})",
file=sys.stderr,
)
sys.exit(1)
if args.expect is not None and args.expect.lower() not in answer.lower():
print(
f"FAIL: expected {args.expect!r} in answer {answer!r}",
file=sys.stderr,
)
sys.exit(1)
print("PASS")