2023-06-20 03:45:16 +00:00
|
|
|
from typing import Type, Union
|
2023-06-07 08:09:40 +00:00
|
|
|
|
|
|
|
import torch.nn as nn
|
|
|
|
from transformers.models.gpt2.modeling_gpt2 import GPT2Block, GPT2Model
|
|
|
|
|
2023-06-20 03:45:16 +00:00
|
|
|
import colossalai.shardformer.layer as col_nn
|
|
|
|
from colossalai.shardformer.layer.dropout import Dropout1D
|
2023-06-07 08:09:40 +00:00
|
|
|
|
2023-06-20 03:45:16 +00:00
|
|
|
from ..utils import getattr_, setattr_
|
|
|
|
from .basepolicy import ModulePolicyDescription, Policy, SubModuleReplacementDescription
|
2023-06-07 08:09:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GPT2Policy(Policy):
|
|
|
|
|
2023-06-20 03:45:16 +00:00
|
|
|
def preprocess(self):
|
|
|
|
# reshape the embedding layer
|
|
|
|
r"""
|
|
|
|
Reshape the Embedding layer to make the embedding dimension divisible by world_size
|
|
|
|
"""
|
|
|
|
vocab_size = self.model.config.vocab_size
|
|
|
|
world_size = self.shard_config.tensor_parallel_size
|
|
|
|
if vocab_size % world_size != 0:
|
|
|
|
new_vocab_size = vocab_size + world_size - vocab_size % world_size
|
|
|
|
self.model.resize_token_embeddings(new_vocab_size)
|
|
|
|
return self.model
|
|
|
|
|
|
|
|
def module_policy(self):
|
2023-06-07 08:09:40 +00:00
|
|
|
return {
|
|
|
|
GPT2Model:
|
2023-06-20 03:45:16 +00:00
|
|
|
ModulePolicyDescription(attribute_replacement={},
|
|
|
|
param_replacement=[],
|
|
|
|
sub_module_replacement=[
|
|
|
|
SubModuleReplacementDescription(
|
|
|
|
suffix="wte",
|
|
|
|
target_module=col_nn.VocabParallelEmbedding1D,
|
|
|
|
),
|
|
|
|
]),
|
2023-06-07 08:09:40 +00:00
|
|
|
GPT2Block:
|
2023-06-20 03:45:16 +00:00
|
|
|
ModulePolicyDescription(attribute_replacement={
|
|
|
|
"attn.embed_dim": self.model.config.hidden_size // self.shard_config.tensor_parallel_size,
|
|
|
|
"attn.split_size": self.model.config.hidden_size // self.shard_config.tensor_parallel_size,
|
|
|
|
"attn.num_heads": self.model.config.num_attention_heads // self.shard_config.tensor_parallel_size,
|
|
|
|
},
|
|
|
|
param_replacement=[],
|
|
|
|
sub_module_replacement=[
|
|
|
|
SubModuleReplacementDescription(
|
|
|
|
suffix="attn.c_attn",
|
|
|
|
target_module=col_nn.LinearConv1D_Col,
|
|
|
|
kwargs={
|
|
|
|
"n_cast": 3,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
SubModuleReplacementDescription(
|
|
|
|
suffix="attn.c_proj",
|
|
|
|
target_module=col_nn.LinearConv1D_Row,
|
|
|
|
kwargs={
|
|
|
|
"n_cast": 1,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
SubModuleReplacementDescription(
|
|
|
|
suffix="mlp.c_fc",
|
|
|
|
target_module=col_nn.LinearConv1D_Col,
|
|
|
|
kwargs={
|
|
|
|
"n_cast": 1,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
SubModuleReplacementDescription(
|
|
|
|
suffix="mlp.c_proj",
|
|
|
|
target_module=col_nn.LinearConv1D_Row,
|
|
|
|
kwargs={
|
|
|
|
"n_cast": 1,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
SubModuleReplacementDescription(
|
|
|
|
suffix="attn.attn_dropout",
|
|
|
|
target_module=col_nn.Dropout1D,
|
|
|
|
),
|
|
|
|
SubModuleReplacementDescription(
|
|
|
|
suffix="attn.resid_dropout",
|
|
|
|
target_module=col_nn.Dropout1D,
|
|
|
|
),
|
|
|
|
SubModuleReplacementDescription(
|
|
|
|
suffix="mlp.dropout",
|
|
|
|
target_module=col_nn.Dropout1D,
|
|
|
|
),
|
|
|
|
])
|
2023-06-07 08:09:40 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 03:45:16 +00:00
|
|
|
def new_model_class(self):
|
2023-06-07 08:09:40 +00:00
|
|
|
|
2023-06-20 03:45:16 +00:00
|
|
|
return self.model
|
2023-06-07 08:09:40 +00:00
|
|
|
|
2023-06-20 03:45:16 +00:00
|
|
|
def postprocess(self):
|
|
|
|
return self.model
|
2023-06-07 08:09:40 +00:00
|
|
|
|
|
|
|
|
2023-06-20 03:45:16 +00:00
|
|
|
# GPT2Model
|
|
|
|
class GPT2ModelPolicy(GPT2Policy):
|
2023-06-07 08:09:40 +00:00
|
|
|
|
2023-06-20 03:45:16 +00:00
|
|
|
def __init__(self) -> None:
|
|
|
|
super().__init__()
|