Update README.md
Browse files
README.md
CHANGED
@@ -1,199 +1,167 @@
|
|
1 |
---
|
|
|
|
|
|
|
|
|
|
|
2 |
library_name: transformers
|
3 |
-
tags:
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
---
|
|
|
5 |
|
6 |
-
|
|
|
|
|
7 |
|
8 |
-
|
|
|
9 |
|
|
|
|
|
|
|
|
|
10 |
|
|
|
11 |
|
12 |
-
|
13 |
|
14 |
-
|
|
|
|
|
|
|
15 |
|
16 |
-
|
|
|
|
|
17 |
|
18 |
-
|
|
|
19 |
|
20 |
-
-
|
21 |
-
|
22 |
-
-
|
23 |
-
|
24 |
-
- **Language(s) (NLP):** [More Information Needed]
|
25 |
-
- **License:** [More Information Needed]
|
26 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
27 |
|
28 |
-
|
29 |
|
30 |
-
|
31 |
|
32 |
-
|
33 |
-
- **Paper [optional]:** [More Information Needed]
|
34 |
-
- **Demo [optional]:** [More Information Needed]
|
35 |
|
36 |
-
|
|
|
|
|
37 |
|
38 |
-
|
|
|
39 |
|
40 |
-
|
|
|
41 |
|
42 |
-
|
43 |
|
44 |
-
|
45 |
|
46 |
-
|
|
|
47 |
|
48 |
-
|
49 |
|
50 |
-
|
51 |
|
52 |
-
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
|
55 |
|
56 |
-
|
57 |
|
58 |
-
|
59 |
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
###
|
65 |
|
66 |
-
|
67 |
|
68 |
-
|
|
|
69 |
|
70 |
-
|
|
|
71 |
|
72 |
-
|
|
|
|
|
73 |
|
74 |
-
|
|
|
|
|
75 |
|
76 |
-
|
|
|
|
|
77 |
|
78 |
-
###
|
79 |
|
80 |
-
|
81 |
|
82 |
-
|
|
|
83 |
|
84 |
-
|
85 |
|
86 |
-
|
|
|
|
|
87 |
|
88 |
-
|
|
|
89 |
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
-
|
|
|
|
|
94 |
|
95 |
-
|
96 |
|
97 |
-
|
98 |
|
99 |
-
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
100 |
-
|
101 |
-
[More Information Needed]
|
102 |
-
|
103 |
-
## Evaluation
|
104 |
-
|
105 |
-
<!-- This section describes the evaluation protocols and provides the results. -->
|
106 |
-
|
107 |
-
### Testing Data, Factors & Metrics
|
108 |
-
|
109 |
-
#### Testing Data
|
110 |
-
|
111 |
-
<!-- This should link to a Dataset Card if possible. -->
|
112 |
-
|
113 |
-
[More Information Needed]
|
114 |
-
|
115 |
-
#### Factors
|
116 |
-
|
117 |
-
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
118 |
-
|
119 |
-
[More Information Needed]
|
120 |
-
|
121 |
-
#### Metrics
|
122 |
-
|
123 |
-
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
124 |
-
|
125 |
-
[More Information Needed]
|
126 |
-
|
127 |
-
### Results
|
128 |
-
|
129 |
-
[More Information Needed]
|
130 |
-
|
131 |
-
#### Summary
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
## Model Examination [optional]
|
136 |
-
|
137 |
-
<!-- Relevant interpretability work for the model goes here -->
|
138 |
-
|
139 |
-
[More Information Needed]
|
140 |
-
|
141 |
-
## Environmental Impact
|
142 |
-
|
143 |
-
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
144 |
-
|
145 |
-
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
146 |
-
|
147 |
-
- **Hardware Type:** [More Information Needed]
|
148 |
-
- **Hours used:** [More Information Needed]
|
149 |
-
- **Cloud Provider:** [More Information Needed]
|
150 |
-
- **Compute Region:** [More Information Needed]
|
151 |
-
- **Carbon Emitted:** [More Information Needed]
|
152 |
-
|
153 |
-
## Technical Specifications [optional]
|
154 |
-
|
155 |
-
### Model Architecture and Objective
|
156 |
-
|
157 |
-
[More Information Needed]
|
158 |
-
|
159 |
-
### Compute Infrastructure
|
160 |
-
|
161 |
-
[More Information Needed]
|
162 |
-
|
163 |
-
#### Hardware
|
164 |
-
|
165 |
-
[More Information Needed]
|
166 |
-
|
167 |
-
#### Software
|
168 |
-
|
169 |
-
[More Information Needed]
|
170 |
-
|
171 |
-
## Citation [optional]
|
172 |
-
|
173 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
174 |
-
|
175 |
-
**BibTeX:**
|
176 |
-
|
177 |
-
[More Information Needed]
|
178 |
-
|
179 |
-
**APA:**
|
180 |
-
|
181 |
-
[More Information Needed]
|
182 |
-
|
183 |
-
## Glossary [optional]
|
184 |
-
|
185 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
186 |
-
|
187 |
-
[More Information Needed]
|
188 |
-
|
189 |
-
## More Information [optional]
|
190 |
-
|
191 |
-
[More Information Needed]
|
192 |
-
|
193 |
-
## Model Card Authors [optional]
|
194 |
-
|
195 |
-
[More Information Needed]
|
196 |
-
|
197 |
-
## Model Card Contact
|
198 |
-
|
199 |
-
[More Information Needed]
|
|
|
1 |
---
|
2 |
+
license: apache-2.0
|
3 |
+
datasets:
|
4 |
+
- wikimedia/wikipedia
|
5 |
+
language:
|
6 |
+
- en
|
7 |
library_name: transformers
|
8 |
+
tags:
|
9 |
+
- LLM2Vec
|
10 |
+
- encoder
|
11 |
+
- LLM
|
12 |
+
- classification
|
13 |
+
- NER
|
14 |
+
- question-answering
|
15 |
---
|
16 |
+
# LLM2Vec: Large Language Models Are Secretly Powerful Text Encoders
|
17 |
|
18 |
+
> LLM2Vec is a simple recipe to convert decoder-only LLMs into text encoders. It consists of 3 simple steps: 1) enabling bidirectional attention, 2) masked next token prediction, and 3) unsupervised contrastive learning. The model can be further fine-tuned to achieve state-of-the-art performance.
|
19 |
+
- **Repository:** https://github.com/McGill-NLP/llm2vec
|
20 |
+
- **Paper:** https://arxiv.org/abs/2404.05961
|
21 |
|
22 |
+
## Overview:
|
23 |
+
This is a bi-directional version of Tiny-LLaMA-1.0B trained with masked token prediction on the Wikipedia dataset. Modern decoder models offer several advantages over classical encoders like BERT:
|
24 |
|
25 |
+
They are pre-trained on more recent textual corpora
|
26 |
+
They are trained on larger and more diverse datasets
|
27 |
+
Modern decoders have better support for long-context windows
|
28 |
+
Flash-attention support is available for these models
|
29 |
|
30 |
+
Considering these benefits, we are excited to release a series of decoder models tuned to work in a bi-directional setting. This approach combines the strengths of modern decoder architectures with the versatility of bi-directional context understanding, potentially opening up new possibilities for various natural language processing tasks, such as NER.
|
31 |
|
32 |
+
In comparison to original LLM2Vec we trained all weights of LLama model, it potentially improve bi-directional abilities of the model.
|
33 |
|
34 |
+
## Installation
|
35 |
+
```bash
|
36 |
+
pip install llm2vec
|
37 |
+
```
|
38 |
|
39 |
+
## Usage
|
40 |
+
```python
|
41 |
+
from llm2vec.models import LlamaBiModel
|
42 |
|
43 |
+
import torch
|
44 |
+
from transformers import AutoTokenizer
|
45 |
|
46 |
+
# Loading base Mistral model, along with custom code that enables bidirectional connections in decoder-only LLMs. MNTP LoRA weights are merged into the base model.
|
47 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
48 |
+
"knowledgator/Llama-encoder-1.0B"
|
49 |
+
)
|
|
|
|
|
|
|
50 |
|
51 |
+
model = LLamaBiModel.from_pretrained("knowledgator/Llama-encoder-1.0B")
|
52 |
|
53 |
+
text = "The quick brown fox jumps over the lazy dog."
|
54 |
|
55 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
|
|
|
|
56 |
|
57 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
58 |
+
model = model.to(device)
|
59 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
60 |
|
61 |
+
with torch.no_grad():
|
62 |
+
outputs = model(**inputs)
|
63 |
|
64 |
+
last_hidden_states = outputs.last_hidden_state
|
65 |
+
```
|
66 |
|
67 |
+
Here's an improved and expanded version of the README snippet:
|
68 |
|
69 |
+
## Adapting for Different Discriminative Tasks
|
70 |
|
71 |
+
Our bi-directional LLaMA model can be easily adapted for various discriminative tasks such as text classification, question answering, and token classification.
|
72 |
+
To use these specialized versions, we provide a [fork of LLM2Vec](https://github.com/Knowledgator/llm2vec) with additional functionality.
|
73 |
|
74 |
+
### Installation
|
75 |
|
76 |
+
To get started, clone our fork of LLM2Vec and install it:
|
77 |
|
78 |
+
```bash
|
79 |
+
git clone https://github.com/Knowledgator/llm2vec.git
|
80 |
+
cd llm2vec
|
81 |
+
pip install -e .
|
82 |
+
```
|
83 |
|
84 |
+
Using `-e` flag installs the package in editable mode, which is useful for development.
|
85 |
|
86 |
+
### Usage
|
87 |
|
88 |
+
Here's how to import and use the models for different tasks:
|
89 |
|
90 |
+
```python
|
91 |
+
from llm2vec import (
|
92 |
+
AutoLLMEncoderForSequenceClassification,
|
93 |
+
AutoLLMEncoderForQuestionAnswering,
|
94 |
+
AutoLLMEncoderForTokenClassification
|
95 |
+
)
|
96 |
|
97 |
+
# Load models for different tasks
|
98 |
+
classification_model = AutoLLMEncoderForSequenceClassification.from_pretrained('knowledgator/Llama-encoder-1.0B')
|
99 |
+
question_answering_model = AutoLLMEncoderForQuestionAnswering.from_pretrained('knowledgator/Llama-encoder-1.0B')
|
100 |
+
token_classification_model = AutoLLMEncoderForTokenClassification.from_pretrained('knowledgator/Llama-encoder-1.0B')
|
101 |
+
```
|
102 |
|
103 |
+
### Example: Text Classification
|
104 |
|
105 |
+
Here's a basic example of how to use the model for text classification:
|
106 |
|
107 |
+
```python
|
108 |
+
from transformers import AutoTokenizer
|
109 |
|
110 |
+
# Load tokenizer
|
111 |
+
tokenizer = AutoTokenizer.from_pretrained('knowledgator/Llama-encoder-1.0B')
|
112 |
|
113 |
+
# Prepare input
|
114 |
+
text = "This movie is great!"
|
115 |
+
inputs = tokenizer(text, return_tensors="pt")
|
116 |
|
117 |
+
# Get classification logits
|
118 |
+
outputs = classification_model(**inputs)
|
119 |
+
logits = outputs.logits
|
120 |
|
121 |
+
# The logits can be used with a softmax function to get probabilities
|
122 |
+
# or you can use torch.argmax(logits, dim=1) to get the predicted class
|
123 |
+
```
|
124 |
|
125 |
+
### Fine-tuning
|
126 |
|
127 |
+
To fine-tune these models on your specific task:
|
128 |
|
129 |
+
1. Prepare your dataset in a format compatible with HuggingFace's `datasets` library.
|
130 |
+
2. Use the `Trainer` class from HuggingFace's `transformers` library to fine-tune the model.
|
131 |
|
132 |
+
Here's a basic example:
|
133 |
|
134 |
+
```python
|
135 |
+
from transformers import Trainer, TrainingArguments
|
136 |
+
from datasets import load_dataset
|
137 |
|
138 |
+
# Load your dataset
|
139 |
+
dataset = load_dataset("your_dataset")
|
140 |
|
141 |
+
# Define training arguments
|
142 |
+
training_args = TrainingArguments(
|
143 |
+
output_dir="./results",
|
144 |
+
num_train_epochs=3,
|
145 |
+
per_device_train_batch_size=8,
|
146 |
+
per_device_eval_batch_size=8,
|
147 |
+
warmup_steps=500,
|
148 |
+
weight_decay=0.01,
|
149 |
+
logging_dir="./logs",
|
150 |
+
)
|
151 |
|
152 |
+
# Initialize Trainer
|
153 |
+
trainer = Trainer(
|
154 |
+
model=classification_model,
|
155 |
+
args=training_args,
|
156 |
+
train_dataset=dataset["train"],
|
157 |
+
eval_dataset=dataset["test"],
|
158 |
+
)
|
159 |
|
160 |
+
# Fine-tune the model
|
161 |
+
trainer.train()
|
162 |
+
```
|
163 |
|
164 |
+
### Contributing
|
165 |
|
166 |
+
We welcome contributions! If you have suggestions for improvements or encounter any issues, please open an issue or submit a pull request on our [GitHub repository](https://github.com/Knowledgator/llm2vec).
|
167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|