v148 · origin trial · on-device AI

TopK Visualizer

See exactly how temperature and topK sampling parameters shape the token probability distribution. The chart shows raw logits scaled by temperature, then the topK cutoff that discards the long tail. Adjust both sliders and watch which tokens survive sampling.

Origin trial in Chrome 148. This visualizer uses a simulated token distribution to illustrate the mathematics — it does not call the on-device model directly.
Presets:
temperature
0.1 (deterministic)1.02.5 (chaotic)

Divides logits before softmax. Low temperature → distribution peaks sharply at the top token. High temperature → distribution flattens, all tokens get more equal probability.

topK
140128

Keep only the K highest-probability tokens. Tokens ranked below K are zeroed out before sampling. topK=1 means always pick the single most likely token (greedy decoding).

Token probability distribution 20 tokens shown

the math behind sampling

// 1. Raw logits from the model (one per vocabulary token) logits = [3.2, 1.8, 1.4, 0.9, 0.3, ...]; // 2. Scale by temperature (dividing sharpens or flattens the distribution) scaled = logits.map(l => l / temperature); // 3. Softmax → probabilities (sum to 1.0) const maxL = Math.max(...scaled); const exps = scaled.map(l => Math.exp(l - maxL)); const sumE = exps.reduce((a, b) => a + b, 0); probs = exps.map(e => e / sumE); // 4. TopK: zero out everything below rank K const ranked = [...probs].sort((a, b) => b - a); const kthProb = ranked[topK - 1] ?? 0; masked = probs.map(p => (p >= kthProb ? p : 0)); // 5. Re-normalise and sample const total = masked.reduce((a, b) => a + b, 0); final = masked.map(p => p / total); // → pick one token proportional to final probabilities // Chrome 148 Prompt API: const model = await LanguageModel.create({ temperature: 0.7, topK: 40 }); const response = await model.prompt('Write a haiku about sampling');

references

implementation reference

Need the exact API surface, compatibility boundaries, errors, lifecycle, and source links? Read the matching gendn reference ↗