-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate_trace.py
291 lines (249 loc) · 10.7 KB
/
generate_trace.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
import os
from collections import namedtuple
import requests
import numpy as np
import pandas as pd
from scipy import stats
Distributions = namedtuple('Distributions', ['application_id',
'request_type',
'arrival_process',
'batch_size',
'prompt_size',
'token_size'])
Distribution = namedtuple('Distribution', ['name', 'params'])
def generate_samples(distribution, params, size):
"""
Generate random samples from the given distribution.
"""
if distribution == "constant":
return np.ones(size) * params["value"]
elif distribution == "normal":
return stats.norm(**params).rvs(size=size)
elif distribution == "truncnorm":
return stats.truncnorm(**params).rvs(size=size)
elif distribution == "randint":
return stats.uniform(**params).rvs(size=size)
elif distribution == "uniform":
return stats.uniform(**params).rvs(size=size)
elif distribution == "exponential":
return stats.expon(**params).rvs(size=size)
elif distribution == "poisson":
return stats.poisson(**params).rvs(size=size)
elif distribution == "trace":
df = pd.read_csv(params["filename"])
return df[params["column"]].sample(size, replace=True).values
else:
raise ValueError(f"Invalid distribution: {distribution}")
def generate_trace(max_requests, distributions, end_time=None):
"""
Generate a trace of requests based on the given distributions.
"""
# Generate request IDs
request_ids = np.arange(max_requests)
# Generate the distributions
arrival_timestamps = generate_samples(distributions.arrival_process.name,
distributions.arrival_process.params,
max_requests)
arrival_timestamps = np.cumsum(arrival_timestamps)
application_ids = generate_samples(distributions.application_id.name,
distributions.application_id.params,
max_requests)
application_ids = map(int, application_ids)
batch_sizes = generate_samples(distributions.batch_size.name,
distributions.batch_size.params,
max_requests)
batch_sizes = map(int, batch_sizes)
prompt_sizes = generate_samples(distributions.prompt_size.name,
distributions.prompt_size.params,
max_requests)
prompt_sizes = map(int, prompt_sizes)
token_sizes = generate_samples(distributions.token_size.name,
distributions.token_size.params,
max_requests)
token_sizes = map(int, token_sizes)
request_type_ids = generate_samples(distributions.request_type.name,
distributions.request_type.params,
max_requests)
request_type_ids = map(int, request_type_ids)
# Combine the arrays into a DataFrame
trace_df = pd.DataFrame({
"request_id": request_ids,
"request_type": request_type_ids,
"application_id": application_ids,
"arrival_timestamp": arrival_timestamps,
"batch_size": batch_sizes,
"prompt_size": prompt_sizes,
"token_size": token_sizes,
})
if end_time is not None:
trace_df = trace_df[trace_df["arrival_timestamp"] < end_time]
return trace_df
def get_exponential_scale(num_servers, utilization, request_duration):
"""
assumes that request_duration is in seconds
"""
interarrival_time = request_duration / (1.0 * utilization)
exponential_scale = interarrival_time / num_servers
return exponential_scale
def generate_trace_from_utilization(
max_requests,
end_time,
num_servers,
utilization,
request_duration,
pt_distributions_file):
"""
Generate request traces for the simulator using prompt and token
size distributions.
"""
exponential_scale = get_exponential_scale(num_servers, utilization, request_duration)
distributions = Distributions(
application_id=Distribution("constant", {"value": 0}),
request_type=Distribution("constant", {"value": 2}), # 2 is for LLM inference
arrival_process=Distribution("exponential", {"scale": exponential_scale}),
prompt_size=Distribution("trace", {"filename": pt_distributions_file,
"column": "ContextTokens"}),
token_size=Distribution("trace", {"filename": pt_distributions_file,
"column": "GeneratedTokens"}),
batch_size=Distribution("constant", {"value": 1}),
)
trace_df = generate_trace(max_requests,
distributions,
end_time=end_time)
return trace_df
def generate_trace_from_prompt_token_size_distributions(
max_requests,
end_time,
request_rate,
pt_distributions_filename):
"""
Generate request traces for the simulator using prompt and token
size distributions.
"""
distributions = Distributions(
application_id=Distribution("constant", {"value": 0}),
request_type=Distribution("constant", {"value": 2}), # 2 is for LLM inference
arrival_process=Distribution("exponential", {"scale": 1.0 / request_rate}),
prompt_size=Distribution("trace", {"filename": pt_distributions_filename,
"column": "ContextTokens"}),
#prompt_size=Distribution("truncnorm", {"a": (prompt_min-prompt_mean)/prompt_std,
# "b": (prompt_max-prompt_mean)/prompt_std,
# "loc": prompt_mean,
# "scale": prompt_std}),
token_size=Distribution("trace", {"filename": pt_distributions_filename,
"column": "GeneratedTokens"}),
#token_size=Distribution("truncnorm", {"a": (token_min-token_mean)/token_std,
# "b": (token_max-token_mean)/token_std,
# "loc": token_mean,
# "scale": token_std}),
batch_size=Distribution("constant", {"value": 1}),
)
trace_df = generate_trace(max_requests,
distributions,
end_time=end_time)
return trace_df
def generate_traces(max_requests,
end_time,
request_rates,
pt_distributions_file,
trace_filename_template):
"""
Generate traces with prompt/token size distributions.
"""
for request_rate in request_rates:
trace_df = generate_trace_from_prompt_token_size_distributions(
max_requests,
end_time,
request_rate,
pt_distributions_file)
trace_filename = trace_filename_template.format(request_rate)
trace_df.to_csv(trace_filename, index=False)
def generate_code_traces(
max_requests,
end_time,
request_rates,
code_distributions_file,
trace_filename_template="traces/rr_code_{}.csv"):
"""
code traces distribution
prompt_mean = 2048, prompt_std = 1973, prompt_min = 3, prompt_max = 7437
token_mean = 28, token_std = 60, token_min = 6, token_max = 1899
"""
if not os.path.exists(trace_filename_template[:trace_filename_template.rfind("/")]):
os.makedirs(trace_filename_template[:trace_filename_template.rfind("/")])
generate_traces(max_requests,
end_time,
request_rates,
code_distributions_file,
trace_filename_template)
def generate_conv_traces(
max_requests,
end_time,
request_rates,
conv_distributions_file,
trace_filename_template="traces/rr_conv_{}.csv"):
"""
conv traces distribution
prompt_mean = 1155, prompt_std = 1109, prompt_min = 2, prompt_max = 14050
token_mean = 211, token_std = 163, token_min = 7, token_max = 1000
"""
if not os.path.exists(trace_filename_template[:trace_filename_template.rfind("/")]):
os.makedirs(trace_filename_template[:trace_filename_template.rfind("/")])
generate_traces(max_requests,
end_time,
request_rates,
conv_distributions_file,
trace_filename_template)
def download_file(url, filename):
"""
Download a file from the given URL.
"""
response = requests.get(url)
with open(filename, "wb") as f:
f.write(response.content)
def download_azure_llm_traces():
"""
Download traces from the given URL.
"""
if not os.path.exists("data"):
os.makedirs("data")
url_base = "https://raw.githubusercontent.com/Azure/AzurePublicDataset/master/data/"
if not os.path.exists("data/code_distributions.csv"):
url = url_base + "AzureLLMInferenceTrace_code.csv"
download_file(url, "data/code_distributions.csv")
print("Downloaded code traces")
if not os.path.exists("data/conv_distributions.csv"):
url = url_base + "AzureLLMInferenceTrace_conv.csv"
download_file(url, "data/conv_distributions.csv")
print("Downloaded conv traces")
if __name__ == "__main__":
# download prompt and token size distributions
download_azure_llm_traces()
# generate request traces
generate_code_traces(
max_requests=1000000,
end_time=600,
request_rates=list(range(30, 251, 10)),
code_distributions_file="data/code_distributions.csv")
print("Generated code traces")
generate_conv_traces(
max_requests=1000000,
end_time=600,
request_rates=list(range(30, 251, 10)),
conv_distributions_file="data/conv_distributions.csv")
print("Generated conv traces")
# generate request traces for 2 min
generate_code_traces(
max_requests=1000000,
end_time=120,
request_rates=list(range(30, 101, 10)),
code_distributions_file="data/code_distributions.csv",
trace_filename_template="traces/rr_code_{}_2min.csv")
print("Generated code 2min traces")
generate_conv_traces(
max_requests=1000000,
end_time=120,
request_rates=list(range(30, 101, 10)),
conv_distributions_file="data/conv_distributions.csv",
trace_filename_template="traces/rr_conv_{}_2min.csv")
print("Generated conv 2min traces")