Example 1
Arguments: [
[
2,
4
],
[
1
],
2
]
Expected result: [
2,
2
]Only token 1 is repeated: its positive logit 4 becomes 2.
Decoding and sampling · Easy
Problem 23 of 50 · Python and NumPy
Apply a repetition penalty once to each distinct token ID appearing in history. For a repeated token, divide a positive logit by penalty and multiply a zero or negative logit by penalty. Leave unseen tokens unchanged. Return the adjusted logits in vocabulary order.
def repetition_penalty(logits, history, penalty):Arguments: [
[
2,
4
],
[
1
],
2
]
Expected result: [
2,
2
]Only token 1 is repeated: its positive logit 4 becomes 2.
Arguments: [
[
-2,
0,
2
],
[
0,
1,
2
],
2
]
Expected result: [
-4,
0,
1
]The negative value becomes -4, zero stays zero, and the positive value becomes 1.