Skip to content
DeepForgeby svx
All problems
HardNLPnlp-383

Top P Nucleus Filter Mask

Problem

Nucleus sampling keeps the smallest set of tokens whose cumulative probability reaches p, processing tokens in probability-descending order with ties broken by index. Return a 0/1 mask in the original index order.

Given the probabilities and p, return the mask.

Hint

Add tokens until the running mass first reaches p.

Test cases

case 1
in: [[0.5,0.3,0.2],0.8]
expected: [1,1,0]
case 2
in: [[0.5,0.3,0.2],0.99]
expected: [1,1,1]
case 3
in: [[1],0.5]
expected: [1]
Starter codePython
def top_p_nucleus_filter_mask(probs, p):
    # Your code here
    pass

3 test cases · run your code against them in the editor