File size: 10,599 Bytes
9382e3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
310
311
312
import json
import math
import os
import time
from argparse import ArgumentParser
from collections import defaultdict

import matplotlib.pyplot as plt
import numpy as np
import torch

from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer


os.environ["TOKENIZERS_PARALLELISM"] = "0"
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
# os.environ["HF_HOME"] = "/scratch/ow5/huggingface_cache"


class TorchTracemalloc:
    track_memory_consumption = []

    def __enter__(self):
        self.begin = torch.cuda.memory_allocated()
        torch.cuda.reset_max_memory_allocated()
        return self

    def __exit__(self, *exc):
        peak = torch.cuda.max_memory_allocated()
        peaked = (peak - self.begin) // 1024**2
        TorchTracemalloc.track_memory_consumption.append(peaked)


def save_bar_chart(title, x, y, ylabel, xlabel, output_path):
    try:
        plt.style.use("ggplot")
        width = 0.4
        xs = np.arange(len(x))
        plt.figure(figsize=(10, 6))
        plt.bar(xs, height=y, width=width, color="skyblue")
        plt.title(title)
        plt.xticks(xs, x)
        plt.xlabel(xlabel)
        plt.ylabel(ylabel)
        plt.savefig(output_path)
    except Exception as e:
        print(f"Error saving chart {title}: {str(e)}")
    finally:
        plt.close()


def format_response(dialog, response):
    formatted_dialog = dialog.copy()
    formatted_dialog.append({"role": "assistant", "content": response})
    return formatted_dialog


parser = ArgumentParser("chat_with_llama")
parser.add_argument(
    "--llama", type=str, default="3-instruct", choices=["2", "3-instruct"]
)
# parser.add_argument("--prompts_path", type=str, default="chats_sys_none.json")
parser.add_argument("--prompts_path", type=str, default="chats.json")
parser.add_argument("--model_size", type=int, default=8, choices=[7, 8, 13])
parser.add_argument("--num_new_tokens", type=int, default=512)
parser.add_argument(
    "--temperature", type=float, default=0.4, help="Temperature for sampling"
)
parser.add_argument("--window_length", type=int, default=32)
parser.add_argument("--kv_bits", type=int, default=1)
parser.add_argument("--output_path", type=str, default="./output")
parser.add_argument(
    "--dtype", type=str, default="fp16", choices=["fp16", "fp32", "bf16"]
)
args = parser.parse_args()
bits = args.kv_bits

