初始化项目,由ModelHub XC社区提供模型
Model: flax-sentence-embeddings/all_datasets_v3_roberta-large Source: Original Platform
This commit is contained in:
17
.gitattributes
vendored
Normal file
17
.gitattributes
vendored
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
*.bin.* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.bin filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.h5 filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tflite filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tar.gz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ot filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.onnx filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.arrow filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ftz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.joblib filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.model filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pb filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pt filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pth filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
||||||
7
1_Pooling/config.json
Executable file
7
1_Pooling/config.json
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"word_embedding_dimension": 1024,
|
||||||
|
"pooling_mode_cls_token": false,
|
||||||
|
"pooling_mode_mean_tokens": true,
|
||||||
|
"pooling_mode_max_tokens": false,
|
||||||
|
"pooling_mode_mean_sqrt_len_tokens": false
|
||||||
|
}
|
||||||
97
README.md
Executable file
97
README.md
Executable file
@@ -0,0 +1,97 @@
|
|||||||
|
---
|
||||||
|
pipeline_tag: sentence-similarity
|
||||||
|
tags:
|
||||||
|
- sentence-transformers
|
||||||
|
- feature-extraction
|
||||||
|
- sentence-similarity
|
||||||
|
language: en
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
# Model description
|
||||||
|
|
||||||
|
The project aims to train sentence embedding models on very large sentence level datasets using a self-supervised
|
||||||
|
contrastive learning objective. We used the pretrained [`roberta-large`](https://huggingface.co/roberta-large) model and fine-tuned in on a
|
||||||
|
1B sentence pairs dataset. We use a contrastive learning objective: given a sentence from the pair, the model should predict which out of a set of randomly sampled other sentences, was actually paired with it in our dataset.
|
||||||
|
|
||||||
|
We developped this model during the
|
||||||
|
[Community week using JAX/Flax for NLP & CV](https://discuss.huggingface.co/t/open-to-the-community-community-week-using-jax-flax-for-nlp-cv/7104),
|
||||||
|
organized by Hugging Face. We developped this model as part of the project:
|
||||||
|
[Train the Best Sentence Embedding Model Ever with 1B Training Pairs](https://discuss.huggingface.co/t/train-the-best-sentence-embedding-model-ever-with-1b-training-pairs/7354). We benefited from efficient hardware infrastructure to run the project: 7 TPUs v3-8, as well
|
||||||
|
as intervention from Google’s Flax, JAX, and Cloud team member about efficient deep learning frameworks.
|
||||||
|
|
||||||
|
## Intended uses
|
||||||
|
|
||||||
|
Our model is intented to be used as a sentence encoder. Given an input sentence, it ouptuts a vector which captures
|
||||||
|
the sentence semantic information. The sentence vector may be used for information retrieval, clustering or sentence
|
||||||
|
similarity tasks.
|
||||||
|
|
||||||
|
## How to use
|
||||||
|
|
||||||
|
Here is how to use this model to get the features of a given text using [SentenceTransformers](https://github.com/UKPLab/sentence-transformers) library:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from sentence_transformers import SentenceTransformer
|
||||||
|
|
||||||
|
model = SentenceTransformer('flax-sentence-embeddings/all_datasets_v3_roberta-large')
|
||||||
|
text = "Replace me by any text you'd like."
|
||||||
|
text_embbedding = model.encode(text)
|
||||||
|
# array([-0.01559514, 0.04046123, 0.1317083 , 0.00085931, 0.04585106,
|
||||||
|
# -0.05607086, 0.0138078 , 0.03569756, 0.01420381, 0.04266302 ...],
|
||||||
|
# dtype=float32)
|
||||||
|
```
|
||||||
|
|
||||||
|
# Training procedure
|
||||||
|
|
||||||
|
## Pre-training
|
||||||
|
|
||||||
|
We use the pretrained [`roberta-large`](https://huggingface.co/roberta-large). Please refer to the model
|
||||||
|
card for more detailed information about the pre-training procedure.
|
||||||
|
|
||||||
|
## Fine-tuning
|
||||||
|
|
||||||
|
We fine-tune the model using a contrastive objective. Formally, we compute the cosine similarity from each possible sentence pairs from the batch.
|
||||||
|
We then apply the cross entropy loss by comparing with true pairs.
|
||||||
|
|
||||||
|
### Hyper parameters
|
||||||
|
|
||||||
|
We trained ou model on a TPU v3-8. We train the model during 540k steps using a batch size of 1024 (128 per TPU core).
|
||||||
|
We use a learning rate warm up of 500. The sequence length was limited to 128 tokens. We used the AdamW optimizer with
|
||||||
|
a 2e-5 learning rate. The full training script is accessible in this current repository.
|
||||||
|
|
||||||
|
### Training data
|
||||||
|
|
||||||
|
We use the concatenation from multiple datasets to fine-tune our model. The total number of sentence pairs is above 1 billion sentences.
|
||||||
|
We sampled each dataset given a weighted probability which configuration is detailed in the `data_config.json` file.
|
||||||
|
|
||||||
|
|
||||||
|
| Dataset | Paper | Number of training tuples |
|
||||||
|
|:--------------------------------------------------------:|:----------------------------------------:|:--------------------------:|
|
||||||
|
| [GOOAQ: Open Question Answering with Diverse Answer Types](https://github.com/allenai/gooaq) | [paper](https://arxiv.org/pdf/2104.08727.pdf) | 3,012,496 |
|
||||||
|
| [Stack Exchange](https://huggingface.co/datasets/flax-sentence-embeddings/stackexchange_title_body_jsonl) | - | 364,001 |
|
||||||
|
| [Flickr 30k](https://shannon.cs.illinois.edu/DenotationGraph/) | [paper](https://transacl.org/ojs/index.php/tacl/article/view/229/33) | 317,695 |
|
||||||
|
| [COCO 2020](COCO 2020) | [paper](https://link.springer.com/chapter/10.1007%2F978-3-319-10602-1_48) | 828,395|
|
||||||
|
| [Code Search](https://huggingface.co/datasets/code_search_net) | - | 1,151,414 |
|
||||||
|
| [TriviaqQA](https://huggingface.co/datasets/trivia_qa) | - | 73,346 |
|
||||||
|
| [SQuAD2.0](https://rajpurkar.github.io/SQuAD-explorer/) | [paper](https://aclanthology.org/P18-2124.pdf) | 87,599 |
|
||||||
|
| [Natural Questions (NQ)](https://ai.google.com/research/NaturalQuestions) | [paper](https://transacl.org/ojs/index.php/tacl/article/view/1455) | 100,231 |
|
||||||
|
| [Simple Wikipedia](https://cs.pomona.edu/~dkauchak/simplification/) | [paper](https://www.aclweb.org/anthology/P11-2117/) | 102,225 |
|
||||||
|
| [Quora Question Pairs](https://quoradata.quora.com/First-Quora-Dataset-Release-Question-Pairs) | - | 103,663 |
|
||||||
|
| [Altlex](https://github.com/chridey/altlex/) | [paper](https://aclanthology.org/P16-1135.pdf) | 112,696 |
|
||||||
|
| [Wikihow](https://github.com/pvl/wikihow_pairs_dataset) | [paper](https://arxiv.org/abs/1810.09305) | 128,542 |
|
||||||
|
| [Sentence Compression](https://github.com/google-research-datasets/sentence-compression) | [paper](https://www.aclweb.org/anthology/D13-1155/) | 180,000 |
|
||||||
|
| AllNLI ([SNLI](https://nlp.stanford.edu/projects/snli/) and [MultiNLI](https://cims.nyu.edu/~sbowman/multinli/) | [paper SNLI](https://doi.org/10.18653/v1/d15-1075), [paper MultiNLI](https://doi.org/10.18653/v1/n18-1101) | 277,230 |
|
||||||
|
| [Eli5](https://huggingface.co/datasets/eli5) | [paper](https://doi.org/10.18653/v1/p19-1346) | 325,475 |
|
||||||
|
| [SPECTER](https://github.com/allenai/specter) | [paper](https://doi.org/10.18653/v1/2020.acl-main.207) | 684,100 |
|
||||||
|
| [S2ORC](https://github.com/allenai/s2orc) Title/Abstract | [paper](https://aclanthology.org/2020.acl-main.447/) | 41,769,185 |
|
||||||
|
| [S2ORC](https://github.com/allenai/s2orc) Citation/Citation | [paper](https://aclanthology.org/2020.acl-main.447/) | 52,603,982 |
|
||||||
|
| [S2ORC](https://github.com/allenai/s2orc) Citation/Abstract | [paper](https://aclanthology.org/2020.acl-main.447/) | 116,288,806 |
|
||||||
|
| [PAQ](https://github.com/facebookresearch/PAQ) | [paper](https://arxiv.org/abs/2102.07033) | 64,371,441 |
|
||||||
|
| [WikiAnswers](https://github.com/afader/oqa#wikianswers-corpus) | [paper](https://doi.org/10.1145/2623330.2623677) | 77,427,422 |
|
||||||
|
| SearchQA | - | 582,261 |
|
||||||
|
| [Yahoo Answers](https://www.kaggle.com/soumikrakshit/yahoo-answers-dataset) Title/Answer | [paper](https://proceedings.neurips.cc/paper/2015/hash/250cf8b51c773f3f8dc8b4be867a9a02-Abstract.html) | 1,198,260 |
|
||||||
|
| [Yahoo Answers](https://www.kaggle.com/soumikrakshit/yahoo-answers-dataset) Title/Question | [paper](https://proceedings.neurips.cc/paper/2015/hash/250cf8b51c773f3f8dc8b4be867a9a02-Abstract.html) | 659,896 |
|
||||||
|
| [Yahoo Answers](https://www.kaggle.com/soumikrakshit/yahoo-answers-dataset) Question/Answer | [paper](https://proceedings.neurips.cc/paper/2015/hash/250cf8b51c773f3f8dc8b4be867a9a02-Abstract.html) | 681,164 |
|
||||||
|
| [MS MARCO](https://microsoft.github.io/msmarco/) | [paper](https://doi.org/10.1145/3404835.3462804) | 9,144,553 |
|
||||||
|
| [Reddit conversationnal](https://github.com/PolyAI-LDN/conversational-datasets/tree/master/reddit) | [paper](https://arxiv.org/abs/1904.06472) | 726,484,430 |
|
||||||
|
| total | | 1,097,953,922 |
|
||||||
26
config.json
Normal file
26
config.json
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"_name_or_path": "roberta-large",
|
||||||
|
"architectures": [
|
||||||
|
"RobertaForMaskedLM"
|
||||||
|
],
|
||||||
|
"attention_probs_dropout_prob": 0.1,
|
||||||
|
"bos_token_id": 0,
|
||||||
|
"eos_token_id": 2,
|
||||||
|
"gradient_checkpointing": false,
|
||||||
|
"hidden_act": "gelu",
|
||||||
|
"hidden_dropout_prob": 0.1,
|
||||||
|
"hidden_size": 1024,
|
||||||
|
"initializer_range": 0.02,
|
||||||
|
"intermediate_size": 4096,
|
||||||
|
"layer_norm_eps": 1e-05,
|
||||||
|
"max_position_embeddings": 514,
|
||||||
|
"model_type": "roberta",
|
||||||
|
"num_attention_heads": 16,
|
||||||
|
"num_hidden_layers": 24,
|
||||||
|
"pad_token_id": 1,
|
||||||
|
"position_embedding_type": "absolute",
|
||||||
|
"transformers_version": "4.8.2",
|
||||||
|
"type_vocab_size": 1,
|
||||||
|
"use_cache": true,
|
||||||
|
"vocab_size": 50265
|
||||||
|
}
|
||||||
7
config_sentence_transformers.json
Executable file
7
config_sentence_transformers.json
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"__version__": {
|
||||||
|
"sentence_transformers": "2.0.0",
|
||||||
|
"transformers": "4.6.1",
|
||||||
|
"pytorch": "1.8.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
601
data_config.json
Executable file
601
data_config.json
Executable file
@@ -0,0 +1,601 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/skeptics.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 10009,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/writers.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 10157,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/astronomy.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 10462,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/vi.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 10551,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/cstheory.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 10642,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/engineering.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 10753,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/french.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 10794,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/economics.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 11115,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/anime.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 11444,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/islam.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 11853,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/expressionengine.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 11866,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/politics.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 11894,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/history.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 12021,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/christianity.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 12108,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/boardgames.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 12149,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/civicrm.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 12543,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/craftcms.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 12574,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/hinduism.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 13450,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/networkengineering.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 13454,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/german.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 13950,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/philosophy.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 14829,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/gardening.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 15136,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/space.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 15142,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/bicycles.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 16353,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/quant.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 17261,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/puzzling.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 17851,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/law.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 17941,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/arduino.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 19553,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/aviation.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 20139,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/softwarerecs.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 20142,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/movies.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 20181,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/music.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 20636,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/emacs.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 21055,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/dsp.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 21252,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "flickr30k_captions.jsonl.gz",
|
||||||
|
"lines": 317695,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "coco_captions.jsonl.gz",
|
||||||
|
"lines": 828395,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "codesearchnet.jsonl.gz",
|
||||||
|
"lines": 1151414,
|
||||||
|
"weight": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/japanese.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 22056,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/mechanics.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 22868,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/crypto.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 23231,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/cooking.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 23705,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/photo.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 23753,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/workplace.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 24189,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/biology.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 24447,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/bitcoin.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 25374,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/worldbuilding.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 26763,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/datascience.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 27397,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/ux.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 29403,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/webapps.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 29697,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/graphicdesign.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 30233,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/raspberrypi.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 30625,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/money.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 32021,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/judaism.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 32028,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/ethereum.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 32760,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/academia.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 34331,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/chemistry.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 34506,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/webmasters.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 34559,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/meta.stackoverflow.com.jsonl.gz",
|
||||||
|
"lines": 36456,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/cs.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 38314,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/travel.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 41227,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/rpg.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 42303,
|
||||||
|
"weight": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/codereview.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 45765,
|
||||||
|
"weight": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/gamedev.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 46485,
|
||||||
|
"weight": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/android.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 51608,
|
||||||
|
"weight": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/softwareengineering.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 53942,
|
||||||
|
"weight": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/security.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 58000,
|
||||||
|
"weight": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/diy.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 60083,
|
||||||
|
"weight": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/scifi.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 61528,
|
||||||
|
"weight": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/mathematica.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 73131,
|
||||||
|
"weight": 4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "TriviaQA_pairs.jsonl.gz",
|
||||||
|
"lines": 73346,
|
||||||
|
"weight": 4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/drupal.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 79717,
|
||||||
|
"weight": 4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/blender.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 80766,
|
||||||
|
"weight": 4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/dba.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 81871,
|
||||||
|
"weight": 4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/ell.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 83271,
|
||||||
|
"weight": 4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/meta.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 83510,
|
||||||
|
"weight": 4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "squad_pairs.jsonl.gz",
|
||||||
|
"lines": 87599,
|
||||||
|
"weight": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/gaming.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 88912,
|
||||||
|
"weight": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/sharepoint.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 94011,
|
||||||
|
"weight": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/magento.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 99991,
|
||||||
|
"weight": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "NQ-train_pairs.jsonl.gz",
|
||||||
|
"lines": 100231,
|
||||||
|
"weight": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/wordpress.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 100474,
|
||||||
|
"weight": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "SimpleWiki.jsonl.gz",
|
||||||
|
"lines": 102225,
|
||||||
|
"weight": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "quora_duplicates_triplets.jsonl.gz",
|
||||||
|
"lines": 103663,
|
||||||
|
"weight": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/salesforce.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 105260,
|
||||||
|
"weight": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/english.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 109522,
|
||||||
|
"weight": 6
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/apple.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 110622,
|
||||||
|
"weight": 6
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "altlex.jsonl.gz",
|
||||||
|
"lines": 112696,
|
||||||
|
"weight": 6
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/mathoverflow.net.jsonl.gz",
|
||||||
|
"lines": 120851,
|
||||||
|
"weight": 6
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "wikihow.jsonl.gz",
|
||||||
|
"lines": 128542,
|
||||||
|
"weight": 6
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/gis.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 131000,
|
||||||
|
"weight": 7
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/electronics.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 143582,
|
||||||
|
"weight": 7
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/physics.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 173307,
|
||||||
|
"weight": 9
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/stats.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 173466,
|
||||||
|
"weight": 9
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "sentence-compression.jsonl.gz",
|
||||||
|
"lines": 180000,
|
||||||
|
"weight": 9
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/unix.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 185997,
|
||||||
|
"weight": 9
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/tex.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 202954,
|
||||||
|
"weight": 10
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_duplicate_questions_title-body_title-body.jsonl.gz",
|
||||||
|
"lines": 250460,
|
||||||
|
"weight": 12
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_duplicate_questions_body_body.jsonl.gz",
|
||||||
|
"lines": 250519,
|
||||||
|
"weight": 12
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/serverfault.com.jsonl.gz",
|
||||||
|
"lines": 270904,
|
||||||
|
"weight": 13
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "AllNLI.jsonl.gz",
|
||||||
|
"lines": 277230,
|
||||||
|
"weight": 13
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_duplicate_questions_title_title.jsonl.gz",
|
||||||
|
"lines": 304525,
|
||||||
|
"weight": 15
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "eli5_question_answer.jsonl.gz",
|
||||||
|
"lines": 325475,
|
||||||
|
"weight": 16
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "specter_train_triples.jsonl.gz",
|
||||||
|
"lines": 684100,
|
||||||
|
"weight": 16
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/askubuntu.com.jsonl.gz",
|
||||||
|
"lines": 347925,
|
||||||
|
"weight": 17
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/superuser.com.jsonl.gz",
|
||||||
|
"lines": 435463,
|
||||||
|
"weight": 21
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/small_stackexchanges.jsonl.gz",
|
||||||
|
"lines": 448146,
|
||||||
|
"weight": 21
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "S2ORC_title_abstract.jsonl.gz",
|
||||||
|
"lines": 41769185,
|
||||||
|
"weight": 23
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "S2ORC_citation_pairs.jsonl.gz",
|
||||||
|
"lines": 52603982,
|
||||||
|
"weight": 12
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "S2ORC_citation_pairs_abstract.jsonl.gz",
|
||||||
|
"lines": 116288806,
|
||||||
|
"weight": 12
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "PAQ_pairs.jsonl.gz",
|
||||||
|
"lines": 64371441,
|
||||||
|
"weight": 23
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "WikiAnswers_pairs.jsonl.gz",
|
||||||
|
"lines": 77427422,
|
||||||
|
"weight": 23
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "searchQA_question_top5_snippets_merged.jsonl.gz",
|
||||||
|
"lines": 582261,
|
||||||
|
"weight": 28
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "yahoo_answers_title_question.jsonl.gz",
|
||||||
|
"lines": 659896,
|
||||||
|
"weight": 31
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "yahoo_answers_question_answer.jsonl.gz",
|
||||||
|
"lines": 681164,
|
||||||
|
"weight": 32
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "yahoo_answers_title_answer.jsonl.gz",
|
||||||
|
"lines": 1198260,
|
||||||
|
"weight": 47
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/math.stackexchange.com.jsonl.gz",
|
||||||
|
"lines": 1338443,
|
||||||
|
"weight": 47
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "gooaq_pairs.jsonl.gz",
|
||||||
|
"lines": 3012496,
|
||||||
|
"weight": 47
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "msmarco-query_passage_negative.jsonl.gz",
|
||||||
|
"lines": 9144553,
|
||||||
|
"weight": 47
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stackexchange_title_body/stackoverflow.com-Posts.jsonl.gz",
|
||||||
|
"lines": 18562443,
|
||||||
|
"weight": 47
|
||||||
|
},
|
||||||
|
{"name": "reddit/reddit_2015.jsonl.gz", "weight": 50},
|
||||||
|
{"name": "reddit/reddit_2016.jsonl.gz", "weight": 50},
|
||||||
|
{"name": "reddit/reddit_2017.jsonl.gz", "weight": 50},
|
||||||
|
{"name": "reddit/reddit_2018.jsonl.gz", "weight": 50}
|
||||||
|
]
|
||||||
50001
merges.txt
Normal file
50001
merges.txt
Normal file
File diff suppressed because it is too large
Load Diff
20
modules.json
Executable file
20
modules.json
Executable file
@@ -0,0 +1,20 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"idx": 0,
|
||||||
|
"name": "0",
|
||||||
|
"path": "",
|
||||||
|
"type": "sentence_transformers.models.Transformer"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 1,
|
||||||
|
"name": "1",
|
||||||
|
"path": "1_Pooling",
|
||||||
|
"type": "sentence_transformers.models.Pooling"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 2,
|
||||||
|
"name": "2",
|
||||||
|
"path": "2_Normalize",
|
||||||
|
"type": "sentence_transformers.models.Normalize"
|
||||||
|
}
|
||||||
|
]
|
||||||
3
pytorch_model.bin
Normal file
3
pytorch_model.bin
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:29bb8f3e407eaaa38e2675111fa56cf6f56cb56aab4f2257477fbb1467c07747
|
||||||
|
size 1421566897
|
||||||
4
sentence_bert_config.json
Executable file
4
sentence_bert_config.json
Executable file
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"max_seq_length": 128,
|
||||||
|
"do_lower_case": false
|
||||||
|
}
|
||||||
1
special_tokens_map.json
Normal file
1
special_tokens_map.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"bos_token": "<s>", "eos_token": "</s>", "unk_token": "<unk>", "sep_token": "</s>", "pad_token": "<pad>", "cls_token": "<s>", "mask_token": {"content": "<mask>", "single_word": false, "lstrip": true, "rstrip": false, "normalized": false}}
|
||||||
1
tokenizer.json
Normal file
1
tokenizer.json
Normal file
File diff suppressed because one or more lines are too long
1
tokenizer_config.json
Normal file
1
tokenizer_config.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"unk_token": "<unk>", "bos_token": "<s>", "eos_token": "</s>", "add_prefix_space": false, "errors": "replace", "sep_token": "</s>", "cls_token": "<s>", "pad_token": "<pad>", "mask_token": "<mask>", "model_max_length": 512, "special_tokens_map_file": null, "name_or_path": "roberta-large", "tokenizer_class": "RobertaTokenizer"}
|
||||||
344
train_script.py
Executable file
344
train_script.py
Executable file
@@ -0,0 +1,344 @@
|
|||||||
|
"""
|
||||||
|
Train script for a single file
|
||||||
|
|
||||||
|
Need to set the TPU address first:
|
||||||
|
export XRT_TPU_CONFIG="localservice;0;localhost:51011"
|
||||||
|
"""
|
||||||
|
|
||||||
|
import torch.multiprocessing as mp
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
import sys
|
||||||
|
import argparse
|
||||||
|
import gzip
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import tqdm
|
||||||
|
import torch
|
||||||
|
from torch import nn
|
||||||
|
from torch.utils.data import DataLoader
|
||||||
|
import torch
|
||||||
|
import torch_xla
|
||||||
|
import torch_xla.core
|
||||||
|
import torch_xla.core.functions
|
||||||
|
import torch_xla.core.xla_model as xm
|
||||||
|
import torch_xla.distributed.xla_multiprocessing as xmp
|
||||||
|
import torch_xla.distributed.parallel_loader as pl
|
||||||
|
import os
|
||||||
|
from shutil import copyfile
|
||||||
|
|
||||||
|
|
||||||
|
from transformers import (
|
||||||
|
AdamW,
|
||||||
|
AutoModel,
|
||||||
|
AutoTokenizer,
|
||||||
|
get_linear_schedule_with_warmup,
|
||||||
|
set_seed,
|
||||||
|
)
|
||||||
|
|
||||||
|
class AutoModelForSentenceEmbedding(nn.Module):
|
||||||
|
def __init__(self, model_name, tokenizer, normalize=True):
|
||||||
|
super(AutoModelForSentenceEmbedding, self).__init__()
|
||||||
|
|
||||||
|
self.model = AutoModel.from_pretrained(model_name)
|
||||||
|
self.normalize = normalize
|
||||||
|
self.tokenizer = tokenizer
|
||||||
|
|
||||||
|
def forward(self, **kwargs):
|
||||||
|
model_output = self.model(**kwargs)
|
||||||
|
embeddings = self.mean_pooling(model_output, kwargs['attention_mask'])
|
||||||
|
if self.normalize:
|
||||||
|
embeddings = torch.nn.functional.normalize(embeddings, p=2, dim=1)
|
||||||
|
|
||||||
|
return embeddings
|
||||||
|
|
||||||
|
def mean_pooling(self, model_output, attention_mask):
|
||||||
|
token_embeddings = model_output[0] # First element of model_output contains all token embeddings
|
||||||
|
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
||||||
|
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
||||||
|
|
||||||
|
def save_pretrained(self, output_path):
|
||||||
|
if xm.is_master_ordinal():
|
||||||
|
self.tokenizer.save_pretrained(output_path)
|
||||||
|
self.model.config.save_pretrained(output_path)
|
||||||
|
|
||||||
|
xm.save(self.model.state_dict(), os.path.join(output_path, "pytorch_model.bin"))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def train_function(index, args, queue):
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained(args.model)
|
||||||
|
model = AutoModelForSentenceEmbedding(args.model, tokenizer)
|
||||||
|
|
||||||
|
|
||||||
|
### Train Loop
|
||||||
|
device = xm.xla_device()
|
||||||
|
model = model.to(device)
|
||||||
|
|
||||||
|
# Instantiate optimizer
|
||||||
|
optimizer = AdamW(params=model.parameters(), lr=2e-5, correct_bias=True)
|
||||||
|
|
||||||
|
lr_scheduler = get_linear_schedule_with_warmup(
|
||||||
|
optimizer=optimizer,
|
||||||
|
num_warmup_steps=500,
|
||||||
|
num_training_steps=args.steps,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Now we train the model
|
||||||
|
cross_entropy_loss = nn.CrossEntropyLoss()
|
||||||
|
max_grad_norm = 1
|
||||||
|
|
||||||
|
model.train()
|
||||||
|
|
||||||
|
for global_step in tqdm.trange(args.steps, disable=not xm.is_master_ordinal()):
|
||||||
|
#### Get the batch data
|
||||||
|
batch = queue.get()
|
||||||
|
#print(index, "batch {}x{}".format(len(batch), ",".join([str(len(b)) for b in batch])))
|
||||||
|
|
||||||
|
|
||||||
|
if len(batch[0]) == 2: #(anchor, positive)
|
||||||
|
text1 = tokenizer([b[0] for b in batch], return_tensors="pt", max_length=args.max_length, truncation=True, padding="max_length")
|
||||||
|
text2 = tokenizer([b[1] for b in batch], return_tensors="pt", max_length=args.max_length, truncation=True, padding="max_length")
|
||||||
|
|
||||||
|
### Compute embeddings
|
||||||
|
embeddings_a = model(**text1.to(device))
|
||||||
|
embeddings_b = model(**text2.to(device))
|
||||||
|
|
||||||
|
### Gather all embedings
|
||||||
|
embeddings_a = torch_xla.core.functions.all_gather(embeddings_a)
|
||||||
|
embeddings_b = torch_xla.core.functions.all_gather(embeddings_b)
|
||||||
|
|
||||||
|
### Compute similarity scores 512 x 512
|
||||||
|
scores = torch.mm(embeddings_a, embeddings_b.transpose(0, 1)) * args.scale
|
||||||
|
|
||||||
|
### Compute cross-entropy loss
|
||||||
|
labels = torch.tensor(range(len(scores)), dtype=torch.long, device=embeddings_a.device) # Example a[i] should match with b[i]
|
||||||
|
|
||||||
|
## Symmetric loss as in CLIP
|
||||||
|
loss = (cross_entropy_loss(scores, labels) + cross_entropy_loss(scores.transpose(0, 1), labels)) / 2
|
||||||
|
|
||||||
|
else: #(anchor, positive, negative)
|
||||||
|
text1 = tokenizer([b[0] for b in batch], return_tensors="pt", max_length=args.max_length, truncation=True, padding="max_length")
|
||||||
|
text2 = tokenizer([b[1] for b in batch], return_tensors="pt", max_length=args.max_length, truncation=True, padding="max_length")
|
||||||
|
text3 = tokenizer([b[2] for b in batch], return_tensors="pt", max_length=args.max_length, truncation=True, padding="max_length")
|
||||||
|
|
||||||
|
embeddings_a = model(**text1.to(device))
|
||||||
|
embeddings_b1 = model(**text2.to(device))
|
||||||
|
embeddings_b2 = model(**text3.to(device))
|
||||||
|
|
||||||
|
embeddings_a = torch_xla.core.functions.all_gather(embeddings_a)
|
||||||
|
embeddings_b1 = torch_xla.core.functions.all_gather(embeddings_b1)
|
||||||
|
embeddings_b2 = torch_xla.core.functions.all_gather(embeddings_b2)
|
||||||
|
|
||||||
|
embeddings_b = torch.cat([embeddings_b1, embeddings_b2])
|
||||||
|
|
||||||
|
### Compute similarity scores 512 x 1024
|
||||||
|
scores = torch.mm(embeddings_a, embeddings_b.transpose(0, 1)) * args.scale
|
||||||
|
|
||||||
|
### Compute cross-entropy loss
|
||||||
|
labels = torch.tensor(range(len(scores)), dtype=torch.long, device=embeddings_a.device) # Example a[i] should match with b[i]
|
||||||
|
|
||||||
|
## One-way loss
|
||||||
|
loss = cross_entropy_loss(scores, labels)
|
||||||
|
|
||||||
|
|
||||||
|
# Backward pass
|
||||||
|
optimizer.zero_grad()
|
||||||
|
loss.backward()
|
||||||
|
torch.nn.utils.clip_grad_norm_(model.parameters(), max_grad_norm)
|
||||||
|
|
||||||
|
xm.optimizer_step(optimizer, barrier=True)
|
||||||
|
lr_scheduler.step()
|
||||||
|
|
||||||
|
|
||||||
|
#Save model
|
||||||
|
if (global_step+1) % args.save_steps == 0:
|
||||||
|
output_path = os.path.join(args.output, str(global_step+1))
|
||||||
|
xm.master_print("save model: "+output_path)
|
||||||
|
model.save_pretrained(output_path)
|
||||||
|
|
||||||
|
|
||||||
|
output_path = os.path.join(args.output, "final")
|
||||||
|
xm.master_print("save model final: "+ output_path)
|
||||||
|
model.save_pretrained(output_path)
|
||||||
|
|
||||||
|
|
||||||
|
def produce_data(args, queue, filepaths, dataset_indices):
|
||||||
|
global_batch_size = args.batch_size*args.nprocs #Global batch size
|
||||||
|
size_per_dataset = int(global_batch_size / args.datasets_per_batch) #How many datasets per batch
|
||||||
|
num_same_dataset = int(size_per_dataset / args.batch_size)
|
||||||
|
print("producer", "global_batch_size", global_batch_size)
|
||||||
|
print("producer", "size_per_dataset", size_per_dataset)
|
||||||
|
print("producer", "num_same_dataset", num_same_dataset)
|
||||||
|
|
||||||
|
datasets = []
|
||||||
|
for filepath in filepaths:
|
||||||
|
if "reddit_" in filepath: #Special dataset class for Reddit files
|
||||||
|
data_obj = RedditDataset(filepath)
|
||||||
|
else:
|
||||||
|
data_obj = Dataset(filepath)
|
||||||
|
datasets.append(iter(data_obj))
|
||||||
|
|
||||||
|
# Store if dataset is in a 2 col or 3 col format
|
||||||
|
num_cols = {idx: len(next(dataset)) for idx, dataset in enumerate(datasets)}
|
||||||
|
|
||||||
|
while True:
|
||||||
|
texts_in_batch = set()
|
||||||
|
batch_format = None #2 vs 3 col format for this batch
|
||||||
|
|
||||||
|
#Add data from several sub datasets
|
||||||
|
for _ in range(args.datasets_per_batch):
|
||||||
|
valid_dataset = False #Check that datasets have the same 2/3 col format
|
||||||
|
while not valid_dataset:
|
||||||
|
data_idx = random.choice(dataset_indices)
|
||||||
|
if batch_format is None:
|
||||||
|
batch_format = num_cols[data_idx]
|
||||||
|
valid_dataset = True
|
||||||
|
else: #Check that this dataset has the same format
|
||||||
|
valid_dataset = (batch_format == num_cols[data_idx])
|
||||||
|
|
||||||
|
#Get data from this dataset
|
||||||
|
dataset = datasets[data_idx]
|
||||||
|
for _ in range(num_same_dataset):
|
||||||
|
for _ in range(args.nprocs):
|
||||||
|
batch_device = [] #A batch for one device
|
||||||
|
while len(batch_device) < args.batch_size:
|
||||||
|
sample = next(dataset)
|
||||||
|
in_batch = False
|
||||||
|
for text in sample:
|
||||||
|
if text in texts_in_batch:
|
||||||
|
in_batch = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if not in_batch:
|
||||||
|
for text in sample:
|
||||||
|
texts_in_batch.add(text)
|
||||||
|
batch_device.append(sample)
|
||||||
|
|
||||||
|
queue.put(batch_device)
|
||||||
|
|
||||||
|
|
||||||
|
class RedditDataset:
|
||||||
|
"""
|
||||||
|
A class that handles the reddit data files
|
||||||
|
"""
|
||||||
|
def __init__(self, filepath):
|
||||||
|
self.filepath = filepath
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
while True:
|
||||||
|
with gzip.open(self.filepath, "rt") as fIn:
|
||||||
|
for line in fIn:
|
||||||
|
data = json.loads(line)
|
||||||
|
|
||||||
|
if "response" in data and "context" in data:
|
||||||
|
yield [data["response"], data["context"]]
|
||||||
|
|
||||||
|
class Dataset:
|
||||||
|
"""
|
||||||
|
A class that handles one dataset
|
||||||
|
"""
|
||||||
|
def __init__(self, filepath):
|
||||||
|
self.filepath = filepath
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
max_dataset_size = 10*1000*1000 #Cache small datasets in memory
|
||||||
|
dataset = []
|
||||||
|
data_format = None
|
||||||
|
|
||||||
|
while dataset is None or len(dataset) == 0:
|
||||||
|
with gzip.open(self.filepath, "rt") as fIn:
|
||||||
|
for line in fIn:
|
||||||
|
data = json.loads(line)
|
||||||
|
if isinstance(data, dict):
|
||||||
|
data = data['texts']
|
||||||
|
|
||||||
|
if data_format is None:
|
||||||
|
data_format = len(data)
|
||||||
|
|
||||||
|
#Ensure that all entries are of the same 2/3 col format
|
||||||
|
assert len(data) == data_format
|
||||||
|
|
||||||
|
if dataset is not None:
|
||||||
|
dataset.append(data)
|
||||||
|
if len(dataset) >= max_dataset_size:
|
||||||
|
dataset = None
|
||||||
|
|
||||||
|
yield data
|
||||||
|
|
||||||
|
# Data loaded. Now stream to the queue
|
||||||
|
# Shuffle for each epoch
|
||||||
|
while True:
|
||||||
|
random.shuffle(dataset)
|
||||||
|
for data in dataset:
|
||||||
|
yield data
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('--model', default='nreimers/MiniLM-L6-H384-uncased')
|
||||||
|
parser.add_argument('--steps', type=int, default=2000)
|
||||||
|
parser.add_argument('--save_steps', type=int, default=10000)
|
||||||
|
parser.add_argument('--batch_size', type=int, default=64)
|
||||||
|
parser.add_argument('--max_length', type=int, default=128)
|
||||||
|
parser.add_argument('--nprocs', type=int, default=8)
|
||||||
|
parser.add_argument('--datasets_per_batch', type=int, default=2, help="Number of datasets per batch")
|
||||||
|
parser.add_argument('--scale', type=float, default=20, help="Use 20 for cossim, and 1 when you work with unnormalized embeddings with dot product")
|
||||||
|
parser.add_argument('--data_folder', default="/data", help="Folder with your dataset files")
|
||||||
|
parser.add_argument('data_config', help="A data_config.json file")
|
||||||
|
parser.add_argument('output')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
# Ensure global batch size is divisble by data_sample_size
|
||||||
|
assert (args.batch_size*args.nprocs) % args.datasets_per_batch == 0
|
||||||
|
|
||||||
|
logging.info("Output: "+args.output)
|
||||||
|
if os.path.exists(args.output):
|
||||||
|
print("Output folder already exists.")
|
||||||
|
input("Continue?")
|
||||||
|
|
||||||
|
# Write train script to output path
|
||||||
|
os.makedirs(args.output, exist_ok=True)
|
||||||
|
|
||||||
|
data_config_path = os.path.join(args.output, 'data_config.json')
|
||||||
|
copyfile(args.data_config, data_config_path)
|
||||||
|
|
||||||
|
train_script_path = os.path.join(args.output, 'train_script.py')
|
||||||
|
copyfile(__file__, train_script_path)
|
||||||
|
with open(train_script_path, 'a') as fOut:
|
||||||
|
fOut.write("\n\n# Script was called via:\n#python " + " ".join(sys.argv))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#Load data config
|
||||||
|
with open(args.data_config) as fIn:
|
||||||
|
data_config = json.load(fIn)
|
||||||
|
|
||||||
|
queue = mp.Queue(maxsize=100*args.nprocs)
|
||||||
|
|
||||||
|
filepaths = []
|
||||||
|
dataset_indices = []
|
||||||
|
for idx, data in enumerate(data_config):
|
||||||
|
filepaths.append(os.path.join(os.path.expanduser(args.data_folder), data['name']))
|
||||||
|
dataset_indices.extend([idx]*data['weight'])
|
||||||
|
|
||||||
|
# Start producer
|
||||||
|
p = mp.Process(target=produce_data, args=(args, queue, filepaths, dataset_indices))
|
||||||
|
p.start()
|
||||||
|
|
||||||
|
# Run training
|
||||||
|
print("Start processes:", args.nprocs)
|
||||||
|
xmp.spawn(train_function, args=(args, queue), nprocs=args.nprocs, start_method='fork')
|
||||||
|
print("Training done")
|
||||||
|
print("It might be that not all processes exit automatically. In that case you must manually kill this process.")
|
||||||
|
print("With 'pkill python' you can kill all remaining python processes")
|
||||||
|
p.kill()
|
||||||
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Script was called via:
|
||||||
|
#python train_many_data_files_v2.py --steps 1000000 --batch_size 32 --model roberta-large train_data_configs/all_datasets_v3.json output/all_datasets_v3_roberta-large
|
||||||
1
vocab.json
Normal file
1
vocab.json
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user