Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean mt5 #419

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions dev/model_loader_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ export TEST_OUTPUT=output_unittest
export ONEFLOW_TEST_DEVICE_NUM=4
export ONEFLOW_EP_CUDA_ENABLE_TF32_EXECUTION=0

python3 -m oneflow.distributed.launch --nproc_per_node 4 -m pytest -s --disable-warnings tests/model_utils/test_bert_loader.py
# python3 -m oneflow.distributed.launch --nproc_per_node 4 -m pytest -s --disable-warnings tests/model_utils/test_bert_loader.py

python3 -m oneflow.distributed.launch --nproc_per_node 4 -m pytest -s --disable-warnings tests/model_utils/test_roberta_loader.py
# python3 -m oneflow.distributed.launch --nproc_per_node 4 -m pytest -s --disable-warnings tests/model_utils/test_roberta_loader.py

python3 -m oneflow.distributed.launch --nproc_per_node 4 -m pytest -s --disable-warnings tests/model_utils/test_gpt_loader.py
# python3 -m oneflow.distributed.launch --nproc_per_node 4 -m pytest -s --disable-warnings tests/model_utils/test_gpt_loader.py

python3 -m oneflow.distributed.launch --nproc_per_node 4 -m pytest -s --disable-warnings tests/model_utils/test_mt5_loader.py
# python3 -m oneflow.distributed.launch --nproc_per_node 4 -m pytest -s --disable-warnings tests/model_utils/test_mt5_loader.py

python3 -m oneflow.distributed.launch --nproc_per_node 4 -m pytest -s --disable-warnings tests/model_utils/test_t5_loader.py
# python3 -m oneflow.distributed.launch --nproc_per_node 4 -m pytest -s --disable-warnings tests/model_utils/test_t5_loader.py

python3 -m oneflow.distributed.launch --nproc_per_node 4 -m pytest -s --disable-warnings tests/model_utils/test_swin_loader.py
# python3 -m oneflow.distributed.launch --nproc_per_node 4 -m pytest -s --disable-warnings tests/model_utils/test_swin_loader.py

python3 -m oneflow.distributed.launch --nproc_per_node 4 -m pytest -s --disable-warnings tests/model_utils/test_swinv2_loader.py
# python3 -m oneflow.distributed.launch --nproc_per_node 4 -m pytest -s --disable-warnings tests/model_utils/test_swinv2_loader.py

python3 -m oneflow.distributed.launch --nproc_per_node 4 -m pytest -s --disable-warnings tests/model_utils/test_vit_loader.py
# python3 -m oneflow.distributed.launch --nproc_per_node 4 -m pytest -s --disable-warnings tests/model_utils/test_vit_loader.py

python3 -m oneflow.distributed.launch --nproc_per_node 1 -m pytest -s --disable-warnings tests/model_utils/test_mt5_loader_2.py

rm -rf $TEST_OUTPUT
13 changes: 8 additions & 5 deletions projects/T5/configs/mt5_pretrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
train_data_path = "projects/T5/data/training_data/part_0"
pretrained_model_path = None

micro_batch_size = 64
micro_batch_size = 4
optim["lr"] = 1e-4

