# datasets.Dataset.get\_nearest\_examples() on GPU

**URL:** <https://discuss.huggingface.co/t/datasets-dataset-get-nearest-examples-on-gpu/1348>\
**Category:** 🤗Datasets\
**Created:** [October 2, 2020, 10:48pm UTC](https://discuss.huggingface.co/t/datasets-dataset-get-nearest-examples-on-gpu/1348 "2020-10-02T22:48:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndreGodinho](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/andregodinho/32/3005_2.png) [@AndreGodinho](https://discuss.huggingface.co/u/AndreGodinho)\
**Post date:** [October 2, 2020, 10:48pm UTC](https://discuss.huggingface.co/t/datasets-dataset-get-nearest-examples-on-gpu/1348/1 "2020-10-02T22:48:04Z")

</div>

Hello everyone,

Is it possible to put get\_nearest\_examples() computation on GPU after having added a [FAISS index](https://huggingface.co/docs/datasets/faiss_and_ea.html)?

---

<div class="post-metadata">

**Author:** ![BramVanroy](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/bramvanroy/32/47360_2.png) [@BramVanroy](https://discuss.huggingface.co/u/BramVanroy)\
**Post date:** [October 3, 2020, 10:17am UTC](https://discuss.huggingface.co/t/datasets-dataset-get-nearest-examples-on-gpu/1348/2 "2020-10-03T10:17:41Z")

</div>

You can specify the GPU ID when you instantiated the index. Will use the CPU by default (`None`).

```python
add_faiss_index(column, device=0)

```

---

<div class="post-metadata">

**Author:** ![AndreGodinho](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/andregodinho/32/3005_2.png) [@AndreGodinho](https://discuss.huggingface.co/u/AndreGodinho)\
**Post date:** [October 3, 2020, 10:44am UTC](https://discuss.huggingface.co/t/datasets-dataset-get-nearest-examples-on-gpu/1348/3 "2020-10-03T10:44:53Z")

</div>

Thank you, Bram. Tried a similar solution when loading my faiss\_index.

However, throws an error:` AttributeError: module 'faiss' has no attribute 'StandardGpuResources`

I have created a topic in facebookresearch/faiss repository - [AttributeError: module ‘faiss’ has no attribute ‘StandardGpuResources’ on adding FAISS index to Hugging face Dataset](https://github.com/facebookresearch/faiss/issues/1440).

---

<div class="post-metadata">

**Author:** ![BramVanroy](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/bramvanroy/32/47360_2.png) [@BramVanroy](https://discuss.huggingface.co/u/BramVanroy)\
**Post date:** [October 3, 2020, 4:32pm UTC](https://discuss.huggingface.co/t/datasets-dataset-get-nearest-examples-on-gpu/1348/4 "2020-10-03T16:32:42Z")

</div>

You probably installed a prebuilt faiss-cpu rather than faiss-gpu. Remove that one and install faiss-gpu instead.

---

<div class="post-metadata">

**Author:** ![AndreGodinho](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/andregodinho/32/3005_2.png) [@AndreGodinho](https://discuss.huggingface.co/u/AndreGodinho)\
**Post date:** [October 4, 2020, 9:19am UTC](https://discuss.huggingface.co/t/datasets-dataset-get-nearest-examples-on-gpu/1348/5 "2020-10-04T09:19:49Z")

</div>

Thank you Bram. I have some updates.

[FAISS installation](https://github.com/facebookresearch/faiss/blob/master/INSTALL.md) (1)

1. Not having faiss-cpu, just having faiss-gpu. Installed it by:  
` conda install faiss-gpu cudatoolkit=10.2 -c pytorch`

According to (1), it should be cudatoolkit=10.0, but that leads to downgrades that can lead to future problems.

 ![down-grades](https://us1.discourse-cdn.com/hellohellohello/original/2X/4/460ef3e7cc876f88754892d04d2f1b5b3ce9546e.png)

throws  
`AssertionError: You must install Faiss to use FaissIndex. To do so you can run pip install faiss-cpu or pip install faiss-gpu`

1. Not having faiss-cpu, just having faiss-gpu. Installed it by

`pip install faiss-gpu`

Problem solved.

Cheers

---

<div class="post-metadata">

**Author:** ![BramVanroy](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/bramvanroy/32/47360_2.png) [@BramVanroy](https://discuss.huggingface.co/u/BramVanroy)\
**Post date:** [October 4, 2020, 10:21am UTC](https://discuss.huggingface.co/t/datasets-dataset-get-nearest-examples-on-gpu/1348/6 "2020-10-04T10:21:31Z")

</div>

I don’t use conda so I’m not sure I can help with that - but if I understand you correctly, the problem has been solved by using `pip install`?

---

<div class="post-metadata">

**Author:** ![AndreGodinho](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/andregodinho/32/3005_2.png) [@AndreGodinho](https://discuss.huggingface.co/u/AndreGodinho)\
**Post date:** [October 4, 2020, 11:19am UTC](https://discuss.huggingface.co/t/datasets-dataset-get-nearest-examples-on-gpu/1348/7 "2020-10-04T11:19:42Z")

</div>

Yes, thank you.
