2022-03-02 10:28:29 +00:00
|
|
|
import torch
|
2022-12-05 07:00:03 +00:00
|
|
|
|
2022-03-02 10:28:29 +00:00
|
|
|
from colossalai.registry import OPHOOKS
|
|
|
|
|
|
|
|
from . import BaseOpHook
|
|
|
|
|
|
|
|
|
|
|
|
@OPHOOKS.register_module
|
2022-12-05 07:00:03 +00:00
|
|
|
class ShardGradMemTracerHook(BaseOpHook):
|
2022-03-02 10:28:29 +00:00
|
|
|
"""
|
|
|
|
A hook to process sharded param before and afther FWD and BWD operator executing.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def pre_fwd_exec(self, module: torch.nn.Module, *args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def post_fwd_exec(self, module: torch.nn.Module, *args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def pre_bwd_exec(self, module: torch.nn.Module, input, output):
|
|
|
|
for param in module.parameters():
|
|
|
|
assert hasattr(param, '_sharded_grad')
|
|
|
|
param._sharded_grad.setup()
|
|
|
|
|
|
|
|
def post_bwd_exec(self, module: torch.nn.Module, input):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def post_iter(self):
|
|
|
|
pass
|