2022-03-02 10:28:29 +00:00
|
|
|
import torch
|
2022-12-05 07:00:03 +00:00
|
|
|
|
2023-09-04 11:56:42 +00:00
|
|
|
from colossalai.legacy.registry import OPHOOKS
|
2022-03-02 10:28:29 +00:00
|
|
|
|
|
|
|
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
|
|
|
"""
|
2023-04-26 03:38:43 +00:00
|
|
|
A hook to process sharded param before and after FWD and BWD operator executing.
|
2022-03-02 10:28:29 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
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
|