#include <iostream>
#include <cuda_runtime.h>
#include <cooperative_groups.h>
#include <cassert>

namespace cg = cooperative_groups;

#define N_TOKENS 4096
#define HEADS 8
#define EMBED_DIM 512
#define QKV_DIM (EMBED_DIM / HEADS)

__device__ __inline__ float dot_product(const float* a, const float* b, int size) {
    float sum = 0.0f;
    for (int i = 0; i < size; ++i) {
        sum += a[i] * b[i];
    }
    return sum;
}

__global__ void attention_kernel(float* Q, float* K, float* V, float* output, int n_tokens, int heads, int embed_dim) {
    extern __shared__ float shared_mem[];
    float* q_shared = shared_mem;
    float* k_shared = shared_mem + embed_dim;
    float* v_shared = shared_mem + 2 * embed_dim;

    int batch_id = blockIdx.x;
    int head_id = blockIdx.y;
    int token_id = threadIdx.x;

    int q_idx = batch_id * n_tokens * embed_dim + head_id * QKV_DIM + token_id * QKV_DIM;
    int k_idx = batch_id * n_tokens * embed_dim + head_id * QKV_DIM + token_id * QKV_DIM;
    int v_idx = batch_id * n_tokens * embed_dim + head_id * QKV_DIM + token_id * QKV_DIM;
    int out_idx = batch_id * n_tokens * embed_dim + head_id * QKV_DIM + token_id * QKV_DIM;

    float q[QKV_DIM];
    float k[QKV_DIM];
    float v[QKV_DIM];

    // Load Q, K, V into shared memory
    for (int i = 0; i < QKV_DIM; ++i) {
        q[i] = Q[q_idx + i];
        k[i] = K[k_idx + i];
        v[i] = V[v_idx + i];
    }

    cg::thread_block cta = cg::this_thread_block();
    cg::thread_block_tile<32> tile32 = cg::tiled_partition<32>(cta);

    // Load K and V into shared memory for reuse
    for (int i = tile32.thread_rank(); i < n_tokens * QKV_DIM; i += tile32.size()) {
        int row = i / QKV_DIM;
        int col = i % QKV_DIM;
        k_shared[row * QKV_DIM + col] = K[batch_id * n_tokens * embed_dim + head_id * QKV_DIM + row * QKV_DIM + col];
        v_shared[row * QKV_DIM + col] = V[batch_id * n_tokens * embed_dim + head_id * QKV_DIM + row * QKV_DIM + col];
    }
    cta.sync();

    float attention_scores[N_TOKENS];
    float attention_probs[N_TOKENS];
    float context_vector[QKV_DIM] = {0.0f};

    // Compute attention scores
    for (int j = 0; j < n_tokens; ++j) {
        float* k_j = k_shared + j * QKV_DIM;
        attention_scores[j] = dot_product(q, k_j, QKV_DIM) / sqrtf(QKV_DIM);
    }

    // Softmax
    float max_score = -1e9f;
    for (int j = 0; j < n_tokens; ++j) {
        max_score = fmaxf(max_score, attention_scores[j]);
    }
    float sum_exp = 0.0f;
    for (int j = 0; j < n_tokens; ++j) {
        attention_probs[j] = expf(attention_scores[j] - max_score);
        sum_exp += attention_probs[j];
    }
    for (int j = 0; j < n_tokens; ++j) {
        attention_probs[j] /= sum_exp;
    }

    // Compute context vector
    for (int j = 0; j < n_tokens; ++j) {
        float* v_j = v_shared + j * QKV_DIM;
        for (int i = 0; i < QKV_DIM; ++i) {
            context_vector[i] += attention_probs[j] * v_j[i];
        }
    }

    // Store the result in output
    for (int i = 0; i < QKV_DIM; ++i) {
        output[out_idx + i] = context_vector[i];
    }
}

int main() {
    int n_tokens = N_TOKENS;
    int heads = HEADS;
    int embed_dim = EMBED_DIM;

    size_t qkv_size = n_tokens * embed_dim * sizeof(float);
    float* h_Q = (float*)malloc(qkv_size);
    float* h_K = (float*)malloc(qkv_size);
    float* h_V = (float*)malloc(qkv_size);
    float* h_output = (float*)malloc(qkv_size);

    // Initialize Q, K, V with random values
    for (int i = 0; i < n_tokens * embed_dim; ++i) {
        h_Q[i] = static_cast<float>(rand()) / RAND_MAX;
        h_K[i] = static_cast<float>(rand()) / RAND_MAX;
        h_V[i] = static_cast<float>(rand()) / RAND_MAX;
    }

    float* d_Q, *d_K, *d_V, *d_output;
    cudaMalloc(&d_Q, qkv_size);
    cudaMalloc(&d_K, qkv_size);
    cudaMalloc(&d_V, qkv_size);
    cudaMalloc(&d_output, qkv_size);

    cudaMemcpy(d_Q, h_Q, qkv_size, cudaMemcpyHostToDevice);
    cudaMemcpy(d_K, h_K, qkv_size, cudaMemcpyHostToDevice);
    cudaMemcpy(d_V, h_V, qkv_size, cudaMemcpyHostToDevice);

    dim3 grid(1, heads);
    dim3 block(n_tokens);
    size_t shared_mem_size = 3 * n_tokens * QKV_DIM * sizeof(float);

    attention_kernel<<<grid, block, shared_mem_size>>>(d_Q, d_K, d_V, d_output, n_tokens, heads, embed_dim);

    cudaMemcpy(h_output, d_output, qkv_size, cudaMemcpyDeviceToHost);

    // Free device memory
    cudaFree(d_Q);
    cudaFree(d_K);
    cudaFree(d_V);
    cudaFree(d_output);

    // Free host memory
    free(h_Q);
    free(h_K);
    free(h_V);
    free(h_output);

    // Check for errors
    cudaError_t err = cudaGetLastError();
    if (err != cudaSuccess) {
        std::cerr << "CUDA error: " << cudaGetErrorString(err) << std::endl;
        return -1;
    }

    std::cout << "Attention mechanism computed successfully." << std::endl;

    return 0;
}
	.file	"tmpxft_0004a4f1_00000000-6_cuda_code_029166.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB6836:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE6836:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z45__device_stub__Z16attention_kernelPfS_S_S_iiiPfS_S_S_iii
	.type	_Z45__device_stub__Z16attention_kernelPfS_S_S_iiiPfS_S_S_iii, @function
