haystack/test/benchmarks/retriever.py
bogdankostic ee2745bad8
ci: Add Github workflow to automate benchmark runs (#5399)
* Add config files

* log benchmarks to stdout

* Add top-k and batch size to configs

* Add batch size to configs

* fix: don't download files if they already exist

* Add batch size to configs

* refine script

* Remove configs using 1m docs

* update run script

* update run script

* update run script

* datadog integration

* remove out folder

* gitignore benchmarks output

* test: send benchmarks to datadog

* remove uncommented lines in script

* feat: take branch/tag argument for benchmark setup script

* fix: run.sh should ignore errors

* Add GH workflow to run benchmarks periodically

* Remove unused script

* Adapt cml.yml

* Adapt cml.yml

* Rename cml.yml to benchmarks.yml

* Revert "Rename cml.yml to benchmarks.yml"

This reverts commit 897299433a71a55827124728adff5de918d46d21.

* remove benchmarks.yml

* Use same file extension for all config files

* Use checkout@v3

* Run benchmarks sequentially

* Add timeout-minutes parameter

* Remove changes unrelated to datadog

* Apply black

* use haystack-oss aws account

* Update test/benchmarks/utils.py

Co-authored-by: Silvano Cerza <3314350+silvanocerza@users.noreply.github.com>

* PR feedback

* fix aws credentials step

* Fix path

* check docker

* Allow spinning up containers from within container

* Allow spinning up containers from within container

* Separate launching doc stores from benchmarks

* Remove docker related commands

* run only retrievers

* change port

* Revert "change port"

This reverts commit 6e5bcebb1d16e03ba7672be7e8a089084c7fc3a7.

* Run opensearch benchmark only

* Run weaviate benchmark only

* Run bm25 benchmarks only

* Changes host of doc stores

* add step to get docker logs

* Revert "add step to get docker logs"

This reverts commit c10e6faa76bde5df406a027203bd775d18c93c90.

* Install docker

* Launch doc store containers from wtihin runner container

* Remove kill command

* Change host

* dump docker logs

* change port

* Add cloud startup script

* dump docker logs

* add network param

* add network to startup.sh

* check cluster health

* move steps

* change port

* try using services

* check cluster health

* use services

* run only weaviate

* change host

* Upload benchmark results as artifacts

* Update configs

* Delete index after benchmark run

* Use correct index name

* Run only failing config

* Use smaller batch size

* Increase memory for opensearch

* Reduce batch size further

* Provide more storage

* Reduce batch size

* dump docker logs

* add java opts

* Spin up only opensearch container

* Create separate job for each doc store

* Run benchmarks sequentially

* Set working directory

* Account for reader benchmarks not doing indexing

* Change key of reader metrics

* Apply PR feedback

* Remove whitespace

* Adapt workflow to changes in datadog scripts

* Adapt workflow to changes in datadog scripts

* Increase memory for opensearch

* Reduce batch size

* Add preprocessing_batch_size to Readers

* Remove unrelated change

* Move order

* Fix path

* Manually terminate EC2 instance

Manually terminate EC2 instance

Manually terminate EC2 instance

Manually terminate EC2 instance

Manually terminate EC2 instance

Manually terminate EC2 instance

Manually terminate EC2 instance

Manually terminate EC2 instance

* Manually terminate EC2 instance

* Manually terminate EC2 instance

* Always terminate runner

* Always terminate runner

* Remove unnecessary terminate-runner job

* Add cron schedule

* Disable telemetry

* Rename cml.yml to benchmarks.yml

---------

Co-authored-by: rjanjua <rohan.janjua@gmail.com>
Co-authored-by: Paul Steppacher <p.steppacher91@gmail.com>
Co-authored-by: Silvano Cerza <3314350+silvanocerza@users.noreply.github.com>
Co-authored-by: Silvano Cerza <silvanocerza@gmail.com>
2023-08-17 12:56:45 +02:00

152 lines
5.8 KiB
Python

from pathlib import Path
from time import perf_counter
import logging
import datetime
import traceback
from typing import Dict
from haystack.nodes import BaseRetriever
from haystack import Pipeline
from haystack.utils import aggregate_labels
from utils import load_eval_data, get_retriever_config
def benchmark_retriever(
indexing_pipeline: Pipeline, querying_pipeline: Pipeline, documents_directory: Path, eval_set: Path
) -> Dict:
"""
Benchmark indexing and querying on retriever pipelines on a given dataset.
:param indexing_pipeline: Pipeline for indexing documents.
:param querying_pipeline: Pipeline for querying documents.
:param documents_directory: Directory containing files to index.
:param eval_set: Path to evaluation set.
"""
# Indexing
indexing_results = benchmark_indexing(indexing_pipeline, documents_directory)
# Querying
querying_results = benchmark_querying(querying_pipeline, eval_set)
results = {"indexing": indexing_results, "querying": querying_results}
doc_store = indexing_pipeline.get_document_store()
doc_store.delete_index(index="document")
return results
def benchmark_indexing(pipeline: Pipeline, documents_directory: Path) -> Dict:
"""
Benchmark indexing.
:param pipeline: Pipeline for indexing documents.
:param documents_directory: Directory containing files to index.
"""
try:
# Indexing Pipelines take a list of file paths as input
file_paths = [str(fp) for fp in documents_directory.iterdir() if fp.is_file() and not fp.name.startswith(".")]
# Indexing
start_time = perf_counter()
pipeline.run_batch(file_paths=file_paths)
end_time = perf_counter()
indexing_time = end_time - start_time
n_docs = len(file_paths)
retrievers = pipeline.get_nodes_by_class(BaseRetriever)
retriever_type = retrievers[0].__class__.__name__ if retrievers else "No component of type BaseRetriever found"
doc_store = pipeline.get_document_store()
doc_store_type = doc_store.__class__.__name__ if doc_store else "No DocumentStore found"
results = {
"retriever": retriever_type,
"doc_store": doc_store_type,
"n_docs": n_docs,
"indexing_time": indexing_time,
"docs_per_second": n_docs / indexing_time,
"date_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"error": None,
}
except Exception:
tb = traceback.format_exc()
logging.error("##### The following Error was raised while running indexing run:")
logging.error(tb)
retrievers = pipeline.get_nodes_by_class(BaseRetriever)
retriever_type = retrievers[0].__class__.__name__ if retrievers else "No component of type BaseRetriever found"
doc_store = pipeline.get_document_store()
doc_store_type = doc_store.__class__.__name__ if doc_store else "No DocumentStore found"
results = {
"retriever": retriever_type,
"doc_store": doc_store_type,
"n_docs": 0,
"indexing_time": 0,
"docs_per_second": 0,
"date_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"error": str(tb),
}
return results
def benchmark_querying(pipeline: Pipeline, eval_set: Path) -> Dict:
"""
Benchmark querying. This method should only be called if indexing has already been done.
:param pipeline: Pipeline for querying documents.
:param eval_set: Path to evaluation set.
"""
try:
# Load eval data
labels, _ = load_eval_data(eval_set)
multi_labels = aggregate_labels(labels)
queries = [label.query for label in multi_labels]
# Run querying
start_time = perf_counter()
predictions = pipeline.run_batch(queries=queries, labels=multi_labels, debug=True)
end_time = perf_counter()
querying_time = end_time - start_time
# Evaluate predictions
eval_result = pipeline._generate_eval_result_from_batch_preds(predictions_batches=predictions)
metrics = eval_result.calculate_metrics()["Retriever"]
retriever_type, retriever_top_k = get_retriever_config(pipeline)
doc_store = pipeline.get_document_store()
doc_store_type = doc_store.__class__.__name__ if doc_store else "No DocumentStore found"
results = {
"retriever": retriever_type,
"doc_store": doc_store_type,
"n_docs": doc_store.get_document_count(),
"n_queries": len(labels),
"querying_time": querying_time,
"queries_per_second": len(labels) / querying_time,
"seconds_per_query": querying_time / len(labels),
"recall": metrics["recall_single_hit"],
"map": metrics["map"],
"top_k": retriever_top_k,
"date_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"error": None,
}
except Exception:
tb = traceback.format_exc()
logging.error("##### The following Error was raised while running querying run:")
logging.error(tb)
retriever_type, retriever_top_k = get_retriever_config(pipeline)
doc_store = pipeline.get_document_store()
doc_store_type = doc_store.__class__.__name__ if doc_store else "No DocumentStore found"
results = {
"retriever": retriever_type,
"doc_store": doc_store_type,
"n_docs": 0,
"n_queries": 0,
"retrieve_time": 0,
"queries_per_second": 0,
"seconds_per_query": 0,
"recall": 0,
"map": 0,
"top_k": retriever_top_k,
"date_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"error": str(tb),
}
return results