-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathperformance_model.py
270 lines (233 loc) · 10.4 KB
/
performance_model.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
import math
import os
from abc import ABC, abstractmethod
import pandas as pd
from hydra.utils import get_original_cwd
from scipy.interpolate import interp1d
from task import TaskType, PromptTask, TokenTask
performance_model = None
class PerformanceModel(ABC):
"""
PerformanceModel helps estimate the duration of tasks or iterations,
under given hardware, model, and parallelism configurations.
Abstract class that must be subclassed.
"""
def __init__(self):
global performance_model
performance_model = self
@abstractmethod
def get_duration(self, task, batch, instance, *args, **kwargs):
"""
Returns the execution time of the task.
"""
raise NotImplementedError
@abstractmethod
def get_iteration_duration(self, batch, instance, *args, **kwargs):
"""
Returns the execution time of a contiguous iteration.
"""
raise NotImplementedError
class ConstantPerformanceModel(PerformanceModel):
"""
PerformanceModel that returns a constant value regardless of other parameters.
Used for testing purposes.
"""
def __init__(self, prompt_time, token_time):
super().__init__()
self.prompt_time = prompt_time
self.token_time = token_time
def get_duration(self, task, batch, instance, *args, **kwargs):
if task.task_type == TaskType.PROMPT:
return self.prompt_time
elif task.task_type == TaskType.TOKEN:
return self.token_time
else:
raise NotImplementedError
def get_iteration_duration(self, batch, instance, *args, **kwargs):
raise NotImplementedError
class DatabasePerformanceModel(PerformanceModel):
"""
PerformanceModel based on a CSV database of characterization runs.
Interpolates between data points and updates the database correspondingly.
The underlying predictor could be changed for different interpolation strategies.
"""
def __init__(self, db_path):
super().__init__()
self.db = pd.read_csv(os.path.join(get_original_cwd(), db_path),
dtype={"model": "category", "hardware": "category"})
# ensure the database has the correct columns
# and remove extraneous columns
self.db = self.db[["model",
"hardware",
"tensor_parallel",
"prompt_size",
"batch_size",
"token_size",
"prompt_time",
"token_time"]]
# convert to seconds
self.db["prompt_time"] = self.db["prompt_time"] / 1000
self.db["token_time"] = self.db["token_time"] / 1000
self.init_predictor()
def init_predictor(self):
"""
Predict using number of tokens in the batch.
"""
self.prompt_time_predictors = {}
self.token_time_predictors = {}
self.prompt_time_cache = {}
self.token_time_cache = {}
for model in self.db["model"].unique():
for hardware in self.db["hardware"].unique():
for tensor_parallel in self.db["tensor_parallel"].unique():
mask = (self.db["model"] == model) & \
(self.db["hardware"] == hardware) & \
(self.db["tensor_parallel"] == tensor_parallel)
db_subset = self.db[mask].copy()
if len(db_subset) == 0:
continue
db_subset["batch_tokens"] = db_subset["prompt_size"] * db_subset["batch_size"]
x = db_subset[["batch_tokens", "prompt_time"]].groupby("batch_tokens").median().index
y = db_subset[["batch_tokens", "prompt_time"]].groupby("batch_tokens").median()["prompt_time"]
self.prompt_time_predictors[(model, hardware, tensor_parallel)] = interp1d(
x, y, fill_value="extrapolate")
x = db_subset[["batch_tokens", "token_time"]].groupby("batch_tokens").median().index
y = db_subset[["batch_tokens", "token_time"]].groupby("batch_tokens").median()["token_time"]
self.token_time_predictors[(model, hardware, tensor_parallel)] = interp1d(
x, y, fill_value="extrapolate")
def _match(self, **kwargs):
"""
Returns a boolean mask for the database from kwargs.
"""
mask = True
for k, v in kwargs.items():
mask &= (self.db[k] == v)
return mask
def predict_new_row(self, **kwargs):
"""
Predicts the prompt and token time for a new row.
Inserts the new row into the database.
"""
model = kwargs["model"]
hardware = kwargs["hardware"]
tensor_parallel = kwargs["tensor_parallel"]
batch_tokens = kwargs["batch_tokens"]
new_row = pd.DataFrame(kwargs, index=[0])
prompt_time = self.prompt_time_predictors[(model, hardware, tensor_parallel)](batch_tokens)
token_time = self.token_time_predictors[(model, hardware, tensor_parallel)](batch_tokens)
new_row["prompt_time"] = prompt_time
new_row["token_time"] = token_time
self.db = pd.concat([self.db, new_row], ignore_index=True)
return new_row
def get_prompt_time(self, **kwargs):
"""
Returns the prompt time from the database.
"""
prompt_time = self.db[self._match(**kwargs)]["prompt_time"].median()
# if not found, predict
if math.isnan(prompt_time):
new_row = self.predict_new_row(**kwargs)
prompt_time = new_row["prompt_time"][0]
return prompt_time
def get_token_time(self, **kwargs):
"""
Returns the prompt time from the database.
"""
token_time = self.db[self._match(**kwargs)]["token_time"].median()
# if not found, predict
if math.isnan(token_time):
new_row = self.predict_new_row(**kwargs)
token_time = new_row["token_time"][0]
return token_time
def get_duration(self,
task,
batch,
instance,
*args,
**kwargs):
model = instance.model.name
hardware = instance.processors[0].name
pipeline_parallel = instance.model.parallelism.pipeline_parallelism
tensor_parallel = instance.model.parallelism.tensor_parallelism
if task.task_type == TaskType.PROMPT:
prompt_size = task.request.prompt_size
token_size = task.request.token_size
batch_size = len(batch)
prompt_time = self.get_prompt_time(model=model,
hardware=hardware,
tensor_parallel=tensor_parallel,
prompt_size=prompt_size,
batch_size=batch_size,
token_size=token_size,
batch=batch)
return prompt_time
elif task.task_type == TaskType.TOKEN:
prompt_size = task.request.prompt_size
token_size = task.request.token_size
batch_size = len(batch)
token_time = self.get_token_time(model=model,
hardware=hardware,
tensor_parallel=tensor_parallel,
prompt_size=prompt_size,
batch_size=batch_size,
token_size=token_size,
batch=batch)
return token_time * task.token_size
else:
raise NotImplementedError
def get_iteration_duration(self,
batch,
instance,
*args,
**kwargs):
"""
Note: assumes that prompts are always processed fully.
i.e., we currently do not support prompt chunking.
"""
model = instance.model.name
hardware = instance.processors[0].name
pipeline_parallel = instance.model.parallelism.pipeline_parallelism
tensor_parallel = instance.model.parallelism.tensor_parallelism
prompt_tasks = []
token_tasks = []
batch_tokens = 0
for task in batch:
if isinstance(task, PromptTask):
prompt_tasks.append(task)
batch_tokens += task.request.prompt_size
elif isinstance(task, TokenTask):
token_tasks.append(task)
batch_tokens += 1
else:
raise NotImplementedError
iteration_time = None
cache_key = (model, hardware, tensor_parallel, batch_tokens)
predictors_key = (model, hardware, tensor_parallel)
if len(prompt_tasks) == len(batch):
iteration_time = self.prompt_time_cache.get(cache_key)
if iteration_time is None:
iteration_time = float(self.prompt_time_predictors[predictors_key](batch_tokens))
self.prompt_time_cache[cache_key] = float(iteration_time)
elif len(token_tasks) == len(batch):
iteration_time = self.token_time_cache.get(cache_key)
if iteration_time is None:
iteration_time = float(self.token_time_predictors[predictors_key](batch_tokens))
self.token_time_cache[cache_key] = float(iteration_time)
else:
iteration_time = self.prompt_time_cache.get(cache_key)
if iteration_time is None:
iteration_time = float(self.prompt_time_predictors[predictors_key](batch_tokens))
self.prompt_time_cache[cache_key] = float(iteration_time)
iteration_time *= 1.1
assert iteration_time > 0
return iteration_time
def get_duration(*args, **kwargs):
"""
Returns the execution time of the task.
"""
return performance_model.get_duration(*args, **kwargs)
def get_iteration_duration(*args, **kwargs):
"""
Returns the execution time of a contiguous iteration.
"""
return performance_model.get_iteration_duration(*args, **kwargs)