# LICENSE HEADER MANAGED BY add-license-header
#
# Copyright 2018 Kornia Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

# References:
#   https://github.com/facebookresearch/dino/blob/master/vision_transformer.py
#   https://github.com/rwightman/pytorch-image-models/tree/master/timm/models/vision_transformer.py

from torch import Tensor, nn

from kornia.core._compat import scaled_dot_product_attention


class Attention(nn.Module):
    """Implement the standard Multi-Head Self-Attention mechanism.

    Args:
        dim: Total dimension of the input and output features.
        num_heads: Number of attention heads.
        qkv_bias: If True, add a bias term to the query, key, and value projections.
        proj_bias: If True, add a bias term to the output projection.
        attn_drop: Dropout probability applied to the attention weights. A non-zero value makes the training-mode
            forward materialise the ``(B, num_heads, N, N)`` weights.
        proj_drop: Dropout probability applied to the output projection.
    """

    def __init__(
        self,
        dim: int,
        num_heads: int = 8,
        qkv_bias: bool = False,
        proj_bias: bool = True,
        attn_drop: float = 0.0,
        proj_drop: float = 0.0,
    ) -> None:
        super().__init__()
        self.num_heads = num_heads
        head_dim = dim // num_heads
        self.scale = head_dim**-0.5

        self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
        self.attn_drop = nn.Dropout(attn_drop)
        self.proj = nn.Linear(dim, dim, bias=proj_bias)
        self.proj_drop = nn.Dropout(proj_drop)

    def forward(self, x: Tensor) -> Tensor:
        """Run this DeDoDe module forward.

        Args:
            x: Input token sequence with shape ``(B, N, C)``, where ``B`` is batch size,
               ``N`` is number of tokens (patches + class token), and ``C`` is the embedding dimension.

        Returns:
            Attention output with shape ``(B, N, C)``.
        """
        B, N, C = x.shape
        qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)

        q, k, v = qkv[0], qkv[1], qkv[2]
        if self.training and self.attn_drop.p > 0.0:
            # The dropout of ``attn_drop`` acts on the weights, so they are materialised here:
            # ``scaled_dot_product_attention`` has no dropout on MPS.
            attn = self.attn_drop(((q * self.scale) @ k.transpose(-2, -1)).softmax(dim=-1))
            x = attn @ v
        else:
            # softmax(q k^T * scale) v; the fused kernels do not materialise the N x N weights of every head.
            x = scaled_dot_product_attention(q, k, v, scale=self.scale)
        x = x.transpose(1, 2).reshape(B, N, C)
        x = self.proj(x)
        return self.proj_drop(x)


class MemEffAttention(Attention):
    """Implement Multi-Head Self-Attention under the class name the vendored DINOv2 builders select.

    The computation is the one of :class:`Attention`. The class name is kept because the DINOv2 model
    builders reference it; the extra ``attn_bias`` argument is kept so :meth:`forward` matches the upstream
    DINOv2 signature. Nothing in kornia passes it, and it must be ``None``.
    """

    def forward(self, x: Tensor, attn_bias=None) -> Tensor:
        """Run this DeDoDe module forward.

        Args:
            x: Input token sequence with shape ``(B, N, C)``.
            attn_bias: Unsupported; must be ``None``.

        Returns:
            Attention output with shape ``(B, N, C)``.
        """
        if attn_bias is not None:
            raise NotImplementedError("attn_bias is not supported")
        return super().forward(x)
