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

#include "../shim.attn_fwd.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_attn_fwd__A10__F6

#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({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 16, "BLOCK_N": 16, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 16, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 16, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 16, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 16, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 16, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 16, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw"
};
static const char* kernel_copts[] = {
  R"xyzw({"waves_per_eu": 1, "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": 3, "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": 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": 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"
};
#endif

struct PerfFields {
  int16_t BLOCK_M;
    int16_t BLOCK_N;
    int8_t  PERSISTENT_TYPE;
    int8_t  GRID_CU_MULTIP;
    int8_t  NUM_XCDS;
    bool    PRE_LOAD_V;
};

static PerfFields image_perf_list [] = {
    { .BLOCK_M = 16, .BLOCK_N = 16, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 16, .BLOCK_N = 16, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 16, .BLOCK_N = 16, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 16, .BLOCK_N = 16, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 16, .BLOCK_N = 16, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 16, .BLOCK_N = 16, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 16, .BLOCK_N = 16, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 16, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 16, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true }
};

// 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/attn_fwd.zip)xyzw" };
#else
{ R"xyzw(amd-gfx120x/flash/attn_fwd.zip)xyzw" };
#endif
constexpr std::string_view FUNC_NAME   { R"xyzw(Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0)xyzw"   };
constexpr std::string_view ARCH_NAME { R"xyzw(gfx1200)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[] = {
    { 0xf83ce4d9u, 0xf13194bdu, 3746, 664 }, // f83ce4d9f13194bd = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=16;BLOCK_N=16;PRE_LOAD_V=False;NUM_XCDS=1;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx1200
    { 0xf102ec1du, 0x284fcf0bu, 3831, 664 }, // f102ec1d284fcf0b = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=16;BLOCK_N=16;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx1200
    { 0x856c9d16u, 0xb97effd2u, 3831, 3666 }, // 856c9d16b97effd2 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=16;BLOCK_N=16;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=1;num_warps=8;num_stages=1;;arch=gfx1200
    { 0xf316a6b6u, 0x1f0701f2u, 3831, 373 }, // f316a6b61f0701f2 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=16;BLOCK_N=16;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=2;num_warps=4;num_stages=1;;arch=gfx1200
    { 0x36d7e055u, 0xcc1c64ceu, 3831, 3706 }, // 36d7e055cc1c64ce = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=16;BLOCK_N=16;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=2;num_warps=8;num_stages=1;;arch=gfx1200
    { 0x24249f01u, 0x31730745u, 3831, 1251 }, // 24249f0131730745 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=16;BLOCK_N=16;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=3;num_warps=4;num_stages=1;;arch=gfx1200
    { 0xa9db943eu, 0x99a62343u, 3831, 537 }, // a9db943e99a62343 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=16;BLOCK_N=16;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=4;num_warps=4;num_stages=1;;arch=gfx1200
    { 0x490e42efu, 0xe95b3109u, 4588, 664 }, // 490e42efe95b3109 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=32;BLOCK_N=16;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx1200
    { 0x6f7b290du, 0x2bc57590u, 4588, 1251 }, // 6f7b290d2bc57590 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=32;BLOCK_N=16;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=3;num_warps=4;num_stages=1;;arch=gfx1200
    { 0xdf63965eu, 0xda0292f7u, 4503, 664 }, // df63965eda0292f7 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=32;BLOCK_N=32;PRE_LOAD_V=False;NUM_XCDS=1;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx1200
    { 0xdc4f16afu, 0x00e3c087u, 4503, 373 }, // dc4f16af00e3c087 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=32;BLOCK_N=32;PRE_LOAD_V=False;NUM_XCDS=1;;#CO;waves_per_eu=2;num_warps=4;num_stages=1;;arch=gfx1200
    { 0x3bb410a0u, 0x7ac1d83fu, 4503, 1251 }, // 3bb410a07ac1d83f = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=32;BLOCK_N=32;PRE_LOAD_V=False;NUM_XCDS=1;;#CO;waves_per_eu=3;num_warps=4;num_stages=1;;arch=gfx1200
    { 0x5c80d6abu, 0xd5f92dccu, 4503, 537 }, // 5c80d6abd5f92dcc = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=32;BLOCK_N=32;PRE_LOAD_V=False;NUM_XCDS=1;;#CO;waves_per_eu=4;num_warps=4;num_stages=1;;arch=gfx1200
    { 0x3ed11146u, 0x5b29b880u, 4254, 664 }, // 3ed111465b29b880 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=32;BLOCK_N=32;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx1200
    { 0xeb41324cu, 0x13695c56u, 4254, 373 }, // eb41324c13695c56 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=32;BLOCK_N=32;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=2;num_warps=4;num_stages=1;;arch=gfx1200
    { 0x819d4bbeu, 0xef82a401u, 4254, 537 }, // 819d4bbeef82a401 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=32;BLOCK_N=32;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=4;num_warps=4;num_stages=1;;arch=gfx1200
    { 0xab4a2e9bu, 0x615e2684u, 3915, 664 }, // ab4a2e9b615e2684 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=64;BLOCK_N=32;PRE_LOAD_V=False;NUM_XCDS=1;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx1200
    { 0x0ebe2a69u, 0x4e5bd209u, 3915, 373 }, // 0ebe2a694e5bd209 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=64;BLOCK_N=32;PRE_LOAD_V=False;NUM_XCDS=1;;#CO;waves_per_eu=2;num_warps=4;num_stages=1;;arch=gfx1200
    { 0xa821b7ebu, 0x4dfea8c9u, 3915, 1251 }, // a821b7eb4dfea8c9 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=64;BLOCK_N=32;PRE_LOAD_V=False;NUM_XCDS=1;;#CO;waves_per_eu=3;num_warps=4;num_stages=1;;arch=gfx1200
    { 0x6989e12cu, 0x742d8fdfu, 4000, 664 }, // 6989e12c742d8fdf = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=64;BLOCK_N=32;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx1200
    { 0xdca3e815u, 0x0f99be32u, 4000, 373 }, // dca3e8150f99be32 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=64;BLOCK_N=32;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=2;num_warps=4;num_stages=1;;arch=gfx1200
    { 0x88ebc31fu, 0xb85479a0u, 4000, 1251 }, // 88ebc31fb85479a0 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=16;PADDED_HEAD=False;ENABLE_DROPOUT=True;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=64;BLOCK_N=32;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=3;num_warps=4;num_stages=1;;arch=gfx1200
};

