# coding: utf-8
# Source: https://raw.githubusercontent.com/pytorch/pytorch/master/torch/nn/modules/transformer.py
import math
import numpy as np
import torch
import copy

from torch.autograd import Variable
from torch.nn import functional as F, Embedding, DataParallel
from torch.nn.modules.module import Module
from torch.nn.modules.activation import MultiheadAttention
from torch.nn.modules.container import ModuleList
from torch.nn.init import xavier_uniform_
from torch.nn.modules.dropout import Dropout
from torch.nn.modules.linear import Linear
from torch.nn.modules.normalization import LayerNorm


class ParallelTransformer(DataParallel):

    def __init__(self, module, device_ids=None, output_device=None, dim=0):
        super(ParallelTransformer, self).__init__(module, device_ids, output_device, dim)

    def __getattr__(self, item):
        # print(self.__dict__.keys())
        if item != 'module':
            return getattr(self.module, item)
        else:
            return self.__dict__['_modules']['module']


class Transformer(Module):
    r"""A transformer model. User is able to modify the attributes as needed. The architechture
    is based on the paper "Attention Is All You Need". Ashish Vaswani, Noam Shazeer,
    Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Lukasz Kaiser, and
    Illia Polosukhin. 2017. Attention is all you need. In Advances in Neural Information
    Processing Systems, pages 6000-6010.

    Args:
        d_model: the number of expected features in the encoder/decoder inputs (default=512).
        nhead: the number of heads in the multiheadattention models (default=8).
        num_encoder_layers: the number of sub-encoder-layers in the encoder (default=6).
        num_decoder_layers: the number of sub-decoder-layers in the decoder (default=6).
        dim_feedforward: the dimension of the feedforward network model (default=2048).
        dropout: the dropout value (default=0.1).
        custom_encoder: custom encoder (default=None).
        custom_decoder: custom decoder (default=None).

    Examples::
        >>> transformer_model = nn.Transformer(src_vocab, tgt_vocab)
        >>> transformer_model = nn.Transformer(src_vocab, tgt_vocab, nhead=16, num_encoder_layers=12)
    """

    def __init__(self, vocab_size, field, d_model=512, nhead=8, num_encoder_layers=6,
                 num_decoder_layers=6, dim_feedforward=2048, dropout=0.1,
                 custom_encoder=None, custom_decoder=None, model_type='base'):
        super(Transformer, self).__init__()

        self.src_embed = TransformerEmbedding(vocab_size, d_model, dropout)
        self.tgt_embed = TransformerEmbedding(vocab_size, d_model, dropout)

        if custom_encoder is not None:
            self.encoder = custom_encoder
        else:
            encoder_layer = TransformerEncoderLayer(d_model, nhead, dim_feedforward, dropout)
            encoder_norm = LayerNorm(d_model)
            self.encoder = TransformerEncoder(encoder_layer, num_encoder_layers, encoder_norm)

        if custom_decoder is not None:
            self.decoder = custom_decoder
        else:
            decoder_layer = TransformerDecoderLayer(d_model, nhead, dim_feedforward, dropout)
            decoder_norm = LayerNorm(d_model)
            self.decoder = TransformerDecoder(decoder_layer, num_decoder_layers, decoder_norm)

        if model_type == "base":
            self.generator = Generator(vocab_size, d_model)
        elif model_type == "base-pg":
            self.generator = CopyGenerator(vocab_size, d_model, field.vocab.itos, field.vocab.stoi['<pad>'])

        self._reset_parameters()

        self.d_model = d_model
        self.nhead = nhead

    def forward(self, src, tgt, src_mask=None, tgt_mask=None,
                memory_mask=None, src_key_padding_mask=None,
                tgt_key_padding_mask=None, memory_key_padding_mask=None):
        r"""Take in and process masked source/target sequences.

        Args:
            src: the sequence to the encoder (required).
            tgt: the sequence to the decoder (required).
            src_mask: the additive mask for the src sequence (optional).
            tgt_mask: the additive mask for the tgt sequence (optional).
            memory_mask: the additive mask for the encoder output (optional).
            src_key_padding_mask: the ByteTensor mask for src keys per batch (optional).
            tgt_key_padding_mask: the ByteTensor mask for tgt keys per batch (optional).
            memory_key_padding_mask: the ByteTensor mask for memory keys per batch (optional).

        Shape:
            - src: :math:`(S, N, E)`.
            - tgt: :math:`(T, N, E)`.
            - src_mask: :math:`(S, S)`.
            - tgt_mask: :math:`(T, T)`.
            - memory_mask: :math:`(T, S)`.
            - src_key_padding_mask: :math:`(N, S)`.
            - tgt_key_padding_mask: :math:`(N, T)`.
            - memory_key_padding_mask: :math:`(N, S)`.

            Note: [src/tgt/memory]_mask should be filled with
            float('-inf') for the masked positions and float(0.0) else. These masks
            ensure that predictions for position i depend only on the unmasked positions
            j and are applied identically for each sequence in a batch.
            [src/tgt/memory]_key_padding_mask should be a ByteTensor where True values are positions
            that should be masked with float('-inf') and False values will be unchanged.
            This mask ensures that no information will be taken from position i if
            it is masked, and has a separate mask for each sequence in a batch.

            - output: :math:`(T, N, E)`.

            Note: Due to the multi-head attention architecture in the transformer model,
            the output sequence length of a transformer is same as the input sequence
            (i.e. target) length of the decode.

            where S is the source sequence length, T is the target sequence length, N is the
            batch size, E is the feature number

        Examples:
            >>> output = transformer_model(src, tgt, src_mask=src_mask, tgt_mask=tgt_mask)
        """

        memory = self.encode(src, src_mask, src_key_padding_mask)
        output = self.decode(src, tgt, memory, tgt_mask=tgt_mask, memory_mask=memory_mask,
                             tgt_key_padding_mask=tgt_key_padding_mask,
                             memory_key_padding_mask=memory_key_padding_mask)

        return output

    def encode(self, src, src_mask=None, src_key_padding_mask=None):

        if src_key_padding_mask is not None:
            src_key_padding_mask = src_key_padding_mask.transpose(0, 1)
        src = self.src_embed(src)

        return self.encoder(src, mask=src_mask, src_key_padding_mask=src_key_padding_mask)

    def decode(self, src, tgt, memory, tgt_mask=None, memory_mask=None, tgt_key_padding_mask=None,
               memory_key_padding_mask=None):
        if tgt_key_padding_mask is not None:
            tgt_key_padding_mask = tgt_key_padding_mask.transpose(0, 1)
        if tgt_mask is not None:
            tgt_mask = tgt_mask[:, 0, ...]
        tgt = self.tgt_embed(tgt)
        output, attns = self.decoder(tgt, memory, tgt_mask=tgt_mask, memory_mask=memory_mask,
                                     tgt_key_padding_mask=tgt_key_padding_mask,
                                     memory_key_padding_mask=memory_key_padding_mask)

        return self.generator(src, output, attns[-1], memory)

    @staticmethod
    def generate_square_subsequent_mask(sz):
        r"""Generate a square mask for the sequence. The masked positions are filled with float('-inf').
            Unmasked positions are filled with float(0.0).
        """
        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 _reset_parameters(self):
        r"""Initiate parameters in the transformer model."""

        for p in self.parameters():
            if p.dim() > 1:
                xavier_uniform_(p)


