| 10 | } |
| 11 | |
| 12 | float Sampler::sample_prob(int index, const InferenceState& s) { |
| 13 | const float* logits = s.logits(); |
| 14 | // Find max value to moderate the logits later on for numerical stability |
| 15 | float max_val = -FLT_MAX; |
| 16 | for (int i = 0; i < vocab_size; ++i) { |
| 17 | if (logits[i] > max_val) { |
| 18 | max_val = logits[i]; |
| 19 | } |
| 20 | } |
| 21 | float sum = 0; |
| 22 | for (int i = 0; i < vocab_size; ++i) { |
| 23 | sum += expf(logits[i] - max_val); |
| 24 | } |
| 25 | return expf(logits[index] - max_val) / sum; |
| 26 | } |
| 27 | |
| 28 | int Sampler::sample_argmax(const InferenceState& s) { |
| 29 | const float* logits = s.logits(); |
no test coverage detected