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_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 512
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 8
    x2 = xindex // 32
    x3 = xindex // 8
    x4 = xindex
    tmp0 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (16 * x2 + x0), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp9 = tl.load(in_ptr0 + (4 * x3 + (-4 + x0)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tl.store(out_ptr0 + x4, tmp10, xmask)


@triton.jit
def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 64
    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_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 64
    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_mean_mul_sum_3(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 % 4
    x1 = xindex // 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask)
    tmp1 = tl.load(in_ptr1 + 16 * x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (4 + x0 + 64 * x1), xmask)
    tmp4 = tl.load(in_ptr1 + (1 + 16 * x1), xmask, eviction_policy='evict_last'
        )
    tmp7 = tl.load(in_ptr0 + (8 + x0 + 64 * x1), xmask)
    tmp8 = tl.load(in_ptr1 + (2 + 16 * x1), xmask, eviction_policy='evict_last'
        )
    tmp11 = tl.load(in_ptr0 + (12 + x0 + 64 * x1), xmask)
    tmp12 = tl.load(in_ptr1 + (3 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp15 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask)
    tmp16 = tl.load(in_ptr1 + (4 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp18 = tl.load(in_ptr0 + (20 + x0 + 64 * x1), xmask)
    tmp19 = tl.load(in_ptr1 + (5 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp22 = tl.load(in_ptr0 + (24 + x0 + 64 * x1), xmask)
    tmp23 = tl.load(in_ptr1 + (6 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp26 = tl.load(in_ptr0 + (28 + x0 + 64 * x1), xmask)
    tmp27 = tl.load(in_ptr1 + (7 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp31 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask)
    tmp32 = tl.load(in_ptr1 + (8 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp34 = tl.load(in_ptr0 + (36 + x0 + 64 * x1), xmask)
    tmp35 = tl.load(in_ptr1 + (9 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp38 = tl.load(in_ptr0 + (40 + x0 + 64 * x1), xmask)
    tmp39 = tl.load(in_ptr1 + (10 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp42 = tl.load(in_ptr0 + (44 + x0 + 64 * x1), xmask)
    tmp43 = tl.load(in_ptr1 + (11 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp47 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask)
    tmp48 = tl.load(in_ptr1 + (12 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp50 = tl.load(in_ptr0 + (52 + x0 + 64 * x1), xmask)
    tmp51 = tl.load(in_ptr1 + (13 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp54 = tl.load(in_ptr0 + (56 + x0 + 64 * x1), xmask)
    tmp55 = tl.load(in_ptr1 + (14 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp58 = tl.load(in_ptr0 + (60 + x0 + 64 * x1), xmask)
    tmp59 = tl.load(in_ptr1 + (15 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tmp17 = tmp15 * tmp16
    tmp20 = tmp18 * tmp19
    tmp21 = tmp17 + tmp20
    tmp24 = tmp22 * tmp23
    tmp25 = tmp21 + tmp24
    tmp28 = tmp26 * tmp27
    tmp29 = tmp25 + tmp28
    tmp30 = tmp14 + tmp29
    tmp33 = tmp31 * tmp32
    tmp36 = tmp34 * tmp35
    tmp37 = tmp33 + tmp36
    tmp40 = tmp38 * tmp39
    tmp41 = tmp37 + tmp40
    tmp44 = tmp42 * tmp43
    tmp45 = tmp41 + tmp44
    tmp46 = tmp30 + tmp45
    tmp49 = tmp47 * tmp48
    tmp52 = tmp50 * tmp51
    tmp53 = tmp49 + tmp52
    tmp56 = tmp54 * tmp55
    tmp57 = tmp53 + tmp56
    tmp60 = tmp58 * tmp59
    tmp61 = tmp57 + tmp60
    tmp62 = tmp46 + tmp61
    tmp63 = 4.0
    tmp64 = tmp62 / tmp63
    tl.store(out_ptr0 + x2, tmp64, xmask)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (1, 8), (8, 1))
    assert_size_stride(primals_3, (1,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4, 8), (128, 32, 8, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_cat_0[grid(512)](primals_1, buf0, 512, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf2 = empty_strided_cuda((64, 1), (1, 1), torch.float32)
        extern_kernels.addmm(primals_3, reinterpret_tensor(buf0, (64, 8), (
            8, 1), 0), reinterpret_tensor(primals_2, (8, 1), (1, 8), 0),
            alpha=1, beta=1, out=buf2)
        del primals_2
        del primals_3
        buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused__softmax_1[grid(64)](buf2, buf3, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused__softmax_2[grid(64)](buf3, buf4, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del buf3
        buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_mean_mul_sum_3[grid(16)](primals_1, buf4, buf5, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        del buf4
    return buf5, primals_1, reinterpret_tensor(buf0, (64, 8), (8, 1), 0), buf2


class AttentionNew(nn.Module):

    def __init__(self, n_h):
        super(AttentionNew, self).__init__()
        self.linear = nn.Linear(n_h * 2, 1)
        self.softmax = nn.Softmax(dim=2)

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


Measured latency_ms: 0.064952