"examples/pytorch/hilander/__init__.py" did not exist on "15a2c22c98416304689e90601230843ac4bece6e"
main.py 10.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# coding: utf-8
import argparse
import time
import math
import torch
import torch.nn as nn
import data
import model

try:
    from apex.fp16_utils import *
except ImportError:
    raise ImportError("Please install apex from https://www.github.com/nvidia/apex to run this example.")

parser = argparse.ArgumentParser(description='PyTorch Wikitext-2 RNN/LSTM Language Model')
parser.add_argument('--data', type=str, default='./data/wikitext-2',
                    help='location of the data corpus')
parser.add_argument('--model', type=str, default='LSTM',
                    help='type of recurrent net (RNN_TANH, RNN_RELU, LSTM, GRU)')
20
parser.add_argument('--emsize', type=int, default=1504,
21
                    help='size of word embeddings')
22
parser.add_argument('--nhid', type=int, default=1504,
23
24
25
26
27
28
29
30
31
                    help='number of hidden units per layer')
parser.add_argument('--nlayers', type=int, default=2,
                    help='number of layers')
parser.add_argument('--lr', type=float, default=20,
                    help='initial learning rate')
parser.add_argument('--clip', type=float, default=0.25,
                    help='gradient clipping')
parser.add_argument('--epochs', type=int, default=40,
                    help='upper epoch limit')
32
parser.add_argument('--batch_size', type=int, default=24, metavar='N',
33
34
35
                    help='batch size')
parser.add_argument('--bptt', type=int, default=35,
                    help='sequence length')
36
parser.add_argument('--dropout', type=float, default=0.65,
37
38
39
40
41
42
43
44
45
46
47
48
49
                    help='dropout applied to layers (0 = no dropout)')
parser.add_argument('--tied', action='store_true',
                    help='tie the word embedding and softmax weights')
parser.add_argument('--seed', type=int, default=1111,
                    help='random seed')
parser.add_argument('--cuda', action='store_true',
                    help='use CUDA')
parser.add_argument('--log-interval', type=int, default=200, metavar='N',
                    help='report interval')
parser.add_argument('--save', type=str,  default='model.pt',
                    help='path to save the final model')
parser.add_argument('--fp16', action='store_true',
                    help='Run model in pseudo-fp16 mode (fp16 storage fp32 math).')
50
parser.add_argument('--static-loss-scale', type=float, default=128.0,
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
                    help='Static loss scale, positive power of 2 values can improve fp16 convergence.')

args = parser.parse_args()

# Set the random seed manually for reproducibility.
torch.manual_seed(args.seed)
if torch.cuda.is_available():
    if not args.cuda:
        print("WARNING: You have a CUDA device, so you should probably run with --cuda")
if args.fp16 and not args.cuda:
    print("WARNING: --fp16 requires --cuda, ignoring --fp16 option")

###############################################################################
# Load data
###############################################################################

67
68
69
# Ensure that the dictionary length is a multiple of 8,
# so that the decoder's GEMMs will use Tensor Cores.
corpus = data.Corpus(args.data, pad_to_multiple_of=8)
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

# Starting from sequential data, batchify arranges the dataset into columns.
# For instance, with the alphabet as the sequence and batch size 4, we'd get
# ┌ a g m s ┐
# │ b h n t │
# │ c i o u │
# │ d j p v │
# │ e k q w │
# └ f l r x ┘.
# These columns are treated as independent by the model, which means that the
# dependence of e. g. 'g' on 'f' can not be learned, but allows more efficient
# batch processing.

def batchify(data, bsz):
    # Work out how cleanly we can divide the dataset into bsz parts.
    nbatch = data.size(0) // bsz
    # Trim off any extra elements that wouldn't cleanly fit (remainders).
    data = data.narrow(0, 0, nbatch * bsz)
    # Evenly divide the data across the bsz batches.
    data = data.view(bsz, -1).t().contiguous()
    if args.cuda:
        data = data.cuda()
    return data

eval_batch_size = 10
train_data = batchify(corpus.train, args.batch_size)
val_data = batchify(corpus.valid, eval_batch_size)
test_data = batchify(corpus.test, eval_batch_size)

###############################################################################
# Build the model
###############################################################################

ntokens = len(corpus.dictionary)
104
105
106
107
108
109
110
111
112
113

if args.fp16 and args.cuda:
    if ntokens%8 != 0:
        print("Warning: the dictionary size (ntokens = {}) should be a multiple of 8 to ensure "
              "Tensor Core use for the decoder's GEMMs.".format(ntokens))
    if args.emsize%8 != 0 or args.nhid%8 != 0 or args.batch_size%8 != 0:
        print("Warning: emsize = {}, nhid = {}, batch_size = {} should all be multiples of 8 "
              "to ensure Tensor Core use for the RNN's GEMMs.".format(
              args.emsize, args.nhid, args.batch_size))

114
115
116
117
118
119
120
model = model.RNNModel(args.model, ntokens, args.emsize, args.nhid, args.nlayers, args.dropout, args.tied)

if args.cuda and args.fp16:
    model.type(torch.cuda.HalfTensor)
    model_params, master_params = prep_param_lists(model)
elif args.cuda:
    model.cuda()
121
122
123
124
125
126

