Implement Top-k Sampling in PyTorch
Mediumllm-decodingauto-graded
Select the k highest-probability tokens, zero out the rest, renormalize, and sample for controlled text generation.
Solve it
Check your answer
The grader verifies properties of your implementation, so a correct solution written differently from ours still passes.
pip install torchleet
from torchleet import check
check("top-k-sampling", top_k_sample)