forked from jina-ai/late-chunking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_chunked_eval.py
115 lines (100 loc) · 2.84 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
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
@click.command()
@click.option(
'--model-name',
default='jinaai/jina-embeddings-v2-small-en',
help='The name of the model to use.',
)
@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.',
)
def main(model_name, strategy, task_name, eval_split, chunking_model):
try:
task_cls = globals()[task_name]
except:
raise ValueError(f'Unknown task name: {task_name}')
model, has_instructions = load_model(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
chunking_args = {
'chunk_size': DEFAULT_CHUNK_SIZE,
'n_sentences': DEFAULT_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,
**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,
**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()