#!POPCORN leaderboard amd-fp8-mm
from torch.utils.cpp_extension import load_inline
from task import input_t, output_t
import sys

import os
if "PYTORCH_ROCM_ARCH" not in os.environ:
    os.environ["PYTORCH_ROCM_ARCH"] = "gfx942:xnack-"

CPP_WRAPPER = """
void fp8_mm(torch::Tensor a, torch::Tensor b, torch::Tensor as, torch::Tensor bs, torch::Tensor c);
"""

CUDA_SRC = """
#include <hip/hip_runtime.h>
#include <hip/amd_detail/amd_hip_fp8.h>
#include <hip/amd_detail/amd_hip_bf16.h>
#include <hip/hip_cooperative_groups.h>
#include <rocwmma/rocwmma.hpp>
namespace cg = cooperative_groups;

#define K_JUMP 128
#define WMMA_M 32
#define WMMA_N 32
#define WMMA_K 16
#define NUM_WARP_X 4
#define NUM_WARP_Y 4
#define WARP_SIZE 64
#define WMMA_PLACEHOLDER 4
#define OPTION 0

__global__ void __launch_bounds__(1024, 8) custom_kernel(const __hip_fp8_e4m3_fnuz* A_ptr, const __hip_fp8_e4m3_fnuz* B_ptr, const float* As_ptr, const float* Bs_ptr, rocwmma::bfloat16_t* C_ptr, int M, int N, int K) {
    /*
    A: M x K col-maj  --- As (1x128 scaling): M x K/128 col-maj
    B: K x N row-maj  --- Bs (128x128 scaling): K/128 x N/128 row-maj
    C: M x N row-maj
    M, N is divisible by 64 (some are only divisible by 32)
    K is divisible by 128
    18.5 KD of LDS
    */
    
    __shared__ __hip_fp8_e4m3_fnuz tileA[4][4][512]; // row-major
    __shared__ __hip_fp8_e4m3_fnuz tileB[4][4][512]; // col-major, = row-major transposed
    // __shared__ __hip_fp8_e4m3_fnuz tileB[128][72]; // col-major, = row-major transposed
    int lda = 72;
    int ldb = 72;
    __shared__ float tileAs[128];
    auto frag_acc_master = rocwmma::fragment<rocwmma::accumulator, WMMA_M, WMMA_N, WMMA_K, float>();
    rocwmma::fill_fragment(frag_acc_master, 0.f);

    // int lda = M;
    // int ldb = N;
    cg::thread_block this_tb = cg::this_thread_block();
    auto group64 = cg::tiled_partition<64>(this_tb); // 1024 threads -> 64 x 16
    int t64x = group64.thread_rank();
    int t64y = group64.meta_group_rank(); 
    
    for (int kbase = 0; kbase < K; kbase += K_JUMP) {

        // Load SMEM: tileA, tileB, tileAs, tileBs
        // TRY: test tiled partition 64
        int warp_idx_x = threadIdx.x / WARP_SIZE;    
        int col_base_C = (blockIdx.x * NUM_WARP_X + warp_idx_x) * WMMA_N;
        // if (col_base_C >= N) return;    
    
        int warp_idx_y = threadIdx.y;
        int row_base_C = (blockIdx.y * NUM_WARP_Y + warp_idx_y) * WMMA_M;
        // if (row_base_C >= M) return;

        // ----------------------------- FIRST HALF --------------------------

        auto frag_load_tileA = rocwmma::fragment<rocwmma::matrix_a, WMMA_M, WMMA_N, WMMA_K, __hip_fp8_e4m3_fnuz, rocwmma::col_major>();

        rocwmma::load_matrix_sync(frag_load_tileA, A_ptr + row_base_C + M * (kbase + warp_idx_x * WMMA_K), M);
        memcpy(&tileA[warp_idx_y][warp_idx_x][t64x * 8], &frag_load_tileA.x[0], sizeof(long));

        auto frag_load_tileB = rocwmma::fragment<rocwmma::matrix_b, WMMA_M, WMMA_N, WMMA_K, __hip_fp8_e4m3_fnuz, rocwmma::row_major>();

        rocwmma::load_matrix_sync(frag_load_tileB, B_ptr + col_base_C + N * (kbase + warp_idx_y * WMMA_K), N);
        memcpy(&tileB[warp_idx_x][warp_idx_y][t64x * 8], &frag_load_tileB.x[0], sizeof(long));

        // #pragma unroll
        // for (int kmini_load = 0; kmini_load < 64; kmini_load += 16){  // 16 is num group
        //     tileB[t64x][kmini_load + t64y] = B_ptr[(kbase + kmini_load + t64y) * N + blockIdx.x * WMMA_N * NUM_WARP_X + t64x];
        //     tileB[t64x + 64][kmini_load + t64y] = B_ptr[(kbase + kmini_load + t64y) * N + blockIdx.x * WMMA_N * NUM_WARP_X + t64x + 64];
        // } // end for kmini_load < K_JUMP


        // int t32x = this_tb.thread_rank() % 32;
        // int t32y = this_tb.thread_rank() / 32;

        // __syncthreads();

        // int t32x = this_tb.thread_rank() % 32;
        // int t32y = this_tb.thread_rank() / 32;
        // int t128x = this_tb.thread_rank() % 128;
        // int t128y = this_tb.thread_rank() / 128; 


        if (t64y == 0) {
            int sn = (N + 128 - 1) / 128;    
            float tileBs = Bs_ptr[(kbase / 128) * sn + ((blockIdx.x * NUM_WARP_X * WMMA_N) / 128)];
            float2 val = reinterpret_cast<const float2*>(As_ptr + blockIdx.y * WMMA_M * NUM_WARP_Y + M * (kbase / 128))[t64x];
            reinterpret_cast<float2*>(tileAs)[t64x] = make_float2(val.x * tileBs, val.y * tileBs);
        }       

        __syncthreads();

        // MFMA on SMEM
        auto frag_acc_block = rocwmma::fragment<rocwmma::accumulator, WMMA_M, WMMA_N, WMMA_K, float>() ;
        rocwmma::fill_fragment(frag_acc_block, 0.f);

        auto frag_a = rocwmma::fragment<rocwmma::matrix_a, WMMA_M, WMMA_N, WMMA_K, __hip_fp8_e4m3_fnuz, rocwmma::row_major>();
        auto frag_b = rocwmma::fragment<rocwmma::matrix_b, WMMA_M, WMMA_N, WMMA_K, __hip_fp8_e4m3_fnuz, rocwmma::col_major>(); 

        // int warp_idx_y = threadIdx.y;
        // int warp_idx_x = threadIdx.x / WARP_SIZE;

        #pragma unroll
        for (int kblockidx = 0; kblockidx < 4; ++kblockidx) {
            memcpy(&frag_a.x[0], &tileA[warp_idx_y][kblockidx][t64x * 8], sizeof(long));
            memcpy(&frag_b.x[0], &tileB[warp_idx_x][kblockidx][t64x * 8], sizeof(long));
            // rocwmma::load_matrix_sync(frag_b, &tileB[0][0] + kblockidx * WMMA_K + ldb * (warp_idx_x * WMMA_N), ldb);
            rocwmma::mma_sync(frag_acc_block, frag_a, frag_b, frag_acc_block);
        } // end for kmini

        __syncthreads();

        // ----------------------------- SECOND HALF --------------------------
        
        // auto frag_load_tileA2 = rocwmma::fragment<rocwmma::matrix_a, WMMA_M, WMMA_N, WMMA_K, __hip_fp8_e4m3_fnuz, rocwmma::col_major>();

        rocwmma::load_matrix_sync(frag_load_tileA, A_ptr + row_base_C + M * (kbase + warp_idx_x * WMMA_K + 64), M);
        memcpy(&tileA[warp_idx_y][warp_idx_x][t64x * 8], &frag_load_tileA.x[0], sizeof(long));

        // auto frag_load_tileB2 = rocwmma::fragment<rocwmma::matrix_b, WMMA_M, WMMA_N, WMMA_K, __hip_fp8_e4m3_fnuz, rocwmma::row_major>();

        rocwmma::load_matrix_sync(frag_load_tileB, B_ptr + col_base_C + N * (kbase + warp_idx_y * WMMA_K + 64), N);
        memcpy(&tileB[warp_idx_x][warp_idx_y][t64x * 8], &frag_load_tileB.x[0], sizeof(long));

        // #pragma unroll
        // for (int kmini_load = 0; kmini_load < 64; kmini_load += 16){  // 16 is num group
        //     tileB[t64x][kmini_load + t64y] = B_ptr[(kbase + kmini_load + 64 + t64y) * N + blockIdx.x * WMMA_N * NUM_WARP_X + t64x];
        //     tileB[t64x + 64][kmini_load + t64y] = B_ptr[(kbase + kmini_load + 64 + t64y) * N + blockIdx.x * WMMA_N * NUM_WARP_X + t64x + 64];
        // } // end for kmini_load < K_JUMP

        __syncthreads();

        #pragma unroll
        for (int kblockidx = 0; kblockidx < 4; ++kblockidx) {
            memcpy(&frag_a.x[0], &tileA[warp_idx_y][kblockidx][t64x * 8], sizeof(long));
            memcpy(&frag_b.x[0], &tileB[warp_idx_x][kblockidx][t64x * 8], sizeof(long));
            // rocwmma::load_matrix_sync(frag_b, &tileB[0][0] + kblockidx * WMMA_K + ldb * (warp_idx_x * WMMA_N), ldb);
            rocwmma::mma_sync(frag_acc_block, frag_a, frag_b, frag_acc_block);
        } // end for kmini


        // __syncthreads();

        int place = warp_idx_y * WMMA_M + 4 * (t64x / 32);;

        // i = 0
        frag_acc_master.x[0]  += tileAs[place + 0] * frag_acc_block.x[0];
        frag_acc_master.x[1]  += tileAs[place + 1] * frag_acc_block.x[1];
        frag_acc_master.x[2]  += tileAs[place + 2] * frag_acc_block.x[2];
        frag_acc_master.x[3]  += tileAs[place + 3] * frag_acc_block.x[3];

        // i = 1
        frag_acc_master.x[4]  += tileAs[place + 8] * frag_acc_block.x[4];
        frag_acc_master.x[5]  += tileAs[place + 9] * frag_acc_block.x[5];
        frag_acc_master.x[6]  += tileAs[place + 10] * frag_acc_block.x[6];
        frag_acc_master.x[7]  += tileAs[place + 11] * frag_acc_block.x[7];

        // i = 2
        frag_acc_master.x[8]  += tileAs[place + 16] * frag_acc_block.x[8];
        frag_acc_master.x[9]  += tileAs[place + 17] * frag_acc_block.x[9];
        frag_acc_master.x[10] += tileAs[place + 18] * frag_acc_block.x[10];
        frag_acc_master.x[11] += tileAs[place + 19] * frag_acc_block.x[11];

        // i = 3
        frag_acc_master.x[12] += tileAs[place + 24] * frag_acc_block.x[12];
        frag_acc_master.x[13] += tileAs[place + 25] * frag_acc_block.x[13];
        frag_acc_master.x[14] += tileAs[place + 26] * frag_acc_block.x[14];
        frag_acc_master.x[15] += tileAs[place + 27] * frag_acc_block.x[15];

        __syncthreads(); // We need this synthreads()
    } // end for kbase

    int warp_idx_x = threadIdx.x / WARP_SIZE;    
    int col_base_C = (blockIdx.x * NUM_WARP_X + warp_idx_x) * WMMA_N;
    if (col_base_C >= N) return;    

    int warp_idx_y = threadIdx.y;
    int row_base_C = (blockIdx.y * NUM_WARP_Y + warp_idx_y) * WMMA_M;
    if (row_base_C >= M) return;

    // Convert fp32 to bf16 and store
    auto frag_result = rocwmma::fragment<rocwmma::accumulator, WMMA_M, WMMA_N, WMMA_PLACEHOLDER, rocwmma::bfloat16_t>(); //Placeholder frag
    
    #pragma unroll
    for (int i = 0; i < frag_acc_master.num_elements; ++i) {
        frag_result.x[i] = (rocwmma::bfloat16_t)frag_acc_master.x[i];
    }

    //int warp_idx_x = threadIdx.x / WARP_SIZE;    
    //int col_base_C = (blockIdx.x * NUM_WARP_X + warp_idx_x) * WMMA_N;  

    //int warp_idx_y = threadIdx.y;
    //int row_base_C = (blockIdx.y * NUM_WARP_Y + warp_idx_y) * WMMA_M;
      
    rocwmma::store_matrix_sync(C_ptr + row_base_C * N + col_base_C, frag_result, N, rocwmma::mem_row_major);  
}

void fp8_mm(torch::Tensor a, torch::Tensor b, torch::Tensor as, torch::Tensor bs, torch::Tensor c) {
    int M = a.size(0);
    int N = b.size(0);
    int K = a.size(1);

    int num_warp_x = NUM_WARP_X;
    int num_warp_y = NUM_WARP_Y;
    dim3 block_size((WARP_SIZE * num_warp_x), num_warp_y);
    dim3 grid_size((N + num_warp_x * WMMA_N - 1) / (num_warp_x * WMMA_N), 
                   (M + num_warp_y * WMMA_M - 1) / (num_warp_y * WMMA_M));

    custom_kernel<<<grid_size, block_size, 0, 0>>> ((__hip_fp8_e4m3_fnuz*)a.data_ptr(), (__hip_fp8_e4m3_fnuz*)b.data_ptr(), 
    as.data_ptr<float>(), bs.data_ptr<float>(), (rocwmma::bfloat16_t*)c.data_ptr(), M, N, K);
    //C10_CUDA_CHECK(cudaGetLastError());
}
"""

