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 import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
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_mul_pow_repeat_sqrt_sum_view_0(in_out_ptr0,
    in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (4 * x0 + r1 % 4), xmask, other=0.0)
    tmp8 = tl.load(in_ptr1 + (r1 + 16 * x0), xmask, other=0.0)
    tmp1 = tmp0 * tmp0
    tmp2 = 1e-05
    tmp3 = tmp1 + tmp2
    tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
    tmp6 = tl.where(xmask, tmp4, 0)
    tmp7 = tl.sum(tmp6, 1)[:, None]
    tmp9 = tmp8 * tmp8
    tmp10 = tmp9 + tmp2
    tmp11 = tl.broadcast_to(tmp10, [XBLOCK, RBLOCK])
    tmp13 = tl.where(xmask, tmp11, 0)
    tmp14 = tl.sum(tmp13, 1)[:, None]
    tmp15 = tmp0 * tmp8
    tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK])
    tmp18 = tl.where(xmask, tmp16, 0)
    tmp19 = tl.sum(tmp18, 1)[:, None]
    tmp20 = libdevice.sqrt(tmp7)
    tmp21 = libdevice.sqrt(tmp14)
    tmp22 = tmp20 * tmp21
    tmp23 = tmp19 / tmp22
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp23, xmask)


@triton.jit
def triton_per_fused__softmax_1(in_ptr0, out_ptr2, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    RBLOCK: tl.constexpr = 4
    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 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = triton_helpers.max2(tmp1, 1)[:, None]
    tmp4 = tmp0 - tmp3
    tmp5 = tl_math.exp(tmp4)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp8 = tl.sum(tmp6, 1)[:, None]
    tmp9 = tmp5 / tmp8
    tl.store(out_ptr2 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp9, None)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4, 16), (16, 1))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    assert_size_stride(primals_4, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4,), (1,), torch.float32)
        buf3 = buf0
        del buf0
        get_raw_stream(0)
        triton_per_fused_add_div_mul_pow_repeat_sqrt_sum_view_0[grid(4)](buf3,
            primals_1, primals_2, 4, 16, XBLOCK=1, num_warps=2, num_stages=1)
        del primals_1
        del primals_2
        buf4 = empty_strided_cuda((1, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_4, reinterpret_tensor(buf3, (1, 4), (0,
            1), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), alpha
            =1, beta=1, out=buf4)
        del primals_3
        del primals_4
        buf7 = empty_strided_cuda((4,), (1,), torch.float32)
        triton_per_fused__softmax_1[grid(1)](buf4, buf7, 1, 4, XBLOCK=1,
            num_warps=2, num_stages=1)
        del buf4
    return buf7, reinterpret_tensor(buf3, (1, 4), (4, 1), 0), buf7


def activation_func(name):
    name = name.lower()
    if name == 'sigmoid':
        return torch.nn.Sigmoid()
    elif name == 'tanh':
        return torch.nn.Tanh()
    elif name == 'relu':
        return torch.nn.ReLU()
    elif name == 'softmax':
        return torch.nn.Softmax()
    elif name == 'leaky_relu':
        return torch.nn.LeakyReLU(0.1)
    else:
        return torch.nn.Sequential()


def cosine_similarity(input1, input2):
    query_norm = torch.sqrt(torch.sum(input1 ** 2 + 1e-05, 1))
    doc_norm = torch.sqrt(torch.sum(input2 ** 2 + 1e-05, 1))
    prod = torch.sum(torch.mul(input1, input2), 1)
    norm_prod = torch.mul(query_norm, doc_norm)
    cos_sim_raw = torch.div(prod, norm_prod)
    return cos_sim_raw


class AttentionNew(torch.nn.Module):

    def __init__(self, n_k, activation='relu'):
        super(AttentionNew, self).__init__()
        self.n_k = n_k
        self.fc_layer = torch.nn.Linear(self.n_k, self.n_k, activation_func
            (activation))
        self.soft_max_layer = torch.nn.Softmax()

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


Measured latency_ms: 0.037697