Crystalcareai commited on
Commit
fc142ce
1 Parent(s): fdc4c73

Upload 8 files

Browse files
README.md CHANGED
@@ -1,3 +1,215 @@
1
  ---
2
  license: mit
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: mit
3
+ language:
4
+ - en
5
+ tags:
6
+ - ColBERT
7
  ---
8
+ <p align="center">
9
+ <img align="center" src="docs/images/colbertofficial.png" width="430px" />
10
+ </p>
11
+ <p align="left">
12
+
13
+ # ColBERT (v2)
14
+
15
+ ### ColBERT is a _fast_ and _accurate_ retrieval model, enabling scalable BERT-based search over large text collections in tens of milliseconds.
16
+
17
+ [<img align="center" src="https://colab.research.google.com/assets/colab-badge.svg" />](https://colab.research.google.com/github/stanford-futuredata/ColBERT/blob/main/docs/intro2new.ipynb)
18
+
19
+
20
+ <p align="center">
21
+ <img align="center" src="docs/images/ColBERT-Framework-MaxSim-W370px.png" />
22
+ </p>
23
+ <p align="center">
24
+ <b>Figure 1:</b> ColBERT's late interaction, efficiently scoring the fine-grained similarity between a queries and a passage.
25
+ </p>
26
+
27
+ As Figure 1 illustrates, ColBERT relies on fine-grained **contextual late interaction**: it encodes each passage into a **matrix** of token-level embeddings (shown above in blue). Then at search time, it embeds every query into another matrix (shown in green) and efficiently finds passages that contextually match the query using scalable vector-similarity (`MaxSim`) operators.
28
+
29
+ These rich interactions allow ColBERT to surpass the quality of _single-vector_ representation models, while scaling efficiently to large corpora. You can read more in our papers:
30
+
31
+ * [**ColBERT: Efficient and Effective Passage Search via Contextualized Late Interaction over BERT**](https://arxiv.org/abs/2004.12832) (SIGIR'20).
32
+ * [**Relevance-guided Supervision for OpenQA with ColBERT**](https://arxiv.org/abs/2007.00814) (TACL'21).
33
+ * [**Baleen: Robust Multi-Hop Reasoning at Scale via Condensed Retrieval**](https://arxiv.org/abs/2101.00436) (NeurIPS'21).
34
+ * [**ColBERTv2: Effective and Efficient Retrieval via Lightweight Late Interaction**](https://arxiv.org/abs/2112.01488) (NAACL'22).
35
+ * [**PLAID: An Efficient Engine for Late Interaction Retrieval**](https://arxiv.org/abs/2205.09707) (CIKM'22).
36
+
37
+ ----
38
+
39
+ ## 🚨 **Announcements**
40
+
41
+ * (1/29/23) We have merged a new index updater feature and support for additional Hugging Face models! These are in beta so please give us feedback as you try them out.
42
+ * (1/24/23) If you're looking for the **DSP** framework for composing ColBERTv2 and LLMs, it's at: https://github.com/stanfordnlp/dsp
43
+
44
+ ----
45
+
46
+ ## ColBERTv1
47
+
48
+ The ColBERTv1 code from the SIGIR'20 paper is in the [`colbertv1` branch](https://github.com/stanford-futuredata/ColBERT/tree/colbertv1). See [here](#branches) for more information on other branches.
49
+
50
+
51
+ ## Installation
52
+
53
+ ColBERT requires Python 3.7+ and Pytorch 1.9+ and uses the [Hugging Face Transformers](https://github.com/huggingface/transformers) library.
54
+
55
+ We strongly recommend creating a conda environment using the commands below. (If you don't have conda, follow the official [conda installation guide](https://docs.anaconda.com/anaconda/install/linux/#installation).)
56
+
57
+ We have also included a new environment file specifically for CPU-only environments (`conda_env_cpu.yml`), but note that if you are testing CPU execution on a machine that includes GPUs you might need to specify `CUDA_VISIBLE_DEVICES=""` as part of your command. Note that a GPU is required for training and indexing.
58
+
59
+ ```
60
+ conda env create -f conda_env[_cpu].yml
61
+ conda activate colbert
62
+ ```
63
+
64
+ If you face any problems, please [open a new issue](https://github.com/stanford-futuredata/ColBERT/issues) and we'll help you promptly!
65
+
66
+
67
+
68
+ ## Overview
69
+
70
+ Using ColBERT on a dataset typically involves the following steps.
71
+
72
+ **Step 0: Preprocess your collection.** At its simplest, ColBERT works with tab-separated (TSV) files: a file (e.g., `collection.tsv`) will contain all passages and another (e.g., `queries.tsv`) will contain a set of queries for searching the collection.
73
+
74
+ **Step 1: Download the [pre-trained ColBERTv2 checkpoint](https://downloads.cs.stanford.edu/nlp/data/colbert/colbertv2/colbertv2.0.tar.gz).** This checkpoint has been trained on the MS MARCO Passage Ranking task. You can also _optionally_ [train your own ColBERT model](#training).
75
+
76
+ **Step 2: Index your collection.** Once you have a trained ColBERT model, you need to [index your collection](#indexing) to permit fast retrieval. This step encodes all passages into matrices, stores them on disk, and builds data structures for efficient search.
77
+
78
+ **Step 3: Search the collection with your queries.** Given the model and index, you can [issue queries over the collection](#retrieval) to retrieve the top-k passages for each query.
79
+
80
+ Below, we illustrate these steps via an example run on the MS MARCO Passage Ranking task.
81
+
82
+
83
+ ## API Usage Notebook
84
+
85
+ **NEW**: We have an experimental notebook on [Google Colab](https://colab.research.google.com/github/stanford-futuredata/ColBERT/blob/main/docs/intro2new.ipynb) that you can use with free GPUs. Indexing 10,000 on the free Colab T4 GPU takes six minutes.
86
+
87
+ This Jupyter notebook **[docs/intro.ipynb notebook](docs/intro.ipynb)** illustrates using the key features of ColBERT with the new Python API.
88
+
89
+ It includes how to download the ColBERTv2 model checkpoint trained on MS MARCO Passage Ranking and how to download our new LoTTE benchmark.
90
+
91
+
92
+ ## Data
93
+
94
+ This repository works directly with a simple **tab-separated file** format to store queries, passages, and top-k ranked lists.
95
+
96
+
97
+ * Queries: each line is `qid \t query text`.
98
+ * Collection: each line is `pid \t passage text`.
99
+ * Top-k Ranking: each line is `qid \t pid \t rank`.
100
+
101
+ This works directly with the data format of the [MS MARCO Passage Ranking](https://github.com/microsoft/MSMARCO-Passage-Ranking) dataset. You will need the training triples (`triples.train.small.tar.gz`), the official top-1000 ranked lists for the dev set queries (`top1000.dev`), and the dev set relevant passages (`qrels.dev.small.tsv`). For indexing the full collection, you will also need the list of passages (`collection.tar.gz`).
102
+
103
+
104
+ ## Indexing
105
+
106
+ For fast retrieval, indexing precomputes the ColBERT representations of passages.
107
+
108
+ Example usage:
109
+
110
+ ```
111
+ from colbert.infra import Run, RunConfig, ColBERTConfig
112
+ from colbert import Indexer
113
+
114
+ if __name__=='__main__':
115
+ with Run().context(RunConfig(nranks=1, experiment="msmarco")):
116
+
117
+ config = ColBERTConfig(
118
+ nbits=2,
119
+ root="/path/to/experiments",
120
+ )
121
+ indexer = Indexer(checkpoint="/path/to/checkpoint", config=config)
122
+ indexer.index(name="msmarco.nbits=2", collection="/path/to/MSMARCO/collection.tsv")
123
+ ```
124
+
125
+
126
+ ## Retrieval
127
+
128
+ We typically recommend that you use ColBERT for **end-to-end** retrieval, where it directly finds its top-k passages from the full collection:
129
+
130
+ ```
131
+ from colbert.data import Queries
132
+ from colbert.infra import Run, RunConfig, ColBERTConfig
133
+ from colbert import Searcher
134
+
135
+ if __name__=='__main__':
136
+ with Run().context(RunConfig(nranks=1, experiment="msmarco")):
137
+
138
+ config = ColBERTConfig(
139
+ root="/path/to/experiments",
140
+ )
141
+ searcher = Searcher(index="msmarco.nbits=2", config=config)
142
+ queries = Queries("/path/to/MSMARCO/queries.dev.small.tsv")
143
+ ranking = searcher.search_all(queries, k=100)
144
+ ranking.save("msmarco.nbits=2.ranking.tsv")
145
+ ```
146
+
147
+ You can optionally specify the `ncells`, `centroid_score_threshold`, and `ndocs` search hyperparameters to trade off between speed and result quality. Defaults for different values of `k` are listed in colbert/searcher.py.
148
+
149
+ We can evaluate the MSMARCO rankings using the following command:
150
+
151
+ ```
152
+ python -m utility.evaluate.msmarco_passages --ranking "/path/to/msmarco.nbits=2.ranking.tsv" --qrels "/path/to/MSMARCO/qrels.dev.small.tsv"
153
+ ```
154
+
155
+ ## Training
156
+
157
+ We provide a [pre-trained model checkpoint](https://downloads.cs.stanford.edu/nlp/data/colbert/colbertv2/colbertv2.0.tar.gz), but we also detail how to train from scratch here.
158
+ Note that this example demonstrates the ColBERTv1 style of training, but the provided checkpoint was trained with ColBERTv2.
159
+
160
+ Training requires a JSONL triples file with a `[qid, pid+, pid-]` list per line. The query IDs and passage IDs correspond to the specified `queries.tsv` and `collection.tsv` files respectively.
161
+
162
+ Example usage (training on 4 GPUs):
163
+
164
+ ```
165
+ from colbert.infra import Run, RunConfig, ColBERTConfig
166
+ from colbert import Trainer
167
+
168
+ if __name__=='__main__':
169
+ with Run().context(RunConfig(nranks=4, experiment="msmarco")):
170
+
171
+ config = ColBERTConfig(
172
+ bsize=32,
173
+ root="/path/to/experiments",
174
+ )
175
+ trainer = Trainer(
176
+ triples="/path/to/MSMARCO/triples.train.small.tsv",
177
+ queries="/path/to/MSMARCO/queries.train.small.tsv",
178
+ collection="/path/to/MSMARCO/collection.tsv",
179
+ config=config,
180
+ )
181
+
182
+ checkpoint_path = trainer.train()
183
+
184
+ print(f"Saved checkpoint to {checkpoint_path}...")
185
+ ```
186
+
187
+ ## Running a lightweight ColBERTv2 server
188
+ We provide a script to run a lightweight server which serves k (upto 100) results in ranked order for a given search query, in JSON format. This script can be used to power DSP programs.
189
+
190
+ To run the server, update the environment variables `INDEX_ROOT` and `INDEX_NAME` in the `.env` file to point to the appropriate ColBERT index. The run the following command:
191
+ ```
192
+ python server.py
193
+ ```
194
+
195
+ A sample query:
196
+ ```
197
+ http://localhost:8893/api/search?query=Who won the 2022 FIFA world cup&k=25
198
+ ```
199
+
200
+ ## Branches
201
+
202
+ ### Supported branches
203
+
204
+ * [`main`](https://github.com/stanford-futuredata/ColBERT/tree/main): Stable branch with ColBERTv2 + PLAID.
205
+ * [`colbertv1`](https://github.com/stanford-futuredata/ColBERT/tree/colbertv1): Legacy branch for ColBERTv1.
206
+
207
+ ### Deprecated branches
208
+ * [`new_api`](https://github.com/stanford-futuredata/ColBERT/tree/new_api): Base ColBERTv2 implementation.
209
+ * [`cpu_inference`](https://github.com/stanford-futuredata/ColBERT/tree/cpu_inference): ColBERTv2 implementation with CPU search support.
210
+ * [`fast_search`](https://github.com/stanford-futuredata/ColBERT/tree/fast_search): ColBERTv2 implementation with PLAID.
211
+ * [`binarization`](https://github.com/stanford-futuredata/ColBERT/tree/binarization): ColBERT with a baseline binarization-based compression strategy (as opposed to ColBERTv2's residual compression, which we found to be more robust).
212
+
213
+ ## Acknowledgments
214
+
215
+ ColBERT logo designed by Chuyi Zhang.
artifact.metadata ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nprobe": 2,
3
+ "ncandidates": 8192,
4
+ "index_path": null,
5
+ "nbits": 1,
6
+ "kmeans_niters": 20,
7
+ "similarity": "cosine",
8
+ "bsize": 8,
9
+ "accumsteps": 1,
10
+ "lr": 1e-5,
11
+ "maxsteps": 400000,
12
+ "save_every": null,
13
+ "resume": false,
14
+ "warmup": 20000,
15
+ "warmup_bert": null,
16
+ "relu": false,
17
+ "nway": 64,
18
+ "use_ib_negatives": true,
19
+ "query_maxlen": 32,
20
+ "attend_to_mask_tokens": false,
21
+ "dim": 128,
22
+ "doc_maxlen": 180,
23
+ "mask_punctuation": true,
24
+ "checkpoint": "\/future\/u\/okhattab\/root\/unit\/experiments\/2021.10\/none\/kld.v3.nway32.l2.ib\/checkpoints\/colbert-150000\/",
25
+ "triples": "\/future\/u\/okhattab\/root\/unit\/experiments\/2021.10\/downstream.distillation.round2.2_score\/round2.nway6.cosine.ib\/examples.64.json",
26
+ "collection": "\/future\/u\/okhattab\/data\/MSMARCO\/collection.tsv",
27
+ "queries": "\/future\/u\/okhattab\/data\/MSMARCO\/queries.train.tsv",
28
+ "index_name": null,
29
+ "overwrite": false,
30
+ "root": "\/future\/u\/okhattab\/root\/unit\/experiments",
31
+ "experiment": "2021.10",
32
+ "index_root": null,
33
+ "name": "kldR2.nway64.ib",
34
+ "rank": 0,
35
+ "nranks": 4,
36
+ "amp": true,
37
+ "gpus": 8,
38
+ "meta": {
39
+ "hostname": "future-hgx-1.stanford.edu",
40
+ "git_branch": "api_residual_compression3_KLD",
41
+ "git_hash": "62198f65e45bc7887eda4fdd57f9d3a103c72a7b",
42
+ "git_commit_datetime": "2021-10-29 07:05:32-07:00",
43
+ "current_datetime": "Oct 29, 2021 ; 9:05AM PDT (-0700)",
44
+ "cmd": "\/future\/u\/okhattab\/repos\/QolBERT2\/tests\/train_kldR2_nway64.py",
45
+ "version": "colbert-v0.4"
46
+ }
47
+ }
config.json ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "/future/u/okhattab/root/unit/experiments/2021.10/none/kld.v3.nway32.l2.ib/checkpoints/colbert-150000/",
3
+ "architectures": [
4
+ "HF_ColBERT"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "classifier_dropout": null,
8
+ "gradient_checkpointing": false,
9
+ "hidden_act": "gelu",
10
+ "hidden_dropout_prob": 0.1,
11
+ "hidden_size": 768,
12
+ "initializer_range": 0.02,
13
+ "intermediate_size": 3072,
14
+ "layer_norm_eps": 1e-12,
15
+ "max_position_embeddings": 512,
16
+ "model_type": "bert",
17
+ "num_attention_heads": 12,
18
+ "num_hidden_layers": 12,
19
+ "pad_token_id": 0,
20
+ "position_embedding_type": "absolute",
21
+ "torch_dtype": "float32",
22
+ "transformers_version": "4.10.0",
23
+ "type_vocab_size": 2,
24
+ "use_cache": true,
25
+ "vocab_size": 30522
26
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:26e4c2f9f95a3da4442252bb40d99e4fbfd098e733edac1d785c9937b8a278da
3
+ size 438405935
special_tokens_map.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]"}
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"do_lower_case": true, "unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]", "tokenize_chinese_chars": true, "strip_accents": null, "model_max_length": 512, "special_tokens_map_file": null, "name_or_path": "/future/u/okhattab/root/unit/experiments/2021.10/none/kld.v3.nway32.l2.ib/checkpoints/colbert-150000/", "tokenizer_class": "BertTokenizer"}
vocab.txt ADDED
The diff for this file is too large to render. See raw diff