class TransformerEncoder(Module):
    r"""TransformerEncoder is a stack of N encoder layers

    Args:
        encoder_layer: an instance of the TransformerEncoderLayer() class (required).
        num_layers: the number of sub-encoder-layers in the encoder (required).
        norm: the layer normalization component (optional).

    Examples::
        >>> encoder_layer = nn.TransformerEncoderLayer(d_model, nhead)
        >>> transformer_encoder = nn.TransformerEncoder(encoder_layer, num_layers)
    """

    def __init__(self, encoder_layer, num_layers, norm=None):
        super(TransformerEncoder, self).__init__()
        self.layers = _get_clones(encoder_layer, num_layers)
        self.num_layers = num_layers
        self.norm = norm

    def forward(self, src, mask=None, src_key_padding_mask=None):
        r"""Pass the input through the endocder layers in turn.

        Args:
            src: the sequnce to the encoder (required).
            mask: the mask for the src sequence (optional).
            src_key_padding_mask: the mask for the src keys per batch (optional).

        Shape:
            see the docs in Transformer class.
        """
        output = src

        for i in range(self.num_layers):
            output = self.layers[i](output, src_mask=mask,
                                    src_key_padding_mask=src_key_padding_mask)

        if self.norm:
            output = self.norm(output)

        return output


