// Copyright © 2023-2025 Advanced Micro Devices, Inc.
// SPDX-License-Identifier: MIT

#include "../shim.bwd_kernel_fuse.h"
#include <aotriton/_internal/triton_kernel.h>
#include <aotriton/_internal/kernel_cluster.h>
#include <aotriton/_internal/log.h>
#include <aotriton/cpp_tune.h>
#include <assert.h>
#include <string_view>

#define CURRENT_ENTRY_PUBLIC Autotune_bwd_kernel_fuse__A9__F236

#define ARRAY_SIZE(array)  (sizeof(array) / sizeof(array[0]))

namespace { // Anonymous namespace

using namespace std::literals::string_view_literals;

#if AOTRITON_BUILD_FOR_TUNING
// PSels and Copts in JSON String
static const char* kernel_psels[] = {
  R"xyzw({"BLOCK_M": 16, "BLOCK_N": 16})xyzw",
  R"xyzw({"BLOCK_M": 16, "BLOCK_N": 16})xyzw",
  R"xyzw({"BLOCK_M": 16, "BLOCK_N": 16})xyzw",
  R"xyzw({"BLOCK_M": 16, "BLOCK_N": 16})xyzw",
  R"xyzw({"BLOCK_M": 32, "BLOCK_N": 16})xyzw",
  R"xyzw({"BLOCK_M": 32, "BLOCK_N": 16})xyzw",
  R"xyzw({"BLOCK_M": 32, "BLOCK_N": 16})xyzw",
  R"xyzw({"BLOCK_M": 32, "BLOCK_N": 16})xyzw",
  R"xyzw({"BLOCK_M": 32, "BLOCK_N": 32})xyzw",
  R"xyzw({"BLOCK_M": 32, "BLOCK_N": 32})xyzw",
  R"xyzw({"BLOCK_M": 64, "BLOCK_N": 16})xyzw",
  R"xyzw({"BLOCK_M": 64, "BLOCK_N": 16})xyzw",
  R"xyzw({"BLOCK_M": 64, "BLOCK_N": 16})xyzw",
  R"xyzw({"BLOCK_M": 64, "BLOCK_N": 16})xyzw",
  R"xyzw({"BLOCK_M": 64, "BLOCK_N": 16})xyzw",
  R"xyzw({"BLOCK_M": 64, "BLOCK_N": 16})xyzw",
  R"xyzw({"BLOCK_M": 64, "BLOCK_N": 32})xyzw",
  R"xyzw({"BLOCK_M": 64, "BLOCK_N": 32})xyzw",
  R"xyzw({"BLOCK_M": 64, "BLOCK_N": 64})xyzw",
  R"xyzw({"BLOCK_M": 64, "BLOCK_N": 64})xyzw",
  R"xyzw({"BLOCK_M": 64, "BLOCK_N": 64})xyzw"
};
static const char* kernel_copts[] = {
  R"xyzw({"waves_per_eu": 1, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 2, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 3, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 4, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 1, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 2, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 3, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 4, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 1, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 2, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 1, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 1, "num_warps": 8, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 2, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 2, "num_warps": 8, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 3, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 4, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 1, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 4, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 1, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 3, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 4, "num_warps": 4, "num_stages": 1})xyzw"
};
#endif

struct PerfFields {
  int16_t BLOCK_M;
    int16_t BLOCK_N;
};

static PerfFields image_perf_list [] = {
    { .BLOCK_M = 16, .BLOCK_N = 16 },
    { .BLOCK_M = 16, .BLOCK_N = 16 },
    { .BLOCK_M = 16, .BLOCK_N = 16 },
    { .BLOCK_M = 16, .BLOCK_N = 16 },
    { .BLOCK_M = 32, .BLOCK_N = 16 },
    { .BLOCK_M = 32, .BLOCK_N = 16 },
    { .BLOCK_M = 32, .BLOCK_N = 16 },
    { .BLOCK_M = 32, .BLOCK_N = 16 },
    { .BLOCK_M = 32, .BLOCK_N = 32 },
    { .BLOCK_M = 32, .BLOCK_N = 32 },
    { .BLOCK_M = 64, .BLOCK_N = 16 },
    { .BLOCK_M = 64, .BLOCK_N = 16 },
    { .BLOCK_M = 64, .BLOCK_N = 16 },
    { .BLOCK_M = 64, .BLOCK_N = 16 },
    { .BLOCK_M = 64, .BLOCK_N = 16 },
    { .BLOCK_M = 64, .BLOCK_N = 16 },
    { .BLOCK_M = 64, .BLOCK_N = 32 },
    { .BLOCK_M = 64, .BLOCK_N = 32 },
    { .BLOCK_M = 64, .BLOCK_N = 64 },
    { .BLOCK_M = 64, .BLOCK_N = 64 },
    { .BLOCK_M = 64, .BLOCK_N = 64 }
};

// u8R generates char8_t which is poorly supported almost everywhere.
// TODO: deduplicate FLATZIP_PATH via shim.cc packed_string
constexpr pstring_view FLATZIP_PATH
#if defined(_WIN32)
{ LR"xyzw(amd-gfx120x/flash/bwd_kernel_fuse.zip)xyzw" };
#else
{ R"xyzw(amd-gfx120x/flash/bwd_kernel_fuse.zip)xyzw" };
#endif
constexpr std::string_view FUNC_NAME   { R"xyzw(Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0)xyzw"   };
constexpr std::string_view ARCH_NAME { R"xyzw(gfx1201)xyzw" };

// Checksum can be confirmed with `echo -n '<string>' | b2sum -l 64`
// For example:
//   $ echo -n 'amd-gfx110x/flash/attn_fwd/FONLY__^bf16@16,128,False,False,0,0___gfx1100__P__32_32_0_2_False__CO__wave3_warp2_stg1-Gpu-gfx1100' | b2sum -l 64
//   c4b51ee645d79580  -
static AOTRITON_NS::TritonKernelCompactMeta meta_list[] = {
    { 0x78c61dc3u, 0xe1975304u, 328, 350 }, // 78c61dc3e1975304 = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=16;BLOCK_N=16;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx1201
    { 0xdb35fbc3u, 0x6d085f4au, 328, 164 }, // db35fbc36d085f4a = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=16;BLOCK_N=16;;#CO;waves_per_eu=2;num_warps=4;num_stages=1;;arch=gfx1201
    { 0xebcf602bu, 0xcce155fbu, 328, 244 }, // ebcf602bcce155fb = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=16;BLOCK_N=16;;#CO;waves_per_eu=3;num_warps=4;num_stages=1;;arch=gfx1201
    { 0x43b5eb13u, 0x1c79cefeu, 328, 390 }, // 43b5eb131c79cefe = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=16;BLOCK_N=16;;#CO;waves_per_eu=4;num_warps=4;num_stages=1;;arch=gfx1201
    { 0x5dd90d12u, 0xde909d4cu, 0, 350 }, // 5dd90d12de909d4c = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=32;BLOCK_N=16;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx1201
    { 0x9a8f31bcu, 0x14d38c21u, 0, 164 }, // 9a8f31bc14d38c21 = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=32;BLOCK_N=16;;#CO;waves_per_eu=2;num_warps=4;num_stages=1;;arch=gfx1201
    { 0x018af777u, 0x9d0657cau, 0, 244 }, // 018af7779d0657ca = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=32;BLOCK_N=16;;#CO;waves_per_eu=3;num_warps=4;num_stages=1;;arch=gfx1201
    { 0xd6211ee0u, 0xaf850471u, 0, 390 }, // d6211ee0af850471 = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=32;BLOCK_N=16;;#CO;waves_per_eu=4;num_warps=4;num_stages=1;;arch=gfx1201
    { 0xb4cc8bc6u, 0xd4cc2fc8u, 430, 350 }, // b4cc8bc6d4cc2fc8 = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=32;BLOCK_N=32;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx1201
    { 0xe468a26au, 0xdae574d0u, 430, 164 }, // e468a26adae574d0 = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=32;BLOCK_N=32;;#CO;waves_per_eu=2;num_warps=4;num_stages=1;;arch=gfx1201
    { 0x44b9faacu, 0xfb52cdd5u, 102, 350 }, // 44b9faacfb52cdd5 = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=64;BLOCK_N=16;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx1201
    { 0x8fd43347u, 0x643019c3u, 102, 572 }, // 8fd43347643019c3 = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=64;BLOCK_N=16;;#CO;waves_per_eu=1;num_warps=8;num_stages=1;;arch=gfx1201
    { 0x00b8fe69u, 0x990e800bu, 102, 164 }, // 00b8fe69990e800b = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=64;BLOCK_N=16;;#CO;waves_per_eu=2;num_warps=4;num_stages=1;;arch=gfx1201
    { 0x5ccfab10u, 0x34810142u, 102, 492 }, // 5ccfab1034810142 = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=64;BLOCK_N=16;;#CO;waves_per_eu=2;num_warps=8;num_stages=1;;arch=gfx1201
    { 0x47be7c56u, 0xd9a16669u, 102, 244 }, // 47be7c56d9a16669 = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=64;BLOCK_N=16;;#CO;waves_per_eu=3;num_warps=4;num_stages=1;;arch=gfx1201
    { 0xfbe4626du, 0x6e64f586u, 102, 390 }, // fbe4626d6e64f586 = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=64;BLOCK_N=16;;#CO;waves_per_eu=4;num_warps=4;num_stages=1;;arch=gfx1201
    { 0x27b08d1fu, 0x793f8e52u, 284, 350 }, // 27b08d1f793f8e52 = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=64;BLOCK_N=32;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx1201
    { 0xd147fd70u, 0xf0f88e2du, 284, 390 }, // d147fd70f0f88e2d = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=64;BLOCK_N=32;;#CO;waves_per_eu=4;num_warps=4;num_stages=1;;arch=gfx1201
    { 0xc71260beu, 0xd602e07au, 306, 350 }, // c71260bed602e07a = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=64;BLOCK_N=64;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx1201
    { 0x50bdaaeeu, 0x1f1618ecu, 306, 244 }, // 50bdaaee1f1618ec = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=64;BLOCK_N=64;;#CO;waves_per_eu=3;num_warps=4;num_stages=1;;arch=gfx1201
    { 0xf8a1f91cu, 0xd0aa8227u, 306, 390 }, // f8a1f91cd0aa8227 = b2sum -l 64 <<< amd-gfx120x/flash/bwd_kernel_fuse.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;CAUSAL_TYPE=3;ENABLE_DROPOUT=True;PADDED_HEAD=False;BIAS_TYPE=0;;#P;BLOCK_M=64;BLOCK_N=64;;#CO;waves_per_eu=4;num_warps=4;num_stages=1;;arch=gfx1201
};

static constexpr int kTotalNumKernels = ARRAY_SIZE(meta_list);

using AOTRITON_NS::v3::flash::autotune::bwd_kernel_fuse_packed_string;

static AOTRITON_NS::TritonKernelCluster<kTotalNumKernels>
kernel_cluster(meta_list,
               bwd_kernel_fuse_packed_string);

static int8_t lut[1][10][10] =
{
// GPU gfx1201_mod0
{{ 0, 3, 0, 0, 0, 1, 1, 0, 1, 2},
 { 0, 0, 2, 5, 3, 0, 0, 2, 1, 1},
 { 3, 1, 5, 4, 4, 4, 1, 0, 1, 0},
 { 5, 5, 4, 5, 5, 5, 6, 3, 0, 1},
 { 5, 5, 7, 6, 9,10, 6,12, 5, 1},
 {10,10,10,12,10, 8, 8, 9, 8, 8},
 {13,11,11,12, 9, 8,16, 8, 9, 8},
 {10,10,12,12,17,17,16,16,16,16},
 {14,14,15,12,12,17,17,16,18,20},
 {14,15,12,15,14,16,16,16,18,19}}
// End of GPU gfx1201_mod0
}
;

}; // End of anonymous namespace

