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
OpenDAS
Uni-Core
Commits
31fe887e
Unverified
Commit
31fe887e
authored
Jul 30, 2022
by
Guolin Ke
Committed by
GitHub
Jul 30, 2022
Browse files
support gradient checkpointing
parent
89ac1b7b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
0 deletions
+29
-0
unicore/utils.py
unicore/utils.py
+29
-0
No files found.
unicore/utils.py
View file @
31fe887e
...
@@ -290,6 +290,35 @@ def eval_bool(x, default=False):
...
@@ -290,6 +290,35 @@ def eval_bool(x, default=False):
return
default
return
default
def
checkpoint_sequential
(
functions
,
input
,
):
def
wrap_tuple
(
a
):
return
(
a
,)
if
type
(
a
)
is
not
tuple
else
a
def
exec
(
func
,
a
):
return
wrap_tuple
(
func
(
*
a
))
def
get_wrap_exec
(
func
):
def
wrap_exec
(
*
a
):
return
exec
(
func
,
a
)
return
wrap_exec
input
=
wrap_tuple
(
input
)
is_grad_enabled
=
torch
.
is_grad_enabled
()
if
is_grad_enabled
:
for
func
in
functions
:
input
=
torch
.
utils
.
checkpoint
.
checkpoint
(
get_wrap_exec
(
func
),
*
input
)
else
:
for
func
in
functions
:
input
=
exec
(
func
,
input
)
return
input
def
permute_final_dims
(
tensor
:
torch
.
Tensor
,
inds
:
List
[
int
]):
def
permute_final_dims
(
tensor
:
torch
.
Tensor
,
inds
:
List
[
int
]):
zero_index
=
-
1
*
len
(
inds
)
zero_index
=
-
1
*
len
(
inds
)
first_inds
=
list
(
range
(
len
(
tensor
.
shape
[:
zero_index
])))
first_inds
=
list
(
range
(
len
(
tensor
.
shape
[:
zero_index
])))
...
...
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