class TransformerDecoder(Module):
    r"""TransformerDecoder is a stack of N decoder layers

    Args:
        decoder_layer: an instance of the TransformerDecoderLayer() class (required).
        num_layers: the number of sub-decoder-layers in the decoder (required).
        norm: the layer normalization component (optional).

    Examples::
        >>> decoder_layer = nn.TransformerDecoderLayer(d_model, nhead)
        >>> transformer_decoder = nn.TransformerDecoder(decoder_layer, num_layers)
    """

    def __init__(self, decoder_layer, num_layers, norm=None):
        super(TransformerDecoder, self).__init__()
        self.layers = _get_clones(decoder_layer, num_layers)
        self.num_layers = num_layers
        self.norm = norm

    def forward(self, tgt, memory, tgt_mask=None,
                memory_mask=None, tgt_key_padding_mask=None,
                memory_key_padding_mask=None):
        r"""Pass the inputs (and mask) through the decoder layer in turn.

        Args:
            tgt: the sequence to the decoder (required).
            memory: the sequnce from the last layer of the encoder (required).
            tgt_mask: the mask for the tgt sequence (optional).
            memory_mask: the mask for the memory sequence (optional).
            tgt_key_padding_mask: the mask for the tgt keys per batch (optional).
            memory_key_padding_mask: the mask for the memory keys per batch (optional).

        Shape:
            see the docs in Transformer class.
        """
        output = tgt
        attns = []
        for i in range(self.num_layers):
            output, attn = self.layers[i](output, memory, tgt_mask=tgt_mask,
                                          memory_mask=memory_mask,
                                          tgt_key_padding_mask=tgt_key_padding_mask,
                                          memory_key_padding_mask=memory_key_padding_mask)
            attns.append(attn)
        if self.norm:
            output = self.norm(output)

        return output, attns


class TransformerEncoderLayer(Module):
    r"""TransformerEncoderLayer is made up of self-attn and feedforward network.
    This standard encoder layer is based on the paper "Attention Is All You Need".
    Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez,
    Lukasz Kaiser, and Illia Polosukhin. 2017. Attention is all you need. In Advances in
    Neural Information Processing Systems, pages 6000-6010. Users may modify or implement
    in a different way during application.

    Args:
        d_model: the number of expected features in the input (required).
        nhead: the number of heads in the multiheadattention models (required).
        dim_feedforward: the dimension of the feedforward network model (default=2048).
        dropout: the dropout value (default=0.1).

    Examples::
        >>> encoder_layer = nn.TransformerEncoderLayer(d_model, nhead)
    """

    def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1):
        super(TransformerEncoderLayer, self).__init__()
        self.self_attn = MultiheadAttention(d_model, nhead, dropout=dropout)
        # Implementation of Feedforward model
        self.linear1 = Linear(d_model, dim_feedforward)
        self.dropout = Dropout(dropout)
        self.linear2 = Linear(dim_feedforward, d_model)

        self.norm1 = LayerNorm(d_model)
        self.norm2 = LayerNorm(d_model)
        self.dropout1 = Dropout(dropout)
        self.dropout2 = Dropout(dropout)

    def forward(self, src, src_mask=None, src_key_padding_mask=None):
        r"""Pass the input through the endocder layer.

        Args:
            src: the sequnce to the encoder layer (required).
            src_mask: the mask for the src sequence (optional).
            src_key_padding_mask: the mask for the src keys per batch (optional).

        Shape:
            see the docs in Transformer class.
        """
        src2 = self.self_attn(src, src, src, attn_mask=src_mask,
                              key_padding_mask=src_key_padding_mask)[0]
        src = src + self.dropout1(src2)
        src = self.norm1(src)
        src2 = self.linear2(self.dropout(F.relu(self.linear1(src))))
        src = src + self.dropout2(src2)
        src = self.norm2(src)
        return src


