Spaces:
Running
on
T4
Running
on
T4
File size: 24,511 Bytes
2776201 |
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 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 |
# Copyright Lightning AI. Licensed under the Apache License 2.0, see LICENSE file.
from typing import Any, Literal, Optional
import torch
# import torch._dynamo.config
# import torch._inductor.config
from litgpt.model import GPT
from utils.snac_utils import layershift, snac_config
from tqdm import tqdm
def multinomial_num_samples_1(probs: torch.Tensor) -> torch.Tensor:
if torch._dynamo.is_compiling():
# Faster alternative to `torch.multinomial(probs, num_samples=1)` that is also CUDAGraph friendly
distribution = torch.empty_like(probs).exponential_(1)
return torch.argmax(probs / distribution, dim=-1, keepdim=True)
return torch.multinomial(probs, num_samples=1)
def sample_top_p(logits: torch.Tensor, top_p: float) -> torch.Tensor:
sorted_logits, sorted_indices = torch.sort(logits, descending=False)
cumulative_probs = sorted_logits.softmax(dim=-1).cumsum(dim=-1)
# Example:
# sorted_probs=[0.1, 0.15, 0.2, 0.25, 0.3] -> sorted_cumprobs=[0.1, 0.25, 0.45, 0.7, 1.0]
# sorted_indices_to_remove = [1, 1, 0, 0, 0] if top_p=0.7
sorted_indices_to_remove = cumulative_probs <= (1 - top_p)
# Keep at least 1 token always to prevent the case where no token is selected
# In this case the most probable one is always kept
sorted_indices_to_remove[-1:] = 0
indices_to_remove = sorted_indices_to_remove.scatter(
0, sorted_indices, sorted_indices_to_remove
)
logits = logits.masked_fill(indices_to_remove, float("-inf"))
return logits
def sample(
logits: torch.Tensor,
temperature: float = 1.0,
top_k: Optional[int] = None,
top_p: float = 1.0,
) -> torch.Tensor:
if top_p < 0.0 or top_p > 1.0:
raise ValueError(f"top_p must be in [0, 1], got {top_p}")
logits = logits[0, -1]
# optionally crop the logits to only the top k options
if top_k is not None:
v, i = torch.topk(logits, min(top_k, logits.size(-1)))
# do not use `torch.where` as in nanogpt because it will repeat top-k collisions
logits = torch.full_like(logits, float("-inf")).scatter_(-1, i, v)
# optionally scale the logits and sample from a probability distribution
if temperature > 0.0 or top_p > 0.0:
if temperature > 0.0:
logits = logits / temperature
# optionally crop the logits to smallest set of logits with a cumulative probability above top_p
if top_p < 1.0:
logits = sample_top_p(logits, top_p)
probs = torch.nn.functional.softmax(logits, dim=-1)
return multinomial_num_samples_1(probs)
return torch.argmax(logits, dim=-1, keepdim=True)
def next_token(
model: GPT, input_pos: torch.Tensor, x: list, **kwargs: Any
) -> torch.Tensor:
input_pos = input_pos.to(model.device)
logits_a, logit_t = model(x, input_pos)
next_audio_tokens = []
for logit_a in logits_a:
next_a = sample(logit_a, **kwargs).to(dtype=x[0].dtype)
next_audio_tokens.append(next_a)
next_t = sample(logit_t, **kwargs).to(dtype=x[0].dtype)
return next_audio_tokens, next_t
def next_token_asr(
model: GPT,
input_pos: torch.Tensor,
audio_features: torch.tensor,
lens: int,
input_ids: list,
**kwargs: Any,
) -> torch.Tensor:
input_pos = input_pos.to(model.device)
input_ids = [input_id.to(model.device) for input_id in input_ids]
logits_a, logit_t = model(audio_features, input_ids, input_pos, whisper_lens=lens)
next_audio_tokens = []
for logit_a in logits_a:
next_a = sample(logit_a, **kwargs).to(dtype=input_ids[0].dtype)
next_audio_tokens.append(next_a)
next_t = sample(logit_t, **kwargs).to(dtype=input_ids[0].dtype)
return next_audio_tokens, next_t
def next_token_A1T2(
model: GPT,
audio_features: torch.tensor,
input_ids: list,
whisper_lens: int,
task: list,
input_pos: torch.Tensor,
**kwargs: Any,
) -> torch.Tensor:
input_pos = input_pos.to(model.device)
input_ids = [input_id.to(model.device) for input_id in input_ids]
logits_a, logit_t = model(
audio_features, input_ids, input_pos, whisper_lens=whisper_lens, task=task
)
next_audio_tokens = []
for logit_a in logits_a:
next_a = sample(logit_a, **kwargs).to(dtype=input_ids[0].dtype)
next_audio_tokens.append(next_a)
next_t = sample(logit_t, **kwargs).to(dtype=input_ids[0].dtype)
return next_audio_tokens, next_t
def next_token_A1T1(
model: GPT,
audio_features: torch.tensor,
input_ids: list,
whisper_lens: int,
task: list,
input_pos: torch.Tensor,
**kwargs: Any,
) -> torch.Tensor:
input_pos = input_pos.to(model.device)
input_ids = [input_id.to(model.device) for input_id in input_ids]
logits_a, logit_t = model(
audio_features, input_ids, input_pos, whisper_lens=whisper_lens, task=task
)
next_t = sample(logit_t, **kwargs).to(dtype=input_ids[0].dtype)
return next_t
def next_token_batch(
model: GPT,
audio_features: torch.tensor,
input_ids: list,
whisper_lens: int,
task: list,
input_pos: torch.Tensor,
**kwargs: Any,
) -> torch.Tensor:
input_pos = input_pos.to(model.device)
input_ids = [input_id.to(model.device) for input_id in input_ids]
logits_a, logit_t = model(
audio_features, input_ids, input_pos, whisper_lens=whisper_lens, task=task
)
for i in range(7):
logits_a[i] = logits_a[i][0].unsqueeze(0)
logit_t = logit_t[1].unsqueeze(0)
next_audio_tokens = []
for logit_a in logits_a:
next_a = sample(logit_a, **kwargs).to(dtype=input_ids[0].dtype)
next_audio_tokens.append(next_a)
next_t = sample(logit_t, **kwargs).to(dtype=input_ids[0].dtype)
return next_audio_tokens, next_t
# torch._dynamo.config.automatic_dynamic_shapes = True
# torch._inductor.config.triton.unique_kernel_names = True
# torch._inductor.config.coordinate_descent_tuning = True
# next_token = torch.compile(next_token, mode="reduce-overhead")
@torch.inference_mode()
def generate(
model: GPT,
input_ids: list,
max_returned_tokens: int,
*,
temperature: float = 1.0,
top_k: Optional[int] = None,
top_p: float = 1.0,
eos_id_a: Optional[int] = None,
eos_id_t: Optional[int] = None,
pad_id: Optional[int] = None,
shift: Optional[int] = None,
include_prompt: bool = True,
generate_text=False,
) -> torch.Tensor:
# print("eos_id_a:", eos_id_a)
# print("eos_id_t:", eos_id_t)
# print("pad_id:", pad_id)
"""
Takes a conditioning sequence (prompt) as input and continues to generate as many tokens as requested.
The implementation of this function is modified from A. Karpathy's nanoGPT.
Args:
model: The model to use.
prompt: Tensor of shape (T) with indices of the prompt sequence.
max_returned_tokens: The maximum number of tokens to return (given plus generated).
temperature: Scales the predicted logits by 1 / temperature.
top_k: If specified, only sample among the tokens with the k highest probabilities.
top_p: If specified, it represents the cumulative probability threshold to consider in the sampling process.
In top-p sampling, the next token is sampled from the highest probability tokens
whose cumulative probability exceeds the threshold `top_p`. When specified,
it must be `0 <= top_p <= 1`. Here, `top_p=0` is equivalent
to sampling the most probable token, while `top_p=1` samples from the whole distribution.
It can be used in conjunction with `top_k` and `temperature` with the following order
of application:
1. `top_k` sampling
2. `temperature` scaling
3. `top_p` sampling
For more details, see https://arxiv.org/abs/1904.09751
or https://huyenchip.com/2024/01/16/sampling.html#top_p
eos_id: If specified, stop generating any more token once the <eos> token is triggered.
include_prompt: If true (default) prepends the prompt (after applying the prompt style) to the output.
"""
T = input_ids[0].size(0)
device = input_ids[0].device
assert max_returned_tokens > T
if model.max_seq_length < max_returned_tokens - 1:
# rolling the kv cache based on the `input_pos` value would be necessary. However, doing so would introduce a
# data dependency on the `input_pos` tensor and impact model compilation. Since this setting is uncommon, we do
# not support it to avoid negatively impacting the overall speed
raise NotImplementedError(
f"max_seq_length {model.max_seq_length} needs to be >= {max_returned_tokens - 1}"
)
for input_id in input_ids:
input_id = [input_id]
(
tokens_A1,
tokens_A2,
tokens_A3,
tokens_A4,
tokens_A5,
tokens_A6,
tokens_A7,
tokens_T,
) = input_ids
tokens_A1_output = [tokens_A1]
tokens_A2_output = [tokens_A2]
tokens_A3_output = [tokens_A3]
tokens_A4_output = [tokens_A4]
tokens_A5_output = [tokens_A5]
tokens_A6_output = [tokens_A6]
tokens_A7_output = [tokens_A7]
tokens_T_output = [tokens_T]
list_output = [
tokens_A1_output,
tokens_A2_output,
tokens_A3_output,
tokens_A4_output,
tokens_A5_output,
tokens_A6_output,
tokens_A7_output,
tokens_T_output,
]
input_pos = torch.tensor([T], device=device)
model_input_ids = [
tokens_A1.view(1, -1),
tokens_A2.view(1, -1),
tokens_A3.view(1, -1),
tokens_A4.view(1, -1),
tokens_A5.view(1, -1),
tokens_A6.view(1, -1),
tokens_A7.view(1, -1),
tokens_T.view(1, -1),
]
tokens_A, token_T = next_token(
model,
torch.arange(0, T, device=device),
model_input_ids,
temperature=temperature,
top_k=top_k,
top_p=top_p,
)
for i in range(7):
list_output[i].append(tokens_A[i].clone())
list_output[7].append(token_T.clone())
# prepare the input for the next iteration
for i in range(7):
tokens_A[i] = tokens_A[i].clone() + shift + i * snac_config.padded_vocab_size
token_T = token_T.clone()
text_end = False
max_returned_tokens = 1000
for _ in tqdm(range(2, max_returned_tokens - T + 1)):
model_input_ids = [
token_a.view(1, -1).to(torch.int32) for token_a in tokens_A
] + [token_T.view(1, -1).to(torch.int32)]
tokens_A, token_T = next_token(
model,
input_pos,
model_input_ids,
temperature=temperature,
top_k=top_k,
top_p=top_p,
)
if text_end:
token_T = torch.tensor([pad_id], device=device)
for i in range(7):
list_output[i].append(tokens_A[i].clone())
list_output[7].append(token_T.clone())
if tokens_A[-1] == eos_id_a:
break
if token_T == eos_id_t:
if generate_text:
break
text_end = True
for i in range(7):
tokens_A[i] = tokens_A[i].clone() + shift + i * snac_config.padded_vocab_size
token_T = token_T.clone()
input_pos = input_pos.add_(1)
for i in range(len(list_output)):
list_output[i] = torch.cat(list_output[i])
return list_output
@torch.inference_mode()
def generate_TA_BATCH(
model: GPT,
audio_features: torch.Tensor,
input_ids: list,
leng,
task,
max_returned_tokens: int = 1000,
*,
temperature: float = 1.0,
top_k: Optional[int] = None,
top_p: float = 1.0,
eos_id_a: Optional[int] = None,
eos_id_t: Optional[int] = None,
pad_id_t: Optional[int] = None,
shift: Optional[int] = None,
include_prompt: bool = True,
generate_text=False,
) -> torch.Tensor:
T = input_ids[0].size(1)
device = input_ids[0].device
assert max_returned_tokens > T
if model.max_seq_length < max_returned_tokens - 1:
raise NotImplementedError(
f"max_seq_length {model.max_seq_length} needs to be >= {max_returned_tokens - 1}"
)
input_pos = torch.tensor([T], device=device)
model_input_ids = input_ids
list_output = [[] for i in range(8)]
tokens_A, token_T = next_token_batch(
model,
audio_features.to(torch.float32).to(model.device),
input_ids,
[T - 3, T - 3],
["A1T2", "A1T2"],
input_pos=torch.arange(0, T, device=device),
temperature=temperature,
top_k=top_k,
top_p=top_p,
)
for i in range(7):
list_output[i].append(tokens_A[i].tolist()[0])
list_output[7].append(token_T.tolist()[0])
model_input_ids = [[] for i in range(8)]
for i in range(7):
tokens_A[i] = tokens_A[i].clone() + shift + i * snac_config.padded_vocab_size
model_input_ids[i].append(tokens_A[i].clone().to(device).to(torch.int32))
model_input_ids[i].append(torch.tensor([layershift(snac_config.end_of_audio, i)], device=device))
model_input_ids[i] = torch.stack(model_input_ids[i])
model_input_ids[-1].append(token_T.clone().to(torch.int32))
model_input_ids[-1].append(token_T.clone().to(torch.int32))
model_input_ids[-1] = torch.stack(model_input_ids[-1])
text_end = False
for _ in range(2, max_returned_tokens - T + 1):
tokens_A, token_T = next_token_batch(
model,
None,
model_input_ids,
None,
None,
input_pos=input_pos,
temperature=temperature,
top_k=top_k,
top_p=top_p,
)
if text_end:
token_T = torch.tensor([pad_id_t], device=device)
if tokens_A[-1] == eos_id_a:
break
if token_T == eos_id_t:
text_end = True
for i in range(7):
list_output[i].append(tokens_A[i].tolist()[0])
list_output[7].append(token_T.tolist()[0])
model_input_ids = [[] for i in range(8)]
for i in range(7):
tokens_A[i] = tokens_A[i].clone() + shift + i * snac_config.padded_vocab_size
model_input_ids[i].append(tokens_A[i].clone().to(device).to(torch.int32))
model_input_ids[i].append(
torch.tensor([layershift(snac_config.end_of_audio, i)], device=device)
)
model_input_ids[i] = torch.stack(model_input_ids[i])
model_input_ids[-1].append(token_T.clone().to(torch.int32))
model_input_ids[-1].append(token_T.clone().to(torch.int32))
model_input_ids[-1] = torch.stack(model_input_ids[-1])
input_pos = input_pos.add_(1)
return list_output
@torch.inference_mode()
def generate_TT(
model: GPT,
audio_features: torch.Tensor,
input_ids: list,
leng,
task,
max_returned_tokens: int = 2048,
*,
temperature: float = 1.0,
top_k: Optional[int] = None,
top_p: float = 1.0,
eos_id_a: Optional[int] = None,
eos_id_t: Optional[int] = None,
pad_id_t: Optional[int] = None,
shift: Optional[int] = None,
include_prompt: bool = True,
generate_text=False,
) -> torch.Tensor:
T = input_ids[0].size(1)
device = input_ids[0].device
output = []
token_T = next_token_A1T1(
model,
None,
input_ids,
None,
None,
input_pos=torch.arange(0, T, device=device),
temperature=temperature,
top_k=top_k,
top_p=top_p,
)
output.append(token_T.clone().tolist()[0])
input_pos = torch.tensor([T], device=device)
for _ in tqdm(range(2, max_returned_tokens - T + 1)):
model_input_ids = []
for i in range(7):
model_input_ids.append(
torch.tensor([layershift(snac_config.end_of_audio, i)])
.view(1, -1)
.to(torch.int32)
.to(device)
)
model_input_ids.append(token_T.clone().view(1, -1).to(torch.int32).to(device))
token_T = next_token_A1T1(
model,
None,
model_input_ids,
None,
None,
input_pos=input_pos,
temperature=temperature,
top_k=top_k,
top_p=top_p,
)
if token_T == eos_id_t:
break
output.append(token_T.clone().tolist()[0])
input_pos = input_pos.add_(1)
return output
@torch.inference_mode()
def generate_AT(
model: GPT,
audio_features: torch.Tensor,
input_ids: list,
leng,
task,
max_returned_tokens: int = 2048,
*,
temperature: float = 1.0,
top_k: Optional[int] = None,
top_p: float = 1.0,
eos_id_a: Optional[int] = None,
eos_id_t: Optional[int] = None,
pad_id_t: Optional[int] = None,
shift: Optional[int] = None,
include_prompt: bool = True,
generate_text=False,
) -> torch.Tensor:
T = input_ids[0].size(1)
device = input_ids[0].device
output = []
token_T = next_token_A1T1(
model,
audio_features.to(torch.float32).to(model.device),
input_ids,
[T - 3],
["AT"],
input_pos=torch.arange(0, T, device=device),
temperature=temperature,
top_k=top_k,
top_p=top_p,
)
output.append(token_T.clone().tolist()[0])
input_pos = torch.tensor([T], device=device)
text_end = False
for _ in tqdm(range(2, max_returned_tokens - T + 1)):
model_input_ids = []
for i in range(7):
model_input_ids.append(
torch.tensor([layershift(snac_config.end_of_audio, i)])
.view(1, -1)
.to(torch.int32)
.to(device)
)
model_input_ids.append(token_T.clone().view(1, -1).to(torch.int32).to(device))
token_T = next_token_A1T1(
model,
None,
model_input_ids,
None,
None,
input_pos=input_pos,
temperature=temperature,
top_k=top_k,
top_p=top_p,
)
if token_T == eos_id_t:
break
output.append(token_T.clone().tolist()[0])
input_pos = input_pos.add_(1)
return output
@torch.inference_mode()
def generate_TA(
model: GPT,
audio_features: torch.Tensor,
input_ids: list,
leng,
task,
max_returned_tokens: int = 2048,
*,
temperature: float = 1.0,
top_k: Optional[int] = None,
top_p: float = 1.0,
eos_id_a: Optional[int] = None,
eos_id_t: Optional[int] = None,
pad_id_t: Optional[int] = None,
shift: Optional[int] = None,
include_prompt: bool = True,
generate_text=False,
) -> torch.Tensor:
T = input_ids[0].size(1)
device = input_ids[0].device
output = [[] for _ in range(8)]
tokens_A, token_T = next_token_A1T2(
model,
None,
input_ids,
None,
None,
input_pos=torch.arange(0, T, device=device),
temperature=temperature,
top_k=top_k,
top_p=top_p,
)
for i in range(7):
output[i].append(tokens_A[i].clone().tolist()[0])
output[7].append(token_T.clone().tolist()[0])
input_pos = torch.tensor([T], device=device)
text_end = False
for _ in tqdm(range(2, max_returned_tokens - T + 1)):
model_input_ids = []
for i in range(7):
model_input_ids.append(
layershift(tokens_A[i].clone(), i)
.view(1, -1)
.to(torch.int32)
.to(device)
)
model_input_ids.append(token_T.clone().view(1, -1).to(torch.int32).to(device))
tokens_A, token_T = next_token_A1T2(
model,
None,
model_input_ids,
None,
None,
input_pos=input_pos,
temperature=temperature,
top_k=top_k,
top_p=top_p,
)
if text_end:
token_T = torch.tensor([pad_id_t], device=device)
if tokens_A[-1] == eos_id_a:
break
if token_T == eos_id_t:
text_end = True
for i in range(7):
output[i].append(tokens_A[i].clone().tolist()[0])
output[7].append(token_T.clone().tolist()[0])
input_pos = input_pos.add_(1)
return output
@torch.inference_mode()
def generate_AA(
model: GPT,
audio_features: torch.Tensor,
input_ids: list,
leng,
task,
max_returned_tokens: int = 2048,
*,
temperature: float = 1.0,
top_k: Optional[int] = None,
top_p: float = 1.0,
eos_id_a: Optional[int] = None,
eos_id_t: Optional[int] = None,
pad_id_t: Optional[int] = None,
shift: Optional[int] = None,
include_prompt: bool = True,
generate_text=False,
) -> torch.Tensor:
T = input_ids[0].size(1)
device = input_ids[0].device
output = [[] for _ in range(8)]
tokens_A, token_T = next_token_A1T2(
model,
audio_features.to(torch.float32).to(model.device),
input_ids,
[T - 3],
["A1T2"],
input_pos=torch.arange(0, T, device=device),
temperature=temperature,
top_k=top_k,
top_p=top_p,
)
for i in range(7):
output[i].append(tokens_A[i].clone().tolist()[0])
output[7].append(token_T.clone().tolist()[0])
input_pos = torch.tensor([T], device=device)
text_end = False
for _ in tqdm(range(2, max_returned_tokens - T + 1)):
model_input_ids = []
for i in range(7):
model_input_ids.append(
layershift(tokens_A[i].clone(), i)
.view(1, -1)
.to(torch.int32)
.to(device)
)
model_input_ids.append(token_T.clone().view(1, -1).to(torch.int32).to(device))
tokens_A, token_T = next_token_A1T2(
model,
None,
model_input_ids,
None,
None,
input_pos=input_pos,
temperature=temperature,
top_k=top_k,
top_p=top_p,
)
if text_end:
token_T = torch.tensor([pad_id_t], device=device)
if tokens_A[-1] == eos_id_a:
break
if token_T == eos_id_t:
# print("text_end")
text_end = True
for i in range(7):
output[i].append(tokens_A[i].clone().tolist()[0])
output[7].append(token_T.clone().tolist()[0])
input_pos = input_pos.add_(1)
return output
@torch.inference_mode()
def generate_ASR(
model: GPT,
audio_features: torch.Tensor,
input_ids: list,
leng,
task,
max_returned_tokens: int = 1200,
*,
temperature: float = 1.0,
top_k: Optional[int] = None,
top_p: float = 1.0,
eos_id_a: Optional[int] = None,
eos_id_t: Optional[int] = None,
pad_id_t: Optional[int] = None,
shift: Optional[int] = None,
include_prompt: bool = True,
generate_text=False,
) -> torch.Tensor:
T = input_ids[0].size(1)
device = input_ids[0].device
output = []
token_T = next_token_A1T1(
model,
audio_features.to(torch.float32).to(model.device),
input_ids,
[T - 3],
["asr"],
input_pos=torch.arange(0, T, device=device),
temperature=temperature,
top_k=top_k,
top_p=top_p,
)
output.append(token_T.clone().tolist()[0])
input_pos = torch.tensor([T], device=device)
text_end = False
for _ in tqdm(range(2, max_returned_tokens - T + 1)):
model_input_ids = []
for i in range(7):
model_input_ids.append(
torch.tensor([layershift(snac_config.end_of_audio, i)])
.view(1, -1)
.to(torch.int32)
.to(device)
)
model_input_ids.append(token_T.clone().view(1, -1).to(torch.int32).to(device))
token_T = next_token_A1T1(
model,
None,
model_input_ids,
None,
None,
input_pos=input_pos,
temperature=temperature,
top_k=top_k,
top_p=top_p,
)
if token_T == eos_id_t:
break
output.append(token_T.clone().tolist()[0])
input_pos = input_pos.add_(1)
return output
|