forked from tenstorrent/tt-metal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_text_demo.py
761 lines (678 loc) · 32.6 KB
/
simple_text_demo.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
# SPDX-FileCopyrightText: © 2023 Tenstorrent Inc.
# SPDX-License-Identifier: Apache-2.0
from pathlib import Path
from typing import Optional
from loguru import logger
from time import time
from datetime import datetime
import hashlib
import requests
import json
from pkg_resources import resource_filename
import math
from termcolor import cprint
import torch
import pytest
import os
import ttnn
from llama_models.llama3.api.tokenizer import Tokenizer
from models.demos.llama3.tt.generator import LlamaGenerator
from models.demos.llama3.tt.model_config import LlamaOptimizations
from models.demos.llama3.tt.llama_common import (
preprocess_inputs_prefill,
get_rot_transformation_mat,
encode_prompt_llama_instruct,
PagedAttentionConfig,
sample_host,
)
from models.perf.benchmarking_utils import BenchmarkProfiler
from models.demos.utils.llm_demo_utils import create_benchmark_data
def load_and_cache_context(context_url, cache_dir, max_length=None):
cache_file = cache_dir / hashlib.md5(context_url.encode()).hexdigest()
if cache_file.exists():
with open(cache_file, "r") as f:
context_text = f.read()
logger.info(f"Loaded context from cache: {context_url}")
else:
try:
response = requests.get(context_url)
if response.status_code == 200:
context_text = response.text
with open(cache_file, "w") as f:
f.write(context_text)
logger.info(f"Downloaded and cached context: {context_url}")
else:
logger.warning(f"Failed to fetch context from URL: {context_url}. Status code: {response.status_code}")
context_text = ""
except Exception as e:
logger.error(f"Error fetching context from URL: {context_url}. Error: {str(e)}")
context_text = ""
# Clip the context to the max length provided
if max_length:
context_text = context_text[:max_length]
logger.info(f"Clipped the context text to {max_length} characters")
return context_text
# load input prompts from json, return as a list
def load_inputs(user_input, batch, instruct):
if isinstance(user_input, str):
with open(user_input, "r") as f:
user_input = json.load(f)
if len(user_input) < batch:
logger.warning(
f"Number of users in the file is less than the provided batch={batch}. Repeating the prompts to match the batch size."
)
user_input = user_input * batch
in_prompt = []
cache_dir = Path("models/demos/llama3/demo/context_cache")
cache_dir.mkdir(parents=True, exist_ok=True)
# The demo supports a custom prompt file, where the context is provided by a link to a book from the gutenberg project
# It clips the excerpt to the max length provided to allow testing different long context lengthts
for i in range(batch):
prompt = user_input[i]["prompt"]
if "context" in user_input[i]:
if "max_length" in user_input[i]: # Clip the context to the max length provided
context_text = load_and_cache_context(
user_input[i]["context"], cache_dir, max_length=user_input[i]["max_length"]
)
else:
context_text = load_and_cache_context(user_input[i]["context"], cache_dir)
if instruct:
prompt = (
"```" + context_text + "```\n\n" + prompt
) # Add the markdown block to the context to comply with the prompt
else:
prompt = context_text
in_prompt.append(prompt)
return in_prompt
def create_tt_model(
mesh_device,
instruct,
max_batch_size,
optimizations,
max_seq_len,
page_params,
dtype=ttnn.bfloat8_b,
use_paged_kv_cache=False,
):
from models.demos.llama3.tt.llama_model import TtTransformer
from models.demos.llama3.tt.model_config import TtModelArgs
tt_model_args = TtModelArgs(
mesh_device,
instruct=instruct,
max_batch_size=max_batch_size,
optimizations=optimizations,
max_seq_len=max_seq_len,
)
state_dict = tt_model_args.load_state_dict()
page_table = None
paged_attention_config = None
tt_kv_cache = None
if use_paged_kv_cache:
paged_attention_config = PagedAttentionConfig(
block_size=page_params["page_block_size"],
max_num_blocks=page_params["page_max_num_blocks"],
)
# Implied shuffling of blocks
permutation = torch.randperm(paged_attention_config.max_num_blocks)
# Page table which maps virtual blocks to physical
reverse_permutation = torch.argsort(permutation)
page_table = reverse_permutation.reshape(
tt_model_args.max_batch_size, paged_attention_config.max_num_blocks // tt_model_args.max_batch_size
)
paged_attention_config = PagedAttentionConfig(
block_size=page_params["page_block_size"],
max_num_blocks=page_params["page_max_num_blocks"],
)
model = TtTransformer(
args=tt_model_args,
mesh_device=mesh_device,
dtype=dtype,
state_dict=state_dict,
weight_cache_path=tt_model_args.weight_cache_path(dtype),
paged_attention_config=paged_attention_config,
)
if use_paged_kv_cache:
tt_kv_cache = [l.attention.layer_past for l in model.layers]
return tt_model_args, model, page_table, tt_kv_cache
# List of supported Parameters for demo.py
#
# input_prompts (string): input json file with prompts to process. See models/demos/llama3/demo/*.json for list of input files
# instruct (bool): Whether to use instruct weights or general weights
# repeat_batches (int): Number of consecutive batches of users to run (default: 1)
# max_seq_len (int): Maximum context length supported by the model (Llama3.1 and Llama3.2 models have a maximum context length of 128k, i.e., 128 * 1024)
# batch_size (int): Number of users in a batch (Supports 1/2/4/8/16/32 batches)
# max_generated_tokens (int): Maximum number of tokens to generate for each user (Note that the users will stop generation before this limit if they reach a EoS token)
# paged_attention (bool): Whether to use paged attention or default attention (vLLM requires paged attention)
# page_params (dict): Page parameters for paged attention (block_size, max_num_blocks) For smaller context lengths use block_size=32 and max_num_blocks=1024, for larger context use block_size=64 and max_num_blocks=2048
# sampling_params (dict): Sampling parameters for decoding (temperature, top_p). If temperature is set to 0, argmax (greedy decode) is used.
# stop_at_eos (bool): Whether to stop decoding when the model generates an EoS token
#
# optimization (LlamaOptimizations): Optimization level to use for the model (performance or accuracy)
# FAKE_DEVICE (str): Fake device to use for testing (N150, N300, T3K, TG). Usage: `export FAKE_DEVICE=N150`, will enable running a single-chip demo on a multi-chip system.
@pytest.mark.parametrize(
"input_prompts, instruct, repeat_batches, max_seq_len, batch_size, max_generated_tokens, paged_attention, page_params, sampling_params, stop_at_eos, ci_only",
[
( # Batch-1 run (Latency) - single user, small prompt
"models/demos/llama3/demo/sample_prompts/input_data_questions_prefill_128.json", # input_prompts
True, # instruct mode
1, # repeat_batches
1024, # max_seq_len
1, # batch_size
200, # max_generated_tokens
True, # paged_attention
{"page_block_size": 32, "page_max_num_blocks": 1024}, # page_params
{"temperature": 0, "top_p": 0.08}, # sampling_params (argmax)
True, # stop_at_eos
False, # ci_only
),
( # Batch-32 run (Throughput) - 32 users, small prompt
"models/demos/llama3/demo/sample_prompts/input_data_questions_prefill_128.json", # input_prompts
True, # instruct mode
1, # repeat_batches
1024, # max_seq_len
32, # batch_size
200, # max_generated_tokens
True, # paged_attention
{"page_block_size": 32, "page_max_num_blocks": 1024}, # page_params
{"temperature": 0, "top_p": 0.08}, # sampling_params (argmax)
True, # stop_at_eos
False, # ci_only
),
( # Long-context run - Single user, long prompt (adapted to the model being used and architecture)
"models/demos/llama3/demo/sample_prompts/input_data_long_64k.json", # input_prompts
True, # instruct mode
1, # repeat_batches
128 * 1024, # max_seq_len
1, # batch_size
200, # max_generated_tokens
True, # paged_attention
{"page_block_size": 32, "page_max_num_blocks": 2048}, # page_params
{"temperature": 0, "top_p": 0.08}, # sampling_params (argmax)
True, # stop_at_eos
False, # ci_only
),
( # Batch-1 run (Reasoning) - single user, small prompt, long thinking time
"models/demos/llama3/demo/input_data_questions_reasoning.json", # input_prompts
True, # instruct mode
1, # repeat_batches
16 * 1024, # max_seq_len
1, # batch_size
15000, # max_generated_tokens
True, # paged_attention
{"page_block_size": 32, "page_max_num_blocks": 1024}, # page_params # TODO This will be serviced by vLLM
{"temperature": 0, "top_p": 0.08}, # sampling_params (argmax)
False, # stop_at_eos
False, # ci_only
),
( # CI Batch-1 run - Measures the performance of a single user over 4096 iterations
"models/demos/llama3/demo/sample_prompts/input_data_questions_prefill_128.json", # input_prompts
True, # instruct mode
1, # repeat_batches
8192, # max_seq_len
1, # batch_size
4096, # max_generated_tokens
True, # paged_attention
{"page_block_size": 32, "page_max_num_blocks": 1024}, # page_params
{"temperature": 0, "top_p": 0.08}, # sampling_params (argmax)
False, # stop_at_eos
True, # ci_only
),
( # CI Batch-32 run - Measures the performance of a 32 users over 4096 iterations
"models/demos/llama3/demo/sample_prompts/input_data_questions_prefill_128.json", # input_prompts
True, # instruct mode
1, # repeat_batches
2000, # max_seq_len
32, # batch_size
1024, # max_generated_tokens # TODO Update this to 4096, and make sure it fits in DRAM with correct page_params
True, # paged_attention # TODO Find the correct paged_attn params to avoid hangs in this config with long context generation
{"page_block_size": 64, "page_max_num_blocks": 1024}, # page_params
{"temperature": 0, "top_p": 0.08}, # sampling_params (argmax)
False, # stop_at_eos
True, # ci_only
),
],
ids=[
"batch-1", # latency
"batch-32", # throughput
"long-context", # max-length
"reasoning-1", # reasoning
"ci-1", # CI batch 1
"ci-32", # CI batch 32
],
)
@pytest.mark.parametrize(
"optimizations",
[
LlamaOptimizations.performance,
LlamaOptimizations.accuracy,
],
)
@pytest.mark.parametrize("device_params", [{"trace_region_size": 23887872, "num_command_queues": 2}], indirect=True)
@pytest.mark.parametrize(
"mesh_device",
[
{"N150": (1, 1), "N300": (1, 2), "T3K": (1, 8), "TG": (8, 4)}.get(
os.environ.get("FAKE_DEVICE"), len(ttnn.get_device_ids())
)
],
indirect=True,
)
def test_llama_demo_text(
input_prompts,
instruct,
repeat_batches,
max_seq_len,
batch_size,
max_generated_tokens,
paged_attention,
page_params,
sampling_params,
optimizations,
stop_at_eos,
mesh_device,
use_program_cache,
is_ci_env,
ci_only,
reset_seeds,
request,
):
"""
Simple Llama demo with limited dependence on reference code.
"""
if is_ci_env and (optimizations == LlamaOptimizations.accuracy or not ci_only):
pytest.skip("CI only runs the CI-only tests")
# TODO: Remove this once all batch sizes are supported on TG
if os.environ.get("FAKE_DEVICE") == "TG" and batch_size not in [1, 32]:
pytest.skip("TG only supports batch 1 and 32")
mesh_device.enable_async(True)
enable_trace = True # Use tracing for better perf
print_to_file = False # Enable this flag to print the output of all users to a file
# Override parameters from command line if they are provided
input_prompts = request.config.getoption("--input_prompts") or input_prompts
if request.config.getoption("--instruct") in [
0,
1,
]: # If the flag is provided, use it. Take an int instead of bool due to parser limitations
instruct = request.config.getoption("--instruct")
repeat_batches = request.config.getoption("--repeat_batches") or repeat_batches
max_seq_len = request.config.getoption("--max_seq_len") or max_seq_len
batch_size = request.config.getoption("--batch_size") or batch_size
max_generated_tokens = request.config.getoption("--max_generated_tokens") or max_generated_tokens
paged_attention = request.config.getoption("--paged_attention") or paged_attention
page_params = request.config.getoption("--page_params") or page_params
sampling_params = request.config.getoption("--sampling_params") or sampling_params
if request.config.getoption("--stop_at_eos") in [
0,
1,
]: # If the flag is provided, use it. Take an int instead of bool due to parser limitations
stop_at_eos = request.config.getoption("--stop_at_eos")
if not stop_at_eos:
logger.info(f"The decode generation will only stop at the max_generated_tokens limit == {max_generated_tokens}")
if print_to_file:
# Creat batch output file
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
output_directory = "models/demos/llama3/demo/output"
os.makedirs(output_directory, exist_ok=True)
os.chmod(output_directory, 0o755)
output_filename = f"{output_directory}/llama_text_demo_output_{timestamp}.txt"
# Start profiler
logger.info(f"Start profiler")
profiler = BenchmarkProfiler()
profiler.start("run")
logger.info(f"Reading inputs...")
profiler.start("loading_inputs")
if len(input_prompts) == 1: # Manual input
input_prompts = input_prompts * batch_size
else: # Inputs from file
input_prompts = load_inputs(input_prompts, batch_size, input_prompts)
profiler.end("loading_inputs")
# To simulate a deployment environment, the demo supports repeating batched prompts.
# This loop will rotate the prompts between the users for each batch, to simulate users sending different requests
# If batch_size=1, the same prompt is repeated for each batch
repeat_batch_prompts = []
for i in range(repeat_batches):
repeat_batch_prompts.append([input_prompts[(j + i) % len(input_prompts)] for j in range(len(input_prompts))])
model_args, model, page_table, tt_kv_cache = create_tt_model(
mesh_device,
instruct=instruct,
max_batch_size=batch_size,
optimizations=optimizations,
max_seq_len=max_seq_len,
page_params=page_params,
dtype=ttnn.bfloat8_b,
use_paged_kv_cache=paged_attention,
)
tokenizer = model_args.tokenizer
generator = LlamaGenerator(model, model_args, mesh_device, tokenizer=tokenizer)
num_tokens_generated_decode = []
logger.info("Starting inference...")
for batch_idx, input_prompts in enumerate(repeat_batch_prompts):
logger.info(f"Processing batch {batch_idx}")
profiler.start(f"preprocess_prefill_inputs", iteration=batch_idx)
# Preprocess initial prompt inputs
(
input_tokens_prefill_pt,
encoded_prompts,
decoding_pos,
prefill_lens,
) = preprocess_inputs_prefill(
input_prompts,
tokenizer,
model_args,
instruct,
max_generated_tokens,
)
max_encoded_prompt_len = max(len(p) for p in encoded_prompts)
assert (
max_generated_tokens + max_encoded_prompt_len <= max_seq_len
), f"Prompt prefill tokens ({max_encoded_prompt_len}) + maximum number of decoded iterations ({max_generated_tokens}) needs to be <= than max_seq_len ({max_seq_len})"
profiler.end(f"preprocess_prefill_inputs", iteration=batch_idx)
# when doing repeating batches, set kv-caches to zero, to avoid context leaking
if batch_idx != 0:
for layer in model.layers:
k_cache, v_cache = layer.attention.layer_past
k_cache = ttnn.mul(k_cache, 0, output_tensor=k_cache)
v_cache = ttnn.mul(v_cache, 0, output_tensor=v_cache)
input_tokens_prefill_pt = torch.stack(input_tokens_prefill_pt).view(batch_size, -1)
logger.info("Starting prefill warmup...")
profiler.start(f"compile_prefill", iteration=batch_idx)
logits = generator.prefill_forward_text(
input_tokens_prefill_pt[0].unsqueeze(0), # Just warmup prefill for 1 user
page_table=page_table,
kv_cache=tt_kv_cache,
prompt_lens=decoding_pos,
)
profiler.end(f"compile_prefill", iteration=batch_idx)
logger.info("Finished prefill warmup")
logger.info(f"Starting prefill...")
profiler.start(f"inference_prefill", iteration=batch_idx)
logits = generator.prefill_forward_text(
input_tokens_prefill_pt,
page_table=page_table,
kv_cache=tt_kv_cache,
prompt_lens=decoding_pos,
)
prefilled_token = torch.argmax(logits, dim=-1)
profiler.end(f"inference_prefill", iteration=batch_idx)
logger.info(f"Prefill finished")
# Keep track of generated outputs to print out every iteration
all_outputs = [encoded_prompts[b][: prefill_lens[b]] for b in range(batch_size)]
for user in range(batch_size):
user_tok = int(prefilled_token[user].item())
all_outputs[user].append(user_tok)
user_done = [False] * batch_size # Keeps track when a user reaches EoD token
# TODO Argmax on device is only supported for batch_size=1
argmax_on_device = False if (batch_size > 1 or sampling_params["temperature"] != 0) else True
# Initial positions
current_pos = torch.tensor([decoding_pos[b] for b in range(batch_size)])
# Start decoding
iteration = 0
users_decoding = True
out_tok = prefilled_token
logger.info(f"Starting decode loop...")
# Log total inference (accounting for compile_decode as well)
profiler.start(f"inference_decode", iteration=batch_idx)
while users_decoding:
if iteration == 0: # First iteration also accounts for compile time
profiler.start(f"compile_decode", iteration=batch_idx)
else:
profiler.start(f"inference_decode_time_{iteration}", iteration=batch_idx)
# Run decode forward
logits = generator.decode_forward_text(
out_tok,
current_pos,
enable_trace=enable_trace,
page_table=page_table,
kv_cache=tt_kv_cache,
argmax_on_device=argmax_on_device,
)
# Get the next token
if argmax_on_device:
out_tok = logits.unsqueeze(1)
else:
# TODO Fix use case with temperature > 0
_, out_tok = sample_host(
logits,
None,
temperature=sampling_params["temperature"],
top_p=sampling_params["top_p"],
on_host=True,
)
if iteration == 0: # First iteration will account the compile time
profiler.end(f"compile_decode", iteration=batch_idx)
decode_iteration_time = profiler.get_duration("compile_decode", iteration=batch_idx)
else:
profiler.end(f"inference_decode_time_{iteration}", iteration=batch_idx)
decode_iteration_time = profiler.get_duration(f"inference_decode_time_{iteration}", iteration=batch_idx)
# Always print perf after every iteration
tokens_per_second_per_user = 1 / decode_iteration_time
logger.info(
f"Iteration {iteration}: {1000*decode_iteration_time:.0f}ms @ {tokens_per_second_per_user:.1f} tok/s/user ({batch_size*tokens_per_second_per_user:.1f} tok/s throughput)"
)
current_pos += 1
# Save output token to print out later
for user in range(batch_size):
user_tok = out_tok[user].item()
if (
user_tok not in tokenizer.stop_tokens and user_done[user] == False
): # Read until an eos token (e.g. <|eot_id|>); create_tokenizer adds stop_tokens to HF tokenizers
all_outputs[user].append(user_tok)
else:
if (
stop_at_eos
): # For performance gathering in CI, we want to sometimes force decoding for a fixed number of iterations
user_done[user] = True
logger.trace(f"[User {user}] Finished decoding at iteration {iteration}")
if all(user_done):
users_decoding = False
# Print out generated outputs for each user at the end of every iteration
if not is_ci_env:
for user in range(batch_size):
text = "".join(tokenizer.decode(all_outputs[user]))
if len(text) > 100:
text = "..." + text[-97:]
text = text.replace("\n", " ")
logger.info("[User {}] {}".format(user, text))
iteration += 1
# Upper limit of generated tokens for each user
if iteration >= max_generated_tokens:
users_decoding = False
# Final print
if not users_decoding:
profiler.start(f"log_saving_file", iteration=batch_idx)
logger.info("Finished decoding, printing the final outputs...\n")
for i, (output, prompt) in enumerate(zip(all_outputs, input_prompts)):
text = tokenizer.decode(output)
prompt_including_assistant_tags = tokenizer.decode(
model_args.encode_prompt(prompt, instruct=instruct)
)
text_after_prompt = text.replace(prompt_including_assistant_tags, "", 1)
if print_to_file:
with open(output_filename, "a") as f:
f.write(
f"\nbatch: {batch_idx} user: {i}\nprompt: {prompt} \noutput:\n{text_after_prompt}\n"
)
else:
# Strip leading newlines from output when sent to terminal
short_prompt = (
(prompt[:100] + "\n<long prompt not printed in full>\n" + prompt[-100:])
if len(prompt) > 200
else prompt
)
logger.info(
f"\n==REPEAT BATCH {batch_idx}\n==USER {i} - PROMPT\n{short_prompt} \n==USER {i} - OUTPUT\n{text_after_prompt.strip()}\n"
)
profiler.end(f"log_saving_file", iteration=batch_idx)
num_tokens_generated_decode.append(iteration) # Save the number of tokens generated for each repeat batch
profiler.end(f"inference_decode", iteration=batch_idx)
# Finish profiling at the end of inference for all repeated batches
profiler.end("run")
# Prepare profile benchmark metrics for the first repeat batch only
compile_prefill_time = profiler.get_duration("compile_prefill")
compile_decode_time = profiler.get_duration("compile_decode")
total_inference_prefill_time = profiler.get_duration("inference_prefill")
total_inference_decode_time = 0
for i in range(1, iteration): # Iteration 0 is the compile time
total_inference_decode_time += profiler.get_duration(f"inference_decode_time_{i}")
# Average prefill time for each user
avg_time_to_first_token = total_inference_prefill_time / batch_size
# Average decode time per batch iteration
avg_decode_iteration_time = total_inference_decode_time / (iteration - 1)
prefill_tok_s = prefill_lens[0] / total_inference_prefill_time * batch_size
decode_tok_s_user = (num_tokens_generated_decode[0] - 1) / total_inference_decode_time # Remove the compile time
decode_tok_s = (
(num_tokens_generated_decode[0] - 1) / total_inference_decode_time * batch_size
) # Remove the compile time
measurements = {
# Required measurements
"compile_prefill": compile_prefill_time,
"compile_decode": compile_decode_time,
"inference_prefill": total_inference_prefill_time,
"inference_decode": total_inference_decode_time,
"prefill_time_to_token": avg_time_to_first_token,
"prefill_t/s": prefill_tok_s, # tokens/s
"decode_t/s/u": decode_tok_s_user, # tokens/s/u
"decode_t/s": decode_tok_s, # tokens/s
# Optional measurements
"Total compile time": compile_prefill_time + compile_decode_time,
"Full demo runtime": profiler.get_duration("run"),
}
# Decode performance for some specific tokens
tok_1_perf = profiler.get_duration(f"inference_decode_time_{1}") # Iteration 0 is compile time
tok_128_perf = profiler.get_duration(f"inference_decode_time_{127}") if 127 < iteration else 0
tok_1024_perf = profiler.get_duration(f"inference_decode_time_{1023}") if 1023 < iteration else 0
tok_4096_perf = profiler.get_duration(f"inference_decode_time_{4095}") if 4095 < iteration else 0
if not stop_at_eos:
logger.info(f"Please note that 'stop_at_eos' is disabled. Output repetition is expected.")
logger.info("")
logger.info(f"=== Performance metrics ===")
logger.info(
f"1st token decode time: {tok_1_perf*1000:.2f}ms [{round(1/tok_1_perf, 2)} t/s/u, {round((1/tok_1_perf)*batch_size, 2)} t/s]"
)
if tok_128_perf > 0:
logger.info(
f"128th token decode time: {tok_128_perf*1000:.2f}ms [{round(1/tok_128_perf, 2)} t/s/u, {round((1/tok_128_perf)*batch_size, 2)} t/s]"
)
if tok_1024_perf > 0:
logger.info(
f"1024th token decode time: {tok_1024_perf*1000:.2f}ms [{round(1/tok_1024_perf, 2)} t/s/u, {round((1/tok_1024_perf)*batch_size, 2)} t/s]"
)
if tok_4096_perf > 0:
logger.info(
f"4096th token decode time: {tok_4096_perf*1000:.2f}ms [{round(1/tok_4096_perf, 2)} t/s/u, {round((1/tok_4096_perf)*batch_size, 2)} t/s]"
)
# Print some of the perf metrics
logger.info("==")
logger.info(f"Prefill compile time: {round(compile_prefill_time, 2)}s")
logger.info(f"Decode compile time: {round(compile_decode_time, 2)}s")
logger.info("")
logger.info(f"Average Time to First Token (TTFT): {round(avg_time_to_first_token*1000, 2)}ms")
logger.info(
f"Average speed: {round(avg_decode_iteration_time * 1000, 2)}ms @ {round(decode_tok_s_user, 2)} tok/s/user ({round(decode_tok_s, 2)} tok/s throughput)"
)
# Benchmark targets
supported_models = ["Llama3.2-1B", "Llama3.2-3B", "Llama3.1-8B", "Llama3.2-11B", "Llama3.1-70B"]
supported_devices = ["N150", "N300", "T3K", "TG"]
tt_device_name = model_args.device_name
if model_args.base_model_name in supported_models:
assert tt_device_name in supported_devices, f"Device {tt_device_name} not supported"
# Set the target times to first token for every combination of device and model
target_prefill_tok_s = {
"N150_Llama3.2-1B": 1050, # TODO Update target
"N300_Llama3.2-1B": 1050, # TODO Update target
"T3K_Llama3.2-1B": 1050, # TODO Update target
"TG_Llama3.2-1B": 1050, # TODO Update target
#
"N150_Llama3.2-3B": 1050, # TODO Update target
"N300_Llama3.2-3B": 1050, # TODO Update target
"T3K_Llama3.2-3B": 1050, # TODO Update target
"TG_Llama3.2-3B": 1050, # TODO Update target
#
"N150_Llama3.1-8B": 1050,
"N300_Llama3.1-8B": 1050,
"T3K_Llama3.1-8B": 1050,
"TG_Llama3.1-8B": 1050,
#
"N150_Llama3.2-11B": 1050, # TODO Update target
"N300_Llama3.2-11B": 1050, # TODO Update target
"T3K_Llama3.2-11B": 1050, # TODO Update target
"TG_Llama3.2-11B": 1050, # TODO Update target
#
"N150_Llama3.1-70B": 1050, # TODO Update target
"N300_Llama3.1-70B": 1050, # TODO Update target
"T3K_Llama3.1-70B": 1050, # TODO Update target
"TG_Llama3.1-70B": 1050, # TODO Update target
}[f"{tt_device_name}_{model_args.base_model_name}"]
# Set the target decode timesfor every combination of device and model
target_decode_tok_s_u = {
"N150_Llama3.2-1B": 160, # TODO Update target
"N300_Llama3.2-1B": 250, # TODO Update target
"T3K_Llama3.2-1B": 300, # TODO Update target
"TG_Llama3.2-1B": 300, # TODO Update target
#
"N150_Llama3.2-3B": 60, # TODO Update target
"N300_Llama3.2-3B": 100, # TODO Update target
"T3K_Llama3.2-3B": 150, # TODO Update target
"TG_Llama3.2-3B": 150, # TODO Update target
#
"N150_Llama3.1-8B": 23, # TODO Update target
"N300_Llama3.1-8B": 38,
"T3K_Llama3.1-8B": 45,
"TG_Llama3.1-8B": 45, # TODO Update target
#
"N150_Llama3.2-11B": 23,
"N300_Llama3.2-11B": 38, # TODO Update target
"T3K_Llama3.2-11B": 45, # TODO Update target
"TG_Llama3.2-11B": 45, # TODO Update target
#
"T3K_Llama3.1-70B": 20, # TODO Update target
"TG_Llama3.1-70B": 20, # TODO Update target
}[f"{tt_device_name}_{model_args.base_model_name}"]
target_decode_tok_s = target_decode_tok_s_u * batch_size
targets = {
"prefill_t/s": target_prefill_tok_s,
"decode_t/s": target_decode_tok_s,
"decode_t/s/u": target_decode_tok_s_u,
}
else:
logger.warning(f"Model {model_args.base_model_name} not does not have performance targets set")
targets = {}
# Save benchmark data for CI dashboard
if is_ci_env:
# Instead of running warmup iterations, the demo profiles the initial compile iteration
bench_n_warmup_iter = {"inference_prefill": 0, "inference_decode": 1}
benchmark_data = create_benchmark_data(profiler, measurements, bench_n_warmup_iter, targets)
# Save the decode performance of every iteration for plotting in superset
for i in range(1, iteration):
benchmark_data.add_measurement(
profiler,
0,
"inference_decode",
f"time_to_token_{i}",
profiler.get_duration(f"inference_decode_time_{i}") * 1000,
step_warm_up_num_iterations=None,
target=None,
)
# Also save the avg decode performance for the 128 iterations (excluding the compile time)
inference_decode_time_first_128 = sum(
profiler.get_duration(f"inference_decode_time_{i}") for i in range(1, 128)
)
benchmark_data.add_measurement(
profiler,
0,
"inference_decode",
"avg_decode_time_first_128",
inference_decode_time_first_128 * 1000 / 127,
step_warm_up_num_iterations=None,
target=None,
)
benchmark_data.save_partial_run_json(
profiler,
run_type=f"{tt_device_name}-demo",
ml_model_name=model_args.base_model_name,
ml_model_type="llm",
num_layers=model_args.n_layers,
batch_size=batch_size,
input_sequence_length=max(prefill_lens),
output_sequence_length=num_tokens_generated_decode[0],
)