if (not args.fp16) or (not args.cuda):
    print("Warning:  static_loss_scale != 1.0 is only necessary with --fp16. "
          "Resetting static_loss_scale to 1.0")
    args.static_loss_scale = 1.0

127
128
129
130
131
132
133
134
criterion = nn.CrossEntropyLoss()

###############################################################################
# Training code
###############################################################################


def repackage_hidden(h):
ptrblck's avatar
ptrblck committed
135
    """Detaches hidden states from their history."""
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    if torch.is_tensor(h):
        return h.detach()
    else:
        return tuple(repackage_hidden(v) for v in h)


# get_batch subdivides the source data into chunks of length args.bptt.
# If source is equal to the example output of the batchify function, with
# a bptt-limit of 2, we'd get the following two Variables for i = 0:
# ┌ a g m s ┐ ┌ b h n t ┐
# └ b h n t ┘ └ c i o u ┘
# Note that despite the name of the function, the subdivison of data is not
# done along the batch dimension (i.e. dimension 1), since that was handled
# by the batchify function. The chunks are along dimension 0, corresponding
# to the seq_len dimension in the LSTM.

152
def get_batch(source, i):
153
    seq_len = min(args.bptt, len(source) - 1 - i)
ptrblck's avatar
ptrblck committed
154
155
    data = source[i:i+seq_len]
    target = source[i+1:i+1+seq_len].view(-1)
156
157
158
159
160
161
162
163
164
    return data, target


def evaluate(data_source):
    # Turn on evaluation mode which disables dropout.
    model.eval()
    total_loss = 0
    ntokens = len(corpus.dictionary)
    hidden = model.init_hidden(eval_batch_size)
165
166
167
168
169
170
171
172
    with torch.no_grad():
        for i in range(0, data_source.size(0) - 1, args.bptt):
            data, targets = get_batch(data_source, i)
            output, hidden = model(data, hidden)
            output_flat = output.view(-1, ntokens)
            #total loss can overflow if accumulated in fp16.
            total_loss += len(data) * criterion(output_flat, targets).data.float()
            hidden = repackage_hidden(hidden)
173
    return to_python_float(total_loss) / len(data_source)
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192


def train():
    # Turn on training mode which enables dropout.
    model.train()
    total_loss = 0
    start_time = time.time()
    ntokens = len(corpus.dictionary)
    hidden = model.init_hidden(args.batch_size)
    for batch, i in enumerate(range(0, train_data.size(0) - 1, args.bptt)):
        data, targets = get_batch(train_data, i)
        # Starting each batch, we detach the hidden state from how it was previously produced.
        # If we didn't, the model would try backpropagating all the way to start of the dataset.
        hidden = repackage_hidden(hidden)
        model.zero_grad()
        output, hidden = model(data, hidden)
        loss = criterion(output.view(-1, ntokens), targets)
        loss = loss * args.static_loss_scale
        loss.backward()
193
        loss.data = loss.data / args.static_loss_scale
194
195
196

        if args.fp16 and args.cuda:
            model_grads_to_master_grads(model_params, master_params)
197
198
199
200
            if args.static_loss_scale != 1:
                for param in master_params:
                    param.grad.data = param.grad.data/args.static_loss_scale
            clip_grad_norm(master_params, args.clip)
201
            for param in master_params:
202
                param.data = param.data - param.grad.data * lr
203
204
            master_params_to_model_params(model_params, master_params)
        else:
205
            clip_grad_norm(model.parameters(), args.clip)
206
            for p in model.parameters():
207
                p.data.add_(-lr, p.grad.data)
208
209
210
211

        total_loss += loss.data

        if batch % args.log_interval == 0 and batch > 0:
212
            cur_loss = to_python_float(total_loss) / args.log_interval
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
            elapsed = time.time() - start_time
            print('| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.2f} | ms/batch {:5.2f} | '
                  'loss {:5.2f} | ppl {:8.2f}'.format(
                      epoch, batch, len(train_data) // args.bptt, lr,
                      elapsed * 1000 / args.log_interval, cur_loss, math.exp(min(cur_loss, 20))))
            total_loss = 0
            start_time = time.time()


# Loop over epochs.
lr = args.lr
best_val_loss = None

# At any point you can hit Ctrl + C to break out of training early.
try:
    for epoch in range(1, args.epochs+1):
        epoch_start_time = time.time()
        train()
        val_loss = evaluate(val_data)
        print('-' * 89)
        print('| end of epoch {:3d} | time: {:5.2f}s | valid loss {:5.2f} | '
              'valid ppl {:8.2f}'.format(epoch, (time.time() - epoch_start_time),
                                         val_loss, math.exp(min(val_loss, 20))))
        print('-' * 89)
        # Save the model if the validation loss is the best we've seen so far.
        if not best_val_loss or val_loss < best_val_loss:
            with open(args.save, 'wb') as f:
                torch.save(model, f)
            best_val_loss = val_loss
        else:
            # Anneal the learning rate if no improvement has been seen in the validation dataset.
            lr /= 4.0
except KeyboardInterrupt:
    print('-' * 89)
    print('Exiting from training early')

# Load the best saved model.
with open(args.save, 'rb') as f:
    model = torch.load(f)

# Run on test data.
test_loss = evaluate(test_data)
print('=' * 89)
print('| End of training | test loss {:5.2f} | test ppl {:8.2f}'.format(
    test_loss, math.exp(test_loss)))
print('=' * 89)