Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

WIP: Remove Asserts#444

Closed
brandenchan wants to merge 1 commit intomasterfrom
remove_assert
Closed

WIP: Remove Asserts#444
brandenchan wants to merge 1 commit intomasterfrom
remove_assert

Conversation

@brandenchan
Copy link
Copy Markdown
Contributor

The idea of this PR generally is to remove assertion statement so that running systems do not get interrupted by errors. What used to be asserts will, where appropriate, be converted into logging.error() or logging.warning().

This PR focuses only on QA related components. Asserts may still be present if they can be caught by try catch statements such as Processor._featurize_samples() or Processor._init_samples_in_baskets().

Copy link
Copy Markdown
Contributor

@Timoeller Timoeller left a comment

Choose a reason for hiding this comment

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

I made some suggestions in the code

logit_input = logits is not None
preds_input = preds is not None

if logit_input and preds_input:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

syntax seems confusing:
how about

if logits and preds: logger.warning("Both logits and preds have been passed as input to the TextClassificationHead")

if (logits is None) and (preds is None): logger.error("Neither logits nor preds have been passed as input to the TextClassificationHead")

(f"Label_tensor_names are missing inside the {head.task_name} Prediction Head. Did you connect the model"
" with the processor through either 'model.connect_heads_with_processor(processor.tasks)'"
" or by passing the processor to the Adaptive Model?")
if not hasattr(head, "label_tensor_name"):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure about this one. Can the model work without this? e.g. if it is in inference mode.

If so we should remove the assert, if not and the functionality must break further downstream, then lets keep the assert here

if logit_input:
logger.warning("QuestionAnsweringHead.formatted_preds() received logit input when it only expects pred input")
if not preds_input:
logger.warning("QuestionAnsweringHead.formatted_preds() did not receive the preds input it expects")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this should be at least an logger.error or even kept as assert, since the app breaks without preds

# are prediction spans
preds_d = self.aggregate_preds(preds, passage_start_t, ids, seq_2_start_t)

assert len(preds_d) == len(baskets)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we should through a logger error here, since we need as many preds_d (on document level) as we have baskets.
If that is not the case it isnt necessarily breaking a cosuming app. Example: haystack retrieves 10 docs, one of those documents is malformatted so it cannot contain preds. Still we want to give back preds_d on all other 9 docs so the reader produces some answer

else:
start_of_word.append(False)

assert len(tokens) == len(token_offsets) == len(start_of_word)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

here we either need tests or keep the assert?


# This fn is used to align QA output of len=n_docs and Classification output of len=n_passages
def chunk(iterable, lengths):
assert sum(lengths) == len(iterable)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I do not know what this assert does... lets discuss in detail or throw a logger.error?

@brandenchan
Copy link
Copy Markdown
Contributor Author

See #468 for how these changes were actually implemented

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants