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
import math
import torch.nn.functional as F
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_poi_fused_cat_0(in_ptr0, 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 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + 4 * x0, tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tmp7 = tl.full([1], 8, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tmp6 & tmp8
    tmp10 = tl.load(in_ptr0 + (1 + 4 * (-4 + x0)), tmp9 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp11 = tmp0 >= tmp7
    tmp12 = tl.full([1], 12, tl.int64)
    tmp13 = tmp0 < tmp12
    tmp14 = tmp11 & tmp13
    tmp15 = tl.load(in_ptr0 + (2 + 4 * (-8 + x0)), tmp14 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp16 = tmp0 >= tmp12
    tl.full([1], 16, tl.int64)
    tmp19 = tl.load(in_ptr0 + (3 + 4 * (-12 + x0)), tmp16 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp20 = tl.where(tmp14, tmp15, tmp19)
    tmp21 = tl.where(tmp9, tmp10, tmp20)
    tmp22 = tl.where(tmp4, tmp5, tmp21)
    tl.store(out_ptr0 + x0, tmp22, xmask)


@triton.jit
def triton_per_fused__softmax_1(in_ptr0, out_ptr2, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    xnumel = 16
    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 + (r1 + 16 * x0), xmask, other=0.0)
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.where(xmask, tmp3, float('-inf'))
    tmp6 = triton_helpers.max2(tmp5, 1)[:, None]
    tmp7 = tmp2 - tmp6
    tmp8 = 0.5
    tmp9 = tmp7 * tmp8
    tmp10 = tl_math.exp(tmp9)
    tmp11 = tl.broadcast_to(tmp10, [XBLOCK, RBLOCK])
    tmp13 = tl.where(xmask, tmp11, 0)
    tmp14 = tl.sum(tmp13, 1)[:, None]
    tmp15 = tmp10 / tmp14
    tl.store(out_ptr2 + (r1 + 16 * x0), tmp15, xmask)


@triton.jit
def triton_poi_fused_add_cat_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    x1 = xindex // 4
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = x0
    tl.full([1], 0, tl.int64)
    tmp4 = tl.full([1], 1, tl.int64)
    tmp5 = tmp1 < tmp4
    tmp6 = tl.load(in_ptr0 + x1, tmp5 & xmask, eviction_policy='evict_last',
        other=0.0)
    tmp7 = tmp1 >= tmp4
    tmp8 = tl.full([1], 2, tl.int64)
    tmp9 = tmp1 < tmp8
    tmp10 = tmp7 & tmp9
    tmp11 = tl.load(in_ptr0 + (4 + x1), tmp10 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp12 = tmp1 >= tmp8
    tmp13 = tl.full([1], 3, tl.int64)
    tmp14 = tmp1 < tmp13
    tmp15 = tmp12 & tmp14
    tmp16 = tl.load(in_ptr0 + (8 + x1), tmp15 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp17 = tmp1 >= tmp13
    tl.full([1], 4, tl.int64)
    tmp20 = tl.load(in_ptr0 + (12 + x1), tmp17 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp21 = tl.where(tmp15, tmp16, tmp20)
    tmp22 = tl.where(tmp10, tmp11, tmp21)
    tmp23 = tl.where(tmp5, tmp6, tmp22)
    tmp24 = tmp0 + tmp23
    tl.store(in_out_ptr0 + x2, tmp24, xmask)


@triton.jit
def triton_poi_fused_add_relu_threshold_backward_3(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
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x2, xmask)
    tmp2 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp4 = tl.full([1], 0, tl.int32)
    tmp5 = triton_helpers.maximum(tmp4, tmp3)
    tmp6 = tmp0 + tmp5
    tmp7 = 0.0
    tmp8 = tmp5 <= tmp7
    tl.store(out_ptr0 + x2, tmp6, xmask)
    tl.store(out_ptr1 + x2, tmp8, xmask)


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, primals_14, primals_15, primals_16, primals_17, primals_18
        ) = 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, 4), (4, 1))
    assert_size_stride(primals_4, (4,), (1,))
    assert_size_stride(primals_5, (4, 4), (4, 1))
    assert_size_stride(primals_6, (4,), (1,))
    assert_size_stride(primals_7, (4, 4), (4, 1))
    assert_size_stride(primals_8, (4,), (1,))
    assert_size_stride(primals_9, (4, 4), (4, 1))
    assert_size_stride(primals_10, (4,), (1,))
    assert_size_stride(primals_11, (4, 4), (4, 1))
    assert_size_stride(primals_12, (4,), (1,))
    assert_size_stride(primals_13, (4, 4), (4, 1))
    assert_size_stride(primals_14, (4,), (1,))
    assert_size_stride(primals_15, (4, 4), (4, 1))
    assert_size_stride(primals_16, (4,), (1,))
    assert_size_stride(primals_17, (4, 4), (4, 1))
    assert_size_stride(primals_18, (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_4, primals_2, reinterpret_tensor(
            primals_3, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf0)
        del primals_4
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_6, primals_1, reinterpret_tensor(
            primals_5, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf1)
        del primals_5
        del primals_6
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_8, primals_1, reinterpret_tensor(
            primals_7, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf2)
        del primals_7
        del primals_8
        buf3 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_cat_0[grid(16)](buf0, buf3, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf4 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        triton_poi_fused_cat_0[grid(16)](buf2, buf4, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf5 = reinterpret_tensor(buf2, (16, 1), (1, 1), 0)
        del buf2
        triton_poi_fused_cat_0[grid(16)](buf1, buf5, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf6 = empty_strided_cuda((16, 16), (16, 1), torch.float32)
        extern_kernels.mm(buf3, reinterpret_tensor(buf5, (1, 16), (0, 1), 0
            ), out=buf6)
        buf9 = empty_strided_cuda((16, 16), (16, 1), torch.float32)
        triton_per_fused__softmax_1[grid(16)](buf6, buf9, 16, 16, XBLOCK=8,
            num_warps=2, num_stages=1)
        buf10 = reinterpret_tensor(buf1, (16, 1), (1, 1), 0)
        del buf1
        extern_kernels.mm(buf9, buf4, out=buf10)
        buf11 = buf0
        del buf0
        triton_poi_fused_add_cat_2[grid(16)](buf11, buf10, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf12 = reinterpret_tensor(buf10, (4, 4), (4, 1), 0)
        del buf10
        extern_kernels.mm(buf11, reinterpret_tensor(primals_9, (4, 4), (1, 
            4), 0), out=buf12)
        buf13 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf29 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
        triton_poi_fused_add_relu_threshold_backward_3[grid(16)](buf11,
            buf12, primals_10, buf13, buf29, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        del primals_10
        buf14 = buf12
        del buf12
        extern_kernels.addmm(primals_12, primals_1, reinterpret_tensor(
            primals_11, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf14)
        del primals_11
        del primals_12
        buf15 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_14, buf13, reinterpret_tensor(
            primals_13, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf15)
        del primals_14
        buf16 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_16, buf13, reinterpret_tensor(
            primals_15, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf16)
        del primals_16
        buf17 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        triton_poi_fused_cat_0[grid(16)](buf14, buf17, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf18 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        triton_poi_fused_cat_0[grid(16)](buf16, buf18, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf19 = reinterpret_tensor(buf16, (16, 1), (1, 1), 0)
        del buf16
        triton_poi_fused_cat_0[grid(16)](buf15, buf19, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf20 = buf6
        del buf6
        extern_kernels.mm(buf17, reinterpret_tensor(buf19, (1, 16), (0, 1),
            0), out=buf20)
        buf23 = empty_strided_cuda((16, 16), (16, 1), torch.float32)
        triton_per_fused__softmax_1[grid(16)](buf20, buf23, 16, 16, XBLOCK=
            8, num_warps=2, num_stages=1)
        del buf20
        buf24 = reinterpret_tensor(buf15, (16, 1), (1, 1), 0)
        del buf15
        extern_kernels.mm(buf23, buf18, out=buf24)
        buf25 = buf14
        del buf14
        triton_poi_fused_add_cat_2[grid(16)](buf25, buf24, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf26 = reinterpret_tensor(buf24, (4, 4), (4, 1), 0)
        del buf24
        extern_kernels.mm(buf25, reinterpret_tensor(primals_17, (4, 4), (1,
            4), 0), out=buf26)
        buf27 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf28 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
        triton_poi_fused_add_relu_threshold_backward_3[grid(16)](buf25,
            buf26, primals_18, buf27, buf28, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        del buf26
        del primals_18
    return (buf27, primals_1, primals_2, buf3, buf9, buf11, buf13, buf17,
        buf23, buf25, buf28, primals_17, reinterpret_tensor(buf18, (1, 16),
        (1, 1), 0), buf19, primals_15, primals_13, buf29, primals_9,
        reinterpret_tensor(buf4, (1, 16), (1, 1), 0), buf5, primals_3)


class MAB(nn.Module):

    def __init__(self, dim_X, dim_Y, dim, num_heads=4, ln=False, p=None):
        super().__init__()
        self.num_heads = num_heads
        self.fc_q = nn.Linear(dim_X, dim)
        self.fc_k = nn.Linear(dim_Y, dim)
        self.fc_v = nn.Linear(dim_Y, dim)
        self.fc_o = nn.Linear(dim, dim)
        self.ln1 = nn.LayerNorm(dim) if ln else nn.Identity()
        self.ln2 = nn.LayerNorm(dim) if ln else nn.Identity()
        self.dropout1 = nn.Dropout(p=p) if p is not None else nn.Identity()
        self.dropout2 = nn.Dropout(p=p) if p is not None else nn.Identity()

    def forward(self, X, Y, mask=None):
        Q, K, V = self.fc_q(X), self.fc_k(Y), self.fc_v(Y)
        Q_ = torch.cat(Q.chunk(self.num_heads, -1), 0)
        K_ = torch.cat(K.chunk(self.num_heads, -1), 0)
        V_ = torch.cat(V.chunk(self.num_heads, -1), 0)
        A_logits = Q_ @ K_.transpose(-2, -1) / math.sqrt(Q.shape[-1])
        if mask is not None:
            mask = torch.stack([mask] * Q.shape[-2], -2)
            mask = torch.cat([mask] * self.num_heads, 0)
            A_logits.masked_fill_(mask, -float('inf'))
            A = torch.softmax(A_logits, -1)
            A.masked_fill_(torch.isnan(A), 0.0)
        else:
            A = torch.softmax(A_logits, -1)
        attn = torch.cat((A @ V_).chunk(self.num_heads, 0), -1)
        O = self.ln1(Q + self.dropout1(attn))
        O = self.ln2(O + self.dropout2(F.relu(self.fc_o(O))))
        return O


class PMA(nn.Module):

    def __init__(self, dim_X, dim, num_inds, **kwargs):
        super().__init__()
        self.I = nn.Parameter(torch.Tensor(num_inds, dim))
        nn.init.xavier_uniform_(self.I)
        self.mab = MAB(dim, dim_X, dim, **kwargs)

    def forward(self, X, mask=None):
        I = self.I if X.dim() == 2 else self.I.repeat(X.shape[0], 1, 1)
        return self.mab(I, X, mask=mask)


class ISABNew(nn.Module):

    def __init__(self, dim_X, dim, num_inds, **kwargs):
        super().__init__()
        self.pma = PMA(dim_X, dim, num_inds, **kwargs)
        self.mab = MAB(dim_X, dim, dim, **kwargs)

    def forward(self, input_0):
        primals_1 = self.pma.I
        primals_2 = self.pma.mab.fc_q.weight
        primals_4 = self.pma.mab.fc_q.bias
        primals_3 = self.pma.mab.fc_k.weight
        primals_6 = self.pma.mab.fc_k.bias
        primals_5 = self.pma.mab.fc_v.weight
        primals_8 = self.pma.mab.fc_v.bias
        primals_7 = self.pma.mab.fc_o.weight
        primals_10 = self.pma.mab.fc_o.bias
        primals_9 = self.mab.fc_q.weight
        primals_12 = self.mab.fc_q.bias
        primals_11 = self.mab.fc_k.weight
        primals_14 = self.mab.fc_k.bias
        primals_13 = self.mab.fc_v.weight
        primals_16 = self.mab.fc_v.bias
        primals_15 = self.mab.fc_o.weight
        primals_18 = self.mab.fc_o.bias
        primals_17 = 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, primals_14,
            primals_15, primals_16, primals_17, primals_18])
        return output[0]


Measured latency_ms: 0.24445