TensorRT-LLMs/tensorrt_llm/tools/ppl.py
Kaiyu Xie f044eb8d94
Update TensorRT-LLM (#302)
* Update TensorRT-LLM

---------

Co-authored-by: wangruohui <12756472+wangruohui@users.noreply.github.com>
2023-11-07 19:51:58 +08:00

8 lines
216 B
Python

def ppl(logits, output_ids):
"""
Calculate per-token perplexity.
"""
nlls = -logits.log_softmax(dim=-1)
ppls = nlls.gather(-1, output_ids.long().unsqueeze(-1))
return ppls.mean().exp().item()