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

Bugfix for BERT & GPT2 #406

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion NLP/BERT/utils/lamb_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import Callable, Dict, Iterator, List, Union, Tuple

import oneflow as flow
from oneflow.nn.optimizer.optimizer import Optimizer, ParamGroup
from oneflow.optim import Optimizer
from oneflow.nn.parameter import Parameter


Expand Down
17 changes: 8 additions & 9 deletions NLP/BERT/utils/lr_scheduler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import math
import oneflow as flow
from oneflow.nn.optimizer.lr_scheduler import LrScheduler
from oneflow.optim.lr_scheduler import _LRScheduler as LrScheduler


class PolynomialLR(LrScheduler):
Expand Down Expand Up @@ -50,19 +50,18 @@ def __init__(
self.cycle = cycle
super().__init__(optimizer, last_step, verbose)

def get_lr(self):
def get_lr(self, base_lr, step):
decay_batch = self.max_decay_steps
cur_batch = self.last_step
cur_batch = step
if self.cycle:
if cur_batch == 0:
cur_batch = 1
decay_batch = decay_batch * math.ceil(cur_batch / decay_batch)
else:
cur_batch = min(cur_batch, decay_batch)
return [
(base_lr - self.end_learning_rate)
* ((1 - cur_batch / decay_batch) ** (self.power))
+ self.end_learning_rate
for base_lr in self.base_lrs
]

factor = (1 - cur_batch / decay_batch) ** (self.power)
return (base_lr - self.end_learning_rate) * factor + self.end_learning_rate

def _generate_conf_for_graph(self, opt_confs):
# CosineDecayLR is the same as CosineDecayConf in nn.Graph
Expand Down
6 changes: 3 additions & 3 deletions NLP/GPT2/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def _merge_heads(self, tensor, num_heads, attn_head_size):
bsz, seq_len = tensor.size()[:-2]
# new_shape = tensor.size()[:-2] + (num_heads * attn_head_size,)
new_shape = (bsz, seq_len, num_heads * attn_head_size)
return tensor.view(*new_shape)
return tensor.reshape(*new_shape)

def forward(self, hidden_states, layer_past=None, use_cache=False):
hidden_states = self.c_attn(hidden_states)
Expand Down Expand Up @@ -356,8 +356,8 @@ def forward(

# Flatten the tokens
loss_fct = nn.CrossEntropyLoss()
shift_logits = shift_logits.view(-1, shift_logits.size(-1))
shift_labels = shift_labels.view(-1)
shift_logits = shift_logits.reshape(-1, shift_logits.size(-1))
shift_labels = shift_labels.reshape(-1)
loss = loss_fct(shift_logits, shift_labels)

output = (lm_logits,) + transformer_outputs[1:]
Expand Down
2 changes: 1 addition & 1 deletion NLP/GPT2/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def train_single_epoch(self, data_loader, epoch):
"step": step,
"avg_loss": losses.avg,
"loss": losses.val,
"lr": self.lr_scheduler.get_lr()[0],
"lr": self.lr_scheduler.get_last_lr()[0],
}
data_iter.set_postfix(logging)

Expand Down