mirror of https://github.com/hpcaitech/ColossalAI
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.
48 lines
1.3 KiB
48 lines
1.3 KiB
3 years ago
|
import torch
|
||
|
from colossalai.registry import OPHOOKS
|
||
|
|
||
3 years ago
|
from . import BaseOpHook
|
||
|
|
||
|
|
||
3 years ago
|
@OPHOOKS.register_module
|
||
|
class ShardParamHook(BaseOpHook):
|
||
|
"""
|
||
|
A hook to process sharded param before and afther FWD and BWD operator executing.
|
||
|
"""
|
||
3 years ago
|
|
||
3 years ago
|
def __init__(self):
|
||
|
super().__init__()
|
||
|
|
||
|
def niter(self):
|
||
|
return self._niter
|
||
|
|
||
|
def pre_fwd_exec(self, module: torch.nn.Module, *args):
|
||
|
for param in module.parameters():
|
||
|
assert hasattr(param, 'ca_attr')
|
||
|
param.ca_attr.gather()
|
||
3 years ago
|
param.data = param.ca_attr.payload()
|
||
3 years ago
|
|
||
|
def post_fwd_exec(self, module: torch.nn.Module, *args):
|
||
|
for param in module.parameters():
|
||
|
assert hasattr(param, 'ca_attr')
|
||
|
param.ca_attr.shard()
|
||
3 years ago
|
param.data = param.ca_attr.payload()
|
||
3 years ago
|
|
||
|
def pre_bwd_exec(self, module: torch.nn.Module, input, output):
|
||
|
for param in module.parameters():
|
||
|
assert hasattr(param, 'ca_attr')
|
||
|
param.ca_attr.gather()
|
||
3 years ago
|
param.data = param.ca_attr.payload()
|
||
3 years ago
|
|
||
|
def post_bwd_exec(self, module: torch.nn.Module, input):
|
||
|
for param in module.parameters():
|
||
|
assert hasattr(param, 'ca_attr')
|
||
|
param.ca_attr.shard()
|
||
3 years ago
|
param.data = param.ca_attr.payload()
|
||
3 years ago
|
|
||
|
def pre_iter(self):
|
||
|
pass
|
||
|
|
||
|
def post_iter(self):
|
||
|
pass
|