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

#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": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 16, "BLOCK_N": 16, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 16, "BLOCK_N": 16, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 16, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 16, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 16, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 16, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 16, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 16, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 32, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": false, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "GRID_CU_MULTIP": 2, "BLOCK_M": 64, "BLOCK_N": 32, "PRE_LOAD_V": true, "NUM_XCDS": 1})xyzw",
  R"xyzw({"PERSISTENT_TYPE": 0, "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": 2, "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": 3, "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": 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": 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": 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": 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": 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": 1, "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": 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": 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": 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"
};
#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 = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 16, .BLOCK_N = 16, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 16, .BLOCK_N = 16, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 16, .BLOCK_N = 16, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 16, .BLOCK_N = 16, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 16, .BLOCK_N = 16, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 16, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 32, .BLOCK_N = 16, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 16, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 16, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 16, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 32, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = false },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .GRID_CU_MULTIP = 2, .NUM_XCDS = 1, .PRE_LOAD_V = true },
    { .BLOCK_M = 64, .BLOCK_N = 32, .PERSISTENT_TYPE = 0, .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=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1)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[] = {
    { 0x5f47ee2bu, 0x1e1f919du, 4084, 664 }, // 5f47ee2b1e1f919d = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0xf8c89a78u, 0xe59c1618u, 4084, 3706 }, // f8c89a78e59c1618 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;GRID_CU_MULTIP=2;BLOCK_M=16;BLOCK_N=16;PRE_LOAD_V=False;NUM_XCDS=1;;#CO;waves_per_eu=2;num_warps=8;num_stages=1;;arch=gfx1200
    { 0x4dae2779u, 0xb4be91f1u, 3582, 664 }, // 4dae2779b4be91f1 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0x2a66b125u, 0xf5af6226u, 3582, 373 }, // 2a66b125f5af6226 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0xc38f9df3u, 0x17538cebu, 3582, 1251 }, // c38f9df317538ceb = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0x2acb0221u, 0x3aee62c5u, 3582, 537 }, // 2acb02213aee62c5 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0x8936bf59u, 0xcb817c15u, 4169, 1251 }, // 8936bf59cb817c15 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;GRID_CU_MULTIP=2;BLOCK_M=32;BLOCK_N=16;PRE_LOAD_V=False;NUM_XCDS=1;;#CO;waves_per_eu=3;num_warps=4;num_stages=1;;arch=gfx1200
    { 0x69c635f7u, 0xdb7a46c8u, 3160, 373 }, // 69c635f7db7a46c8 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;GRID_CU_MULTIP=2;BLOCK_M=32;BLOCK_N=16;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=2;num_warps=4;num_stages=1;;arch=gfx1200
    { 0xf30ca29bu, 0x5ecb59f3u, 3160, 1251 }, // f30ca29b5ecb59f3 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0xee722056u, 0x983176c4u, 3160, 4338 }, // ee722056983176c4 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;GRID_CU_MULTIP=2;BLOCK_M=32;BLOCK_N=16;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=3;num_warps=8;num_stages=1;;arch=gfx1200
    { 0xf8f13e1cu, 0xca811b4cu, 3160, 537 }, // f8f13e1cca811b4c = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;GRID_CU_MULTIP=2;BLOCK_M=32;BLOCK_N=16;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=4;num_warps=4;num_stages=1;;arch=gfx1200
    { 0xbac1065cu, 0xc94c8564u, 3244, 664 }, // bac1065cc94c8564 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0xc5ea56e0u, 0x65d0415au, 3244, 3666 }, // c5ea56e065d0415a = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0x13ee0637u, 0x8f069bbdu, 3244, 3706 }, // 13ee06378f069bbd = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0xb660269eu, 0xda23b7bfu, 3244, 4338 }, // b660269eda23b7bf = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0x495c5feeu, 0xc375fbaau, 3244, 4378 }, // 495c5feec375fbaa = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0xd28631e5u, 0x05c024f1u, 3329, 664 }, // d28631e505c024f1 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0x1ee1cff4u, 0x331ed50eu, 3329, 3666 }, // 1ee1cff4331ed50e = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;GRID_CU_MULTIP=2;BLOCK_M=32;BLOCK_N=32;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=1;num_warps=8;num_stages=1;;arch=gfx1200
    { 0x6ed25261u, 0x051dd0a3u, 3329, 3706 }, // 6ed25261051dd0a3 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;GRID_CU_MULTIP=2;BLOCK_M=32;BLOCK_N=32;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=2;num_warps=8;num_stages=1;;arch=gfx1200
    { 0x5f42b3bdu, 0x5d7da24fu, 3329, 4338 }, // 5f42b3bd5d7da24f = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;GRID_CU_MULTIP=2;BLOCK_M=32;BLOCK_N=32;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=3;num_warps=8;num_stages=1;;arch=gfx1200
    { 0x4ea3f044u, 0x6600e7c9u, 3329, 537 }, // 4ea3f0446600e7c9 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0xd3b2d624u, 0x5bca1f80u, 3329, 4378 }, // d3b2d6245bca1f80 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0x476c1496u, 0xe357eba5u, 3413, 3666 }, // 476c1496e357eba5 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0xc17142e4u, 0x4c8b6308u, 3413, 537 }, // c17142e44c8b6308 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;GRID_CU_MULTIP=2;BLOCK_M=64;BLOCK_N=32;PRE_LOAD_V=False;NUM_XCDS=1;;#CO;waves_per_eu=4;num_warps=4;num_stages=1;;arch=gfx1200
    { 0xf37d0594u, 0x89268485u, 3498, 664 }, // f37d059489268485 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0x507c67a3u, 0x03115c46u, 3498, 373 }, // 507c67a303115c46 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0x7eed7ffdu, 0xc78ad8bfu, 3498, 3706 }, // 7eed7ffdc78ad8bf = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;GRID_CU_MULTIP=2;BLOCK_M=64;BLOCK_N=32;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=2;num_warps=8;num_stages=1;;arch=gfx1200
    { 0x614df53du, 0x615a8ea4u, 3498, 1251 }, // 614df53d615a8ea4 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0x866c499du, 0xc6b3444eu, 3498, 4338 }, // 866c499dc6b3444e = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;GRID_CU_MULTIP=2;BLOCK_M=64;BLOCK_N=32;PRE_LOAD_V=True;NUM_XCDS=1;;#CO;waves_per_eu=3;num_warps=8;num_stages=1;;arch=gfx1200
    { 0xffd9a330u, 0xd76f6e42u, 3498, 537 }, // ffd9a330d76f6e42 = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
    { 0xc2cbe4a2u, 0x0e8a174du, 3498, 4378 }, // c2cbe4a20e8a174d = b2sum -l 64 <<< amd-gfx120x/flash/attn_fwd.zip;;#F;Q='*fp16:16';BLOCK_DMODEL=128;PADDED_HEAD=False;ENABLE_DROPOUT=False;CAUSAL_TYPE=0;BIAS_TYPE=1;;#P;PERSISTENT_TYPE=0;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
{{ 9,17,17,12,15,13, 3,14, 2, 3},
 { 2, 3, 4, 2, 3, 5, 3,14,14, 5},
 { 3, 3, 2, 2, 2, 2,15, 2,13, 3},
 { 1, 4, 4, 4, 2, 4, 3, 5,17,17},
 { 3, 3, 2, 4, 4,18,18,17,21,19},
 { 0, 4, 5, 4, 2,30,21,19,19,26},
 {11, 6, 7, 8,28,28,16,26,26,16},
 {25,10,22,23,29,29,24,29,29,29},
 {22, 8,22,10,27,22,29,24,25,24},
 {27,20,16, 8,22,28,24,24,24,24}}
// 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__F97 "
                 "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 = 1;
    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_az = 0
// Num_head_q = "i32"
// cu_seqlens_q = "*i32:16"
// BLOCK_DMODEL = 128
// Hdim_qk = 128
// PADDED_HEAD = False
// ENABLE_DROPOUT = False
// dropout_p = 0
// philox_seed_ptr = 0
// philox_offset2 = 0
// RETURN_ENCODED_SOFTMAX = False
// CAUSAL_TYPE = 0
// Window_left = 0
// Window_right = 0
// BIAS_TYPE = 1
// USE_ALIBI = False
// INT8 = False
// persistent_atomic_counter = 0
// 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', 128, False, False, 0, 1])

// vim: set fileencoding=utf-8

