import math
import torch
import torch.nn as nn
import torch.nn.functional as F

class RNNModel(nn.Module):
    """Container module with an encoder, a recurrent module, and a decoder."""

    def __init__(self, rnn_type, ntoken, ninp, nhid, nlayers, dropout=0.5, tie_weights=False, encoder=None, decoder=None):
        super(RNNModel, self).__init__()
        self.ntoken = ntoken
        self.drop = nn.Dropout(dropout)

        if encoder is None:
            self.encoder = nn.Embedding(ntoken, ninp)
        else:
            self.encoder = encoder
        if rnn_type in ['LSTM', 'GRU']:
            self.rnn = getattr(nn, rnn_type)(ninp, nhid, nlayers, dropout=dropout)
        else:
            try:
                nonlinearity = {'RNN_TANH': 'tanh', 'RNN_RELU': 'relu'}[rnn_type]
            except KeyError:
                raise ValueError( """An invalid option for `--model` was supplied,
                                 options are ['LSTM', 'GRU', 'RNN_TANH' or 'RNN_RELU']""")
            self.rnn = nn.RNN(ninp, nhid, nlayers, nonlinearity=nonlinearity, dropout=dropout)
        if decoder is None:
            self.decoder = nn.Linear(nhid, ntoken)
        else:
            self.decoder = decoder

        # Optionally tie weights as in:
        # "Using the Output Embedding to Improve Language Models" (Press & Wolf 2016)
        # https://arxiv.org/abs/1608.05859
        # and
        # "Tying Word Vectors and Word Classifiers: A Loss Framework for Language Modeling" (Inan et al. 2016)
        # https://arxiv.org/abs/1611.01462
        if tie_weights:
            if nhid != ninp:
                raise ValueError('When using the tied flag, nhid must be equal to emsize')
            self.decoder.weight = self.encoder.weight

        self.rnn_type = rnn_type
        self.nhid = nhid
        self.nlayers = nlayers

    def init_weights(self):
        initrange = 0.1
        if self.encoder.__class__.__name__ == "Embedding":
            self.encoder.weight.data.uniform_(-initrange, initrange)
        else:
            self.encoder.init_weights()

        if self.decoder.__class__.__name__ == "Linear":
            self.decoder.bias.data.zero_()
            self.decoder.weight.data.uniform_(-initrange, initrange)
        else:
            self.decoder.init_weights()

    def forward(self, input, hidden):
        emb = self.drop(self.encoder(input))
        hidden_ = []
        for h in hidden:
            if isinstance(h, torch.LongTensor) or isinstance(h, torch.cuda.LongTensor):
                h = h.to(torch.float)
            hidden_.append(h)
        output, hidden = self.rnn(emb, tuple(hidden_))
        output = self.drop(output)
        decoded = self.decoder(output)
        decoded = decoded.view(-1, self.ntoken)
        return F.log_softmax(decoded, dim=1), hidden

    def init_hidden(self, bsz):
        weight = next(self.parameters())
        if self.rnn_type == 'LSTM':
            return (weight.new_zeros(self.nlayers, bsz, self.nhid),
                    weight.new_zeros(self.nlayers, bsz, self.nhid))
        else:
            return weight.new_zeros(self.nlayers, bsz, self.nhid)

# Temporarily leave PositionalEncoding module here. Will be moved somewhere else.
class PositionalEncoding(nn.Module):
    r"""Inject some information about the relative or absolute position of the tokens
        in the sequence. The positional encodings have the same dimension as
        the embeddings, so that the two can be summed. Here, we use sine and cosine
        functions of different frequencies.
    .. math::
        \text{PosEncoder}(pos, 2i) = sin(pos/10000^(2i/d_model))
        \text{PosEncoder}(pos, 2i+1) = cos(pos/10000^(2i/d_model))
        \text{where pos is the word position and i is the embed idx)
    Args:
        d_model: the embed dim (required).
        dropout: the dropout value (default=0.1).
        max_len: the max. length of the incoming sequence (default=5000).
    Examples:
        >>> pos_encoder = PositionalEncoding(d_model)
    """

    def __init__(self, d_model, dropout=0.1, max_len=5000):
        super(PositionalEncoding, self).__init__()
        self.dropout = nn.Dropout(p=dropout)

        pe = torch.zeros(max_len, d_model)
        position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
        div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        pe = pe.unsqueeze(0).transpose(0, 1)
        self.register_buffer('pe', pe)

    def forward(self, x):
        r"""Inputs of forward function
        Args:
            x: the sequence fed to the positional encoder model (required).
        Shape:
            x: [sequence length, batch size, embed dim]
            output: [sequence length, batch size, embed dim]
        Examples:
            >>> output = pos_encoder(x)
        """

        x = x + self.pe[:x.size(0), :]
        return self.dropout(x)

