-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathembedding.py
146 lines (113 loc) · 3.76 KB
/
embedding.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import asyncio
import json
import random
import re
import time
from abc import abstractmethod
from dataclasses import dataclass
from typing import List, NamedTuple
import numpy as np
import torch
from sentence_transformers import SentenceTransformer
from transformers import PreTrainedTokenizer
from ...clients.client import Client
from ...features import Example, FeatureRecord
from ..scorer import Scorer, ScorerResult
@dataclass
class EmbeddingOutput:
text: str
"""The text that was used to evaluate the similarity"""
distance: float | int
"""Quantile or neighbor distance"""
similarity: list[float] = 0
"""What is the similarity of the example to the explanation"""
class Sample(NamedTuple):
text: str
activations: list[float]
data: EmbeddingOutput
class EmbeddingScorer(Scorer):
name = "embedding"
def __init__(
self,
model,
tokenizer: PreTrainedTokenizer | None = None,
verbose: bool = False,
**generation_kwargs,
):
self.model = model
self.verbose = verbose
self.tokenizer = tokenizer
self.generation_kwargs = generation_kwargs
async def __call__(
self,
record: FeatureRecord,
) -> list[EmbeddingOutput]:
samples = self._prepare(record)
random.shuffle(samples)
results = self._query(
record.explanation,
samples,
)
return ScorerResult(record=record, score=results)
def call_sync(self, record: FeatureRecord) -> list[EmbeddingOutput]:
return asyncio.run(self.__call__(record))
def _prepare(self, record: FeatureRecord) -> list[list[Sample]]:
"""
Prepare and shuffle a list of samples for classification.
"""
defaults = {
"tokenizer": self.tokenizer,
}
samples = examples_to_samples(
record.extra_examples,
distance=-1,
**defaults,
)
for i, examples in enumerate(record.test):
samples.extend(
examples_to_samples(
examples,
distance=i + 1,
**defaults,
)
)
return samples
def _query(self, explanation: str, samples: list[Sample]) -> list[EmbeddingOutput]:
explanation_prompt = "Instruct: Retrieve sentences that could be related to the explanation.\nQuery:" + explanation
query_embeding = self.model.encode(explanation_prompt)
samples_text = [sample.text for sample in samples]
# # Temporary batching
# sample_embedings = []
# for i in range(0, len(samples_text), 10):
# sample_embedings.extend(self.model.encode(samples_text[i:i+10]))
sample_embedings = self.model.encode(samples_text)
similarity = self.model.similarity(query_embeding,sample_embedings)[0]
results = []
for i in range(len(samples)):
#print(i)
samples[i].data.similarity = similarity[i].item()
results.append(samples[i].data)
return results
def examples_to_samples(
examples: list[Example],
tokenizer: PreTrainedTokenizer,
**sample_kwargs,
) -> list[Sample]:
samples = []
for example in examples:
if tokenizer is not None:
text = "".join(tokenizer.batch_decode(example.tokens))
else:
text = "".join(example.tokens)
activations = example.activations.tolist()
samples.append(
Sample(
text=text,
activations=activations,
data=EmbeddingOutput(
text=text,
**sample_kwargs
),
)
)
return samples