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

#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": 32, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 64, "PRE_LOAD_V": false, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 64, "PRE_LOAD_V": false, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 64, "PRE_LOAD_V": false, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 64, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 64, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 64, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 128, "BLOCK_N": 64, "PRE_LOAD_V": false, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 128, "BLOCK_N": 64, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 128, "BLOCK_N": 64, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 128, "BLOCK_N": 64, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 128, "BLOCK_N": 128, "PRE_LOAD_V": false, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 128, "BLOCK_N": 128, "PRE_LOAD_V": false, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 128, "BLOCK_N": 128, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 256, "BLOCK_N": 64, "PRE_LOAD_V": false, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 256, "BLOCK_N": 64, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 2, "GRID_CU_MULTIP": 2, "BLOCK_M": 256, "BLOCK_N": 64, "PRE_LOAD_V": true, "NUM_XCDS": 8})xyzw"
};
static const char* kernel_copts[] = {
  R"xyzw({"waves_per_eu": 1, "num_warps": 2, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 2, "num_warps": 2, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 3, "num_warps": 2, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 4, "num_warps": 2, "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": 2, "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": 2, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 1, "num_warps": 2, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 2, "num_warps": 2, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 3, "num_warps": 2, "num_stages": 1})xyzw",
  R"xyzw({"waves_per_eu": 4, "num_warps": 2, "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": 2, "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": 2, "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": 2, "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": 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": 4})xyzw",
  R"xyzw({"waves_per_eu": 1, "num_warps": 8, "num_stages": 4})xyzw",
  R"xyzw({"waves_per_eu": 2, "num_warps": 8, "num_stages": 4})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 = 32, .BLOCK_N = 16, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 16, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 16, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 16, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 16, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = false },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = false },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = false },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 64, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = false },
    { .BLOCK_M = 64, .BLOCK_N = 64, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = false },
    { .BLOCK_M = 64, .BLOCK_N = 64, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = false },
    { .BLOCK_M = 64, .BLOCK_N = 64, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 64, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 64, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 128, .BLOCK_N = 64, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = false },
    { .BLOCK_M = 128, .BLOCK_N = 64, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 128, .BLOCK_N = 64, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 128, .BLOCK_N = 64, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 128, .BLOCK_N = 128, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = false },
    { .BLOCK_M = 128, .BLOCK_N = 128, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = false },
    { .BLOCK_M = 128, .BLOCK_N = 128, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 256, .BLOCK_N = 64, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = false },
    { .BLOCK_M = 256, .BLOCK_N = 64, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .PRE_LOAD_V = true },
    { .BLOCK_M = 256, .BLOCK_N = 64, .PERSISTENT_TYPE = 2, .GRID_CU_MULTIP = 2, .NUM_XCDS = 8, .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-gfx950/flash/attn_fwd.zip)xyzw" };
