You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ColossalAI/tests/test_autochunk/benchmark_autochunk.py

123 lines
3.4 KiB

2 years ago
import time
2 years ago
import torch
import torch.fx
2 years ago
from colossalai.autochunk.autochunk_codegen import AutoChunkCodeGen
2 years ago
from colossalai.fx import ColoTracer
from colossalai.fx.graph_module import ColoGraphModule
2 years ago
from colossalai.fx.passes.meta_info_prop import MetaInfoProp
2 years ago
from colossalai.fx.profiler import MetaTensor
2 years ago
from tests.test_autochunk.evoformer.evoformer import evoformer_base
from tests.test_autochunk.openfold.evoformer import EvoformerBlock
2 years ago
2 years ago
def _benchmark_evoformer(model: torch.nn.Module, node, pair, title, chunk_size=None):
2 years ago
torch.cuda.reset_peak_memory_stats()
now_mem = torch.cuda.memory_allocated() / 1024**2
loop = 3
2 years ago
with torch.no_grad():
for _ in range(loop // 2 + 1):
2 years ago
if chunk_size:
model(node, pair, chunk_size)
else:
model(node, pair)
2 years ago
torch.cuda.synchronize()
time1 = time.time()
for _ in range(loop):
2 years ago
if chunk_size:
model(node, pair, chunk_size)
else:
model(node, pair)
2 years ago
torch.cuda.synchronize()
time2 = time.time()
2 years ago
new_max_mem = torch.cuda.max_memory_allocated() / 1024**2
print(
"%s: time %.4fs, mem %dMB"
% (title, (time2 - time1) / loop, new_max_mem - now_mem)
)
2 years ago
2 years ago
def _build_autochunk(model, max_memory, node, pair):
2 years ago
# trace the module and replace codegen
graph = ColoTracer().trace(
model,
meta_args={
"node": node.to(torch.device("meta")),
"pair": pair.to(torch.device("meta")),
},
)
2 years ago
gm_prop = torch.fx.symbolic_trace(model) # must use symbolic_trace
interp = MetaInfoProp(gm_prop)
interp.propagate(
MetaTensor(node, fake_device="cuda:0"), MetaTensor(pair, fake_device="cuda:0")
)
2 years ago
# now run it twice to get meta info in graph module, not necessary
gm = torch.fx.GraphModule(model, graph)
interp = MetaInfoProp(gm)
interp.propagate(
MetaTensor(node, fake_device="cuda:0"), MetaTensor(pair, fake_device="cuda:0")
)
2 years ago
# set code_gen
codegen = AutoChunkCodeGen(gm_prop, max_memory, print_mem=False)
2 years ago
graph.set_codegen(codegen)
gm = ColoGraphModule(model, graph)
gm.recompile()
2 years ago
# print
# code = graph.python_code("self").src
# print(code)
2 years ago
return gm
2 years ago
2 years ago
def _build_openfold():
model = EvoformerBlock(
c_m=256,
c_z=128,
c_hidden_msa_att=32,
c_hidden_opm=32,
c_hidden_mul=128,
c_hidden_pair_att=32,
no_heads_msa=8,
no_heads_pair=4,
transition_n=4,
msa_dropout=0.15,
pair_dropout=0.15,
inf=1e4,
eps=1e-4,
is_multimer=False,
).cuda()
return model
2 years ago
def benchmark_evoformer():
# init data and model
msa_len = 256
2 years ago
pair_len = 512
2 years ago
node = torch.randn(1, msa_len, pair_len, 256).cuda()
pair = torch.randn(1, pair_len, pair_len, 128).cuda()
model = evoformer_base().cuda()
# build autochunk model
2 years ago
# max_memory = 1000 # MB, fit memory mode
max_memory = None # min memory mode
2 years ago
autochunk = _build_autochunk(evoformer_base().cuda(), max_memory, node, pair)
# build openfold
chunk_size = 64
2 years ago
openfold = _build_openfold()
2 years ago
# benchmark
2 years ago
_benchmark_evoformer(model, node, pair, "base")
_benchmark_evoformer(openfold, node, pair, "openfold", chunk_size=chunk_size)
2 years ago
_benchmark_evoformer(autochunk, node, pair, "autochunk")
2 years ago
if __name__ == "__main__":
benchmark_evoformer()