Slayer1B-Poetry / sampling.py
kacperwikiel's picture
Release Slayer1B-1.09 English poetry LoRA adapter with model card and evaluation
a46f4fa verified
Raw History Blame Contribute Delete
632 Bytes
"""Sign-aware token repetition penalty, excluding document-control token IDs."""
import math
import torch
def penalize_repetition(logits,token_ids,penalty=1.1,first_text_id=6):
if not math.isfinite(penalty) or penalty<1:raise ValueError('repetition penalty must be finite and >= 1')
if penalty==1:return logits
seen=sorted({n for n in token_ids if first_text_id<=n<logits.shape[-1]})
if not seen:return logits
ids=torch.tensor(seen,device=logits.device,dtype=torch.long)
values=logits.index_select(-1,ids)
logits.index_copy_(-1,ids,torch.where(values<0,values*penalty,values/penalty))
return logits