# AOT ID: ['0_forward']
from ctypes import c_void_p, c_long, c_int
import torch
import math
import random
import os
import tempfile
from math import inf, nan
from torch._inductor.hooks import run_intermediate_hooks
from torch._inductor.utils import maybe_profile
from torch._inductor.codegen.memory_planning import _align as align
from torch import device, empty_strided
from torch._inductor.async_compile import AsyncCompile
from torch._inductor.select_algorithm import extern_kernels
from torch._inductor.codegen.multi_kernel import MultiKernelCall
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid, split_scan_grid, grid_combo_kernels, start_graph, end_graph
from torch._C import _cuda_getCurrentRawStream as get_raw_stream

aten = torch.ops.aten
inductor_ops = torch.ops.inductor
_quantized = torch.ops._quantized
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cpu = torch._C._dynamo.guards._empty_strided_cpu
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
empty_strided_xpu = torch._C._dynamo.guards._empty_strided_xpu
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
alloc_from_pool = torch.ops.inductor._alloc_from_pool
async_compile = AsyncCompile()


# kernel path: runs/run_shard_1/inductor_cache/hv/chv7m4fc52mdvsvi543asiruw7qww65fwfdxhjiuhvfn5lqumlpq.py
# Topologically Sorted Source Nodes: [Q_], Original ATen: [aten.cat]
# Source node to ATen node mapping:
#   Q_ => cat
# Graph fragment:
#   %cat : [num_users=2] = call_function[target=torch.ops.aten.cat.default](args = ([%getitem, %getitem_1, %getitem_2, %getitem_3],), kwargs = {})
triton_poi_fused_cat_0 = async_compile.triton('triton_poi_fused_cat_0', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[16], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=90, major=9, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=132), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_cat_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 4, 'num_reduction': 0, 'backend_hash': 'B098E03CDA7B8ADC90DAFFDF24A2956451D1B13F297756A5DCC209498AA53705', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@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
    tmp1 = tl.full([1], 0, tl.int64)
    tmp2 = tmp0 >= tmp1
    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
    tmp17 = tl.full([1], 16, tl.int64)
    tmp18 = tmp0 < tmp17
    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)
''', device_str='cuda')


# kernel path: runs/run_shard_1/inductor_cache/tj/ctj2xnoux3cn7w56gxs6hpicdagsiu4zb4dom4od6voujshwgzie.py
# Topologically Sorted Source Nodes: [A], Original ATen: [aten._softmax]
# Source node to ATen node mapping:
#   A => div_1, exp, sum_1
# Graph fragment:
#   %mul_tensor_1 : [num_users=2] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mm, 1), kwargs = {})
#   %amax_default_1 : [num_users=1] = call_function[target=torch.ops.aten.amax.default](args = (%mul_tensor_1, [-1], True), kwargs = {})
#   %sub_tensor_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%mul_tensor_1, %amax_default_1), kwargs = {})
#   %div_tensor_1 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sub_tensor_1, 2.0), kwargs = {})
#   %exp : [num_users=2] = call_function[target=torch.ops.aten.exp.default](args = (%div_tensor_1,), kwargs = {})
#   %sum_1 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%exp, [-1], True), kwargs = {})
#   %div_1 : [num_users=2] = call_function[target=torch.ops.aten.div.Tensor](args = (%exp, %sum_1), kwargs = {})
triton_per_fused__softmax_1 = async_compile.triton('triton_per_fused__softmax_1', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.persistent_reduction(
    size_hints=[16, 16],
    reduction_hint=ReductionHint.INNER,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32', 3: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=90, major=9, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=132), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused__softmax_1', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 1, 'num_reduction': 2, 'backend_hash': 'B098E03CDA7B8ADC90DAFFDF24A2956451D1B13F297756A5DCC209498AA53705', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False}
)
@triton.jit
def triton_per_fused__softmax_1(in_ptr0, out_ptr2, xnumel, rnumel, XBLOCK : tl.constexpr):
    xnumel = 16
    rnumel = 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, :]
    roffset = 0
    rmask = 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)
''', device_str='cuda')


