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 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_clone_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 4
    xnumel = 16
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x1 = xindex % 4
    x2 = xindex // 4
    y0 = yindex
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * x1), xmask & ymask,
        eviction_policy='evict_last')
    tl.store(out_ptr0 + (x3 + 16 * y0), tmp0, xmask & ymask)


@triton.jit
def triton_poi_fused_clone_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 4
    xnumel = 16
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x1 = xindex % 4
    x2 = xindex // 4
    y0 = yindex
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (128 + y0 + 4 * x2 + 16 * x1), xmask & ymask,
        eviction_policy='evict_last')
    tl.store(out_ptr0 + (x3 + 16 * y0), tmp0, xmask & ymask)


@triton.jit
def triton_poi_fused_clone_2(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 4
    xnumel = 16
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x1 = xindex % 4
    x2 = xindex // 4
    y0 = yindex
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (192 + y0 + 4 * x2 + 16 * x1), xmask & ymask,
        eviction_policy='evict_last')
    tl.store(out_ptr0 + (x3 + 16 * y0), tmp0, xmask & ymask)


@triton.jit
def triton_poi_fused_clone_3(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 4
    xnumel = 16
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x1 = xindex % 4
    x2 = xindex // 4
    y0 = yindex
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (64 + y0 + 4 * x2 + 16 * x1), xmask & ymask,
        eviction_policy='evict_last')
    tl.store(out_ptr0 + (x3 + 16 * y0), tmp0, xmask & ymask)


