Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9d1f2dd
add basic pipeline.eval_batch for qa without filters
julian-risch Aug 1, 2022
4a398a3
Merge branch 'master' into batch-eval
julian-risch Aug 1, 2022
59b64c3
black formatting
julian-risch Aug 1, 2022
789cc2d
pydoc-markdown
julian-risch Aug 1, 2022
9a6c681
remove batch eval tests failing due to bugs
julian-risch Aug 3, 2022
671826a
remove comment
julian-risch Aug 8, 2022
ad40a55
explain commented out tests
julian-risch Aug 8, 2022
1bda44b
avoid code duplication
julian-risch Aug 8, 2022
7bacd93
black
julian-risch Aug 8, 2022
8cb7a51
mypy
julian-risch Aug 8, 2022
5cf8f0a
pydoc markdown
julian-risch Aug 8, 2022
b3b57f5
add batch option to execute_eval_run
julian-risch Aug 8, 2022
9aad547
pydoc markdown
julian-risch Aug 8, 2022
313a5e8
Merge branch 'master' into batch-eval
julian-risch Aug 8, 2022
2738994
Apply documentation suggestions from code review
julian-risch Aug 9, 2022
d184d20
Apply documentation suggestion from code review
julian-risch Aug 9, 2022
07682eb
add documentation based on review comments
julian-risch Aug 9, 2022
a2d4d6f
Merge branch 'batch-eval' of github.com:deepset-ai/haystack into batc…
julian-risch Aug 9, 2022
195f8a1
black
julian-risch Aug 9, 2022
17f750d
black
julian-risch Aug 9, 2022
16076ea
schema updates
julian-risch Aug 9, 2022
7339b45
remove duplicate tests
julian-risch Aug 9, 2022
0fa1bcb
add separate method for column reordering
julian-risch Aug 9, 2022
a1ac6b4
merge _build_eval_dataframe methods
julian-risch Aug 17, 2022
afd03a5
pylint ignore in function
julian-risch Aug 17, 2022
4b0b242
change type annotation of queries to list only
julian-risch Aug 24, 2022
ab4dccb
one-liner addressing review comment on params dict
julian-risch Aug 24, 2022
af434ee
black
julian-risch Aug 25, 2022
16cf698
markdown files updated
julian-risch Aug 25, 2022
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
107 changes: 105 additions & 2 deletions docs/_src/api/api/pipelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ Each metric is represented by a dictionary containing the scores for each top_k

