2022-12-05 04:45:11 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
|
2022-11-26 05:30:24 +00:00
|
|
|
import numpy as np
|
2023-08-24 01:29:25 +00:00
|
|
|
import pytest
|
2022-11-26 05:30:24 +00:00
|
|
|
import torch
|
|
|
|
|
2023-04-06 06:51:35 +00:00
|
|
|
from colossalai.testing import clear_cache_before_run
|
2023-04-04 05:48:16 +00:00
|
|
|
from colossalai.zero.gemini.memory_tracer.runtime_mem_tracer import RuntimeMemTracer
|
2022-12-05 04:45:11 +00:00
|
|
|
from tests.components_to_test import run_fwd_bwd
|
2022-11-26 05:30:24 +00:00
|
|
|
from tests.components_to_test.registry import non_distributed_component_funcs
|
|
|
|
|
2022-12-05 04:45:11 +00:00
|
|
|
|
2023-08-24 01:29:25 +00:00
|
|
|
@pytest.mark.skip("this is not used")
|
2023-04-06 06:51:35 +00:00
|
|
|
@clear_cache_before_run()
|
2022-12-05 05:32:16 +00:00
|
|
|
def test_runtime_mem_tracer():
|
2022-12-05 06:09:34 +00:00
|
|
|
test_models = ['gpt2', 'bert', 'simple_net', 'repeated_computed_layers', 'nested_model', 'albert']
|
2022-12-05 05:32:16 +00:00
|
|
|
|
2022-11-26 05:30:24 +00:00
|
|
|
for model_name in test_models:
|
|
|
|
get_components_func = non_distributed_component_funcs.get_callable(model_name)
|
|
|
|
model_builder, train_dataloader, _, _, criterion = get_components_func()
|
|
|
|
|
2023-08-24 01:29:25 +00:00
|
|
|
model = model_builder(checkpoint=False).cuda()
|
2022-11-26 05:30:24 +00:00
|
|
|
|
2022-12-05 04:45:11 +00:00
|
|
|
model_bk = deepcopy(model)
|
|
|
|
runtime_mem_tracer = RuntimeMemTracer(model)
|
2022-11-26 05:30:24 +00:00
|
|
|
|
|
|
|
for i, (data, label) in enumerate(train_dataloader):
|
|
|
|
if i > 1:
|
|
|
|
break
|
|
|
|
data = data.cuda()
|
|
|
|
label = label.cuda()
|
|
|
|
|
2022-12-05 08:22:49 +00:00
|
|
|
run_fwd_bwd(runtime_mem_tracer, data, label, criterion, optimizer=runtime_mem_tracer)
|
2022-12-05 04:45:11 +00:00
|
|
|
|
|
|
|
for p1, p2 in zip(model_bk.parameters(), model.parameters()):
|
|
|
|
torch.allclose(p1.to(torch.half), p2)
|
2022-11-26 05:30:24 +00:00
|
|
|
|
2022-12-06 11:48:20 +00:00
|
|
|
non_model_data_list = runtime_mem_tracer._memstats.non_model_data_list('cuda')
|
|
|
|
cuda_non_model_data_list = np.array(non_model_data_list) / 1024**2
|
2022-11-26 05:30:24 +00:00
|
|
|
print("cuda_non_model_data_list", len(cuda_non_model_data_list))
|
2022-12-06 11:48:20 +00:00
|
|
|
print(non_model_data_list)
|
2022-11-26 05:30:24 +00:00
|
|
|
|
2022-12-09 08:13:03 +00:00
|
|
|
cnt1 = 0
|
|
|
|
for p in runtime_mem_tracer.parameters_in_runtime_order():
|
|
|
|
cnt1 += 1
|
|
|
|
cnt2 = 0
|
|
|
|
for p in model.parameters():
|
|
|
|
cnt2 += 1
|
|
|
|
assert cnt2 == cnt1, f'visited param number {cnt1} vs real param number {cnt2}'
|
2022-11-26 05:30:24 +00:00
|
|
|
del model
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-12-05 05:32:16 +00:00
|
|
|
test_runtime_mem_tracer()
|