Download sampling.py from SlayerLab/Slayer1B-Poetry: direct link, hf CLI and curl.
- Browser
- Download file 632 Bytes
-
https://huggingface.co/SlayerLab/Slayer1B-Poetry/resolve/main/sampling.py
- Command line
-
hf download hf://SlayerLab/Slayer1B-Poetry/sampling.py
-
curl -L -o sampling.py https://huggingface.co/SlayerLab/Slayer1B-Poetry/resolve/main/sampling.py
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 | |