_Z45__device_stub__Z16attention_kernelPfS_S_S_iiiPfS_S_S_iii:
.LFB6858:
	.cfi_startproc
	endbr64
	subq	$184, %rsp
	.cfi_def_cfa_offset 192
	movq	%rdi, 40(%rsp)
	leaq	64(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	76(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	48(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	56(%rsp), %rcx
	movl	%r8d, 12(%rsp)
	movl	%r9d, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	192(%rsp), %rax
	movq	%rax, 160(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	56(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_Z16attention_kernelPfS_S_S_iii(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 208
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 200
	popq	%rdx
	.cfi_def_cfa_offset 192
.L2:
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$184, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE6858:
	.size	_Z45__device_stub__Z16attention_kernelPfS_S_S_iiiPfS_S_S_iii, .-_Z45__device_stub__Z16attention_kernelPfS_S_S_iiiPfS_S_S_iii
	.globl	_Z16attention_kernelPfS_S_S_iii
	.type	_Z16attention_kernelPfS_S_S_iii, @function
_Z16attention_kernelPfS_S_S_iii:
.LFB6859:
	.cfi_startproc
	endbr64
	jmp	_Z45__device_stub__Z16attention_kernelPfS_S_S_iiiPfS_S_S_iii
	.cfi_endproc
.LFE6859:
	.size	_Z16attention_kernelPfS_S_S_iii, .-_Z16attention_kernelPfS_S_S_iii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"CUDA error: "
.LC2:
	.string	"Attention mechanism computed successfully."
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB6833:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movl	$8388608, %edi
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	xorl	%ebx, %ebx
	subq	$64, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movl	$8388608, %edi
	movq	%rax, %r13
	call	malloc@PLT
	movl	$8388608, %edi
	movq	%rax, %r12
	call	malloc@PLT
	movl	$8388608, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	movq	%rax, %r14
.L9:
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC0(%rip), %xmm0
	movss	%xmm0, 0(%r13,%rbx)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC0(%rip), %xmm0
	movss	%xmm0, (%r12,%rbx)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC0(%rip), %xmm0
	movss	%xmm0, 0(%rbp,%rbx)
	addq	$4, %rbx
	cmpq	$8388608, %rbx
	jne	.L9
	movq	%rsp, %rdi
	movl	$8388608, %esi
	call	cudaMalloc@PLT
	leaq	8(%rsp), %rdi
	movl	$8388608, %esi
	call	cudaMalloc@PLT
	leaq	16(%rsp), %rdi
	movl	$8388608, %esi
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movl	$8388608, %esi
	call	cudaMalloc@PLT
	movq	(%rsp), %rdi
	movl	$1, %ecx
	movq	%r13, %rsi
	movl	$8388608, %edx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdi
	movl	$1, %ecx
	movq	%r12, %rsi
	movl	$8388608, %edx
	call	cudaMemcpy@PLT
	movq	16(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbp, %rsi
	movl	$8388608, %edx
	call	cudaMemcpy@PLT
	movl	$1048577, %edx
	xorl	%r9d, %r9d
	movl	$3145728, %r8d
	salq	$12, %rdx
	movl	$1, %ecx
	movl	$1, %esi
	movabsq	$34359738369, %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L10
	pushq	%rax
	.cfi_def_cfa_offset 120
	movl	$8, %r9d
	movl	$4096, %r8d
	pushq	$512
	.cfi_def_cfa_offset 128
	movq	40(%rsp), %rcx
	movq	32(%rsp), %rdx
	movq	24(%rsp), %rsi
	movq	16(%rsp), %rdi
	call	_Z45__device_stub__Z16attention_kernelPfS_S_S_iiiPfS_S_S_iii
	popq	%rdx
	.cfi_def_cfa_offset 120
	popq	%rcx
	.cfi_def_cfa_offset 112
.L10:
	movq	24(%rsp), %rsi
	movl	$2, %ecx
	movl	$8388608, %edx
	movq	%r14, %rdi
	call	cudaMemcpy@PLT
	movq	(%rsp), %rdi
	call	cudaFree@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	%r13, %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	%r14, %rdi
	call	free@PLT
	call	cudaGetLastError@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	je	.L11
	leaq	.LC1(%rip), %rsi
	leaq	_ZSt4cerr(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	%ebx, %edi
	movq	%rax, %rbp
	call	cudaGetErrorString@PLT
	movq	%rbp, %rdi
	movq	%rax, %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	orl	$-1, %eax
	jmp	.L8
.L11:
	leaq	.LC2(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	xorl	%eax, %eax
.L8:
	movq	56(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	addq	$64, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE6833:
	.size	main, .-main
	.section	.rodata.str1.1
.LC3:
	.string	"_Z16attention_kernelPfS_S_S_iii"
.LC4:
	.string	"_ZN50_INTERNAL_e8c9a838_19_cuda_code_029166_cu_78feb7ad4cuda3std3__419piecewise_constructE"
.LC5:
	.string	"_ZN50_INTERNAL_e8c9a838_19_cuda_code_029166_cu_78feb7ad4cuda3std6ranges3__45__cpo4swapE"
.LC6:
	.string	"_ZN50_INTERNAL_e8c9a838_19_cuda_code_029166_cu_78feb7ad4cuda3std6ranges3__45__cpo9iter_moveE"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB6861:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC3(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z16attention_kernelPfS_S_S_iii(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC4(%rip), %rdx
	movl	$1, %r9d
	leaq	_ZN4cuda3std3__419piecewise_constructE(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	call	__cudaRegisterVar@PLT
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rdx
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC5(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movq	%rdx, %rcx
	movl	$1, %r9d
	leaq	_ZN4cuda3std6ranges3__45__cpo4swapE(%rip), %rsi
	call	__cudaRegisterVar@PLT
	popq	%rcx
	.cfi_def_cfa_offset 24
	popq	%rsi
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC6(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movl	$1, %r9d
	movq	%rdx, %rcx
	leaq	_ZN4cuda3std6ranges3__45__cpo9iter_moveE(%rip), %rsi
	call	__cudaRegisterVar@PLT
	popq	%rdi
	.cfi_def_cfa_offset 24
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	popq	%r8
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE6861:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.weak	_ZN4cuda3std3__419piecewise_constructE
	.section	.rodata._ZN4cuda3std3__419piecewise_constructE,"aG",@progbits,_ZN4cuda3std3__419piecewise_constructE,comdat
	.type	_ZN4cuda3std3__419piecewise_constructE, @gnu_unique_object
	.size	_ZN4cuda3std3__419piecewise_constructE, 1
_ZN4cuda3std3__419piecewise_constructE:
	.zero	1
	.weak	_ZN4cuda3std6ranges3__45__cpo9iter_moveE
	.section	.rodata._ZN4cuda3std6ranges3__45__cpo9iter_moveE,"aG",@progbits,_ZN4cuda3std6ranges3__45__cpo9iter_moveE,comdat
	.type	_ZN4cuda3std6ranges3__45__cpo9iter_moveE, @gnu_unique_object
	.size	_ZN4cuda3std6ranges3__45__cpo9iter_moveE, 1
_ZN4cuda3std6ranges3__45__cpo9iter_moveE:
	.zero	1
	.weak	_ZN4cuda3std6ranges3__45__cpo4swapE
	.section	.rodata._ZN4cuda3std6ranges3__45__cpo4swapE,"aG",@progbits,_ZN4cuda3std6ranges3__45__cpo4swapE,comdat
	.type	_ZN4cuda3std6ranges3__45__cpo4swapE, @gnu_unique_object
	.size	_ZN4cuda3std6ranges3__45__cpo4swapE, 1
_ZN4cuda3std6ranges3__45__cpo4swapE:
	.zero	1
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC0:
	.long	805306368
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#include <iostream>
#include <hip/hip_runtime.h>
#include <hip/hip_cooperative_groups.h>
#include <cassert>

namespace cg = cooperative_groups;

#define N_TOKENS 4096
#define HEADS 8
#define EMBED_DIM 512
#define QKV_DIM (EMBED_DIM / HEADS)

__device__ __inline__ float dot_product(const float* a, const float* b, int size) {
    float sum = 0.0f;
    for (int i = 0; i < size; ++i) {
        sum += a[i] * b[i];
    }
    return sum;
}

__global__ void attention_kernel(float* Q, float* K, float* V, float* output, int n_tokens, int heads, int embed_dim) {
    extern __shared__ float shared_mem[];
    float* q_shared = shared_mem;
    float* k_shared = shared_mem + embed_dim;
    float* v_shared = shared_mem + 2 * embed_dim;

    int batch_id = blockIdx.x;
    int head_id = blockIdx.y;
    int token_id = threadIdx.x;

    int q_idx = batch_id * n_tokens * embed_dim + head_id * QKV_DIM + token_id * QKV_DIM;
    int k_idx = batch_id * n_tokens * embed_dim + head_id * QKV_DIM + token_id * QKV_DIM;
    int v_idx = batch_id * n_tokens * embed_dim + head_id * QKV_DIM + token_id * QKV_DIM;
    int out_idx = batch_id * n_tokens * embed_dim + head_id * QKV_DIM + token_id * QKV_DIM;

    float q[QKV_DIM];
    float k[QKV_DIM];
    float v[QKV_DIM];

    // Load Q, K, V into shared memory
    for (int i = 0; i < QKV_DIM; ++i) {
        q[i] = Q[q_idx + i];
        k[i] = K[k_idx + i];
        v[i] = V[v_idx + i];
    }

    cg::thread_block cta = cg::this_thread_block();
    cg::thread_block_tile<32> tile32 = cg::tiled_partition<32>(cta);

    // Load K and V into shared memory for reuse
    for (int i = tile32.thread_rank(); i < n_tokens * QKV_DIM; i += tile32.size()) {
        int row = i / QKV_DIM;
        int col = i % QKV_DIM;
        k_shared[row * QKV_DIM + col] = K[batch_id * n_tokens * embed_dim + head_id * QKV_DIM + row * QKV_DIM + col];
        v_shared[row * QKV_DIM + col] = V[batch_id * n_tokens * embed_dim + head_id * QKV_DIM + row * QKV_DIM + col];
    }
    cta.sync();

    float attention_scores[N_TOKENS];
    float attention_probs[N_TOKENS];
    float context_vector[QKV_DIM] = {0.0f};

    // Compute attention scores
    for (int j = 0; j < n_tokens; ++j) {
        float* k_j = k_shared + j * QKV_DIM;
        attention_scores[j] = dot_product(q, k_j, QKV_DIM) / sqrtf(QKV_DIM);
    }

    // Softmax
    float max_score = -1e9f;
    for (int j = 0; j < n_tokens; ++j) {
        max_score = fmaxf(max_score, attention_scores[j]);
    }
    float sum_exp = 0.0f;
    for (int j = 0; j < n_tokens; ++j) {
        attention_probs[j] = expf(attention_scores[j] - max_score);
        sum_exp += attention_probs[j];
    }
    for (int j = 0; j < n_tokens; ++j) {
        attention_probs[j] /= sum_exp;
    }

    // Compute context vector
    for (int j = 0; j < n_tokens; ++j) {
        float* v_j = v_shared + j * QKV_DIM;
        for (int i = 0; i < QKV_DIM; ++i) {
            context_vector[i] += attention_probs[j] * v_j[i];
        }
    }

    // Store the result in output
    for (int i = 0; i < QKV_DIM; ++i) {
        output[out_idx + i] = context_vector[i];
    }
}

int main() {
    int n_tokens = N_TOKENS;
    int heads = HEADS;
    int embed_dim = EMBED_DIM;

    size_t qkv_size = n_tokens * embed_dim * sizeof(float);
    float* h_Q = (float*)malloc(qkv_size);
    float* h_K = (float*)malloc(qkv_size);
    float* h_V = (float*)malloc(qkv_size);
    float* h_output = (float*)malloc(qkv_size);

    // Initialize Q, K, V with random values
    for (int i = 0; i < n_tokens * embed_dim; ++i) {
        h_Q[i] = static_cast<float>(rand()) / RAND_MAX;
        h_K[i] = static_cast<float>(rand()) / RAND_MAX;
        h_V[i] = static_cast<float>(rand()) / RAND_MAX;
    }

    float* d_Q, *d_K, *d_V, *d_output;
    hipMalloc(&d_Q, qkv_size);
    hipMalloc(&d_K, qkv_size);
    hipMalloc(&d_V, qkv_size);
    hipMalloc(&d_output, qkv_size);

    hipMemcpy(d_Q, h_Q, qkv_size, hipMemcpyHostToDevice);
    hipMemcpy(d_K, h_K, qkv_size, hipMemcpyHostToDevice);
    hipMemcpy(d_V, h_V, qkv_size, hipMemcpyHostToDevice);

    dim3 grid(1, heads);
    dim3 block(n_tokens);
    size_t shared_mem_size = 3 * n_tokens * QKV_DIM * sizeof(float);

    attention_kernel<<<grid, block, shared_mem_size>>>(d_Q, d_K, d_V, d_output, n_tokens, heads, embed_dim);

    hipMemcpy(h_output, d_output, qkv_size, hipMemcpyDeviceToHost);

    // Free device memory
    hipFree(d_Q);
    hipFree(d_K);
    hipFree(d_V);
    hipFree(d_output);

    // Free host memory
    free(h_Q);
    free(h_K);
    free(h_V);
    free(h_output);

    // Check for errors
    hipError_t err = hipGetLastError();
    if (err != hipSuccess) {
        std::cerr << "CUDA error: " << hipGetErrorString(err) << std::endl;
        return -1;
    }

    std::cout << "Attention mechanism computed successfully." << std::endl;

    return 0;
}
	.text
	.file	"cuda_code_029166.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	_Z31__device_stub__attention_kernelPfS_S_S_iii # -- Begin function _Z31__device_stub__attention_kernelPfS_S_S_iii
	.type	_Z31__device_stub__attention_kernelPfS_S_S_iii,@function
_Z31__device_stub__attention_kernelPfS_S_S_iii: # @_Z31__device_stub__attention_kernelPfS_S_S_iii
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$160, %rsp
	.cfi_def_cfa_offset 208
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	56(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	48(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	40(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	32(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	12(%rsp), %rcx
	movl	%r8d, (%rcx)
	leaq	8(%rsp), %r8
	movl	%r9d, (%r8)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	leaq	208(%rsp), %rax
	movq	%rax, 48(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z16attention_kernelPfS_S_S_iii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$176, %rsp
	.cfi_adjust_cfa_offset -176
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z31__device_stub__attention_kernelPfS_S_S_iii, .Lfunc_end0-_Z31__device_stub__attention_kernelPfS_S_S_iii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI1_0:
	.long	0x30000000                      # float 4.65661287E-10
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$8388608, %edi                  # imm = 0x800000
	callq	malloc
	movq	%rax, %rbx
	movl	$8388608, %edi                  # imm = 0x800000
	callq	malloc
	movq	%rax, %r14
	movl	$8388608, %edi                  # imm = 0x800000
	callq	malloc
	movq	%rax, %r15
	movl	$8388608, %edi                  # imm = 0x800000
	callq	malloc
	movq	%rax, 48(%rsp)                  # 8-byte Spill
	xorl	%r12d, %r12d
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	.LCPI1_0(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	mulss	%xmm1, %xmm0
	movss	%xmm0, (%rbx,%r12,4)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI1_0(%rip), %xmm0
	movss	%xmm0, (%r14,%r12,4)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI1_0(%rip), %xmm0
	movss	%xmm0, (%r15,%r12,4)
	incq	%r12
	cmpq	$2097152, %r12                  # imm = 0x200000
	jne	.LBB1_1
# %bb.2:
	leaq	40(%rsp), %r12
	movl	$8388608, %esi                  # imm = 0x800000
	movq	%r12, %rdi
	callq	hipMalloc
	leaq	32(%rsp), %rbp
	movl	$8388608, %esi                  # imm = 0x800000
	movq	%rbp, %rdi
	callq	hipMalloc
	leaq	24(%rsp), %r13
	movl	$8388608, %esi                  # imm = 0x800000
	movq	%r13, %rdi
	callq	hipMalloc
	leaq	16(%rsp), %rdi
	movl	$8388608, %esi                  # imm = 0x800000
	callq	hipMalloc
	movq	(%r12), %rdi
	movl	$8388608, %edx                  # imm = 0x800000
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%rbp), %rdi
	movl	$8388608, %edx                  # imm = 0x800000
	movq	%r14, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%r13), %rdi
	movl	$8388608, %edx                  # imm = 0x800000
	movq	%r15, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movabsq	$34359738369, %rdi              # imm = 0x800000001
	movabsq	$4294971392, %rdx               # imm = 0x100001000
	movl	$3145728, %r8d                  # imm = 0x300000
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_4
# %bb.3:
	movq	40(%rsp), %rdi
	movq	32(%rsp), %rsi
	movq	24(%rsp), %rdx
	movq	16(%rsp), %rcx
	movl	$512, (%rsp)                    # imm = 0x200
	movl	$4096, %r8d                     # imm = 0x1000
	movl	$8, %r9d
	callq	_Z31__device_stub__attention_kernelPfS_S_S_iii
.LBB1_4:
	movq	16(%rsp), %rsi
	movl	$8388608, %edx                  # imm = 0x800000
	movq	48(%rsp), %r12                  # 8-byte Reload
	movq	%r12, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	movq	%r12, %rdi
	callq	free
	callq	hipGetLastError
	testl	%eax, %eax
	je	.LBB1_7
# %bb.5:
	movl	%eax, %ebp
	movl	$_ZSt4cerr, %ebx
	movl	$_ZSt4cerr, %edi
	movl	$.L.str, %esi
	movl	$12, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	%ebp, %edi
	callq	hipGetErrorString
	testq	%rax, %rax
	je	.LBB1_8
# %bb.6:
	movq	%rax, %r14
	movq	%rax, %rdi
	callq	strlen
	movl	$_ZSt4cerr, %edi
	movq	%r14, %rsi
	movq	%rax, %rdx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	jmp	.LBB1_9
.LBB1_7:
	movl	$_ZSt4cout, %ebx
	movl	$_ZSt4cout, %edi
	movl	$.L.str.1, %esi
	movl	$42, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	xorl	%ebp, %ebp
	jmp	.LBB1_10
.LBB1_8:
	movq	_ZSt4cerr(%rip), %rax
	movq	-24(%rax), %rax
	leaq	_ZSt4cerr(%rax), %rdi
	movl	32(%rdi), %esi
	orl	$1, %esi
	callq	_ZNSt9basic_iosIcSt11char_traitsIcEE5clearESt12_Ios_Iostate
.LBB1_9:                                # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit
	movl	$-1, %ebp
.LBB1_10:                               # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit
	movq	(%rbx), %rax
	movq	-24(%rax), %rdi
	addq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%rbx, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movl	%ebp, %eax
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB2_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB2_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z16attention_kernelPfS_S_S_iii, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end2:
	.size	__hip_module_ctor, .Lfunc_end2-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB3_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB3_2:
	retq
.Lfunc_end3:
	.size	__hip_module_dtor, .Lfunc_end3-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z16attention_kernelPfS_S_S_iii,@object # @_Z16attention_kernelPfS_S_S_iii
	.section	.rodata,"a",@progbits
	.globl	_Z16attention_kernelPfS_S_S_iii
	.p2align	3, 0x0
_Z16attention_kernelPfS_S_S_iii:
	.quad	_Z31__device_stub__attention_kernelPfS_S_S_iii
	.size	_Z16attention_kernelPfS_S_S_iii, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"CUDA error: "
	.size	.L.str, 13

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Attention mechanism computed successfully."
	.size	.L.str.1, 43

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z16attention_kernelPfS_S_S_iii"
	.size	.L__unnamed_1, 32

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym _Z31__device_stub__attention_kernelPfS_S_S_iii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z16attention_kernelPfS_S_S_iii
	.addrsig_sym _ZSt4cerr
	.addrsig_sym _ZSt4cout
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z16attention_kernelPfS_S_S_iii ; -- Begin function _Z16attention_kernelPfS_S_S_iii
	.globl	_Z16attention_kernelPfS_S_S_iii
	.p2align	8
	.type	_Z16attention_kernelPfS_S_S_iii,@function
_Z16attention_kernelPfS_S_S_iii:        ; @_Z16attention_kernelPfS_S_S_iii
; %bb.0:
	s_clause 0x2
	s_load_b32 s12, s[0:1], 0x20
	s_load_b32 s3, s[0:1], 0x28
	s_load_b256 s[4:11], s[0:1], 0x0
	v_and_b32_e32 v2, 0x3ff, v0
	s_lshl_b32 s13, s15, 6
	s_mov_b32 s15, 0
	s_waitcnt lgkmcnt(0)
	s_mul_i32 s2, s14, s12
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_mul_i32 s2, s2, s3
	s_add_i32 s2, s2, s13
	s_mov_b32 s13, 0x8110
	v_lshl_add_u32 v1, v2, 6, s2
	s_add_i32 s14, s13, 0xf0
	s_mov_b32 s13, s12
	s_delay_alu instid0(VALU_DEP_1)
	v_mov_b32_e32 v3, v1
.LBB0_1:                                ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	v_ashrrev_i32_e32 v4, 31, v3
	s_add_i32 s16, s15, 16
	s_add_i32 s15, s15, 4
	s_cmpk_eq_i32 s15, 0x100
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[4:5], 2, v[3:4]
	v_add_nc_u32_e32 v3, 1, v3
	v_add_co_u32 v4, vcc_lo, s4, v4
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e32 v5, vcc_lo, s5, v5, vcc_lo
	global_load_b32 v4, v[4:5], off
	s_waitcnt vmcnt(0)
	scratch_store_b32 off, v4, s16
	s_cbranch_scc0 .LBB0_1
; %bb.2:
	s_load_b32 s0, s[0:1], 0x3c
	v_bfe_u32 v3, v0, 10, 5
	v_bfe_u32 v0, v0, 20, 10
	s_lshl_b32 s4, s3, 2
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s1, s0, 16
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_mad_u32_u24 v0, v0, s1, v3
	s_mov_b32 s1, exec_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[3:4], null, v0, s0, v[2:3]
	s_lshl_b32 s0, s12, 6
	v_and_b32_e32 v0, 31, v3
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_i32_e64 s0, v0
	s_cbranch_execz .LBB0_5
; %bb.3:                                ; %.lr.ph.preheader
	v_lshlrev_b32_e32 v3, 2, v0
	s_lshl_b32 s3, s3, 3
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_add3_u32 v2, 0, s3, v3
	v_add3_u32 v3, 0, s4, v3
	s_mov_b32 s3, 0
.LBB0_4:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	v_add_nc_u32_e32 v4, s2, v0
	v_add_nc_u32_e32 v0, 32, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v5, 31, v4
	v_lshlrev_b64 v[4:5], 2, v[4:5]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v6, vcc_lo, s6, v4
	v_add_co_ci_u32_e32 v7, vcc_lo, s7, v5, vcc_lo
	v_add_co_u32 v4, vcc_lo, s8, v4
	v_add_co_ci_u32_e32 v5, vcc_lo, s9, v5, vcc_lo
	v_cmp_le_i32_e32 vcc_lo, s0, v0
	global_load_b32 v6, v[6:7], off
	global_load_b32 v4, v[4:5], off
	s_waitcnt vmcnt(1)
	ds_store_b32 v3, v6
	s_waitcnt vmcnt(0)
	ds_store_b32 v2, v4
	v_add_nc_u32_e32 v2, 0x80, v2
	v_add_nc_u32_e32 v3, 0x80, v3
	s_or_b32 s3, vcc_lo, s3
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s3
	s_cbranch_execnz .LBB0_4
.LBB0_5:                                ; %Flow253
	s_or_b32 exec_lo, exec_lo, s1
	s_mov_b32 s0, 0
	s_waitcnt lgkmcnt(0)
	s_waitcnt_vscnt null, 0x0
	s_mov_b32 s3, s0
	s_mov_b32 s1, s0
	s_mov_b32 s2, s0
	s_delay_alu instid0(SALU_CYCLE_1)
	v_dual_mov_b32 v5, s3 :: v_dual_mov_b32 v4, s2
	v_dual_mov_b32 v3, s1 :: v_dual_mov_b32 v2, s0
	s_add_i32 s1, s4, 0
	s_cmp_gt_i32 s12, 0
	s_barrier
	s_cselect_b32 s2, -1, 0
	s_cmp_lt_i32 s12, 1
	buffer_gl0_inv
	s_clause 0xf
	scratch_store_b128 off, v[2:5], s14
	scratch_store_b128 off, v[2:5], s14 offset:-16
	scratch_store_b128 off, v[2:5], s14 offset:-32
	scratch_store_b128 off, v[2:5], s14 offset:-48
	scratch_store_b128 off, v[2:5], s14 offset:-64
	scratch_store_b128 off, v[2:5], s14 offset:-80
	scratch_store_b128 off, v[2:5], s14 offset:-96
	scratch_store_b128 off, v[2:5], s14 offset:-112
	scratch_store_b128 off, v[2:5], s14 offset:-128
	scratch_store_b128 off, v[2:5], s14 offset:-144
	scratch_store_b128 off, v[2:5], s14 offset:-160
	scratch_store_b128 off, v[2:5], s14 offset:-176
	scratch_store_b128 off, v[2:5], s14 offset:-192
	scratch_store_b128 off, v[2:5], s14 offset:-208
	scratch_store_b128 off, v[2:5], s14 offset:-224
	scratch_store_b128 off, v[2:5], s14 offset:-240
	s_cbranch_scc1 .LBB0_12
; %bb.6:                                ; %.lr.ph157.preheader
	s_mov_b32 s3, s1
.LBB0_7:                                ; %.lr.ph157
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_8 Depth 2
	v_mov_b32_e32 v0, 0
	s_mov_b32 s5, 0
.LBB0_8:                                ;   Parent Loop BB0_7 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_add_i32 s6, s5, 16
	scratch_load_b32 v2, off, s6
	s_add_i32 s6, s3, s5
	s_add_i32 s5, s5, 4
	v_mov_b32_e32 v3, s6
	s_cmpk_lg_i32 s5, 0x100
	ds_load_b32 v3, v3
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_fmac_f32_e32 v0, v2, v3
	s_cbranch_scc1 .LBB0_8
; %bb.9:                                ; %_Z11dot_productPKfS0_i.exit
                                        ;   in Loop: Header=BB0_7 Depth=1
	s_lshl_b32 s5, s0, 2
	s_delay_alu instid0(VALU_DEP_1)
	v_mul_f32_e32 v0, 0x3e000000, v0
	s_add_i32 s0, s0, 1
	s_addk_i32 s5, 0x110
	s_addk_i32 s3, 0x100
	s_cmp_eq_u32 s0, s12
	scratch_store_b32 off, v0, s5
	s_cbranch_scc0 .LBB0_7
; %bb.10:                               ; %.preheader152
	v_mov_b32_e32 v0, 0x110
	v_mov_b32_e32 v2, 0xce6e6b28
	s_mov_b32 s0, s13
.LBB0_11:                               ; %.lr.ph160
                                        ; =>This Inner Loop Header: Depth=1
	scratch_load_b32 v3, v0, off
	v_max_f32_e32 v2, v2, v2
	v_add_nc_u32_e32 v0, 4, v0
	s_add_i32 s0, s0, -1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	s_cmp_eq_u32 s0, 0
	s_waitcnt vmcnt(0)
	v_max_f32_e32 v3, v3, v3
	v_max_f32_e32 v2, v2, v3
	s_cbranch_scc0 .LBB0_11
	s_branch .LBB0_13
.LBB0_12:
	v_mov_b32_e32 v2, 0xce6e6b28
.LBB0_13:                               ; %.preheader151
	v_cndmask_b32_e64 v3, 0, 1, s2
	s_and_not1_b32 vcc_lo, exec_lo, s2
	s_cbranch_vccnz .LBB0_16
; %bb.14:                               ; %.lr.ph163.preheader
	v_mov_b32_e32 v4, 0x110
	v_dual_mov_b32 v5, 0x4110 :: v_dual_mov_b32 v0, 0
	s_mov_b32 s0, s13
.LBB0_15:                               ; %.lr.ph163
                                        ; =>This Inner Loop Header: Depth=1
	scratch_load_b32 v6, v4, off
	s_add_i32 s0, s0, -1
	v_add_nc_u32_e32 v4, 4, v4
	s_cmp_eq_u32 s0, 0
	s_waitcnt vmcnt(0)
	v_sub_f32_e32 v6, v6, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v7, 0x3fb8aa3b, v6
	v_fma_f32 v8, 0x3fb8aa3b, v6, -v7
	v_rndne_f32_e32 v9, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_4)
	v_sub_f32_e32 v7, v7, v9
	v_cmp_ngt_f32_e32 vcc_lo, 0xc2ce8ed0, v6
	v_fmac_f32_e32 v8, 0x32a5705f, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_f32_e32 v7, v7, v8
	v_cvt_i32_f32_e32 v8, v9
	v_exp_f32_e32 v7, v7
	s_waitcnt_depctr 0xfff
	v_ldexp_f32 v7, v7, v8
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cndmask_b32_e32 v7, 0, v7, vcc_lo
	v_cmp_nlt_f32_e32 vcc_lo, 0x42b17218, v6
	v_cndmask_b32_e32 v6, 0x7f800000, v7, vcc_lo
	scratch_store_b32 v5, v6, off
	v_dual_add_f32 v0, v0, v6 :: v_dual_add_nc_u32 v5, 4, v5
	s_cbranch_scc0 .LBB0_15
	s_branch .LBB0_17
.LBB0_16:
	v_mov_b32_e32 v0, 0
.LBB0_17:                               ; %.preheader150
	s_delay_alu instid0(VALU_DEP_2)
	v_cmp_ne_u32_e32 vcc_lo, 1, v3
	s_cbranch_vccnz .LBB0_24
; %bb.18:                               ; %.lr.ph166.preheader
	v_mov_b32_e32 v2, 0x4110
.LBB0_19:                               ; %.lr.ph166
                                        ; =>This Inner Loop Header: Depth=1
	scratch_load_b32 v3, v2, off
	s_add_i32 s13, s13, -1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_3) | instid1(VALU_DEP_2)
	s_cmp_eq_u32 s13, 0
	s_waitcnt vmcnt(0)
	v_div_scale_f32 v4, null, v0, v0, v3
	v_div_scale_f32 v7, vcc_lo, v3, v0, v3
	v_rcp_f32_e32 v5, v4
	s_waitcnt_depctr 0xfff
	v_fma_f32 v6, -v4, v5, 1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v5, v6, v5
	v_mul_f32_e32 v6, v7, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f32 v8, -v4, v6, v7
	v_fmac_f32_e32 v6, v8, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f32 v4, -v4, v6, v7
	v_div_fmas_f32 v4, v4, v5, v6
	s_delay_alu instid0(VALU_DEP_1)
	v_div_fixup_f32 v3, v4, v0, v3
	scratch_store_b32 v2, v3, off
	v_add_nc_u32_e32 v2, 4, v2
	s_cbranch_scc0 .LBB0_19
; %bb.20:                               ; %.preheader149
	s_add_i32 s0, s1, s4
	s_mov_b32 s1, 0
.LBB0_21:                               ; %.lr.ph169
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_22 Depth 2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_lshl_b32 s2, s1, 2
	s_addk_i32 s2, 0x4110
	scratch_load_b32 v0, off, s2
	s_mov_b32 s2, 0
.LBB0_22:                               ;   Parent Loop BB0_21 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_4) | instid1(SALU_CYCLE_1)
	s_add_i32 s3, s2, 0x8110
	s_add_i32 s4, s0, s2
	scratch_load_b32 v2, off, s3
	v_mov_b32_e32 v3, s4
	s_add_i32 s2, s2, 4
	s_cmpk_eq_i32 s2, 0x100
	ds_load_b32 v3, v3
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_fmac_f32_e32 v2, v0, v3
	scratch_store_b32 off, v2, s3
	s_cbranch_scc0 .LBB0_22
; %bb.23:                               ;   in Loop: Header=BB0_21 Depth=1
	s_add_i32 s1, s1, 1
	s_addk_i32 s0, 0x100
	s_cmp_eq_u32 s1, s12
	s_cbranch_scc0 .LBB0_21
.LBB0_24:                               ; %.preheader.preheader
	s_mov_b32 s0, 0
.LBB0_25:                               ; %.preheader
                                        ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_3) | instid1(SALU_CYCLE_1)
	s_add_i32 s1, s0, 0x8110
	v_ashrrev_i32_e32 v2, 31, v1
	scratch_load_b32 v0, off, s1
	s_add_i32 s0, s0, 4
	s_cmpk_eq_i32 s0, 0x100
	v_lshlrev_b64 v[2:3], 2, v[1:2]
	v_add_nc_u32_e32 v1, 1, v1
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_u32 v2, vcc_lo, s10, v2
	v_add_co_ci_u32_e32 v3, vcc_lo, s11, v3, vcc_lo
	s_waitcnt vmcnt(0)
	global_store_b32 v[2:3], v0, off
	s_cbranch_scc0 .LBB0_25
; %bb.26:
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z16attention_kernelPfS_S_S_iii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 33296
		.amdhsa_kernarg_size 304
		.amdhsa_user_sgpr_count 14
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 1
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 2
		.amdhsa_next_free_vgpr 10
		.amdhsa_next_free_sgpr 17
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z16attention_kernelPfS_S_S_iii, .Lfunc_end0-_Z16attention_kernelPfS_S_S_iii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 1368
; NumSgprs: 19
; NumVgprs: 10
; ScratchSize: 33296
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 19
; NumVGPRsForWavesPerEU: 10
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 1
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 2
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .offset:         32
        .size:           4
        .value_kind:     by_value
      - .offset:         36
        .size:           4
        .value_kind:     by_value
      - .offset:         40
        .size:           4
        .value_kind:     by_value
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         52
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         56
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         60
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         62
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         64
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         66
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         68
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         70
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         104
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         112
        .size:           2
        .value_kind:     hidden_grid_dims
      - .offset:         168
        .size:           4
        .value_kind:     hidden_dynamic_lds_size
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 304
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z16attention_kernelPfS_S_S_iii
    .private_segment_fixed_size: 33296
    .sgpr_count:     19
    .sgpr_spill_count: 0
    .symbol:         _Z16attention_kernelPfS_S_S_iii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     10
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