static constexpr int kTotalNumKernels = ARRAY_SIZE(meta_list);

using AOTRITON_NS::v3::flash::autotune::attn_fwd_packed_string;

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

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

}; // End of anonymous namespace

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

// using AOTRITON_NS::v2::flash::AttnFwdContext;

// 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(AttnFwdContext& context, int mod_number) {
#if AOTRITON_BUILD_FOR_TUNING
    constexpr int kLutInTuningModeAlwaysReturnZero = 0;
    AOTRITON_LOG(LOG_DEBUG,
                 "Autotune_attn_fwd__A10__F6 "
                 "kTotalNumKernels = %d _has_preferred_kernel = %d",
                 kTotalNumKernels, context._has_preferred_kernel);
#ifndef NDEBUG
    assert(attn_fwd__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 = attn_fwd__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.PERSISTENT_TYPE = perf.PERSISTENT_TYPE;
    context.GRID_CU_MULTIP = perf.GRID_CU_MULTIP;
    context.BLOCK_M = perf.BLOCK_M;
    context.BLOCK_N = perf.BLOCK_N;
    context.PRE_LOAD_V = perf.PRE_LOAD_V;
    context.NUM_XCDS = perf.NUM_XCDS;
    return kernel_index;
}

#undef CURRENT_ENTRY_PUBLIC
}

// Human-readable Signature 
// Q = "*fp16:16"
// Sm_scale = "fp32"
// L = "*fp32:16"
// Q_descale = 0
// stride_bz = 0
// stride_bh = 0
// stride_bm = 0
// stride_az = 0
// Num_head_q = "i32"
// cu_seqlens_q = "*i32:16"
// BLOCK_DMODEL = 16
// Hdim_qk = 16
// PADDED_HEAD = False
// ENABLE_DROPOUT = True
// dropout_p = "fp32"
// philox_seed_ptr = "*u64"
// philox_offset2 = "u64"
// RETURN_ENCODED_SOFTMAX = False
// CAUSAL_TYPE = 3
// Window_left = "i32"
// Window_right = "i32"
// BIAS_TYPE = 0
// USE_ALIBI = False
// INT8 = False
// persistent_atomic_counter = "*i32"
// Num_CU = "i32"

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

// vim: set fileencoding=utf-8

