Before sampling the next token, LLM decoders usually filter the probability distribution.
Implement filter_probs(probs, top_k=None, top_p=None):
top_k is set, keep only the k most probable tokens.top_p is set, keep the smallest set of most-probable tokens whose cumulative probability is .Apply top_k first, then top_p on the result. Return a NumPy array the same shape as probs.
Example
filter_probs(np.array([0.5, 0.3, 0.15, 0.05]), top_p=0.8)
# array([0.625, 0.375, 0.0, 0.0])