from task import input_t, output_t
from typing import Tuple

import torch
import triton
import triton.language as tl

import os
os.environ["OPTIMIZE_EPILOGUE"] = "1"

fp8_gemm_configs = [
    triton.Config({'BLOCK_SIZE_M': block_m, 'BLOCK_SIZE_N': 128, 'BLOCK_SIZE_K': 128,
            'waves_per_eu': wpeu, 'matrix_instr_nonkdim': 16},
           num_stages=2, num_warps=num_warps)
    for wpeu in [3, 4]
    for block_m in [16, 32, 64]
    for num_warps in [4, 8]
]

@triton.autotune(configs=fp8_gemm_configs, key=['N', 'K'])
@triton.jit
def fp8_gemm_kernel(a_ptr, b_ptr, c_ptr,
                    a_s_ptr, b_s_ptr,
                    M, N: tl.constexpr, K: tl.constexpr,
                    BLOCK_SIZE_M: tl.constexpr,
                    BLOCK_SIZE_N: tl.constexpr,
                    BLOCK_SIZE_K: tl.constexpr,
                    split_k: tl.constexpr):
    pid_m = tl.program_id(axis=0)
    pid_n = tl.program_id(axis=1)
    pid_k = tl.program_id(axis=2)

    k_per_split = tl.cdiv(K, split_k)
    start_k = pid_k * k_per_split
    end_k = tl.minimum(start_k + k_per_split, K)

    offs_m = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)
    offs_n = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)
    offs_k = tl.arange(0, BLOCK_SIZE_K)

    accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32)

    for k in range(start_k, end_k, BLOCK_SIZE_K):
        a_ptrs = a_ptr + offs_m[:, None] * K + (k + offs_k)[None, :]
        b_ptrs = b_ptr + offs_n[None, :] * K + (k + offs_k)[:, None]

        k_block_idx = k // BLOCK_SIZE_K
        k_blocks_total = tl.cdiv(K, BLOCK_SIZE_K)

        a_s_ptrs = a_s_ptr + offs_m * k_blocks_total + k_block_idx
        b_s_ptrs = b_s_ptr + (offs_n // BLOCK_SIZE_K) * k_blocks_total + k_block_idx

        a = tl.load(a_ptrs, mask=(k + offs_k)[None, :] < K, other=0.0)
        b = tl.load(b_ptrs, mask=(k + offs_k)[:, None] < K, other=0.0)
        a_s = tl.load(a_s_ptrs)
        b_s = tl.load(b_s_ptrs)

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

    c = accumulator.to(tl.float32)
    c_ptrs = c_ptr + offs_m[:, None] * N + offs_n[None, :]
    mask = (offs_m[:, None] < M) & (offs_n[None, :] < N)

    if split_k == 1:
        tl.store(c_ptrs, c.to(c_ptr.dtype.element_ty), mask=mask)
    else:
        # Atomic add only supports float32/float16, not bf16, so keep accumulator in float32
        tl.atomic_add(c_ptrs, c, mask=mask)

def fp8_gemm(a: torch.Tensor, a_s: torch.Tensor, b: torch.Tensor, b_s: torch.Tensor, c: torch.Tensor, split_k: int = 1):
    assert a.is_contiguous() and b.is_contiguous(), 'Input tensors must be contiguous'
    assert a_s.is_contiguous() and b_s.is_contiguous(), 'Scaling factor tensors must be contiguous'
    M, K = a.shape
    N = b.size(0)
    grid = lambda META: (
        triton.cdiv(M, META['BLOCK_SIZE_M']),
        triton.cdiv(N, META['BLOCK_SIZE_N']),
        split_k
    )
    fp8_gemm_kernel[grid](a, b, c, a_s, b_s, M, N, K, split_k=split_k)
    return c.to(torch.bfloat16)

def custom_kernel(data: input_t) -> output_t:
    a, b, a_scale, b_scale, c = data

    a = a.contiguous()
    a_scale = a_scale.contiguous()
    b = b.contiguous()
    b_scale = b_scale.contiguous()

    return fp8_gemm(a, a_scale, b, b_scale, c, split_k=1)

python eval.py benchmark /tmp/tmp5xyb77bz