Update README.md
Browse files
README.md
CHANGED
@@ -1,201 +1,94 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
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 |
-
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
96 |
-
|
97 |
-
#### Speeds, Sizes, Times [optional]
|
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]
|
200 |
-
|
201 |
-
|
|
|
1 |
+
# LLARA-7B-Passage
|
2 |
+
|
3 |
+
This model is fine-tuned from LLaMA-2-7B using LoRA and the embedding size is 4096.
|
4 |
+
|
5 |
+
## Training Data
|
6 |
+
|
7 |
+
The model is fine-tuned on the training split of [MS MARCO Passage Ranking](https://microsoft.github.io/msmarco/Datasets) datasets for 1 epoch. Please check our paper for details.
|
8 |
+
|
9 |
+
## Usage
|
10 |
+
|
11 |
+
Below is an example to encode a query and a passage, and then compute their similarity using their embedding.
|
12 |
+
|
13 |
+
```python
|
14 |
+
import torch
|
15 |
+
from transformers import AutoModel, AutoTokenizer, LlamaModel
|
16 |
+
|
17 |
+
def get_query_inputs(queries, tokenizer, max_length=512):
|
18 |
+
prefix = '"'
|
19 |
+
suffix = '", predict the following passage within eight words: <s9><s10><s11><s12><s13><s14><s15><s16>'
|
20 |
+
prefix_ids = tokenizer(prefix, return_tensors=None)['input_ids']
|
21 |
+
suffix_ids = tokenizer(suffix, return_tensors=None)['input_ids'][1:]
|
22 |
+
queries_inputs = []
|
23 |
+
for query in queries:
|
24 |
+
inputs = tokenizer(query,
|
25 |
+
return_tensors=None,
|
26 |
+
max_length=max_length,
|
27 |
+
truncation=True,
|
28 |
+
add_special_tokens=False)
|
29 |
+
inputs['input_ids'] = prefix_ids + inputs['input_ids'] + suffix_ids
|
30 |
+
inputs['attention_mask'] = [1] * len(inputs['input_ids'])
|
31 |
+
queries_inputs.append(inputs)
|
32 |
+
return tokenizer.pad(
|
33 |
+
queries_inputs,
|
34 |
+
padding=True,
|
35 |
+
max_length=max_length,
|
36 |
+
pad_to_multiple_of=8,
|
37 |
+
return_tensors='pt',
|
38 |
+
)
|
39 |
+
|
40 |
+
def get_passage_inputs(passages, tokenizer, max_length=512):
|
41 |
+
prefix = '"'
|
42 |
+
suffix = '", summarize the above passage within eight words: <s1><s2><s3><s4><s5><s6><s7><s8>'
|
43 |
+
prefix_ids = tokenizer(prefix, return_tensors=None)['input_ids']
|
44 |
+
suffix_ids = tokenizer(suffix, return_tensors=None)['input_ids'][1:]
|
45 |
+
passages_inputs = []
|
46 |
+
for passage in passages:
|
47 |
+
inputs = tokenizer(passage,
|
48 |
+
return_tensors=None,
|
49 |
+
max_length=max_length,
|
50 |
+
truncation=True,
|
51 |
+
add_special_tokens=False)
|
52 |
+
inputs['input_ids'] = prefix_ids + inputs['input_ids'] + suffix_ids
|
53 |
+
inputs['attention_mask'] = [1] * len(inputs['input_ids'])
|
54 |
+
passages_inputs.append(inputs)
|
55 |
+
return tokenizer.pad(
|
56 |
+
passages_inputs,
|
57 |
+
padding=True,
|
58 |
+
max_length=max_length,
|
59 |
+
pad_to_multiple_of=8,
|
60 |
+
return_tensors='pt',
|
61 |
+
)
|
62 |
+
|
63 |
+
# Load the tokenizer and model
|
64 |
+
tokenizer = AutoTokenizer.from_pretrained('cfli/LLARA-passage')
|
65 |
+
model = AutoModel.from_pretrained('cfli/LLARA-passage')
|
66 |
+
|
67 |
+
# Define query and passage inputs
|
68 |
+
query = "What is llama?"
|
69 |
+
title = "Llama"
|
70 |
+
passage = "The llama is a domesticated South American camelid, widely used as a meat and pack animal by Andean cultures since the pre-Columbian era."
|
71 |
+
query_input = get_query_inputs([query], tokenizer)
|
72 |
+
passage_input = get_passage_inputs([passage], tokenizer)
|
73 |
+
|
74 |
+
|
75 |
+
with torch.no_grad():
|
76 |
+
# compute query embedding
|
77 |
+
query_outputs = model(**query_input, return_dict=True, output_hidden_states=True)
|
78 |
+
query_embedding = query_outputs.hidden_states[-1][:, -8:, :]
|
79 |
+
query_embedding = torch.mean(query_embedding, dim=1)
|
80 |
+
query_embedding = torch.nn.functional.normalize(query_embedding, dim=-1)
|
81 |
+
|
82 |
+
# compute passage embedding
|
83 |
+
passage_outputs = model(**passage_input, return_dict=True, output_hidden_states=True)
|
84 |
+
passage_embeddings = passage_outputs.hidden_states[-1][:, -8:, :]
|
85 |
+
passage_embeddings = torch.mean(passage_embeddings, dim=1)
|
86 |
+
passage_embeddings = torch.nn.functional.normalize(passage_embeddings, dim=-1)
|
87 |
+
|
88 |
+
# compute similarity score
|
89 |
+
score = query_embedding @ passage_embeddings.T
|
90 |
+
print(score)
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|