class TransformerDecoderLayer(Module):
    r"""TransformerDecoderLayer is made up of self-attn, multi-head-attn and feedforward network.
    This standard decoder layer is based on the paper "Attention Is All You Need".
    Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez,
    Lukasz Kaiser, and Illia Polosukhin. 2017. Attention is all you need. In Advances in
    Neural Information Processing Systems, pages 6000-6010. Users may modify or implement
    in a different way during application.

    Args:
        d_model: the number of expected features in the input (required).
        nhead: the number of heads in the multiheadattention models (required).
        dim_feedforward: the dimension of the feedforward network model (default=2048).
        dropout: the dropout value (default=0.1).

    Examples::
        >>> decoder_layer = nn.TransformerDecoderLayer(d_model, nhead)
    """

    def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1):
        super(TransformerDecoderLayer, self).__init__()
        self.self_attn = MultiheadAttention(d_model, nhead, dropout=dropout)
        self.multihead_attn = MultiheadAttention(d_model, nhead, dropout=dropout)
        # Implementation of Feedforward model
        self.linear1 = Linear(d_model, dim_feedforward)
        self.dropout = Dropout(dropout)
        self.linear2 = Linear(dim_feedforward, d_model)

        self.norm1 = LayerNorm(d_model)
        self.norm2 = LayerNorm(d_model)
        self.norm3 = LayerNorm(d_model)
        self.dropout1 = Dropout(dropout)
        self.dropout2 = Dropout(dropout)
        self.dropout3 = Dropout(dropout)

    def forward(self, tgt, memory, tgt_mask=None, memory_mask=None,
                tgt_key_padding_mask=None, memory_key_padding_mask=None):
        r"""Pass the inputs (and mask) through the decoder layer.

        Args:
            tgt: the sequence to the decoder layer (required).
            memory: the sequnce from the last layer of the encoder (required).
            tgt_mask: the mask for the tgt sequence (optional).
            memory_mask: the mask for the memory sequence (optional).
            tgt_key_padding_mask: the mask for the tgt keys per batch (optional).
            memory_key_padding_mask: the mask for the memory keys per batch (optional).

        Shape:
            see the docs in Transformer class.
        """
        tgt2 = self.self_attn(tgt, tgt, tgt, attn_mask=tgt_mask,
                              key_padding_mask=tgt_key_padding_mask)[0]
        tgt = tgt + self.dropout1(tgt2)
        tgt = self.norm1(tgt)
        tgt2, attn = self.multihead_attn(tgt, memory, memory, attn_mask=memory_mask,
                                         key_padding_mask=memory_key_padding_mask)
        tgt = tgt + self.dropout2(tgt2)
        tgt = self.norm2(tgt)
        tgt2 = self.linear2(self.dropout(F.relu(self.linear1(tgt))))
        tgt = tgt + self.dropout3(tgt2)
        tgt = self.norm3(tgt)
        return tgt, attn


class TransformerEmbedding(Module):

    def __init__(self, vocab_size, d_model, dropout=0.1):
        super(TransformerEmbedding, self).__init__()
        self.embed = Embedding(vocab_size, d_model)
        self.d_model = d_model
        self.pos = PositionalEncoding(d_model, dropout)

    def forward(self, x):
        return self.pos(self.embed(x))


