Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPEX benchmarking fix #58

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions scripts/optimizations/embedders/embedders.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, List, Union
from typing import Any, Dict, List, Union

import numpy as np
import torch
Expand Down Expand Up @@ -115,6 +115,16 @@ def _mean_pooling(outputs, attention_mask):


class EmbedderModelMTEB(EmbedderModel):
def encode(
self, sentences: list[str], batch_size=32, **kwargs: Any
) -> torch.Tensor | np.ndarray:
return self.encode_sentences(
sentences=sentences,
batch_size=batch_size,
normalize=True,
convert_to_numpy=True,
)

def encode_queries(self, queries: List[str], batch_size=32, **kwargs):
if self.query_prompt:
sentences = [self.query_prompt + q for q in queries]
Expand All @@ -133,9 +143,11 @@ def encode_corpus(
sep = " "
if type(corpus[0]) is dict:
sentences = [
(doc["title"].strip() + sep + doc["text"]).strip()
if "title" in doc
else doc["text"].strip()
(
(doc["title"].strip() + sep + doc["text"]).strip()
if "title" in doc
else doc["text"].strip()
)
for doc in corpus
]
else:
Expand Down
54 changes: 39 additions & 15 deletions scripts/optimizations/embedders/quantize_embedder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from neural_compressor.config import PostTrainingQuantConfig
from optimum.intel import INCQuantizer, IPEXModel
from sentence_transformers import SentenceTransformer
from simple_parsing import field
from transformers import AutoModel, AutoTokenizer


Expand Down Expand Up @@ -125,10 +124,41 @@ def preprocess_function(examples):
],
"retrieval": [
"ArguAna",
# "ClimateFEVER",
# "FEVER",
# "FiQA2018",
# "HotpotQA",
"ClimateFEVER",
"CQADupstackAndroidRetrieval",
"CQADupstackEnglishRetrieval",
"CQADupstackGamingRetrieval",
"CQADupstackGisRetrieval",
"CQADupstackMathematicaRetrieval",
"CQADupstackPhysicsRetrieval",
"CQADupstackProgrammersRetrieval",
"CQADupstackStatsRetrieval",
"CQADupstackTexRetrieval",
"CQADupstackUnixRetrieval",
"CQADupstackWebmastersRetrieval",
"CQADupstackWordpressRetrieval",
"DBPedia",
"FaithDial",
"FeedbackQARetrieval",
"FEVER",
"FiQA2018",
"HagridRetrieval",
"HotpotQA",
"LegalBenchConsumerContractsQA",
"LegalBenchCorporateLobbying",
"LegalSummarization",
"MLQuestions",
"MSMARCO",
"NarrativeQARetrieval",
"NFCorpus",
"NQ",
"RARbCode",
"RARbMath",
"SCIDOCS",
"SciFact",
"TopiOCQA",
"Touche2020",
"TRECCOVID",
],
}

Expand All @@ -137,8 +167,8 @@ def _gather_rerank_results(results):
res = {}
total = 0.0
for task in results:
res[task] = results[task]["test"]["map"]
total += res[task]
res[task.task_name] = task.scores["test"][0]["map"]
total += res[task.task_name]
res["avg"] = total / len(results)
return res

Expand All @@ -147,7 +177,7 @@ def _gather_retrieval_results(results):
res = {}
total = 0.0
for task in results:
res[task] = results[task]["test"]["ndcg_at_10"]
res[task.task_name] = task.scores["test"][0]["ndcg_at_10"]
total += res[task]
res["avg"] = total / len(results)
return res
Expand All @@ -158,14 +188,8 @@ def _gather_retrieval_results(results):
"retrieval": _gather_retrieval_results,
}

TASK_TYPES = {
"rerank": "Reranking",
"retrieval": "Retrieval"
}

def _run_validation(model, task, model_path):
tasks = mteb.get_tasks(task_types=TASK_TYPES[task], languages=["eng"])
evaluation = MTEB(tasks=tasks)
evaluation = MTEB(tasks=mteb.get_tasks(tasks=benchmarks[task]))
results = evaluation.run(
model, overwrite_results=True, output_folder=model_path, eval_splits=["test"]
)
Expand Down