2022-05-31 04:00:12 +00:00
|
|
|
import torch
|
|
|
|
from contextlib import contextmanager
|
|
|
|
from abc import ABC, abstractmethod
|
2022-06-13 08:11:53 +00:00
|
|
|
from typing import List, Tuple, Any
|
2022-06-17 08:12:05 +00:00
|
|
|
from colossalai.tensor.colo_tensor import ColoTensor
|
2022-07-06 08:15:16 +00:00
|
|
|
from colossalai.tensor import ColoTensorSpec
|
2022-05-31 04:00:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ParamOpHook(ABC):
|
2022-06-13 08:11:53 +00:00
|
|
|
"""Hook which is triggered by each operation when operands contain ColoParameter.
|
|
|
|
To customize it, you must inherit this abstract class, and implement ``pre_forward``,
|
|
|
|
``post_forward``, ``pre_backward`` and ``post_backward``. These four methods take a list
|
|
|
|
of ColoParameter.
|
|
|
|
"""
|
2022-05-31 04:00:12 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def pre_forward(self, params: List[torch.Tensor]) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def post_forward(self, params: List[torch.Tensor]) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def pre_backward(self, params: List[torch.Tensor]) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def post_backward(self, params: List[torch.Tensor]) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-06-13 08:11:53 +00:00
|
|
|
class ParamOpHookManager:
|
|
|
|
"""Manage your param op hooks. It only has static methods.
|
|
|
|
The only static method you should call is ``use_hooks(*hooks)``.
|
|
|
|
"""
|
2022-05-31 04:00:12 +00:00
|
|
|
hooks: Tuple[ParamOpHook, ...] = tuple()
|
|
|
|
|
2022-06-13 08:11:53 +00:00
|
|
|
@staticmethod
|
|
|
|
@contextmanager
|
|
|
|
def use_hooks(*hooks: ParamOpHook):
|
|
|
|
"""Change the param op hooks you use. Nested calling is allowed.
|
|
|
|
|
|
|
|
Example::
|
|
|
|
>>> with ParamOpHookManager.use_hooks(*hooks):
|
|
|
|
>>> do_something()
|
|
|
|
>>> with ParamOpHookManager.use_hooks():
|
|
|
|
>>> // clear hooks
|
|
|
|
>>> do_something()
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
old_param_op_hooks = ParamOpHookManager.hooks
|
|
|
|
ParamOpHookManager.hooks = hooks
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
ParamOpHookManager.hooks = old_param_op_hooks
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _trigger_pre_forward(params: List[torch.Tensor]) -> None:
|
|
|
|
for hook in ParamOpHookManager.hooks:
|
|
|
|
hook.pre_forward(params)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _trigger_post_forward(params: List[torch.Tensor]) -> None:
|
|
|
|
for hook in ParamOpHookManager.hooks:
|
|
|
|
hook.post_forward(params)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _trigger_pre_backward(params: List[torch.Tensor]) -> None:
|
|
|
|
for hook in ParamOpHookManager.hooks:
|
|
|
|
hook.pre_backward(params)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _trigger_post_backward(params: List[torch.Tensor]) -> None:
|
|
|
|
for hook in ParamOpHookManager.hooks:
|
|
|
|
hook.post_backward(params)
|
|
|
|
|
|
|
|
@staticmethod
|
2022-06-17 08:12:05 +00:00
|
|
|
def pre_op(params: List[torch.Tensor], *args: Any) -> list:
|
2022-06-13 08:11:53 +00:00
|
|
|
ParamOpHookManager._trigger_pre_forward(params)
|
2022-06-17 08:12:05 +00:00
|
|
|
args_info = _get_colo_tensors_info(*args)
|
|
|
|
rets = PreFwdPostBwd.apply(params, *args)
|
|
|
|
return _update_colo_tensors(args_info, *rets)
|
2022-06-13 08:11:53 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-06-17 08:12:05 +00:00
|
|
|
def post_op(params: List[torch.Tensor], arg: Any) -> Any:
|
2022-06-13 08:11:53 +00:00
|
|
|
ParamOpHookManager._trigger_post_forward(params)
|
2022-06-17 08:12:05 +00:00
|
|
|
arg_info = _get_colo_tensors_info(arg)
|
|
|
|
ret = PostFwdPreBwd.apply(params, arg)
|
|
|
|
return _unpack_args(_update_colo_tensors(arg_info, ret))
|
2022-06-13 08:11:53 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def has_hook() -> bool:
|
|
|
|
return len(ParamOpHookManager.hooks) > 0
|
|
|
|
|
2022-05-31 04:00:12 +00:00
|
|
|
|
|
|
|
class PreFwdPostBwd(torch.autograd.Function):
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def forward(ctx, params, *args):
|
|
|
|
ctx.params = params
|
2022-06-17 08:12:05 +00:00
|
|
|
return _unpack_args(args)
|
2022-05-31 04:00:12 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def backward(ctx, *grads):
|
2022-06-13 08:11:53 +00:00
|
|
|
ParamOpHookManager._trigger_post_backward(ctx.params)
|
2022-05-31 04:00:12 +00:00
|
|
|
return (None,) + grads
|
|
|
|
|
|
|
|
|
|
|
|
class PostFwdPreBwd(torch.autograd.Function):
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def forward(ctx, params, args):
|
|
|
|
ctx.params = params
|
|
|
|
return args
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def backward(ctx, *grads):
|
2022-06-13 08:11:53 +00:00
|
|
|
ParamOpHookManager._trigger_pre_backward(ctx.params)
|
2022-05-31 04:00:12 +00:00
|
|
|
return (None,) + grads
|
2022-06-17 08:12:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _unpack_args(args):
|
|
|
|
if len(args) == 1:
|
|
|
|
return args[0]
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
|
|
|
def _get_colo_tensors_info(*args) -> list:
|
|
|
|
info = []
|
|
|
|
for arg in args:
|
|
|
|
if isinstance(arg, ColoTensor):
|
2022-07-06 08:15:16 +00:00
|
|
|
info.append((arg.__class__, ColoTensorSpec(arg.get_process_group(), arg.dist_spec, arg.compute_spec)))
|
2022-06-17 08:12:05 +00:00
|
|
|
else:
|
|
|
|
info.append(None)
|
|
|
|
return info
|
|
|
|
|
|
|
|
|
|
|
|
def _update_colo_tensors(info, *args) -> list:
|
|
|
|
ret = []
|
|
|
|
for t_info, arg in zip(info, args):
|
|
|
|
if t_info is not None:
|
|
|
|
t_cls, spec = t_info
|
|
|
|
arg = t_cls.from_torch_tensor(arg, spec=spec)
|
|
|
|
ret.append(arg)
|
|
|
|
return ret
|