# dataloader
Expand All @@ -30,7 +30,7 @@
)
],
collate_fn=collate_fn(
vocab_size=12902,
vocab_size=12900,
max_seq_length=512,
noise_density=0.15,
mean_noise_span_length=3,
Expand All @@ -43,7 +43,7 @@
model = LazyCall(T5ForPreTraining)(cfg=cfg)

# model config
model.cfg.vocab_size = 12902
model.cfg.vocab_size = 12900
model.cfg.hidden_size = 512
model.cfg.hidden_layers = 8
model.cfg.num_attention_heads = 6
Expand All @@ -53,7 +53,8 @@
model.cfg.attention_probs_dropout_prob = 0.0
model.cfg.embedding_dropout_prob = 0.0
model.cfg.layernorm_eps = 1e-6
model.cfg.model_type = "mt5"

# model.cfg.model_type = "mt5"
model.cfg.pretrained_model_path = pretrained_model_path

train.update(
Expand All @@ -63,7 +64,7 @@
train_epoch=1,
train_iter=24000,
log_period=10,
amp=dict(enabled=False),
amp=dict(enabled=True),
warmup_ratio=1 / 24,
# checkpointer=dict(period=10, max_to_keep=20),
dist=dict(
Expand All @@ -89,3 +90,5 @@

train.zero_optimization.enabled = True
train.zero_optimization.stage = 2
train.activation_checkpoint.enabled = False
train.num_accumulation_steps = 8
2 changes: 1 addition & 1 deletion projects/T5/configs/t5_model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
initializer_range=0.02,
layernorm_eps=1e-5,
amp_enabled=False,
model_type="t5",
# model_type="t5",
)

cfg = DictConfig(cfg)
68 changes: 24 additions & 44 deletions projects/T5/models/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,13 @@ def forward(
use_cache (bool, optional): it will be set to True, when the model is in the inference
phase and used for incremental decoding. Defaults to False.
"""

# hidden_states, encoder_states: [S(0), B]
# attention_mask: [S(0), B]

if encoder_states is not None:
encoder_states = encoder_states.to_global(placement=hidden_states.placement)

if attention_mask is not None:
attention_mask = attention_mask.to_global(placement=hidden_states.placement)

bsz, real_seq_length = hidden_states.size()[:2]
real_seq_length, bsz = hidden_states.size()[:2]

if past_key_value is not None:
assert (
Expand All @@ -166,47 +162,39 @@ def forward(
f"Got {len(past_key_value)} past states.\n"
real_seq_length += past_key_value[0].shape[2] if query_length is None else query_length

key_length = real_seq_length if encoder_states is None else encoder_states.shape[1]

key_length = real_seq_length if encoder_states is None else encoder_states.shape[0]
if self.is_cross_attention:
# if it is cross attention, key and value should be calculated only once, and the
# result can be reused.
query = self.query(hidden_states)
query = query.view(bsz, -1, self.num_heads, self.head_size)
query = query.permute(0, 2, 1, 3)
query = query.view(-1, bsz, self.num_heads, self.head_size)
query = query.permute(1, 2, 0, 3) # bsz, num_head, seq_len, head_size

if past_key_value is not None:
key, value = past_key_value
elif encoder_states is not None:
key_value = self.key_value(encoder_states)
key_value = key_value.view(bsz, -1, self.num_heads, 2 * self.head_size)
key_value = key_value.permute(0, 2, 1, 3)
key_value = key_value.view(-1, bsz, self.num_heads, 2 * self.head_size)
key_value = key_value.permute(1, 2, 0, 3)
key, value = flow.chunk(key_value, chunks=2, dim=-1)
else:
raise ValueError(
"past_key_value and encoder_states cannot be None at the same time."
)
else:
# if it is self attention, query, key, and value are all obtained from hidden_states.
# when in the inference phase of an incremental decoder,
# hidden_states is the last-added state,
# the full key and value could be obtained by concatenating with past_key_value.
query_key_value = self.query_key_value(hidden_states)
query_key_value = query_key_value.view(bsz, -1, self.num_heads, 3 * self.head_size)
query_key_value = query_key_value.permute(
0, 2, 1, 3
) # [bsz, num_heads, src_len, 3 * head_size]
query, key, value = flow.chunk(query_key_value, chunks=3, dim=-1)
attention_scores, value = flow._C.fused_self_attention(
query_key_value, head_size=self.head_size, alpha=1
)
if past_key_value is not None:
past_key, past_value = past_key_value
key = flow.cat((past_key.type_as(key), key), dim=2)
value = flow.cat((past_value.type_as(value), value), dim=2)

# query, key, value: [S(0), S(1)], shape: [bsz, num_heads, seq_length, head_size]
if use_cache:
past_key_value = (key, value)

# [bsz, num_heads, tgt_len, src_len] with [S(0), S(1)]
attention_scores = flow.matmul(query, key, transpose_b=True)
if self.is_cross_attention:
attention_scores = flow.matmul(query, key, transpose_b=True, alpha=1)

if position_bias is None:
if not self.has_relative_attention_bias:
Expand All @@ -228,30 +216,23 @@ def forward(

attention_scores = attention_scores + position_bias

# [S(0), S(1)] x [S(0), B] = [S(0), S(1)]
if attention_mask is not None:
attention_scores = flow.mul(attention_scores, attention_mask)
attention_scores = attention_scores - 10000.0 * (1 - attention_mask)
# TODO(xingyu.liao): graph will occur `where_scalar` errors
# when using `masked_fill`
# attention_scores = attention_scores.masked_fill(1 - attention_mask, -10000.0)
attention_weights = flow.softmax(attention_scores, dim=-1)
# [bsz, num_heads, tgt_len, src_len]
attention_weights = self.dropout(attention_weights)
attention_mask = attention_mask.expand_as(attention_scores) if use_cache else attention_mask
attention_weights = flow._C.fused_scale_mask_softmax_dropout(
attention_scores,
attention_mask,
fill_value=-10000.0,
scale=1,
p=self.attention_dropout_prob,
)[0]
else:
attention_weights = flow.softmax(attention_scores, dim=-1)
# [bsz, num_heads, tgt_len, src_len]
attention_weights = self.dropout(attention_weights)

# Context shape: [bsz, num_heads, tgt_len, head_size] with [S(0), S(1)]
context = flow.matmul(attention_weights, value)
# Change shape: [bsz, num_heads, tgt_len, head_size] -> [bsz, tgt_len, num_heads, head_size]
context = context.transpose(1, 2)

# Concat multi-head results from
# [bsz, tgt_len, num_heads, head_size] -> [bsz, tgt_len, num_heads * head_size]
# SBP sign: [S(0), S(2)]
# [S(0), S(2)] x [B, S(0)] = [S(0), P] -> [S(0), B]
context = flow._C.transpose(context, perm=(2, 0, 1, 3))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

关于今天提到的这里有个transpose操作的原因:

  • attention_scores, value = flow._C.fused_self_attention(...),这个fused_self_attention返回的tensor的shape是attention_scores:[batch_size, num_head, seq_len1, seq_len2]以及value:[bsz, num_head, seq_len, head_size],所以这里0维就是batch_size
  • 然后这里的context是由context = flow.matmul(attention_weights, value)得到,所以context的shape是 [bsz, num_head, seq_len, head_size],所以最后需要有这个transpose的操作,没改之前也有transpose的操作:context = context.transpose(1, 2),只不过现在交换了3个维度( context = flow._C.transpose(context, perm=(2, 0, 1, 3)))


output = self.dense(context.flatten(2))

output = self.output_dropout(output)
Expand All @@ -272,7 +253,6 @@ def extra_repr(self) -> str:
def _relative_position_bucket(
self, relative_position, bidirectional=True, num_buckets=32, max_distance=128
):
# relative_position: (seq_len, seq_len)
relative_buckets = 0
if bidirectional:
num_buckets //= 2
Expand Down Expand Up @@ -347,4 +327,4 @@ def compute_bias(self, query_length, key_length, placement=None):
values = values.permute([2, 0, 1]).unsqueeze(
0
) # shape (1, num_heads, query_length, key_length)
return values
return values
6 changes: 0 additions & 6 deletions projects/T5/models/embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,9 @@ def __init__(
)
)
self.init_method(self.weight)
# FIXME(lxy): Fill padding_idx is not supported in nd_sbp right now.
# self._fill_padding_idx_with_zero()

def forward(self, input_ids):
weight = flow._C.amp_white_identity(self.weight) if self.amp_enabled else self.weight
# embeddings with sbp sign: [B, B]
# [B, B] x [S(0), B] --> [S(0), B]
# ↑ ↑ ↑
# embed pos_ids pos_embed
input_embeds = flow._C.gather(weight, input_ids, axis=0)
return input_embeds

Expand Down
50 changes: 25 additions & 25 deletions projects/T5/models/logits.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,32 @@


class LMLogits(nn.Module):
def __init__(self, vocab_size, hidden_size=None, bias=False, model_type="t5", layer_idx=-1):
def __init__(self, vocab_size, hidden_size=None, bias=False, layer_idx=-1):
super().__init__()
self.model_type = model_type
if model_type == "t5":
self.bias = (
nn.Parameter(
flow.zeros(
(vocab_size,),
dtype=flow.float32,
placement=dist.get_layer_placement(layer_idx),
sbp=dist.get_nd_sbp([flow.sbp.broadcast, flow.sbp.split(0)]),
)
)
if bias
else None
)
elif model_type == "mt5":
self.linear = Linear(hidden_size, vocab_size, bias=False, layer_idx=layer_idx)
# self.model_type = model_type
# if model_type == "t5":
# self.bias = (
# nn.Parameter(
# flow.zeros(
# (vocab_size,),
# dtype=flow.float32,
# placement=dist.get_layer_placement(layer_idx),
# sbp=dist.get_nd_sbp([flow.sbp.broadcast, flow.sbp.split(0)]),
# )
# )
# if bias
# else None
# )
# elif model_type == "mt5":
self.linear = Linear(hidden_size, vocab_size, bias=False, layer_idx=layer_idx)

def forward(self, input, word_embeddings=None):
if self.model_type == "t5":
w = word_embeddings.to_global(placement=input.placement)
input = input.to_global(grad_sbp=input.sbp)
logits = flow._C.matmul(input, w, transpose_b=True)
if self.bias is not None:
logits = logits + self.bias
else:
logits = self.linear(input)
# if self.model_type == "t5":
# w = word_embeddings.to_global(placement=input.placement)
# input = input.to_global(grad_sbp=input.sbp)
# logits = flow._C.matmul(input, w, transpose_b=True)
# if self.bias is not None:
# logits = logits + self.bias
# else:
logits = self.linear(input)
return logits
Loading