Text Generation #
Score filtering, top-k sampling, logit extraction, decoding, and causal masks used by language-model examples.
Return the indices of the top k scores (largest first).
This deterministic utility is used by the GPT-style examples. The direct O(k*vocab) implementation
is adequate for the vocabulary sizes and top-k values used by these executable examples.
Instances For
Apply a repetition penalty by subtracting
$\mathrm{repeatPenalty}\,\mathrm{count}(\mathrm{token})$ for tokens
appearing in recent.
This is a local sampling heuristic; it is not the same as the presence or frequency penalties used by hosted APIs, but it gives examples a deterministic way to discourage immediate repetition.
Instances For
Printable ASCII bytes plus newline.
Instances For
Escape one byte token for display inside a quoted string.
Instances For
Escape byte ids as a one-line quoted display string.
Instances For
Sample one token id from scores using temperature + top-k sampling.
The randomness is deterministic given (seed, counter), so a run with the same flags produces the
same sampled text.
Instances For
Autoregressively extend token ids with a model-provided score callback.
The callback receives the padded context window and the sequence position whose logits should be
used for the next token. The shared policy crops to the last seqLen tokens, pads, applies repeat
penalties, samples by top-k/temperature, and appends one token per step.
Instances For
Extract the vocabulary-score row at one sequence position.
Instances For
Extract a vocabulary-score row from batched logits.
Instances For
Decode a matrix of token logits by taking argmax independently at each sequence position.
The shape is (seqLen × vocab), i.e. one logits vector per token position. This helper is for
inspection/debugging and is not differentiable.
Instances For
Decode (seqLen × vocab) logits as text using a tokenizer.
Instances For
Extract batchIdx from batched logits and return the per-position argmax token ids.
Instances For
Decode one batch row of (batch × seqLen × vocab) logits as text.
Instances For
Causal (autoregressive) attention mask of shape (seqLen × seqLen).
Entry $(i,j)$ is true iff $j \leq i$, meaning position $i$ may attend to itself and earlier
positions but not to future positions.