import torch
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


@triton.jit
def triton_poi_fused_cat_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 32
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 2
    x1 = xindex // 2
    x2 = xindex
    tmp5 = tl.load(in_ptr0 + 0)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK])
    tmp10 = tl.load(in_ptr0 + 1)
    tmp11 = tl.broadcast_to(tmp10, [XBLOCK])
    tmp16 = tl.load(in_ptr0 + 2)
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK])
    tmp22 = tl.load(in_ptr0 + 3)
    tmp23 = tl.broadcast_to(tmp22, [XBLOCK])
    tmp0 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 1, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp7 = tl.load(in_ptr1 + 4 * x1, tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp8 = tmp6 - tmp7
    tmp9 = tmp8 * tmp8
    tmp12 = tl.load(in_ptr1 + (1 + 4 * x1), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp13 = tmp11 - tmp12
    tmp14 = tmp13 * tmp13
    tmp15 = tmp9 + tmp14
    tmp18 = tl.load(in_ptr1 + (2 + 4 * x1), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp19 = tmp17 - tmp18
    tmp20 = tmp19 * tmp19
    tmp21 = tmp15 + tmp20
    tmp24 = tl.load(in_ptr1 + (3 + 4 * x1), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp25 = tmp23 - tmp24
    tmp26 = tmp25 * tmp25
    tmp27 = tmp21 + tmp26
    tmp28 = -tmp27
    tmp29 = tl.full(tmp28.shape, 0.0, tmp28.dtype)
    tmp30 = tl.where(tmp4, tmp28, tmp29)
    tmp31 = tmp0 >= tmp3
    tl.full([1], 2, tl.int64)
    tmp34 = tl.load(in_ptr1 + (64 + 4 * x1), tmp31 & xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp35 = tmp6 - tmp34
    tmp36 = tmp35 * tmp35
    tmp37 = tl.load(in_ptr1 + (65 + 4 * x1), tmp31 & xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp38 = tmp11 - tmp37
    tmp39 = tmp38 * tmp38
    tmp40 = tmp36 + tmp39
    tmp41 = tl.load(in_ptr1 + (66 + 4 * x1), tmp31 & xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp42 = tmp17 - tmp41
    tmp43 = tmp42 * tmp42
    tmp44 = tmp40 + tmp43
    tmp45 = tl.load(in_ptr1 + (67 + 4 * x1), tmp31 & xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp46 = tmp23 - tmp45
    tmp47 = tmp46 * tmp46
    tmp48 = tmp44 + tmp47
    tmp49 = -tmp48
    tmp50 = tl.full(tmp49.shape, 0.0, tmp49.dtype)
    tmp51 = tl.where(tmp31, tmp49, tmp50)
    tmp52 = tl.where(tmp4, tmp30, tmp51)
    tl.store(out_ptr0 + x2, tmp52, xmask)


@triton.jit
def triton_poi_fused__softmax_max_1(in_ptr0, out_ptr0, out_ptr1, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 8 * x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (1 + 8 * x0), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (2 + 8 * x0), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (3 + 8 * x0), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (4 + 8 * x0), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (5 + 8 * x0), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (6 + 8 * x0), xmask, eviction_policy='evict_last'
        )
    tmp12 = tl.load(in_ptr0 + (7 + 8 * x0), xmask, eviction_policy='evict_last'
        )
    tmp2 = triton_helpers.maximum(tmp0, tmp1)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp6 = triton_helpers.maximum(tmp2, tmp5)
    tmp9 = triton_helpers.maximum(tmp7, tmp8)
    tmp10 = triton_helpers.maximum(tmp6, tmp9)
    tmp13 = triton_helpers.maximum(tmp11, tmp12)
    tmp14 = triton_helpers.maximum(tmp10, tmp13)
    tmp15 = tmp2 - tmp14
    tmp16 = tl_math.exp(tmp15)
    tmp17 = tmp5 - tmp14
    tmp18 = tl_math.exp(tmp17)
    tmp19 = tmp16 + tmp18
    tmp20 = tmp9 - tmp14
    tmp21 = tl_math.exp(tmp20)
    tmp22 = tmp19 + tmp21
    tmp23 = tmp13 - tmp14
    tmp24 = tl_math.exp(tmp23)
    tmp25 = tmp22 + tmp24
    tl.store(out_ptr0 + x0, tmp14, xmask)
    tl.store(out_ptr1 + x0, tmp25, xmask)


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


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (1, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 2), (8, 2, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_cat_0[grid(32)](arg1_1, arg0_1, buf0, 32, XBLOCK=
            32, num_warps=1, num_stages=1)
        del arg0_1
        del arg1_1
        buf1 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        buf2 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        triton_poi_fused__softmax_max_1[grid(4)](buf0, buf1, buf2, 4,
            XBLOCK=4, num_warps=1, num_stages=1)
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused__softmax_max_squeeze_2[grid(16)](buf0, buf1, buf2,
            buf3, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del buf0
        del buf1
        del buf2
    return buf3,


class CnptAttentionNew(nn.Module):

    def __init__(self, in_dim, out_dim):
        super(CnptAttentionNew, self).__init__()
        self.softmax = nn.Softmax(dim=-1)

    def forward(self, input_0, input_1):
        arg1_1 = input_0
        arg0_1 = input_1
        output = call([arg0_1, arg1_1])
        return output[0]


Measured latency_ms: 0.016254