2022-03-24 06:29:41 +00:00
|
|
|
from colossalai.utils.memory_tracer.model_data_memtracer import GLOBAL_MODEL_DATA_TRACER
|
2022-03-25 06:02:55 +00:00
|
|
|
from colossalai.utils.memory_utils.utils import colo_model_data_tensor_move, colo_model_data_tensor_move_inline
|
2022-03-24 06:29:41 +00:00
|
|
|
from colossalai.utils import free_port
|
2022-03-25 09:25:12 +00:00
|
|
|
from colossalai.testing import rerun_on_exception
|
2022-03-24 06:29:41 +00:00
|
|
|
from colossalai.zero.sharded_param import ShardedTensor
|
|
|
|
import colossalai
|
|
|
|
|
|
|
|
import torch
|
|
|
|
|
|
|
|
import torch.multiprocessing as mp
|
|
|
|
|
|
|
|
|
|
|
|
def run_tensor_move(rank):
|
|
|
|
colossalai.launch(config={}, rank=0, world_size=1, host='localhost', port=free_port(), backend='nccl')
|
|
|
|
|
|
|
|
assert (GLOBAL_MODEL_DATA_TRACER.cuda_usage == 0)
|
2022-03-25 03:23:35 +00:00
|
|
|
GLOBAL_MODEL_DATA_TRACER.start()
|
2022-03-24 06:29:41 +00:00
|
|
|
|
|
|
|
src_t = torch.ones(2, 3).cuda()
|
|
|
|
GLOBAL_MODEL_DATA_TRACER.add_tensor(src_t)
|
|
|
|
assert (GLOBAL_MODEL_DATA_TRACER.cuda_usage == 24)
|
|
|
|
tgt_t = torch.zeros(2, 3)
|
|
|
|
|
|
|
|
colo_model_data_tensor_move(src_t, tgt_t)
|
|
|
|
assert (GLOBAL_MODEL_DATA_TRACER.cuda_usage == 0)
|
|
|
|
assert (torch.sum(tgt_t) == 6.0), f"{torch.sum(tgt_t.payload)} vs. 6.0"
|
|
|
|
|
|
|
|
src_t = torch.ones(2, 3)
|
|
|
|
tgt_t = torch.zeros(2, 3).cuda().half()
|
|
|
|
colo_model_data_tensor_move(src_t, tgt_t)
|
|
|
|
assert (GLOBAL_MODEL_DATA_TRACER.cuda_usage == 12), f"cuda usage {GLOBAL_MODEL_DATA_TRACER.cuda_usage}"
|
|
|
|
# the src_t has been removed
|
|
|
|
assert (src_t.numel() == 0)
|
|
|
|
assert (torch.sum(tgt_t) == 6.0), f"{torch.sum(tgt_t.payload)} vs. 6.0"
|
|
|
|
|
|
|
|
src_t = ShardedTensor(torch.ones(2, 3))
|
|
|
|
tgt_t = ShardedTensor(torch.zeros(2, 3).cuda().half())
|
|
|
|
colo_model_data_tensor_move(src_t, tgt_t)
|
|
|
|
assert (GLOBAL_MODEL_DATA_TRACER.cuda_usage == 24), f"cuda usage {GLOBAL_MODEL_DATA_TRACER.cuda_usage}"
|
|
|
|
assert (torch.sum(tgt_t.payload) == 6.0), f"{torch.sum(tgt_t.payload)} vs. 6.0"
|
2022-03-25 06:02:55 +00:00
|
|
|
|
|
|
|
assert (tgt_t.device.type == 'cuda')
|
|
|
|
colo_model_data_tensor_move_inline(tgt_t, torch.device('cpu'))
|
|
|
|
assert (tgt_t.device.type == 'cpu')
|
|
|
|
assert (GLOBAL_MODEL_DATA_TRACER.cuda_usage == 12), f"cuda usage {GLOBAL_MODEL_DATA_TRACER.cuda_usage}"
|
|
|
|
|
2022-03-25 03:23:35 +00:00
|
|
|
GLOBAL_MODEL_DATA_TRACER.close()
|
2022-03-24 06:29:41 +00:00
|
|
|
|
|
|
|
|
2022-03-25 09:25:12 +00:00
|
|
|
@rerun_on_exception(exception_type=mp.ProcessRaisedException, pattern=".*Address already in use.*")
|
2022-03-24 06:29:41 +00:00
|
|
|
def test_tensor_move():
|
|
|
|
mp.spawn(run_tensor_move, nprocs=1)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_tensor_move()
|