-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmlx_distributed_benchmark.py
More file actions
379 lines (327 loc) · 11.6 KB
/
Copy pathmlx_distributed_benchmark.py
File metadata and controls
379 lines (327 loc) · 11.6 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
#!/usr/bin/env python3
"""
Benchmark script for MLX distributed inference via mlx.launch.
This script runs benchmarks using mlx_lm.generate launched with mlx.launch,
which makes it behave similarly to mlx_benchmark.py while using distributed
execution (for example with the JACCL backend).
"""
import argparse
import math
import re
import subprocess
import sys
import time
from pathlib import Path
from typing import Dict, List, Optional
import benchmark_common as common
def get_env_value(env_list: List[str], key: str) -> Optional[str]:
prefix = f"{key}="
for item in env_list:
if item.startswith(prefix):
return item.split("=", 1)[1]
return None
def set_env_value(env_list: List[str], key: str, value: str) -> List[str]:
prefix = f"{key}="
updated: List[str] = []
replaced = False
for item in env_list:
if item.startswith(prefix):
updated.append(f"{key}={value}")
replaced = True
else:
updated.append(item)
if not replaced:
updated.append(f"{key}={value}")
return updated
def resolve_sharded_script(script_arg: str) -> Optional[Path]:
"""Resolve sharded_generate.py from explicit path or installed mlx_lm package."""
candidate = Path(script_arg).expanduser()
candidates = [candidate]
if not candidate.is_absolute():
candidates.append(Path.cwd() / candidate)
try:
import mlx_lm
candidates.append(Path(mlx_lm.__file__).resolve().parent / "examples" / "sharded_generate.py")
except Exception:
pass
for path in candidates:
if path.exists():
return path.resolve()
return None
def run_benchmark(
model_url: str,
context_file: Path,
backend: str,
hostfile: str,
sharded_script: str,
launcher: str = "mlx.launch",
launch_env: Optional[List[str]] = None,
pipeline: bool = False,
max_tokens: int = 128,
timeout: int = 1800,
) -> Optional[Dict]:
"""Run one distributed MLX benchmark for a context file."""
print(f"Running distributed benchmark for {context_file}...")
script_path = Path(sharded_script).expanduser()
cmd = [launcher, "--backend", backend]
if launch_env:
for env_var in launch_env:
cmd.extend(["--env", env_var])
cmd.extend(
[
"--hostfile",
hostfile,
str(script_path),
"--prompt",
"-",
"--model",
model_url,
"--max-tokens",
str(max_tokens),
]
)
if pipeline:
cmd.append("--pipeline")
start_time = time.time()
try:
with open(context_file, "r") as handle:
process = subprocess.Popen(
cmd,
stdin=handle,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1,
)
lines: List[str] = []
try:
assert process.stdout is not None
for line in process.stdout:
lines.append(line)
return_code = process.wait(timeout=timeout)
except subprocess.TimeoutExpired:
process.kill()
process.wait()
print(f"Timeout running benchmark for {context_file}")
return None
total_wall_time = time.time() - start_time
if return_code != 0:
print("Error running distributed benchmark command.")
return None
output = "".join(lines)
prompt_match = re.search(
r"Prompt:\s*(\d+)\s*tokens,\s*([\d.]+)\s*tokens-per-sec",
output,
)
gen_match = re.search(
r"Generation:\s*(\d+)\s*tokens,\s*([\d.]+)\s*tokens-per-sec",
output,
)
memory_match = re.search(r"Peak memory:\s*([\d.]+)\s*GB", output)
if not prompt_match or not gen_match:
print(f"Failed to parse MLX distributed output for {context_file}")
return None
# Best-effort extraction of generated text before the metrics section.
metrics_start = output.find("\n==========\nPrompt:")
if metrics_start != -1:
generated_text = output[:metrics_start].strip()
else:
generated_text = output
print(f" Prompt: {prompt_match.group(1)} tokens, {prompt_match.group(2)} tokens-per-sec")
print(f" Generation: {gen_match.group(1)} tokens, {gen_match.group(2)} tokens-per-sec")
if memory_match:
print(f" Peak memory: {memory_match.group(1)} GB")
print(f" Total wall time: {total_wall_time:.2f}s")
parsed: Dict[str, object] = {
"context_size": Path(context_file).stem,
"prompt_tokens": int(prompt_match.group(1)),
"prompt_tps": float(prompt_match.group(2)),
"generation_tokens": int(gen_match.group(1)),
"generation_tps": float(gen_match.group(2)),
"total_time": total_wall_time,
"eval_duration": math.nan,
"prompt_eval_duration": math.nan,
"time_to_first_token": math.nan,
"generated_text": generated_text,
}
if memory_match:
parsed["peak_memory_gb"] = float(memory_match.group(1))
return parsed
except Exception as exc:
print(f"Error running benchmark: {exc}")
return None
def check_mlx_installed() -> bool:
"""Check if mlx_lm is installed."""
try:
import mlx_lm # noqa: F401
return True
except ImportError:
return False
def main() -> int:
parser = argparse.ArgumentParser(description="Run distributed MLX benchmarks on context files via mlx.launch")
parser.add_argument("model", help="MLX model path or repo")
parser.add_argument(
"--backend",
default="jaccl",
help="Distributed backend for mlx.launch (default: jaccl)",
)
parser.add_argument(
"--hostfile",
required=True,
help="Path to mlx.launch hostfile JSON",
)
parser.add_argument(
"--sharded-script",
default="mlx_lm/examples/sharded_generate.py",
help="Path to mlx_lm/examples/sharded_generate.py",
)
parser.add_argument(
"--env",
action="append",
default=[],
help="Environment variable for mlx.launch, KEY=VALUE (repeatable)",
)
parser.add_argument(
"--launcher",
default="mlx.launch",
help="Launcher command (default: mlx.launch)",
)
parser.add_argument(
"--pipeline",
action="store_true",
help="Use pipeline parallelism (passed to sharded_generate.py)",
)
parser.add_argument(
"--fallback-fast-synch-off",
dest="fallback_fast_synch_off",
action="store_true",
help=(
"If a context run fails with MLX_METAL_FAST_SYNCH=1, retry once with "
"MLX_METAL_FAST_SYNCH=0 (default: enabled)"
),
)
parser.add_argument(
"--no-fallback-fast-synch-off",
dest="fallback_fast_synch_off",
action="store_false",
help="Disable automatic retry with MLX_METAL_FAST_SYNCH=0",
)
parser.set_defaults(fallback_fast_synch_off=True)
common.setup_common_args(parser)
args = parser.parse_args()
if not check_mlx_installed():
print("MLX-LM is not installed. Please install it with: pip install mlx-lm")
return 1
resolved_script = resolve_sharded_script(args.sharded_script)
if not resolved_script:
print(f"Error: Cannot find sharded script from '{args.sharded_script}'.")
print("Provide --sharded-script /path/to/mlx_lm/examples/sharded_generate.py")
return 1
model_name = args.model.rstrip("/").split("/")[-1]
output_dir = common.create_output_directory("mlx-distributed", model_name, args.output_dir, cold_prefill=True)
context_files = common.find_context_files(args.contexts, args.context_dir)
if not context_files:
return 1
print("\nCollecting hardware information...")
hardware_info = common.get_hardware_info()
hardware_str = common.format_hardware_string(hardware_info)
print(f"Hardware: {hardware_str}")
print(f"Model: {args.model}")
print(f"Backend: {args.backend}")
print(f"Hostfile: {args.hostfile}")
print(f"Sharded script: {resolved_script}")
print(f"Max tokens: {args.max_tokens}")
if args.env:
print(f"Launch env: {', '.join(args.env)}")
if args.pipeline:
print("Parallel mode: pipeline")
# Warmup run
warmup_file = common.find_warmup_file()
if warmup_file:
print(f"\n{'=' * 50}")
print(f"Warmup run (excluded from results): {warmup_file.name}")
print(f"{'=' * 50}")
run_benchmark(
model_url=args.model,
context_file=warmup_file,
backend=args.backend,
hostfile=args.hostfile,
sharded_script=str(resolved_script),
launcher=args.launcher,
launch_env=args.env,
pipeline=args.pipeline,
max_tokens=args.max_tokens,
timeout=args.timeout,
)
print("Warmup complete.")
else:
print("Warning: 0.5k.txt not found, skipping warmup.")
start_time = time.time()
results = []
for context_file in context_files:
print(f"\n{'=' * 50}")
print(f"Benchmarking {context_file.name}...")
print(f"{'=' * 50}")
result = common.run_benchmark_peak(
run_benchmark,
model_url=args.model,
context_file=context_file,
backend=args.backend,
hostfile=args.hostfile,
sharded_script=str(resolved_script),
launcher=args.launcher,
launch_env=args.env,
pipeline=args.pipeline,
max_tokens=args.max_tokens,
timeout=args.timeout,
n_runs=args.runs,
)
if not result and args.fallback_fast_synch_off and get_env_value(args.env, "MLX_METAL_FAST_SYNCH") == "1":
print("Fast sync run failed; retrying this context with MLX_METAL_FAST_SYNCH=0...")
fallback_env = set_env_value(args.env, "MLX_METAL_FAST_SYNCH", "0")
result = common.run_benchmark_peak(
run_benchmark,
model_url=args.model,
context_file=context_file,
backend=args.backend,
hostfile=args.hostfile,
sharded_script=str(resolved_script),
launcher=args.launcher,
launch_env=fallback_env,
pipeline=args.pipeline,
max_tokens=args.max_tokens,
timeout=args.timeout,
n_runs=args.runs,
)
if result:
result["fast_synch_fallback"] = 1
if result:
results.append(result)
if args.save_responses:
output_filename = output_dir / f"response_{result['context_size']}.txt"
common.save_generated_text(result, args.model, output_filename, "MLX Distributed")
total_benchmark_time = time.time() - start_time
if not results:
print("\nNo successful benchmark results")
return 1
include_memory = any("peak_memory_gb" in item for item in results)
common.save_all_outputs(
results,
output_dir,
model_name,
"MLX Distributed",
hardware_info,
args,
include_memory=include_memory,
)
common.print_benchmark_summary(
results,
model_name,
"MLX Distributed",
hardware_info,
output_dir,
total_benchmark_time,
)
return 0
if __name__ == "__main__":
sys.exit(main())