mirror of https://github.com/hpcaitech/ColossalAI
aibig-modeldata-parallelismdeep-learningdistributed-computingfoundation-modelsheterogeneous-traininghpcinferencelarge-scalemodel-parallelismpipeline-parallelism
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.
33 lines
713 B
33 lines
713 B
1 year ago
|
from contextlib import contextmanager
|
||
|
|
||
|
import torch
|
||
|
import torch.nn as nn
|
||
|
|
||
|
|
||
|
@contextmanager
|
||
|
def low_precision_init(target_dtype: torch.dtype = torch.float16):
|
||
|
dtype = torch.get_default_dtype()
|
||
|
try:
|
||
|
torch.set_default_dtype(target_dtype)
|
||
|
yield
|
||
|
finally:
|
||
|
torch.set_default_dtype(dtype)
|
||
|
|
||
|
|
||
|
def get_model_numel(model: nn.Module) -> int:
|
||
|
return sum(p.numel() for p in model.parameters())
|
||
|
|
||
|
|
||
|
def format_numel_str(numel: int) -> str:
|
||
|
B = 1024**3
|
||
|
M = 1024**2
|
||
|
K = 1024
|
||
|
if numel >= B:
|
||
1 year ago
|
return f"{numel / B:.2f} B"
|
||
1 year ago
|
elif numel >= M:
|
||
1 year ago
|
return f"{numel / M:.2f} M"
|
||
1 year ago
|
elif numel >= K:
|
||
1 year ago
|
return f"{numel / K:.2f} K"
|
||
1 year ago
|
else:
|
||
1 year ago
|
return f"{numel}"
|