2022-07-25 01:39:10 +00:00
|
|
|
import pytest
|
2022-11-01 02:43:15 +00:00
|
|
|
import torch
|
2022-08-12 06:52:31 +00:00
|
|
|
import torch.multiprocessing as mp
|
2022-11-01 02:43:15 +00:00
|
|
|
import torch.nn.functional as F
|
2022-07-25 01:39:10 +00:00
|
|
|
from torch.fx import GraphModule
|
2022-11-01 02:43:15 +00:00
|
|
|
from torch.utils.checkpoint import checkpoint
|
|
|
|
|
2022-08-12 04:23:30 +00:00
|
|
|
import colossalai
|
|
|
|
from colossalai.core import global_context as gpc
|
2022-11-01 02:43:15 +00:00
|
|
|
from colossalai.fx import ColoTracer
|
2022-08-22 08:59:54 +00:00
|
|
|
from colossalai.fx.graph_module import ColoGraphModule
|
2022-11-01 02:43:15 +00:00
|
|
|
from colossalai.utils import free_port
|
2022-07-25 01:39:10 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from colossalai.fx.codegen import ActivationCheckpointCodeGen
|
2022-07-25 15:35:31 +00:00
|
|
|
with_codegen = True
|
2022-07-25 01:39:10 +00:00
|
|
|
except:
|
2022-07-25 15:35:31 +00:00
|
|
|
# fall back to older pytorch version
|
|
|
|
from colossalai.fx.codegen import python_code_with_activation_checkpoint
|
|
|
|
with_codegen = False
|
2022-07-25 01:39:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MLP(torch.nn.Module):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.linear1 = torch.nn.Linear(4, 4)
|
|
|
|
self.linear2 = torch.nn.Linear(4, 4)
|
|
|
|
|
|
|
|
def forward(self, x):
|
2022-08-17 02:34:50 +00:00
|
|
|
return self.linear1(x), self.linear2(x)
|
|
|
|
|
|
|
|
|
|
|
|
class relu(torch.nn.Module):
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.relu = torch.nn.ReLU(inplace=True)
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
return self.relu(x)
|
2022-07-25 01:39:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MyModule(torch.nn.Module):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.mlp1 = MLP()
|
2022-08-17 02:34:50 +00:00
|
|
|
self.relu = relu()
|
2022-08-22 08:59:54 +00:00
|
|
|
self.linear2 = torch.nn.Linear(4, 4)
|
2022-07-25 01:39:10 +00:00
|
|
|
|
2022-08-27 11:39:21 +00:00
|
|
|
def ckpt2(self, x):
|
|
|
|
return F.relu(x, inplace=True)
|
|
|
|
|
|
|
|
def ckpt3(self, x, y):
|
|
|
|
return self.linear2(x) + self.linear2(y)
|
|
|
|
|
|
|
|
def forward(self, x, y):
|
2022-07-25 01:39:10 +00:00
|
|
|
y1, y2 = checkpoint(self.mlp1, x)
|
2022-08-17 02:34:50 +00:00
|
|
|
y3 = checkpoint(self.relu, x)
|
|
|
|
|
2022-08-27 11:39:21 +00:00
|
|
|
y4 = checkpoint(self.ckpt2, y)
|
|
|
|
y5 = checkpoint(self.ckpt3, y, y4)
|
|
|
|
y6 = self.linear2(y4)
|
|
|
|
return y1 + y2 + y3 + y4 + y5 + y6
|
2022-07-25 01:39:10 +00:00
|
|
|
|
|
|
|
|
2022-08-12 06:52:31 +00:00
|
|
|
def _run_act_ckpt_codegen(rank):
|
2022-08-12 04:23:30 +00:00
|
|
|
# launch colossalai to make sure we could execute colossalai.utils.checkpoint currectly
|
2022-08-12 06:52:31 +00:00
|
|
|
colossalai.launch(config={}, rank=rank, world_size=1, host='localhost', port=free_port(), backend='nccl')
|
2022-08-12 04:23:30 +00:00
|
|
|
|
2022-07-25 01:39:10 +00:00
|
|
|
# build model and run forward
|
|
|
|
model = MyModule()
|
2022-08-27 11:39:21 +00:00
|
|
|
data1 = torch.rand(4, 4)
|
|
|
|
data2 = torch.rand(4, 4)
|
2022-08-12 04:23:30 +00:00
|
|
|
|
|
|
|
# copy model to cuda
|
|
|
|
model = model.to(device="cuda")
|
2022-08-27 11:39:21 +00:00
|
|
|
data1 = data1.to(device="cuda")
|
|
|
|
data2 = data2.to(device="cuda")
|
2022-08-12 04:23:30 +00:00
|
|
|
|
2022-08-27 11:39:21 +00:00
|
|
|
non_fx_out = model(data1, data2)
|
2022-07-25 01:39:10 +00:00
|
|
|
|
|
|
|
# trace the module and replace codegen
|
|
|
|
tracer = ColoTracer(trace_act_ckpt=True)
|
|
|
|
graph = tracer.trace(model)
|
|
|
|
codegen = ActivationCheckpointCodeGen()
|
|
|
|
graph.set_codegen(codegen)
|
|
|
|
|
|
|
|
# check ops are annotated with ckpt
|
2022-08-12 04:23:30 +00:00
|
|
|
# also annotate the selected node for offloading
|
2022-08-17 02:34:50 +00:00
|
|
|
ckpt_nodes = ['mlp1_linear1', 'mlp1_linear2', 'relu_relu', 'relu']
|
|
|
|
offload_starts = ['mlp1_linear1']
|
2022-07-25 01:39:10 +00:00
|
|
|
for node in graph.nodes:
|
|
|
|
if node.name in ckpt_nodes:
|
2022-11-01 02:43:15 +00:00
|
|
|
assert 'activation_checkpoint' in node.meta
|
2022-07-25 01:39:10 +00:00
|
|
|
|
2022-08-12 04:23:30 +00:00
|
|
|
# annotate the selected node for offload
|
|
|
|
if node.name in offload_starts:
|
2022-11-01 02:43:15 +00:00
|
|
|
node.meta['activation_offload'] = True
|
2022-08-12 04:23:30 +00:00
|
|
|
|
2022-08-22 08:59:54 +00:00
|
|
|
gm = ColoGraphModule(model, graph)
|
2022-08-17 02:34:50 +00:00
|
|
|
gm.recompile()
|
|
|
|
|
2022-08-12 04:23:30 +00:00
|
|
|
# assert checkpoint function will be generated and
|
|
|
|
# the offload option is correct
|
2022-07-25 01:39:10 +00:00
|
|
|
code = graph.python_code('self').src
|
2022-08-27 11:39:21 +00:00
|
|
|
assert 'colossalai.utils.activation_checkpoint.checkpoint(self.checkpoint_0, True, x, use_reentrant=False)' in code and \
|
2022-08-22 08:59:54 +00:00
|
|
|
'colossalai.utils.activation_checkpoint.checkpoint(self.checkpoint_1, False, x, use_reentrant=False)' in code and \
|
2022-08-27 11:39:21 +00:00
|
|
|
'colossalai.utils.activation_checkpoint.checkpoint(self.checkpoint_2, False, y, use_reentrant=False)' in code and \
|
|
|
|
'colossalai.utils.activation_checkpoint.checkpoint(self.checkpoint_3, False, y, relu, use_reentrant=True)' in code
|
2022-07-25 01:39:10 +00:00
|
|
|
|
|
|
|
# recompile and verify the outputs are consistent
|
2022-08-27 11:39:21 +00:00
|
|
|
fx_out = gm(data1, data2)
|
2022-07-25 01:39:10 +00:00
|
|
|
assert torch.equal(non_fx_out, fx_out)
|
|
|
|
|
2022-08-12 04:23:30 +00:00
|
|
|
gpc.destroy()
|
|
|
|
|
2022-07-25 01:39:10 +00:00
|
|
|
|
2022-08-12 06:52:31 +00:00
|
|
|
@pytest.mark.skipif(not with_codegen, reason='torch version is lower than 1.12.0')
|
|
|
|
def test_act_ckpt_codegen():
|
|
|
|
mp.spawn(_run_act_ckpt_codegen, nprocs=1)
|
|
|
|
|
|
|
|
|
|
|
|
def _run_act_ckpt_python_code_torch11(rank):
|
2022-08-12 04:23:30 +00:00
|
|
|
# launch colossalai to make sure we could execute colossalai.utils.checkpoint currectly
|
2022-08-12 06:52:31 +00:00
|
|
|
colossalai.launch(config={}, rank=rank, world_size=1, host='localhost', port=free_port(), backend='nccl')
|
2022-08-12 04:23:30 +00:00
|
|
|
|
2022-07-25 15:35:31 +00:00
|
|
|
# build model and run forward
|
|
|
|
model = MyModule()
|
2022-08-27 11:39:21 +00:00
|
|
|
data1 = torch.rand(4, 4)
|
|
|
|
data2 = torch.rand(4, 4)
|
2022-08-12 04:23:30 +00:00
|
|
|
|
|
|
|
# copy model to cuda
|
2022-08-27 11:39:21 +00:00
|
|
|
data1 = data1.to(device="cuda")
|
|
|
|
data2 = data2.to(device="cuda")
|
2022-08-12 04:23:30 +00:00
|
|
|
|
2022-08-27 11:39:21 +00:00
|
|
|
non_fx_out = model(data1, data2)
|
2022-07-25 15:35:31 +00:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
# check ops are annotated with ckpt
|
2022-08-17 02:34:50 +00:00
|
|
|
ckpt_nodes = ['mlp1_linear1', 'mlp1_linear2', 'relu_relu', 'relu']
|
|
|
|
offload_starts = ['mlp1_linear1']
|
2022-07-25 15:35:31 +00:00
|
|
|
for node in graph.nodes:
|
|
|
|
if node.name in ckpt_nodes:
|
2022-11-01 02:43:15 +00:00
|
|
|
assert 'activation_checkpoint' in node.meta
|
2022-07-25 15:35:31 +00:00
|
|
|
|
2022-08-12 04:23:30 +00:00
|
|
|
# annotate the selected node for offload
|
|
|
|
if node.name in offload_starts:
|
2022-11-01 02:43:15 +00:00
|
|
|
node.meta['activation_offload'] = True
|
2022-08-12 04:23:30 +00:00
|
|
|
|
2022-08-22 08:59:54 +00:00
|
|
|
gm = ColoGraphModule(model, graph)
|
2022-08-17 02:34:50 +00:00
|
|
|
gm.recompile()
|
2022-08-12 04:23:30 +00:00
|
|
|
# assert checkpoint function will be generated and
|
|
|
|
# the offload option is correct
|
2022-07-25 15:35:31 +00:00
|
|
|
code = graph.python_code('self').src
|
2022-08-27 11:39:21 +00:00
|
|
|
assert 'colossalai.utils.activation_checkpoint.checkpoint(self.checkpoint_0, True, x, use_reentrant=False)' in code and \
|
2022-08-22 08:59:54 +00:00
|
|
|
'colossalai.utils.activation_checkpoint.checkpoint(self.checkpoint_1, False, x, use_reentrant=False)' in code and \
|
2022-08-27 11:39:21 +00:00
|
|
|
'colossalai.utils.activation_checkpoint.checkpoint(self.checkpoint_2, False, y, use_reentrant=False)' in code and \
|
|
|
|
'colossalai.utils.activation_checkpoint.checkpoint(self.checkpoint_3, False, y, relu, use_reentrant=True)' in code
|
2022-07-25 15:35:31 +00:00
|
|
|
|
|
|
|
# recompile and verify the outputs are consistent
|
2022-08-27 11:39:21 +00:00
|
|
|
fx_out = gm(data1, data2)
|
2022-07-25 15:35:31 +00:00
|
|
|
assert torch.equal(non_fx_out, fx_out)
|
|
|
|
|
2022-08-12 04:23:30 +00:00
|
|
|
gpc.destroy()
|
|
|
|
|
2022-07-25 15:35:31 +00:00
|
|
|
|
2022-08-12 06:52:31 +00:00
|
|
|
@pytest.mark.skipif(with_codegen, reason='torch version is equal to or higher than 1.12.0')
|
2022-08-22 08:59:54 +00:00
|
|
|
@pytest.mark.skip(reason="currently torch11 ColoGraphModule is not done")
|
2022-08-12 06:52:31 +00:00
|
|
|
def test_act_ckpt_python_code_torch11():
|
|
|
|
mp.spawn(_run_act_ckpt_python_code_torch11, nprocs=1)
|
|
|
|
|
|
|
|
|
2022-07-25 01:39:10 +00:00
|
|
|
if __name__ == '__main__':
|
2022-08-22 08:59:54 +00:00
|
|
|
_run_act_ckpt_codegen(rank=0)
|