MCPcopy Index your code
hub / github.com/zai-org/CodeGeeX / expand_beams

Function expand_beams

codegeex/megatron/code_generation_utils.py:632–658  ·  view source on GitHub ↗
(beams: List[Beam], num_beams: int, model)

Source from the content-addressed store, hash-verified

630
631
632def 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
661def beam_search(model, context_tokens, num_beams: int):

Callers 1

beam_searchFunction · 0.85

Calls 6

get_argsFunction · 0.90
get_tokenizerFunction · 0.90
get_batch_Function · 0.85
sample_topk_tokensFunction · 0.85
BeamClass · 0.85
pad_batchFunction · 0.70

Tested by

no test coverage detected