# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import math
from typing import Dict, Optional, Tuple

import torch
import torch.nn.functional as F
from fairseq import utils
from fairseq.incremental_decoding_utils import with_incremental_state
from fairseq.modules.fairseq_dropout import FairseqDropout
from fairseq.modules.quant_noise import quant_noise
from torch import Tensor, nn
from torch.nn import Parameter

@with_incremental_state
class MultiheadAttention(nn.Module):
    """Multi-headed attention.

    See "Attention Is All You Need" for more details.
    """

    def __init__(
        self,
        embed_dim,
        num_heads,
        kdim=None,
        vdim=None,
        dropout=0.0,
        bias=True,
        add_bias_kv=False,
        add_zero_attn=False,
        self_attention=False,
        encoder_decoder_attention=False,
        q_noise=0.0,
        qn_block_size=8,
    ):
        super().__init__()
        self.embed_dim = embed_dim
        self.kdim = kdim if kdim is not None else embed_dim
        self.vdim = vdim if vdim is not None else embed_dim
        self.qkv_same_dim = self.kdim == embed_dim and self.vdim == embed_dim

        self.num_heads = num_heads
        self.dropout_module = FairseqDropout(
            dropout, module_name=self.__class__.__name__
        )

        self.head_dim = embed_dim // num_heads
        assert (
            self.head_dim * num_heads == self.embed_dim
        ), "embed_dim must be divisible by num_heads"
        self.scaling = self.head_dim ** -0.5

        self.self_attention = self_attention
        self.encoder_decoder_attention = encoder_decoder_attention

        assert not self.self_attention or self.qkv_same_dim, (
            "Self-attention requires query, key and " "value to be of the same size"
        )

        self.k_proj = quant_noise(
            nn.Linear(self.kdim, embed_dim, bias=bias), q_noise, qn_block_size
        )
        self.v_proj = quant_noise(
            nn.Linear(self.vdim, embed_dim, bias=bias), q_noise, qn_block_size
        )
        self.q_proj = quant_noise(
            nn.Linear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size
        )

        self.out_proj = quant_noise(
            nn.Linear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size
        )

        if add_bias_kv:
            self.bias_k = Parameter(torch.Tensor(1, 1, embed_dim))
            self.bias_v = Parameter(torch.Tensor(1, 1, embed_dim))
        else:
            self.bias_k = self.bias_v = None

        self.add_zero_attn = add_zero_attn

        self.reset_parameters()

        self.onnx_trace = False

    def prepare_for_onnx_export_(self):
        self.onnx_trace = True

    def reset_parameters(self):
        if self.qkv_same_dim:
            # Empirically observed the convergence to be much better with
            # the scaled initialization
            nn.init.xavier_uniform_(self.k_proj.weight, gain=1 / math.sqrt(2))
            nn.init.xavier_uniform_(self.v_proj.weight, gain=1 / math.sqrt(2))
            nn.init.xavier_uniform_(self.q_proj.weight, gain=1 / math.sqrt(2))
        else:
            nn.init.xavier_uniform_(self.k_proj.weight)
            nn.init.xavier_uniform_(self.v_proj.weight)
            nn.init.xavier_uniform_(self.q_proj.weight)

        nn.init.xavier_uniform_(self.out_proj.weight)
        if self.out_proj.bias is not None:
            nn.init.constant_(self.out_proj.bias, 0.0)
        if self.bias_k is not None:
            nn.init.xavier_normal_(self.bias_k)
        if self.bias_v is not None:
            nn.init.xavier_normal_(self.bias_v)

    def forward(
        self,
        query,
        key: Optional[Tensor],
        value: Optional[Tensor],
        key_padding_mask: Optional[Tensor] = None,
        incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]] = None,
        need_weights: bool = True,
        static_kv: bool = False,
        attn_mask: Optional[Tensor] = None,
        before_softmax: bool = False,
        need_head_weights: bool = False,
    ) -> Tuple[Tensor, Optional[Tensor]]:
        """Input shape: Time x Batch x Channel

        Args:
            key_padding_mask (ByteTensor, optional): mask to exclude
                keys that are pads, of shape `(batch, src_len)`, where
                padding elements are indicated by 1s.
            need_weights (bool, optional): return the attention weights,
                averaged over heads (default: False).
            attn_mask (ByteTensor, optional): typically used to
                implement causal attention, where the mask prevents the
                attention from looking forward in time (default: None).
            before_softmax (bool, optional): return the raw attention
                weights and values before the attention softmax.
            need_head_weights (bool, optional): return the attention
                weights for each head. Implies *need_weights*. Default:
                return the average attention weights over all heads.
        """
        if need_head_weights:
            need_weights = True

        is_tpu = query.device.type == "xla"

        tgt_len, bsz, embed_dim = query.size()
        src_len = tgt_len
        assert embed_dim == self.embed_dim
        assert list(query.size()) == [tgt_len, bsz, embed_dim]
        if key is not None:
            src_len, key_bsz, _ = key.size()
            if not torch.jit.is_scripting():
                assert key_bsz == bsz
                assert value is not None
                assert src_len, bsz == value.shape[:2]

        if (
            not self.onnx_trace
            and not is_tpu  # don't use PyTorch version on TPUs
            and incremental_state is None
            and not static_kv
            # A workaround for quantization to work. Otherwise JIT compilation
            # treats bias in linear module as method.
            and not torch.jit.is_scripting()
        ):
            assert key is not None and value is not None
            return F.multi_head_attention_forward(
                query,
                key,
                value,
                self.embed_dim,
                self.num_heads,
                torch.empty([0]),
                torch.cat((self.q_proj.bias, self.k_proj.bias, self.v_proj.bias)),
                self.bias_k,
                self.bias_v,
                self.add_zero_attn,
                self.dropout_module.p,
                self.out_proj.weight,
                self.out_proj.bias,
                self.training or self.dropout_module.apply_during_inference,
                key_padding_mask,
                need_weights,
                attn_mask,
                use_separate_proj_weight=True,
                q_proj_weight=self.q_proj.weight,
                k_proj_weight=self.k_proj.weight,
                v_proj_weight=self.v_proj.weight,
            )

        if incremental_state is not None:
            saved_state = self._get_input_buffer(incremental_state)
            if saved_state is not None and "prev_key" in saved_state:
                # previous time steps are cached - no need to recompute
                # key and value if they are static
                if static_kv:
                    assert self.encoder_decoder_attention and not self.self_attention
                    key = value = None
        else:
            saved_state = None

        if self.self_attention:
            q = self.q_proj(query)
            k = self.k_proj(query)
            v = self.v_proj(query)
        elif self.encoder_decoder_attention:
            # encoder-decoder attention
            q = self.q_proj(query)
            if key is None:
                assert value is None
                k = v = None
            else:
                k = self.k_proj(key)
                v = self.v_proj(key)

        else:
            assert key is not None and value is not None
            q = self.q_proj(query)
            k = self.k_proj(key)
            v = self.v_proj(value)
        q *= self.scaling

        if self.bias_k is not None:
            assert self.bias_v is not None
            k = torch.cat([k, self.bias_k.repeat(1, bsz, 1)])
            v = torch.cat([v, self.bias_v.repeat(1, bsz, 1)])
            if attn_mask is not None:
                attn_mask = torch.cat(
                    [attn_mask, attn_mask.new_zeros(attn_mask.size(0), 1)], dim=1
                )
            if key_padding_mask is not None:
                key_padding_mask = torch.cat(
                    [
                        key_padding_mask,
                        key_padding_mask.new_zeros(key_padding_mask.size(0), 1),
                    ],
                    dim=1,
                )

        q = (
            q.contiguous()
            .view(tgt_len, bsz * self.num_heads, self.head_dim)
            .transpose(0, 1)
        )
        if k is not None:
            k = (
                k.contiguous()
                .view(-1, bsz * self.num_heads, self.head_dim)
                .transpose(0, 1)
            )
        if v is not None:
            v = (
                v.contiguous()
                .view(-1, bsz * self.num_heads, self.head_dim)
                .transpose(0, 1)
            )

        if saved_state is not None:
            # saved states are stored with shape (bsz, num_heads, seq_len, head_dim)
            if "prev_key" in saved_state:
                _prev_key = saved_state["prev_key"]
                assert _prev_key is not None
                prev_key = _prev_key.view(bsz * self.num_heads, -1, self.head_dim)
                if static_kv:
                    k = prev_key
                else:
                    assert k is not None
                    k = torch.cat([prev_key, k], dim=1)
                src_len = k.size(1)
            if "prev_value" in saved_state:
                _prev_value = saved_state["prev_value"]
                assert _prev_value is not None
                prev_value = _prev_value.view(bsz * self.num_heads, -1, self.head_dim)
                if static_kv:
                    v = prev_value
                else:
                    assert v is not None
                    v = torch.cat([prev_value, v], dim=1)
            prev_key_padding_mask: Optional[Tensor] = None
            if "prev_key_padding_mask" in saved_state:
                prev_key_padding_mask = saved_state["prev_key_padding_mask"]
            assert k is not None and v is not None
            key_padding_mask = MultiheadAttention._append_prev_key_padding_mask(
                key_padding_mask=key_padding_mask,
                prev_key_padding_mask=prev_key_padding_mask,
                batch_size=bsz,
                src_len=k.size(1),
                static_kv=static_kv,
            )

            saved_state["prev_key"] = k.view(bsz, self.num_heads, -1, self.head_dim)
            saved_state["prev_value"] = v.view(bsz, self.num_heads, -1, self.head_dim)
            saved_state["prev_key_padding_mask"] = key_padding_mask
            # In this branch incremental_state is never None
            assert incremental_state is not None
            incremental_state = self._set_input_buffer(incremental_state, saved_state)
        assert k is not None
        assert k.size(1) == src_len

        # This is part of a workaround to get around fork/join parallelism
        # not supporting Optional types.
        if key_padding_mask is not None and key_padding_mask.dim() == 0:
            key_padding_mask = None

        if key_padding_mask is not None:
            assert key_padding_mask.size(0) == bsz
            assert key_padding_mask.size(1) == src_len

        if self.add_zero_attn:
            assert v is not None
            src_len += 1
            k = torch.cat([k, k.new_zeros((k.size(0), 1) + k.size()[2:])], dim=1)
            v = torch.cat([v, v.new_zeros((v.size(0), 1) + v.size()[2:])], dim=1)
            if attn_mask is not None:
                attn_mask = torch.cat(
                    [attn_mask, attn_mask.new_zeros(attn_mask.size(0), 1)], dim=1
                )
            if key_padding_mask is not None:
                key_padding_mask = torch.cat(
                    [
                        key_padding_mask,
                        torch.zeros(key_padding_mask.size(0), 1).type_as(
                            key_padding_mask
                        ),
                    ],
                    dim=1,
                )

        attn_weights = torch.bmm(q, k.transpose(1, 2))
        attn_weights = self.apply_sparse_mask(attn_weights, tgt_len, src_len, bsz)

        assert list(attn_weights.size()) == [bsz * self.num_heads, tgt_len, src_len]

        if attn_mask is not None:
            attn_mask = attn_mask.unsqueeze(0)
            if self.onnx_trace:
                attn_mask = attn_mask.repeat(attn_weights.size(0), 1, 1)
            attn_weights += attn_mask

        if key_padding_mask is not None:
            # don't attend to padding symbols
            attn_weights = attn_weights.view(bsz, self.num_heads, tgt_len, src_len)
            if not is_tpu:
                attn_weights = attn_weights.masked_fill(
                    key_padding_mask.unsqueeze(1).unsqueeze(2).to(torch.bool),
                    float("-inf"),
                )
            else:
                attn_weights = attn_weights.transpose(0, 2)
                attn_weights = attn_weights.masked_fill(key_padding_mask, float("-inf"))
                attn_weights = attn_weights.transpose(0, 2)
            attn_weights = attn_weights.view(bsz * self.num_heads, tgt_len, src_len)

        if before_softmax:
            return attn_weights, v

        attn_weights_float = utils.softmax(
            attn_weights, dim=-1, onnx_trace=self.onnx_trace
        )
        attn_weights = attn_weights_float.type_as(attn_weights)
        attn_probs = self.dropout_module(attn_weights)

        assert v is not None
        attn = torch.bmm(attn_probs, v)
        assert list(attn.size()) == [bsz * self.num_heads, tgt_len, self.head_dim]
        if self.onnx_trace and attn.size(1) == 1:
            # when ONNX tracing a single decoder step (sequence length == 1)
            # the transpose is a no-op copy before view, thus unnecessary
            attn = attn.contiguous().view(tgt_len, bsz, embed_dim)
        else:
            attn = attn.transpose(0, 1).contiguous().view(tgt_len, bsz, embed_dim)
        attn = self.out_proj(attn)
        attn_weights: Optional[Tensor] = None
        if need_weights:
            attn_weights = attn_weights_float.view(
                bsz, self.num_heads, tgt_len, src_len
            ).transpose(1, 0)
            if not need_head_weights:
                # average attention weights over heads
                attn_weights = attn_weights.mean(dim=0)

        return attn, attn_weights

    @staticmethod
    def _append_prev_key_padding_mask(
        key_padding_mask: Optional[Tensor],
        prev_key_padding_mask: Optional[Tensor],
        batch_size: int,
        src_len: int,
        static_kv: bool,
    ) -> Optional[Tensor]:
        # saved key padding masks have shape (bsz, seq_len)
        if prev_key_padding_mask is not None and static_kv:
            new_key_padding_mask = prev_key_padding_mask
        elif prev_key_padding_mask is not None and key_padding_mask is not None:
            new_key_padding_mask = torch.cat(
                [prev_key_padding_mask.float(), key_padding_mask.float()], dim=1
            )
        # During incremental decoding, as the padding token enters and
        # leaves the frame, there will be a time when prev or current
        # is None
        elif prev_key_padding_mask is not None:
            filler = torch.zeros(
                (batch_size, src_len - prev_key_padding_mask.size(1)),
                device=prev_key_padding_mask.device,
            )
            new_key_padding_mask = torch.cat(
                [prev_key_padding_mask.float(), filler.float()], dim=1
            )
        elif key_padding_mask is not None:
            filler = torch.zeros(
                (batch_size, src_len - key_padding_mask.size(1)),
                device=key_padding_mask.device,
            )
            new_key_padding_mask = torch.cat(
                [filler.float(), key_padding_mask.float()], dim=1
            )
        else:
            new_key_padding_mask = prev_key_padding_mask
        return new_key_padding_mask

    @torch.jit.export
    def reorder_incremental_state(
        self,
        incremental_state: Dict[str, Dict[str, Optional[Tensor]]],
        new_order: Tensor,
    ):
        """Reorder buffered internal state (for incremental generation)."""
        input_buffer = self._get_input_buffer(incremental_state)
        if input_buffer is not None:
            for k in input_buffer.keys():
                input_buffer_k = input_buffer[k]
                if input_buffer_k is not None:
                    if self.encoder_decoder_attention and input_buffer_k.size(
                        0
                    ) == new_order.size(0):
                        break
                    input_buffer[k] = input_buffer_k.index_select(0, new_order)
            incremental_state = self._set_input_buffer(incremental_state, input_buffer)
        return incremental_state

    def _get_input_buffer(
        self, incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]]
    ) -> Dict[str, Optional[Tensor]]:
        result = self.get_incremental_state(incremental_state, "attn_state")
        if result is not None:
            return result
        else:
            empty_result: Dict[str, Optional[Tensor]] = {}
            return empty_result

    def _set_input_buffer(
        self,
        incremental_state: Dict[str, Dict[str, Optional[Tensor]]],
        buffer: Dict[str, Optional[Tensor]],
    ):
        return self.set_incremental_state(incremental_state, "attn_state", buffer)

    def apply_sparse_mask(self, attn_weights, tgt_len: int, src_len: int, bsz: int):
        return attn_weights

    def upgrade_state_dict_named(self, state_dict, name):
        prefix = name + "." if name != "" else ""
        items_to_add = {}
        keys_to_remove = []
        for k in state_dict.keys():
            if k.endswith(prefix + "in_proj_weight"):
                # in_proj_weight used to be q + k + v with same dimensions
                dim = int(state_dict[k].shape[0] / 3)
                items_to_add[prefix + "q_proj.weight"] = state_dict[k][:dim]
                items_to_add[prefix + "k_proj.weight"] = state_dict[k][dim : 2 * dim]
                items_to_add[prefix + "v_proj.weight"] = state_dict[k][2 * dim :]

                keys_to_remove.append(k)

                k_bias = prefix + "in_proj_bias"
                if k_bias in state_dict.keys():
                    dim = int(state_dict[k].shape[0] / 3)
                    items_to_add[prefix + "q_proj.bias"] = state_dict[k_bias][:dim]
                    items_to_add[prefix + "k_proj.bias"] = state_dict[k_bias][
                        dim : 2 * dim
                    ]
                    items_to_add[prefix + "v_proj.bias"] = state_dict[k_bias][2 * dim :]

                    keys_to_remove.append(prefix + "in_proj_bias")

        for k in keys_to_remove:
            del state_dict[k]

        for key, value in items_to_add.items():
            state_dict[key] = value