```python
@classmethod
def execute_eval_run(cls, index_pipeline: Pipeline, query_pipeline: Pipeline, evaluation_set_labels: List[MultiLabel], corpus_file_paths: List[str], experiment_name: str, experiment_run_name: str, experiment_tracking_tool: Literal["mlflow", None] = None, experiment_tracking_uri: Optional[str] = None, corpus_file_metas: List[Dict[str, Any]] = None, corpus_meta: Dict[str, Any] = {}, evaluation_set_meta: Dict[str, Any] = {}, pipeline_meta: Dict[str, Any] = {}, index_params: dict = {}, query_params: dict = {}, sas_model_name_or_path: str = None, sas_batch_size: int = 32, sas_use_gpu: bool = True, add_isolated_node_eval: bool = False, reuse_index: bool = False, custom_document_id_field: Optional[str] = None, document_scope: Literal[
def execute_eval_run(cls, index_pipeline: Pipeline, query_pipeline: Pipeline, evaluation_set_labels: List[MultiLabel], corpus_file_paths: List[str], experiment_name: str, experiment_run_name: str, experiment_tracking_tool: Literal["mlflow", None] = None, experiment_tracking_uri: Optional[str] = None, corpus_file_metas: List[Dict[str, Any]] = None, corpus_meta: Dict[str, Any] = {}, evaluation_set_meta: Dict[str, Any] = {}, pipeline_meta: Dict[str, Any] = {}, index_params: dict = {}, query_params: dict = {}, sas_model_name_or_path: str = None, sas_batch_size: int = 32, sas_use_gpu: bool = True, use_batch_mode: bool = False, add_isolated_node_eval: bool = False, reuse_index: bool = False, custom_document_id_field: Optional[str] = None, document_scope: Literal[
"document_id",
"context",
"document_id_and_context",
Expand Down Expand Up @@ -454,6 +454,7 @@ If you use custom cross encoders please make sure they work with sentence_transf
- `sas_batch_size`: Number of prediction label pairs to encode at once by CrossEncoder or SentenceTransformer while calculating SAS.
- `sas_use_gpu`: Whether to use a GPU or the CPU for calculating semantic answer similarity.
Falls back to CPU if no GPU is available.
- `use_batch_mode`: Whether to use batches for pipeline executions or single queries (default).
- `add_isolated_node_eval`: If set to True, in addition to the integrated evaluation of the pipeline, each node is evaluated in isolated evaluation mode.
This mode helps to understand the bottlenecks of a pipeline in terms of output quality of each individual node.
If a node performs much better in the isolated evaluation than in the integrated evaluation, the previous node needs to be optimized to improve the pipeline's performance.
Expand Down Expand Up @@ -509,7 +510,7 @@ Thus [AB] <-> [BC] (score ~50) gets recalculated with B <-> B (score ~100) scori

```python
@send_event
def eval(labels: List[MultiLabel], documents: Optional[List[List[Document]]] = None, params: Optional[dict] = None, sas_model_name_or_path: str = None, sas_batch_size: int = 32, sas_use_gpu: bool = True, add_isolated_node_eval: bool = False, custom_document_id_field: Optional[str] = None, context_matching_min_length: int = 100, context_matching_boost_split_overlaps: bool = True, context_matching_threshold: float = 65.0) -> EvaluationResult
def eval(labels: List[MultiLabel], documents: Optional[List[List[Document]]] = None, params: Optional[dict] = None, sas_model_name_or_path: Optional[str] = None, sas_batch_size: int = 32, sas_use_gpu: bool = True, add_isolated_node_eval: bool = False, custom_document_id_field: Optional[str] = None, context_matching_min_length: int = 100, context_matching_boost_split_overlaps: bool = True, context_matching_threshold: float = 65.0) -> EvaluationResult
```

Evaluates the pipeline by running the pipeline once per query in debug mode
Expand Down Expand Up @@ -564,6 +565,67 @@ we cut the context on the same side, recalculate the score and take the mean of
Thus [AB] <-> [BC] (score ~50) gets recalculated with B <-> B (score ~100) scoring ~75 in total.
- `context_matching_threshold`: Score threshold that candidates must surpass to be included into the result list. Range: [0,100]

<a id="base.Pipeline.eval_batch"></a>

#### Pipeline.eval\_batch

```python
@send_event
def eval_batch(labels: List[MultiLabel], documents: Optional[List[List[Document]]] = None, params: Optional[dict] = None, sas_model_name_or_path: Optional[str] = None, sas_batch_size: int = 32, sas_use_gpu: bool = True, add_isolated_node_eval: bool = False, custom_document_id_field: Optional[str] = None, context_matching_min_length: int = 100, context_matching_boost_split_overlaps: bool = True, context_matching_threshold: float = 65.0) -> EvaluationResult
```

Evaluates the pipeline by running the pipeline in batches in debug mode
Comment thread
julian-risch marked this conversation as resolved.
Outdated

and putting together all data that is needed for evaluation, e.g. calculating metrics.
Comment thread
julian-risch marked this conversation as resolved.
Outdated

If you want to calculate SAS (Semantic Answer Similarity) metrics, you have to specify `sas_model_name_or_path`.
Comment thread
julian-risch marked this conversation as resolved.
Outdated

You will be able to control the scope within which an answer or a document is considered correct afterwards (See `document_scope` and `answer_scope` params in `EvaluationResult.calculate_metrics()`).
Comment thread
julian-risch marked this conversation as resolved.
Outdated
Some of these scopes require additional information that already needs to be specified during `eval()`:
Comment thread
julian-risch marked this conversation as resolved.
Outdated
- `custom_document_id_field` param to select a custom document ID from document's meta data for ID matching (only affects 'document_id' scopes)
Comment thread
julian-risch marked this conversation as resolved.
Outdated
- `context_matching_...` param to fine-tune the fuzzy matching mechanism that determines whether some text contexts match each other (only affects 'context' scopes, default values should work most of the time)
Comment thread
julian-risch marked this conversation as resolved.
Outdated

**Arguments**:

- `labels`: The labels to evaluate on
Comment thread
julian-risch marked this conversation as resolved.
Outdated
- `documents`: List of List of Document that the first node in the pipeline should get as input per multilabel. Can be used to evaluate a pipeline that consists of a reader without a retriever.
Comment thread
julian-risch marked this conversation as resolved.
Outdated
- `params`: Dictionary of parameters to be dispatched to the nodes.
If you want to pass a param to all nodes, you can just use: {"top_k":10}
Comment thread
julian-risch marked this conversation as resolved.
Outdated
If you want to pass it to targeted nodes, you can do:
{"Retriever": {"top_k": 10}, "Reader": {"top_k": 3, "debug": True}}
- `sas_model_name_or_path`: Name or path of "Semantic Answer Similarity (SAS) model". When set, the model will be used to calculate similarity between predictions and labels and generate the SAS metric.
The SAS metric correlates better with human judgement of correct answers as it does not rely on string overlaps.
Example: Prediction = "30%", Label = "thirty percent", EM and F1 would be overly pessimistic with both being 0, while SAS paints a more realistic picture.
More info in the paper: https://arxiv.org/abs/2108.06130
Models:
- You can use Bi Encoders (sentence transformers) or cross encoders trained on Semantic Textual Similarity (STS) data.
Not all cross encoders can be used because of different return types.
If you use custom cross encoders please make sure they work with sentence_transformers.CrossEncoder class
- Good default for multiple languages: "sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
- Large, powerful, but slow model for English only: "cross-encoder/stsb-roberta-large"
- Large model for German only: "deepset/gbert-large-sts"
- `sas_batch_size`: Number of prediction label pairs to encode at once by CrossEncoder or SentenceTransformer while calculating SAS.
- `sas_use_gpu`: Whether to use a GPU or the CPU for calculating semantic answer similarity.
Falls back to CPU if no GPU is available.
- `add_isolated_node_eval`: If set to True, in addition to the integrated evaluation of the pipeline, each node is evaluated in isolated evaluation mode.
This mode helps to understand the bottlenecks of a pipeline in terms of output quality of each individual node.
If a node performs much better in the isolated evaluation than in the integrated evaluation, the previous node needs to be optimized to improve the pipeline's performance.
If a node's performance is similar in both modes, this node itself needs to be optimized to improve the pipeline's performance.
The isolated evaluation calculates the upper bound of each node's evaluation metrics under the assumption that it received perfect inputs from the previous node.
To this end, labels are used as input to the node instead of the output of the previous node in the pipeline.
The generated dataframes in the EvaluationResult then contain additional rows, which can be distinguished from the integrated evaluation results based on the
values "integrated" or "isolated" in the column "eval_mode" and the evaluation report then additionally lists the upper bound of each node's evaluation metrics.
- `custom_document_id_field`: Custom field name within `Document`'s `meta` which identifies the document and is being used as criterion for matching documents to labels during evaluation.
This is especially useful if you want to match documents on other criteria (e.g. file names) than the default document ids as these could be heavily influenced by preprocessing.
If not set (default) the `Document`'s `id` is being used as criterion for matching documents to labels.
- `context_matching_min_length`: The minimum string length context and candidate need to have in order to be scored.
Returns 0.0 otherwise.
- `context_matching_boost_split_overlaps`: Whether to boost split overlaps (e.g. [AB] <-> [BC]) that result from different preprocessing params.
If we detect that the score is near a half match and the matching part of the candidate is at its boundaries
we cut the context on the same side, recalculate the score and take the mean of both.
Thus [AB] <-> [BC] (score ~50) gets recalculated with B <-> B (score ~100) scoring ~75 in total.
- `context_matching_threshold`: Score threshold that candidates must surpass to be included into the result list. Range: [0,100]

<a id="base.Pipeline.get_nodes_by_class"></a>

#### Pipeline.get\_nodes\_by\_class
Expand Down Expand Up @@ -1241,6 +1303,47 @@ we cut the context on the same side, recalculate the score and take the mean of
Thus [AB] <-> [BC] (score ~50) gets recalculated with B <-> B (score ~100) scoring ~75 in total.
- `context_matching_threshold`: Score threshold that candidates must surpass to be included into the result list. Range: [0,100]

<a id="standard_pipelines.BaseStandardPipeline.eval_batch"></a>

#### BaseStandardPipeline.eval\_batch

```python
def eval_batch(labels: List[MultiLabel], params: Optional[dict] = None, sas_model_name_or_path: Optional[str] = None, sas_batch_size: int = 32, sas_use_gpu: bool = True, add_isolated_node_eval: bool = False, custom_document_id_field: Optional[str] = None, context_matching_min_length: int = 100, context_matching_boost_split_overlaps: bool = True, context_matching_threshold: float = 65.0) -> EvaluationResult
```

Evaluates the pipeline by running the pipeline once per query in debug mode

and putting together all data that is needed for evaluation, e.g. calculating metrics.

If you want to calculate SAS (Semantic Answer Similarity) metrics, you have to specify `sas_model_name_or_path`.

You will be able to control the scope within which an answer or a document is considered correct afterwards (See `document_scope` and `answer_scope` params in `EvaluationResult.calculate_metrics()`).
Some of these scopes require additional information that already needs to be specified during `eval()`:
- `custom_document_id_field` param to select a custom document ID from document's meta data for ID matching (only affects 'document_id' scopes)
- `context_matching_...` param to fine-tune the fuzzy matching mechanism that determines whether some text contexts match each other (only affects 'context' scopes, default values should work most of the time)

**Arguments**:

- `labels`: The labels to evaluate on
- `params`: Params for the `retriever` and `reader`. For instance,
params={"Retriever": {"top_k": 10}, "Reader": {"top_k": 5}}
- `sas_model_name_or_path`: SentenceTransformers semantic textual similarity model to be used for sas value calculation,
should be path or string pointing to downloadable models.
- `sas_batch_size`: Number of prediction label pairs to encode at once by CrossEncoder or SentenceTransformer while calculating SAS.
- `sas_use_gpu`: Whether to use a GPU or the CPU for calculating semantic answer similarity.
Falls back to CPU if no GPU is available.
- `add_isolated_node_eval`: Whether to additionally evaluate the reader based on labels as input instead of output of previous node in pipeline
- `custom_document_id_field`: Custom field name within `Document`'s `meta` which identifies the document and is being used as criterion for matching documents to labels during evaluation.
This is especially useful if you want to match documents on other criteria (e.g. file names) than the default document ids as these could be heavily influenced by preprocessing.
If not set (default) the `Document`'s `id` is being used as criterion for matching documents to labels.
- `context_matching_min_length`: The minimum string length context and candidate need to have in order to be scored.
Returns 0.0 otherwise.
- `context_matching_boost_split_overlaps`: Whether to boost split overlaps (e.g. [AB] <-> [BC]) that result from different preprocessing params.
If we detect that the score is near a half match and the matching part of the candidate is at its boundaries
we cut the context on the same side, recalculate the score and take the mean of both.
Thus [AB] <-> [BC] (score ~50) gets recalculated with B <-> B (score ~100) scoring ~75 in total.
- `context_matching_threshold`: Score threshold that candidates must surpass to be included into the result list. Range: [0,100]

<a id="standard_pipelines.BaseStandardPipeline.print_eval_report"></a>

#### BaseStandardPipeline.print\_eval\_report
Expand Down
2 changes: 1 addition & 1 deletion docs/_src/api/api/retriever.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ Otherwise raw similarity scores (e.g. cosine or dot_product) will be used.
#### TfidfRetriever.retrieve\_batch

```python
def retrieve_batch(queries: List[str], filters: Optional[Dict[str, Union[Dict, List, str, int, float, bool]]] = None, top_k: Optional[int] = None, index: str = None, headers: Optional[Dict[str, str]] = None, batch_size: Optional[int] = None, scale_score: bool = None) -> List[List[Document]]
def retrieve_batch(queries: Union[str, List[str]], filters: Optional[Dict[str, Union[Dict, List, str, int, float, bool]]] = None, top_k: Optional[int] = None, index: str = None, headers: Optional[Dict[str, str]] = None, batch_size: Optional[int] = None, scale_score: bool = None) -> List[List[Document]]
```

Scan through documents in DocumentStore and return a small number documents
Expand Down
35 changes: 33 additions & 2 deletions haystack/nodes/reader/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ def run(self, query: str, documents: List[Document], top_k: Optional[int] = None

def run_batch( # type: ignore
self,
queries: List[str],
queries: Union[str, List[str]],
Comment thread
julian-risch marked this conversation as resolved.
Outdated
documents: Union[List[Document], List[List[Document]]],
top_k: Optional[int] = None,
batch_size: Optional[int] = None,
labels: Optional[List[MultiLabel]] = None,
add_isolated_node_eval: bool = False,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this parameter add_isolated_node_eval? As a user of this API it wasn't clear to me immediately what it is about and why do we need it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we need it. It's the same parameter as in the standard run(). If it is set to True, the evaluation is executed with labels as node inputs in addition to the integrated evaluation, where the node inputs are the outputs of the previous node in the pipeline.

):
self.query_count += len(queries) if isinstance(queries, list) else 1
if not documents:
Expand All @@ -129,8 +131,37 @@ def run_batch( # type: ignore
flattened_documents.extend(doc_list)
else:
flattened_documents.append(doc_list)
for answer in answer_iterator:

results["answers_isolated"] = [
BaseReader.add_doc_meta_data_to_answer(documents=flattened_documents, answer=answer)
for answer in answer_iterator
]
Comment thread
julian-risch marked this conversation as resolved.
Outdated

# run evaluation with labels as node inputs
if add_isolated_node_eval and labels is not None:
relevant_documents = []
for labelx in labels:
relevant_documents.append([label.document for label in labelx.labels])
results_label_input = predict_batch(queries=queries, documents=relevant_documents, top_k=top_k)

# Add corresponding document_name and more meta data, if an answer contains the document_id
answer_iterator = itertools.chain.from_iterable(results_label_input["answers"])
if isinstance(documents[0], Document):
if isinstance(queries, list):
answer_iterator = itertools.chain.from_iterable(
itertools.chain.from_iterable(results_label_input["answers"])
)
flattened_documents = []
for doc_list in documents:
if isinstance(doc_list, list):
flattened_documents.extend(doc_list)
else:
flattened_documents.append(doc_list)

results["answers_isolated"] = [
BaseReader.add_doc_meta_data_to_answer(documents=flattened_documents, answer=answer)
for answer in answer_iterator
]
Comment thread
julian-risch marked this conversation as resolved.
Outdated

return results, "output_1"

Expand Down
2 changes: 1 addition & 1 deletion haystack/nodes/retriever/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def retrieve(

def retrieve_batch(
self,
queries: List[str],
queries: Union[str, List[str]],
filters: Optional[Dict[str, Union[Dict, List, str, int, float, bool]]] = None,
top_k: Optional[int] = None,
index: str = None,
Expand Down
Loading