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
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
25 changes: 16 additions & 9 deletions ludwig/features/text_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,22 @@ def update_metrics(
decoded_targets, decoded_predictions = get_decoded_targets_and_predictions(targets, predictions, tokenizer)
for metric_name, metric_fn in self._metric_functions.items():
prediction_key = get_metric_tensor_input(metric_name)
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:
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)
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}")
Comment on lines +305 to +320
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.)


@staticmethod
def update_config_with_metadata(feature_config, feature_metadata, *args, **kwargs):
Expand Down
Loading