# SPDX-License-Identifier: MIT
 
import os
import json
import logging
import functools
from functools import partial
from typing import Any, Dict, List, Optional, Tuple
from aiter.ops.triton.utils.core import AITER_TRITON_CONFIGS_PATH
import aiter.ops.triton.utils.arch_info as arch_info
from aiter import logger
from aiter.jit.utils.chip_info import get_cu_num
from aiter.ops.triton.utils.common_utils import save_kernel_path, has_kernel_cache, get_triton_cache_dir
from aiter.jit.utils.chip_info import get_gfx, get_cu_num

import torch
import triton
import triton.language as tl

@functools.lru_cache
def get_w8a8_block_int8_configs(N: int, K: int, block_n: int,
                                block_k: int) -> Optional[dict[int, Any]]:
    """
    Return optimized configurations for the w8a8 block fp8 kernel.

    The return value will be a dictionary that maps an irregular grid of
    batch sizes to configurations of the w8a8 block fp8 kernel. To evaluate the
    kernel on a given batch size bs, the closest batch size in the grid should
    be picked and the associated configuration chosen to invoke the kernel.
    """

    # First look up if an optimized configuration is available in the configs
    # directory
    # device_name = current_platform.get_device_name().replace(" ", "_")
    device_name = arch_info.get_device()
    device_name = "BW200" if device_name.lower().startswith("bw") else device_name
    
    # new config by arch and cu number
    arch = triton.runtime.driver.active.get_current_target().arch
    num_cu = get_cu_num()
    json_file_name = f"N={N},K={K},arch={arch},cu={num_cu},dtype=int8_w8a8,block_shape=[{block_n}, {block_k}].json" # noqa: E501
    config_file_path = os.path.join(
        f"{AITER_TRITON_CONFIGS_PATH}", "gemm/block_w8a8", json_file_name
    )

    # Fallback to device config (to be removed)
    if not os.path.exists(config_file_path):
        json_file_name = f"N={N},K={K},device_name={device_name},dtype=int8_w8a8,block_shape=[{block_n}, {block_k}].json"  # noqa: E501
    
    config_file_path = os.path.join(
        f"{AITER_TRITON_CONFIGS_PATH}", "gemm/block_w8a8", json_file_name
    )
    
    if os.path.exists(config_file_path):
        with open(config_file_path) as f:
            #logger.info(
            #    "Using configuration from %s for W8A8 Block INT8 kernel.",
            #    config_file_path,
            #)
            # If a configuration has been found, return it
            return {int(key): val for key, val in json.load(f).items()}

    # If no optimized configuration is available, we will use the default
    # configuration
    logger.warning(
        ("Using default W8A8 Block INT8 kernel config. Performance might "
         "be sub-optimal! Config file not found at %s"),
        config_file_path,
    )
    return None

'''
configs = [
    triton.Config(
        {"BLOCK_SIZE_M": BLOCK_SIZE_M, "BLOCK_SIZE_N": BLOCK_SIZE_N, "BLOCK_SIZE_K": BLOCK_SIZE_K,
         "GROUP_SIZE_M": GROUP_SIZE_M, "COMBINE_SCALE_LOAD": COMBINE_SCALE_LOAD,
         "USE_MLS_LOAD": USE_MLS_LOAD},
        num_warps=num_warps, num_stages=num_stages)
        # for BLOCK_SIZE_M in [16, 32, 64, 128, 256]
        # for BLOCK_SIZE_N in [16, 32, 64, 128, 256]
        # for BLOCK_SIZE_M in [16, 32]
        for BLOCK_SIZE_M in [16, 32, 64]
        for BLOCK_SIZE_N in [16, 32, 64, 128]
        for BLOCK_SIZE_K in [128]
        for GROUP_SIZE_M in [16, 32, 64]
        # for GROUP_SIZE_M in [32]
        for COMBINE_SCALE_LOAD in [True, False]
        # for COMBINE_SCALE_LOAD in [False]
        for USE_MLS_LOAD in [True, False]
        # for USE_MLS_LOAD in [True]
        for num_warps in [1, 2, 4, 8, 16]
        for num_stages in [1, 2]
]

# @triton.autotune(
@triton.utils.hcutune(
    configs=configs,
    key=["M", "K", "N", "group_n", "group_k"],
    # perf_debug=True
)
'''

