| 8573 | } |
| 8574 | |
| 8575 | llama_token llama_sample_token_mirostat_v2(struct llama_context * ctx, llama_token_data_array * candidates, float tau, float eta, float * mu) { |
| 8576 | int64_t t_start_sample_us; |
| 8577 | t_start_sample_us = ggml_time_us(); |
| 8578 | |
| 8579 | llama_sample_softmax(ctx, candidates); |
| 8580 | |
| 8581 | // Truncate the words with surprise values greater than mu |
| 8582 | candidates->size = std::distance(candidates->data, std::find_if(candidates->data, candidates->data + candidates->size, [&](const llama_token_data & candidate) { |
| 8583 | return -log2f(candidate.p) > *mu; |
| 8584 | })); |
| 8585 | |
| 8586 | if (candidates->size == 0) { |
| 8587 | candidates->size = 1; |
| 8588 | } |
| 8589 | |
| 8590 | if (ctx) { |
| 8591 | ctx->t_sample_us += ggml_time_us() - t_start_sample_us; |
| 8592 | } |
| 8593 | |
| 8594 | // Normalize the probabilities of the remaining words |
| 8595 | llama_sample_softmax(ctx, candidates); |
| 8596 | |
| 8597 | // Sample the next word X from the remaining words |
| 8598 | llama_token X = llama_sample_token(ctx, candidates); |
| 8599 | t_start_sample_us = ggml_time_us(); |
| 8600 | |
| 8601 | // Compute error as the difference between observed surprise and target surprise value |
| 8602 | size_t X_idx = std::distance(candidates->data, std::find_if(candidates->data, candidates->data + candidates->size, [&](const llama_token_data & candidate) { |
| 8603 | return candidate.id == X; |
| 8604 | })); |
| 8605 | float observed_surprise = -log2f(candidates->data[X_idx].p); |
| 8606 | float e = observed_surprise - tau; |
| 8607 | |
| 8608 | // Update mu using the learning rate and error |
| 8609 | *mu = *mu - eta * e; |
| 8610 | |
| 8611 | if (ctx) { |
| 8612 | ctx->t_sample_us += ggml_time_us() - t_start_sample_us; |
| 8613 | } |
| 8614 | return X; |
| 8615 | } |
| 8616 | |
| 8617 | llama_token llama_sample_token_greedy(struct llama_context * ctx, llama_token_data_array * candidates) { |
| 8618 | const int64_t t_start_sample_us = ggml_time_us(); |
no test coverage detected