-
Notifications
You must be signed in to change notification settings - Fork 29
/
run_chunked_eval.py
175 lines (159 loc) · 4.75 KB
/
run_chunked_eval.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import click
import torch.cuda
from mteb import MTEB
from transformers import AutoModel, AutoTokenizer
from chunked_pooling.chunked_eval_tasks import *
from chunked_pooling.wrappers import load_model
DEFAULT_CHUNKING_STRATEGY = 'fixed'
DEFAULT_CHUNK_SIZE = 256
DEFAULT_N_SENTENCES = 5
BATCH_SIZE = 1
DEFAULT_LONG_LATE_CHUNKING_OVERLAP_SIZE = 256
DEFAULT_LONG_LATE_CHUNKING_EMBED_SIZE = 0 # set to 0 to disable long late chunking
DEFAULT_TRUNCATE_MAX_LENGTH = None
@click.command()
@click.option(
'--model-name',
default='jinaai/jina-embeddings-v2-small-en',
help='The name of the model to use.',
)
@click.option(
'--model-weights',
default=None,
help='The path to the model weights to use, e.g. in case of finetuning.',
)
@click.option(
'--strategy',
default=DEFAULT_CHUNKING_STRATEGY,
help='The chunking strategy to be applied.',
)
@click.option(
'--task-name', default='SciFactChunked', help='The evaluation task to perform.'
)
@click.option(
'--eval-split', default='test', help='The name of the evaluation split in the task.'
)
@click.option(
'--chunking-model',
default=None,
required=False,
help='The name of the model used for semantic chunking.',
)
@click.option(
'--truncate-max-length',
default=DEFAULT_TRUNCATE_MAX_LENGTH,
type=int,
help='Maximum number of tokens; by default, truncation to 8192 tokens. If None, Long Late Chunking algorithm should be enabled.',
)
@click.option(
'--chunk-size',
default=DEFAULT_CHUNK_SIZE,
type=int,
help='Number of tokens per chunk for fixed strategy.',
)
@click.option(
'--n-sentences',
default=DEFAULT_N_SENTENCES,
type=int,
help='Number of sentences per chunk for sentence strategy.',
)
@click.option(
'--long-late-chunking-embed-size',
default=DEFAULT_LONG_LATE_CHUNKING_EMBED_SIZE,
type=int,
help='Number of tokens per chunk for fixed strategy.',
)
@click.option(
'--long-late-chunking-overlap-size',
default=DEFAULT_LONG_LATE_CHUNKING_OVERLAP_SIZE,
type=int,
help='Token length of the embeddings that come before/after soft boundaries (i.e. overlapping embeddings). Above zero, overlap is used between neighbouring embeddings.',
)
def main(
model_name,
model_weights,
strategy,
task_name,
eval_split,
chunking_model,
truncate_max_length,
chunk_size,
n_sentences,
long_late_chunking_embed_size,
long_late_chunking_overlap_size,
):
try:
task_cls = globals()[task_name]
except:
raise ValueError(f'Unknown task name: {task_name}')
if truncate_max_length is not None and (long_late_chunking_embed_size > 0):
truncate_max_length = None
print(
f'Truncation is disabled because Long Late Chunking algorithm is enabled.'
)
model, has_instructions = load_model(model_name, model_weights)
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
chunking_args = {
'chunk_size': chunk_size,
'n_sentences': n_sentences,
'chunking_strategy': strategy,
'model_has_instructions': has_instructions,
'embedding_model_name': chunking_model if chunking_model else model_name,
}
if torch.cuda.is_available():
model = model.cuda()
model.eval()
# Evaluate with late chunking
tasks = [
task_cls(
chunked_pooling_enabled=True,
tokenizer=tokenizer,
prune_size=None,
truncate_max_length=truncate_max_length,
long_late_chunking_embed_size=long_late_chunking_embed_size,
long_late_chunking_overlap_size=long_late_chunking_overlap_size,
**chunking_args,
)
]
evaluation = MTEB(
tasks=tasks,
chunked_pooling_enabled=True,
tokenizer=tokenizer,
prune_size=None,
**chunking_args,
)
evaluation.run(
model,
output_folder='results-chunked-pooling',
eval_splits=[eval_split],
overwrite_results=True,
batch_size=BATCH_SIZE,
encode_kwargs={'batch_size': BATCH_SIZE},
)
# Encode without late chunking
tasks = [
task_cls(
chunked_pooling_enabled=False,
tokenizer=tokenizer,
prune_size=None,
truncate_max_length=truncate_max_length,
**chunking_args,
)
]
evaluation = MTEB(
tasks=tasks,
chunked_pooling_enabled=False,
tokenizer=tokenizer,
prune_size=None,
**chunking_args,
)
evaluation.run(
model,
output_folder='results-normal-pooling',
eval_splits=[eval_split],
overwrite_results=True,
batch_size=BATCH_SIZE,
encode_kwargs={'batch_size': BATCH_SIZE},
)
if __name__ == '__main__':
main()