import os
os.environ["CXX"] = "clang++"
import torch
block_shape = 128


# big one, used when N is >= 3072
module_big = load_inline(
    name='fp8_mm',
    cpp_sources=[CPP_WRAPPER],
    cuda_sources=[CUDA_SRC],
    functions=['fp8_mm'],
    verbose=True,
    extra_cuda_cflags=["-std=c++20", "-ffast-math"],    
)

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

python submission.py
[92mSuccessfully preprocessed all matching files.[0m

/home/runner/.cache/torch_extensions/py310_cpu/fp8_mm/cuda.cu -> /home/runner/.cache/torch_extensions/py310_cpu/fp8_mm/hip.hip [ok]
/home/runner/.cache/torch_extensions/py310_cpu/fp8_mm/main.cpp -> /home/runner/.cache/torch_extensions/py310_cpu/fp8_mm/main.cpp [skipped, no changes]
Total number of unsupported CUDA function calls: 0


Total number of replaced kernel launches: 1
[1/3] clang++ -MMD -MF main.o.d -DTORCH_EXTENSION_NAME=fp8_mm -DTORCH_API_INCLUDE_EXTENSION_H -DPYBIND11_COMPILER_TYPE=\"_gcc\" -DPYBIND11_STDLIB=\"_libstdcpp\" -DPYBIND11_BUILD_ABI=\"_cxxabi1016\" -isystem /usr/local/lib/python3.10/dist-packages/torch/include -isystem /usr/local/lib/python3.10/dist-packages/torch/include/torch/csrc/api/include -isystem /usr/local/lib/python3.10/dist-packages/torch/include/THH -isystem /opt/rocm-6.3.1/include -isystem /usr/include/python3.10 -fPIC -std=c++17 -c /home/runner/.cache/torch_extensions/py310_cpu/fp8_mm/main.cpp -o main.o -D__HIP_PLATFORM_AMD__=1 -DUSE_ROCM=1 -DHIPBLAS_V2 -fPIC
[2/3] /opt/rocm-6.3.1/bin/hipcc  -DWITH_HIP -DTORCH_EXTENSION_NAME=fp8_mm -DTORCH_API_INCLUDE_EXTENSION_H -DPYBIND11_COMPILER_TYPE=\"_gcc\" -DPYBIND11_STDLIB=\"_libstdcpp\" -DPYBIND11_BUILD_ABI=\"_cxxabi1016\" -isystem /usr/local/lib/python3.10/dist-packages/torch/include -isystem /usr/local/lib/python3.10/dist-packages/torch/include/torch/csrc/api/include -isystem /usr/local/lib/python3.10/dist-packages/torch/include/THH -isystem /opt/rocm-6.3.1/include -isystem /usr/include/python3.10 -fPIC -std=c++17 -D__HIP_PLATFORM_AMD__=1 -DUSE_ROCM=1 -DHIPBLAS_V2 -fPIC -DCUDA_HAS_FP16=1 -D__HIP_NO_HALF_OPERATORS__=1 -D__HIP_NO_HALF_CONVERSIONS__=1 -DHIP_ENABLE_WARP_SYNC_BUILTINS=1 --offload-arch=gfx942:xnack- -fno-gpu-rdc -std=c++20 -ffast-math -c /home/runner/.cache/torch_extensions/py310_cpu/fp8_mm/hip.hip -o hip.cuda.o 
[3/3] clang++ main.o hip.cuda.o -shared -L/usr/local/lib/python3.10/dist-packages/torch/lib -lc10 -lc10_hip -ltorch_cpu -ltorch_hip -ltorch -ltorch_python -L/opt/rocm-6.3.1/lib -lamdhip64 -o fp8_mm.so

python eval.py benchmark /tmp/tmpxi_ntnnr
[92mSuccessfully preprocessed all matching files.[0m

/home/runner/.cache/torch_extensions/py310_cpu/fp8_mm/cuda.cu -> /home/runner/.cache/torch_extensions/py310_cpu/fp8_mm/hip.hip [skipped, already hipified]
/home/runner/.cache/torch_extensions/py310_cpu/fp8_mm/main.cpp -> /home/runner/.cache/torch_extensions/py310_cpu/fp8_mm/main.cpp [skipped, no changes]
Total number of unsupported CUDA function calls: 0


Total number of replaced kernel launches: 1
ninja: no work to do.