class TransformerModel(nn.Module):
    """Container module with an encoder, a recurrent or transformer module, and a decoder."""

    def __init__(self, ntoken, ninp, nhead, nhid, nlayers, dropout=0.5, encoder=None, decoder=None):
        super(TransformerModel, self).__init__()
        try:
            from torch.nn import TransformerEncoder, TransformerEncoderLayer
        except:
            raise ImportError('TransformerEncoder module does not exist in PyTorch 1.1 or lower.')
        self.model_type = 'Transformer'
        self.src_mask = None
        self.pos_encoder = PositionalEncoding(ninp, dropout)
        encoder_layers = TransformerEncoderLayer(ninp, nhead, nhid, dropout)
        self.transformer_encoder = TransformerEncoder(encoder_layers, nlayers)
        if encoder is None:
            self.encoder = nn.Embedding(ntoken, ninp)
        else:
            self.encoder = encoder
        self.ninp = ninp
        if decoder is None:
            self.decoder = nn.Linear(nhid, ntoken)
        else:
            self.decoder = decoder

    def _generate_square_subsequent_mask(self, sz):
        mask = (torch.triu(torch.ones(sz, sz)) == 1).transpose(0, 1)
        mask = mask.float().masked_fill(mask == 0, float('-inf')).masked_fill(mask == 1, float(0.0))
        return mask

    def init_weights(self):
        initrange = 0.1
        if self.encoder.__class__.__name__ == "Embedding":
            self.encoder.weight.data.uniform_(-initrange, initrange)
        else:
            self.encoder.init_weights()

        if self.decoder.__class__.__name__ == "Linear":
            self.decoder.bias.data.zero_()
            self.decoder.weight.data.uniform_(-initrange, initrange)
        else:
            self.decoder.init_weights()

    def forward(self, src, has_mask=True):
        if has_mask:
            device = src.device
            if self.src_mask is None or self.src_mask.size(0) != len(src):
                mask = self._generate_square_subsequent_mask(len(src)).to(device)
                self.src_mask = mask
        else:
            self.src_mask = None

        src = self.encoder(src) * math.sqrt(self.ninp)
        src = self.pos_encoder(src)
        output = self.transformer_encoder(src, self.src_mask)
        output = self.decoder(output)
        return F.log_softmax(output, dim=-1)

[((12, 20, 12, 39), 'torch.nn.Dropout', 'nn.Dropout', ({(12, 31, 12, 38): 'dropout'}, {}), '(dropout)', True, 'import torch.nn as nn\n'), ((101, 23, 101, 44), 'torch.nn.Dropout', 'nn.Dropout', (), '', True, 'import torch.nn as nn\n'), ((103, 13, 103, 42), 'torch.zeros', 'torch.zeros', ({(103, 25, 103, 32): 'max_len', (103, 34, 103, 41): 'd_model'}, {}), '(max_len, d_model)', False, 'import torch\n'), ((106, 22, 106, 52), 'torch.sin', 'torch.sin', ({(106, 32, 106, 51): 'position * div_term'}, {}), '(position * div_term)', False, 'import torch\n'), ((107, 22, 107, 52), 'torch.cos', 'torch.cos', ({(107, 32, 107, 51): 'position * div_term'}, {}), '(position * div_term)', False, 'import torch\n'), ((137, 25, 137, 76), 'torch.nn.TransformerEncoderLayer', 'TransformerEncoderLayer', ({(137, 49, 137, 53): 'ninp', (137, 55, 137, 60): 'nhead', (137, 62, 137, 66): 'nhid', (137, 68, 137, 75): 'dropout'}, {}), '(ninp, nhead, nhid, dropout)', False, 'from torch.nn import TransformerEncoder, TransformerEncoderLayer\n'), ((138, 35, 138, 78), 'torch.nn.TransformerEncoder', 'TransformerEncoder', ({(138, 54, 138, 68): 'encoder_layers', (138, 70, 138, 77): 'nlayers'}, {}), '(encoder_layers, nlayers)', False, 'from torch.nn import TransformerEncoder, TransformerEncoderLayer\n'), ((180, 15, 180, 44), 'torch.nn.functional.log_softmax', 'F.log_softmax', (), '', True, 'import torch.nn.functional as F\n'), ((15, 27, 15, 53), 'torch.nn.Embedding', 'nn.Embedding', ({(15, 40, 15, 46): 'ntoken', (15, 48, 15, 52): 'ninp'}, {}), '(ntoken, ninp)', True, 'import torch.nn as nn\n'), ((26, 23, 26, 94), 'torch.nn.RNN', 'nn.RNN', (), '', True, 'import torch.nn as nn\n'), ((28, 27, 28, 50), 'torch.nn.Linear', 'nn.Linear', ({(28, 37, 28, 41): 'nhid', (28, 43, 28, 49): 'ntoken'}, {}), '(nhid, ntoken)', True, 'import torch.nn as nn\n'), ((71, 15, 71, 44), 'torch.nn.functional.log_softmax', 'F.log_softmax', (), '', True, 'import torch.nn.functional as F\n'), ((140, 27, 140, 53), 'torch.nn.Embedding', 'nn.Embedding', ({(140, 40, 140, 46): 'ntoken', (140, 48, 140, 52): 'ninp'}, {}), '(ntoken, ninp)', True, 'import torch.nn as nn\n'), ((145, 27, 145, 50), 'torch.nn.Linear', 'nn.Linear', ({(145, 37, 145, 41): 'nhid', (145, 43, 145, 49): 'ntoken'}, {}), '(nhid, ntoken)', True, 'import torch.nn as nn\n'), ((176, 34, 176, 54), 'math.sqrt', 'math.sqrt', ({(176, 44, 176, 53): 'self.ninp'}, {}), '(self.ninp)', False, 'import math\n'), ((104, 19, 104, 62), 'torch.arange', 'torch.arange', (), '', False, 'import torch\n'), ((105, 29, 105, 56), 'torch.arange', 'torch.arange', ({(105, 42, 105, 43): '0', (105, 45, 105, 52): 'd_model', (105, 54, 105, 55): '2'}, {}), '(0, d_model, 2)', False, 'import torch\n'), ((105, 69, 105, 86), 'math.log', 'math.log', ({(105, 78, 105, 85): '10000.0'}, {}), '(10000.0)', False, 'import math\n'), ((150, 27, 150, 45), 'torch.ones', 'torch.ones', ({(150, 38, 150, 40): 'sz', (150, 42, 150, 44): 'sz'}, {}), '(sz, sz)', False, 'import torch\n')]