RichardErkhov
commited on
uploaded readme
Browse files
README.md
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Quantization made by Richard Erkhov.
|
2 |
+
|
3 |
+
[Github](https://github.com/RichardErkhov)
|
4 |
+
|
5 |
+
[Discord](https://discord.gg/pvy7H8DZMG)
|
6 |
+
|
7 |
+
[Request more models](https://github.com/RichardErkhov/quant_request)
|
8 |
+
|
9 |
+
|
10 |
+
opt-350m-InBedder - AWQ
|
11 |
+
- Model creator: https://huggingface.co/BrandonZYW/
|
12 |
+
- Original model: https://huggingface.co/BrandonZYW/opt-350m-InBedder/
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
Original model description:
|
18 |
+
---
|
19 |
+
license: mit
|
20 |
+
datasets:
|
21 |
+
- KomeijiForce/Inbedder-Pretrain-Data
|
22 |
+
language:
|
23 |
+
- en
|
24 |
+
---
|
25 |
+
|
26 |
+
# [ACL2024] Answer is All You Need: Instruction-following Text Embedding via Answering the Question
|
27 |
+
|
28 |
+
InBedder🛌 is a text embedder that is designed to follow instructions. Instruction-following text embedder can capture characteristics of texts specified by user instructions. InBedder offers a novel viewpoint that treats the instruction as a question about the input text and encodes the expected answers to obtain the representation accordingly. We show that InBedder is aware of instructions with different evaluation tasks.
|
29 |
+
|
30 |
+
![image/png](https://cdn-uploads.huggingface.co/production/uploads/64323dd503d81fa4d26deaf9/jLbqF-2uT8Aw9DsN7XCVG.png)
|
31 |
+
|
32 |
+
The following is a use case from [https://github.com/zhang-yu-wei/InBedder/blob/main/UseCase.ipynb](https://github.com/zhang-yu-wei/InBedder/blob/main/UseCase.ipynb)
|
33 |
+
|
34 |
+
```python
|
35 |
+
import torch
|
36 |
+
from torch import nn
|
37 |
+
from torch.nn.functional import gelu, cosine_similarity
|
38 |
+
from transformers import AutoTokenizer, AutoModel, AutoModelForMaskedLM
|
39 |
+
|
40 |
+
import numpy as np
|
41 |
+
|
42 |
+
class InBedder():
|
43 |
+
|
44 |
+
def __init__(self, path='KomeijiForce/inbedder-roberta-large', device='cuda:0'):
|
45 |
+
|
46 |
+
model = AutoModelForMaskedLM.from_pretrained(path)
|
47 |
+
|
48 |
+
self.tokenizer = AutoTokenizer.from_pretrained(path)
|
49 |
+
self.model = model.roberta
|
50 |
+
self.dense = model.lm_head.dense
|
51 |
+
self.layer_norm = model.lm_head.layer_norm
|
52 |
+
|
53 |
+
self.device = torch.device(device)
|
54 |
+
self.model = self.model.to(self.device)
|
55 |
+
self.dense = self.dense.to(self.device)
|
56 |
+
self.layer_norm = self.layer_norm.to(self.device)
|
57 |
+
|
58 |
+
self.vocab = self.tokenizer.get_vocab()
|
59 |
+
self.vocab = {self.vocab[key]:key for key in self.vocab}
|
60 |
+
|
61 |
+
def encode(self, input_texts, instruction, n_mask):
|
62 |
+
|
63 |
+
if type(instruction) == str:
|
64 |
+
prompts = [instruction + self.tokenizer.mask_token*n_mask for input_text in input_texts]
|
65 |
+
elif type(instruction) == list:
|
66 |
+
prompts = [inst + self.tokenizer.mask_token*n_mask for inst in instruction]
|
67 |
+
|
68 |
+
inputs = self.tokenizer(input_texts, prompts, padding=True, truncation=True, return_tensors='pt').to(self.device)
|
69 |
+
|
70 |
+
mask = inputs.input_ids.eq(self.tokenizer.mask_token_id)
|
71 |
+
|
72 |
+
outputs = self.model(**inputs)
|
73 |
+
|
74 |
+
logits = outputs.last_hidden_state[mask]
|
75 |
+
|
76 |
+
logits = self.layer_norm(gelu(self.dense(logits)))
|
77 |
+
|
78 |
+
logits = logits.reshape(len(input_texts), n_mask, -1)
|
79 |
+
|
80 |
+
logits = logits.mean(1)
|
81 |
+
|
82 |
+
logits = (logits - logits.mean(1, keepdim=True)) / logits.std(1, keepdim=True)
|
83 |
+
|
84 |
+
return logits
|
85 |
+
|
86 |
+
inbedder = InBedder(path='KomeijiForce/inbedder-roberta-large', device='cpu')
|
87 |
+
|
88 |
+
texts = ["I love cat!", "I love dog!", "I dislike cat!"]
|
89 |
+
instruction = "What is the animal mentioned here?"
|
90 |
+
embeddings = inbedder.encode(texts, instruction, 3)
|
91 |
+
|
92 |
+
cosine_similarity(embeddings[:1], embeddings[1:], dim=1)
|
93 |
+
# tensor([0.9374, 0.9917], grad_fn=<SumBackward1>)
|
94 |
+
|
95 |
+
texts = ["I love cat!", "I love dog!", "I dislike cat!"]
|
96 |
+
instruction = "What is emotion expressed here?"
|
97 |
+
embeddings = inbedder.encode(texts, instruction, 3)
|
98 |
+
|
99 |
+
cosine_similarity(embeddings[:1], embeddings[1:], dim=1)
|
100 |
+
# tensor([0.9859, 0.8537], grad_fn=<SumBackward1>)
|
101 |
+
```
|
102 |
+
|