Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
41fa672d
Unverified
Commit
41fa672d
authored
Feb 14, 2023
by
Younes Belkada
Committed by
GitHub
Feb 14, 2023
Browse files
Enable `requires_grad` on input embedding to train on top of frozen layers (#21598)
* v1 * make fixup * add more methods
parent
8c502662
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
0 deletions
+17
-0
src/transformers/modeling_utils.py
src/transformers/modeling_utils.py
+17
-0
No files found.
src/transformers/modeling_utils.py
View file @
41fa672d
...
@@ -1148,6 +1148,23 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix
...
@@ -1148,6 +1148,23 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix
return
False
return
False
return
True
return
True
def
enable_input_require_grads
(
self
):
"""
Enables the gradients for the input embeddings. This is useful for fine-tuning adapter weights while keeping
the model weights fixed.
"""
def
make_inputs_require_grads
(
module
,
input
,
output
):
output
.
requires_grad_
(
True
)
self
.
_require_grads_hook
=
self
.
get_input_embeddings
().
register_forward_hook
(
make_inputs_require_grads
)
def
disable_input_require_grads
(
self
):
"""
Removes the `_require_grads_hook`.
"""
self
.
_require_grads_hook
.
remove
()
def
get_input_embeddings
(
self
)
->
nn
.
Module
:
def
get_input_embeddings
(
self
)
->
nn
.
Module
:
"""
"""
Returns the model's input embeddings.
Returns the model's input embeddings.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment