soehi/moonlm/sampler does not have a README file

    SamplerError

    pub(all) suberror SamplerError {
    BadTemperature(value~ : Double)
    BadK(value~ : Int)
    BadP(value~ : Double)
    EmptyLogits
    } derive(Eq,
    Debug
    )

    Sampling errors.

    SamplerError::message

    fn SamplerError::message(self : SamplerError) -> String

    Rng

    pub(all) struct Rng {
    state : UInt64
    } derive(
    Debug
    )

    Deterministic xorshift64* generator.

    Seeded explicitly so a run is reproducible: the same seed and the same logits must produce the same token sequence, which is what makes sampling testable at all. xorshift64* has a period of 2^64-1 and passes the usual batteries for sampling use; it is not cryptographic and is not used for anything that needs to be.

    Rng::new

    fn Rng::new(seed : UInt64) -> Rng

    Create a generator. A zero seed is remapped because xorshift64* has a fixed point at zero and would emit zeros forever.

    Rng::next_double

    fn Rng::next_double(self : Rng) -> Double

    Uniform sample in [0, 1).

    Uses the top 53 bits so every representable Double in the interval is reachable with the right frequency, which a modulo-based mapping would not give.

    Rng::next_u64

    fn Rng::next_u64(self : Rng) -> UInt64

    Next raw 64-bit value.

    Sampler

    pub(all) enum Sampler {
    Greedy
    Temperature(temperature~ : Double)
    TopK(temperature~ : Double, k~ : Int)
    TopP(temperature~ : Double, p~ : Double)
    } derive(Eq,
    Debug
    )

    How to pick the next token from logits.

    probs_from

    fn probs_from(logits : FixedArray[Float], temperature : Double) -> Array[Double]

    Softmax over logits / temperature, with the max subtracted first.

    Subtracting the max is what keeps exp in range: raw logits from a model routinely reach 30+, and exp(30) in Float is already near the top of the range, so a larger value would overflow to infinity and make the distribution all-NaN.

    sample

    fn sample(logits : FixedArray[Float], sampler : Sampler, rng : Rng) -> Result[Int, SamplerError]

    Sample a token id from logits.

    rng is unused by greedy, so a greedy sequence is reproducible without seeding anything.

    truncate_top_k

    fn truncate_top_k(probs : Array[Double], k : Int) -> Array[Double]

    Zero every probability except the k largest.

    Ties at the cut-off are resolved by id order, so the result depends only on the logits and not on sort stability.

    truncate_top_p

    fn truncate_top_p(probs : Array[Double], p : Double) -> Array[Double]

    Zero every probability outside the smallest set whose cumulative mass reaches p, then renormalize.

    The token that crosses the threshold is kept, so the retained mass is always at least p — the standard "nucleus" definition.

    Source Files