2021-12-29 15:32:10 +00:00
|
|
|
import torch.nn as nn
|
|
|
|
from colossalai.context import ParallelMode, seed
|
|
|
|
|
|
|
|
from ..parallel_1d import *
|
|
|
|
from ..utils import get_tensor_parallel_mode
|
2022-04-01 08:49:56 +00:00
|
|
|
from ._utils import ColossalaiModule
|
2021-12-29 15:32:10 +00:00
|
|
|
|
|
|
|
|
2022-04-01 08:49:56 +00:00
|
|
|
class Dropout(ColossalaiModule):
|
2022-03-25 05:02:39 +00:00
|
|
|
"""Dropout layer of colossalai.
|
2022-01-10 10:05:58 +00:00
|
|
|
|
2022-03-25 05:02:39 +00:00
|
|
|
Args:
|
|
|
|
p (float, optional): probability of an element to be zeroed, defaults 0.5.
|
|
|
|
inplace (bool, optional): whether to do dropout in-place, default to be False.
|
2022-01-10 10:05:58 +00:00
|
|
|
"""
|
2021-12-29 15:32:10 +00:00
|
|
|
def __init__(self, p: float = 0.5, inplace: bool = False) -> None:
|
2022-04-01 08:49:56 +00:00
|
|
|
tensor_parallel = get_tensor_parallel_mode()
|
|
|
|
if tensor_parallel == "1d":
|
|
|
|
drop = Dropout1D(p, inplace)
|
2021-12-29 15:32:10 +00:00
|
|
|
else:
|
2022-04-01 08:49:56 +00:00
|
|
|
drop = nn.Dropout(p, inplace)
|
|
|
|
super().__init__(drop, tensor_parallel=tensor_parallel)
|
2021-12-29 15:32:10 +00:00
|
|
|
|
|
|
|
def forward(self, *args):
|
2022-02-14 03:15:02 +00:00
|
|
|
if self.tensor_parallel in [None, '1d']:
|
2022-04-01 08:49:56 +00:00
|
|
|
return self._forward_func(*args)
|
2022-02-14 03:15:02 +00:00
|
|
|
else:
|
|
|
|
with seed(ParallelMode.TENSOR):
|
2022-04-01 08:49:56 +00:00
|
|
|
return self._forward_func(*args)
|