liveBook · Manning

WeightedIndex

As explained in Server-Side WebAssembly

WeightedIndex is a Rust random-sampling distribution that selects an index with probability proportional to its supplied weight.

WeightedIndex is a Rust random-sampling distribution that chooses an index according to the relative weights supplied to it. In token generation, it turns model probabilities into a stochastic next-token choice rather than always selecting the highest-probability token. section 5.4.2

Sampling the next story token

After softmax converts the final token logits into probabilities, construct a distribution, sample it with a random-number generator, append the selected index to input_ids, and decode the updated sequence. section 5.4.2

let dist = rand::distributions::WeightedIndex::new(&probabilities).unwrap();
let next_token = dist.sample(&mut rng);
input_ids.push(next_token as u32);
let generated_text = tokenizer.decode(&input_ids, true).unwrap();

The complete sampling expression shows how probabilities, an RNG, token IDs, and tokenizer decoding form one generation step. section 5.4.2

How Server-Side WebAssembly covers it

How deep it goes
Shows construction and sampling in one generation step, but does not explain the distribution's internal algorithm.
Examples
Uses language-model logits, softmax probabilities, input_ids, and a generated story as concrete sampling artifacts.
What it assumes
Assumes probability values, a random-number generator, token IDs, and tokenizer decoding are already available.
What you can do after
Apply it via rand::distributions::WeightedIndex to sample probability-weighted token indices.
How it connects
Connects softmax, probability distributions, random-number generation, tokenization, and autoregressive token generation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Server-Side WebAssembly. The section links point to the parts of the book it draws on.