mirror of
https://github.com/karpathy/llm.c.git
synced 2026-07-26 20:15:08 -04:00
28 lines
No EOL
733 B
C
28 lines
No EOL
733 B
C
/*
|
|
Implements a simple Sampler, used during model inference to sample tokens.
|
|
*/
|
|
#ifndef SAMPLER_H
|
|
#define SAMPLER_H
|
|
|
|
#include <math.h>
|
|
|
|
int sample_softmax(const float* logits, int n, float coin) {
|
|
// sample index from logits (converted to probabilities using softmax)
|
|
// coin is a random number in [0, 1), usually from random_f32()
|
|
double norm = 0;
|
|
for (int i = 0; i < n; i++) {
|
|
norm += expf(logits[i]);
|
|
}
|
|
// instead of dividing all exp(logits), we can just multiply coin.
|
|
coin *= norm;
|
|
float cdf = 0.0f;
|
|
for (int i = 0; i < n; i++) {
|
|
cdf += expf(logits[i]);
|
|
if (coin < cdf) {
|
|
return i;
|
|
}
|
|
}
|
|
return n - 1; // in case of rounding errors
|
|
}
|
|
|
|
#endif |