# kernel path: runs/run_shard_1/inductor_cache/fq/cfqwft6dz4fpsvuwioex77z56kqurxctenywfa72old47ny4ciza.py
# Topologically Sorted Source Nodes: [attn, O], Original ATen: [aten.cat, aten.add]
# Source node to ATen node mapping:
#   O => add
#   attn => cat_3
# Graph fragment:
#   %cat_3 : [num_users=1] = call_function[target=torch.ops.aten.cat.default](args = ([%getitem_12, %getitem_13, %getitem_14, %getitem_15], -1), kwargs = {})
#   %add : [num_users=3] = call_function[target=torch.ops.aten.add.Tensor](args = (%addmm, %cat_3), kwargs = {})
triton_poi_fused_add_cat_2 = async_compile.triton('triton_poi_fused_add_cat_2', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[16], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=90, major=9, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=132), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_add_cat_2', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 5, 'num_reduction': 0, 'backend_hash': 'B098E03CDA7B8ADC90DAFFDF24A2956451D1B13F297756A5DCC209498AA53705', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@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
    tmp2 = tl.full([1], 0, tl.int64)
    tmp3 = tmp1 >= tmp2
    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
    tmp18 = tl.full([1], 4, tl.int64)
    tmp19 = tmp1 < tmp18
    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)
''', device_str='cuda')


# kernel path: runs/run_shard_1/inductor_cache/vi/cvia7hnsmbaj62zpkiccjajpbfnpgvwttuvei34ggjsqqzpejdng.py
# Topologically Sorted Source Nodes: [relu, O_1], Original ATen: [aten.relu, aten.add, aten.threshold_backward]
# Source node to ATen node mapping:
#   O_1 => add_1
#   relu => relu
# Graph fragment:
#   %add_tensor_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mm_default_1, %primals_10), kwargs = {})
#   %relu : [num_users=2] = call_function[target=torch.ops.aten.relu.default](args = (%add_tensor_1,), kwargs = {})
#   %add_1 : [num_users=3] = call_function[target=torch.ops.aten.add.Tensor](args = (%add, %relu), kwargs = {})
#   %le_1 : [num_users=1] = call_function[target=torch.ops.aten.le.Scalar](args = (%relu, 0), kwargs = {})
triton_poi_fused_add_relu_threshold_backward_3 = async_compile.triton('triton_poi_fused_add_relu_threshold_backward_3', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[16], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: '*i1', 5: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=90, major=9, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=132), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4, 5), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_add_relu_threshold_backward_3', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 3, 'num_reduction': 0, 'backend_hash': 'B098E03CDA7B8ADC90DAFFDF24A2956451D1B13F297756A5DCC209498AA53705', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@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)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

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)
        # Topologically Sorted Source Nodes: [Q], Original ATen: [aten.addmm]
        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)
        # Topologically Sorted Source Nodes: [K], Original ATen: [aten.addmm]
        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)
        # Topologically Sorted Source Nodes: [V], Original ATen: [aten.addmm]
        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)
        # Topologically Sorted Source Nodes: [Q_], Original ATen: [aten.cat]
        stream0 = get_raw_stream(0)
        triton_poi_fused_cat_0.run(buf0, buf3, 16, grid=grid(16), stream=stream0)
        buf4 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        # Topologically Sorted Source Nodes: [V_], Original ATen: [aten.cat]
        triton_poi_fused_cat_0.run(buf2, buf4, 16, grid=grid(16), stream=stream0)
        buf5 = reinterpret_tensor(buf2, (16, 1), (1, 1), 0); del buf2  # reuse
        # Topologically Sorted Source Nodes: [K_], Original ATen: [aten.cat]
        triton_poi_fused_cat_0.run(buf1, buf5, 16, grid=grid(16), stream=stream0)
        buf6 = empty_strided_cuda((16, 16), (16, 1), torch.float32)
        # Topologically Sorted Source Nodes: [matmul], Original ATen: [aten.mm]
        extern_kernels.mm(buf3, reinterpret_tensor(buf5, (1, 16), (0, 1), 0), out=buf6)
        buf9 = empty_strided_cuda((16, 16), (16, 1), torch.float32)
        # Topologically Sorted Source Nodes: [A], Original ATen: [aten._softmax]
        triton_per_fused__softmax_1.run(buf6, buf9, 16, 16, grid=grid(16), stream=stream0)
        buf10 = reinterpret_tensor(buf1, (16, 1), (1, 1), 0); del buf1  # reuse
        # Topologically Sorted Source Nodes: [matmul_1], Original ATen: [aten.mm]
        extern_kernels.mm(buf9, buf4, out=buf10)
        buf11 = buf0; del buf0  # reuse
        # Topologically Sorted Source Nodes: [attn, O], Original ATen: [aten.cat, aten.add]
        triton_poi_fused_add_cat_2.run(buf11, buf10, 16, grid=grid(16), stream=stream0)
        buf12 = reinterpret_tensor(buf10, (4, 4), (4, 1), 0); del buf10  # reuse
        # Topologically Sorted Source Nodes: [], Original ATen: []
        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)
        # Topologically Sorted Source Nodes: [relu, O_1], Original ATen: [aten.relu, aten.add, aten.threshold_backward]
        triton_poi_fused_add_relu_threshold_backward_3.run(buf11, buf12, primals_10, buf13, buf29, 16, grid=grid(16), stream=stream0)
        del primals_10
        buf14 = buf12; del buf12  # reuse
        # Topologically Sorted Source Nodes: [Q_1], Original ATen: [aten.addmm]
        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)
        # Topologically Sorted Source Nodes: [K_1], Original ATen: [aten.addmm]
        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)
        # Topologically Sorted Source Nodes: [V_1], Original ATen: [aten.addmm]
        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)
        # Topologically Sorted Source Nodes: [Q__1], Original ATen: [aten.cat]
        triton_poi_fused_cat_0.run(buf14, buf17, 16, grid=grid(16), stream=stream0)
        buf18 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        # Topologically Sorted Source Nodes: [V__1], Original ATen: [aten.cat]
        triton_poi_fused_cat_0.run(buf16, buf18, 16, grid=grid(16), stream=stream0)
        buf19 = reinterpret_tensor(buf16, (16, 1), (1, 1), 0); del buf16  # reuse
        # Topologically Sorted Source Nodes: [K__1], Original ATen: [aten.cat]
        triton_poi_fused_cat_0.run(buf15, buf19, 16, grid=grid(16), stream=stream0)
        buf20 = buf6; del buf6  # reuse
        # Topologically Sorted Source Nodes: [matmul_2], Original ATen: [aten.mm]
        extern_kernels.mm(buf17, reinterpret_tensor(buf19, (1, 16), (0, 1), 0), out=buf20)
        buf23 = empty_strided_cuda((16, 16), (16, 1), torch.float32)
        # Topologically Sorted Source Nodes: [A_1], Original ATen: [aten._softmax]
        triton_per_fused__softmax_1.run(buf20, buf23, 16, 16, grid=grid(16), stream=stream0)
        del buf20
        buf24 = reinterpret_tensor(buf15, (16, 1), (1, 1), 0); del buf15  # reuse
        # Topologically Sorted Source Nodes: [matmul_3], Original ATen: [aten.mm]
        extern_kernels.mm(buf23, buf18, out=buf24)
        buf25 = buf14; del buf14  # reuse
        # Topologically Sorted Source Nodes: [attn_1, O_2], Original ATen: [aten.cat, aten.add]
        triton_poi_fused_add_cat_2.run(buf25, buf24, 16, grid=grid(16), stream=stream0)
        buf26 = reinterpret_tensor(buf24, (4, 4), (4, 1), 0); del buf24  # reuse
        # Topologically Sorted Source Nodes: [], Original ATen: []
        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)
        # Topologically Sorted Source Nodes: [relu_1, O_3], Original ATen: [aten.relu, aten.add, aten.threshold_backward]
        triton_poi_fused_add_relu_threshold_backward_3.run(buf25, buf26, primals_18, buf27, buf28, 16, grid=grid(16), stream=stream0)
        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, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    primals_1 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_2 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_3 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_4 = rand_strided((4, ), (1, ), device='cuda:0', dtype=torch.float32)
    primals_5 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_6 = rand_strided((4, ), (1, ), device='cuda:0', dtype=torch.float32)
    primals_7 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_8 = rand_strided((4, ), (1, ), device='cuda:0', dtype=torch.float32)
    primals_9 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_10 = rand_strided((4, ), (1, ), device='cuda:0', dtype=torch.float32)
    primals_11 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_12 = rand_strided((4, ), (1, ), device='cuda:0', dtype=torch.float32)
    primals_13 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_14 = rand_strided((4, ), (1, ), device='cuda:0', dtype=torch.float32)
    primals_15 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_16 = rand_strided((4, ), (1, ), device='cuda:0', dtype=torch.float32)
    primals_17 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_18 = rand_strided((4, ), (1, ), device='cuda:0', dtype=torch.float32)
    fn = lambda: 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 print_performance(fn, times=times, repeat=repeat)


if __name__ == "__main__":
    from torch._inductor.wrapper_benchmark import compiled_module_main
    compiled_module_main('None', benchmark_compiled_module)

import math
import torch
import torch.nn.functional as F
import torch.nn as nn


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 ISAB(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, X, mask=None):
        return self.mab(X, self.pma(X, mask=mask))


def get_inputs():
    return [torch.rand([4, 4])]


def get_init_inputs():
    return [[], {'dim_X': 4, 'dim': 4, 'num_inds': 4}]

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]
