# Perplexity randomly failing due to missing cache file

**URL:** <https://discuss.huggingface.co/t/perplexity-randomly-failing-due-to-missing-cache-file/70790>\
**Category:** Beginners\
**Created:** [January 27, 2024, 6:23am UTC](https://discuss.huggingface.co/t/perplexity-randomly-failing-due-to-missing-cache-file/70790 "2024-01-27T06:23:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adambjorn](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@adambjorn](https://discuss.huggingface.co/u/adambjorn)\
**Post date:** [January 27, 2024, 6:23am UTC](https://discuss.huggingface.co/t/perplexity-randomly-failing-due-to-missing-cache-file/70790/1 "2024-01-27T06:23:20Z")

</div>

Hi! I am new to the transformers and evaluate libraries but I am noticing that when trying to calculate perplexity my notebook will randomly fail with the error:

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/9/2/92b8881b242148044f931df9691ce2d7775a4a44.png)

It seems to happen randomly. When I call `model.generate` I am passing the following parameters:  
`inputs, min_new_tokens=200, max_length=350, do_sample=do_sample, top_p=top_p, top_k=top_k`

And here is my function for calculating perplexity:

```python
from evaluate import load
def calc_perplexity(model_name, text):
  perplexity = load("perplexity", module_type="metric")
  results = perplexity.compute(predictions=text, model_id=model_name)
  return round(results.get('mean_perplexity'),2)

```

The exact same parameters being passed on different runs will sometimes fail and sometimes not. Normally the first call to perplexity will work, but then the second call does not. Any ideas what could be happening here?

EDIT: It fails after a few consecutive calls, generally about 3

Solved: Well I am a goober. A string cleaning function I had was not returning in some edge scenarios and that caused a bunch of down stream issues. I’ll leave thi post up though in case anyone runs into something similiar.

---

<div class="post-metadata">

**Author:** ![adambjorn](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@adambjorn](https://discuss.huggingface.co/u/adambjorn)\
**Post date:** [January 27, 2024, 6:33am UTC](https://discuss.huggingface.co/t/perplexity-randomly-failing-due-to-missing-cache-file/70790/2 "2024-01-27T06:33:54Z")

</div>

Here is a copy of my notebook if it helps: [Google Colab](https://colab.research.google.com/drive/18ul3Imm6-W2wyYkOQyXIvSPqhMypJuj7?usp=sharing)

Don’t judge the code too harshly I’m still learning the library 🙂
