2021-10-28 16:21:23 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
import math
|
2022-04-01 08:51:52 +00:00
|
|
|
from collections import OrderedDict
|
2021-12-27 07:04:32 +00:00
|
|
|
from typing import Callable, Tuple
|
|
|
|
|
2021-10-28 16:21:23 +00:00
|
|
|
import torch
|
|
|
|
import torch.nn.functional as F
|
2021-12-27 07:04:32 +00:00
|
|
|
from colossalai.communication import broadcast
|
|
|
|
from colossalai.context import ParallelMode, seed
|
2021-10-28 16:21:23 +00:00
|
|
|
from colossalai.core import global_context as gpc
|
2022-02-14 03:15:02 +00:00
|
|
|
from colossalai.global_variables import tensor_parallel_env as env
|
2022-04-01 08:51:52 +00:00
|
|
|
from colossalai.kernel import LayerNorm
|
2021-12-27 07:04:32 +00:00
|
|
|
from colossalai.nn import init as init
|
2021-10-28 16:21:23 +00:00
|
|
|
from colossalai.registry import LAYERS
|
2022-04-01 08:51:52 +00:00
|
|
|
from colossalai.utils.checkpointing import (broadcast_state_dict, gather_tensor_parallel_state_dict,
|
|
|
|
partition_tensor_parallel_state_dict)
|
2022-02-14 03:15:02 +00:00
|
|
|
from colossalai.utils.cuda import get_current_device
|
|
|
|
from torch import Tensor
|
2021-12-27 07:04:32 +00:00
|
|
|
from torch.nn.parameter import Parameter
|
2022-04-14 03:43:56 +00:00
|
|
|
from ..vanilla import VanillaPatchEmbedding, VanillaLayerNorm
|
2021-12-27 07:04:32 +00:00
|
|
|
|
2021-10-28 16:21:23 +00:00
|
|
|
from ..base_layer import ParallelLayer
|
2022-04-01 08:51:52 +00:00
|
|
|
from ..colossalai_layer._utils import ColossalaiModule
|
2021-12-29 15:32:10 +00:00
|
|
|
from ..utils import divide, set_tensor_parallel_attribute_by_partition
|
2022-02-17 14:03:39 +00:00
|
|
|
from ._utils import (gather_forward_split_backward, get_parallel_input, reduce_grad, reduce_input, set_parallel_input,
|
|
|
|
split_forward_gather_backward)
|
2021-12-29 15:32:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@LAYERS.register_module
|
2022-04-01 08:51:52 +00:00
|
|
|
class Linear1D(ColossalaiModule):
|
2022-03-25 05:02:39 +00:00
|
|
|
r"""Linear layer for 1D parallelism.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
in_features (int): size of each input sample.
|
|
|
|
out_features (int): size of each output sample.
|
|
|
|
bias (bool, optional): If set to ``False``, the layer will not learn an additive bias, defaults to ``True``.
|
|
|
|
dtype (:class:`torch.dtype`, optional): The dtype of parameters, defaults to None.
|
|
|
|
gather_output (bool, optional): Whether to call all-gather on output, defaults to False.
|
|
|
|
skip_bias_add (bool, optional): If set to ``True``, it will skip bias add for linear layer,
|
|
|
|
which is preserved for kernel fusion, defaults to False
|
|
|
|
weight_initializer (:class:`typing.Callable`, optional):
|
|
|
|
The initializer of weight, defaults to kaiming uniform initializer.
|
|
|
|
bias_initializer (:class:`typing.Callable`, optional):
|
|
|
|
The initializer of bias, defaults to xavier uniform initializer.
|
|
|
|
|
|
|
|
More details about ``initializer`` please refer to
|
|
|
|
`init <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/nn/init.py>`_.
|
2022-01-10 10:05:58 +00:00
|
|
|
"""
|
2022-02-14 03:15:02 +00:00
|
|
|
|
2021-12-29 15:32:10 +00:00
|
|
|
def __init__(self,
|
|
|
|
in_features: int,
|
|
|
|
out_features: int,
|
|
|
|
bias: bool = True,
|
|
|
|
dtype: torch.dtype = None,
|
|
|
|
gather_output: bool = False,
|
|
|
|
skip_bias_add: bool = False,
|
|
|
|
weight_initializer: Callable = init.kaiming_uniform_(a=math.sqrt(5)),
|
|
|
|
bias_initializer: Callable = init.xavier_uniform_(a=1, scale=1)):
|
|
|
|
parallel_input = get_parallel_input()
|
|
|
|
if not parallel_input:
|
2022-04-01 08:51:52 +00:00
|
|
|
layer = Linear1D_Col(in_features,
|
|
|
|
out_features,
|
|
|
|
bias=bias,
|
|
|
|
dtype=dtype,
|
|
|
|
gather_output=gather_output,
|
|
|
|
skip_bias_add=skip_bias_add,
|
|
|
|
weight_initializer=weight_initializer,
|
|
|
|
bias_initializer=bias_initializer)
|
2021-12-29 15:32:10 +00:00
|
|
|
else:
|
2022-04-01 08:51:52 +00:00
|
|
|
layer = Linear1D_Row(in_features,
|
|
|
|
out_features,
|
|
|
|
bias=bias,
|
|
|
|
dtype=dtype,
|
|
|
|
parallel_input=parallel_input,
|
|
|
|
skip_bias_add=skip_bias_add,
|
|
|
|
weight_initializer=weight_initializer,
|
|
|
|
bias_initializer=bias_initializer)
|
|
|
|
super().__init__(layer)
|
2021-12-29 15:32:10 +00:00
|
|
|
|
|
|
|
|
2022-04-01 08:51:52 +00:00
|
|
|
@LAYERS.register_module
|
|
|
|
class LayerNorm1D(ColossalaiModule):
|
|
|
|
r"""
|
|
|
|
Layer Normalization for colossalai
|
|
|
|
|
2022-04-14 03:43:56 +00:00
|
|
|
Args:
|
|
|
|
normalized_shape (int): input shape from an expected input of size.
|
|
|
|
:math:`[* \times \text{normalized_shape}[0] \times \text{normalized_shape}[1]
|
|
|
|
\times \ldots \times \text{normalized_shape}[-1]]`
|
|
|
|
If a single integer is used, it is treated as a singleton list, and this module will
|
|
|
|
normalize over the last dimension which is expected to be of that specific size.
|
|
|
|
eps (float): a value added to the denominator for numerical stability, defaults to 1e-05.
|
|
|
|
bias (bool, optional): Whether to add a bias, defaults to ``True``.
|
|
|
|
dtype (:class:`torch.dtype`, optional): The dtype of parameters, defaults to None.
|
2022-04-01 08:51:52 +00:00
|
|
|
"""
|
2021-12-29 15:32:10 +00:00
|
|
|
|
2022-04-14 03:43:56 +00:00
|
|
|
def __init__(self, normalized_shape: int, eps=1e-05, bias=True, dtype=None):
|
|
|
|
norm = VanillaLayerNorm(normalized_shape, eps=eps, bias=bias, dtype=dtype)
|
2022-04-01 08:51:52 +00:00
|
|
|
super().__init__(norm)
|
|
|
|
|
|
|
|
def _load_from_state_dict(self, state_dict, prefix, *args):
|
|
|
|
local_state = OrderedDict()
|
|
|
|
weight_key = prefix + 'weight'
|
|
|
|
bias_key = prefix + 'bias'
|
|
|
|
if gpc.get_local_rank(ParallelMode.TENSOR) == 0:
|
|
|
|
# weight
|
|
|
|
weight = state_dict.pop(weight_key, None)
|
|
|
|
if weight is not None:
|
|
|
|
local_state[weight_key] = weight
|
|
|
|
# bias
|
|
|
|
bias = state_dict.pop(bias_key, None)
|
|
|
|
if bias is not None:
|
|
|
|
local_state[bias_key] = bias
|
|
|
|
|
|
|
|
local_state = broadcast_state_dict(local_state, ParallelMode.PARALLEL_1D)
|
|
|
|
super()._load_from_state_dict(local_state, prefix, *args)
|
|
|
|
|
|
|
|
def _save_to_state_dict(self, destination, prefix, keep_vars):
|
|
|
|
if gpc.get_local_rank(ParallelMode.TENSOR) == 0:
|
|
|
|
super()._save_to_state_dict(destination, prefix, keep_vars)
|
2021-12-29 15:32:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@LAYERS.register_module
|
|
|
|
class Classifier1D(ParallelLayer):
|
2022-03-25 05:02:39 +00:00
|
|
|
r"""RowLinear with given weight. Classifier of 1D parallelism.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
in_features (int): size of each input sample.
|
|
|
|
num_classes (int): number of classes.
|
|
|
|
weight (:class:`torch.nn.Parameter`, optional): weight of the classifier, defaults to None.
|
|
|
|
bias (bool, optional): If set to ``False``, the layer will not learn an additive bias, defaults to ``True``.
|
|
|
|
dtype (:class:`torch.dtype`, optional): The dtype of parameters, defaults to None.
|
|
|
|
weight_initializer (:class:`typing.Callable`, optional):
|
|
|
|
The initializer of weight, defaults to kaiming uniform initializer.
|
|
|
|
bias_initializer (:class:`typing.Callable`, optional):
|
|
|
|
The initializer of bias, defaults to xavier uniform initializer.
|
|
|
|
|
|
|
|
More details about ``initializer`` please refer to
|
|
|
|
`init <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/nn/init.py>`_.
|
2022-01-10 10:05:58 +00:00
|
|
|
"""
|
2022-02-14 03:15:02 +00:00
|
|
|
|
2021-12-29 15:32:10 +00:00
|
|
|
def __init__(self,
|
|
|
|
in_features: int,
|
|
|
|
num_classes: int,
|
|
|
|
weight: Parameter = None,
|
|
|
|
bias: bool = True,
|
2022-02-14 03:15:02 +00:00
|
|
|
dtype: torch.dtype = None,
|
2021-12-29 15:32:10 +00:00
|
|
|
weight_initializer: Callable = init.kaiming_uniform_(a=math.sqrt(5)),
|
|
|
|
bias_initializer: Callable = init.xavier_uniform_(a=1, scale=1)):
|
|
|
|
super().__init__()
|
|
|
|
self.in_features = in_features
|
|
|
|
self.num_classes = num_classes
|
|
|
|
self.parallel_input = get_parallel_input()
|
|
|
|
|
|
|
|
# Divide the weight matrix along the last dimension.
|
|
|
|
self.input_size_per_partition = divide(in_features, gpc.tensor_parallel_size)
|
|
|
|
|
|
|
|
# Parameters.
|
|
|
|
# Initialize weight.
|
|
|
|
factory_kwargs = {'device': get_current_device(), 'dtype': dtype}
|
|
|
|
if weight is not None:
|
|
|
|
self.weight = weight
|
|
|
|
self.has_weight = False
|
|
|
|
else:
|
|
|
|
self.weight = Parameter(torch.empty(self.num_classes, self.input_size_per_partition, **factory_kwargs))
|
|
|
|
self.has_weight = True
|
|
|
|
if bias:
|
|
|
|
self.bias = Parameter(torch.empty(self.num_classes, **factory_kwargs))
|
|
|
|
else:
|
|
|
|
self.bias = None
|
|
|
|
with seed(ParallelMode.TENSOR):
|
|
|
|
self.reset_parameters(weight_initializer, bias_initializer)
|
|
|
|
self._set_tensor_parallel_attributes()
|
|
|
|
set_parallel_input(False)
|
2022-02-14 03:15:02 +00:00
|
|
|
env.vocab_parallel = False
|
2021-12-29 15:32:10 +00:00
|
|
|
|
|
|
|
def reset_parameters(self, weight_initializer, bias_initializer) -> None:
|
|
|
|
fan_in, fan_out = self.in_features, self.num_classes
|
|
|
|
if self.has_weight:
|
|
|
|
weight_initializer(self.weight, fan_in=fan_in, fan_out=fan_out)
|
|
|
|
if self.bias is not None:
|
|
|
|
bias_initializer(self.bias, fan_in=fan_in)
|
|
|
|
broadcast(self.bias, gpc.get_ranks_in_group(ParallelMode.PARALLEL_1D)[0], ParallelMode.PARALLEL_1D)
|
|
|
|
|
|
|
|
def _set_tensor_parallel_attributes(self):
|
|
|
|
if self.has_weight:
|
|
|
|
num_partition = gpc.get_world_size(ParallelMode.TENSOR)
|
|
|
|
set_tensor_parallel_attribute_by_partition(self.weight, num_partition)
|
|
|
|
|
2022-09-06 12:18:35 +00:00
|
|
|
def _load_from_global_state_dict(self, state_dict, prefix, *args):
|
2022-04-01 08:51:52 +00:00
|
|
|
local_state = OrderedDict()
|
|
|
|
weight_key = prefix + 'weight'
|
|
|
|
bias_key = prefix + 'bias'
|
|
|
|
if gpc.get_local_rank(ParallelMode.TENSOR) == 0:
|
|
|
|
# weight
|
|
|
|
if self.has_weight:
|
|
|
|
weight = state_dict.pop(weight_key, None)
|
|
|
|
if weight is not None:
|
|
|
|
local_state[weight_key] = weight
|
|
|
|
# bias
|
|
|
|
if self.bias is not None:
|
|
|
|
bias = state_dict.pop(bias_key, None)
|
|
|
|
if bias is not None:
|
|
|
|
local_state[bias_key] = bias
|
|
|
|
|
|
|
|
local_state = partition_tensor_parallel_state_dict(local_state,
|
|
|
|
ParallelMode.PARALLEL_1D,
|
|
|
|
dims={
|
|
|
|
weight_key: -1,
|
|
|
|
bias_key: 0
|
|
|
|
},
|
|
|
|
partition_states={
|
|
|
|
weight_key: True,
|
|
|
|
bias_key: False
|
|
|
|
})
|
2022-09-06 12:18:35 +00:00
|
|
|
super()._load_from_global_state_dict(local_state, prefix, *args)
|
2022-04-01 08:51:52 +00:00
|
|
|
|
2022-09-06 12:18:35 +00:00
|
|
|
def _save_to_global_state_dict(self, destination, prefix, keep_vars):
|
2022-04-01 08:51:52 +00:00
|
|
|
weight_key = prefix + 'weight'
|
|
|
|
bias_key = prefix + 'bias'
|
|
|
|
local_state = OrderedDict()
|
|
|
|
if self.has_weight:
|
|
|
|
local_state[weight_key] = self.weight
|
|
|
|
if self.bias is not None:
|
|
|
|
local_state[bias_key] = self.bias
|
|
|
|
local_state = gather_tensor_parallel_state_dict(local_state,
|
|
|
|
ParallelMode.PARALLEL_1D,
|
|
|
|
dims={
|
|
|
|
weight_key: -1,
|
|
|
|
bias_key: 0
|
|
|
|
},
|
|
|
|
partition_states={
|
|
|
|
weight_key: True,
|
|
|
|
bias_key: False
|
|
|
|
},
|
|
|
|
keep_vars=keep_vars)
|
|
|
|
destination.update(local_state)
|
|
|
|
|
2021-12-29 15:32:10 +00:00
|
|
|
def forward(self, input_: Tensor) -> Tensor:
|
|
|
|
# Set up backprop all-reduce.
|
|
|
|
if self.parallel_input:
|
2022-04-01 08:44:23 +00:00
|
|
|
assert input_.shape[-1] == self.weight.shape[-1], \
|
2022-09-06 12:18:35 +00:00
|
|
|
'Invalid shapes in Classifier1D forward: input={}, weight={}. Expected last dim of input {}.'.format(
|
2022-04-01 08:44:23 +00:00
|
|
|
input_.shape, self.weight.shape, self.weight.shape[-1])
|
2021-12-29 15:32:10 +00:00
|
|
|
input_ = input_
|
|
|
|
else:
|
2022-04-01 08:44:23 +00:00
|
|
|
assert divide(input_.shape[-1], gpc.tensor_parallel_size) == self.weight.shape[-1], \
|
2022-09-06 12:18:35 +00:00
|
|
|
'Invalid shapes in Classifier1D forward: input={}, weight={}. Expected last dim of input {}.'.format(
|
2022-04-01 08:44:23 +00:00
|
|
|
input_.shape, self.weight.shape, self.weight.shape[-1] * gpc.tensor_parallel_size)
|
2021-12-29 15:32:10 +00:00
|
|
|
input_ = split_forward_gather_backward(input_, ParallelMode.PARALLEL_1D, dim=-1)
|
|
|
|
|
|
|
|
output_parallel = F.linear(input_, self.weight)
|
|
|
|
output = reduce_input(output_parallel, ParallelMode.PARALLEL_1D)
|
2022-01-04 12:52:31 +00:00
|
|
|
if self.bias is not None:
|
|
|
|
output = output + self.bias
|
2021-12-29 15:32:10 +00:00
|
|
|
return output
|
2021-10-28 16:21:23 +00:00
|
|
|
|
|
|
|
|
2022-02-14 03:15:02 +00:00
|
|
|
@LAYERS.register_module
|
|
|
|
class VocabParallelClassifier1D(ParallelLayer):
|
2022-03-25 05:02:39 +00:00
|
|
|
r"""ColLinear with given weight. Classifier of 1D parallelism.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
in_features (int): size of each input sample.
|
|
|
|
num_classes (int): number of classes.
|
|
|
|
weight (:class:`torch.nn.Parameter`, optional): weight of the classifier, defaults to None.
|
|
|
|
bias (bool, optional): If set to ``False``, the layer will not learn an additive bias, defaults to ``True``.
|
|
|
|
dtype (:class:`torch.dtype`, optional): The dtype of parameters, defaults to None.
|
|
|
|
weight_initializer (:class:`typing.Callable`, optional):
|
|
|
|
The initializer of weight, defaults to kaiming uniform initializer.
|
|
|
|
bias_initializer (:class:`typing.Callable`, optional):
|
|
|
|
The initializer of bias, defaults to xavier uniform initializer.
|
|
|
|
|
|
|
|
More details about ``initializer`` please refer to
|
|
|
|
`init <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/nn/init.py>`_.
|
2022-02-14 03:15:02 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
in_features: int,
|
|
|
|
num_classes: int,
|
|
|
|
weight: Parameter = None,
|
|
|
|
bias: bool = True,
|
|
|
|
dtype: torch.dtype = None,
|
2022-09-08 08:40:56 +00:00
|
|
|
gather_output: bool = False,
|
2022-02-14 03:15:02 +00:00
|
|
|
weight_initializer: Callable = init.kaiming_uniform_(a=math.sqrt(5)),
|
|
|
|
bias_initializer: Callable = init.xavier_uniform_(a=1, scale=1)):
|
|
|
|
super().__init__()
|
|
|
|
self.in_features = in_features
|
|
|
|
self.num_classes = num_classes
|
2022-09-08 08:40:56 +00:00
|
|
|
self.gather_output = gather_output
|
2022-02-14 03:15:02 +00:00
|
|
|
self.parallel_input = get_parallel_input()
|
|
|
|
|
|
|
|
# Divide the weight matrix along the last dimension.
|
|
|
|
self.num_classes_per_partition = divide(num_classes, gpc.tensor_parallel_size)
|
|
|
|
|
|
|
|
# Parameters.
|
|
|
|
# Initialize weight.
|
|
|
|
factory_kwargs = {'device': get_current_device(), 'dtype': dtype}
|
|
|
|
if weight is not None:
|
|
|
|
self.weight = weight
|
|
|
|
self.has_weight = False
|
|
|
|
else:
|
|
|
|
self.weight = Parameter(torch.empty(self.num_classes_per_partition, self.in_features, **factory_kwargs))
|
|
|
|
self.has_weight = True
|
|
|
|
if bias:
|
|
|
|
self.bias = Parameter(torch.empty(self.num_classes_per_partition, **factory_kwargs))
|
|
|
|
else:
|
|
|
|
self.bias = None
|
|
|
|
with seed(ParallelMode.TENSOR):
|
|
|
|
self.reset_parameters(weight_initializer, bias_initializer)
|
|
|
|
self._set_tensor_parallel_attributes()
|
|
|
|
set_parallel_input(False)
|
|
|
|
env.vocab_parallel = True
|
|
|
|
|
|
|
|
def reset_parameters(self, weight_initializer, bias_initializer) -> None:
|
|
|
|
fan_in, fan_out = self.in_features, self.num_classes
|
|
|
|
if self.has_weight:
|
|
|
|
weight_initializer(self.weight, fan_in=fan_in, fan_out=fan_out)
|
|
|
|
if self.bias is not None:
|
|
|
|
bias_initializer(self.bias, fan_in=fan_in)
|
|
|
|
|
|
|
|
def _set_tensor_parallel_attributes(self):
|
|
|
|
num_partition = gpc.get_world_size(ParallelMode.TENSOR)
|
|
|
|
if self.has_weight:
|
|
|
|
set_tensor_parallel_attribute_by_partition(self.weight, num_partition)
|
|
|
|
if self.bias is not None:
|
|
|
|
set_tensor_parallel_attribute_by_partition(self.bias, num_partition)
|
|
|
|
|
2022-09-06 12:18:35 +00:00
|
|
|
def _load_from_global_state_dict(self, state_dict, prefix, *args):
|
2022-04-01 08:51:52 +00:00
|
|
|
local_state = OrderedDict()
|
|
|
|
weight_key = prefix + 'weight'
|
|
|
|
bias_key = prefix + 'bias'
|
|
|
|
if gpc.get_local_rank(ParallelMode.TENSOR) == 0:
|
|
|
|
# weight
|
|
|
|
if self.has_weight:
|
|
|
|
weight = state_dict.pop(weight_key, None)
|
|
|
|
if weight is not None:
|
|
|
|
local_state[weight_key] = weight
|
|
|
|
# bias
|
|
|
|
if self.bias is not None:
|
|
|
|
bias = state_dict.pop(bias_key, None)
|
|
|
|
if bias is not None:
|
|
|
|
local_state[bias_key] = bias
|
|
|
|
|
|
|
|
local_state = partition_tensor_parallel_state_dict(local_state,
|
|
|
|
ParallelMode.PARALLEL_1D,
|
|
|
|
dims={
|
|
|
|
weight_key: 0,
|
|
|
|
bias_key: 0
|
|
|
|
},
|
|
|
|
partition_states={
|
|
|
|
weight_key: True,
|
|
|
|
bias_key: True
|
|
|
|
})
|
2022-09-06 12:18:35 +00:00
|
|
|
super()._load_from_global_state_dict(local_state, prefix, *args)
|
2022-04-01 08:51:52 +00:00
|
|
|
|
2022-09-06 12:18:35 +00:00
|
|
|
def _save_to_global_state_dict(self, destination, prefix, keep_vars):
|
2022-04-01 08:51:52 +00:00
|
|
|
weight_key = prefix + 'weight'
|
|
|
|
bias_key = prefix + 'bias'
|
|
|
|
local_state = OrderedDict()
|
|
|
|
if self.has_weight:
|
|
|
|
local_state[weight_key] = self.weight
|
|
|
|
if self.bias is not None:
|
|
|
|
local_state[bias_key] = self.bias
|
|
|
|
local_state = gather_tensor_parallel_state_dict(local_state,
|
|
|
|
ParallelMode.PARALLEL_1D,
|
|
|
|
dims={
|
|
|
|
weight_key: 0,
|
|
|
|
bias_key: 0
|
|
|
|
},
|
|
|
|
partition_states={
|
|
|
|
weight_key: True,
|
|
|
|
bias_key: True
|
|
|
|
},
|
|
|
|
keep_vars=keep_vars)
|
|
|
|
destination.update(local_state)
|
|
|
|
|
2022-02-14 03:15:02 +00:00
|
|
|
def forward(self, input_: Tensor) -> Tensor:
|
2022-04-01 08:44:23 +00:00
|
|
|
assert input_.shape[-1] == self.weight.shape[-1], \
|
|
|
|
'Invalid shapes in VocabParallelClassifier1D forward: input={}, weight={}. Expected last dim of input {}.'.format(
|
|
|
|
input_.shape, self.weight.shape, self.weight.shape[-1])
|
2022-02-14 03:15:02 +00:00
|
|
|
# Set up backprop all-reduce.
|
|
|
|
input_parallel = reduce_grad(input_, ParallelMode.PARALLEL_1D)
|
|
|
|
# Matrix multiply.
|
2022-09-08 08:40:56 +00:00
|
|
|
output_parallel = F.linear(input_parallel, self.weight, self.bias)
|
|
|
|
if self.gather_output:
|
|
|
|
# All-gather across the partitions.
|
|
|
|
output = gather_forward_split_backward(output_parallel, ParallelMode.PARALLEL_1D, dim=-1)
|
|
|
|
else:
|
|
|
|
output = output_parallel
|
2022-02-14 03:15:02 +00:00
|
|
|
return output
|
|
|
|
|
|
|
|
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
@LAYERS.register_module
|
2021-10-28 16:21:23 +00:00
|
|
|
class Linear1D_Col(ParallelLayer):
|
2022-03-25 05:02:39 +00:00
|
|
|
r"""Linear layer with column parallelism.
|
2021-10-28 16:21:23 +00:00
|
|
|
|
|
|
|
The linear layer is defined as :math:`Y = XA + b`. A is parallelized along
|
|
|
|
its second dimension as :math:`A = [A_1, ..., A_p]`.
|
|
|
|
|
2022-03-25 05:02:39 +00:00
|
|
|
Args:
|
|
|
|
in_features (int): size of each input sample.
|
|
|
|
out_features (int): size of each output sample.
|
|
|
|
bias (bool, optional): If set to ``False``, the layer will not learn an additive bias, defaults to ``True``.
|
|
|
|
dtype (:class:`torch.dtype`, optional): The dtype of parameters, defaults to None.
|
|
|
|
gather_output (bool, optional): If true, call all-gather on output and make Y available
|
2021-10-28 16:21:23 +00:00
|
|
|
to all GPUs, otherwise, every GPU will have its output
|
|
|
|
which is :math:`Y_i = XA_i`, defaults to False
|
2022-03-25 05:02:39 +00:00
|
|
|
skip_bias_add (bool, optional): If set to ``True``, it will skip bias add for linear layer,
|
|
|
|
which is preserved for kernel fusion, defaults to Fals
|
|
|
|
weight_initializer (:class:`typing.Callable`, optional):
|
|
|
|
The initializer of weight, defaults to kaiming uniform initializer.
|
|
|
|
bias_initializer (:class:`typing.Callable`, optional):
|
|
|
|
The initializer of bias, defaults to xavier uniform initializer.
|
|
|
|
|
|
|
|
More details about ``initializer`` please refer to
|
|
|
|
`init <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/nn/init.py>`_.
|
2021-10-28 16:21:23 +00:00
|
|
|
"""
|
2022-01-04 12:52:31 +00:00
|
|
|
|
2021-10-28 16:21:23 +00:00
|
|
|
def __init__(self,
|
|
|
|
in_features: int,
|
2021-12-27 07:04:32 +00:00
|
|
|
out_features: int,
|
2021-10-28 16:21:23 +00:00
|
|
|
bias: bool = True,
|
|
|
|
dtype: torch.dtype = None,
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
gather_output: bool = False,
|
|
|
|
skip_bias_add: bool = False,
|
2021-12-27 07:04:32 +00:00
|
|
|
weight_initializer: Callable = init.kaiming_uniform_(a=math.sqrt(5)),
|
|
|
|
bias_initializer: Callable = init.xavier_uniform_(a=1, scale=1)):
|
2021-10-28 16:21:23 +00:00
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
# Keep input parameters
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
self.in_features = in_features
|
2021-12-27 07:04:32 +00:00
|
|
|
self.out_features = out_features
|
2021-10-28 16:21:23 +00:00
|
|
|
self.gather_output = gather_output
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
self.skip_bias_add = skip_bias_add
|
2021-10-28 16:21:23 +00:00
|
|
|
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
if skip_bias_add and not bias:
|
|
|
|
raise ValueError('cannot skip bias addition if bias is None')
|
|
|
|
|
2021-12-27 07:04:32 +00:00
|
|
|
self.out_features_per_partition = divide(out_features, gpc.tensor_parallel_size)
|
2021-10-28 16:21:23 +00:00
|
|
|
|
|
|
|
# Parameters.
|
|
|
|
# Initialize weight.
|
|
|
|
factory_kwargs = {'device': get_current_device(), 'dtype': dtype}
|
2021-12-27 07:04:32 +00:00
|
|
|
self.weight = Parameter(torch.empty(self.out_features_per_partition, self.in_features, **factory_kwargs))
|
2021-10-28 16:21:23 +00:00
|
|
|
|
|
|
|
if bias:
|
2021-12-27 07:04:32 +00:00
|
|
|
self.bias = Parameter(torch.empty(self.out_features_per_partition, **factory_kwargs))
|
2021-10-28 16:21:23 +00:00
|
|
|
else:
|
2021-12-27 07:04:32 +00:00
|
|
|
self.bias = None
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
with seed(ParallelMode.TENSOR):
|
2021-12-27 07:04:32 +00:00
|
|
|
self.reset_parameters(weight_initializer, bias_initializer)
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
self._set_tensor_parallel_attributes()
|
2022-03-31 09:11:03 +00:00
|
|
|
is_parallel_output = not self.gather_output
|
|
|
|
set_parallel_input(is_parallel_output)
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
|
2021-12-27 07:04:32 +00:00
|
|
|
def reset_parameters(self, weight_initializer, bias_initializer) -> None:
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
fan_in, fan_out = self.in_features, self.out_features
|
2021-12-27 07:04:32 +00:00
|
|
|
weight_initializer(self.weight, fan_in=fan_in, fan_out=fan_out)
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
if self.bias is not None:
|
2021-12-27 07:04:32 +00:00
|
|
|
bias_initializer(self.bias, fan_in=fan_in)
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
|
|
|
|
def _set_tensor_parallel_attributes(self):
|
|
|
|
num_partition = gpc.get_world_size(ParallelMode.TENSOR)
|
|
|
|
set_tensor_parallel_attribute_by_partition(self.weight, num_partition)
|
|
|
|
if self.bias is not None:
|
|
|
|
set_tensor_parallel_attribute_by_partition(self.bias, num_partition)
|
2021-10-28 16:21:23 +00:00
|
|
|
|
2022-09-06 12:18:35 +00:00
|
|
|
def _load_from_global_state_dict(self, state_dict, prefix, *args):
|
2022-04-01 08:51:52 +00:00
|
|
|
local_state = OrderedDict()
|
|
|
|
weight_key = prefix + 'weight'
|
|
|
|
bias_key = prefix + 'bias'
|
|
|
|
if gpc.get_local_rank(ParallelMode.TENSOR) == 0:
|
|
|
|
# weight
|
|
|
|
weight = state_dict.pop(weight_key, None)
|
|
|
|
if weight is not None:
|
|
|
|
local_state[weight_key] = weight
|
|
|
|
# bias
|
|
|
|
if self.bias is not None:
|
|
|
|
bias = state_dict.pop(bias_key, None)
|
|
|
|
if bias is not None:
|
|
|
|
local_state[bias_key] = bias
|
|
|
|
|
|
|
|
local_state = partition_tensor_parallel_state_dict(local_state,
|
|
|
|
ParallelMode.PARALLEL_1D,
|
|
|
|
dims={
|
|
|
|
weight_key: 0,
|
|
|
|
bias_key: 0
|
|
|
|
},
|
|
|
|
partition_states={
|
|
|
|
weight_key: True,
|
|
|
|
bias_key: True
|
|
|
|
})
|
2022-09-06 12:18:35 +00:00
|
|
|
super()._load_from_global_state_dict(local_state, prefix, *args)
|
2022-04-01 08:51:52 +00:00
|
|
|
|
2022-09-06 12:18:35 +00:00
|
|
|
def _save_to_global_state_dict(self, destination, prefix, keep_vars):
|
2022-04-01 08:51:52 +00:00
|
|
|
weight_key = prefix + 'weight'
|
|
|
|
bias_key = prefix + 'bias'
|
|
|
|
local_state = OrderedDict({weight_key: self.weight})
|
|
|
|
if self.bias is not None:
|
|
|
|
local_state[bias_key] = self.bias
|
|
|
|
local_state = gather_tensor_parallel_state_dict(local_state,
|
|
|
|
ParallelMode.PARALLEL_1D,
|
|
|
|
dims={
|
|
|
|
weight_key: 0,
|
|
|
|
bias_key: 0
|
|
|
|
},
|
|
|
|
partition_states={
|
|
|
|
weight_key: True,
|
|
|
|
bias_key: True
|
|
|
|
},
|
|
|
|
keep_vars=keep_vars)
|
|
|
|
destination.update(local_state)
|
|
|
|
|
2021-10-28 16:21:23 +00:00
|
|
|
def forward(self, input_: Tensor) -> Tuple[Tensor, Tensor]:
|
2022-04-01 08:44:23 +00:00
|
|
|
assert input_.shape[-1] == self.weight.shape[-1], \
|
|
|
|
'Invalid shapes in Linear1D_Col forward: input={}, weight={}. Expected last dim of input {}.'.format(
|
|
|
|
input_.shape, self.weight.shape, self.weight.shape[-1])
|
2021-10-28 16:21:23 +00:00
|
|
|
# Set up backprop all-reduce.
|
|
|
|
input_parallel = reduce_grad(input_, ParallelMode.PARALLEL_1D)
|
|
|
|
# Matrix multiply.
|
|
|
|
|
|
|
|
bias = self.bias if not self.skip_bias_add else None
|
|
|
|
output_parallel = F.linear(input_parallel, self.weight, bias)
|
|
|
|
if self.gather_output:
|
|
|
|
# All-gather across the partitions.
|
2021-12-27 07:04:32 +00:00
|
|
|
output = gather_forward_split_backward(output_parallel, ParallelMode.PARALLEL_1D, dim=-1)
|
2021-10-28 16:21:23 +00:00
|
|
|
else:
|
|
|
|
output = output_parallel
|
2022-04-01 08:44:23 +00:00
|
|
|
|
2021-10-28 16:21:23 +00:00
|
|
|
if self.skip_bias_add:
|
|
|
|
return output, self.bias
|
|
|
|
else:
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
@LAYERS.register_module
|
|
|
|
class Linear1D_Row(ParallelLayer):
|
2022-03-25 05:02:39 +00:00
|
|
|
r""" Linear layer with row parallelism
|
|
|
|
|
|
|
|
Args:
|
|
|
|
in_features (int): size of each input sample.
|
|
|
|
out_features (int): size of each output sample.
|
|
|
|
bias (bool, optional): If set to ``False``, the layer will not learn an additive bias, defaults to ``True``.
|
|
|
|
dtype (:class:`torch.dtype`, optional): The dtype of parameters, defaults to None.
|
|
|
|
parallel_input (bool, optional): If set to ``True``, it's assumed that the input is split, defaults to False.
|
|
|
|
skip_bias_add (bool, optional): If set to ``True``, it will skip bias add for linear layer,
|
|
|
|
which is preserved for kernel fusion, defaults to Fals
|
|
|
|
weight_initializer (:class:`typing.Callable`, optional):
|
|
|
|
The initializer of weight, defaults to kaiming uniform initializer.
|
|
|
|
bias_initializer (:class:`typing.Callable`, optional):
|
|
|
|
The initializer of bias, defaults to xavier uniform initializer.
|
|
|
|
|
|
|
|
More details about ``initializer`` please refer to
|
|
|
|
`init <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/nn/init.py>`_.
|
2021-10-28 16:21:23 +00:00
|
|
|
"""
|
2022-01-04 12:52:31 +00:00
|
|
|
|
2021-10-28 16:21:23 +00:00
|
|
|
def __init__(self,
|
|
|
|
in_features: int,
|
|
|
|
out_features: int,
|
|
|
|
bias: bool = True,
|
|
|
|
dtype: torch.dtype = None,
|
2021-12-27 07:04:32 +00:00
|
|
|
parallel_input: bool = True,
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
skip_bias_add: bool = False,
|
2021-12-27 07:04:32 +00:00
|
|
|
weight_initializer: Callable = init.kaiming_uniform_(a=math.sqrt(5)),
|
|
|
|
bias_initializer: Callable = init.xavier_uniform_(a=1, scale=1)):
|
2021-10-28 16:21:23 +00:00
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
# Keep input parameters
|
|
|
|
self.in_features = in_features
|
|
|
|
self.out_features = out_features
|
|
|
|
self.parallel_input = parallel_input
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
self.skip_bias_add = skip_bias_add
|
|
|
|
|
|
|
|
if skip_bias_add and not bias:
|
|
|
|
raise ValueError('cannot skip bias addition if bias is None')
|
2021-10-28 16:21:23 +00:00
|
|
|
|
|
|
|
# Divide the weight matrix along the last dimension.
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
self.input_size_per_partition = divide(in_features, gpc.tensor_parallel_size)
|
2021-10-28 16:21:23 +00:00
|
|
|
|
|
|
|
# Parameters.
|
|
|
|
# Initialize weight.
|
|
|
|
factory_kwargs = {'device': get_current_device(), 'dtype': dtype}
|
2021-12-27 07:04:32 +00:00
|
|
|
self.weight = Parameter(torch.empty(self.out_features, self.input_size_per_partition, **factory_kwargs))
|
2021-10-28 16:21:23 +00:00
|
|
|
|
|
|
|
if bias:
|
2021-12-27 07:04:32 +00:00
|
|
|
self.bias = Parameter(torch.empty(self.out_features, **factory_kwargs))
|
2021-10-28 16:21:23 +00:00
|
|
|
else:
|
2021-12-27 07:04:32 +00:00
|
|
|
self.bias = None
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
with seed(ParallelMode.TENSOR):
|
2021-12-27 07:04:32 +00:00
|
|
|
self.reset_parameters(weight_initializer, bias_initializer)
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
self._set_tensor_parallel_attributes()
|
2021-12-29 15:32:10 +00:00
|
|
|
set_parallel_input(False)
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
|
2021-12-27 07:04:32 +00:00
|
|
|
def reset_parameters(self, weight_initializer, bias_initializer) -> None:
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
fan_in, fan_out = self.in_features, self.out_features
|
2021-12-27 07:04:32 +00:00
|
|
|
weight_initializer(self.weight, fan_in=fan_in, fan_out=fan_out)
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
if self.bias is not None:
|
2021-12-27 07:04:32 +00:00
|
|
|
bias_initializer(self.bias, fan_in=fan_in)
|
2022-02-14 03:15:02 +00:00
|
|
|
broadcast(self.bias, gpc.get_ranks_in_group(ParallelMode.PARALLEL_1D)[0], ParallelMode.PARALLEL_1D)
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
|
|
|
|
def _set_tensor_parallel_attributes(self):
|
|
|
|
num_partition = gpc.get_world_size(ParallelMode.TENSOR)
|
|
|
|
set_tensor_parallel_attribute_by_partition(self.weight, num_partition)
|
2021-10-28 16:21:23 +00:00
|
|
|
|
2022-09-06 12:18:35 +00:00
|
|
|
def _load_from_global_state_dict(self, state_dict, prefix, *args):
|
2022-04-01 08:51:52 +00:00
|
|
|
local_state = OrderedDict()
|
|
|
|
weight_key = prefix + 'weight'
|
|
|
|
bias_key = prefix + 'bias'
|
|
|
|
if gpc.get_local_rank(ParallelMode.TENSOR) == 0:
|
|
|
|
# weight
|
|
|
|
weight = state_dict.pop(weight_key, None)
|
|
|
|
if weight is not None:
|
|
|
|
local_state[weight_key] = weight
|
|
|
|
# bias
|
|
|
|
if self.bias is not None:
|
|
|
|
bias = state_dict.pop(bias_key, None)
|
|
|
|
if bias is not None:
|
|
|
|
local_state[bias_key] = bias
|
|
|
|
|
|
|
|
local_state = partition_tensor_parallel_state_dict(local_state,
|
|
|
|
ParallelMode.PARALLEL_1D,
|
|
|
|
dims={
|
|
|
|
weight_key: -1,
|
|
|
|
bias_key: 0
|
|
|
|
},
|
|
|
|
partition_states={
|
|
|
|
weight_key: True,
|
|
|
|
bias_key: False
|
|
|
|
})
|
2022-09-06 12:18:35 +00:00
|
|
|
super()._load_from_global_state_dict(local_state, prefix, *args)
|
2022-04-01 08:51:52 +00:00
|
|
|
|
2022-09-06 12:18:35 +00:00
|
|
|
def _save_to_global_state_dict(self, destination, prefix, keep_vars):
|
2022-04-01 08:51:52 +00:00
|
|
|
weight_key = prefix + 'weight'
|
|
|
|
bias_key = prefix + 'bias'
|
|
|
|
local_state = OrderedDict({weight_key: self.weight})
|
|
|
|
if self.bias is not None:
|
|
|
|
local_state[bias_key] = self.bias
|
|
|
|
local_state = gather_tensor_parallel_state_dict(local_state,
|
|
|
|
ParallelMode.PARALLEL_1D,
|
|
|
|
dims={
|
|
|
|
weight_key: -1,
|
|
|
|
bias_key: 0
|
|
|
|
},
|
|
|
|
partition_states={
|
|
|
|
weight_key: True,
|
|
|
|
bias_key: False
|
|
|
|
},
|
|
|
|
keep_vars=keep_vars)
|
|
|
|
destination.update(local_state)
|
|
|
|
|
2021-10-28 16:21:23 +00:00
|
|
|
def forward(self, input_: Tensor) -> Tensor:
|
|
|
|
# Set up backprop all-reduce.
|
|
|
|
if self.parallel_input:
|
2022-04-01 08:44:23 +00:00
|
|
|
assert input_.shape[-1] == self.weight.shape[-1], \
|
2022-09-06 12:18:35 +00:00
|
|
|
'Invalid shapes in Linear1D_Row forward: input={}, weight={}. Expected last dim of input {}.'.format(
|
2022-04-01 08:44:23 +00:00
|
|
|
input_.shape, self.weight.shape, self.weight.shape[-1])
|
2021-10-28 16:21:23 +00:00
|
|
|
input_ = input_
|
|
|
|
else:
|
2022-04-01 08:44:23 +00:00
|
|
|
assert divide(input_.shape[-1], gpc.tensor_parallel_size) == self.weight.shape[-1], \
|
2022-09-06 12:18:35 +00:00
|
|
|
'Invalid shapes in Linear1D_Row forward: input={}, weight={}. Expected last dim of input {}.'.format(
|
2022-04-01 08:44:23 +00:00
|
|
|
input_.shape, self.weight.shape, self.weight.shape[-1] * gpc.tensor_parallel_size)
|
2021-12-27 07:04:32 +00:00
|
|
|
input_ = split_forward_gather_backward(input_, ParallelMode.PARALLEL_1D, dim=-1)
|
2021-10-28 16:21:23 +00:00
|
|
|
|
|
|
|
output_parallel = F.linear(input_, self.weight)
|
|
|
|
output = reduce_input(output_parallel, ParallelMode.PARALLEL_1D)
|
|
|
|
|
|
|
|
if not self.skip_bias_add:
|
2022-02-14 03:15:02 +00:00
|
|
|
if self.bias is not None:
|
|
|
|
output = output + self.bias
|
Develop/experiments (#59)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
* Split conv2d, class token, positional embedding in 2d, Fix random number in ddp
Fix convergence in cifar10, Imagenet1000
* Integrate 1d tensor parallel in Colossal-AI (#39)
* fixed 1D and 2D convergence (#38)
* optimized 2D operations
* fixed 1D ViT convergence problem
* Feature/ddp (#49)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* support torch ddp
* fix loss accumulation
* add log for ddp
* change seed
* modify timing hook
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* Feature/pipeline (#40)
* remove redundancy func in setup (#19) (#20)
* use env to control the language of doc (#24) (#25)
* Support TP-compatible Torch AMP and Update trainer API (#27)
* Add gradient accumulation, fix lr scheduler
* fix FP16 optimizer and adapted torch amp with tensor parallel (#18)
* fixed bugs in compatibility between torch amp and tensor parallel and performed some minor fixes
* fixed trainer
* Revert "fixed trainer"
This reverts commit 2e0b0b76990e8d4e337add483d878c0f61cf5097.
* improved consistency between trainer, engine and schedule (#23)
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
* add an example of ViT-B/16 and remove w_norm clipping in LAMB (#29)
* add explanation for ViT example (#35) (#36)
* optimize communication of pipeline parallel
* fix grad clip for pipeline
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
* optimized 3d layer to fix slow computation ; tested imagenet performance with 3d; reworked lr_scheduler config definition; fixed launch args; fixed some printing issues; simplified apis of 3d layers (#51)
* Update 2.5d layer code to get a similar accuracy on imagenet-1k dataset
* update api for better usability (#58)
update api for better usability
Co-authored-by: 1SAA <c2h214748@gmail.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: puck_WCR <46049915+WANG-CR@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
Co-authored-by: BoxiangW <45734921+BoxiangW@users.noreply.github.com>
2021-12-09 07:08:29 +00:00
|
|
|
return output
|
|
|
|
else:
|
|
|
|
return output, self.bias
|
|
|
|
|
|
|
|
|
2021-12-29 15:32:10 +00:00
|
|
|
@LAYERS.register_module
|
|
|
|
class Embedding1D(ParallelLayer):
|
2022-03-25 05:02:39 +00:00
|
|
|
r"""Embedding for 1D parallelism.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
num_embeddings (int): number of embeddings.
|
|
|
|
embedding_dim (int): dimension of embedding.
|
|
|
|
padding_idx (int, optional): If specified, the entries at padding_idx do not contribute to the gradient;
|
|
|
|
therefore, the embedding vector at padding_idx is not updated during training,
|
|
|
|
i.e. it remains as a fixed “pad”, defaults to None.
|
|
|
|
dtype (:class:`torch.dtype`, optional): The dtype of parameters, defaults to None.
|
|
|
|
weight_initializer (:class:`typing.Callable`, optional):
|
|
|
|
he initializer of weight, defaults to normal initializer.
|
|
|
|
|
|
|
|
The ``args`` and ``kwargs`` used in :class:`torch.nn.functional.embedding` should contain:
|
|
|
|
::
|
|
|
|
|
|
|
|
max_norm (float, optional): If given, each embedding vector with norm larger than max_norm is
|
|
|
|
renormalized to have norm max_norm. Note: this will modify weight in-place.
|
|
|
|
norm_type (float, optional): The p of the p-norm to compute for the max_norm option. Default 2.
|
|
|
|
scale_grad_by_freq (bool, optional): If given, this will scale gradients by the inverse
|
|
|
|
of frequency of the words in the mini-batch. Default False.
|
|
|
|
sparse (bool, optional): If True, gradient w.r.t. weight will be a sparse tensor. Default False.
|
|
|
|
|
|
|
|
More details about ``args`` and ``kwargs`` could be found in
|
|
|
|
`Embedding <https://pytorch.org/docs/stable/generated/torch.nn.functional.embedding.html#torch.nn.functional.embedding>`_.
|
|
|
|
|
|
|
|
More details about ``initializer`` please refer to
|
|
|
|
`init <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/nn/init.py>`_
|
2022-01-10 10:05:58 +00:00
|
|
|
"""
|
2022-02-14 03:15:02 +00:00
|
|
|
|
2021-12-29 15:32:10 +00:00
|
|
|
def __init__(self,
|
|
|
|
num_embeddings: int,
|
|
|
|
embedding_dim: int,
|
|
|
|
padding_idx: int = None,
|
2022-02-14 03:15:02 +00:00
|
|
|
dtype: torch.dtype = None,
|
2021-12-29 15:32:10 +00:00
|
|
|
weight_initializer: Callable = init.normal_(),
|
|
|
|
*args,
|
|
|
|
**kwargs):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
self.num_embeddings = num_embeddings
|
|
|
|
self.embed_dim = embedding_dim
|
|
|
|
embed_dim_per_partition = divide(embedding_dim, gpc.tensor_parallel_size)
|
|
|
|
|
|
|
|
self.padding_idx = padding_idx
|
|
|
|
self.embed_args = args
|
|
|
|
self.embed_kwargs = kwargs
|
|
|
|
|
|
|
|
self.weight = Parameter(
|
|
|
|
torch.empty((num_embeddings, embed_dim_per_partition), device=get_current_device(), dtype=dtype))
|
|
|
|
|
|
|
|
self.reset_parameters(weight_initializer)
|
|
|
|
self._set_tensor_parallel_attributes()
|
|
|
|
set_parallel_input(False)
|
|
|
|
|
|
|
|
def _set_tensor_parallel_attributes(self):
|
|
|
|
set_tensor_parallel_attribute_by_partition(self.weight, gpc.tensor_parallel_size)
|
|
|
|
|
|
|
|
def reset_parameters(self, weight_initializer) -> None:
|
|
|
|
with seed(ParallelMode.TENSOR):
|
|
|
|
fan_in, fan_out = self.num_embeddings, self.embed_dim
|
|
|
|
weight_initializer(self.weight, fan_in=fan_in, fan_out=fan_out)
|
|
|
|
self._fill_padding_idx_with_zero()
|
|
|
|
|
|
|
|
def _fill_padding_idx_with_zero(self) -> None:
|
|
|
|
if self.padding_idx is not None:
|
|
|
|
with torch.no_grad():
|
|
|
|
self.weight[self.padding_idx].fill_(0)
|
|
|
|
|
2022-09-06 12:18:35 +00:00
|
|
|
def _load_from_global_state_dict(self, state_dict, prefix, *args):
|
2022-04-01 08:51:52 +00:00
|
|
|
local_state = OrderedDict()
|
|
|
|
weight_key = prefix + 'weight'
|
|
|
|
if gpc.get_local_rank(ParallelMode.TENSOR) == 0:
|
|
|
|
# weight
|
|
|
|
weight = state_dict.pop(weight_key, None)
|
|
|
|
if weight is not None:
|
|
|
|
local_state[weight_key] = weight
|
|
|
|
|
|
|
|
local_state = partition_tensor_parallel_state_dict(local_state,
|
|
|
|
ParallelMode.PARALLEL_1D,
|
|
|
|
dims={weight_key: -1},
|
|
|
|
partition_states={weight_key: True})
|
2022-09-06 12:18:35 +00:00
|
|
|
super()._load_from_global_state_dict(local_state, prefix, *args)
|
2022-04-01 08:51:52 +00:00
|
|
|
|
2022-09-06 12:18:35 +00:00
|
|
|
def _save_to_global_state_dict(self, destination, prefix, keep_vars):
|
2022-04-01 08:51:52 +00:00
|
|
|
weight_key = prefix + 'weight'
|
|
|
|
local_state = OrderedDict({weight_key: self.weight})
|
|
|
|
local_state = gather_tensor_parallel_state_dict(local_state,
|
|
|
|
ParallelMode.PARALLEL_1D,
|
|
|
|
dims={weight_key: -1},
|
|
|
|
partition_states={weight_key: True},
|
|
|
|
keep_vars=keep_vars)
|
|
|
|
destination.update(local_state)
|
|
|
|
|
2021-12-29 15:32:10 +00:00
|
|
|
def forward(self, input_: Tensor) -> Tensor:
|
|
|
|
|
|
|
|
output_parallel = F.embedding(input_, self.weight, self.padding_idx, *self.embed_args, **self.embed_kwargs)
|
|
|
|
|
|
|
|
output = gather_forward_split_backward(output_parallel, ParallelMode.PARALLEL_1D, dim=-1)
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
2022-02-14 03:15:02 +00:00
|
|
|
@LAYERS.register_module
|
2022-09-06 12:18:35 +00:00
|
|
|
class VocabParallelEmbedding1D(ParallelLayer):
|
2022-03-25 05:02:39 +00:00
|
|
|
r"""Embedding parallelized in the vocabulary dimension.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
num_embeddings (int): number of embeddings.
|
|
|
|
embedding_dim (int): dimension of embedding.
|
|
|
|
padding_idx (int, optional): If specified, the entries at padding_idx do not contribute to the gradient;
|
|
|
|
therefore, the embedding vector at padding_idx is not updated during training,
|
|
|
|
i.e. it remains as a fixed “pad”, defaults to None.
|
|
|
|
dtype (:class:`torch.dtype`, optional): The dtype of parameters, defaults to None.
|
|
|
|
weight_initializer (:class:`typing.Callable`, optional):
|
|
|
|
he initializer of weight, defaults to normal initializer.
|
|
|
|
|
|
|
|
The ``args`` and ``kwargs`` used in :class:``torch.nn.functional.embedding`` should contain:
|
|
|
|
::
|
|
|
|
|
|
|
|
max_norm (float, optional): If given, each embedding vector with norm larger than max_norm is
|
|
|
|
renormalized to have norm max_norm. Note: this will modify weight in-place.
|
|
|
|
norm_type (float, optional): The p of the p-norm to compute for the max_norm option. Default 2.
|
|
|
|
scale_grad_by_freq (bool, optional): If given, this will scale gradients by the inverse
|
|
|
|
of frequency of the words in the mini-batch. Default False.
|
|
|
|
sparse (bool, optional): If True, gradient w.r.t. weight will be a sparse tensor. Default False.
|
|
|
|
|
|
|
|
More details about ``args`` and ``kwargs`` could be found in
|
|
|
|
`Embedding <https://pytorch.org/docs/stable/generated/torch.nn.functional.embedding.html#torch.nn.functional.embedding>`_.
|
|
|
|
|
|
|
|
More details about initializer please refer to
|
|
|
|
`init <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/nn/init.py>`_.
|
2022-02-14 03:15:02 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
num_embeddings: int,
|
|
|
|
embedding_dim: int,
|
|
|
|
padding_idx: int = None,
|
|
|
|
dtype: torch.dtype = None,
|
|
|
|
weight_initializer: Callable = init.normal_(),
|
|
|
|
*args,
|
|
|
|
**kwargs):
|
|
|
|
super().__init__()
|
|
|
|
self.num_embeddings = num_embeddings
|
|
|
|
self.embed_dim = embedding_dim
|
|
|
|
self.padding_idx = padding_idx
|
|
|
|
self.embed_args = args
|
|
|
|
self.embed_kwargs = kwargs
|
|
|
|
|
|
|
|
tensor_parallel_size = gpc.get_world_size(ParallelMode.PARALLEL_1D)
|
|
|
|
tensor_parallel_rank = gpc.get_local_rank(ParallelMode.PARALLEL_1D)
|
|
|
|
self.num_embeddings_per_partition = divide(num_embeddings, tensor_parallel_size)
|
|
|
|
self.vocab_start_index = tensor_parallel_rank * self.num_embeddings_per_partition
|
|
|
|
self.vocab_end_index = self.vocab_start_index + self.num_embeddings_per_partition
|
|
|
|
|
|
|
|
self.weight = Parameter(
|
|
|
|
torch.empty((self.num_embeddings_per_partition, self.embed_dim), device=get_current_device(), dtype=dtype))
|
|
|
|
|
|
|
|
self.reset_parameters(weight_initializer)
|
|
|
|
self._set_tensor_parallel_attributes()
|
|
|
|
set_parallel_input(False)
|
|
|
|
env.vocab_parallel = True
|
|
|
|
|
|
|
|
def _set_tensor_parallel_attributes(self):
|
|
|
|
set_tensor_parallel_attribute_by_partition(self.weight, gpc.tensor_parallel_size)
|
|
|
|
|
|
|
|
def reset_parameters(self, weight_initializer) -> None:
|
|
|
|
with seed(ParallelMode.TENSOR):
|
|
|
|
fan_in, fan_out = self.num_embeddings, self.embed_dim
|
|
|
|
weight_initializer(self.weight, fan_in=fan_in, fan_out=fan_out)
|
|
|
|
self._fill_padding_idx_with_zero()
|
|
|
|
|
|
|
|
def _fill_padding_idx_with_zero(self) -> None:
|
2022-02-17 14:03:39 +00:00
|
|
|
if self.padding_idx is not None and \
|
2022-04-01 08:51:52 +00:00
|
|
|
self.padding_idx >= self.vocab_start_index and self.padding_idx < self.vocab_end_index:
|
2022-02-14 03:15:02 +00:00
|
|
|
with torch.no_grad():
|
2022-02-17 14:03:39 +00:00
|
|
|
self.weight[self.padding_idx - self.vocab_start_index].fill_(0)
|
2022-02-14 03:15:02 +00:00
|
|
|
|
2022-09-06 12:18:35 +00:00
|
|
|
def _load_from_global_state_dict(self, state_dict, prefix, *args):
|
2022-04-01 08:51:52 +00:00
|
|
|
local_state = OrderedDict()
|
|
|
|
weight_key = prefix + 'weight'
|
|
|
|
if gpc.get_local_rank(ParallelMode.TENSOR) == 0:
|
|
|
|
# weight
|
|
|
|
weight = state_dict.pop(weight_key, None)
|
|
|
|
if weight is not None:
|
|
|
|
local_state[weight_key] = weight
|
|
|
|
|
|
|
|
local_state = partition_tensor_parallel_state_dict(local_state,
|
|
|
|
ParallelMode.PARALLEL_1D,
|
|
|
|
dims={weight_key: 0},
|
|
|
|
partition_states={weight_key: True})
|
2022-09-06 12:18:35 +00:00
|
|
|
super()._load_from_global_state_dict(local_state, prefix, *args)
|
2022-04-01 08:51:52 +00:00
|
|
|
|
2022-09-06 12:18:35 +00:00
|
|
|
def _save_to_global_state_dict(self, destination, prefix, keep_vars):
|
2022-04-01 08:51:52 +00:00
|
|
|
weight_key = prefix + 'weight'
|
|
|
|
local_state = OrderedDict({weight_key: self.weight})
|
|
|
|
local_state = gather_tensor_parallel_state_dict(local_state,
|
|
|
|
ParallelMode.PARALLEL_1D,
|
|
|
|
dims={weight_key: 0},
|
|
|
|
partition_states={weight_key: True},
|
|
|
|
keep_vars=keep_vars)
|
|
|
|
destination.update(local_state)
|
|
|
|
|
2022-02-14 03:15:02 +00:00
|
|
|
def forward(self, input_: Tensor) -> Tensor:
|
|
|
|
# Build the mask.
|
|
|
|
input_mask = (input_ < self.vocab_start_index) | (input_ >= self.vocab_end_index)
|
|
|
|
# Mask the input.
|
|
|
|
masked_input = input_.clone() - self.vocab_start_index
|
|
|
|
masked_input[input_mask] = 0
|
|
|
|
|
|
|
|
output_parallel = F.embedding(masked_input, self.weight, self.padding_idx, *self.embed_args,
|
|
|
|
**self.embed_kwargs)
|
|
|
|
|
|
|
|
# Mask the output embedding.
|
|
|
|
output_parallel[input_mask, :] = 0.
|
|
|
|
# Reduce across all the model parallel GPUs.
|
|
|
|
output = reduce_input(output_parallel, ParallelMode.PARALLEL_1D)
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
2021-12-29 15:32:10 +00:00
|
|
|
@LAYERS.register_module
|
|
|
|
class Dropout1D(ParallelLayer):
|
2022-03-25 05:02:39 +00:00
|
|
|
"""Dropout layer of 1D parallelism.
|
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
|
|
|
"""
|
2022-02-14 03:15:02 +00:00
|
|
|
|
2021-12-29 15:32:10 +00:00
|
|
|
def __init__(self, p: float = 0.5, inplace: bool = False):
|
|
|
|
super().__init__()
|
|
|
|
self.parallel_input = get_parallel_input()
|
|
|
|
self.p = p
|
|
|
|
self.inplace = inplace
|
|
|
|
|
|
|
|
def forward(self, input_: Tensor) -> Tensor:
|
2022-02-14 03:15:02 +00:00
|
|
|
if self.parallel_input:
|
|
|
|
with seed(ParallelMode.TENSOR):
|
|
|
|
output = F.dropout(input_, self.p, self.training, self.inplace)
|
|
|
|
else:
|
2021-12-29 15:32:10 +00:00
|
|
|
output = F.dropout(input_, self.p, self.training, self.inplace)
|
|
|
|
return output
|
2022-04-01 08:51:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
@LAYERS.register_module
|
|
|
|
class PatchEmbedding1D(ColossalaiModule):
|
|
|
|
"""
|
|
|
|
2D Image to Patch Embedding
|
|
|
|
|
|
|
|
:param img_size: image size
|
|
|
|
:type img_size: int
|
|
|
|
:param patch_size: patch size
|
|
|
|
:type patch_size: int
|
|
|
|
:param in_chans: number of channels of input image
|
|
|
|
:type in_chans: int
|
|
|
|
:param embed_size: size of embedding
|
|
|
|
:type embed_size: int
|
|
|
|
:param dtype: The dtype of parameters, defaults to None
|
|
|
|
:type dtype: torch.dtype, optional
|
|
|
|
:param flatten: whether to flatten output tensor, defaults to True
|
|
|
|
:type flatten: bool, optional
|
|
|
|
:param weight_initializer: The intializer of weight, defaults to kaiming uniform initializer
|
|
|
|
:type weight_initializer: typing.Callable, optional
|
|
|
|
:param bias_initializer: The intializer of bias, defaults to xavier uniform initializer
|
|
|
|
:type bias_initializer: typing.Callable, optional
|
|
|
|
:param position_embed_initializer: The intializer of position embedding, defaults to zero
|
|
|
|
:type position_embed_initializer: typing.Callable, optional
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
img_size: int,
|
|
|
|
patch_size: int,
|
|
|
|
in_chans: int,
|
|
|
|
embed_size: int,
|
|
|
|
dtype: torch.dtype = None,
|
|
|
|
flatten: bool = True,
|
|
|
|
weight_initializer: Callable = init.kaiming_uniform_(a=math.sqrt(5)),
|
|
|
|
bias_initializer: Callable = init.xavier_uniform_(a=1, scale=1),
|
|
|
|
position_embed_initializer: Callable = init.zeros_()):
|
|
|
|
embed = VanillaPatchEmbedding(img_size,
|
|
|
|
patch_size,
|
|
|
|
in_chans,
|
|
|
|
embed_size,
|
|
|
|
dtype=dtype,
|
|
|
|
flatten=flatten,
|
|
|
|
weight_initializer=weight_initializer,
|
|
|
|
bias_initializer=bias_initializer,
|
|
|
|
position_embed_initializer=position_embed_initializer)
|
|
|
|
super().__init__(embed)
|
|
|
|
|
|
|
|
def _load_from_state_dict(self, state_dict, prefix, *args):
|
|
|
|
local_state = OrderedDict()
|
|
|
|
param_keys = [prefix + 'weight', prefix + 'bias', prefix + 'cls_token', prefix + 'pos_embed']
|
|
|
|
if gpc.get_local_rank(ParallelMode.TENSOR) == 0:
|
|
|
|
for key in param_keys:
|
|
|
|
param = state_dict.pop(key, None)
|
|
|
|
if param is not None:
|
|
|
|
local_state[key] = param
|
|
|
|
|
|
|
|
local_state = broadcast_state_dict(local_state, ParallelMode.PARALLEL_1D)
|
|
|
|
super()._load_from_state_dict(local_state, prefix, *args)
|
|
|
|
|
|
|
|
def _save_to_state_dict(self, destination, prefix, keep_vars):
|
|
|
|
if gpc.get_local_rank(ParallelMode.TENSOR) == 0:
|
|
|
|
super()._save_to_state_dict(destination, prefix, keep_vars)
|