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

Wrap each metric update in try/except. #3562

Merged
merged 3 commits into from
Aug 30, 2023
Merged

Wrap each metric update in try/except. #3562

merged 3 commits into from
Aug 30, 2023

Conversation

justinxzhao
Copy link
Collaborator

No description provided.

Comment on lines +305 to +320
try:
if prediction_key == RESPONSE:
if tokenizer is not None:
# RESPONSE metrics cannot be computed if decoded texts are not provided.
# Decoded texts are only provided using the LLM model type.
if decoded_targets is not None and decoded_predictions is not None:
if metric_name == "bleu":
# BLEU takes in targets as a list.
metric_fn.update(decoded_predictions, [decoded_targets])
else:
metric_fn.update(decoded_predictions, decoded_targets)
else:
metric_fn = metric_fn.to(predictions[prediction_key].device)
metric_fn.update(predictions[prediction_key].detach(), targets)
except Exception as e:
logger.info(f"Ran into error when calculating metric {metric_name}. Skipping. The error is: {e}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, so will the metric function have no value then that we can collect later? Will that cause an error?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think we should be ok. get_metrics() is already wrapped in a try/except:

    def get_metrics(self):
        metric_vals = {}
        for metric_name, metric_fn in self._metric_functions.items():
            try:
                computed_metric = metric_fn.compute()
            except Exception as e:
                logger.exception(f"Caught exception computing metric: {metric_name} with error: {e}.")
                continue

And it also looks like there's default values for torchmetrics, even if no update() was called.

from torchmetrics.text import BLEUScore
bleu = BLEUScore()
bleu.compute()
/Users/justinzhao/ludwig/env/lib/python3.8/site-packages/torchmetrics/utilities/prints.py:36: UserWarning: The ``compute`` method of metric BLEUScore was called before the ``update`` method which may lead to errors, as metric states have not yet been updated.
  warnings.warn(*args, **kwargs)
tensor(0.)

@justinxzhao justinxzhao merged commit 6dfc68f into master Aug 30, 2023
11 of 15 checks passed
@justinxzhao justinxzhao deleted the try_catch_bleu branch August 30, 2023 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants