mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-10-03 08:06:24 +00:00
[example] add llama2 example (#4527)
* [example] transfer llama-1 example * [example] fit llama-2 * [example] refactor scripts folder * [example] fit new gemini plugin * [cli] fix multinode runner * [example] fit gemini optim checkpoint * [example] refactor scripts * [example] update requirements * [example] update requirements * [example] rename llama to llama2 * [example] update readme and pretrain script * [example] refactor scripts
This commit is contained in:
32
examples/language/llama2/model_utils.py
Normal file
32
examples/language/llama2/model_utils.py
Normal file
@@ -0,0 +1,32 @@
|
||||
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:
|
||||
return f'{numel / B:.2f} B'
|
||||
elif numel >= M:
|
||||
return f'{numel / M:.2f} M'
|
||||
elif numel >= K:
|
||||
return f'{numel / K:.2f} K'
|
||||
else:
|
||||
return f'{numel}'
|
Reference in New Issue
Block a user