// 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__F246

#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": 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": 32, "BLOCK_N": 16, "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": 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": 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": 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",
  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": 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",
  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": 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": 8, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 4, "num_warps": 4, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 4, "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": 4, "num_warps": 8, "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": 4, "num_warps": 8, "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": 4, "num_warps": 8, "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 = 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 = 32, .BLOCK_N = 16, .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 = 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 = 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 = 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 },
    { .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='*bf16:16';BLOCK_DMODEL=64;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[] = {
    { 0x80db8a9au, 0xf45e6b51u, 3746, 373 }, // 80db8a9af45e6b51 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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=2;num_warps=4;num_stages=1;;arch=gfx1200
    { 0x2487c132u, 0xe302e0ddu, 3746, 1251 }, // 2487c132e302e0dd = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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=3;num_warps=4;num_stages=1;;arch=gfx1200
    { 0xc3e01912u, 0x00148b16u, 3831, 664 }, // c3e0191200148b16 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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
    { 0x8d3da551u, 0x228ed2b6u, 3831, 373 }, // 8d3da551228ed2b6 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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
    { 0x3f78cbd1u, 0x5fef9d9au, 3831, 1251 }, // 3f78cbd15fef9d9a = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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
    { 0x50cd87dfu, 0xdc064ce2u, 3831, 537 }, // 50cd87dfdc064ce2 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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
    { 0x31b3402au, 0xf5076841u, 4418, 664 }, // 31b3402af5076841 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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=False;NUM_XCDS=1;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx1200
    { 0xf7240f89u, 0x2c4eaaf1u, 4503, 664 }, // f7240f892c4eaaf1 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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
    { 0x48b078d3u, 0x6ad413e7u, 4503, 3666 }, // 48b078d36ad413e7 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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=8;num_stages=1;;arch=gfx1200
    { 0x969e3eeeu, 0x10c24b6eu, 4503, 373 }, // 969e3eee10c24b6e = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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
    { 0xfc76e4e3u, 0x9f2ff0dfu, 4503, 3706 }, // fc76e4e39f2ff0df = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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=8;num_stages=1;;arch=gfx1200
    { 0x60b773f2u, 0xd9013afcu, 4503, 4338 }, // 60b773f2d9013afc = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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=8;num_stages=1;;arch=gfx1200
    { 0x0a972553u, 0xd5db72f6u, 4503, 537 }, // 0a972553d5db72f6 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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
    { 0xe967a4deu, 0x60a47b25u, 4503, 4378 }, // e967a4de60a47b25 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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=8;num_stages=1;;arch=gfx1200
    { 0xa939cd9fu, 0x9214ebd8u, 4254, 373 }, // a939cd9f9214ebd8 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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
    { 0x73e2b560u, 0x90cc1568u, 4254, 4378 }, // 73e2b56090cc1568 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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=8;num_stages=1;;arch=gfx1200
    { 0x61ddec42u, 0x959e21d2u, 3915, 664 }, // 61ddec42959e21d2 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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
    { 0x44a227a5u, 0x8b66ce7du, 3915, 3666 }, // 44a227a58b66ce7d = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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=8;num_stages=1;;arch=gfx1200
    { 0xcb30417cu, 0xe41e0e75u, 3915, 373 }, // cb30417ce41e0e75 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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
    { 0x35e9eec1u, 0x49e17137u, 3915, 4378 }, // 35e9eec149e17137 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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=4;num_warps=8;num_stages=1;;arch=gfx1200
    { 0xfe9ec862u, 0x9c79a99du, 4000, 664 }, // fe9ec8629c79a99d = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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
    { 0x621471d5u, 0x1d5916e7u, 4000, 373 }, // 621471d51d5916e7 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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
    { 0x446a79bdu, 0x8f2d3eeau, 4000, 1251 }, // 446a79bd8f2d3eea = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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
    { 0x0baa9706u, 0x532739a8u, 4000, 537 }, // 0baa9706532739a8 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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=4;num_warps=4;num_stages=1;;arch=gfx1200
    { 0xaa2c1cd4u, 0x8c443c4du, 4000, 4378 }, // aa2c1cd48c443c4d = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=64;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=4;num_warps=8;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
{{ 5, 5, 4, 0, 1, 1, 2, 1, 4, 2},
 { 0,10, 7,13,13,12, 9,15,11, 8},
 { 2,14,14,14,14,14,14,14,14,14},
 { 3,14,14,14,14,14,14,14,14,14},
 { 5,14,14,14,14,14,14,14,14,14},
 { 6,14,14,18,18,18,18,18,18,18},
 {22,16,23,16,18,18,18,18,18,18},
 {17,18,18,18,18,18,20,18,21,21},
 {24,18,18,18,18,18,18,18,18,18},
 {19,21,18,18,18,18,18,18,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__F246 "
                 "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 = "*bf16: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 = 64
// Hdim_qk = 64
// 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.bfloat16', 64, False, True, 3, 0])

// vim: set fileencoding=utf-8

