-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathrun.py
More file actions
230 lines (191 loc) · 7.25 KB
/
Copy pathrun.py
File metadata and controls
230 lines (191 loc) · 7.25 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
#!/usr/bin/env python3
"""
Unified GRPO trainer with integrated vLLM server (shared_vllm mode).
Combines vLLM server startup and trainer into a single command:
python example_trainer/run.py --model Qwen/Qwen3-4B-Instruct --training-steps 20
This script:
1. Starts vLLM server with shared weights enabled
2. Waits for vLLM to be ready and bridge config to be created
3. Starts the GRPO trainer in shared_vllm mode
4. Handles cleanup on exit
For other modes (legacy, LoRA), use grpo.py instead.
"""
import atexit
import os
import signal
import subprocess
import sys
import time
from pathlib import Path
import requests
from .cli import create_unified_parser
from .config import TrainingConfig
from .trainers import train_shared_vllm
def wait_for_vllm(port: int, timeout: int = 300) -> bool:
"""Wait for vLLM server to be ready."""
print(f"[Run] Waiting for vLLM server on port {port}...")
start = time.time()
while time.time() - start < timeout:
try:
response = requests.get(f"http://localhost:{port}/health", timeout=5)
if response.status_code == 200:
print(f"[Run] ✓ vLLM server is ready (took {time.time() - start:.1f}s)")
return True
except requests.exceptions.ConnectionError:
pass
except Exception as e:
print(f"[Run] Health check error: {e}")
time.sleep(2)
print(f"[Run] ✗ vLLM server failed to start within {timeout}s")
return False
def wait_for_bridge_config(config_path: str, timeout: int = 60) -> bool:
"""Wait for vLLM bridge config to be created."""
print(f"[Run] Waiting for bridge config at {config_path}...")
start = time.time()
while time.time() - start < timeout:
if os.path.exists(config_path):
try:
import json
with open(config_path, "r") as f:
config = json.load(f)
if config.get("ipc_handles") and len(config["ipc_handles"]) > 0:
print(
f"[Run] ✓ Bridge config ready with {len(config['ipc_handles'])} IPC handles"
)
return True
except Exception:
pass
time.sleep(1)
print(f"[Run] ✗ Bridge config not created within {timeout}s")
return False
def main():
# Parse args using shared CLI module
parser = create_unified_parser()
args = parser.parse_args()
# Create log directory
log_dir = getattr(args, "log_dir", "./logs")
os.makedirs(log_dir, exist_ok=True)
# Bridge config path
bridge_config_path = "./vllm_bridge_config.json"
# Clean up old bridge config
if os.path.exists(bridge_config_path):
os.remove(bridge_config_path)
print("[Run] Removed old bridge config")
# === Print Configuration ===
print("\n" + "=" * 60)
print("STARTING UNIFIED GRPO TRAINER (shared_vllm mode)")
print("=" * 60)
print(f"Model: {args.model_name}")
print(f"vLLM port: {args.vllm_port}")
print(f"GPU memory utilization: {args.gpu_memory_utilization}")
print(f"Training steps: {args.training_steps}")
print(f"Optimizer: {args.optimizer}")
print(f"GRPO: clip_eps={args.clip_eps}")
print("=" * 60 + "\n")
# Get the path to vllm_api_server.py
script_dir = Path(__file__).parent
vllm_server_script = script_dir / "vllm_api_server.py"
if not vllm_server_script.exists():
print(f"[Run] ✗ vLLM server script not found at {vllm_server_script}")
sys.exit(1)
# Extract device index from args.device
device_index = "0"
if ":" in args.device:
device_index = args.device.split(":")[1]
# Build vLLM environment
vllm_env = os.environ.copy()
vllm_env["VLLM_ENABLE_SHARED_WEIGHTS"] = "1"
vllm_env["VLLM_BRIDGE_CONFIG_PATH"] = bridge_config_path
vllm_env["CUDA_VISIBLE_DEVICES"] = device_index
vllm_env["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
vllm_env["VLLM_USE_V1"] = "0" # v0 engine required for shared weights patches
vllm_env["VLLM_WORKER_MULTIPROC_METHOD"] = "spawn" # Required for CUDA
# Build vLLM command
vllm_cmd = [
sys.executable,
"-u",
str(vllm_server_script),
"--model",
args.model_name,
"--port",
str(args.vllm_port),
"--dtype",
args.dtype,
"--gpu-memory-utilization",
str(args.gpu_memory_utilization),
"--max-model-len",
str(args.max_model_len),
"--enforce-eager", # Required for shared weights
]
vllm_log_path = os.path.join(log_dir, "vllm.log")
print(f"[Run] Starting vLLM server (log: {vllm_log_path})...")
vllm_log = open(vllm_log_path, "w")
vllm_process = subprocess.Popen(
vllm_cmd,
env=vllm_env,
stdout=vllm_log,
stderr=subprocess.STDOUT,
)
# Register cleanup
def cleanup():
print("\n[Run] Cleaning up...")
if vllm_process.poll() is None:
print("[Run] Terminating vLLM server...")
vllm_process.terminate()
try:
vllm_process.wait(timeout=10)
except subprocess.TimeoutExpired:
vllm_process.kill()
vllm_log.close()
print("[Run] Cleanup complete.")
atexit.register(cleanup)
signal.signal(signal.SIGINT, lambda s, f: sys.exit(0))
signal.signal(signal.SIGTERM, lambda s, f: sys.exit(0))
# Wait for vLLM to be ready
if not wait_for_vllm(args.vllm_port, timeout=500):
print("[Run] ✗ vLLM server failed to start. Check logs at:", vllm_log_path)
sys.exit(1)
# Wait for bridge config
if not wait_for_bridge_config(bridge_config_path, timeout=60):
print("[Run] ✗ Bridge config not created. Check vLLM logs.")
sys.exit(1)
# === Start Trainer ===
print("\n[Run] Starting GRPO trainer...")
# Build config - override some fields for shared_vllm mode
config = TrainingConfig(
model_name=args.model_name,
lr=args.lr,
training_steps=args.training_steps,
batch_size=args.batch_size,
seq_len=args.seq_len,
gradient_accumulation_steps=args.gradient_accumulation_steps,
warmup_steps=getattr(args, "warmup_steps", 0),
optimizer=args.optimizer,
device="cuda:0", # Always 0 since we set CUDA_VISIBLE_DEVICES
save_path=args.save_path,
checkpoint_interval=args.checkpoint_interval,
# GRPO hyperparameters
clip_eps=args.clip_eps,
# vLLM settings
vllm_port=args.vllm_port,
vllm_gpu_memory_utilization=args.gpu_memory_utilization,
vllm_config_path=bridge_config_path,
# Mode settings
weight_bridge_mode="shared_vllm", # Always shared_vllm for run.py
atropos_url=args.atropos_url,
# Logging
use_wandb=args.use_wandb,
wandb_project=args.wandb_project,
benchmark=True, # Always show timing info for run.py
debug_loading=getattr(args, "debug_loading", False),
)
try:
train_shared_vllm(config)
print("\n[Run] ✓ Training completed successfully!")
except Exception as e:
print(f"\n[Run] ✗ Training failed: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()