#else
{ R"xyzw(amd-gfx950/flash/attn_fwd.zip)xyzw" };
#endif
constexpr std::string_view FUNC_NAME   { R"xyzw(Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0)xyzw"   };
constexpr std::string_view ARCH_NAME { R"xyzw(gfx950)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[] = {
    { 0xd0e55407u, 0xc1da23b7u, 1291, 209 }, // d0e55407c1da23b7 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;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=8;;#CO;waves_per_eu=1;num_warps=2;num_stages=1;;arch=gfx950
    { 0x37c45b6eu, 0xa8b724fcu, 1291, 333 }, // 37c45b6ea8b724fc = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;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=8;;#CO;waves_per_eu=2;num_warps=2;num_stages=1;;arch=gfx950
    { 0x9af40f17u, 0x6e448177u, 1291, 84 }, // 9af40f176e448177 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;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=8;;#CO;waves_per_eu=3;num_warps=2;num_stages=1;;arch=gfx950
    { 0x9d8504d2u, 0x34b57dd2u, 1291, 413 }, // 9d8504d234b57dd2 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;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=8;;#CO;waves_per_eu=4;num_warps=2;num_stages=1;;arch=gfx950
    { 0x4bb773e6u, 0x27e77f2eu, 1291, 537 }, // 4bb773e627e77f2e = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;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=8;;#CO;waves_per_eu=4;num_warps=4;num_stages=1;;arch=gfx950
    { 0x909c8194u, 0xdafea1d3u, 1375, 209 }, // 909c8194dafea1d3 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;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=8;;#CO;waves_per_eu=1;num_warps=2;num_stages=1;;arch=gfx950
    { 0x33dcdce5u, 0xc4b44bc4u, 1375, 664 }, // 33dcdce5c4b44bc4 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;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=8;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx950
    { 0xe9b25a93u, 0x8b6a1d73u, 1375, 413 }, // e9b25a938b6a1d73 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;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=8;;#CO;waves_per_eu=4;num_warps=2;num_stages=1;;arch=gfx950
    { 0x95bd296eu, 0xa5635949u, 1460, 209 }, // 95bd296ea5635949 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;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=8;;#CO;waves_per_eu=1;num_warps=2;num_stages=1;;arch=gfx950
    { 0xb5204f1du, 0x395fda51u, 1460, 333 }, // b5204f1d395fda51 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;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=8;;#CO;waves_per_eu=2;num_warps=2;num_stages=1;;arch=gfx950
    { 0x5d8a055au, 0xbe2211bau, 1460, 84 }, // 5d8a055abe2211ba = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;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=8;;#CO;waves_per_eu=3;num_warps=2;num_stages=1;;arch=gfx950
    { 0x63e6aafbu, 0xd8ba2b3bu, 1460, 413 }, // 63e6aafbd8ba2b3b = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;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=8;;#CO;waves_per_eu=4;num_warps=2;num_stages=1;;arch=gfx950
    { 0xa46210b1u, 0x0b1617c0u, 1544, 664 }, // a46210b10b1617c0 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=64;BLOCK_N=64;PRE_LOAD_V=False;NUM_XCDS=8;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx950
    { 0x362e2522u, 0x0e870337u, 1544, 373 }, // 362e25220e870337 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=64;BLOCK_N=64;PRE_LOAD_V=False;NUM_XCDS=8;;#CO;waves_per_eu=2;num_warps=4;num_stages=1;;arch=gfx950
    { 0x0f2ec9aeu, 0x080e0d70u, 1544, 1251 }, // 0f2ec9ae080e0d70 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=64;BLOCK_N=64;PRE_LOAD_V=False;NUM_XCDS=8;;#CO;waves_per_eu=3;num_warps=4;num_stages=1;;arch=gfx950
    { 0x0c7f82f6u, 0x6817dd2eu, 1629, 209 }, // 0c7f82f66817dd2e = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=64;BLOCK_N=64;PRE_LOAD_V=True;NUM_XCDS=8;;#CO;waves_per_eu=1;num_warps=2;num_stages=1;;arch=gfx950
    { 0xabeac64fu, 0x4a48d520u, 1629, 664 }, // abeac64f4a48d520 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=64;BLOCK_N=64;PRE_LOAD_V=True;NUM_XCDS=8;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx950
    { 0x72115b5fu, 0x02bbcd5eu, 1629, 333 }, // 72115b5f02bbcd5e = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=64;BLOCK_N=64;PRE_LOAD_V=True;NUM_XCDS=8;;#CO;waves_per_eu=2;num_warps=2;num_stages=1;;arch=gfx950
    { 0xea092a6cu, 0xe4ea3448u, 1713, 664 }, // ea092a6ce4ea3448 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=128;BLOCK_N=64;PRE_LOAD_V=False;NUM_XCDS=8;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx950
    { 0x660cb7cau, 0x079dc778u, 1799, 209 }, // 660cb7ca079dc778 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=128;BLOCK_N=64;PRE_LOAD_V=True;NUM_XCDS=8;;#CO;waves_per_eu=1;num_warps=2;num_stages=1;;arch=gfx950
    { 0x0d29f60eu, 0x7d8d256cu, 1799, 664 }, // 0d29f60e7d8d256c = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=128;BLOCK_N=64;PRE_LOAD_V=True;NUM_XCDS=8;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx950
    { 0xfcc62156u, 0x0c285984u, 1799, 373 }, // fcc621560c285984 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=128;BLOCK_N=64;PRE_LOAD_V=True;NUM_XCDS=8;;#CO;waves_per_eu=2;num_warps=4;num_stages=1;;arch=gfx950
    { 0xa4623231u, 0x30be46ccu, 1884, 664 }, // a462323130be46cc = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=128;BLOCK_N=128;PRE_LOAD_V=False;NUM_XCDS=8;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx950
    { 0x05c3f09au, 0x17606805u, 1884, 373 }, // 05c3f09a17606805 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=128;BLOCK_N=128;PRE_LOAD_V=False;NUM_XCDS=8;;#CO;waves_per_eu=2;num_warps=4;num_stages=1;;arch=gfx950
    { 0xbb1c6f6du, 0x0378ea8cu, 1971, 664 }, // bb1c6f6d0378ea8c = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=128;BLOCK_N=128;PRE_LOAD_V=True;NUM_XCDS=8;;#CO;waves_per_eu=1;num_warps=4;num_stages=1;;arch=gfx950
    { 0x3e65a2e3u, 0x6be62202u, 2057, 876 }, // 3e65a2e36be62202 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=256;BLOCK_N=64;PRE_LOAD_V=False;NUM_XCDS=8;;#CO;waves_per_eu=1;num_warps=8;num_stages=4;;arch=gfx950
    { 0x6f05626eu, 0x157e0aa4u, 2143, 876 }, // 6f05626e157e0aa4 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=256;BLOCK_N=64;PRE_LOAD_V=True;NUM_XCDS=8;;#CO;waves_per_eu=1;num_warps=8;num_stages=4;;arch=gfx950
    { 0x1dc98d51u, 0xc2284c99u, 2143, 916 }, // 1dc98d51c2284c99 = b2sum -l 64 <<< amd-gfx950/flash/attn_fwd.zip;;#F;Q='*bf16:16';BLOCK_DMODEL=32;PADDED_HEAD=True;ENABLE_DROPOUT=False;CAUSAL_TYPE=3;BIAS_TYPE=0;;#P;PERSISTENT_TYPE=2;GRID_CU_MULTIP=2;BLOCK_M=256;BLOCK_N=64;PRE_LOAD_V=True;NUM_XCDS=8;;#CO;waves_per_eu=2;num_warps=8;num_stages=4;;arch=gfx950
};

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 gfx950_mod0
{{ 1, 0, 4, 2, 0, 2, 0, 1, 0, 4},
 { 2, 8, 8,10, 8, 9,10, 9, 8,11},
 { 3, 8,14,16,13,13,16,13,12,14},
 { 8,11,14,16,15,17,17,17,17,17},
 { 7, 5,14,17,15,17,15,17,17,17},
 {10, 8,12,15,17,24,24,24,24,24},
 { 5, 9,20,17,17,24,24,24,24,24},
 {20, 6,21,24,24,24,24,24,24,24},
 {26,25,19,22,24,24,24,24,24,24},
 {27,26,18,22,23,22,22,24,24,24}}
// End of GPU gfx950_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__A0__F218 "
                 "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 = 8;
    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 = 32
// Hdim_qk = "i32"
// PADDED_HEAD = True
// ENABLE_DROPOUT = False
// dropout_p = 0
// philox_seed_ptr = 0
// philox_offset2 = 0
// 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 = ?', ['gfx950_mod0', 'torch.bfloat16', 32, False, False, 3, 0])

// vim: set fileencoding=utf-8