try:
    if args.llama == 2:
        model_name = "NousResearch/Llama-2-7b-hf"
    else:
        model_name = "NousResearch/Meta-Llama-3-8B-Instruct"
    tokenizer = AutoTokenizer.from_pretrained(model_name)

    special_tokens = {"pad_token": "<PAD>"}
    tokenizer.add_special_tokens(special_tokens)

    config = AutoConfig.from_pretrained(model_name)

    if isinstance(bits, int):
        if args.llama == 2:
            setattr(
                config,
                "quantizer_path",
                f"codebooks/llama-2-7b_{bits}bit.xmad",
            )
            print(f"Using {bits}-bit quantization for Llama-2-7b-base")
        else:
            setattr(
                config,
                "quantizer_path",
                f"codebooks/llama-3-8b-instruct_{bits}bit.xmad",
            )
            print(f"Using {bits}-bit quantization for Llama-3-8b-Instruct")
    if isinstance(args.window_length, int):
        setattr(config, "window_length", args.window_length)

    if args.dtype == "bf16":
        dtype = torch.bfloat16
    elif args.dtype == "fp16":
        dtype = torch.float16
    elif args.dtype == "fp32":
        dtype = torch.float32

    # ! When OOM with cuda:0 at batch_size=120, "auto" does NOT help with offloading memory
    model = AutoModelForCausalLM.from_pretrained(
        model_name, config=config, torch_dtype=dtype, device_map="cuda:0"
    )

    if len(tokenizer) > model.get_input_embeddings().weight.shape[0]:
        print(
            "WARNING: Resizing the embedding matrix to match the tokenizer vocab size."
        )
        model.resize_token_embeddings(len(tokenizer))

    tokenizer.padding_side = "left"
    model.config.pad_token_id = tokenizer.pad_token_id

    with open(args.prompts_path, "r") as file:
        dialogs = json.load(file)

    num_dialogs = len(dialogs)
    print(f"Loaded {num_dialogs} dialogues...")

    batch_inputs = [
        tokenizer.apply_chat_template(
            dialog, tokenize=False, add_generation_prompt=True
        )
        for dialog in dialogs
    ]

    terminators = [
        tokenizer.eos_token_id,
        tokenizer.convert_tokens_to_ids("<|eot_id|>"),
    ]

    batch_sizes = [60]
    

    memory_avg = []
    tokens_per_sec_avg = []
    time_to_first_token_avg = []
    responses_by_batch_size = defaultdict(list)

    # !CHECK: Total generation time summed across all batches
    total_generation_time = 0

    os.makedirs(args.output_path, exist_ok=True)

    for batch_size in batch_sizes:
        print(f"\nProcessing with batch size: {batch_size}")

        actual_batch_size = min(batch_size, num_dialogs)
        total_time = 0
        total_tokens = 0
        total_ttft = 0
        num_batches = math.ceil(num_dialogs / actual_batch_size)

        # ! CHECK: Gen time for each batch
        batch_generation_time = 0

        with TorchTracemalloc() as tt:
            for i in range(0, num_dialogs, actual_batch_size):
                batch = batch_inputs[i : i + actual_batch_size]

                try:
                    encoded_inputs = tokenizer(
                        batch,
                        padding=True,
                        truncation=False,
                        return_tensors="pt",
                    )

                    input_ids = encoded_inputs["input_ids"].to(model.device)
                    attention_mask = encoded_inputs["attention_mask"].to(
                        model.device
                    )

                    torch.cuda.synchronize()
                    start_time = time.perf_counter()

                    with torch.no_grad():
                        output_tokens = model.generate(
                            input_ids,
                            attention_mask=attention_mask,
                            max_new_tokens=args.num_new_tokens,
                            num_return_sequences=1,
                            do_sample=True,
                            temperature=args.temperature,
                            pad_token_id=tokenizer.pad_token_id,
                            eos_token_id=terminators,
                        )

                    torch.cuda.synchronize()
                    end_time = time.perf_counter()

                    batch_time = end_time - start_time
                    total_time += batch_time
                    batch_generation_time += (
                        batch_time  # Add to batch generation time
                    )
                    total_generation_time += (
                        batch_time  # Add to total generation time
                    )
                    total_tokens += output_tokens.numel()

                    if i == 0:
                        total_ttft = batch_time

                    # Decode the generated responses
                    decoded_outputs = tokenizer.batch_decode(
                        output_tokens, skip_special_tokens=True
                    )

                    # Store the responses
                    for j, response in enumerate(decoded_outputs):
                        original_dialog = dialogs[i + j]
                        formatted_response = format_response(
                            original_dialog, response
                        )
                        responses_by_batch_size[batch_size].append(
                            formatted_response
                        )

                    torch.cuda.empty_cache()

                except Exception as e:
                    print(
                        f"Error processing batch {i//batch_size + 1}: {str(e)}"
                    )
                    continue

        avg_memory = np.mean(TorchTracemalloc.track_memory_consumption)
        memory_avg.append(avg_memory)

        tokens_per_sec = total_tokens / total_time if total_time > 0 else 0
        tokens_per_sec_avg.append(tokens_per_sec)

        # Use actual_batch_size in calculations
        time_to_first_token = (
            total_ttft / actual_batch_size if actual_batch_size > 0 else 0
        )
        time_to_first_token_avg.append(time_to_first_token)

        print(f"Actual Batch Size Used: {actual_batch_size}")
        print(f"GPU Memory Consumption (MiB): {avg_memory:.2f} MiB")
        print(f"Tokens per Second: {tokens_per_sec:.2f}")
        print(f"TTFT (seconds): {time_to_first_token:.4f} seconds")
        print(
            f"Time to generate answers for this batch size: {batch_generation_time:.2f} seconds"
        )

    for batch_size, responses in responses_by_batch_size.items():
        output_file = os.path.join(
            args.output_path, f"batch_{batch_size}_responses.json"
        )
        with open(output_file, "w") as f:
            json.dump(responses, f, indent=2)

    save_bar_chart(
        title="GPU Memory Consumption as a Function of Batch Size",
        x=batch_sizes,
        y=memory_avg,
        xlabel="Batch Size",
        ylabel="GPU Memory Consumption (MiB)",
        output_path=f"{args.output_path}/memory_usage.png",
    )

    save_bar_chart(
        title="Number of Tokens per Second as a Function of Batch Size",
        x=batch_sizes,
        y=tokens_per_sec_avg,
        xlabel="Batch Size",
        ylabel="Tokens per Second",
        output_path=f"{args.output_path}/tokens_per_second.png",
    )

    save_bar_chart(
        title="Time to First Token (TTFT) as a Function of Batch Size",
        x=batch_sizes,
        y=time_to_first_token_avg,
        xlabel="Batch Size",
        ylabel="TTFT (seconds)",
        output_path=f"{args.output_path}/time_to_first_token.png",
    )

    print(
        f"\nBenchmarking Results -> Model size: {args.model_size}, Max New Tokens: {args.num_new_tokens}, KV bits: {bits}"
    )
    print(f"Batch Sizes: {batch_sizes}")
    print(f"GPU Memory Consumption (MiB): {memory_avg}")
    print(f"Tokens per Second: {tokens_per_sec_avg}")
    print(f"Time to First Token (seconds): {time_to_first_token_avg}")
    print(
        f"\nTotal time to generate all answers across all batches: {total_generation_time:.2f} seconds"
    )
    print(f"Results and responses saved in: {args.output_path}")

except Exception as e:
    print(f"An error occurred during script execution: {str(e)}")