class MultiheadSelfAttention(nn.Module):

    def __init__(
        self,
        embed_dim,
        num_heads,
        kdim=None,
        vdim=None,
        dropout=0.0,
        bias=True,
        add_bias_kv=False,
        add_zero_attn=False,
        self_attention=False,
        encoder_decoder_attention=False,
        q_noise=0.0,
        qn_block_size=8,
    ):
        super().__init__()
        self.embed_dim = embed_dim
        self.kdim = kdim if kdim is not None else embed_dim
        self.vdim = vdim if vdim is not None else embed_dim
        self.qkv_same_dim = self.kdim == embed_dim and self.vdim == embed_dim

        self.num_heads = num_heads
        self.dropout_module = FairseqDropout(
            dropout, module_name=self.__class__.__name__
        )

        self.head_dim = embed_dim // num_heads

        assert (
            self.head_dim * num_heads == self.embed_dim
        ), "embed_dim must be divisible by num_heads"

        self.scaling = self.head_dim ** -0.5

        self.k_proj = quant_noise(
            nn.Linear(self.kdim, embed_dim, bias=bias), q_noise, qn_block_size
        )
        self.v_proj = quant_noise(
            nn.Linear(self.vdim, embed_dim, bias=bias), q_noise, qn_block_size
        )
        self.q_proj = quant_noise(
            nn.Linear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size
        )

        self.out_proj = quant_noise(
            nn.Linear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size
        )

    def reset_parameters(self):
        if self.qkv_same_dim:
            # Empirically observed the convergence to be much better with
            # the scaled initialization
            nn.init.xavier_uniform_(self.k_proj.weight, gain=1 / math.sqrt(2))
            nn.init.xavier_uniform_(self.v_proj.weight, gain=1 / math.sqrt(2))
            nn.init.xavier_uniform_(self.q_proj.weight, gain=1 / math.sqrt(2))
        else:
            nn.init.xavier_uniform_(self.k_proj.weight)
            nn.init.xavier_uniform_(self.v_proj.weight)
            nn.init.xavier_uniform_(self.q_proj.weight)

        nn.init.xavier_uniform_(self.out_proj.weight)
        if self.out_proj.bias is not None:
            nn.init.constant_(self.out_proj.bias, 0.0)

    def forward(
        self,
        query,
        key: Optional[Tensor],
        value: Optional[Tensor],
        key_padding_mask: Optional[Tensor] = None,
        incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]] = None,
        need_weights: bool = True,
        static_kv: bool = False,
        attn_mask: Optional[Tensor] = None,
        before_softmax: bool = False,
        need_head_weights: bool = False,
    ) -> Tuple[Tensor, Optional[Tensor]]:
        """Input shape: Time x Batch x Channel"""

        tgt_len, bsz, embed_dim = query.size()  # tbc
        src_len = tgt_len
        assert embed_dim == self.embed_dim

        q = self.q_proj(query)
        k = self.k_proj(query)
        v = self.v_proj(query)

        q *= self.scaling

        q = (q.contiguous().view(tgt_len, bsz * self.num_heads, self.head_dim).transpose(0, 1))
        k = (k.contiguous().view(-1, bsz * self.num_heads, self.head_dim).transpose(0, 1))
        v = (v.contiguous().view(-1, bsz * self.num_heads, self.head_dim).transpose(0, 1))

        if key_padding_mask is not None and key_padding_mask.dim() == 0:
            key_padding_mask = None

        if key_padding_mask is not None:
            assert key_padding_mask.size(0) == bsz
            assert key_padding_mask.size(1) == src_len

        attn_weights = torch.bmm(q, k.transpose(1, 2))
        assert list(attn_weights.size()) == [bsz * self.num_heads, tgt_len, src_len]

        if key_padding_mask is not None:
            # don't attend to padding symbols
            attn_weights = attn_weights.view(bsz, self.num_heads, tgt_len, src_len)
            attn_weights = attn_weights.masked_fill(
                    key_padding_mask.unsqueeze(1).unsqueeze(2).to(torch.bool),
                    float("-inf"),
                )
            attn_weights = attn_weights.view(bsz * self.num_heads, tgt_len, src_len)

        attn_weights = F.softmax(attn_weights, dim=-1)
        attn_probs = self.dropout_module(attn_weights)

        attn = torch.bmm(attn_probs, v)
        assert list(attn.size()) == [bsz * self.num_heads, tgt_len, self.head_dim]

        attn = attn.transpose(0, 1).contiguous().view(tgt_len, bsz, embed_dim)

        attn = self.out_proj(attn)

        attn_weights: Optional[Tensor] = None

        return attn, attn_weights