@triton.heuristics(
    values={
        "DIVISIBLE_M": lambda args: args["M"] % args["BLOCK_SIZE_M"] == 0,
        "DIVISIBLE_N": lambda args: args["N"] % args["BLOCK_SIZE_N"] == 0,
        'DIVISIBLE_K': lambda args: args['K'] % args['BLOCK_SIZE_K'] == 0,
    }
)
@triton.jit
def _w8a8_block_int8_matmul(
    # Pointers to inputs and output
    A,
    B,
    C,
    As,
    Bs,
    # Shape for matmul
    M,
    N,
    K,
    # Block size for block-wise quantization
    group_n: tl.constexpr,
    group_k: tl.constexpr,
    # Stride for inputs and output
    stride_am: tl.constexpr,
    stride_ak: tl.constexpr,
    stride_bk: tl.constexpr,
    stride_bn: tl.constexpr,
    stride_cm: tl.constexpr,
    stride_cn: tl.constexpr,
    stride_As_m: tl.constexpr,
    stride_As_k: tl.constexpr,
    stride_Bs_k: tl.constexpr,
    stride_Bs_n: tl.constexpr,
    # Meta-parameters
    BLOCK_SIZE_M: tl.constexpr,
    BLOCK_SIZE_N: tl.constexpr,
    BLOCK_SIZE_K: tl.constexpr,
    GROUP_SIZE_M: tl.constexpr,
    DIVISIBLE_M: tl.constexpr,
    DIVISIBLE_N: tl.constexpr,
    DIVISIBLE_K: tl.constexpr,
    COMBINE_SCALE_LOAD: tl.constexpr = False,
    USE_MLS_LOAD: tl.constexpr = False
):
    """Triton-accelerated function used to perform linear operations (dot
    product) on input tensors `A` and `B` with block-wise quantization, and
    store the result in output tensor `C`.
    """

    pid = tl.program_id(axis=0)
    num_pid_m = tl.cdiv(M, BLOCK_SIZE_M)
    num_pid_n = tl.cdiv(N, BLOCK_SIZE_N)
    num_pid_in_group = GROUP_SIZE_M * num_pid_n
    group_id = pid // num_pid_in_group
    first_pid_m = group_id * GROUP_SIZE_M
    group_size_m = min(num_pid_m - first_pid_m, GROUP_SIZE_M)
    pid_m = first_pid_m + (pid % group_size_m)
    pid_n = (pid % num_pid_in_group) // group_size_m

    tl.assume(pid > 0)
    tl.assume(pid_m > 0)
    tl.assume(pid_n > 0)
    tl.assume(group_n > 0)
    tl.assume(group_k > 0)
    tl.assume(stride_am > 0)
    tl.assume(stride_ak > 0)
    tl.assume(stride_bk > 0)
    tl.assume(stride_bn > 0)
    tl.assume(stride_cm > 0)
    tl.assume(stride_cn > 0)
    tl.assume(stride_As_m > 0)
    tl.assume(stride_As_k > 0)
    tl.assume(stride_Bs_k > 0)
    tl.assume(stride_Bs_n > 0)

    if group_k > 0:
        tl.static_assert(BLOCK_SIZE_K <= group_k and group_k % BLOCK_SIZE_K == 0,
            "BLOCK_SIZE_K must be divisible by GROUP_SIZE_K")
    if COMBINE_SCALE_LOAD: # used for use_int8_w8a8
        tl.static_assert(stride_As_k == 1,
            "COMBINE_SCALE_LOAD implictly stride_As_k == 1!")
        tl.static_assert(DIVISIBLE_K == True and BLOCK_SIZE_K == group_k,
            "COMBINE_SCALE_LOAD only add and verify on block_k_diviable!")
    if USE_MLS_LOAD:
        tl.static_assert(DIVISIBLE_K == True and DIVISIBLE_N == True,
            "USE_MLS_LOAD must require block_k_diviable and block_n_diviable!")

    offs_am = (pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)) % M
    offs_bn = (pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)) % N
    offs_k = tl.arange(0, BLOCK_SIZE_K)
    a_ptrs = A + (offs_am[:, None] * stride_am + offs_k[None, :] * stride_ak)
    b_ptrs = B + (offs_k[:, None] * stride_bk + offs_bn[None, :] * stride_bn)

    mls_offs_k = 0
    if COMBINE_SCALE_LOAD:
        As_ptrs = As + offs_am[:, None] * stride_As_m
        offs_bsn = offs_bn // group_n
        Bs_ptrs = Bs + offs_bsn[:, None] * stride_Bs_n
        accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32)
        for k in range(0, tl.cdiv(K, BLOCK_SIZE_K), 2):
            a0 = tl.load(a_ptrs,
                        mask=offs_k[None, :] < K - k * BLOCK_SIZE_K,
                        other=0.0)
            if not USE_MLS_LOAD:
                b0 = tl.load(b_ptrs,
                            mask=offs_k[:, None] < K - k * BLOCK_SIZE_K,
                            other=0.0)
            else:
                b0 = tl.matrix_load(
                            B,
                            shape=[K, N],
                            strides=[stride_bk, stride_bn],
                            block_shape=[BLOCK_SIZE_K, BLOCK_SIZE_N],
                            offsets=[mls_offs_k, (pid_n * BLOCK_SIZE_N) % N])

            k_start = k * BLOCK_SIZE_K
            offs_ks = k_start // group_k + tl.arange(0, 2)
            a_s = tl.load(As_ptrs + offs_ks[None, :] * stride_As_k,
                          mask=offs_ks[None, :] <= (K - 1) // group_k,
                          other=0.0)
            b_s = tl.load(Bs_ptrs + offs_ks[None, :] * stride_Bs_k,
                          mask=offs_ks[None, :] <= (K - 1) // group_k,
                          other=0.0)
            a_s0, a_s1 = tl.split(a_s)
            b_s0, b_s1 = tl.split(b_s)

            accumulator += tl.dot(a0, b0).to(tl.float32) * a_s0[:, None] * b_s0[None, :]

            a0 = tl.load(a_ptrs + BLOCK_SIZE_K * stride_ak,
                        mask=offs_k[None, :] < K - (k + 1)* BLOCK_SIZE_K,
                        other=0.0)
            if not USE_MLS_LOAD:
                b0 = tl.load(b_ptrs + BLOCK_SIZE_K * stride_bk,
                        mask=offs_k[:, None] < K - (k + 1) * BLOCK_SIZE_K,
                        other=0.0)
            else:
                b0 = tl.matrix_load(
                        B,
                        shape=[K, N],
                        strides=[stride_bk, stride_bn],
                        block_shape=[BLOCK_SIZE_K, BLOCK_SIZE_N],
                        offsets=[mls_offs_k + BLOCK_SIZE_K, (pid_n * BLOCK_SIZE_N) % N])

            accumulator += tl.dot(a0, b0).to(tl.float32) * a_s1[:, None] * b_s1[None, :]

            a_ptrs += BLOCK_SIZE_K * stride_ak * 2
            b_ptrs += BLOCK_SIZE_K * stride_bk * 2
            mls_offs_k += BLOCK_SIZE_K * 2
    else:
        As_ptrs = As + offs_am * stride_As_m
        offs_bsn = offs_bn // group_n
        Bs_ptrs = Bs + offs_bsn * stride_Bs_n
        accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32)
        for k in range(0, tl.cdiv(K, BLOCK_SIZE_K)):
            a = tl.load(a_ptrs,
                        mask=offs_k[None, :] < K - k * BLOCK_SIZE_K,
                        other=0.0)
            if not USE_MLS_LOAD:
                b = tl.load(b_ptrs,
                        mask=offs_k[:, None] < K - k * BLOCK_SIZE_K,
                        other=0.0)
            else:
                b = tl.matrix_load(
                            B,
                            shape=[K, N],
                            strides=[stride_bk, stride_bn],
                            block_shape=[BLOCK_SIZE_K, BLOCK_SIZE_N],
                            offsets=[mls_offs_k, (pid_n * BLOCK_SIZE_N) % N])

            k_start = k * BLOCK_SIZE_K
            offs_ks = k_start // group_k
            a_s = tl.load(As_ptrs + offs_ks * stride_As_k)
            b_s = tl.load(Bs_ptrs + offs_ks * stride_Bs_k)

            accumulator += tl.dot(a, b).to(tl.float32) * a_s[:, None] * b_s[None, :]

            a_ptrs += BLOCK_SIZE_K * stride_ak
            b_ptrs += BLOCK_SIZE_K * stride_bk
            mls_offs_k += BLOCK_SIZE_K
 

    if C.dtype.element_ty == tl.bfloat16:
        c = accumulator.to(tl.bfloat16)
    elif C.dtype.element_ty == tl.float16:
        c = accumulator.to(tl.float16)
    else:
        c = accumulator.to(tl.float32)

    offs_cm = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)
    offs_cn = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)

    offs_c = stride_cm * offs_cm[:, None] + stride_cn * offs_cn[None, :]
    c_ptrs = C + offs_c

    STORE_MASK_FREE: tl.constexpr = DIVISIBLE_M & DIVISIBLE_N
    if STORE_MASK_FREE:
        tl.store(c_ptrs, c)
    else:
        c_mask = (offs_cm[:, None] < M) & (offs_cn[None, :] < N)
        tl.store(c_ptrs, c, mask=c_mask)


