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


@triton.jit
def triton_poi_fused_add_mul_sum_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    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 % 4
    x2 = xindex // 16
    x3 = xindex % 16
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (x3 + 64 * x2), xmask)
    tmp6 = tl.load(in_ptr0 + (4 + x1), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + (16 + x3 + 64 * x2), xmask)
    tmp12 = tl.load(in_ptr0 + (8 + x1), xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr1 + (32 + x3 + 64 * x2), xmask)
    tmp18 = tl.load(in_ptr0 + (12 + x1), xmask, eviction_policy='evict_last')
    tmp21 = tl.load(in_ptr1 + (48 + x3 + 64 * x2), xmask)
    tmp24 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
    tmp27 = tl.load(in_ptr3 + (x3 + 64 * x2), xmask)
    tmp29 = tl.load(in_ptr2 + (4 + x1), xmask, eviction_policy='evict_last')
    tmp32 = tl.load(in_ptr3 + (16 + x3 + 64 * x2), xmask)
    tmp35 = tl.load(in_ptr2 + (8 + x1), xmask, eviction_policy='evict_last')
    tmp38 = tl.load(in_ptr3 + (32 + x3 + 64 * x2), xmask)
    tmp41 = tl.load(in_ptr2 + (12 + x1), xmask, eviction_policy='evict_last')
    tmp44 = tl.load(in_ptr3 + (48 + x3 + 64 * x2), xmask)
    tmp1 = 1.0
    tmp2 = tmp0 == tmp1
    tmp3 = tmp2.to(tl.float32)
    tmp5 = tmp3 * tmp4
    tmp7 = tmp6 == tmp1
    tmp8 = tmp7.to(tl.float32)
    tmp10 = tmp8 * tmp9
    tmp11 = tmp5 + tmp10
    tmp13 = tmp12 == tmp1
    tmp14 = tmp13.to(tl.float32)
    tmp16 = tmp14 * tmp15
    tmp17 = tmp11 + tmp16
    tmp19 = tmp18 == tmp1
    tmp20 = tmp19.to(tl.float32)
    tmp22 = tmp20 * tmp21
    tmp23 = tmp17 + tmp22
    tmp25 = tmp24 == tmp1
    tmp26 = tmp25.to(tl.float32)
    tmp28 = tmp26 * tmp27
    tmp30 = tmp29 == tmp1
    tmp31 = tmp30.to(tl.float32)
    tmp33 = tmp31 * tmp32
    tmp34 = tmp28 + tmp33
    tmp36 = tmp35 == tmp1
    tmp37 = tmp36.to(tl.float32)
    tmp39 = tmp37 * tmp38
    tmp40 = tmp34 + tmp39
    tmp42 = tmp41 == tmp1
    tmp43 = tmp42.to(tl.float32)
    tmp45 = tmp43 * tmp44
    tmp46 = tmp40 + tmp45
    tmp47 = tmp23 + tmp46
    tl.store(out_ptr0 + x4, tmp47, xmask)


@triton.jit
def triton_poi_fused_add_index_put_lift_fresh_max_1(in_out_ptr0, in_ptr0,
    in_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 16
    x1 = xindex // 16
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (x0 + 64 * x1), xmask)
    tmp3 = tl.load(in_ptr0 + (16 + x0), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (16 + x0 + 64 * x1), xmask)
    tmp7 = tl.load(in_ptr0 + (32 + x0), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (32 + x0 + 64 * x1), xmask)
    tmp11 = tl.load(in_ptr0 + (48 + x0), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr1 + (48 + x0 + 64 * x1), xmask)
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = triton_helpers.maximum(tmp2, tmp5)
    tmp9 = tmp7 + tmp8
    tmp10 = triton_helpers.maximum(tmp6, tmp9)
    tmp13 = tmp11 + tmp12
    tmp14 = triton_helpers.maximum(tmp10, tmp13)
    tmp15 = 1.0
    tmp16 = tmp14 <= tmp15
    tmp17 = tl.where(tmp16, tmp15, tmp14)
    tl.store(in_out_ptr0 + x2, tmp17, xmask)


@triton.jit
def triton_poi_fused_add_div_2(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex % 64
    x4 = xindex
    x0 = xindex % 16
    x2 = xindex // 64
    tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x4, xmask)
    tmp3 = tl.load(in_ptr2 + (x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 / tmp3
    tl.store(out_ptr0 + x4, tmp4, xmask)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1, arg4_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4), (4, 1))
    assert_size_stride(arg2_1, (4, 4), (4, 1))
    assert_size_stride(arg3_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg4_1, (4, 4, 4, 4), (64, 16, 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_add_mul_sum_0[grid(64)](arg1_1, arg0_1, arg2_1,
            arg3_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
        del arg3_1
        buf1 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
        buf2 = buf1
        del buf1
        triton_poi_fused_add_index_put_lift_fresh_max_1[grid(64)](buf2,
            buf0, arg4_1, 64, XBLOCK=64, num_warps=1, num_stages=1)
        buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_add_div_2[grid(256)](buf0, arg4_1, buf2, buf3, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        del arg4_1
        del buf0
        del buf2
    return buf3,


class NormAttnMap(nn.Module):

    def __init__(self, norm_type='cossim'):
        super(NormAttnMap, self).__init__()
        self.norm_type = norm_type

    def forward(self, attn_map):
        if self.norm_type != 'cosssim':
            norm = torch.max(attn_map, dim=1, keepdim=True)[0].detach()
        else:
            norm = torch.max(torch.abs(attn_map), dim=1, keepdim=True)[0
                ].detach()
        norm[norm <= 1] = 1
        attn = attn_map / norm
        return attn, norm


class MergeModuleNew(nn.Module):

    def __init__(self, norm_type='cossim', need_norm=True):
        super(MergeModuleNew, self).__init__()
        self.norm_type = norm_type
        self.need_norm = need_norm
        self.norm_fun = NormAttnMap(norm_type)

    def forward(self, input_0, input_1, input_2, input_3, input_4):
        arg0_1 = input_0
        arg3_1 = input_1
        arg4_1 = input_2
        arg1_1 = input_3
        arg2_1 = input_4
        output = call([arg0_1, arg1_1, arg2_1, arg3_1, arg4_1])
        return output[0]


Measured latency_ms: 0.015098