class KmeansAttention0(nn.Module):

    def __init__(
        self,
        embed_dim,
        num_heads,
        kdim=None,
        vdim=None,
        dropout=0.0,
        bias=True,
        q_noise=0.0,
        qn_block_size=8,
    ):
        super().__init__()
        self.embed_dim = embed_dim
        self.kdim = kdim if kdim is not None else embed_dim
        self.vdim = vdim if vdim is not None else embed_dim
        self.qkv_same_dim = self.kdim == embed_dim and self.vdim == embed_dim

        self.num_heads = num_heads
        self.dropout_module = FairseqDropout(
            dropout, module_name=self.__class__.__name__
        )

        self.head_dim = embed_dim // num_heads

        assert (
            self.head_dim * num_heads == self.embed_dim
        ), "embed_dim must be divisible by num_heads"

        self.scaling = self.head_dim ** -0.5

        self.kvc_proj = quant_noise(
            nn.Linear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size
        )
        self.qks_proj = quant_noise(
            nn.Linear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size
        )
        self.vs_proj = quant_noise(
            nn.Linear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size
        )

        self.out_proj = quant_noise(
            nn.Linear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size
        )

        self.scaling_cluster = embed_dim ** -0.5
        self.reset_parameters()

    def reset_parameters(self):
        if self.qkv_same_dim:
            # Empirically observed the convergence to be much better with
            # the scaled initialization
            nn.init.xavier_uniform_(self.kvc_proj.weight, gain=1 / math.sqrt(2))
            nn.init.xavier_uniform_(self.qks_proj.weight, gain=1 / math.sqrt(2))
            nn.init.xavier_uniform_(self.vs_proj.weight, gain=1 / math.sqrt(2))
        else:
            nn.init.xavier_uniform_(self.kvc_proj.weight)
            nn.init.xavier_uniform_(self.qks_proj.weight)
            nn.init.xavier_uniform_(self.vs_proj.weight)

        nn.init.xavier_uniform_(self.out_proj.weight)
        if self.out_proj.bias is not None:
            nn.init.constant_(self.out_proj.bias, 0.0)

    def perform_attention(self, q, k, v, mask=None, need_weight=False):
        attn = torch.matmul(q, k.transpose(1, 2)) # b,t,t
        if mask is not None:
            attn = attn.masked_fill(mask, -1e10)
        attn = F.softmax(attn, dim=-1)
        output = torch.matmul(attn, v)
        if need_weight:
            return output, attn
        else:
            return output


    def get_index(self, similarity_matrix):
        _, buckets_index = torch.max(similarity_matrix.transpose(1, 2), dim=-1)
        return buckets_index

    def forward(
        self,
        query,
        clusterCenter,
        add_loss=False,
        key_padding_mask: Optional[Tensor] = None,
    ):
        """Input shape: Time x Batch x Channel"""

        src_len, bsz, embed_dim = query.size()  # tbc
        assert embed_dim == self.embed_dim

        n_clusters = clusterCenter.shape[1]
        cluster_size = src_len // n_clusters

        q = query.transpose(0, 1) # b,t,c

        kvc = self.kvc_proj(q)
        qks = self.qks_proj(q)
        vs = self.vs_proj(q)


        ### cluster attention

        if key_padding_mask is not None:
            cluster_mask = key_padding_mask.unsqueeze(1).to(torch.bool)
        else:
            cluster_mask = None

        c, similarity_matrix = self.perform_attention(clusterCenter*self.scaling_cluster, kvc, kvc, cluster_mask, need_weight=True)

        q = F.normalize(q, 2, dim=-1)
        clusterCenter = F.normalize(clusterCenter+c, 2, dim=-1)
        buckets_index = self.get_index(similarity_matrix)

        if add_loss:
            expand_cluster = expand_dim(buckets_index, -1, c.shape[-1])  # [64,30,128]
            x_means = clusterCenter.gather(1, expand_cluster)

            loss1 = torch.sum((q - x_means) ** 2) / (q.shape[0] * q.shape[1])

            loss2 = torch.sum(
                (clusterCenter - torch.cat([clusterCenter[:, -1:, :], clusterCenter[:, :-1, :]], dim=1)) ** 2) \
                    / (clusterCenter.shape[0] * clusterCenter.shape[1])
        else:
            loss1, loss2 = None, None


        ### multi-head-self-attention

        _, idx1 = torch.sort(buckets_index, -1)
        index11 = idx1.unsqueeze(-1).expand_as(qks).type(q.dtype).long()
        qk_sort = qks.gather(1, index11).contiguous().view(bsz, n_clusters, cluster_size, self.num_heads, self.head_dim)
        v_sort = vs.gather(1, index11).contiguous().view(bsz, n_clusters, cluster_size, self.num_heads, self.head_dim)

        k_sort = look_one_back(qk_sort)
        v_sort = look_one_back(v_sort)

        qk_sort = qk_sort.permute(3, 0, 1, 2, 4).contiguous().view(-1, cluster_size, self.head_dim)
        k_sort = k_sort.permute(3, 0, 1, 2, 4).contiguous().view(-1, 2 * cluster_size, self.head_dim)
        v_sort = v_sort.permute(3, 0, 1, 2, 4).contiguous().view(-1, 2 * cluster_size, self.head_dim)

        if key_padding_mask is None:
            output = self.perform_attention(qk_sort * self.scaling, k_sort, v_sort)
        else:
            mask_sort = key_padding_mask.gather(1, idx1).contiguous().view(bsz, n_clusters, cluster_size)
            mask_sort = look_one_back(mask_sort)
            mask_sort = mask_sort.unsqueeze(0).unsqueeze(3).expand\
                (self.num_heads, -1, -1, cluster_size,-1)
            mask_sort = mask_sort.contiguous().view(-1, cluster_size, 2 * cluster_size)
            output = self.perform_attention(qk_sort, k_sort, v_sort, mask=mask_sort)

        _, idx2 = torch.sort(idx1)
        index22 = idx2.unsqueeze(-1).expand_as(qks).type(q.dtype).long()
        output = output.view(self.num_heads, bsz, n_clusters, cluster_size, self.head_dim)
        output = output.permute(1, 2, 3, 0, 4).contiguous().view(bsz, src_len, -1).gather(1, index22)
        output = output.transpose(0, 1)

        output = self.out_proj(output)

        return output, loss1, loss2


