import torch
import triton
import triton.language as tl

@triton.jit
def matmul_kernel_fp8dot(
    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_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_dot: tl.constexpr,   
    SCALE_K_GRANULARITY: 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_scale_block_outer_start in range(0, K, SCALE_K_GRANULARITY):
        current_k_scale_idx = k_scale_block_outer_start // SCALE_K_GRANULARITY
        
        a_scale_load_ptrs = A_SCALE_ptr + (offs_m_for_c_tile * stride_a_scale_m_dim +
                                           current_k_scale_idx * stride_a_scale_k_scaled_dim)
        mask_a_scale_load = (offs_m_for_c_tile < M)
        a_scales_m_vector = tl.load(a_scale_load_ptrs, mask=mask_a_scale_load, other=1.0)

        b_scale_n_block_idx = pid_n 
        b_scale_scalar_ptr = B_SCALE_ptr + (b_scale_n_block_idx * stride_b_scale_n_dim +
                                            current_k_scale_idx * stride_b_scale_k_scaled_dim)
        b_scale_val = tl.load(b_scale_scalar_ptr) 

        k_dot_loop_end_for_this_scale_block = tl.minimum(k_scale_block_outer_start + SCALE_K_GRANULARITY, K)
        for k_dot_loop_inner_start in range(k_scale_block_outer_start, k_dot_loop_end_for_this_scale_block, BLOCK_SIZE_K_dot):
            a_k_indices_range_dot = tl.arange(0, BLOCK_SIZE_K_dot)
            a_k_indices_absolute_dot = k_dot_loop_inner_start + a_k_indices_range_dot

            a_tile_ptrs = A_ptr + (offs_m_for_c_tile[:, None] * stride_a_m_dim +
                                   a_k_indices_absolute_dot[None, :] * stride_a_k_dim)
            mask_a_data_load = (offs_m_for_c_tile[:, None] < M) & (a_k_indices_absolute_dot[None, :] < K)
            a_block_fp8 = tl.load(a_tile_ptrs, mask=mask_a_data_load, other=0.0)

            b_tile_ptrs = B_ptr + (offs_n_for_c_tile[None, :] * stride_b_n_dim + 
                                   a_k_indices_absolute_dot[:, None] * stride_b_k_dim) 
            mask_b_data_load = (a_k_indices_absolute_dot[:, None] < K) & (offs_n_for_c_tile[None, :] < N)
            b_block_fp8_transposed = tl.load(b_tile_ptrs, mask=mask_b_data_load, other=0.0)
            
            dot_output_f32 = tl.dot(a_block_fp8, b_block_fp8_transposed, out_dtype=tl.float32, allow_tf32=False)
            
            scaled_dot_output = dot_output_f32 * a_scales_m_vector[:, None] * b_scale_val
            accumulator += scaled_dot_output

    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_orig = a.shape 
    N, K_check = b.shape
    K = K_orig 
    
    assert K == K_check, f"K dimension mismatch: a.shape[1]={K_orig}, b.shape[1]={K_check}"
    assert a.dtype == torch.float8_e4m3fnuz
    assert b.dtype == torch.float8_e4m3fnuz
    assert a_scale.dtype == torch.float32
    assert b_scale.dtype == torch.float32
    assert c.dtype == torch.bfloat16
    assert c.shape == (M, 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_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_dot_val = 64        
    SCALE_K_GRANULARITY_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_fp8dot[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_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_dot=BLOCK_SIZE_K_dot_val,
        SCALE_K_GRANULARITY=SCALE_K_GRANULARITY_val,
        num_warps=num_warps_val,
        num_stages=num_stages_val
    )
    return c
python eval.py leaderboard /tmp/tmpeea1vs3h