| 372 | return token == tokenizer_eos; |
| 373 | } |
| 374 | |
| 375 | void apply_repetition_penalty( |
| 376 | std::vector<float> & logits, |
| 377 | const std::vector<int32_t> & history, |
| 378 | float penalty, |
| 379 | SamplerScratch & scratch) { |
| 380 | if (penalty == 1.0F) { |
| 381 | return; |
| 382 | } |
| 383 | if (!(penalty > 0.0F)) { |
| 384 | throw std::runtime_error("Vevo2 AR repetition penalty must be positive"); |
| 385 | } |
| 386 | if (scratch.seen_marks.size() < logits.size()) { |
| 387 | scratch.seen_marks.assign(logits.size(), 0); |
| 388 | scratch.seen_stamp = 1; |
| 389 | } else if (scratch.seen_stamp == std::numeric_limits<uint32_t>::max()) { |
| 390 | std::fill(scratch.seen_marks.begin(), scratch.seen_marks.end(), 0); |
| 391 | scratch.seen_stamp = 1; |
| 392 | } |
| 393 | const uint32_t stamp = scratch.seen_stamp++; |
| 394 | for (const int32_t token : history) { |
| 395 | if (token < 0 || static_cast<size_t>(token) >= logits.size()) { |
| 396 | continue; |
| 397 | } |
| 398 | const size_t index = static_cast<size_t>(token); |
| 399 | if (scratch.seen_marks[index] == stamp) { |
| 400 | continue; |
| 401 | } |
| 402 | scratch.seen_marks[index] = stamp; |
| 403 | float & value = logits[index]; |
| 404 | value = value < 0.0F ? value * penalty : value / penalty; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | void mask_min_new_token_eos(std::vector<float> & logits, int generated_tokens, int32_t eos_token) { |
| 409 | if (generated_tokens >= kMinNewTokens || eos_token < 0 || static_cast<size_t>(eos_token) >= logits.size()) { |
| 410 | return; |
| 411 | } |
| 412 | logits[static_cast<size_t>(eos_token)] = -std::numeric_limits<float>::infinity(); |
| 413 | } |
| 414 | |
| 415 | int32_t sample_token( |
| 416 | const std::vector<float> & logits, |
| 417 | int top_k, |
| 418 | float top_p, |
| 419 | float temperature, |
| 420 | uint64_t seed, |
| 421 | int step, |
| 422 | const TorchCudaSamplingPolicy & sampling_policy, |
| 423 | SamplerScratch & scratch) { |
no test coverage detected