Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
LLaMA-Factory
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dbis-public
LLaMA-Factory
Commits
38a56706
Unverified
Commit
38a56706
authored
1 year ago
by
hoshi-hiyouga
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Update utils.py
parent
a950f3b8
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/llmtuner/model/utils.py
+33
-37
33 additions, 37 deletions
src/llmtuner/model/utils.py
with
33 additions
and
37 deletions
src/llmtuner/model/utils.py
+
33
−
37
View file @
38a56706
from
enum
import
Enum
,
unique
from
typing
import
TYPE_CHECKING
,
Dict
,
List
from
functools
import
partial
from
typing
import
TYPE_CHECKING
,
Any
,
Dict
,
List
,
Optional
import
torch
from
transformers
import
PreTrainedModel
...
...
@@ -100,6 +101,37 @@ def find_expanded_modules(model: "PreTrainedModel", target_modules: List[str], n
return
module_names
def
gradient_checkpointing_enable
(
self
:
"
PreTrainedModel
"
,
gradient_checkpointing_kwargs
:
Optional
[
Dict
[
str
,
Any
]]
=
None
)
->
None
:
r
"""
Activates gradient checkpointing for the current model.
Modification of the original method to enable gradient checkpointing for block-wise optimizer.
"""
from
torch.utils.checkpoint
import
checkpoint
if
not
self
.
supports_gradient_checkpointing
:
raise
ValueError
(
"
{} does not support gradient checkpointing.
"
.
format
(
self
.
__class__
.
__name__
))
if
gradient_checkpointing_kwargs
is
None
:
gradient_checkpointing_kwargs
=
{
"
use_reentrant
"
:
True
}
gradient_checkpointing_func
=
partial
(
checkpoint
,
**
gradient_checkpointing_kwargs
)
def
custom_gradient_checkpointing_func
(
func
,
*
args
,
**
kwargs
):
module
:
"
torch.nn.Module
"
=
func
.
__self__
if
any
(
param
.
requires_grad
for
param
in
module
.
parameters
()):
for
arg
in
args
:
if
torch
.
is_tensor
(
arg
)
and
torch
.
is_floating_point
(
arg
):
arg
.
requires_grad_
(
True
)
return
gradient_checkpointing_func
(
func
,
*
args
,
**
kwargs
)
self
.
_set_gradient_checkpointing
(
enable
=
True
,
gradient_checkpointing_func
=
custom_gradient_checkpointing_func
)
def
load_valuehead_params
(
path_or_repo_id
:
str
,
model_args
:
"
ModelArguments
"
)
->
Dict
[
str
,
torch
.
Tensor
]:
r
"""
Loads value head parameters from Hugging Face Hub or local disk.
...
...
@@ -135,39 +167,3 @@ def register_autoclass(config: "PretrainedConfig", model: "PreTrainedModel", tok
model
.
__class__
.
register_for_auto_class
()
if
"
AutoTokenizer
"
in
tokenizer
.
init_kwargs
.
get
(
"
auto_map
"
,
{}):
tokenizer
.
__class__
.
register_for_auto_class
()
def
gradient_checkpointing_enable
(
self
,
gradient_checkpointing_kwargs
=
None
):
"""
Modification of the original method to enable gradient checkpointing for block-wise optimizer.
Activates gradient checkpointing for the current model.
We pass the `__call__` method of the modules instead of `forward` because `__call__` attaches all the hooks of
the module. https://discuss.pytorch.org/t/any-different-between-model-input-and-model-forward-input/3690/2
Args:
gradient_checkpointing_kwargs (dict, *optional*):
Additional keyword arguments passed along to the `torch.utils.checkpoint.checkpoint` function.
"""
from
torch.utils.checkpoint
import
checkpoint
import
functools
if
not
self
.
supports_gradient_checkpointing
:
raise
ValueError
(
f
"
{
self
.
__class__
.
__name__
}
does not support gradient checkpointing.
"
)
if
gradient_checkpointing_kwargs
is
None
:
gradient_checkpointing_kwargs
=
{
"
use_reentrant
"
:
True
}
checkpoint
=
functools
.
partial
(
checkpoint
,
**
gradient_checkpointing_kwargs
)
def
gradient_checkpointing_func
(
func
,
*
args
,
**
kwargs
):
module
=
func
.
__self__
if
any
(
p
.
requires_grad
for
p
in
module
.
parameters
()):
for
arg
in
args
:
if
torch
.
is_tensor
(
arg
)
and
torch
.
is_floating_point
(
arg
):
arg
.
requires_grad_
(
True
)
return
checkpoint
(
func
,
*
args
,
**
kwargs
)
self
.
_set_gradient_checkpointing
(
enable
=
True
,
gradient_checkpointing_func
=
gradient_checkpointing_func
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment