import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_per_fused_add_div_exp_sum_tanh_0(in_out_ptr0, in_ptr0, out_ptr0,
    xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = libdevice.tanh(tmp0)
    tmp2 = tl_math.exp(tmp1)
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.sum(tmp3, 1)[:, None]
    tmp6 = 1e-10
    tmp7 = tmp5 + tmp6
    tmp8 = tmp2 / tmp7
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp7, None)
    tl.store(out_ptr0 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp8, None)


@triton.jit
def triton_poi_fused_sum_1(in_out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_out_ptr0 + x0, xmask)
    tl.store(in_out_ptr0 + x0, tmp0, xmask)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (1, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0),
            primals_2, out=buf0)
        del primals_2
        buf1 = empty_strided_cuda((1, 64), (64, 1), torch.float32)
        extern_kernels.mm(primals_3, reinterpret_tensor(buf0, (4, 64), (64,
            1), 0), out=buf1)
        buf2 = empty_strided_cuda((1, 1), (1, 1), torch.float32)
        buf3 = buf2
        del buf2
        buf4 = empty_strided_cuda((1, 64), (64, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_add_div_exp_sum_tanh_0[grid(1)](buf3, buf1, buf4, 
            1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        buf5 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (4, 64), (64, 1), 0
            ), reinterpret_tensor(buf4, (64, 1), (1, 0), 0), out=buf5)
        del buf4
        buf6 = reinterpret_tensor(buf5, (4,), (1,), 0)
        del buf5
        triton_poi_fused_sum_1[grid(4)](buf6, 4, XBLOCK=4, num_warps=1,
            num_stages=1)
    return buf6, primals_1, buf1, buf3, reinterpret_tensor(primals_3, (4, 1
        ), (1, 4), 0), reinterpret_tensor(buf0, (64, 4), (1, 64), 0)


class AttentionNew(nn.Module):

    def __init__(self, input_dim, feature_dim):
        super(AttentionNew, self).__init__()
        self.feature_dim = feature_dim
        self.input_dim = input_dim
        weight = torch.zeros(self.feature_dim, self.feature_dim)
        nn.init.kaiming_uniform_(weight)
        self.weight = nn.Parameter(weight)
        w = torch.zeros(1, self.feature_dim)
        nn.init.kaiming_uniform_(w)
        self.w = nn.Parameter(w)

    def forward(self, input_0):
        primals_2 = self.weight
        primals_3 = self.w
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3])
        return output[0]


Measured latency_ms: 0.05085