2023-05-22 07:02:17 +00:00
|
|
|
# part of code modified from https://github.com/tunib-ai/parallelformers
|
|
|
|
|
2023-05-24 02:26:46 +00:00
|
|
|
from dataclasses import dataclass, field
|
|
|
|
from typing import Any, Callable, Dict, List, Tuple, Type
|
|
|
|
|
2023-05-22 07:02:17 +00:00
|
|
|
import torch
|
|
|
|
import torch.nn as nn
|
|
|
|
from transformers import AutoConfig
|
2023-05-24 02:26:46 +00:00
|
|
|
|
2023-05-22 07:02:17 +00:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Argument:
|
2023-05-24 02:26:46 +00:00
|
|
|
r"""
|
|
|
|
The argument class for the policy
|
|
|
|
|
|
|
|
Args:
|
|
|
|
attr_dict (Dict[str, Any]): The dict for the param setting
|
|
|
|
param_funcs (:class:`List[Callable]`): The list for the param functions
|
|
|
|
"""
|
|
|
|
attr_dict: Dict[str, Any]
|
|
|
|
param_funcs: List[Callable]
|
|
|
|
|
2023-05-22 07:02:17 +00:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Layer:
|
2023-05-24 02:26:46 +00:00
|
|
|
r"""
|
2023-05-22 07:02:17 +00:00
|
|
|
The layer object for the policy
|
|
|
|
|
|
|
|
Args:
|
2023-05-24 02:26:46 +00:00
|
|
|
weight (str): The weight suffix of the layer
|
|
|
|
bias (str): The bias suffix of the layer
|
|
|
|
replace_layer (:class:`colosalai.nn`): The layer to replace the original layer
|
|
|
|
ignore (bool): Whether to ignore this layer if it is not in the model
|
2023-05-22 07:02:17 +00:00
|
|
|
"""
|
|
|
|
weight: str = None
|
|
|
|
bias: str = None
|
|
|
|
replace_layer: Any = None
|
|
|
|
ignore: bool = False
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Col_Layer(Layer):
|
2023-05-24 02:26:46 +00:00
|
|
|
r"""
|
2023-05-22 07:02:17 +00:00
|
|
|
Class for col shard layer in MegatronLM
|
2023-05-24 02:26:46 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
gather_output (bool): Whether to gather the output of the layer
|
2023-05-22 07:02:17 +00:00
|
|
|
"""
|
|
|
|
gather_output: bool = False
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Row_Layer(Layer):
|
2023-05-24 02:26:46 +00:00
|
|
|
r"""
|
2023-05-22 07:02:17 +00:00
|
|
|
Class for col shard layer in MegatronLM
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Policy():
|
2023-05-24 02:26:46 +00:00
|
|
|
r"""
|
2023-05-22 07:02:17 +00:00
|
|
|
The base class for all the policies
|
2023-05-24 02:26:46 +00:00
|
|
|
For each different model, it should have a different policy class, like BertPolicy for Bert Model
|
|
|
|
or OPTPolicy for OPT model.
|
2023-05-22 07:02:17 +00:00
|
|
|
AutoPolicy:
|
2023-05-24 02:26:46 +00:00
|
|
|
Shardformer already defined some policies for huggingface model, just set ``custom_policy`` = None
|
2023-05-22 07:02:17 +00:00
|
|
|
to use the auto policy. In shardformer autopolicy, we define a base policy for one type model,
|
2023-05-24 02:26:46 +00:00
|
|
|
like BertPolicy, and for each different Bert modle in huggingface like, BertForMaskedLM,
|
2023-05-22 07:02:17 +00:00
|
|
|
BertForSequenceClassification, etc., for each different Bert model we difine different policy class
|
2023-05-24 02:26:46 +00:00
|
|
|
and overwrite the method like ``inject_policy`` to modify the forward and backward process.
|
|
|
|
|
2023-05-22 07:02:17 +00:00
|
|
|
CustomPolicy:
|
2023-05-24 02:26:46 +00:00
|
|
|
If you want to define your own policy, you can set ``custom_policy`` = CustomPolicy, and overwrite
|
|
|
|
all the methods in ``Policy`` class. You can refer to any policy we defined like the ``BertPolicy``
|
|
|
|
class for the example.
|
|
|
|
|
2023-05-22 07:02:17 +00:00
|
|
|
"""
|
2023-05-24 02:26:46 +00:00
|
|
|
|
2023-05-22 07:02:17 +00:00
|
|
|
@staticmethod
|
2023-05-24 02:26:46 +00:00
|
|
|
def argument_policy(model_config, shard_config: int) -> Dict[nn.Module, Argument]:
|
|
|
|
r"""
|
|
|
|
Return the dict for the modify policy, the key is the original layer class and the value is the
|
|
|
|
argument for the modify layer
|
2023-05-22 07:02:17 +00:00
|
|
|
|
|
|
|
Args:
|
2023-05-24 02:26:46 +00:00
|
|
|
model_config (:class:`tansformer.Config`): The config of transformer model
|
|
|
|
shard_config (:class:`ShardConfig`): The config for sharding model
|
|
|
|
|
2023-05-22 07:02:17 +00:00
|
|
|
Return:
|
|
|
|
Dict for the modify policy,
|
2023-05-24 02:26:46 +00:00
|
|
|
::
|
2023-05-22 07:02:17 +00:00
|
|
|
{
|
|
|
|
origin layer class1 (nn.Module): Argument(
|
|
|
|
attr_dict = {
|
|
|
|
argument1: value1,
|
|
|
|
argument2: value2,
|
|
|
|
...
|
|
|
|
},
|
|
|
|
param_funcs = [
|
|
|
|
staticmethod1,
|
|
|
|
staticmethod2,
|
|
|
|
...
|
|
|
|
]
|
|
|
|
),
|
|
|
|
origin layer class2 (nn.Module): Argument(
|
|
|
|
attr_dict = {
|
|
|
|
argument1: value1,
|
|
|
|
argument2: value2,
|
|
|
|
...
|
|
|
|
},
|
|
|
|
param_funcs = [
|
|
|
|
staticmethod1,
|
|
|
|
staticmethod2,
|
|
|
|
...
|
|
|
|
]
|
|
|
|
),
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def inject_policy() -> Tuple[nn.Module, nn.Module]:
|
2023-05-24 02:26:46 +00:00
|
|
|
r"""
|
|
|
|
Return the dict for the inject model
|
2023-05-22 07:02:17 +00:00
|
|
|
|
|
|
|
Return:
|
|
|
|
The injected model, key is the original model and value is the new shardmodel
|
2023-05-24 02:26:46 +00:00
|
|
|
::
|
|
|
|
(OrignModel, CustomModel)
|
|
|
|
in `CustomModel`, we can overwrite the forward and backward process
|
2023-05-22 07:02:17 +00:00
|
|
|
"""
|
|
|
|
return ()
|
|
|
|
|
|
|
|
@staticmethod
|
2023-05-24 02:26:46 +00:00
|
|
|
def binding_policy() -> Dict:
|
|
|
|
r"""
|
|
|
|
Return the dict for the binding model
|
|
|
|
|
|
|
|
Return:
|
|
|
|
This method should return the binding relationship for some layers share the weight or bias,
|
|
|
|
the key and value is the suffix of the weight or bias of the model
|
|
|
|
::
|
|
|
|
return {
|
|
|
|
"bert.embeddings.word_embeddings.weight": "cls.predictions.decoder.weight",
|
|
|
|
}
|
2023-05-22 07:02:17 +00:00
|
|
|
"""
|
2023-05-24 02:26:46 +00:00
|
|
|
return NotImplementedError
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def attn_in() -> List:
|
|
|
|
r"""
|
2023-05-22 07:02:17 +00:00
|
|
|
Attention qkv layer
|
2023-05-24 02:26:46 +00:00
|
|
|
In this kind of method, we should return the list of ``Layer`` object, each ``Layer`` object should be
|
|
|
|
``Layer`` for no slicing, ``Col_Layer`` for col slicing, ``Row_Layer`` for row slicing. And the parameters
|
|
|
|
in ``Layer`` object can refer to the ``Layer`` class.
|
2023-05-22 07:02:17 +00:00
|
|
|
|
|
|
|
Returns:
|
2023-05-24 02:26:46 +00:00
|
|
|
List[Layer]: List of layer object, each layer is the new
|
2023-05-22 07:02:17 +00:00
|
|
|
"""
|
|
|
|
return NotImplementedError
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def attn_out() -> List:
|
2023-05-24 02:26:46 +00:00
|
|
|
r"""
|
2023-05-22 07:02:17 +00:00
|
|
|
Attention output projection layer
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List[Layer]: List of layer object
|
|
|
|
"""
|
|
|
|
return NotImplementedError
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def mlp_in() -> List:
|
2023-05-24 02:26:46 +00:00
|
|
|
r"""
|
2023-05-22 07:02:17 +00:00
|
|
|
h -> 4h mlp layer
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List[Layer]: List of layer object
|
|
|
|
"""
|
|
|
|
return NotImplementedError
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def mlp_out() -> List:
|
2023-05-24 02:26:46 +00:00
|
|
|
r"""
|
2023-05-22 07:02:17 +00:00
|
|
|
4h -> h mlp layer
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List[Layer]: List of layer object
|
|
|
|
"""
|
|
|
|
return NotImplementedError
|
2023-05-24 02:26:46 +00:00
|
|
|
|
2023-05-22 07:02:17 +00:00
|
|
|
@staticmethod
|
2023-05-24 02:26:46 +00:00
|
|
|
def embedding() -> List:
|
|
|
|
r"""
|
2023-05-22 07:02:17 +00:00
|
|
|
Partially slice the embedding layer
|
|
|
|
|
|
|
|
Return:
|
|
|
|
List[Layer]: List of layer object
|
|
|
|
"""
|
|
|
|
return NotImplementedError
|
2023-05-24 02:26:46 +00:00
|
|
|
|
2023-05-22 07:02:17 +00:00
|
|
|
@staticmethod
|
2023-05-24 02:26:46 +00:00
|
|
|
def unembedding() -> List:
|
|
|
|
r"""
|
2023-05-22 07:02:17 +00:00
|
|
|
Partially slice the embedding layer
|
|
|
|
|
|
|
|
Return:
|
|
|
|
List[Layer]: List of layer object
|
|
|
|
"""
|
|
|
|
return NotImplementedError
|