Performs a streaming top-k search using the given parameters. Args: input_ids (torch.Tensor): The input IDs tensor. attention_mask (torch.Tensor): The attention mask tensor. temperature (float, optional): The temperature for logits. Defaults to 0
(
self,
input_ids: torch.Tensor,
attention_mask: torch.Tensor,
temperature: float = 0.7,
repetition_penalty: float = 1.02,
top_k: int = 0,
top_p: float = 0.8,
max_iterations: int = 1024,
regulation_start: int = 512,
length_penalty: float = 1,
max_time: int = 60,
)
| 191 | return preds_i[len(self.prefix):] |
| 192 | |
| 193 | def streaming_topk_search( |
| 194 | self, |
| 195 | input_ids: torch.Tensor, |
| 196 | attention_mask: torch.Tensor, |
| 197 | temperature: float = 0.7, |
| 198 | repetition_penalty: float = 1.02, |
| 199 | top_k: int = 0, |
| 200 | top_p: float = 0.8, |
| 201 | max_iterations: int = 1024, |
| 202 | regulation_start: int = 512, |
| 203 | length_penalty: float = 1, |
| 204 | max_time: int = 60, |
| 205 | ) -> torch.Tensor: |
| 206 | """ |
| 207 | Performs a streaming top-k search using the given parameters. |
| 208 | |
| 209 | Args: |
| 210 | input_ids (torch.Tensor): The input IDs tensor. |
| 211 | attention_mask (torch.Tensor): The attention mask tensor. |
| 212 | temperature (float, optional): The temperature for logits. Defaults to 0.7. |
| 213 | repetition_penalty (float, optional): The repetition penalty factor. Defaults to 1.02. |
| 214 | top_k (int, optional): The top-k value for filtering. Defaults to 0. |
| 215 | top_p (float, optional): The top-p value for filtering. Defaults to 0.92. |
| 216 | max_iterations (int, optional): The maximum number of iterations. Defaults to 1024. |
| 217 | regulation_start (int, optional): The number of iterations after which regulation starts. Defaults to 512. |
| 218 | length_penalty (float, optional): The length penalty factor. Defaults to 1. |
| 219 | max_time (int, optional): The maximum allowed time in seconds. Defaults to 60. |
| 220 | |
| 221 | Returns: |
| 222 | torch.Tensor: The generated output IDs tensor. |
| 223 | """ |
| 224 | assert input_ids.dtype == torch.int64 and attention_mask.dtype == torch.int64 |
| 225 | |
| 226 | self.bsz, self.seqlen = input_ids.shape |
| 227 | |
| 228 | input_ids, attention_mask = input_ids.to('cuda'), attention_mask.to('cuda') |
| 229 | last_token_indices = attention_mask.sum(1) - 1 |
| 230 | |
| 231 | moss_stopwords = self.moss_stopwords.to(input_ids.device) |
| 232 | queue_for_moss_stopwords = torch.empty(size=(self.bsz, len(self.moss_stopwords)), device=input_ids.device, dtype=input_ids.dtype) |
| 233 | all_shall_stop = torch.tensor([False] * self.bsz, device=input_ids.device) |
| 234 | moss_stop = torch.tensor([False] * self.bsz, device=input_ids.device) |
| 235 | |
| 236 | generations, start_time = torch.ones(self.bsz, 1, dtype=torch.int64), time.time() |
| 237 | |
| 238 | past_key_values = None |
| 239 | for i in range(int(max_iterations)): |
| 240 | logits, past_key_values = self.infer_(input_ids if i == 0 else new_generated_id, attention_mask, past_key_values) |
| 241 | |
| 242 | if i == 0: |
| 243 | logits = logits.gather(1, last_token_indices.view(self.bsz, 1, 1).repeat(1, 1, self.vocab_size)).squeeze(1) |
| 244 | else: |
| 245 | logits = logits[:, -1, :] |
| 246 | |
| 247 | |
| 248 | if repetition_penalty > 1: |
| 249 | score = logits.gather(1, input_ids) |
| 250 | # if score < 0 then repetition penalty has to be multiplied to reduce the previous token probability |
no test coverage detected