Download easyeditor/util/perplexity.py from ZekunXi/easyedit: direct link, hf CLI and curl.
- Browser
- Download file 758 Bytes
-
https://huggingface.co/spaces/ZekunXi/easyedit/resolve/main/easyeditor/util/perplexity.py
- Command line
-
hf download hf://spaces/ZekunXi/easyedit/easyeditor/util/perplexity.py
-
curl -L -o perplexity.py https://huggingface.co/spaces/ZekunXi/easyedit/resolve/main/easyeditor/util/perplexity.py
758 Bytes
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| def perplexity( | |
| model: AutoModelForCausalLM, | |
| tok: AutoTokenizer, | |
| text: str, | |
| max_input_length: int = None, | |
| ): | |
| """ | |
| Computes perplexity of a piece of text, measured on a reference model. | |
| Text is truncated to max_input_length tokens. | |
| """ | |
| inputs = tok( | |
| [text], return_tensors="pt", max_length=max_input_length, truncation=True | |
| ).to("cuda") | |
| logits = torch.nn.functional.log_softmax(model(**inputs).logits, dim=2) | |
| log_probs = torch.gather(logits[:, :-1, :], 2, inputs["input_ids"][:, 1:, None])[0] | |
| # Perplexity = exp(-1/N * log P(x_1, ..., x_n)) | |
| return torch.exp(-1 / inputs["input_ids"].size(1) * log_probs.sum()).item() | |