def compute_loss(batch, output, loss_func):
    """Computes the loss of a given batch

    Args:
        batch (dict): The current batch
        output (tuple): Tuple of tensors (output of `TransformerModel`)
        loss_func (:obj:`nn.modules._Loss`): The loss function

    Returns:
        (:obj:`torch.Tensor`, int): The computed loss and the total number of tokens in batch
    """
    output = output[0]
    T, B = output.size(0), output.size(1)
    target = batch["target"]

    loss = loss_func(output.view(T * B, -1), target.contiguous().view(-1))
    return loss, batch["ntokens"]