import torch
import triton
import triton.language as tl


@triton.jit
def matmul_kernel(
    A_ptr, B_ptr, A_SCALE_ptr, B_SCALE_ptr, C_ptr,
    M, N, K,
    stride_a_m_dim, stride_a_k_dim,      
    stride_b_n_dim, stride_b_k_dim,    
    stride_a_scale_m_dim, stride_a_scale_k_scaled_dim,
    stride_b_scale_n_scaled_dim, stride_b_scale_k_scaled_dim, 
    stride_c_m_dim, stride_c_n_dim,    
    BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr, BLOCK_SIZE_K: tl.constexpr,
    SCALE_K_CONST: tl.constexpr, 
    SCALE_N_CONST: tl.constexpr, 
):
    pid_m = tl.program_id(0)
    pid_n = tl.program_id(1)

    offs_m_for_c_tile = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)
    offs_n_for_c_tile = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)

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

    for k_loop_start_offset in range(0, K, BLOCK_SIZE_K):
        a_k_indices_range = tl.arange(0, BLOCK_SIZE_K)
        a_k_indices_absolute = k_loop_start_offset + a_k_indices_range

        k_mask_for_block = a_k_indices_absolute < K 

        a_tile_ptrs = A_ptr + (offs_m_for_c_tile[:, None] * stride_a_m_dim +
                               a_k_indices_absolute[None, :] * stride_a_k_dim)
        mask_m_for_a_tile = offs_m_for_c_tile < M      
        final_a_mask = mask_m_for_a_tile[:, None] & k_mask_for_block[None, :] 
        a_block_fp8 = tl.load(a_tile_ptrs, mask=final_a_mask, other=0.0)

        b_tile_ptrs = B_ptr + (offs_n_for_c_tile[None, :] * stride_b_n_dim +
                               a_k_indices_absolute[:, None] * stride_b_k_dim)
        mask_n_for_b_tile = offs_n_for_c_tile < N     
        final_b_mask = k_mask_for_block[:, None] & mask_n_for_b_tile[None, :] 
        b_block_fp8 = tl.load(b_tile_ptrs, mask=final_b_mask, other=0.0)

        a_block_f32 = a_block_fp8.to(tl.float32)
        b_block_f32 = b_block_fp8.to(tl.float32)

        k_scaled_idx_for_a_and_b = k_loop_start_offset // SCALE_K_CONST

        a_scale_load_ptrs = A_SCALE_ptr + (offs_m_for_c_tile * stride_a_scale_m_dim +
                                           k_scaled_idx_for_a_and_b * stride_a_scale_k_scaled_dim)
        mask_a_scale_load = (offs_m_for_c_tile < M)
        a_scales_vector = tl.load(a_scale_load_ptrs, mask=mask_a_scale_load, other=1.0)
        a_dequant = a_block_f32 * a_scales_vector[:, None]

        n_scaled_indices_for_b_block = offs_n_for_c_tile // SCALE_N_CONST
        b_scale_load_ptrs = B_SCALE_ptr + (n_scaled_indices_for_b_block * stride_b_scale_n_scaled_dim +
                                           k_scaled_idx_for_a_and_b * stride_b_scale_k_scaled_dim)
        
        N_s_ceil_for_mask = (N + SCALE_N_CONST - 1) // SCALE_N_CONST
        mask_b_scale_load = (n_scaled_indices_for_b_block < N_s_ceil_for_mask)
        
        b_scales_vector = tl.load(b_scale_load_ptrs, mask=mask_b_scale_load, other=1.0)
        b_dequant = b_block_f32 * b_scales_vector[None, :]

        accumulator += tl.dot(a_dequant, b_dequant, allow_tf32=False)

    c_tile_ptrs = C_ptr + (offs_m_for_c_tile[:, None] * stride_c_m_dim +
                           offs_n_for_c_tile[None, :] * stride_c_n_dim)
    mask_c_store = (offs_m_for_c_tile[:, None] < M) & (offs_n_for_c_tile[None, :] < N)
    tl.store(c_tile_ptrs, accumulator.to(tl.bfloat16), mask=mask_c_store)


def custom_kernel(kernel_input_data: tuple) -> torch.Tensor:
    a, b, a_scale, b_scale, c = kernel_input_data

    M, K_a_dim = a.shape
    N, K_b_dim = b.shape
    
    K = K_a_dim
    
    assert K == K_b_dim, f"K dimension mismatch: a.shape[1]={K_a_dim}, b.shape[1]={K_b_dim}"
    assert a.dtype == torch.float8_e4m3fnuz, f"a.dtype is {a.dtype}"
    assert b.dtype == torch.float8_e4m3fnuz, f"b.dtype is {b.dtype}"
    assert a_scale.dtype == torch.float32, f"a_scale.dtype is {a_scale.dtype}"
    assert b_scale.dtype == torch.float32, f"b_scale.dtype is {b_scale.dtype}"
    assert c.dtype == torch.bfloat16, f"c.dtype is {c.dtype}"
    assert c.shape == (M, N), f"c.shape {c.shape} does not match M={M}, N={N}"
    
    stride_a_m_dim = 1 
    stride_a_k_dim = M
    
    stride_b_n_dim = 1
    stride_b_k_dim = N 
    
    stride_a_scale_m_dim = 1
    stride_a_scale_k_scaled_dim = M 
    
    stride_b_scale_n_scaled_dim = 1
    stride_b_scale_k_scaled_dim = b_scale.shape[0] if b_scale.nelement() > 0 else 0
    
    stride_c_m_dim = N
    stride_c_n_dim = 1

    
    BLOCK_SIZE_M_val = 128
    BLOCK_SIZE_N_val = 128
    BLOCK_SIZE_K_val = 128 
    
    SCALE_K_CONST_val = 128
    SCALE_N_CONST_val = 128 

    grid = (triton.cdiv(M, BLOCK_SIZE_M_val), triton.cdiv(N, BLOCK_SIZE_N_val))
    
    num_warps_val = 4 
    num_stages_val = 3

    matmul_kernel[grid](
        a, b, a_scale, b_scale, c,
        M, N, K,
        stride_a_m_dim, stride_a_k_dim,
        stride_b_n_dim, stride_b_k_dim,
        stride_a_scale_m_dim, stride_a_scale_k_scaled_dim,
        stride_b_scale_n_scaled_dim, stride_b_scale_k_scaled_dim,
        stride_c_m_dim, stride_c_n_dim,
        BLOCK_SIZE_M=BLOCK_SIZE_M_val,
        BLOCK_SIZE_N=BLOCK_SIZE_N_val,
        BLOCK_SIZE_K=BLOCK_SIZE_K_val,
        SCALE_K_CONST=SCALE_K_CONST_val,
        SCALE_N_CONST=SCALE_N_CONST_val,
        num_warps=num_warps_val,
        num_stages=num_stages_val
    )
    return c
python eval.py leaderboard /tmp/tmp8oojekjy