-
Notifications
You must be signed in to change notification settings - Fork 31.3k
Make gradient-checkpoint enabling tolerant of models without get_input_embeddings #42558
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
Open
molbap
wants to merge
23
commits into
main
Choose a base branch
from
fix_enable_grads_again
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
d5909a7
add embedding getter
molbap a9eb634
modify your own logic
molbap b520cc7
a common test
molbap 7ce45fe
some adapters are not PreTrainedModel s
molbap d41e204
few fixes
molbap 0e93a61
implement correct-ish fix?
molbap de8ff71
fixup
molbap b2618b3
this is needed likely
molbap 5d61150
woops
molbap ef55499
solving some cross-imports issues here and there
molbap 44ab4c6
more ximports issues
molbap fe89c1c
finally
molbap 2920d00
revert changes
molbap b8ccd0f
fixups
molbap b5ae5a6
improve message
molbap d209ff5
add common tests for input_ids first
molbap 79665d4
increase test coverage
molbap 844c707
Merge branch 'main' into fix_enable_grads_again
molbap fcc84a4
bigger update for GC
molbap e970fad
copies
molbap b4f5c15
mlcd is getting on my nerves a bit
molbap 0246a70
ah yes
molbap 81940dd
for BC
molbap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -969,51 +969,51 @@ def get_input_embeddings(self) -> nn.Module: | |
| `nn.Module`: A torch module mapping vocabulary to hidden states. | ||
| """ | ||
|
|
||
| # 1) Check if the model has an attribute named 'embed_tokens' (the standard input embedding layer | ||
| # for most NLP models), and if so, return it. | ||
|
|
||
| name = getattr(self, "_input_embed_layer", "embed_tokens") | ||
|
|
||
| # 1) Direct attribute (most NLP models). | ||
| if (default_embedding := getattr(self, name, None)) is not None: | ||
| return default_embedding | ||
| # 2) encoder/decoder and VLMs like `Gemma3nForConditionalGeneration` | ||
| # 2) Nested embeddings (e.g., self.embeddings.patch_embedding for vision/audio models). | ||
| if hasattr(self, "embeddings") and hasattr(self.embeddings, name): | ||
| return getattr(self.embeddings, name) | ||
| # 3) Encoder/decoder wrappers (e.g., `self.model.embed_tokens` or similar overrides). | ||
| if hasattr(self, "model") and hasattr(self.model, name): | ||
| return getattr(self.model, name) | ||
|
|
||
| if hasattr(self, "model") and hasattr(self.model, "embed_tokens"): | ||
| return self.model.embed_tokens | ||
| base_model = getattr(self, "base_model_prefix", None) | ||
| if base_model is not None: | ||
| base_model = getattr(self, base_model, None) | ||
| if base_model is not None and base_model is not self: | ||
| return base_model.get_input_embeddings() | ||
|
|
||
| # 3) vanilla decoder‑only architectures | ||
| elif hasattr(self, "embed_tokens"): | ||
| return self.embed_tokens | ||
| else: | ||
| base_model = getattr(self, "base_model_prefix", None) | ||
| if base_model is not None: | ||
| base_model = getattr(self, base_model, None) | ||
| if base_model is not None and base_model is not self: | ||
| return base_model.get_input_embeddings() | ||
| raise NotImplementedError( | ||
| f"`get_input_embeddings` not auto‑handled for {self.__class__.__name__}; " | ||
| "please override in the subclass." | ||
| ) | ||
| raise NotImplementedError( | ||
| f"`get_input_embeddings` not auto‑handled for {self.__class__.__name__}; please override in the subclass." | ||
| ) | ||
|
|
||
| def set_input_embeddings(self, value: nn.Module): | ||
| """Fallback setter that handles **~70%** of models in the code-base. | ||
|
|
||
| Order of attempts: | ||
| 1. `self.model.embed_tokens` | ||
| 2. `self.embed_tokens` | ||
| 3. delegate to the *base model* if one exists | ||
| 4. otherwise raise `NotImplementedError` so subclasses still can (and | ||
| 1. `self.<_input_embed_layer>` (direct attribute) | ||
| 2. `self.embeddings.<_input_embed_layer>` (nested embeddings for vision/audio models) | ||
| 3. `self.model.<_input_embed_layer>` (encoder/decoder models) | ||
| 4. delegate to the *base model* if one exists | ||
| 5. otherwise raise `NotImplementedError` so subclasses still can (and | ||
| should) override for exotic layouts. | ||
| """ | ||
|
|
||
| # 1) encoder/decoder and VLMs like `Gemma3nForConditionalGeneration` | ||
| name = getattr(self, "_input_embed_layer", "embed_tokens") | ||
| if hasattr(self, "model") and hasattr(self.model, name): | ||
| setattr(self.model, name, value) | ||
| # 2) as well as vanilla decoder‑only architectures | ||
| elif hasattr(self, name): | ||
| # 1) Direct attribute (most NLP models) | ||
| if hasattr(self, name): | ||
| setattr(self, name, value) | ||
| # 3) recurse once into the registered *base* model (e.g. for encoder/decoder) | ||
| # 2) Nested embeddings (e.g., self.embeddings.patch_embedding for vision models) | ||
| elif hasattr(self, "embeddings") and hasattr(self.embeddings, name): | ||
| setattr(self.embeddings, name, value) | ||
| # 3) encoder/decoder and VLMs like `Gemma3nForConditionalGeneration` | ||
| elif hasattr(self, "model") and hasattr(self.model, name): | ||
| setattr(self.model, name, value) | ||
| # 4) recurse once into the registered *base* model (e.g. for encoder/decoder) | ||
| elif getattr(self, self.base_model_prefix, self) is not self: | ||
| base_model = getattr(self, self.base_model_prefix, self) | ||
| base_model.set_input_embeddings(value) | ||
|
|
@@ -1983,9 +1983,12 @@ def make_inputs_require_grads(module, input, output): | |
| if not (isinstance(module, PreTrainedModel) and hasattr(module, "get_input_embeddings")): | ||
| continue | ||
|
|
||
| input_embeddings = module.get_input_embeddings() | ||
| try: | ||
| input_embeddings = module.get_input_embeddings() | ||
| except NotImplementedError: | ||
| continue | ||
|
Comment on lines
+1987
to
+1990
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no simple way around this unfortunately |
||
|
|
||
| if input_embeddings is None: | ||
| if input_embeddings is None or not hasattr(input_embeddings, "register_forward_hook"): | ||
| continue | ||
|
|
||
| embedding_id = id(input_embeddings) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit:
self.base_modelproperty has the same functionalityThere 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.
true!