forked from ray-project/llmperf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
llm_correctness.py
309 lines (271 loc) · 10.7 KB
/
llm_correctness.py
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
import argparse
import json
import os
from pathlib import Path
import random
import re
import time
from typing import Any, Dict, List, Optional, Tuple
import num2words
import ray
from tqdm import tqdm
from llmperf import common_metrics
from llmperf.common import SUPPORTED_APIS, construct_clients
from llmperf.models import RequestConfig
from llmperf.requests_launcher import RequestsLauncher
from llmperf.utils import (
LLMPerfResults,
)
MAX_RANDOM_NUMBER = 10000
def llm_correctness(
model: str,
additional_sampling_params: Optional[Dict[str, Any]] = None,
num_concurrent_requests: int = 1,
max_num_completed_requests: int = 500,
test_timeout_s=90,
llm_api="chat",
) -> Tuple[Dict[str, Any], List[Dict[str, Any]]]:
"""Get the token throughput and latencies for the given model.
Args:
model: The name of the model to query.
additional_sampling_params: Additional sampling parameters to send with the request.
For more information see the LLM APIs documentation for the completions
num_concurrent_requests: The number of concurrent requests to make. Increase
this to increase the amount of load and vice versa.
test_timeout_s: The amount of time to run the test for before reporting results.
llm_api: The type of request to make. Either "chat" or "litellm".
Returns:
A tuple containing summary metrics and raw results from the test.
"""
if not additional_sampling_params:
additional_sampling_params = {}
clients = construct_clients(llm_api=llm_api, num_clients=num_concurrent_requests)
req_launcher = RequestsLauncher(clients)
start_time = time.monotonic()
num_errored_requests = 0
num_mismatched_requests = 0
num_completed_requests = 0
sampling_params = {"temperature": 0.0}
sampling_params.update(additional_sampling_params)
completed_requests = []
iter = 0
pbar = tqdm(total=max_num_completed_requests)
while (
time.monotonic() - start_time < test_timeout_s
and num_completed_requests < max_num_completed_requests
):
iter += 1
rnd_number = random.randint(0, MAX_RANDOM_NUMBER)
rnd_num_words = num2words.num2words(rnd_number)
prompt = f"Convert the following sequence of words into a number: {rnd_num_words}.\nPrint the number first."
request_config = RequestConfig(
model=model,
prompt=(prompt, 0),
sampling_params=sampling_params,
metadata={"rnd_number": rnd_number},
llm_api=llm_api,
)
req_launcher.launch_requests(request_config)
if not (iter % num_concurrent_requests):
completed_requests.extend(req_launcher.get_next_ready())
pbar.update(len(completed_requests) - num_completed_requests)
num_completed_requests = len(completed_requests)
pbar.close()
end_time = time.monotonic()
if end_time - start_time >= test_timeout_s:
print("Test timed out before all requests could be completed.")
raw_results = []
print("Mismatched and errored requests.")
for out in completed_requests:
metrics, generated_text, completed_request_config = out
raw_results.append(
{
"metrics": metrics,
"generated_text": generated_text,
"request_config": dict(completed_request_config),
}
)
# if there were no errors when making request.
if not metrics[common_metrics.ERROR_CODE]:
try:
commas_between_numbers_re = r"(\d+),(?=\d)"
gen_text_commas_removed = re.sub(
commas_between_numbers_re, r"\1", generated_text
)
nums = re.findall(r"\d+", gen_text_commas_removed)
generated_text = gen_text_commas_removed.replace("\n", " ")
assert str(completed_request_config.metadata["rnd_number"]) in nums
except:
num_mismatched_requests += 1
print(
f" mismatched request: {generated_text}, expected: {completed_request_config.metadata['rnd_number']}"
)
else:
num_errored_requests += 1
print(
f" The request errored: {metrics[common_metrics.ERROR_CODE]}, "
f"{metrics[common_metrics.ERROR_MSG]} "
)
print()
error_rate = num_errored_requests / num_completed_requests
mismatch_rate = num_mismatched_requests / num_completed_requests
num_non_errored_requests = num_completed_requests - num_errored_requests
summary_metrics = {}
summary_metrics[common_metrics.NUM_ERRORS] = num_errored_requests
summary_metrics["num_mismatched_requests"] = num_mismatched_requests
summary_metrics["error_rate"] = error_rate
summary_metrics["mismatch_rate"] = mismatch_rate
summary_metrics[common_metrics.NUM_COMPLETED_REQUESTS] = num_completed_requests
summary_metrics["num_non_errored_requests"] = num_non_errored_requests
# Metadata
summary_metrics["model"] = model
summary_metrics["num_concurrent_requests"] = num_concurrent_requests
summary_metrics["additional_sampling_params"] = additional_sampling_params
summary_metrics["llm_api"] = llm_api
return summary_metrics, raw_results
def run(
llm_api: str,
model: str,
test_timeout_s: int,
max_num_completed_requests: int,
num_concurrent_requests: int,
additional_sampling_params: str,
results_dir: str,
user_metadata: Dict[str, str],
):
"""
Args:
llm_api: The type of request to make. Either "chat" or "litellm".
model: The name of the model to query.
max_num_completed_requests: The number of requests to complete before finishing the test.
test_timeout_s: The amount of time to run the test for before reporting results.
num_concurrent_requests: The number of concurrent requests to make. Increase
this to increase the amount of load and vice versa.
mean_input_tokens: The mean number of tokens to send in the prompt for the request.
stddev_input_tokens: The standard deviation of the number of tokens to send in the prompt for the request.
mean_output_tokens: The mean number of tokens to generate per request.
stddev_output_tokens: The standard deviation of the number of tokens to generate per request.
additional_sampling_params: Additional sampling parameters to send with the request.
For more information see the LLM APIs documentation for the completions.
results_dir: The directory to save the results to.
"""
summary_metrics, raw_results = llm_correctness(
model=model,
llm_api=llm_api,
test_timeout_s=test_timeout_s,
max_num_completed_requests=max_num_completed_requests,
num_concurrent_requests=num_concurrent_requests,
additional_sampling_params=json.loads(additional_sampling_params),
)
time.sleep(2)
print(
f"Results for llm correctness test for {model} queried with the {llm_api} api."
)
print(
f"Errors: {summary_metrics[common_metrics.NUM_ERRORS]}, "
f"Error rate: {summary_metrics['error_rate']}"
)
print(
f"Mismatched: {summary_metrics['num_mismatched_requests']}, "
f"Mismatch rate: {summary_metrics['mismatch_rate']}"
)
print(f"Completed: {summary_metrics[common_metrics.NUM_COMPLETED_REQUESTS]}")
print(f"Completed without errors: {summary_metrics['num_non_errored_requests']}")
if results_dir:
file_name = f"{model}_correctness"
file_name = re.sub(r"[^\w\d-]+", "-", file_name)
file_name = re.sub(r"-{2,}", "-", file_name)
summary_file_name = f"{file_name}_summary"
individual_responses_filename = f"{file_name}_individual_responses"
summary_metrics.update(user_metadata)
results = LLMPerfResults(name=summary_file_name, metadata=summary_metrics)
results_dir = Path(results_dir)
if not results_dir.exists():
results_dir.mkdir(parents=True)
elif not results_dir.is_dir():
raise ValueError(f"{results_dir} is not a directory")
with open(results_dir / f"{summary_file_name}.json", "w") as f:
json.dump(results.to_dict(), f, indent=4)
with open(results_dir / f"{individual_responses_filename}.json", "w") as f:
json.dump(raw_results, f, indent=4)
args = argparse.ArgumentParser(description="Run a correctness test for a given model.")
args.add_argument(
"--model", type=str, required=True, help="The model to use for this load test."
)
args.add_argument(
"--num-concurrent-requests",
type=int,
default=10,
help=("The number of concurrent requests to send. (default: %(default)s)"),
)
args.add_argument(
"--timeout",
type=int,
default=90,
help="The amount of time to run the load test for. (default: %(default)s)",
)
args.add_argument(
"--max-num-completed-requests",
type=int,
default=50,
help=(
"The number of requests to complete before finishing the test. Note "
"that its possible for the test to timeout first. (default: %(default)s)"
),
)
args.add_argument(
"--additional-sampling-params",
type=str,
default="{}",
help=(
"Additional sampling params to send with the each request to the LLM API. "
"(default: %(default)s) No additional sampling params are sent."
),
)
args.add_argument(
"--results-dir",
type=str,
default="",
help=(
"The directory to save the results to. "
"(`default: %(default)s`) No results are saved)"
),
)
args.add_argument(
"--llm-api",
type=str,
default="openai",
help=(
f"The type of request to make. The supported llm apis are {SUPPORTED_APIS} "
" (`default: %(default)s`)"
),
)
args.add_argument(
"--metadata",
type=str,
default="",
help=(
"A comma separated list of metadata to include in the results, e.g. "
"name=foo,bar=1. These will be added to the metadata field of the results. "
),
)
if __name__ == "__main__":
args = args.parse_args()
env_vars = dict(os.environ)
ray.init(runtime_env={"env_vars": env_vars})
# Parse user metadata.
user_metadata = {}
if args.metadata:
for item in args.metadata.split(","):
key, value = item.split("=")
user_metadata[key] = value
run(
llm_api=args.llm_api,
model=args.model,
test_timeout_s=args.timeout,
max_num_completed_requests=args.max_num_completed_requests,
num_concurrent_requests=args.num_concurrent_requests,
additional_sampling_params=args.additional_sampling_params,
results_dir=args.results_dir,
user_metadata=user_metadata,
)