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
ColossalAI
Commits
87cddf7e
Commit
87cddf7e
authored
Oct 27, 2022
by
oahzxl
Browse files
rename and remove useless func
parent
f5c5d4c4
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
440 deletions
+27
-440
chunk_codegen.py
chunk_codegen.py
+24
-374
chunk_codegen_run.py
chunk_codegen_run.py
+3
-66
No files found.
chunk_codegen.py
View file @
87cddf7e
This diff is collapsed.
Click to expand it.
chunk_codegen_run.py
View file @
87cddf7e
...
...
@@ -11,7 +11,7 @@ from colossalai.core import global_context as gpc
from
colossalai.fx.graph_module
import
ColoGraphModule
try
:
from
chunk_codegen
import
ActivationCheckpoint
CodeGen
from
chunk_codegen
import
Chunk
CodeGen
with_codegen
=
True
except
:
# fall back to older pytorch version
...
...
@@ -75,7 +75,7 @@ def _run_offload_codegen(rank):
# trace the module and replace codegen
tracer
=
ColoTracer
(
trace_act_ckpt
=
True
)
graph
=
tracer
.
trace
(
model
)
codegen
=
ActivationCheckpoint
CodeGen
()
codegen
=
Chunk
CodeGen
()
graph
.
set_codegen
(
codegen
)
# annotate the activation offload part
...
...
@@ -99,15 +99,7 @@ def _run_offload_codegen(rank):
# assert we have all the components
code
=
graph
.
python_code
(
"self"
).
src
assert
"def pack_hook_input(self, x):"
in
code
and
\
"def unpack_hook(self, packed):"
in
code
and
\
"def pack_hook_no_input(self, x):"
in
code
and
\
"setattr(x, 'offload', True)"
in
code
and
\
"setattr(linear3, 'offload', False)"
in
code
and
\
"with torch.autograd.graph.saved_tensors_hooks(self.pack_hook_input, self.unpack_hook):"
in
code
and
\
"with torch.autograd.graph.save_on_cpu(pin_memory=True):"
in
code
and
\
"with torch.autograd.graph.saved_tensors_hooks(self.pack_hook_no_input, self.unpack_hook):"
in
code
and
\
"colossalai.utils.activation_checkpoint.checkpoint(self.checkpoint_0, True, linear4, use_reentrant=False)"
in
code
print
(
code
)
_test_fwd_and_bwd
(
model
,
gm
,
data
)
gpc
.
destroy
()
...
...
@@ -118,60 +110,5 @@ def test_act_ckpt_codegen():
mp
.
spawn
(
_run_offload_codegen
,
nprocs
=
1
)
def
_run_offload_codegen_torch11
(
rank
):
# launch colossalai to make sure we could execute colossalai.utils.checkpoint currectly
colossalai
.
launch
(
config
=
{},
rank
=
rank
,
world_size
=
1
,
host
=
'localhost'
,
port
=
free_port
(),
backend
=
'nccl'
)
# build model and input
model
=
MyNet
().
cuda
()
data
=
torch
.
rand
(
4
,
4
).
cuda
()
# trace the module and replace codegen
tracer
=
ColoTracer
(
trace_act_ckpt
=
True
)
graph
=
tracer
.
trace
(
model
)
# replace a bound method of an object
graph
.
_python_code
=
python_code_with_activation_checkpoint
.
__get__
(
graph
)
# annotate the activation offload part
# also annotate the activation_checkpoint so we could test both types
# of input offload
for
node
in
graph
.
nodes
:
if
node
.
name
==
"linear0"
:
setattr
(
node
,
"activation_offload"
,
[
0
,
True
,
False
])
if
node
.
name
==
"linear1"
:
setattr
(
node
,
"activation_offload"
,
[
0
,
True
,
False
])
if
node
.
name
==
"linear2"
:
setattr
(
node
,
"activation_offload"
,
[
1
,
True
,
True
])
if
node
.
name
==
"linear4"
:
setattr
(
node
,
"activation_offload"
,
[
2
,
False
,
True
])
if
node
.
name
==
"linear5"
:
setattr
(
node
,
"activation_checkpoint"
,
[
0
])
setattr
(
node
,
"activation_offload"
,
True
)
gm
=
ColoGraphModule
(
copy
.
deepcopy
(
model
),
graph
)
gm
.
recompile
()
# assert we have all the components
code
=
graph
.
python_code
(
"self"
).
src
assert
"def pack_hook_input(self, x):"
in
code
and
\
"def unpack_hook(self, packed):"
in
code
and
\
"def pack_hook_no_input(self, x):"
in
code
and
\
"setattr(x, 'offload', True)"
in
code
and
\
"setattr(linear3, 'offload', False)"
in
code
and
\
"with torch.autograd.graph.saved_tensors_hooks(self.pack_hook_input, self.unpack_hook):"
in
code
and
\
"with torch.autograd.graph.save_on_cpu(pin_memory=True):"
in
code
and
\
"with torch.autograd.graph.saved_tensors_hooks(self.pack_hook_no_input, self.unpack_hook):"
in
code
and
\
"colossalai.utils.activation_checkpoint.checkpoint(self.checkpoint_0, True, linear4, use_reentrant=False)"
in
code
_test_fwd_and_bwd
(
model
,
gm
,
data
)
gpc
.
destroy
()
@
pytest
.
mark
.
skip
(
reason
=
"currently torch11 ColoGraphModule is not implemented"
)
def
test_act_ckpt_python_code_torch11
():
mp
.
spawn
(
_run_offload_codegen_torch11
,
nprocs
=
1
)
if
__name__
==
"__main__"
:
_run_offload_codegen
(
0
)
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