Repository navigation
Expand file tree
/
Copy pathexp3.py
More file actions
878 lines (734 loc) · 30.5 KB
/
Copy pathexp3.py
File metadata and controls
878 lines (734 loc) · 30.5 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
#!/usr/bin/env python3
"""
exp3.py - Multi-Agent Debate Experiment (MALLM): varying (mis)informed agent composition.
This script is intentionally similar to exp2.py, but instead of sweeping misinformation
strategies across multiple datasets, it:
- runs ONLY winogrande
- runs 6 composition settings with 5 total agents:
0 misinformed, 5 informed
1 misinformed, 4 informed
2 misinformed, 3 informed
3 misinformed, 2 informed
4 misinformed, 1 informed
5 misinformed, 0 informed
Implementation detail:
MALLM's "informed" persona generator reads per-agent information from
InputExample.informations[idx]. We treat an agent as "misinformed" if its info slot
contains a misinformation string, otherwise "informed" (None).
"""
import argparse
import atexit
import glob
import hashlib
import json
import os
import re
import signal
import socket
import subprocess
import sys
import time
import uuid
from typing import List, Optional
from datetime import datetime, timezone
from mint_data import resolve_dataset_path
from model_server_config import (
apply_gpt_oss_tiktoken_env,
normalize_model_name,
vllm_extra_args_for_model,
vllm_gpu_memory_utilization_default,
vllm_max_model_len_default,
vllm_trust_remote_code_default,
)
if sys.version_info < (3, 7):
raise SystemExit(
"exp3.py requires Python >= 3.7 (MALLM depends on the stdlib 'dataclasses'). "
"Use Python >= 3.7, e.g. python3.11 exp3.py ..."
)
#
# Lightweight integration with MALLM (mirrors exp2.py)
#
def _ensure_mallm_on_path() -> None:
"""
Ensure the local MALLM repository is importable without installation.
Expected layout:
github/misinformed_agents/exp3.py
github/mallm/...
"""
this_dir = os.path.dirname(os.path.abspath(__file__))
mallm_repo_root = os.path.abspath(os.path.join(this_dir, "..", "mallm"))
if mallm_repo_root not in sys.path:
sys.path.insert(0, mallm_repo_root)
def _safe_model_name(model_name: str) -> str:
safe = (model_name or "").strip().replace("/", "_")
safe = re.sub(r"[^A-Za-z0-9._-]+", "_", safe)
return safe or "unknown_model"
def _install_runtime_stubs() -> None:
"""
Install lightweight stubs for optional heavy dependencies so that we can run
a mock discussion without installing large packages.
(Copied from exp2.py with minimal changes.)
"""
import types
import logging as _logging
# Stub: langchain_core
if "langchain_core" not in sys.modules:
langchain_core = types.ModuleType("langchain_core")
sys.modules["langchain_core"] = langchain_core
callbacks = types.ModuleType("langchain_core.callbacks")
class Callbacks: # noqa: N801
pass
class CallbackManagerForLLMRun: # noqa: N801
pass
callbacks.Callbacks = Callbacks
callbacks.CallbackManagerForLLMRun = CallbackManagerForLLMRun
sys.modules["langchain_core.callbacks"] = callbacks
callbacks_manager = types.ModuleType("langchain_core.callbacks.manager")
callbacks_manager.CallbackManagerForLLMRun = CallbackManagerForLLMRun
sys.modules["langchain_core.callbacks.manager"] = callbacks_manager
language_models = types.ModuleType("langchain_core.language_models")
class LanguageModelInput: # noqa: N801
pass
language_models.LanguageModelInput = LanguageModelInput
sys.modules["langchain_core.language_models"] = language_models
llms = types.ModuleType("langchain_core.language_models.llms")
class LLM: # noqa: N801
def __init__(self, *args, **kwargs) -> None:
for k, v in kwargs.items():
setattr(self, k, v)
def invoke(self, prompt, **kwargs): # type: ignore[override]
if hasattr(self, "_call"):
return self._call(prompt, **kwargs) # type: ignore[attr-defined]
return ""
llms.LLM = LLM
sys.modules["langchain_core.language_models.llms"] = llms
outputs = types.ModuleType("langchain_core.outputs")
class LLMResult: # noqa: N801
pass
outputs.LLMResult = LLMResult
sys.modules["langchain_core.outputs"] = outputs
prompt_values = types.ModuleType("langchain_core.prompt_values")
class PromptValue: # noqa: N801
pass
prompt_values.PromptValue = PromptValue
sys.modules["langchain_core.prompt_values"] = prompt_values
# Stub: langchain
if "langchain" not in sys.modules:
sys.modules["langchain"] = types.ModuleType("langchain")
# Stub: httpx minimal client (only if package is unavailable)
if "httpx" not in sys.modules:
try:
import httpx # noqa: F401
except Exception:
httpx = types.ModuleType("httpx")
class HTTPError(Exception): # noqa: N801
pass
class Client:
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
return False
httpx.HTTPError = HTTPError
httpx.Client = Client
sys.modules["httpx"] = httpx
# Stub: openai.OpenAI (imported by mallm even for mock://)
if "openai" not in sys.modules:
openai = types.ModuleType("openai")
class APIError(Exception): # noqa: N801
pass
class APIConnectionError(Exception): # noqa: N801
pass
class RateLimitError(Exception): # noqa: N801
pass
class OpenAI: # noqa: N801
def __init__(self, *args, **kwargs):
pass
openai.APIError = APIError
openai.APIConnectionError = APIConnectionError
openai.RateLimitError = RateLimitError
openai.OpenAI = OpenAI
sys.modules["openai"] = openai
# Stub: rich
if "rich" not in sys.modules:
rich = types.ModuleType("rich")
def _rprint(*args, **kwargs):
print(*args, **kwargs)
rich.print = _rprint # type: ignore[attr-defined]
sys.modules["rich"] = rich
rich_logging = types.ModuleType("rich.logging")
class RichHandler(_logging.Handler):
def __init__(self, *args, **kwargs):
super().__init__()
def emit(self, record): # type: ignore[override]
pass
rich_logging.RichHandler = RichHandler
sys.modules["rich.logging"] = rich_logging
rich_progress = types.ModuleType("rich.progress")
class Console:
def __init__(self, record: bool = False):
self.width = 100
def save_html(self, path: str, clear: bool = False):
pass
def print(self, *args, **kwargs):
pass
class Progress:
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
return False
def add_task(self, *args, **kwargs):
return 0
def update(self, *args, **kwargs):
pass
TaskID = int
rich_progress.Console = Console
rich_progress.Progress = Progress
rich_progress.TaskID = TaskID
sys.modules["rich.progress"] = rich_progress
rich_panel = types.ModuleType("rich.panel")
class Panel:
def __init__(self, *args, **kwargs):
pass
rich_panel.Panel = Panel
sys.modules["rich.panel"] = rich_panel
rich_text = types.ModuleType("rich.text")
class Text:
def __init__(self, *args, **kwargs):
pass
def highlight_regex(self, *args, **kwargs):
pass
rich_text.Text = Text
sys.modules["rich.text"] = rich_text
# Stub: contextplus.context
if "contextplus" not in sys.modules:
contextplus = types.ModuleType("contextplus")
def context(input_data: str) -> str:
return ""
contextplus.context = context # type: ignore[attr-defined]
sys.modules["contextplus"] = contextplus
# Stub: torch (MALLM type imports reference torch.Tensor)
if "torch" not in sys.modules:
torch = types.ModuleType("torch")
class Tensor: # noqa: N801
pass
class _Cuda:
@staticmethod
def device_count() -> int:
return 0
torch.Tensor = Tensor # type: ignore[attr-defined]
torch.cuda = _Cuda() # type: ignore[attr-defined]
sys.modules["torch"] = torch
def _read_json(path: str) -> list:
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
def _choose_misinformation(item: dict) -> str:
"""
Prefer 'false_fact'. If unavailable, choose the first available strategy text.
"""
if isinstance(item.get("false_fact"), str) and item["false_fact"].strip():
return item["false_fact"]
strat_map = item.get("misinformation_by_strategy") or {}
if isinstance(strat_map, dict) and strat_map:
for key in sorted(k for k in strat_map.keys() if isinstance(k, str)):
val = strat_map.get(key)
if isinstance(val, str) and val.strip():
return val
return (
"You are adversarial. Argue confidently for an incorrect answer using plausible-sounding but false claims. "
"Avoid agreeing unless the group adopts your incorrect answer."
)
def _stable_shuffle_informations(
informations: List[Optional[str]],
example_id: str,
misinformed_count: int,
global_seed: int,
) -> List[Optional[str]]:
"""
Deterministically shuffle per-example to avoid positional bias while keeping runs reproducible.
"""
seed_material = f"{example_id}::{misinformed_count}::{global_seed}"
seed_int = int(hashlib.md5(seed_material.encode("utf-8")).hexdigest()[:8], 16)
rng = __import__("random").Random(seed_int)
out = list(informations)
rng.shuffle(out)
return out
def _convert_winogrande_for_mix(
src_path: str,
dst_path: str,
misinformed_count: int,
num_agents: int,
debug_mode: bool,
seed: int,
) -> None:
"""
Convert winogrande_misinformed.json to MALLM InputExample list, with
informations sized for `num_agents` and `misinformed_count` misinformation slots.
"""
data = _read_json(src_path)
if debug_mode and isinstance(data, list):
data = data[:5]
out = []
for it in data:
sentence = it.get("sentence", "")
options = it.get("options", [])
answer = it.get("answer")
if isinstance(it.get("gold_index"), int):
# MINT v1.2 items carry the resolved 0-based gold option (see mint_data.py); their
# "answer" field is 0-based too, so the 1-based rule below would empty every reference.
idx = int(it["gold_index"])
else:
try:
idx = int(answer) - 1 if isinstance(answer, str) else int(answer)
except Exception:
idx = 0
correct = ""
if isinstance(options, list) and 0 <= idx < len(options):
correct = str(options[idx])
input_str = sentence
if isinstance(options, list) and len(options) >= 2:
input_str += "\nOptions:\nA) " + str(options[0]) + "\nB) " + str(options[1])
misinfo = _choose_misinformation(it)
informations: List[Optional[str]] = [misinfo] * misinformed_count + [None] * (num_agents - misinformed_count)
informations = _stable_shuffle_informations(
informations=informations,
example_id=str(it.get("id", it.get("ID", "")) or str(uuid.uuid4())),
misinformed_count=misinformed_count,
global_seed=seed,
)
out.append(
{
"example_id": it.get("id", str(uuid.uuid4())),
"dataset_id": "winogrande",
"inputs": [input_str],
"context": None,
"references": [correct] if correct else [],
"metadata": {
"misinformed_count": misinformed_count,
"informed_count": (num_agents - misinformed_count),
"num_agents": num_agents,
},
"informations": informations,
}
)
os.makedirs(os.path.dirname(dst_path), exist_ok=True)
with open(dst_path, "w") as f:
json.dump(out, f)
def run_with_mallm(
mock: bool,
endpoint_url: str,
model_name: str,
debug_mode: bool = False,
num_agents: int = 5,
seed: int = 0,
continue_mode: bool = False,
) -> None:
"""
Run a 5-agent debate using MALLM on winogrande with varying misinformed-agent counts.
"""
_ensure_mallm_on_path()
if mock:
_install_runtime_stubs()
from mallm import scheduler
from mallm.utils.config import Config
script_dir = os.path.dirname(os.path.abspath(__file__))
out_root_dir = os.path.join(script_dir, "out")
os.makedirs(out_root_dir, exist_ok=True)
model_name_suffix = "mock_model" if mock else _safe_model_name(model_name)
out_dir = os.path.join(out_root_dir, model_name_suffix)
os.makedirs(out_dir, exist_ok=True)
mallm_io_dir = os.path.join(out_dir, "exp3_mallm_io")
os.makedirs(mallm_io_dir, exist_ok=True)
# MINT release directory (MINT_dataset_v1.2 first; see mint_data._DATA_DIR_CANDIDATES).
data_dir = str(resolve_dataset_path("winogrande").parent)
src = os.path.join(data_dir, "winogrande_misinformed.json")
if not os.path.exists(src):
raise FileNotFoundError(f"Dataset not found: {src}")
# Determine how many samples we expect per mix for resume logic
try:
raw_data = _read_json(src)
expected_total = len(raw_data[:5]) if debug_mode else len(raw_data)
except Exception:
expected_total = 5 if debug_mode else None
out_path = os.path.join(out_dir, "exp3_results_winogrande.json")
def _load_existing() -> dict:
if not os.path.exists(out_path):
return {}
try:
with open(out_path, "r") as f:
return json.load(f)
except Exception:
return {}
def _evaluate_and_aggregate(output_path: str) -> dict:
try:
with open(output_path, "r") as f:
records = json.load(f)
except Exception:
records = []
# Unified extractor (letter or option text, explicit status).
from answer_extraction import STATUS_PARSED, extract_mc_answer
results = []
correct_count = 0
total = 0
for rec in records:
inputs = rec.get("input") or []
input_str = inputs[0] if isinstance(inputs, list) and inputs else ""
refs = rec.get("references") or []
final_answer = rec.get("finalAnswer")
predicted = str(final_answer) if final_answer is not None else ""
m = re.search(r"Options:\n(?:A\) (.*)\n)(?:B\) (.*))", input_str)
options = [m.group(1), m.group(2)] if m else []
ex = extract_mc_answer(predicted, options)
is_correct = False
if isinstance(refs, list) and refs and ex.status == STATUS_PARSED:
is_correct = ex.text is not None and ex.text.strip().lower() == str(refs[0]).strip().lower()
total += 1
if is_correct:
correct_count += 1
results.append(
{
"sentence": input_str,
"response": predicted,
"predicted_answer": predicted,
"extraction_status": ex.status,
"correct_answer": (refs[0] if isinstance(refs, list) and refs else ""),
"is_correct": bool(is_correct),
"is_multiple_choice": True,
"options": options or None,
"misinformation_strategy": "false_fact",
"mallm_log": rec,
}
)
accuracy = (correct_count / total) if total else 0.0
return {
"accuracy": accuracy,
"correct_count": correct_count,
"total_count": total,
"results": results,
}
def _save_aggregate(mix_key: str, mix_results: dict) -> None:
existing = _load_existing()
existing[mix_key] = mix_results
with open(out_path, "w") as f:
json.dump(existing, f)
# Six mixes requested by user
mixes = list(range(0, num_agents + 1))
existing_results = _load_existing() if continue_mode else {}
for misinformed_count in mixes:
informed_count = num_agents - misinformed_count
mix_key = f"{misinformed_count}_misinformed_{informed_count}_informed"
if continue_mode and mix_key in existing_results:
prev = existing_results.get(mix_key, {}) if isinstance(existing_results, dict) else {}
prev_total = prev.get("total_count")
prev_cfg = prev.get("config") if isinstance(prev, dict) else None
cfg_match = (
isinstance(prev_cfg, dict)
and prev_cfg.get("seed") == seed
and prev_cfg.get("debug_mode") == debug_mode
and prev_cfg.get("num_agents") == num_agents
and prev_cfg.get("misinformed_count") == misinformed_count
and prev_cfg.get("model_name") == model_name
and prev_cfg.get("endpoint_url") == endpoint_url
)
total_match = (expected_total is None) or (prev_total == expected_total)
if cfg_match and total_match:
print(f"[CONTINUE] Skipping already-completed mix: {mix_key} (total_count={prev_total})")
continue
print(f"[CONTINUE] Re-running mix (incomplete or config changed): {mix_key}")
input_path = os.path.join(mallm_io_dir, f"exp3_mallm_input_winogrande_{mix_key}.json")
output_path = os.path.join(mallm_io_dir, f"exp3_mallm_output_winogrande_{mix_key}.json")
_convert_winogrande_for_mix(
src_path=src,
dst_path=input_path,
misinformed_count=misinformed_count,
num_agents=num_agents,
debug_mode=debug_mode,
seed=seed,
)
cfg = Config(
input_json_file_path=input_path,
output_json_file_path=output_path,
task_instruction_prompt="",
task_instruction_prompt_template="winogrande",
endpoint_url=endpoint_url,
model_name=model_name,
discussion_paradigm="memory",
decision_protocol="simple_voting",
max_turns=5,
num_agents=num_agents,
agent_generator="informed",
agent_generators_list=["informed"] * num_agents,
use_chain_of_thought=False,
concurrent_api_requests=250,
shuffle_input_samples=False,
)
mallm_scheduler = scheduler.Scheduler(cfg)
mallm_scheduler.run()
mix_results = _evaluate_and_aggregate(output_path)
mix_results["config"] = {
"dataset": "winogrande",
"num_agents": num_agents,
"misinformed_count": misinformed_count,
"informed_count": informed_count,
"seed": seed,
"debug_mode": debug_mode,
"expected_total_count": expected_total,
"endpoint_url": endpoint_url,
"model_name": model_name,
"timestamp_utc": datetime.now(timezone.utc).isoformat(),
}
_save_aggregate(mix_key, mix_results)
print(f"MALLM debate complete for winogrande [{mix_key}]. Output: {output_path}")
def _env_or_default_endpoint(default_url: str) -> str:
"""
Resolve endpoint from environment variables, falling back to the provided default.
Supported env vars: MALLM_ENDPOINT_URL, OPENAI_BASE_URL, OPENAI_API_BASE
"""
return (
os.environ.get("MALLM_ENDPOINT_URL")
or os.environ.get("OPENAI_BASE_URL")
or os.environ.get("OPENAI_API_BASE")
or default_url
)
def _endpoint_alive(endpoint_url: str, timeout: float = 2.0) -> bool:
"""
Lightweight connectivity probe. Returns True if TCP/HTTP responds (any non-5xx).
Assumes endpoint_url already contains '/v1'.
"""
try:
import requests
base = endpoint_url.rstrip("/")
resp = requests.get(f"{base}/models", timeout=timeout)
return resp.status_code < 500
except Exception:
return False
def _discover_endpoint_from_mallm(model_name: str) -> Optional[str]:
"""
Try to discover a running mallm model endpoint by parsing mallm port files.
"""
this_dir = os.path.dirname(os.path.abspath(__file__))
mallm_repo_root = os.path.abspath(os.path.join(this_dir, "..", "mallm"))
safe_model = model_name.replace("/", "-")
pattern = os.path.join(mallm_repo_root, f"port-{safe_model}-*.txt")
candidates = sorted(glob.glob(pattern), key=os.path.getmtime, reverse=True)
for path in candidates:
try:
with open(path, "r") as f:
text = f.read()
m = re.search(
r"Connect via:\s*ssh\s+-L\s+\d+:(?P<host>[A-Za-z0-9._-]+):(?P<port>\d+)",
text,
)
if m:
host = m.group("host")
port = m.group("port")
url = f"http://{host}:{port}/v1"
if _endpoint_alive(url, timeout=1.5):
return url
m_host = re.search(r"Running on instance:\s*(?P<host>\S+)", text)
m_port = re.search(r"Port:\s*(?P<port>\d+)", text)
if m_host and m_port:
url = f"http://{m_host.group('host')}:{m_port.group('port')}/v1"
if _endpoint_alive(url, timeout=1.5):
return url
except Exception:
continue
return None
def _get_num_gpus() -> int:
try:
import torch # type: ignore
n = int(torch.cuda.device_count())
if n > 0:
return n
except Exception:
pass
cvd = os.environ.get("CUDA_VISIBLE_DEVICES")
if cvd:
try:
return len([x for x in cvd.split(",") if x.strip() != ""])
except Exception:
return 1
return 1
def _port_is_busy(host: str, port: int) -> bool:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(0.5)
try:
return s.connect_ex((host, port)) == 0
except Exception:
return False
def _find_free_port(start_port: int = 8080, host: str = "127.0.0.1", max_tries: int = 50) -> int:
port = start_port
for _ in range(max_tries):
if not _port_is_busy(host, port):
return port
port += 1
return start_port
def _start_model_server(model_name: str, desired_port: Optional[int] = None) -> Optional[dict]:
"""
Start a local vLLM OpenAI-compatible server (aligned with exp2.py).
"""
host = "127.0.0.1"
port = desired_port or 8080
if _port_is_busy(host, port):
port = _find_free_port(start_port=port, host=host)
num_gpus = max(1, _get_num_gpus())
logs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "out")
os.makedirs(logs_dir, exist_ok=True)
proc: Optional[subprocess.Popen] = None
server_kind: Optional[str] = None
log_path: Optional[str] = None
f = None
def _start_vllm() -> bool:
nonlocal proc, server_kind, log_path, f
vllm_module = "vllm.entrypoints.openai.api_server"
try:
apply_gpt_oss_tiktoken_env(model_name)
__import__("vllm")
_mml = os.environ.get("VLLM_MAX_MODEL_LEN", "").strip()
if _mml:
max_model_len = int(_mml)
else:
max_model_len = vllm_max_model_len_default(model_name)
gpu_mem_util = os.environ.get("VLLM_GPU_MEMORY_UTILIZATION", "").strip()
if not gpu_mem_util:
gpu_mem_util = str(vllm_gpu_memory_utilization_default(model_name))
kv_cache_dtype = os.environ.get("VLLM_KV_CACHE_DTYPE", "").strip()
max_num_batched_tokens = os.environ.get("VLLM_MAX_BATCHED_TOKENS", "").strip()
cmd = [
sys.executable,
"-m",
vllm_module,
"--host",
host,
"--port",
str(port),
"--model",
model_name,
"--tensor-parallel-size",
str(num_gpus),
]
trc = (os.environ.get("VLLM_TRUST_REMOTE_CODE") or "").strip().lower()
if not trc and vllm_trust_remote_code_default(model_name):
cmd.append("--trust-remote-code")
elif trc in ("1", "true", "yes"):
cmd.append("--trust-remote-code")
cmd += ["--max-model-len", str(max_model_len)]
if gpu_mem_util:
cmd += ["--gpu-memory-utilization", str(gpu_mem_util)]
if kv_cache_dtype:
cmd += ["--kv-cache-dtype", kv_cache_dtype]
if max_num_batched_tokens:
cmd += ["--max-num-batched-tokens", str(max_num_batched_tokens)]
cmd.extend(vllm_extra_args_for_model(model_name))
log_path = os.path.join(logs_dir, f"vllm_server_{port}.log")
f = open(log_path, "w")
proc = subprocess.Popen(cmd, stdout=f, stderr=subprocess.STDOUT, preexec_fn=os.setsid)
server_kind = "vllm"
return True
except Exception:
proc = None
server_kind = None
return False
try:
if not _start_vllm():
proc = None
server_kind = None
except Exception:
proc = None
server_kind = None
if proc is None or server_kind is None:
if log_path:
try:
f.close() # type: ignore[name-defined]
except Exception:
pass
return None
def _terminate_server() -> None:
try:
if proc.poll() is None:
os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
except Exception:
pass
atexit.register(_terminate_server)
endpoint = f"http://{host}:{port}/v1"
print(
f"[INFO] Started {server_kind} server on {host}:{port} for model '{model_name}'. Waiting for readiness..."
)
start_t = time.time()
max_wait_s = int(os.environ.get("MODEL_STARTUP_TIMEOUT_SECONDS", "1800"))
probe_interval = 5.0
last_report = -1
while time.time() - start_t < max_wait_s:
if _endpoint_alive(endpoint, timeout=2.5):
print(f"[INFO] Model server is ready at {endpoint}.")
return {"process": proc, "endpoint_url": endpoint, "kind": server_kind, "port": port}
elapsed = int(time.time() - start_t)
if elapsed // 15 != last_report // 15:
print(f"[INFO] Waiting for model server... {elapsed}s elapsed")
last_report = elapsed
if proc.poll() is not None:
print(f"[ERROR] Model server process exited prematurely. See log: {log_path}")
break
time.sleep(probe_interval)
print(f"[ERROR] Timed out waiting for model server to start after {max_wait_s}s. See log: {log_path}")
return None
def main() -> None:
parser = argparse.ArgumentParser(
description="Run multi-agent debate (MALLM) on winogrande with varying misinformed/informed agent composition."
)
parser.add_argument("--mock", action="store_true", help="Use mock:// endpoint with lightweight stubs for a quick test run.")
parser.add_argument("--endpoint_url", type=str, default="http://127.0.0.1:8080/v1", help="OpenAI-compatible endpoint URL for real runs.")
parser.add_argument(
"--model_name",
"--model",
dest="model_name",
type=str,
default="openai/gpt-oss-120b",
help="Model name for the endpoint (e.g. zai-org/GLM-4.7-Flash).",
)
parser.add_argument("--no_autostart", action="store_true", help="Do not try to auto-start a local model server if endpoint is unreachable.")
parser.add_argument("--debug", action="store_true", help="Debug mode: process only 5 samples per mix.")
parser.add_argument(
"--continue",
dest="continue_mode",
action="store_true",
help=(
"Resume: skip mixes already completed in "
"out/<model_name>/exp3_results_winogrande.json "
"(only safe if args match)."
),
)
parser.add_argument("--seed", type=int, default=0, help="Seed used for deterministic per-example shuffling of misinformed slots.")
args = parser.parse_args()
resolved_model_name = normalize_model_name(args.model_name)
if args.mock:
run_with_mallm(True, "mock://local", resolved_model_name, debug_mode=args.debug, seed=args.seed, continue_mode=args.continue_mode)
return
resolved_endpoint = _env_or_default_endpoint(args.endpoint_url)
if resolved_endpoint.startswith("http") and not _endpoint_alive(resolved_endpoint, timeout=1.5):
auto = _discover_endpoint_from_mallm(resolved_model_name)
if auto:
print(f"[INFO] Provided endpoint '{resolved_endpoint}' is unreachable. Using discovered endpoint '{auto}' instead.")
resolved_endpoint = auto
else:
print(f"[WARN] Endpoint '{resolved_endpoint}' is unreachable.")
if not args.no_autostart:
try:
host_port = re.search(r"https?://([^:/]+):(\d+)/v1", resolved_endpoint)
desired_port = 8080
if host_port and host_port.group(1) in ("127.0.0.1", "localhost"):
try:
desired_port = int(host_port.group(2))
except Exception:
desired_port = 8080
started = _start_model_server(resolved_model_name, desired_port=desired_port)
if started:
resolved_endpoint = started["endpoint_url"] # type: ignore[index]
else:
print("[ERROR] Could not auto-start a model server. Please start one manually or provide a reachable --endpoint_url.")
except Exception as e:
print(f"[ERROR] Auto-start failed: {e}. Please start a model server or set --no_autostart.")
else:
print("[INFO] Auto-start disabled (--no_autostart). Proceeding without starting a local server.")
run_with_mallm(False, resolved_endpoint, resolved_model_name, debug_mode=args.debug, seed=args.seed, continue_mode=args.continue_mode)
if __name__ == "__main__":
main()