chunk_codegen_run.py 3.17 KB
Newer Older
oahzxl's avatar
init  
oahzxl committed
1
2
3
4
5
6
7
8
9
10
11
import copy
import torch
import torch.nn.functional as F
import pytest
import torch.multiprocessing as mp
from torch.fx import GraphModule
from colossalai.fx import ColoTracer
import colossalai
from colossalai.utils import free_port
from colossalai.core import global_context as gpc
from colossalai.fx.graph_module import ColoGraphModule
oahzxl's avatar
oahzxl committed
12
13
from colossalai.fx.passes.meta_info_prop import MetaInfoProp, TensorMetadata
from colossalai.fx.profiler import MetaTensor
oahzxl's avatar
oahzxl committed
14
15
16
from evoformer.evoformer import evoformer_base
from chunk_codegen import ChunkCodeGen
with_codegen = True
oahzxl's avatar
init  
oahzxl committed
17
18
19
20


def _is_all_gradient_close(m: torch.nn.Module, gm: GraphModule) -> bool:
    for m_p, gm_p in zip(m.parameters(), gm.parameters()):
oahzxl's avatar
oahzxl committed
21
        if m_p.grad is not None and not torch.allclose(m_p.grad, gm_p.grad):
oahzxl's avatar
init  
oahzxl committed
22
23
24
25
            return False
    return True


oahzxl's avatar
oahzxl committed
26
27
28
29
30
31
def _is_all_param_close(m: torch.nn.Module, gm: GraphModule) -> bool:
    for m_p, gm_p in zip(m.parameters(), gm.parameters()):
        if m_p.grad is not None and not torch.allclose(m_p.data, gm_p.data):
            return False
    return True

oahzxl's avatar
init  
oahzxl committed
32

oahzxl's avatar
oahzxl committed
33
def _test_fwd_and_bwd(model: torch.nn.Module, gm: ColoGraphModule, node, pair):
oahzxl's avatar
init  
oahzxl committed
34
    # test forward
oahzxl's avatar
oahzxl committed
35
36
37
38
    non_fx_out = model(node.clone(), pair.clone())
    fx_out = gm(node.clone(), pair.clone())
    assert torch.equal(non_fx_out[0], fx_out[0]), "fx_out doesn't comply with original output"
    assert torch.equal(non_fx_out[1], fx_out[1]), "fx_out doesn't comply with original output"
oahzxl's avatar
init  
oahzxl committed
39
40

    # test barckward
oahzxl's avatar
oahzxl committed
41
42
43
44
45
46
    # loss0 = non_fx_out[0].sum() + non_fx_out[1].sum()
    # loss0.backward()
    # loss1 = fx_out[0].sum() + fx_out[1].sum()
    # loss1.backward()
    # assert _is_all_param_close(model, gm)
    # assert _is_all_gradient_close(model, gm), "gm doesn't have the same gradient as original one"
oahzxl's avatar
init  
oahzxl committed
47
48
49
50
51
52
53


def _run_offload_codegen(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
oahzxl's avatar
oahzxl committed
54
55
56
    model = evoformer_base().cuda()
    node = torch.randn(1, 16, 32, 256).cuda()
    pair = torch.randn(1, 32, 32, 128).cuda()
oahzxl's avatar
init  
oahzxl committed
57
58
59
60

    # trace the module and replace codegen
    tracer = ColoTracer(trace_act_ckpt=True)
    graph = tracer.trace(model)
oahzxl's avatar
oahzxl committed
61
62
63
64
    gm_prop = torch.fx.GraphModule(model, graph)
    interp = MetaInfoProp(gm_prop)
    interp.propagate(MetaTensor(node, fake_device='cuda:0'), MetaTensor(pair, fake_device='cuda:0'))
    
oahzxl's avatar
oahzxl committed
65
66
67
68
69
70
    # annotate the chunk part
    # 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])
oahzxl's avatar
init  
oahzxl committed
71

oahzxl's avatar
oahzxl committed
72
    codegen = ChunkCodeGen(gm_prop)
oahzxl's avatar
oahzxl committed
73
    graph.set_codegen(codegen)
oahzxl's avatar
oahzxl committed
74
    gm = ColoGraphModule(model, graph)
oahzxl's avatar
init  
oahzxl committed
75
76
77
78
    gm.recompile()

    # assert we have all the components
    code = graph.python_code("self").src
oahzxl's avatar
oahzxl committed
79
    print(code)
oahzxl's avatar
init  
oahzxl committed
80

oahzxl's avatar
oahzxl committed
81
    _test_fwd_and_bwd(model, gm, node, pair)
oahzxl's avatar
init  
oahzxl committed
82
83
84
85
86
87
88
89
90
91
    gpc.destroy()


@pytest.mark.skipif(not with_codegen, reason='torch version is lower than 1.12.0')
def test_act_ckpt_codegen():
    mp.spawn(_run_offload_codegen, nprocs=1)


if __name__ == "__main__":
    _run_offload_codegen(0)