(beams: List[Beam], num_beams: int, model)
| 630 | |
| 631 | |
| 632 | def expand_beams(beams: List[Beam], num_beams: int, model) -> List[Beam]: |
| 633 | args = get_args() |
| 634 | tokenizer = get_tokenizer() |
| 635 | |
| 636 | context_tokens = [b.tokens.copy() for b in beams] |
| 637 | context_tokens, context_lengths = pad_batch(context_tokens, tokenizer.eod, args) |
| 638 | |
| 639 | context_lengths = set(context_lengths) |
| 640 | assert len(context_lengths) == 1, "context_lengths must be the same" |
| 641 | context_length = list(context_lengths)[0] |
| 642 | |
| 643 | context_tokens_tensor = torch.cuda.LongTensor(context_tokens) |
| 644 | tokens, attention_mask, position_ids = get_batch_(context_tokens_tensor) |
| 645 | tokens, scores = sample_topk_tokens(model, tokens, attention_mask, position_ids, context_length, num_beams) |
| 646 | tokens = tokens.detach().cpu().tolist() |
| 647 | scores = scores.detach().cpu().tolist() |
| 648 | assert len(tokens) == len(beams), "output tokens and input beams must have the same length" |
| 649 | |
| 650 | all_beams = [] |
| 651 | for i in range(len(beams)): |
| 652 | this_tokens = tokens[i] |
| 653 | this_scores = scores[i] |
| 654 | |
| 655 | for token, score in zip(this_tokens, this_scores): |
| 656 | all_beams.append(Beam(beams[i].tokens + [token], beams[i].score + score)) |
| 657 | |
| 658 | return all_beams |
| 659 | |
| 660 | |
| 661 | def beam_search(model, context_tokens, num_beams: int): |
no test coverage detected