-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathopenai_perf.py
97 lines (91 loc) · 2.95 KB
/
openai_perf.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
import openai
from timeit import default_timer as timer
def ttft_measurer(prompt, args):
model = get_model(args)
def single_request():
start = timer()
completion = openai.Completion.create(
model=model,
echo=False,
prompt=prompt,
max_tokens=1,
temperature=0,
n=1,
stream=True,
)
for _ in completion:
pass
return timer() - start
return single_request
def tpot_measurer(prompt, args):
model = get_model(args)
async def single_request():
start = timer()
completion = openai.Completion.create(
model=model,
echo=False,
prompt=prompt,
max_tokens=args.output_tokens,
temperature=0,
n=1,
stream=True,
)
i = 0
for _ in completion:
if i == 0:
start = timer()
i += 1
return (timer() - start) / (i - 1)
return single_request
def rate_throughput_measurer(prompt, args):
model = get_model(args)
async def single_request():
completion = await openai.Completion.acreate(
model=model,
echo=False,
prompt=prompt,
max_tokens=args.output_tokens,
temperature=0,
n=1,
stream=True,
)
async for _ in completion:
pass
return args.output_tokens
return single_request
def sample_rate_throughput_measurer(args):
model = get_model(args)
async def single_request(sample):
completion = await openai.Completion.acreate(
model=model,
echo=False,
prompt=sample["prompt"],
max_tokens=sample["output_len"],
temperature=0,
n=1,
stream=True,
)
async for _ in completion:
pass
return sample["output_len"]
return single_request
def sample_output_rate_throughput_measurer(args):
model = get_model(args)
async def single_request(sample):
completion = await openai.Completion.acreate(
model=model,
echo=False,
prompt=sample["prompt"],
temperature=1,
max_tokens=2048,
top_k=15,
n=1,
stream=False,
)
return completion.usage.completion_tokens
return single_request
def get_model(args):
openai.api_key = args.api_key
openai.api_base = args.api_base
models = openai.Model.list()
return models["data"][0]["id"]