forked from siyan-zhao/prepacking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiling_time_and_memory.py
290 lines (252 loc) · 9.15 KB
/
profiling_time_and_memory.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
import os
import torch
import threading
import time
import GPUtil
import fire
import random
from tqdm import tqdm
import numpy as np
from typing import List
from prettytable import PrettyTable
from dataset_utils import (
load_and_evaluate_dataset,
sample_batches,
sample_batches_by_length,
unpack_kv,
)
from processor import PrePackProcessor
from utils import integer_program_packing, load_model_and_tokenizer
os.environ["TOKENIZERS_PARALLELISM"] = "false"
def monitor_gpu_utilization(stop_event, utilization_stats, device_id=0, interval=0.1):
max_utilization, total_utilization, count = 0, 0, 0
cuda_visible_devices = os.getenv("CUDA_VISIBLE_DEVICES", None)
visible_device_ids = list(map(int, cuda_visible_devices.split(",")))
while not stop_event.is_set():
gpus = GPUtil.getGPUs()
current_utilization = gpus[visible_device_ids[device_id]].load
max_utilization = max(max_utilization, current_utilization)
total_utilization += current_utilization
count += 1
time.sleep(interval)
utilization_stats["max_util"] = max_utilization * 100 # Convert to percentage
utilization_stats["mean_util"] = (total_utilization / count) * 100 if count > 0 else 0
def prefill_with_prepacking(sentences, model, tokenizer, device, processor):
new_tokens, new_positions, new_mask, restart_dict, original_ids = processor.batch_process(sentences)
with torch.no_grad():
output = model(
input_ids=new_tokens,
attention_mask=new_mask,
position_ids=new_positions,
return_dict=True,
)
return output
def TTFT_with_prepacking(sentences, model, tokenizer, device, processor):
new_tokens, new_positions, new_mask, restart_dict, original_ids = processor.batch_process(sentences)
with torch.no_grad():
packed_outputs = model(
input_ids=new_tokens,
attention_mask=new_mask,
position_ids=new_positions,
return_dict=True,
)
cache, final_tokens, attention_mask = unpack_kv(
packed_outputs["past_key_values"], restart_dict, original_ids, device
)
_ = model.generate(
input_ids=final_tokens,
attention_mask=attention_mask,
max_new_tokens=1,
use_cache=True,
do_sample=False,
past_key_values=cache,
)
return
def prefill_with_baseline(sentences, model, tokenizer, device, processor=None):
batch_sentences = tokenizer(sentences, return_tensors="pt", padding=True, truncation=True)
with torch.no_grad():
batch_sentences_outputs = model(
batch_sentences["input_ids"].to(device),
attention_mask=batch_sentences["attention_mask"].to(device),
return_dict=True,
)
return batch_sentences_outputs
def TTFT_with_baseline(sentences, model, tokenizer, device, processor=None):
batch_sentences = tokenizer(sentences, return_tensors="pt", padding=True, truncation=True).to(device)
with torch.no_grad():
_ = model.generate(
**batch_sentences,
max_new_tokens=1,
use_cache=True,
do_sample=False,
)
return
def get_average_gpu_utilization():
# get current device id, assuming only 1 GPU is used
cuda_visible_devices = os.getenv("CUDA_VISIBLE_DEVICES", None)
visible_device_ids = list(map(int, cuda_visible_devices.split(",")))
gpus = GPUtil.getGPUs()
return gpus[visible_device_ids[0]].load
def measure_inference_resources(
method,
texts,
batch_size,
num_runs,
total_batches,
model,
tokenizer,
model_device,
metric="TTFT",
binpack_algo="greedy",
):
scenario_times = []
if metric == "TTFT":
method_functions = {
"prepacking": TTFT_with_prepacking,
"full-batching": TTFT_with_baseline,
"length-ordered": TTFT_with_baseline,
}
elif metric == "prefill":
method_functions = {
"prepacking": prefill_with_prepacking,
"full-batching": prefill_with_baseline,
"length-ordered": prefill_with_baseline,
}
method_function = method_functions.get(method)
packing_fn = None if binpack_algo == "greedy" else integer_program_packing
optimized_processor = PrePackProcessor(tokenizer, packing_fn=packing_fn)
method_function = method_functions.get(method)
for _ in range(num_runs):
batches_generator = (
sample_batches(texts, batch_size)
if method != "length-ordered"
else sample_batches_by_length(texts, batch_size)
)
max_gpu_utilization = []
max_gpu_memory = []
batch_gpu_memories = []
batch_gpu_utilizations = []
mean_gpu_utilizations = []
for batch in tqdm(batches_generator, total=total_batches, desc=method):
utilization_stats = {}
stop_event = threading.Event()
monitor_thread = threading.Thread(
target=monitor_gpu_utilization, args=(stop_event, utilization_stats), daemon=True
)
monitor_thread.start()
torch.cuda.reset_peak_memory_stats(model_device) # Reset memory stats at the start
torch.cuda.empty_cache()
start_time = time.time()
_ = method_function(batch, model, tokenizer, model_device, optimized_processor)
elapsed = time.time() - start_time
scenario_times.append(elapsed)
stop_event.set()
monitor_thread.join()
peak_memory = torch.cuda.max_memory_allocated(model_device) / (1024**2)
batch_gpu_memories.append(peak_memory)
max_util = utilization_stats.get("max_util", 0)
mean_util = utilization_stats.get("mean_util", 0) # Get mean utilization
batch_gpu_utilizations.append(max_util)
mean_gpu_utilizations.append(mean_util)
max_gpu_memory.append(max(batch_gpu_memories))
max_gpu_utilization.append(max(batch_gpu_utilizations))
avg_scenario_time = np.mean(scenario_times)
avg_gpu_utilization = np.mean(max_gpu_utilization)
avg_gpu_memory = np.mean(max_gpu_memory)
avg_mean_gpu_utilization = np.mean(mean_gpu_utilizations)
std_dev_time = np.std(scenario_times)
std_gpu_utilization = np.std(max_gpu_utilization)
std_gpu_memory = np.std(max_gpu_memory) # = 0
std_mean_gpu_utilization = np.std(mean_gpu_utilizations)
return (
avg_scenario_time,
avg_gpu_utilization,
avg_gpu_memory,
avg_mean_gpu_utilization,
std_dev_time,
std_gpu_utilization,
std_mean_gpu_utilization,
)
def main(
methods: List[str] = [
"prepacking",
"full-batching",
"length-ordered",
],
metric: str = "prefill",
dataset: str = "mmlu",
model_name: str = "llama1b",
loadbit: int = 4,
num_runs: int = 5,
batch_size: int = 64,
binpack_algo: str = "greedy",
):
torch.set_num_threads(5)
seed = 42
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
if binpack_algo != "greedy":
binpack_algo = "ip"
# Load the model and tokenizer
model, tokenizer = load_model_and_tokenizer(base_model=model_name, loadbit=loadbit)
# Load and prepare the dataset
texts = load_and_evaluate_dataset(dataset, tokenizer)
total_batches = len(texts) // batch_size
if len(texts) % batch_size != 0:
total_batches += 1
table = PrettyTable()
table.field_names = [
"Method",
f"Avg {metric} Time /batch (s)",
"Max GPU Utilization (%)",
"Max GPU Memory (MB)",
"Mean GPU Utilization (%)",
"Std Dev Time (s)",
"Std Dev Max GPU Util (%)",
"Std Dev Mean GPU Util (%)",
]
for method in methods:
try:
(
avg_scenario_time,
avg_gpu_utilization,
avg_gpu_memory,
avg_mean_gpu_utilization,
std_dev_time,
std_gpu_util,
std_mean_gpu_util,
) = measure_inference_resources(
method,
texts,
batch_size,
num_runs,
total_batches,
model,
tokenizer,
model.device,
metric=metric,
binpack_algo=binpack_algo,
)
table.add_row(
[
method,
f"{avg_scenario_time:.3f}",
f"{avg_gpu_utilization:.3f}",
f"{avg_gpu_memory:.3f}",
f"{avg_mean_gpu_utilization:.3f}",
f"{std_dev_time:.3f}",
f"{std_gpu_util:.3f}",
f"{std_mean_gpu_util:.3f}",
]
)
print(table)
except Exception as e: # OOM error
print(f"An error occurred while processing method {method}: {e}")
torch.cuda.empty_cache()
finally:
torch.cuda.empty_cache()
if __name__ == "__main__":
fire.Fire(main)