|
6 | 6 |
|
7 | 7 | from mlx_lm import batch_generate, load, stream_generate |
8 | 8 | from mlx_lm.generate import DEFAULT_MODEL |
| 9 | +from mlx_lm.utils import pipeline_load |
9 | 10 |
|
10 | 11 |
|
11 | 12 | def setup_arg_parser(): |
@@ -56,11 +57,21 @@ def main(): |
56 | 57 | args = parser.parse_args() |
57 | 58 | mx.random.seed(0) |
58 | 59 |
|
| 60 | + group = mx.distributed.init() |
| 61 | + rank = group.rank() |
| 62 | + |
| 63 | + def rprint(*args, **kwargs): |
| 64 | + if rank == 0: |
| 65 | + print(*args, **kwargs) |
| 66 | + |
59 | 67 | model_path = args.model or DEFAULT_MODEL |
60 | 68 |
|
61 | | - model, tokenizer, config = load( |
62 | | - args.model, return_config=True, tokenizer_config={"trust_remote_code": True} |
63 | | - ) |
| 69 | + if group.size() > 1: |
| 70 | + model, tokenizer, config = pipeline_load(args.model, return_config=True) |
| 71 | + else: |
| 72 | + model, tokenizer, config = load( |
| 73 | + args.model, return_config=True, tokenizer_config={"trust_remote_code": True} |
| 74 | + ) |
64 | 75 |
|
65 | 76 | # Empty to avoid early stopping |
66 | 77 | tokenizer._eos_token_ids = {} |
@@ -89,26 +100,26 @@ def batch_bench(): |
89 | 100 | else: |
90 | 101 | _bench = batch_bench |
91 | 102 |
|
92 | | - print("Running warmup..") |
| 103 | + rprint("Running warmup..") |
93 | 104 | _bench() |
94 | 105 |
|
95 | 106 | report_keys = ["prompt_tps", "generation_tps", "peak_memory"] |
96 | | - print(f"Timing with {prompt_tokens=}, {generation_tokens=}, {batch_size=}.") |
| 107 | + rprint(f"Timing with {prompt_tokens=}, {generation_tokens=}, {batch_size=}.") |
97 | 108 | responses = [] |
98 | 109 | for i in range(args.num_trials): |
99 | 110 | response = _bench() |
100 | 111 | responses.append(response) |
101 | 112 | results = [(k, getattr(response, k)) for k in report_keys] |
102 | 113 | results = [f"{k}={v:.3f}" for k, v in results] |
103 | | - print(f"Trial {i+1}: " + ", ".join(results)) |
| 114 | + rprint(f"Trial {i+1}: " + ", ".join(results)) |
104 | 115 |
|
105 | 116 | def avg(k): |
106 | 117 | vals = (getattr(response, k) for response in responses) |
107 | 118 | return sum(vals) / args.num_trials |
108 | 119 |
|
109 | 120 | results = [(k, avg(k)) for k in report_keys] |
110 | 121 | results = [f"{k}={v:.3f}" for k, v in results] |
111 | | - print(f"Averages: " + ", ".join(results)) |
| 122 | + rprint(f"Averages: " + ", ".join(results)) |
112 | 123 |
|
113 | 124 |
|
114 | 125 | if __name__ == "__main__": |
|
0 commit comments