namespace AOTRITON_NS::v3::flash::autotune {

// using AOTRITON_NS::v2::flash::BwdKernelFuseContext;

// Returns the selected kernel_index, or -1 when no valid kernel was selected.
// The kernel_index is logged by the caller (lookup_optimal in shim.cc) so the
// AOTRITON_LOG machinery is not duplicated across every generated autotune TU.
int CURRENT_ENTRY_PUBLIC(BwdKernelFuseContext& context, int mod_number) {
#if AOTRITON_BUILD_FOR_TUNING
    constexpr int kLutInTuningModeAlwaysReturnZero = 0;
    AOTRITON_LOG(LOG_DEBUG,
                 "Autotune_bwd_kernel_fuse__A9__F236 "
                 "kTotalNumKernels = %d _has_preferred_kernel = %d",
                 kTotalNumKernels, context._has_preferred_kernel);
#ifndef NDEBUG
    assert(bwd_kernel_fuse__lut_lambda__0(*context.params, mod_number, lut) == kLutInTuningModeAlwaysReturnZero);
#endif
    int preferred = context._has_preferred_kernel;
    int kernel_index = preferred < 0 ? kLutInTuningModeAlwaysReturnZero : preferred;
#else
    auto kernel_index = bwd_kernel_fuse__lut_lambda__0(*context.params, mod_number, lut);
#endif
    if (kernel_index < 0 || kernel_index >= kTotalNumKernels) {
        return -1;
    }
    context.kernel_on_device = kernel_cluster.get(kernel_index);
    context.pp_args_index = 5;
    context.flatzip_path = FLATZIP_PATH;
    // aks2_entry IS the func_name (supplied by f.unified_signature)
    context.aks2_entry   = FUNC_NAME;
    context.func_name = FUNC_NAME;
    context.arch_name = ARCH_NAME;
#if AOTRITON_BUILD_FOR_TUNING
    context._total_number_of_kernels = kTotalNumKernels;
    context._preferred_kernel_psels = kernel_psels[kernel_index];
    context._preferred_kernel_copts = kernel_copts[kernel_index];
#endif
    const auto& perf = image_perf_list[kernel_index];
    context.BLOCK_M = perf.BLOCK_M;
    context.BLOCK_N = perf.BLOCK_N;
    return kernel_index;
}

#undef CURRENT_ENTRY_PUBLIC
}

// Human-readable Signature 
// Q = "*bf16:16"
// sm_scale = "fp32"
// L = "*fp32:16"
// stride_bz = 0
// stride_bh = 0
// stride_bm = 0
// stride_dbz = 0
// stride_dbh = 0
// stride_dbm = 0
// num_head_q = "i32"
// cu_seqlens_q = "*i32:16"
// num_seqlens = "i32"
// dropout_p = "fp32"
// philox_seed_ptr = "*u64"
// philox_offset2 = "u64"
// Window_left = "i32"
// Window_right = "i32"
// BLOCK_DMODEL = 64
// CAUSAL_TYPE = 3
// ENABLE_DROPOUT = True
// PADDED_HEAD = False
// BIAS_TYPE = 0

// ('SELECT * FROM FLASH$bwd_kernel_fuse WHERE gpu IN (?) AND inputs$Q_dtype = ? AND inputs$BLOCK_DMODEL = ? AND inputs$CAUSAL_TYPE = ? AND inputs$ENABLE_DROPOUT = ? AND inputs$PADDED_HEAD = ? AND inputs$BIAS_TYPE = ?', ['gfx1201_mod0', 'torch.bfloat16', 64, 3, True, False, 0])

// vim: set fileencoding=utf-8

