Can we parallelized the add_faiss_index process over dataset shards ?
MEMBER
Hi !
As far as I know not all faiss indexes can be computed in parallel and then merged. 
For example [here](https://github.com/facebookresearch/faiss/wiki/Special-operations-on-indexes#splitting-and-merging-indexes) is is mentioned that only IndexIVF indexes can be merged.
Moreover faiss already works using multithreading to parallelize the workload over your different CPU cores. You can find more info [here](https://github.com/facebookresearch/faiss/wiki/Threads-and-asynchronous-calls#internal-threading)
So I feel like the gains we would get by implementing a parallel `add_faiss_index` would not be that important, but let me know what you think.

2021-03-02T12:58:04Z
https://github.com/huggingface/datasets/issues/1965#issuecomment-788889763
https://api.github.com/repos/huggingface/datasets/issues/1965
MDEyOklzc3VlQ29tbWVudDc4ODg4OTc2Mw==
https://api.github.com/repos/huggingface/datasets/issues/comments/788889763/reactions
2021-03-02T12:58:04Z
https://api.github.com/repos/huggingface/datasets/issues/comments/788889763
https://avatars.githubusercontent.com/u/42851186?v=4
https://api.github.com/users/lhoestq/events{/privacy}
https://api.github.com/users/lhoestq/followers
https://api.github.com/users/lhoestq/following{/other_user}
https://api.github.com/users/lhoestq/gists{/gist_id}
https://github.com/lhoestq
lhoestq
MDQ6VXNlcjQyODUxMTg2
https://api.github.com/users/lhoestq/orgs
https://api.github.com/users/lhoestq/received_events
https://api.github.com/users/lhoestq/repos
https://api.github.com/users/lhoestq/starred{/owner}{/repo}
https://api.github.com/users/lhoestq/subscriptions
User
https://api.github.com/users/lhoestq
public
NONE
Actually, you are right. I also had the same idea. I am trying this in the context of end-ton-end retrieval training in RAG.  So far I have parallelized the embedding re-computation within the training loop by using datasets shards. 

Then I was thinking of can I calculate the indexes for each shard and combined them with **concatenate**  before I save.
2021-03-03T01:32:55Z
https://github.com/huggingface/datasets/issues/1965#issuecomment-789356485
https://api.github.com/repos/huggingface/datasets/issues/1965
MDEyOklzc3VlQ29tbWVudDc4OTM1NjQ4NQ==
https://api.github.com/repos/huggingface/datasets/issues/comments/789356485/reactions
2021-03-03T01:32:55Z
https://api.github.com/repos/huggingface/datasets/issues/comments/789356485
https://avatars.githubusercontent.com/u/16892570?v=4
https://api.github.com/users/shamanez/events{/privacy}
https://api.github.com/users/shamanez/followers
https://api.github.com/users/shamanez/following{/other_user}
https://api.github.com/users/shamanez/gists{/gist_id}
https://github.com/shamanez
shamanez
MDQ6VXNlcjE2ODkyNTcw
https://api.github.com/users/shamanez/orgs
https://api.github.com/users/shamanez/received_events
https://api.github.com/users/shamanez/repos
https://api.github.com/users/shamanez/starred{/owner}{/repo}
https://api.github.com/users/shamanez/subscriptions
User
https://api.github.com/users/shamanez
public
NONE
@lhoestq  As you mentioned faiss is already using multiprocessing. I tried to do the add_index with faiss for a dataset object inside a RAY actor and the process became very slow... if fact it takes so much time. It is because a ray actor comes with a single CPU core unless we assign it more. I also tried assigning more cores but still running add_index in the main process is very fast. 
2021-03-04T19:40:42Z
https://github.com/huggingface/datasets/issues/1965#issuecomment-790878778
https://api.github.com/repos/huggingface/datasets/issues/1965
MDEyOklzc3VlQ29tbWVudDc5MDg3ODc3OA==
https://api.github.com/repos/huggingface/datasets/issues/comments/790878778/reactions
2021-03-04T19:40:56Z
https://api.github.com/repos/huggingface/datasets/issues/comments/790878778
https://avatars.githubusercontent.com/u/16892570?v=4
https://api.github.com/users/shamanez/events{/privacy}
https://api.github.com/users/shamanez/followers
https://api.github.com/users/shamanez/following{/other_user}
https://api.github.com/users/shamanez/gists{/gist_id}
https://github.com/shamanez
shamanez
MDQ6VXNlcjE2ODkyNTcw
https://api.github.com/users/shamanez/orgs
https://api.github.com/users/shamanez/received_events
https://api.github.com/users/shamanez/repos
https://api.github.com/users/shamanez/starred{/owner}{/repo}
https://api.github.com/users/shamanez/subscriptions
User
https://api.github.com/users/shamanez
public
I am thinking of making the  **add_faiss_index** process faster. What if we run the add_faiss_index process on separate dataset shards and then combine them before (dataset.concatenate) saving the faiss.index file ?

I feel theoretically this will reduce the accuracy of retrieval since it affects the indexing process.

@lhoestq