# def gemm_a8w8(
#     x: torch.Tensor,
#     w: torch.Tensor,
#     x_scale: torch.Tensor,
#     w_scale: torch.Tensor,
#     bias: Optional[torch.Tensor] = None,
#     dtype: Optional[float] = torch.bfloat16,
#     y: Optional[torch.Tensor] = None,
#     config: Optional[dict] = None,
# ):

def gemm_w8a8(
    A: torch.Tensor,
    B: torch.Tensor,
    As: torch.Tensor,
    Bs: torch.Tensor,
    block_size: list[int],
    output_dtype: torch.dtype = torch.float16,
) -> torch.Tensor:
    """This function performs matrix multiplication with block-wise
    quantization.

    It takes two input tensors `A` and `B` with scales `As` and `Bs`.
    The output is returned in the specified `output_dtype`.

    Args:
        A: The input tensor, e.g., activation.
        B: The input tensor, e.g., weight.
        As: The per-token-group quantization scale for `A`.
        Bs: The per-block quantization scale for `B`.
        block_size: The block size for per-block quantization. It should be
            2-dim, e.g., [128, 128].
        output_dytpe: The dtype of the returned tensor.

    Returns:
        torch.Tensor: The result of matmul.
    """
    assert len(block_size) == 2
    block_n, block_k = block_size[0], block_size[1]

    assert A.shape[-1] == B.shape[-1]
    assert A.shape[:-1] == As.shape[:-1] and A.is_contiguous()
    assert triton.cdiv(A.shape[-1], block_k) == As.shape[-1]
    M = A.numel() // A.shape[-1]

    assert B.ndim == 2 and B.is_contiguous() and Bs.ndim == 2
    N, K = B.shape
    assert triton.cdiv(N, block_n) == Bs.shape[0]
    assert triton.cdiv(K, block_k) == Bs.shape[1]

    C_shape = A.shape[:-1] + (N, )
    C = A.new_empty(C_shape, dtype=output_dtype)

    configs = get_w8a8_block_int8_configs(N, K, block_size[0], block_size[1])
    if configs:
        # If an optimal configuration map has been found, look up the
        # optimal config
        config = configs[min(configs.keys(), key=lambda x: abs(x - M))]
    else:
        # Default config
        # Block-wise quant: BLOCK_SIZE_K must be divisible by block_size[1]
        config = {
            "BLOCK_SIZE_M": 64,
            "BLOCK_SIZE_N": block_size[0],
            "BLOCK_SIZE_K": block_size[1],
            "GROUP_SIZE_M": 32,
            "COMBINE_SCALE_LOAD": False,
            "USE_MLS_LOAD": False,
            "num_warps": 4,
            "num_stages": 3,
        }

    def grid(META):
        return (triton.cdiv(M, META["BLOCK_SIZE_M"]) *
                triton.cdiv(N, META["BLOCK_SIZE_N"]), )

    _w8a8_block_int8_matmul[grid](
        A,
        B,
        C,
        As,
        Bs,
        M,
        N,
        K,
        block_n,
        block_k,
        A.stride(-2),
        A.stride(-1),
        B.stride(1),
        B.stride(0),
        C.stride(-2),
        C.stride(-1),
        As.stride(-2),
        As.stride(-1),
        Bs.stride(1),
        Bs.stride(0),
        **config,
    )

    # logger.info(f"triton kernel regs: {_compiled_kernel.n_regs}, spills: {_compiled_kernel.n_spills}")
    # best_config = _w8a8_block_int8_matmul.best_config
    # print("Best Config:", best_config)
    return C
