Skip to content

Fix model_accepts_loss_kwargs for timm model#35257

Merged
qubvel merged 3 commits intohuggingface:mainfrom
qubvel:fix-loss-kwargs-for-timm-model
Dec 27, 2024
Merged

Fix model_accepts_loss_kwargs for timm model#35257
qubvel merged 3 commits intohuggingface:mainfrom
qubvel:fix-loss-kwargs-for-timm-model

Conversation

@qubvel
Copy link
Copy Markdown
Contributor

@qubvel qubvel commented Dec 13, 2024

What does this PR do?

TimmWrapper model training is broken due to the num_items_in_batch param passed to the model forward. TimmWrapper model has **kwargs in the transformers model forward, but it goes straight to the timm model and not to the loss. This PR introduces a fix to avoid passing loss_kwargs even if **kwargs are in model forward.

Relevant to:

cc @ArthurZucker

@qubvel qubvel requested a review from ArthurZucker December 13, 2024 11:53
@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Copy Markdown
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Okay, I think we might want to document accepts_loss_kwargs as it's scope right now is super small.
This can't be set to True by default, we can just add a property that checks the forward signature, unless _accept_loss_kwargs is set to false.

@qubvel
Copy link
Copy Markdown
Contributor Author

qubvel commented Dec 17, 2024

@ArthurZucker Do you mean to move it to the PretrainedModel level?

class PreTrainedModel:

    _accept_loss_kwargs = None
    
    @property
    def accepts_loss_kwargs(self):
        if self._accept_loss_kwargs is not None:
            return self._accept_loss_kwargs
        # otherwise check for signature here
        ...
        return accept_loss_kwargs

And then in modeling:

class MyModel(PreTrainedModel):
    _accept_loss_kwargs = False

@ArthurZucker
Copy link
Copy Markdown
Collaborator

No for now let's merge as is! Not sure we'll need the abstraction for more models!

@qubvel qubvel merged commit 5c75087 into huggingface:main Dec 27, 2024
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.

3 participants