def train(epoch):
    model.train()
    total_loss = 0
    total_aux_losses = 0
    start_time = time.time()
    ntokens = len(corpus.dictionary)
    hidden = model.init_hidden(args.batch_size)
    eff_history_mode = ((args.seq_len > args.horizon) and (not args.repack))
    if eff_history_mode:
        validseqlen = (args.seq_len - args.horizon)
        seq_len = args.seq_len
    else:
        validseqlen = args.horizon
        seq_len = args.horizon
    for (batch, i) in enumerate(range(0, (train_data.size(0) - 1), validseqlen)):
        eff_history = (args.horizon if eff_history_mode else 0)
        if ((i + eff_history) >= (train_data.size(0) - 1)):
            continue
        (data, targets) = get_batch(train_data, i, seq_len)
        if args.repack:
            hidden = repackage_hidden(hidden)
        else:
            hidden = model.init_hidden(args.batch_size)
        optimizer.zero_grad()
        data = data.t()
        net = (nn.DataParallel(model) if (data.size(0) > 10) else model)
        ((_, _, decoded), hidden, all_decoded) = net(data, hidden)
        decoded = decoded.transpose(0, 1)
        targets = targets[eff_history:].contiguous().view((- 1))
        final_decoded = decoded[eff_history:].contiguous().view((- 1), ntokens)
        raw_loss = criterion(final_decoded, targets)
        aux_losses = 0
        if (args.aux > 0):
            all_decoded = all_decoded[:, :, eff_history:].permute(1, 2, 0, 3).contiguous()
            aux_size = all_decoded.size(0)
            all_decoded = all_decoded.view(aux_size, (- 1), ntokens)
            aux_losses = (args.aux * sum([criterion(all_decoded[i], targets) for i in range(aux_size)]))
        loss = (raw_loss + aux_losses)
        loss.backward()
        if (args.clip > 0):
            torch.nn.utils.clip_grad_norm_(model.parameters(), args.clip)
        optimizer.step()
        total_loss += raw_loss.data
        if args.aux:
            total_aux_losses += aux_losses.data
        if (((batch % args.log_interval) == 0) and (batch > 0)):
            cur_loss = (total_loss.item() / args.log_interval)
            cur_aux_loss = ((total_aux_losses.item() / args.log_interval) if args.aux else 0)
            elapsed = (time.time() - start_time)
            print('| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.5f} | ms/batch {:5.2f} | raw_loss {:5.3f} | aux_loss {:5.2f} | bpc {:5.3f}'.format(epoch, batch, (len(train_data) // validseqlen), lr, ((elapsed * 1000) / args.log_interval), cur_loss, cur_aux_loss, (cur_loss / math.log(2))))
            total_loss = 0
            total_aux_losses = 0
            start_time = time.time()
            sys.stdout.flush()
    decoded = None
    targets = None
    final_decoded = None
    all_decoded = None
def train(epoch):
    model.train()
    total_loss = 0
    total_aux_losses = 0
    start_time = time.time()
    ntokens = len(corpus.dictionary)
    hidden = model.init_hidden(args.batch_size)
    eff_history_mode = ((args.seq_len > args.horizon) and (not args.repack))
    if eff_history_mode:
        validseqlen = (args.seq_len - args.horizon)
        seq_len = args.seq_len
    else:
        validseqlen = args.horizon
        seq_len = args.horizon
    for (batch, i) in enumerate(range(0, (train_data.size(0) - 1), validseqlen)):
        eff_history = (args.horizon if eff_history_mode else 0)
        if ((i + eff_history) >= (train_data.size(0) - 1)):
            continue
        (data, targets) = get_batch(train_data, i, seq_len)
        if args.repack:
            hidden = repackage_hidden(hidden)
        else:
            hidden = model.init_hidden(args.batch_size)
        optimizer.zero_grad()
        data = data.t()
        net = (nn.DataParallel(model) if (data.size(0) > 10) else model)
        ((_, _, decoded), hidden, all_decoded) = net(data, hidden)
        decoded = decoded.transpose(0, 1)
        targets = targets[eff_history:].contiguous().view((- 1))
        final_decoded = decoded[eff_history:].contiguous().view((- 1), ntokens)
        raw_loss = criterion(final_decoded, targets)
        aux_losses = 0
        if (args.aux > 0):
            all_decoded = all_decoded[:, :, eff_history:].permute(1, 2, 0, 3).contiguous()
            aux_size = all_decoded.size(0)
            all_decoded = all_decoded.view(aux_size, (- 1), ntokens)
            aux_losses = (args.aux * sum([criterion(all_decoded[i], targets) for i in range(aux_size)]))
        loss = (raw_loss + aux_losses)
        loss.backward()
        if (args.clip > 0):
            torch.nn.utils.clip_grad_norm_(model.parameters(), args.clip)
        optimizer.step()
        total_loss += raw_loss.data
        if args.aux:
            total_aux_losses += aux_losses.data
        if (((batch % args.log_interval) == 0) and (batch > 0)):
            cur_loss = (total_loss.item() / args.log_interval)
            cur_aux_loss = ((total_aux_losses.item() / args.log_interval) if args.aux else 0)
            elapsed = (time.time() - start_time)
            print('| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.5f} | ms/batch {:5.2f} | raw_loss {:5.3f} | aux_loss {:5.2f} | bpc {:5.3f}'.format(epoch, batch, (len(train_data) // validseqlen), lr, ((elapsed * 1000) / args.log_interval), cur_loss, cur_aux_loss, (cur_loss / math.log(2))))
            total_loss = 0
            total_aux_losses = 0
            start_time = time.time()
            sys.stdout.flush()
    decoded = None
    targets = None
    final_decoded = None
    all_decoded = None