class KmeansAttention(nn.Module):

    def __init__(
        self,
        embed_dim,
        num_heads,
        kdim=None,
        vdim=None,
        dropout=0.0,
        bias=True,
        q_noise=0.0,
        qn_block_size=8,
    ):
        super().__init__()
        self.embed_dim = embed_dim
        self.kdim = kdim if kdim is not None else embed_dim
        self.vdim = vdim if vdim is not None else embed_dim
        self.qkv_same_dim = self.kdim == embed_dim and self.vdim == embed_dim

        self.num_heads = num_heads
        self.dropout_module = FairseqDropout(
            dropout, module_name=self.__class__.__name__
        )

        self.head_dim = embed_dim // num_heads

        assert (
            self.head_dim * num_heads == self.embed_dim
        ), "embed_dim must be divisible by num_heads"

        self.scaling = self.head_dim ** -0.5

        self.kvc_proj = quant_noise(
            nn.Linear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size
        )
        self.qks_proj = quant_noise(
            nn.Linear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size
        )
        self.vs_proj = quant_noise(
            nn.Linear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size
        )
        self.ks_proj = quant_noise(
            nn.Linear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size
        )

        # self.cc = quant_noise(
        #     nn.Linear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size
        # )

        self.out_proj = quant_noise(
            nn.Linear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size
        )

        self.scaling_cluster = embed_dim ** -0.5
        self.reset_parameters()

    def reset_parameters(self):

        nn.init.xavier_uniform_(self.kvc_proj.weight, gain=1 / math.sqrt(2))
        nn.init.xavier_uniform_(self.qks_proj.weight, gain=1 / math.sqrt(2))
        nn.init.xavier_uniform_(self.vs_proj.weight, gain=1 / math.sqrt(2))
        # nn.init.xavier_uniform_(self.cc.weight, gain=1 / math.sqrt(2))
        nn.init.xavier_uniform_(self.ks_proj.weight, gain=1 / math.sqrt(2))
        
        # nn.init.xavier_uniform_(self.kvc_proj.weight)
        # nn.init.xavier_uniform_(self.qks_proj.weight)
        # nn.init.xavier_uniform_(self.vs_proj.weight)

        nn.init.xavier_uniform_(self.out_proj.weight)
        if self.out_proj.bias is not None:
            nn.init.constant_(self.out_proj.bias, 0.0)

    def perform_attention(self, q, k, v, mask=None, need_weight=False):
        attn = torch.matmul(q, k.transpose(1, 2)) # b,t,t
        if mask is not None:
            attn = attn.masked_fill(mask, -1e10)
        attn = F.softmax(attn, dim=-1)
        attn = self.dropout_module(attn)
        output = torch.matmul(attn, v)
        if need_weight:
            return output, attn
        else:
            return output


    def get_index(self, similarity_matrix):
        _, buckets_index = torch.max(similarity_matrix.transpose(1, 2), dim=-1)
        return buckets_index

    def forward(
        self,
        query,
        clusterCenter,
        add_loss=False,
        key_padding_mask: Optional[Tensor] = None,
    ):
        """Input shape: Time x Batch x Channel"""

        src_len, bsz, embed_dim = query.size()  # tbc
        assert embed_dim == self.embed_dim

        n_clusters = clusterCenter.shape[1]
        cluster_size = src_len // n_clusters

        q = query.transpose(0, 1) # b,t,c

        kvc = self.kvc_proj(q)
        qks = self.qks_proj(q)
        vs = self.vs_proj(q)
        ks = self.ks_proj(q) 
        # clusterCenter = self.cc(clusterCenter)


        ### cluster attention

        if key_padding_mask is not None:
            cluster_mask = key_padding_mask.unsqueeze(1).to(torch.bool)
        else:
            cluster_mask = None

        c, similarity_matrix = self.perform_attention(clusterCenter*self.scaling_cluster, kvc, kvc, cluster_mask, need_weight=True)

        clusterCenter = F.normalize(clusterCenter+c, 2, dim=-1)
        buckets_index = self.get_index(similarity_matrix)

        if add_loss:
            q = F.normalize(q, 2, dim=-1)
            expand_cluster = expand_dim(buckets_index, -1, c.shape[-1])  # [64,30,128]
            x_means = clusterCenter.gather(1, expand_cluster)

            loss1 = torch.sum((q - x_means) ** 2) / (q.shape[0] * q.shape[1])

            loss2 = torch.sum(
                (clusterCenter - torch.cat([clusterCenter[:, -1:, :], clusterCenter[:, :-1, :]], dim=1)) ** 2) \
                    / (clusterCenter.shape[0] * clusterCenter.shape[1])
        else:
            loss1, loss2 = None, None
        # loss1, loss2 = 0, 0 


        ### multi-head-self-attention

        _, idx1 = torch.sort(buckets_index, -1)
        index11 = idx1.unsqueeze(-1).expand_as(qks).type(q.dtype).long()
        qk_sort = qks.gather(1, index11).contiguous().view(bsz, n_clusters, cluster_size, self.num_heads, self.head_dim)
        k_sort = ks.gather(1, index11).contiguous().view(bsz, n_clusters, cluster_size, self.num_heads, self.head_dim)
        v_sort = vs.gather(1, index11).contiguous().view(bsz, n_clusters, cluster_size, self.num_heads, self.head_dim)

        k_sort = look_one_back(k_sort)
        v_sort = look_one_back(v_sort)

        qk_sort = qk_sort.permute(3, 0, 1, 2, 4).contiguous().view(-1, cluster_size, self.head_dim)
        k_sort = k_sort.permute(3, 0, 1, 2, 4).contiguous().view(-1, cluster_size * 2, self.head_dim)
        v_sort = v_sort.permute(3, 0, 1, 2, 4).contiguous().view(-1, cluster_size * 2, self.head_dim)

        if key_padding_mask is None:
            output = self.perform_attention(qk_sort * self.scaling, k_sort, v_sort)
        else:
            mask_sort = key_padding_mask.gather(1, idx1).contiguous().view(bsz, n_clusters, cluster_size)
            mask_sort = look_one_back(mask_sort)
            mask_sort = mask_sort.unsqueeze(0).unsqueeze(3).expand\
                (self.num_heads, -1, -1, cluster_size,-1)
            mask_sort = mask_sort.contiguous().view(-1, cluster_size, cluster_size * 2)
            output = self.perform_attention(qk_sort * self.scaling, k_sort, v_sort, mask=mask_sort)
        ###

        _, idx2 = torch.sort(idx1)
        index22 = idx2.unsqueeze(-1).expand_as(qks).type(q.dtype).long()
        output = output.view(self.num_heads, bsz, n_clusters, cluster_size, self.head_dim)
        output = output.permute(1, 2, 3, 0, 4).contiguous().view(bsz, src_len, -1).gather(1, index22)
        output = output.transpose(0, 1)

        output = self.out_proj(output)

        return output, clusterCenter, loss1, loss2



def expand_dim(t, dim, k):
    t = t.unsqueeze(dim)
    expand_shape = [-1] * len(t.shape)
    expand_shape[dim] = k
    return t.expand(*expand_shape)

def look_one_back(x):
    x_extra = torch.cat([x[:, -1:, :], x[:, :-1, :]], dim=1)
    return torch.cat([x, x_extra], dim=2)