Implement CoT-decoding for a given chat input. Args: model: The Hugging Face transformer model. tokenizer: The associated tokenizer. messages: List of chat messages in the format [{"role": "user", "content": "..."}] k: The number of alternative tokens to
(
model: PreTrainedModel,
tokenizer: PreTrainedTokenizer,
messages: List[Dict[str, str]],
k: int = 10,
num_beams: int = 1,
max_new_tokens: int = 512,
temperature: float = 1.0,
top_p: float = 1.0,
repetition_penalty: float = 1.0,
length_penalty: float = 1.0,
no_repeat_ngram_size: int = 0,
early_stopping: bool = False,
aggregate_paths: bool = False,
)
| 50 | return best_answer, answer_scores[best_answer] |
| 51 | |
| 52 | def cot_decode( |
| 53 | model: PreTrainedModel, |
| 54 | tokenizer: PreTrainedTokenizer, |
| 55 | messages: List[Dict[str, str]], |
| 56 | k: int = 10, |
| 57 | num_beams: int = 1, |
| 58 | max_new_tokens: int = 512, |
| 59 | temperature: float = 1.0, |
| 60 | top_p: float = 1.0, |
| 61 | repetition_penalty: float = 1.0, |
| 62 | length_penalty: float = 1.0, |
| 63 | no_repeat_ngram_size: int = 0, |
| 64 | early_stopping: bool = False, |
| 65 | aggregate_paths: bool = False, |
| 66 | ) -> Tuple[str, float]: |
| 67 | """ |
| 68 | Implement CoT-decoding for a given chat input. |
| 69 | |
| 70 | Args: |
| 71 | model: The Hugging Face transformer model. |
| 72 | tokenizer: The associated tokenizer. |
| 73 | messages: List of chat messages in the format [{"role": "user", "content": "..."}] |
| 74 | k: The number of alternative tokens to consider at the first step. |
| 75 | num_beams: Number of beams for beam search. |
| 76 | max_new_tokens: Maximum number of new tokens to generate. |
| 77 | temperature: Sampling temperature. |
| 78 | top_p: Nucleus sampling probability. |
| 79 | repetition_penalty: Repetition penalty factor. |
| 80 | length_penalty: Length penalty factor. |
| 81 | no_repeat_ngram_size: Size of n-grams to avoid repeating. |
| 82 | early_stopping: Whether to stop generation when all beams are finished. |
| 83 | aggregate_paths: Whether to aggregate multiple paths. |
| 84 | |
| 85 | Returns: |
| 86 | A tuple containing the best path (or aggregated result) and its confidence score. |
| 87 | """ |
| 88 | device = get_device() |
| 89 | model.to(device) |
| 90 | |
| 91 | # Use the chat template to format the input |
| 92 | if tokenizer.chat_template: |
| 93 | input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) |
| 94 | else: |
| 95 | # Fallback for tokenizers without chat templates |
| 96 | input_text = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages]) |
| 97 | input_text += "\nassistant:" |
| 98 | |
| 99 | input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device) |
| 100 | attention_mask = torch.ones_like(input_ids).to(device) |
| 101 | |
| 102 | # Set pad_token_id if it's not set |
| 103 | if tokenizer.pad_token_id is None: |
| 104 | tokenizer.pad_token_id = tokenizer.eos_token_id |
| 105 | |
| 106 | # Get the top-k tokens for the first decoding step |
| 107 | with torch.no_grad(): |
| 108 | outputs = model(input_ids, attention_mask=attention_mask) |
| 109 | first_token_logits = outputs.logits[0, -1, :] |
no test coverage detected