-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Allow trainer to return eval. loss for CLIP-like models #20214
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
Conversation
|
The documentation is not available anymore as the PR was closed or merged. |
src/transformers/trainer.py
Outdated
| """ | ||
| has_labels = False if len(self.label_names) == 0 else all(inputs.get(k) is not None for k in self.label_names) | ||
| # For CLIP-like models capable of returning loss values. | ||
| can_compute_loss = True if len(self.label_names) == 0 and self.can_return_loss else False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to restrict to len(self.label_names) == 0.
For models that has len(self.label_names) > 0, we should check if the inputs contain the required labels by the model - which is done one line above for has_labels.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can_compute_loss actually means can_compute_loss_without_labels, but maybe a too long name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rename all can_compute_loss to loss_without_labels then? It's more informative even if not completely perfect.
sgugger
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice idea thanks a lot! I left a few comments regarding naming and we can make the function can_return_loss a bit better, but overall great PR!
src/transformers/trainer.py
Outdated
| """ | ||
| has_labels = False if len(self.label_names) == 0 else all(inputs.get(k) is not None for k in self.label_names) | ||
| # For CLIP-like models capable of returning loss values. | ||
| can_compute_loss = True if len(self.label_names) == 0 and self.can_return_loss else False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rename all can_compute_loss to loss_without_labels then? It's more informative even if not completely perfect.
src/transformers/utils/generic.py
Outdated
| signature = inspect.signature(model_class.__call__) | ||
| else: | ||
| signature = inspect.signature(model_class.forward) | ||
| return [p for p in signature.parameters if p == "return_loss"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer if this returns a bool then rely on Python magic conversion to bools.
Also I think we should check if the default is True as the Trainer won't change the default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to
any(p == "return_loss" for p in signature.parameters)but do you mean we should have sth. like (conceptually)
any(p == "return_loss" and default_value(p) is True for p in signature.parameters)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes to the second!
src/transformers/trainer.py
Outdated
| """ | ||
| has_labels = False if len(self.label_names) == 0 else all(inputs.get(k) is not None for k in self.label_names) | ||
| # For CLIP-like models capable of returning loss values. | ||
| loss_without_labels = True if len(self.label_names) == 0 and self.can_return_loss and inputs.get("return_loss", None) is True else False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inputs will not contain return_loss if True is the default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK! I see all of our return_loss have None as default value (include CLIP), but I can add extra check to be sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh in this case then your solution works.
| # If `return_loss` is not specified or being `None` in `inputs`, we check if the default value of `return_loss` | ||
| # is `True` in `model.forward`. | ||
| return_loss = inputs.get("return_loss", None) | ||
| if return_loss is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If False, no need to check default value
|
|
||
| for p in signature.parameters: | ||
| if p == "return_loss" and signature.parameters[p].default is True: | ||
| return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will return True only if the default value is True.
|
@sgugger Hopefully the change covers everything that could happen now and in the future. |
sgugger
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect!
…20214) * Allow trainer to return loss for CLIP-like models * Apply suggestions * update * update * update Co-authored-by: ydshieh <[email protected]>
What does this PR do?
Allow trainer to give evaluation loss for CLIP-like models.
Currently, this line
transformers/src/transformers/trainer.py
Line 3192 in 07d8d6e
gives
has_labels = Falsefor CLIP-like models, and can't give loss value in the evaluation.without this PR:
with this PR.