| 1350 | self._samplers.append(sampler) |
| 1351 | |
| 1352 | def _passes_p_min(self, output_index: int) -> bool: |
| 1353 | if self.p_min <= 0.0: |
| 1354 | return True |
| 1355 | logits_ptr = llama_cpp.llama_get_logits_ith(self.ctx, output_index) |
| 1356 | if not logits_ptr: |
| 1357 | return False |
| 1358 | logits = np.ctypeslib.as_array(logits_ptr, shape=(self.n_vocab,)) |
| 1359 | n_values = min(self.top_k, self.n_vocab) |
| 1360 | if n_values <= 0: |
| 1361 | return False |
| 1362 | if n_values == self.n_vocab: |
| 1363 | values = logits |
| 1364 | else: |
| 1365 | top_indices = np.argpartition(logits, -n_values)[-n_values:] |
| 1366 | values = logits[top_indices] |
| 1367 | max_logit = float(np.max(values)) |
| 1368 | weights = np.exp(values.astype(np.float64, copy=False) - max_logit) |
| 1369 | total = float(np.sum(weights)) |
| 1370 | if total <= 0.0 or not math.isfinite(total): |
| 1371 | return False |
| 1372 | return 1.0 / total >= self.p_min |
| 1373 | |
| 1374 | def _sample_token(self, output_index: int = 0, *, seq_id: int = 0) -> Optional[int]: |
| 1375 | if seq_id < 0 or seq_id >= len(self._samplers): |