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 math as tl_math
from torch import 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_poi_fused_mul_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask)
    tmp2 = tmp0 * tmp1
    tl.store(out_ptr0 + x0, tmp2, xmask)


@triton.jit
def triton_poi_fused_mul_sigmoid_1(in_ptr0, in_ptr1, in_ptr2, out_ptr0,
    out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask)
    tmp4 = tl.load(in_ptr2 + x0, xmask)
    tmp2 = tl.sigmoid(tmp1)
    tmp3 = tmp0 * tmp2
    tmp5 = tmp4 * tmp2
    tl.store(out_ptr0 + x0, tmp3, xmask)
    tl.store(out_ptr1 + x0, tmp5, xmask)


@triton.jit
def triton_per_fused__softmax_2(in_ptr0, out_ptr2, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    RBLOCK: tl.constexpr = 16
    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 = 0.5
    tmp2 = tmp0 * tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = triton_helpers.max2(tmp3, 1)[:, None]
    tmp6 = tmp2 - tmp5
    tmp7 = tl_math.exp(tmp6)
    tmp8 = tl.broadcast_to(tmp7, [XBLOCK, RBLOCK])
    tmp10 = tl.sum(tmp8, 1)[:, None]
    tmp11 = tmp7 / tmp10
    tl.store(out_ptr2 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp11, None)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9, primals_10, primals_11, primals_12,
        primals_13) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (4,), (1,))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    assert_size_stride(primals_5, (4,), (1,))
    assert_size_stride(primals_6, (4, 4), (4, 1))
    assert_size_stride(primals_7, (4,), (1,))
    assert_size_stride(primals_8, (4, 4), (4, 1))
    assert_size_stride(primals_9, (4,), (1,))
    assert_size_stride(primals_10, (4, 4), (4, 1))
    assert_size_stride(primals_11, (4,), (1,))
    assert_size_stride(primals_12, (4, 4), (4, 1))
    assert_size_stride(primals_13, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_3, primals_1, reinterpret_tensor(
            primals_2, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf0)
        del primals_2
        del primals_3
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_5, primals_1, reinterpret_tensor(
            primals_4, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf1)
        del primals_4
        del primals_5
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_7, primals_1, reinterpret_tensor(
            primals_6, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf2)
        del primals_6
        del primals_7
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_9, buf1, reinterpret_tensor(primals_8,
            (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf3)
        del primals_9
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_11, buf2, reinterpret_tensor(
            primals_10, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf4)
        del primals_11
        buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_mul_0[grid(16)](buf3, buf4, buf5, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_13, buf5, reinterpret_tensor(
            primals_12, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf6)
        del primals_13
        buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf8 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_mul_sigmoid_1[grid(16)](buf1, buf6, buf2, buf7,
            buf8, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf9 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf7, reinterpret_tensor(buf8, (4, 4), (1, 4), 0),
            out=buf9)
        buf12 = empty_strided_cuda((16,), (1,), torch.float32)
        triton_per_fused__softmax_2[grid(1)](buf9, buf12, 1, 16, XBLOCK=1,
            num_warps=2, num_stages=1)
        buf13 = buf9
        del buf9
        extern_kernels.mm(reinterpret_tensor(buf12, (4, 4), (4, 1), 0),
            buf0, out=buf13)
    return (buf13, primals_1, buf1, buf2, buf3, buf4, buf5, buf6, buf7,
        buf12, reinterpret_tensor(buf0, (4, 4), (1, 4), 0), buf8,
        primals_12, primals_10, primals_8)


class GSAHelper(nn.Module):

    def __init__(self, d):
        super().__init__()
        self.d = d
        self.fc_k = nn.Linear(self.d, self.d)
        self.fc_q = nn.Linear(self.d, self.d)
        self.fc_kq = nn.Linear(self.d, self.d)

    def forward(self, k, q):
        m = k.shape[0]
        k_1 = self.fc_k(k)
        q_1 = self.fc_q(q)
        kq = nn.Sigmoid()(self.fc_kq(torch.mul(k_1, q_1)))
        k_2 = torch.mul(k, kq)
        q_2 = torch.mul(q, kq)
        mul = torch.mm(k_2, torch.t(q_2)) / self.d ** (1.0 / 2)
        a = nn.Softmax()(torch.flatten(mul)).view(m, m)
        return a


class GSANew(nn.Module):

    def __init__(self, d):
        super().__init__()
        self.d = d
        self.fc_v = nn.Linear(self.d, self.d)
        self.fc_k = nn.Linear(self.d, self.d)
        self.fc_q = nn.Linear(self.d, self.d)
        self.gsa_helper = GSAHelper(self.d)

    def forward(self, input_0):
        primals_1 = self.fc_v.weight
        primals_3 = self.fc_v.bias
        primals_2 = self.fc_k.weight
        primals_5 = self.fc_k.bias
        primals_4 = self.fc_q.weight
        primals_7 = self.fc_q.bias
        primals_6 = self.gsa_helper.fc_k.weight
        primals_9 = self.gsa_helper.fc_k.bias
        primals_8 = self.gsa_helper.fc_q.weight
        primals_11 = self.gsa_helper.fc_q.bias
        primals_10 = self.gsa_helper.fc_kq.weight
        primals_13 = self.gsa_helper.fc_kq.bias
        primals_12 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9,
            primals_10, primals_11, primals_12, primals_13])
        return output[0]


Measured latency_ms: 0.037805