2022-10-18 08:31:22 +00:00
|
|
|
from time import time
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import torch
|
|
|
|
import torch.distributed as dist
|
|
|
|
import torch.nn as nn
|
|
|
|
|
|
|
|
from colossalai.gemini.chunk import ChunkManager
|
2023-01-11 04:22:45 +00:00
|
|
|
from colossalai.gemini.chunk.search_utils import search_chunk_configuration
|
|
|
|
from colossalai.utils import is_ddp_ignored
|
2022-10-18 08:31:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def init_chunk_manager(model: nn.Module,
|
|
|
|
init_device: Optional[torch.device] = None,
|
|
|
|
hidden_dim: Optional[int] = None,
|
|
|
|
search_range_mb: Optional[float] = None,
|
|
|
|
min_chunk_size_mb: Optional[float] = None,
|
|
|
|
filter_exlarge_params: Optional[bool] = None) -> ChunkManager:
|
|
|
|
|
|
|
|
kwargs_dict = dict()
|
|
|
|
|
|
|
|
if hidden_dim:
|
|
|
|
search_interval_byte = hidden_dim
|
|
|
|
else:
|
|
|
|
search_interval_byte = 1024 # 1kb
|
|
|
|
kwargs_dict["search_interval_byte"] = search_interval_byte
|
|
|
|
|
|
|
|
if search_range_mb:
|
|
|
|
kwargs_dict["search_range_mb"] = search_range_mb
|
|
|
|
|
|
|
|
if min_chunk_size_mb:
|
|
|
|
kwargs_dict["min_chunk_size_mb"] = min_chunk_size_mb
|
|
|
|
|
|
|
|
if filter_exlarge_params:
|
|
|
|
kwargs_dict["filter_exlarge_params"] = filter_exlarge_params
|
|
|
|
|
2023-01-11 04:22:45 +00:00
|
|
|
params_sizes = [p.numel() for p in model.parameters() if not is_ddp_ignored(p)]
|
2022-10-18 08:31:22 +00:00
|
|
|
total_size = sum(params_sizes) / 1024**2
|
|
|
|
|
|
|
|
dist.barrier()
|
2022-12-12 10:06:16 +00:00
|
|
|
begin = time()
|
2022-10-18 08:31:22 +00:00
|
|
|
|
|
|
|
config_dict, wasted_size = search_chunk_configuration(model, **kwargs_dict)
|
|
|
|
|
|
|
|
dist.barrier()
|
|
|
|
end = time()
|
2022-12-12 10:06:16 +00:00
|
|
|
span_s = end - begin
|
2022-10-18 08:31:22 +00:00
|
|
|
wasted_size /= 1024**2
|
|
|
|
|
|
|
|
if dist.get_rank() == 0:
|
|
|
|
print("searching chunk configuration is completed in {:.2f} s.\n".format(span_s),
|
|
|
|
"used number: {:.2f} MB, wasted number: {:.2f} MB\n".format(total_size, wasted_size),
|
|
|
|
"total wasted percentage is {:.2f}%".format(100 * wasted_size / (total_size + wasted_size)),
|
|
|
|
sep='',
|
|
|
|
flush=True)
|
|
|
|
dist.barrier()
|
|
|
|
|
|
|
|
chunk_manager = ChunkManager(config_dict, init_device)
|
|
|
|
return chunk_manager
|