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.
34 lines
1.1 KiB
34 lines
1.1 KiB
3 years ago
|
import torch
|
||
|
|
||
3 years ago
|
from colossalai.tensor.colo_tensor import ColoTensor
|
||
3 years ago
|
|
||
|
|
||
3 years ago
|
def _convert_tensor(tensor: torch.Tensor) -> ColoTensor:
|
||
|
return ColoTensor(tensor)
|
||
3 years ago
|
|
||
|
|
||
|
def convert_parameter(module: torch.nn.Module, param_name: str):
|
||
|
# Perform some validation first.
|
||
|
if not hasattr(module, param_name):
|
||
|
raise ValueError(f'module: {module} does not have parameter with name: {param_name}')
|
||
|
|
||
|
tensor = getattr(module, param_name)
|
||
|
if not isinstance(tensor, torch.Tensor):
|
||
|
raise ValueError(
|
||
|
f'Expected {type(module).__name__}.{param_name} to be a Tensor, but found {type(tensor).__name__}')
|
||
|
|
||
|
if not tensor.is_contiguous():
|
||
|
raise ValueError(f'param: {param_name} is not a contiguous Tensor')
|
||
|
|
||
|
st = _convert_tensor(tensor)
|
||
|
|
||
3 years ago
|
# Replace param with ColoTensor.
|
||
3 years ago
|
|
||
|
# Need to delete the attribute first since param_name might be
|
||
3 years ago
|
# torch.nn.Parameter and can't be replaced with ColoTensor which is
|
||
3 years ago
|
# not torch.nn.Parameter.
|
||
|
delattr(module, param_name)
|
||
|
|
||
|
# Now we can set the attribute appropriately.
|
||
|
setattr(module, param_name, st)
|