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.triton_helpers import math as tl_math
import torch.nn as nn
import torch.nn.functional as F
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_per_fused_abs_avg_pool2d_index_put_lift_fresh_mul_rsub_sub_sum_0(
    in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, out_ptr3, out_ptr4,
    out_ptr5, 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)
    r2 = rindex // 4
    r1 = rindex % 4
    r3 = rindex
    x0 = xindex
    tmp118 = tl.load(in_ptr0 + (r3 + 16 * x0), xmask, other=0.0)
    tmp124 = tl.load(in_ptr1 + (r3 + 16 * x0), xmask, other=0.0)
    tmp0 = -2 + r2
    tmp1 = tl.full([1, 1], 0, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.full([1, 1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tmp2 & tmp4
    tmp6 = -2 + r1
    tmp7 = tmp6 >= tmp1
    tmp8 = tmp6 < tmp3
    tmp9 = tmp7 & tmp8
    tmp10 = tmp5 & tmp9
    tmp11 = tl.load(in_ptr0 + (-10 + r3 + 16 * x0), tmp10 & xmask, other=0.0)
    tmp12 = -1 + r1
    tmp13 = tmp12 >= tmp1
    tmp14 = tmp12 < tmp3
    tmp15 = tmp13 & tmp14
    tmp16 = tmp5 & tmp15
    tmp17 = tl.load(in_ptr0 + (-9 + r3 + 16 * x0), tmp16 & xmask, other=0.0)
    tmp18 = tmp17 + tmp11
    tmp19 = r1
    tmp20 = tmp19 >= tmp1
    tmp21 = tmp19 < tmp3
    tmp22 = tmp20 & tmp21
    tmp23 = tmp5 & tmp22
    tmp24 = tl.load(in_ptr0 + (-8 + r3 + 16 * x0), tmp23 & xmask, other=0.0)
    tmp25 = tmp24 + tmp18
    tmp26 = 1 + r1
    tmp27 = tmp26 >= tmp1
    tmp28 = tmp26 < tmp3
    tmp29 = tmp27 & tmp28
    tmp30 = tmp5 & tmp29
    tmp31 = tl.load(in_ptr0 + (-7 + r3 + 16 * x0), tmp30 & xmask, other=0.0)
    tmp32 = tmp31 + tmp25
    tmp33 = 2 + r1
    tmp34 = tmp33 >= tmp1
    tmp35 = tmp33 < tmp3
    tmp36 = tmp34 & tmp35
    tmp37 = tmp5 & tmp36
    tmp38 = tl.load(in_ptr0 + (-6 + r3 + 16 * x0), tmp37 & xmask, other=0.0)
    tmp39 = tmp38 + tmp32
    tmp40 = -1 + r2
    tmp41 = tmp40 >= tmp1
    tmp42 = tmp40 < tmp3
    tmp43 = tmp41 & tmp42
    tmp44 = tmp43 & tmp9
    tmp45 = tl.load(in_ptr0 + (-6 + r3 + 16 * x0), tmp44 & xmask, other=0.0)
    tmp46 = tmp45 + tmp39
    tmp47 = tmp43 & tmp15
    tmp48 = tl.load(in_ptr0 + (-5 + r3 + 16 * x0), tmp47 & xmask, other=0.0)
    tmp49 = tmp48 + tmp46
    tmp50 = tmp43 & tmp22
    tmp51 = tl.load(in_ptr0 + (-4 + r3 + 16 * x0), tmp50 & xmask, other=0.0)
    tmp52 = tmp51 + tmp49
    tmp53 = tmp43 & tmp29
    tmp54 = tl.load(in_ptr0 + (-3 + r3 + 16 * x0), tmp53 & xmask, other=0.0)
    tmp55 = tmp54 + tmp52
    tmp56 = tmp43 & tmp36
    tmp57 = tl.load(in_ptr0 + (-2 + r3 + 16 * x0), tmp56 & xmask, other=0.0)
    tmp58 = tmp57 + tmp55
    tmp59 = r2
    tmp60 = tmp59 >= tmp1
    tmp61 = tmp59 < tmp3
    tmp62 = tmp60 & tmp61
    tmp63 = tmp62 & tmp9
    tmp64 = tl.load(in_ptr0 + (-2 + r3 + 16 * x0), tmp63 & xmask, other=0.0)
    tmp65 = tmp64 + tmp58
    tmp66 = tmp62 & tmp15
    tmp67 = tl.load(in_ptr0 + (-1 + r3 + 16 * x0), tmp66 & xmask, other=0.0)
    tmp68 = tmp67 + tmp65
    tmp69 = tmp62 & tmp22
    tmp70 = tl.load(in_ptr0 + (r3 + 16 * x0), tmp69 & xmask, other=0.0)
    tmp71 = tmp70 + tmp68
    tmp72 = tmp62 & tmp29
    tmp73 = tl.load(in_ptr0 + (1 + r3 + 16 * x0), tmp72 & xmask, other=0.0)
    tmp74 = tmp73 + tmp71
    tmp75 = tmp62 & tmp36
    tmp76 = tl.load(in_ptr0 + (2 + r3 + 16 * x0), tmp75 & xmask, other=0.0)
    tmp77 = tmp76 + tmp74
    tmp78 = 1 + r2
    tmp79 = tmp78 >= tmp1
    tmp80 = tmp78 < tmp3
    tmp81 = tmp79 & tmp80
    tmp82 = tmp81 & tmp9
    tmp83 = tl.load(in_ptr0 + (2 + r3 + 16 * x0), tmp82 & xmask, other=0.0)
    tmp84 = tmp83 + tmp77
    tmp85 = tmp81 & tmp15
    tmp86 = tl.load(in_ptr0 + (3 + r3 + 16 * x0), tmp85 & xmask, other=0.0)
    tmp87 = tmp86 + tmp84
    tmp88 = tmp81 & tmp22
    tmp89 = tl.load(in_ptr0 + (4 + r3 + 16 * x0), tmp88 & xmask, other=0.0)
    tmp90 = tmp89 + tmp87
    tmp91 = tmp81 & tmp29
    tmp92 = tl.load(in_ptr0 + (5 + r3 + 16 * x0), tmp91 & xmask, other=0.0)
    tmp93 = tmp92 + tmp90
    tmp94 = tmp81 & tmp36
    tmp95 = tl.load(in_ptr0 + (6 + r3 + 16 * x0), tmp94 & xmask, other=0.0)
    tmp96 = tmp95 + tmp93
    tmp97 = 2 + r2
    tmp98 = tmp97 >= tmp1
    tmp99 = tmp97 < tmp3
    tmp100 = tmp98 & tmp99
    tmp101 = tmp100 & tmp9
    tmp102 = tl.load(in_ptr0 + (6 + r3 + 16 * x0), tmp101 & xmask, other=0.0)
    tmp103 = tmp102 + tmp96
    tmp104 = tmp100 & tmp15
    tmp105 = tl.load(in_ptr0 + (7 + r3 + 16 * x0), tmp104 & xmask, other=0.0)
    tmp106 = tmp105 + tmp103
    tmp107 = tmp100 & tmp22
    tmp108 = tl.load(in_ptr0 + (8 + r3 + 16 * x0), tmp107 & xmask, other=0.0)
    tmp109 = tmp108 + tmp106
    tmp110 = tmp100 & tmp29
    tmp111 = tl.load(in_ptr0 + (9 + r3 + 16 * x0), tmp110 & xmask, other=0.0)
    tmp112 = tmp111 + tmp109
    tmp113 = tmp100 & tmp36
    tmp114 = tl.load(in_ptr0 + (10 + r3 + 16 * x0), tmp113 & xmask, other=0.0)
    tmp115 = tmp114 + tmp112
    tmp116 = 4 + -2 * r1 + -2 * r2 + 2 * (6 * (6 <= 3 + r1) + (3 + r1) * (3 +
        r1 < 6)) + 2 * (6 * (6 <= 3 + r2) + (3 + r2) * (3 + r2 < 6)
        ) + r1 * r2 + (6 * (6 <= 3 + r1) + (3 + r1) * (3 + r1 < 6)) * (6 *
        (6 <= 3 + r2) + (3 + r2) * (3 + r2 < 6)) + -1 * r1 * (6 * (6 <= 3 +
        r2) + (3 + r2) * (3 + r2 < 6)) + -1 * r2 * (6 * (6 <= 3 + r1) + (3 +
        r1) * (3 + r1 < 6))
    tmp117 = tmp115 / tmp116
    tmp119 = tmp118 - tmp117
    tmp120 = 0.0
    tmp121 = tmp119 != tmp120
    tmp122 = 1.0
    tmp123 = tl.where(tmp121, tmp122, tmp119)
    tmp125 = tmp124 - tmp118
    tmp126 = tl_math.abs(tmp125)
    tmp127 = tmp123 * tmp126
    tmp128 = tl.broadcast_to(tmp127, [XBLOCK, RBLOCK])
    tmp130 = tl.where(xmask, tmp128, 0)
    tmp131 = tl.sum(tmp130, 1)[:, None]
    tmp132 = tmp118 * tmp124
    tmp133 = tmp118 - tmp132
    tmp134 = tl.broadcast_to(tmp133, [XBLOCK, RBLOCK])
    tmp136 = tl.where(xmask, tmp134, 0)
    tmp137 = tl.sum(tmp136, 1)[:, None]
    tmp138 = tmp122 - tmp118
    tmp139 = tmp138 * tmp124
    tmp140 = tl.broadcast_to(tmp139, [XBLOCK, RBLOCK])
    tmp142 = tl.where(xmask, tmp140, 0)
    tmp143 = tl.sum(tmp142, 1)[:, None]
    tmp144 = tl.broadcast_to(tmp118, [XBLOCK, RBLOCK])
    tmp146 = tl.where(xmask, tmp144, 0)
    tmp147 = tl.sum(tmp146, 1)[:, None]
    tmp148 = tl.broadcast_to(tmp138, [XBLOCK, RBLOCK])
    tmp150 = tl.where(xmask, tmp148, 0)
    tmp151 = tl.sum(tmp150, 1)[:, None]
    tmp152 = tl.broadcast_to(tmp123, [XBLOCK, RBLOCK])
    tmp154 = tl.where(xmask, tmp152, 0)
    tmp155 = tl.sum(tmp154, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp131, xmask)
    tl.store(out_ptr1 + x0, tmp137, xmask)
    tl.store(out_ptr2 + x0, tmp143, xmask)
    tl.store(out_ptr3 + x0, tmp147, xmask)
    tl.store(out_ptr4 + x0, tmp151, xmask)
    tl.store(out_ptr5 + x0, tmp155, xmask)


@triton.jit
def triton_per_fused_add_div_mean_1(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, in_ptr4, in_ptr5, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.load(in_ptr1 + r0, None)
    tmp5 = tl.load(in_ptr2 + r0, None)
    tmp6 = tl.load(in_ptr3 + r0, None)
    tmp9 = tl.load(in_ptr4 + r0, None)
    tmp10 = tl.load(in_ptr5 + r0, None)
    tmp2 = 1e-06
    tmp3 = tmp1 + tmp2
    tmp4 = tmp0 / tmp3
    tmp7 = tmp6 + tmp2
    tmp8 = tmp5 / tmp7
    tmp11 = tmp10 + tmp2
    tmp12 = tmp9 / tmp11
    tmp13 = tmp8 + tmp12
    tmp14 = tmp4 + tmp13
    tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK])
    tmp17 = tl.sum(tmp15, 1)[:, None]
    tmp18 = 16.0
    tmp19 = tmp17 / tmp18
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp19, None)


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, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_abs_avg_pool2d_index_put_lift_fresh_mul_rsub_sub_sum_0[
            grid(16)](arg0_1, arg1_1, buf2, buf4, buf6, buf5, buf7, buf3, 
            16, 16, XBLOCK=8, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        buf8 = empty_strided_cuda((), (), torch.float32)
        buf9 = buf8
        del buf8
        triton_per_fused_add_div_mean_1[grid(1)](buf9, buf2, buf3, buf4,
            buf5, buf6, buf7, 1, 16, XBLOCK=1, num_warps=2, num_stages=1)
        del buf2
        del buf3
        del buf4
        del buf5
        del buf6
        del buf7
    return buf9,


class HELNew(nn.Module):

    def __init__(self):
        super(HELNew, self).__init__()
        None
        self.eps = 1e-06

    def edge_loss(self, pred, target):
        edge = target - F.avg_pool2d(target, kernel_size=5, stride=1, padding=2
            )
        edge[edge != 0] = 1
        numerator = (edge * (pred - target).abs_()).sum([2, 3])
        denominator = edge.sum([2, 3]) + self.eps
        return numerator / denominator

    def region_loss(self, pred, target):
        numerator_fore = (target - target * pred).sum([2, 3])
        denominator_fore = target.sum([2, 3]) + self.eps
        numerator_back = ((1 - target) * pred).sum([2, 3])
        denominator_back = (1 - target).sum([2, 3]) + self.eps
        return (numerator_fore / denominator_fore + numerator_back /
            denominator_back)

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


Measured latency_ms: 0.039443