class PositionalEncoding(Module):

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

        pe = torch.zeros(max_len, d_model)
        position = torch.arange(0., max_len).unsqueeze(1)
        div_term = torch.exp(torch.arange(0., d_model, 2) * -(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(1)
        self.register_buffer('pe', pe)

    # noinspection PyArgumentList
    def forward(self, x):
        x = x + Variable(self.pe[:x.size(0)], requires_grad=False)
        return self.dropout(x)


class SpanPositionalEncoding():

    def __init__(self, itos, padding_idx):
        self.itos = itos
        self.padding_idx = padding_idx

    def __call__(self, x):
        # print(x.shape)
        seq_len, batch_size = x.shape
        pos_enc = torch.zeros_like(x)

        for b in range(batch_size):
            for i in range(seq_len):
                if '▁' in self.itos[x[i, b].item()] or i == 0:
                    pos_enc[i, b] = 4
                else:
                    if i > 0 and x[i, b].item() != self.padding_idx:
                        pos_enc[i, b] = 1 + pos_enc[i-1, b]
                    else:
                        pos_enc[i, b] = x[i, b]
        return pos_enc

    @staticmethod
    def decay(pos, prev, curr, decay_rate=0.1):
        """Copy rate decaying for current step.

        Arguments:
            pos {[type]} -- [S, B]
            prev {[type]} -- copy rate for last step, [T-1, B, S]
            curr {[type]} -- copy rate for current step,  [T, B, S]

        Keyword Arguments:
            decay_rate {float} -- [description] (default: {0.1})

        Returns:
            [type] -- new copy rate for current step, [T, B, S]
        """
        steps = curr.size(0)
        residual = torch.zeros_like(curr)  # [T, B, S]
        mask = torch.zeros_like(curr)  # [T, B, S]

        residual[1:, ..., 1:] += prev[..., :-1] * decay_rate  # [T, B, S]
        # Only if the current step is within the same span of the last step.
        flag = (pos[1:] > pos[:-1]).float()  # [S-1, B]
        mask[-1:, ..., 1:] += flag.transpose(0, 1).unsqueeze(0).repeat([1, 1, 1])
        new = residual + (1 - decay_rate) * curr
        ans = torch.where(mask.bool(), new, curr)
        return torch.softmax(ans, dim=-1)


class CopyGenerator(Module):

    def __init__(self, vocab_size, d_model, itos, padding_idx):
        super(CopyGenerator, self).__init__()
        self.vocab_size = vocab_size
        self.gen_proj = Linear(d_model, vocab_size)
        self.prob_proj = Linear(d_model, 1)

        self.pos_encoder = SpanPositionalEncoding(itos, padding_idx=padding_idx)

    def forward(self, src_full, decode_output, decode_attn, memory):
        """
        Generate final vocab distribution.

        :param src_full: [S, B]
        :param decode_attn: [B, T, S]
        :param decode_output: [T, B, H]
        :param memory: [S, B, H]
        :return:
        """
        # decode_attn = torch.sum(decode_attn, dim=1)  # [B, T, S]
        assert decode_attn.size(0) == decode_output.size(1)
        assert decode_output.size(1) == memory.size(1)
        batch_size, steps, seq = decode_attn.size()

        src_full = src_full.transpose(0, 1).unsqueeze(0).repeat([steps, 1, 1])          # [T, B, S]

        context = torch.matmul(decode_attn, memory.transpose(0, 1)).transpose(0, 1)     # [T, B, H]
        prob = torch.sigmoid(self.prob_proj(context))                                   # [T, B, H] -> [T, B 1]
        gen_logits = prob * torch.softmax(self.gen_proj(decode_output), dim=-1)                 # [T, B, V]
        copy_logits = torch.zeros_like(gen_logits)                                      # [T, B, V]
        copy_logits = copy_logits.scatter_add(2, src_full, decode_attn.transpose(0, 1))  # [T, B, V]
        copy_logits = torch.softmax(copy_logits, dim=-1)                           # [T, B, V]

        pos = self.pos_encoder(src_full)

        for step in range(1, copy_logits.size(0)):
            copy_logits = pos_encoder.decay(pos, copy_logits[:step], curr[:step+1])

        copy_logits = (1 - prob) * copy_logits

        return torch.log(gen_logits + copy_logits)


class Generator(Module):

    def __init__(self, vocab_size, d_model):
        super(Generator, self).__init__()
        self.vocab_size = vocab_size
        self.gen_proj = Linear(d_model, vocab_size)

    def forward(self, src_full, decode_output, decode_attn, memory):
        """
        Generate final vocab distribution.
        :param decode_output: [T, B, H]
        :return:
        """

        gen_logits = torch.log_softmax(self.gen_proj(decode_output), dim=-1)                 # [T, B, V]
        return gen_logits


def _get_clones(module, N):
    return ModuleList([copy.deepcopy(module) for i in range(N)])


if __name__ == "__main__":

    itos = {
        0: '<unk>',
        1: '<pad>',
        2: '<bos>',
        3: '<eos>',
        4: '▁he',
        5: 'llo',
        6: '▁',
        7: 'world',
        8: '!'
    }

    pos_encoder = SpanPositionalEncoding(itos, padding_idx=1)
    t = np.random.randint(0, 9, 2).reshape(2, 1)  # [S, B]
    d = [[itos[e] for e in x] for x in t.T]  # [B, S]
    pos = pos_encoder(torch.from_numpy(t))  # [S, B]
    # for text, ids in zip(d, pos.T):
    # print(text)
    # print(ids)
    print(pos.transpose(0, 1))
    prev = torch.softmax(torch.rand(2).reshape(1, 1, 2), dim=-1)
    curr = torch.softmax(torch.rand(4).reshape(2, 1, 2), dim=-1)

    print(prev)
    print(curr)

    new = pos_encoder.decay(pos, prev, curr)

    # print(pos)

    print(new)
    # print(torch.softmax(new, dim=-1))