@triton.jit
def triton_poi_fused_add_4(in_out_ptr0, in_out_ptr1, in_out_ptr2,
    in_out_ptr3, in_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_out_ptr1 + x2, xmask)
    tmp5 = tl.load(in_out_ptr2 + x2, xmask)
    tmp7 = tl.load(in_out_ptr3 + x2, xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp3 + tmp1
    tmp6 = tmp5 + tmp1
    tmp8 = tmp7 + tmp1
    tl.store(in_out_ptr0 + x2, tmp2, xmask)
    tl.store(in_out_ptr1 + x2, tmp4, xmask)
    tl.store(in_out_ptr2 + x2, tmp6, xmask)
    tl.store(in_out_ptr3 + x2, tmp8, xmask)


@triton.jit
def triton_poi_fused__softmax_5(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
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tmp0 - tmp7
    tmp9 = tl_math.exp(tmp8)
    tl.store(out_ptr0 + x2, tmp9, xmask)


@triton.jit
def triton_poi_fused__softmax_6(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
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tl.store(out_ptr0 + x2, tmp8, xmask)


@triton.jit
def triton_poi_fused_stack_7(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4
    x0 = xindex % 4
    x2 = xindex
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x0 + 16 * x1), tmp4 & xmask, other=0.0)
    tmp6 = tl.load(in_ptr1 + 4 * x0, tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp7 = tmp5 * tmp6
    tmp8 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), tmp4 & xmask, other=0.0)
    tmp9 = tl.load(in_ptr1 + (1 + 4 * x0), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp10 = tmp8 * tmp9
    tmp11 = tmp7 + tmp10
    tmp12 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), tmp4 & xmask, other=0.0)
    tmp13 = tl.load(in_ptr1 + (2 + 4 * x0), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp14 = tmp12 * tmp13
    tmp15 = tmp11 + tmp14
    tmp16 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), tmp4 & xmask, other=0.0)
    tmp17 = tl.load(in_ptr1 + (3 + 4 * x0), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp18 = tmp16 * tmp17
    tmp19 = tmp15 + tmp18
    tmp20 = 4.0
    tmp21 = tmp19 / tmp20
    tmp22 = tl.full(tmp21.shape, 0.0, tmp21.dtype)
    tmp23 = tl.where(tmp4, tmp21, tmp22)
    tmp24 = tmp0 >= tmp3
    tmp25 = tl.full([1], 8, tl.int64)
    tmp26 = tmp0 < tmp25
    tmp27 = tmp24 & tmp26
    tmp28 = tl.load(in_ptr0 + (64 + x0 + 16 * (-4 + x1)), tmp27 & xmask,
        other=0.0)
    tmp29 = tl.load(in_ptr2 + 4 * x0, tmp27 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp30 = tmp28 * tmp29
    tmp31 = tl.load(in_ptr0 + (68 + x0 + 16 * (-4 + x1)), tmp27 & xmask,
        other=0.0)
    tmp32 = tl.load(in_ptr2 + (1 + 4 * x0), tmp27 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp33 = tmp31 * tmp32
    tmp34 = tmp30 + tmp33
    tmp35 = tl.load(in_ptr0 + (72 + x0 + 16 * (-4 + x1)), tmp27 & xmask,
        other=0.0)
    tmp36 = tl.load(in_ptr2 + (2 + 4 * x0), tmp27 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp37 = tmp35 * tmp36
    tmp38 = tmp34 + tmp37
    tmp39 = tl.load(in_ptr0 + (76 + x0 + 16 * (-4 + x1)), tmp27 & xmask,
        other=0.0)
    tmp40 = tl.load(in_ptr2 + (3 + 4 * x0), tmp27 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp41 = tmp39 * tmp40
    tmp42 = tmp38 + tmp41
    tmp43 = tmp42 / tmp20
    tmp44 = tl.full(tmp43.shape, 0.0, tmp43.dtype)
    tmp45 = tl.where(tmp27, tmp43, tmp44)
    tmp46 = tmp0 >= tmp25
    tmp47 = tl.full([1], 12, tl.int64)
    tmp48 = tmp0 < tmp47
    tmp49 = tmp46 & tmp48
    tmp50 = tl.load(in_ptr0 + (128 + x0 + 16 * (-8 + x1)), tmp49 & xmask,
        other=0.0)
    tmp51 = tl.load(in_ptr3 + 4 * x0, tmp49 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp52 = tmp50 * tmp51
    tmp53 = tl.load(in_ptr0 + (132 + x0 + 16 * (-8 + x1)), tmp49 & xmask,
        other=0.0)
    tmp54 = tl.load(in_ptr3 + (1 + 4 * x0), tmp49 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp55 = tmp53 * tmp54
    tmp56 = tmp52 + tmp55
    tmp57 = tl.load(in_ptr0 + (136 + x0 + 16 * (-8 + x1)), tmp49 & xmask,
        other=0.0)
    tmp58 = tl.load(in_ptr3 + (2 + 4 * x0), tmp49 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp59 = tmp57 * tmp58
    tmp60 = tmp56 + tmp59
    tmp61 = tl.load(in_ptr0 + (140 + x0 + 16 * (-8 + x1)), tmp49 & xmask,
        other=0.0)
    tmp62 = tl.load(in_ptr3 + (3 + 4 * x0), tmp49 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp63 = tmp61 * tmp62
    tmp64 = tmp60 + tmp63
    tmp65 = tmp64 / tmp20
    tmp66 = tl.full(tmp65.shape, 0.0, tmp65.dtype)
    tmp67 = tl.where(tmp49, tmp65, tmp66)
    tmp68 = tmp0 >= tmp47
    tl.full([1], 16, tl.int64)
    tmp71 = tl.load(in_ptr0 + (192 + x0 + 16 * (-12 + x1)), tmp68 & xmask,
        other=0.0)
    tmp72 = tl.load(in_ptr4 + 4 * x0, tmp68 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp73 = tmp71 * tmp72
    tmp74 = tl.load(in_ptr0 + (196 + x0 + 16 * (-12 + x1)), tmp68 & xmask,
        other=0.0)
    tmp75 = tl.load(in_ptr4 + (1 + 4 * x0), tmp68 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp76 = tmp74 * tmp75
    tmp77 = tmp73 + tmp76
    tmp78 = tl.load(in_ptr0 + (200 + x0 + 16 * (-12 + x1)), tmp68 & xmask,
        other=0.0)
    tmp79 = tl.load(in_ptr4 + (2 + 4 * x0), tmp68 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp80 = tmp78 * tmp79
    tmp81 = tmp77 + tmp80
    tmp82 = tl.load(in_ptr0 + (204 + x0 + 16 * (-12 + x1)), tmp68 & xmask,
        other=0.0)
    tmp83 = tl.load(in_ptr4 + (3 + 4 * x0), tmp68 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp84 = tmp82 * tmp83
    tmp85 = tmp81 + tmp84
    tmp86 = tmp85 / tmp20
    tmp87 = tl.full(tmp86.shape, 0.0, tmp86.dtype)
    tmp88 = tl.where(tmp68, tmp86, tmp87)
    tmp89 = tl.where(tmp49, tmp67, tmp88)
    tmp90 = tl.where(tmp27, tmp45, tmp89)
    tmp91 = tl.where(tmp4, tmp23, tmp90)
    tl.store(out_ptr0 + x2, tmp91, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = 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, (4,), (1,))
    assert_size_stride(primals_4, (1, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_clone_0[grid(4, 16)](primals_1, buf0, 4, 16,
            XBLOCK=16, YBLOCK=4, num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf1)
        buf12 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_clone_1[grid(4, 16)](primals_1, buf12, 4, 16,
            XBLOCK=16, YBLOCK=4, num_warps=1, num_stages=1)
        buf13 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf12, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf13)
        buf18 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_clone_2[grid(4, 16)](primals_1, buf18, 4, 16,
            XBLOCK=16, YBLOCK=4, num_warps=1, num_stages=1)
        buf19 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf18, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf19)
        buf6 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_clone_3[grid(4, 16)](primals_1, buf6, 4, 16,
            XBLOCK=16, YBLOCK=4, num_warps=1, num_stages=1)
        buf7 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf6, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf7)
        del primals_2
        buf2 = reinterpret_tensor(buf1, (4, 4, 4), (16, 4, 1), 0)
        del buf1
        buf8 = reinterpret_tensor(buf7, (4, 4, 4), (16, 4, 1), 0)
        del buf7
        buf14 = reinterpret_tensor(buf13, (4, 4, 4), (16, 4, 1), 0)
        del buf13
        buf20 = reinterpret_tensor(buf19, (4, 4, 4), (16, 4, 1), 0)
        del buf19
        triton_poi_fused_add_4[grid(64)](buf2, buf8, buf14, buf20,
            primals_3, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del primals_3
        buf3 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf2, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_4, (4, 1), (1, 4), 0), out=buf3)
        buf4 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused__softmax_5[grid(16)](buf3, buf4, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf5 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused__softmax_6[grid(16)](buf4, buf5, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf9 = reinterpret_tensor(buf4, (16, 1), (1, 1), 0)
        del buf4
        extern_kernels.mm(reinterpret_tensor(buf8, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_4, (4, 1), (1, 4), 0), out=buf9)
        buf10 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused__softmax_5[grid(16)](buf9, buf10, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf11 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused__softmax_6[grid(16)](buf10, buf11, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf15 = reinterpret_tensor(buf10, (16, 1), (1, 1), 0)
        del buf10
        extern_kernels.mm(reinterpret_tensor(buf14, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_4, (4, 1), (1, 4), 0), out=buf15)
        buf16 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused__softmax_5[grid(16)](buf15, buf16, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf17 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused__softmax_6[grid(16)](buf16, buf17, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf21 = reinterpret_tensor(buf16, (16, 1), (1, 1), 0)
        del buf16
        extern_kernels.mm(reinterpret_tensor(buf20, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_4, (4, 1), (1, 4), 0), out=buf21)
        buf22 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused__softmax_5[grid(16)](buf21, buf22, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf23 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused__softmax_6[grid(16)](buf22, buf23, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del buf22
        buf24 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        triton_poi_fused_stack_7[grid(64)](primals_1, buf5, buf11, buf17,
            buf23, buf24, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del buf11
        del buf17
        del buf23
        del buf5
    return reinterpret_tensor(buf24, (4, 4, 4), (16, 4, 1), 0
        ), primals_1, reinterpret_tensor(buf0, (16, 4), (4, 1), 0
        ), reinterpret_tensor(buf2, (16, 4), (4, 1), 0
        ), buf3, reinterpret_tensor(buf6, (16, 4), (4, 1), 0
        ), reinterpret_tensor(buf8, (16, 4), (4, 1), 0
        ), buf9, reinterpret_tensor(buf12, (16, 4), (4, 1), 0
        ), reinterpret_tensor(buf14, (16, 4), (4, 1), 0
        ), buf15, reinterpret_tensor(buf18, (16, 4), (4, 1), 0
        ), reinterpret_tensor(buf20, (16, 4), (4, 1), 0), buf21, primals_4


class AttentionCollapseNew(nn.Module):
    """Collapsing over the channels with attention.

    Parameters
    ----------
    n_channels : int
        Number of input channels.

    Attributes
    ----------
    affine : nn.Module
        Fully connected layer performing linear mapping.

    context_vector : nn.Module
        Fully connected layer encoding direction importance.
    """

    def __init__(self, n_channels):
        super().__init__()
        self.affine = nn.Linear(n_channels, n_channels)
        self.context_vector = nn.Linear(n_channels, 1, bias=False)

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


Measured latency_ms: 0.027376