Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

[RAG] Update RAG readme #3917

Merged
merged 2 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions parlai/agents/rag/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ python generate_dense_embeddings.py -mf zoo:hallucination/multiset_dpr/hf_bert_b
--outfile /tmp/wiki_passage_embeddings/wiki_passages --num-shards 50 --shard-id 0 -bs 32
```

**`--num-shards`**: If your dataset is relatively small, you can feel free to only generate with only one shard.

### 3. Index the Dense Embeddings

The final step is to build the full FAISS index from these dense embeddings. You can use the [`index_dense_embeddings.py`](https://github.com/facebookresearch/ParlAI/blob/master/parlai/agents/rag/scripts/index_dense_embeddings.py) script to achieve this. You can choose one of the following options when indexing your embeddings for varying results, depending on the size of your dataset:

1. **Recommended** `--indexer-type compressed`: This will build a compressed index using FAISS compression techniques; this usually only takes a couple hours, and results in small index files, but comes at the cost of accuracy.
2. `--indexer-type exact`: This will build a large HNSW-style index with the flat embeddings. The index that is built is generally as large, if not more so, than the sum of the sizes of the embeddings. Use with caution.
1. **Recommended for large passage sets** `--indexer-type compressed`: This will build a compressed index using FAISS compression techniques; this usually only takes a couple hours, and results in small index files, but comes at the cost of accuracy. Only use this if your machine would struggle to fit all of your dense embedding vectors in memory.
2. **Recommended for small passage sets** `--indexer-type exact`: This will build a large HNSW-style index with the flat embeddings. The index that is built is generally as large, if not more so, than the sum of the sizes of the embeddings. Use with caution with large passage sets; however, if you can reasonably fit all of your dense embedding vectors in memory, this is a suitable option.
3. `--indexer-type compressed --compressed-indexer-factory <index_factory>`: If you know what you're doing (and understand how to use the [index factory in FAISS](https://github.com/facebookresearch/faiss/wiki/The-index-factory)), feel free to specify your own Index Factory settings. This method is only recommended if you're an advanced FAISS user.

If we saved our embedding shards at `/path/to/saved/embeddings_0`, the script is used as follows:
Expand Down
4 changes: 2 additions & 2 deletions parlai/agents/rag/retrievers.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,12 @@ def index_retrieve(
# recompute exact FAISS scores
scores = torch.bmm(query.unsqueeze(1), vectors.transpose(1, 2)).squeeze(1)
if torch.isnan(scores).sum().item():
logging.error(
raise RuntimeError(
'\n[ Document scores are NaN; please look into the built index. ]\n'
'[ This generally happens if FAISS cannot separate vectors appropriately. ]\n'
'[ If using a compressed index, try building an exact index: ]\n'
'[ $ python index_dense_embeddings --indexer-type exact... ]'
)
scores[scores != scores] = 1
ids = torch.tensor([[int(s) for s in ss] for ss in ids])

return ids, scores
Expand Down