Skip to content
Open
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
5 changes: 3 additions & 2 deletions data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import torch
import torch.distributed as dist
from torch.utils.data import TensorDataset
from tqdm import tqdm

class PretrainInputExample:
"""A single example for unsupervised pre-training.
Expand Down Expand Up @@ -53,7 +54,7 @@ def convert_examples_to_features(examples,

# Create features
features = []
for i, example in enumerate(examples):
for i, example in enumerate(tqdm(examples)):
tokens = tokenizer.tokenize(example.text)
tokens = [bos_token] + tokens[:args.max_seq_len-2] + [eos_token] # BOS, EOS
tokens += [pad_token] * (args.max_seq_len - len(tokens))
Expand Down Expand Up @@ -116,4 +117,4 @@ def create_examples(args, tokenizer, mode='train'):
all_label_ids = torch.tensor([feature.label_id for feature in features], dtype=torch.long)
dataset = TensorDataset(all_input_ids, all_label_ids)

return dataset
return dataset
8 changes: 4 additions & 4 deletions trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import torch.nn as nn
from torch.nn.parallel import DistributedDataParallel
from torch.utils.tensorboard import SummaryWriter
from radam import RAdam
import RAdam

from model import GPT, GPTLMHead, GPTClsHead

Expand Down Expand Up @@ -36,7 +36,7 @@ def __init__(self, args, train_loader, test_loader, tokenizer):
self.vocab_size = tokenizer.vocab_size
self.pad_id = tokenizer.pad_token_id
self.eos_id = tokenizer.eos_token_id
self.device = torch.device('cuda' if torch.cuda.is_available() and not args.no_cuda else 'cpu', args.local_rank)
self.device = torch.device('cuda' if torch.cuda.is_available() and not args.no_cuda else 'cpu', max(0, args.local_rank))
self.writer = SummaryWriter() if args.local_rank in [-1, 0] else None
self.n_gpus = torch.distributed.get_world_size() if args.distributed else torch.cuda.device_count()
assert args.pretrain != args.finetune # Do not set both finetune and pretrain arguments to the same (True, False)
Expand Down Expand Up @@ -101,7 +101,7 @@ def pretrain(self, epoch):

if self.args.local_rank in [-1, 0]:
self.writer.add_scalar('Loss/pre-train', loss.item(), ((epoch-1)*n_batches)+i)
if i % (n_batches//5) == 0 and i != 0:
if n_batches > 5 and i % (n_batches//5) == 0 and i != 0:
print('Iteration {} ({}/{})\tLoss: {:.4f}'.format(i, i, n_batches, losses/i))

print('Train Epoch {} [rank: {}]\t>\tLoss: {:.4f}'.format(epoch, self.args.local_rank, losses/n_batches))
Expand Down Expand Up @@ -134,7 +134,7 @@ def finetune(self, epoch):
if self.args.local_rank in [-1, 0]:
self.writer.add_scalar('Loss/fine-tune', loss.item(), ((epoch-1)*n_batches)+i)
self.writer.add_scalar('Accuracy/fine-tune', acc, ((epoch-1)*n_batches)+i)
if i % (n_batches//5) == 0 and i != 0:
if n_batches > 5 and i % (n_batches//5) == 0 and i != 0:
print('Iteration {} ({}/{})\tLoss: {:.4f} Acc: {:.1f}%'.format(i, i, n_batches, losses/i, accs/i*100.))

print('Train Epoch {} [rank: {}]\t>\tLoss: {:.4f} / Acc: {:.1f}%'.format(epoch, self.args.local_rank, losses/n_batches, accs/n_batches*100.))
Expand Down