# Dataset can't cache model's outputs

**URL:** <https://discuss.huggingface.co/t/dataset-cant-cache-models-outputs/24945>\
**Category:** 🤗Datasets\
**Created:** [October 25, 2022, 12:15pm UTC](https://discuss.huggingface.co/t/dataset-cant-cache-models-outputs/24945 "2022-10-25T12:15:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jongjyh](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/jongjyh/32/24621_2.png) [@jongjyh](https://discuss.huggingface.co/u/jongjyh)\
**Post date:** [October 25, 2022, 12:15pm UTC](https://discuss.huggingface.co/t/dataset-cant-cache-models-outputs/24945/1 "2022-10-25T12:15:04Z")

</div>

Hi,

I try to cache some outputs of teacher model( Knowledge Distillation ) by using map function of Dataset library, while every time I run my code, I still recompute all the sequences. I tested Bert Model like this, I got different hash every single run, so any idea to deal with this?

```auto
from transformers import BertModel
from transformers import AutoTokenizer
import torch
token = ['hello']
model = BertModel.from_pretrained("bert-base-uncased").eval()
tok = AutoTokenizer.from_pretrained("bert-base-uncased")
def abcd():
    with torch.no_grad():
        
        out = model(**tok(token,return_tensors='pt'))[0]
        # out = tok(token)
    return out
from datasets.fingerprint import Hasher
my_func = abcd
print(Hasher.hash(my_func))

print(abcd())

```

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/52888_2.png) [@lhoestq](https://discuss.huggingface.co/u/lhoestq)\
**Post date:** [October 27, 2022, 8:52am UTC](https://discuss.huggingface.co/t/dataset-cant-cache-models-outputs/24945/2 "2022-10-27T08:52:09Z")

</div>

It looks like the model doesn’t have a deterministic hash: every time I run `Hasher.hash(model)` I get a different hash.

If the hash is not deterministic, then the fingerprint of the processed dataset is not deterministic either - so the cache can’t reload previously computed results, see more info in the [docs](https://huggingface.co/docs/datasets/v2.6.1/en/about_cache#fingerprint)

However you can specify a the processed dataset’s fingerprint yourself by passing `new_fingerprint=` to `map`

```python
ds = ds.map(my_func, new_fingerprint="hash_that_identifies_my_func_and_my_dataset")

```

be careful using this though - if you change `my_func` or your input dataset you have to change `new_fingerprint` to a new value - otherwise the cache will reload the result from previous computations

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/52888_2.png) [@lhoestq](https://discuss.huggingface.co/u/lhoestq)\
**Post date:** [October 27, 2022, 9:16am UTC](https://discuss.huggingface.co/t/dataset-cant-cache-models-outputs/24945/3 "2022-10-27T09:16:07Z")

</div>

I opened an issue to improve the hashing of pytorch tensors: [[Caching] Deterministic hashing of torch tensors · Issue #5170 · huggingface/datasets · GitHub](https://github.com/huggingface/datasets/issues/5170)

---

<div class="post-metadata">

**Author:** ![jongjyh](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/jongjyh/32/24621_2.png) [@jongjyh](https://discuss.huggingface.co/u/jongjyh)\
**Post date:** [October 27, 2022, 11:19am UTC](https://discuss.huggingface.co/t/dataset-cant-cache-models-outputs/24945/4 "2022-10-27T11:19:55Z")

</div>

> [@lhoestq](#):
>
> [Caching] Deterministic hashing of torch tensors · Issue #5170 · huggingface/datasets · GitHub

Thanks! it works.
