import torch
import torch.nn.functional as F
from torch import nn


class VisErrorLossV13(nn.Module):

    def __init__(self):
        super(VisErrorLossV13, self).__init__()

    def compute_l1_weighted_loss(self, hm_targets, hm_preds, vismap, ohem=1.0):
        """
        :param hm_targets: [batch size, keypoint number, h, w]
        :param hm_preds: [batch size, keypoint number, h, w]
        :param vismap: [batch size, keypoint number]
        :return: 
        """
        epsilon = 0.0001
        hm_preds = F.relu(hm_preds, False)
        amplitude = torch.max(hm_targets)
        b, k, h, w = hm_targets.size()
        hm_targets = hm_targets.view(b, k, -1)
        hm_preds = hm_preds.view(b, k, -1)
        vismap = vismap.view(b, k, 1).repeat(1, 1, h * w)
        pos_ids = (hm_targets > amplitude / 10) & (vismap == 1)
        neg_ids = (hm_targets <= amplitude / 10) & (vismap == 1)
        diff = (hm_targets - hm_preds).abs()
        pos_loss = (diff * pos_ids.float()).sum(2).sum(0) / (pos_ids.float(
            ).sum(2).sum(0) + epsilon)
        neg_loss = (diff * neg_ids.float()).sum(2).sum(0) / (neg_ids.float(
            ).sum(2).sum(0) + epsilon)
        total_loss = 0.5 * pos_loss + 0.5 * neg_loss
        if ohem < 1:
            k = int(total_loss.size(0) * ohem)
            total_loss, _ = total_loss.topk(k)
        return total_loss.mean()

    def compute_l2_loss(self, hm_targets, hm_preds, vismap, ohem=1.0):
        """
        :param hm_targets: [batch size, keypoint number, h, w]
        :param hm_preds: [batch size, keypoint number, h, w]
        :param vismap: [batch size, keypoint number]
        :return: 
        """
        epsilon = 0.0001
        hm_preds = F.relu(hm_preds, False)
        b, k, h, w = hm_targets.size()
        hm_targets = hm_targets.view(b, k, -1)
        hm_preds = hm_preds.view(b, k, -1)
        vismap = vismap.view(b, k, 1).repeat(1, 1, h * w)
        ids = vismap == 1
        diff = (hm_targets - hm_preds) ** 2
        total_loss = (diff * ids.float()).sum(2).sum(0) / (ids.float().sum(
            2).sum(0) + epsilon)
        if ohem < 1:
            k = int(total_loss.size(0) * ohem)
            total_loss, _ = total_loss.topk(k)
        return total_loss.mean()

    def forward(self, hm_targets, hm_preds1, hm_preds2, vismap):
        """
        :param hm_targets: list of 4 elements, each is [batch size, keypoint number, h, w]
        :param hm_preds1: list of 4 elements, each is [batch size, keypoint number, h, w]
        :param vismap: [batch size, keypoint number] 
        :return: 
        """
        loss1 = 0
        for p in hm_preds1:
            loss1 += self.compute_l1_weighted_loss(hm_targets, p, vismap)
        loss1 /= 4.0
        loss2 = self.compute_l1_weighted_loss(hm_targets, hm_preds2, vismap,
            ohem=0.5)
        return loss1 + loss2, loss1, loss2


def get_inputs():
    return [torch.rand([4, 4, 16, 4]), torch.rand([4, 4, 4]), torch.rand([4,
        4, 64]), torch.rand([4, 4, 1])]


def get_init_inputs():
    return [[], {}]

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
import torch.nn.functional as F
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_per_fused_max_0(in_ptr0, out_ptr0, out_ptr1, out_ptr2, out_ptr3,
    out_ptr4, xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 1024
    xoffset = tl.program_id(0) * XBLOCK
    tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.broadcast_to(tmp0, [RBLOCK])
    tmp3 = triton_helpers.promote_to_tensor(triton_helpers.max2(tmp1, 0))
    tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp3, None)
    tl.store(out_ptr1 + tl.full([1], 0, tl.int32), tmp3, None)
    tl.store(out_ptr2 + tl.full([1], 0, tl.int32), tmp3, None)
    tl.store(out_ptr3 + tl.full([1], 0, tl.int32), tmp3, None)
    tl.store(out_ptr4 + tl.full([1], 0, tl.int32), tmp3, None)


@triton.jit
def triton_per_fused__to_copy_abs_bitwise_and_div_eq_gt_le_mul_relu_repeat_sub_sum_view_1(
    in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7,
    in_ptr8, out_ptr0, out_ptr1, out_ptr2, out_ptr3, out_ptr4, out_ptr5,
    out_ptr6, out_ptr7, out_ptr8, out_ptr9, out_ptr10, out_ptr11, out_ptr12,
    out_ptr13, out_ptr14, out_ptr15, out_ptr16, out_ptr17, out_ptr18,
    out_ptr19, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 64
    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)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 64 * x0), xmask, other=0.0)
    tmp1 = tl.load(in_ptr1 + (r1 + 64 * x0), xmask, other=0.0)
    tmp6 = tl.load(in_ptr2 + 0)
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tmp11 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
    tmp37 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last')
    tmp41 = tl.load(in_ptr5 + 0)
    tmp42 = tl.broadcast_to(tmp41, [XBLOCK, RBLOCK])
    tmp68 = tl.load(in_ptr4 + (16 + x0), xmask, eviction_policy='evict_last')
    tmp72 = tl.load(in_ptr6 + 0)
    tmp73 = tl.broadcast_to(tmp72, [XBLOCK, RBLOCK])
    tmp99 = tl.load(in_ptr4 + (32 + x0), xmask, eviction_policy='evict_last')
    tmp103 = tl.load(in_ptr7 + 0)
    tmp104 = tl.broadcast_to(tmp103, [XBLOCK, RBLOCK])
    tmp130 = tl.load(in_ptr4 + (48 + x0), xmask, eviction_policy='evict_last')
    tmp134 = tl.load(in_ptr8 + 0)
    tmp135 = tl.broadcast_to(tmp134, [XBLOCK, RBLOCK])
    tmp2 = tl.full([1, 1], 0, tl.int32)
    tmp3 = triton_helpers.maximum(tmp2, tmp1)
    tmp4 = tmp0 - tmp3
    tmp5 = tl_math.abs(tmp4)
    tmp8 = 0.1
    tmp9 = tmp7 * tmp8
    tmp10 = tmp0 > tmp9
    tmp12 = 1.0
    tmp13 = tmp11 == tmp12
    tmp14 = tmp10 & tmp13
    tmp15 = tmp14.to(tl.float32)
    tmp16 = tmp5 * tmp15
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp19 = tl.where(xmask, tmp17, 0)
    tmp20 = tl.sum(tmp19, 1)[:, None]
    tmp21 = tmp0 <= tmp9
    tmp22 = tmp21 & tmp13
    tmp23 = tmp22.to(tl.float32)
    tmp24 = tmp5 * tmp23
    tmp25 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK])
    tmp27 = tl.where(xmask, tmp25, 0)
    tmp28 = tl.sum(tmp27, 1)[:, None]
    tmp29 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK])
    tmp31 = tl.where(xmask, tmp29, 0)
    tmp32 = tl.sum(tmp31, 1)[:, None]
    tmp33 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK])
    tmp35 = tl.where(xmask, tmp33, 0)
    tmp36 = tl.sum(tmp35, 1)[:, None]
    tmp38 = triton_helpers.maximum(tmp2, tmp37)
    tmp39 = tmp0 - tmp38
    tmp40 = tl_math.abs(tmp39)
    tmp43 = tmp42 * tmp8
    tmp44 = tmp0 > tmp43
    tmp45 = tmp44 & tmp13
    tmp46 = tmp45.to(tl.float32)
    tmp47 = tmp40 * tmp46
    tmp48 = tl.broadcast_to(tmp47, [XBLOCK, RBLOCK])
    tmp50 = tl.where(xmask, tmp48, 0)
    tmp51 = tl.sum(tmp50, 1)[:, None]
    tmp52 = tmp0 <= tmp43
    tmp53 = tmp52 & tmp13
    tmp54 = tmp53.to(tl.float32)
    tmp55 = tmp40 * tmp54
    tmp56 = tl.broadcast_to(tmp55, [XBLOCK, RBLOCK])
    tmp58 = tl.where(xmask, tmp56, 0)
    tmp59 = tl.sum(tmp58, 1)[:, None]
    tmp60 = tl.broadcast_to(tmp46, [XBLOCK, RBLOCK])
    tmp62 = tl.where(xmask, tmp60, 0)
    tmp63 = tl.sum(tmp62, 1)[:, None]
    tmp64 = tl.broadcast_to(tmp54, [XBLOCK, RBLOCK])
    tmp66 = tl.where(xmask, tmp64, 0)
    tmp67 = tl.sum(tmp66, 1)[:, None]
    tmp69 = triton_helpers.maximum(tmp2, tmp68)
    tmp70 = tmp0 - tmp69
    tmp71 = tl_math.abs(tmp70)
    tmp74 = tmp73 * tmp8
    tmp75 = tmp0 > tmp74
    tmp76 = tmp75 & tmp13
    tmp77 = tmp76.to(tl.float32)
    tmp78 = tmp71 * tmp77
    tmp79 = tl.broadcast_to(tmp78, [XBLOCK, RBLOCK])
    tmp81 = tl.where(xmask, tmp79, 0)
    tmp82 = tl.sum(tmp81, 1)[:, None]
    tmp83 = tmp0 <= tmp74
    tmp84 = tmp83 & tmp13
    tmp85 = tmp84.to(tl.float32)
    tmp86 = tmp71 * tmp85
    tmp87 = tl.broadcast_to(tmp86, [XBLOCK, RBLOCK])
    tmp89 = tl.where(xmask, tmp87, 0)
    tmp90 = tl.sum(tmp89, 1)[:, None]
    tmp91 = tl.broadcast_to(tmp77, [XBLOCK, RBLOCK])
    tmp93 = tl.where(xmask, tmp91, 0)
    tmp94 = tl.sum(tmp93, 1)[:, None]
    tmp95 = tl.broadcast_to(tmp85, [XBLOCK, RBLOCK])
    tmp97 = tl.where(xmask, tmp95, 0)
    tmp98 = tl.sum(tmp97, 1)[:, None]
    tmp100 = triton_helpers.maximum(tmp2, tmp99)
    tmp101 = tmp0 - tmp100
    tmp102 = tl_math.abs(tmp101)
    tmp105 = tmp104 * tmp8
    tmp106 = tmp0 > tmp105
    tmp107 = tmp106 & tmp13
    tmp108 = tmp107.to(tl.float32)
    tmp109 = tmp102 * tmp108
    tmp110 = tl.broadcast_to(tmp109, [XBLOCK, RBLOCK])
    tmp112 = tl.where(xmask, tmp110, 0)
    tmp113 = tl.sum(tmp112, 1)[:, None]
    tmp114 = tmp0 <= tmp105
    tmp115 = tmp114 & tmp13
    tmp116 = tmp115.to(tl.float32)
    tmp117 = tmp102 * tmp116
    tmp118 = tl.broadcast_to(tmp117, [XBLOCK, RBLOCK])
    tmp120 = tl.where(xmask, tmp118, 0)
    tmp121 = tl.sum(tmp120, 1)[:, None]
    tmp122 = tl.broadcast_to(tmp108, [XBLOCK, RBLOCK])
    tmp124 = tl.where(xmask, tmp122, 0)
    tmp125 = tl.sum(tmp124, 1)[:, None]
    tmp126 = tl.broadcast_to(tmp116, [XBLOCK, RBLOCK])
    tmp128 = tl.where(xmask, tmp126, 0)
    tmp129 = tl.sum(tmp128, 1)[:, None]
    tmp131 = triton_helpers.maximum(tmp2, tmp130)
    tmp132 = tmp0 - tmp131
    tmp133 = tl_math.abs(tmp132)
    tmp136 = tmp135 * tmp8
    tmp137 = tmp0 > tmp136
    tmp138 = tmp137 & tmp13
    tmp139 = tmp138.to(tl.float32)
    tmp140 = tmp133 * tmp139
    tmp141 = tl.broadcast_to(tmp140, [XBLOCK, RBLOCK])
    tmp143 = tl.where(xmask, tmp141, 0)
    tmp144 = tl.sum(tmp143, 1)[:, None]
    tmp145 = tmp0 <= tmp136
    tmp146 = tmp145 & tmp13
    tmp147 = tmp146.to(tl.float32)
    tmp148 = tmp133 * tmp147
    tmp149 = tl.broadcast_to(tmp148, [XBLOCK, RBLOCK])
    tmp151 = tl.where(xmask, tmp149, 0)
    tmp152 = tl.sum(tmp151, 1)[:, None]
    tmp153 = tl.broadcast_to(tmp139, [XBLOCK, RBLOCK])
    tmp155 = tl.where(xmask, tmp153, 0)
    tmp156 = tl.sum(tmp155, 1)[:, None]
    tmp157 = tl.broadcast_to(tmp147, [XBLOCK, RBLOCK])
    tmp159 = tl.where(xmask, tmp157, 0)
    tmp160 = tl.sum(tmp159, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp20, xmask)
    tl.store(out_ptr1 + x0, tmp28, xmask)
    tl.store(out_ptr2 + x0, tmp32, xmask)
    tl.store(out_ptr3 + x0, tmp36, xmask)
    tl.store(out_ptr4 + x0, tmp51, xmask)
    tl.store(out_ptr5 + x0, tmp59, xmask)
    tl.store(out_ptr6 + x0, tmp63, xmask)
    tl.store(out_ptr7 + x0, tmp67, xmask)
    tl.store(out_ptr8 + x0, tmp82, xmask)
    tl.store(out_ptr9 + x0, tmp90, xmask)
    tl.store(out_ptr10 + x0, tmp94, xmask)
    tl.store(out_ptr11 + x0, tmp98, xmask)
    tl.store(out_ptr12 + x0, tmp113, xmask)
    tl.store(out_ptr13 + x0, tmp121, xmask)
    tl.store(out_ptr14 + x0, tmp125, xmask)
    tl.store(out_ptr15 + x0, tmp129, xmask)
    tl.store(out_ptr16 + x0, tmp144, xmask)
    tl.store(out_ptr17 + x0, tmp152, xmask)
    tl.store(out_ptr18 + x0, tmp156, xmask)
    tl.store(out_ptr19 + x0, tmp160, xmask)


@triton.jit
def triton_poi_fused_add_div_mul_sum_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    out_ptr0, 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 + x0, xmask)
    tmp1 = tl.load(in_ptr0 + (4 + x0), xmask)
    tmp3 = tl.load(in_ptr0 + (8 + x0), xmask)
    tmp5 = tl.load(in_ptr0 + (12 + x0), xmask)
    tmp7 = tl.load(in_ptr1 + x0, xmask)
    tmp8 = tl.load(in_ptr1 + (4 + x0), xmask)
    tmp10 = tl.load(in_ptr1 + (8 + x0), xmask)
    tmp12 = tl.load(in_ptr1 + (12 + x0), xmask)
    tmp19 = tl.load(in_ptr2 + x0, xmask)
    tmp20 = tl.load(in_ptr2 + (4 + x0), xmask)
    tmp22 = tl.load(in_ptr2 + (8 + x0), xmask)
    tmp24 = tl.load(in_ptr2 + (12 + x0), xmask)
    tmp26 = tl.load(in_ptr3 + x0, xmask)
    tmp27 = tl.load(in_ptr3 + (4 + x0), xmask)
    tmp29 = tl.load(in_ptr3 + (8 + x0), xmask)
    tmp31 = tl.load(in_ptr3 + (12 + x0), xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp9 = tmp7 + tmp8
    tmp11 = tmp9 + tmp10
    tmp13 = tmp11 + tmp12
    tmp14 = 0.0001
    tmp15 = tmp13 + tmp14
    tmp16 = tmp6 / tmp15
    tmp17 = 0.5
    tmp18 = tmp16 * tmp17
    tmp21 = tmp19 + tmp20
    tmp23 = tmp21 + tmp22
    tmp25 = tmp23 + tmp24
    tmp28 = tmp26 + tmp27
    tmp30 = tmp28 + tmp29
    tmp32 = tmp30 + tmp31
    tmp33 = tmp32 + tmp14
    tmp34 = tmp25 / tmp33
    tmp35 = tmp34 * tmp17
    tmp36 = tmp18 + tmp35
    tl.store(out_ptr0 + x0, tmp36, xmask)


@triton.jit
def triton_per_fused_mean_3(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.
    constexpr):
    RBLOCK: tl.constexpr = 2
    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.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp3, None)


@triton.jit
def triton_per_fused_add_div_mean_mul_sum_4(in_out_ptr0, in_out_ptr1,
    in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7,
    in_ptr8, in_ptr9, in_ptr10, in_ptr11, in_ptr12, in_ptr13, in_ptr14,
    in_ptr15, out_ptr7, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 4
    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_ptr0 + (4 + r0), None)
    tmp3 = tl.load(in_ptr0 + (8 + r0), None)
    tmp5 = tl.load(in_ptr0 + (12 + r0), None)
    tmp7 = tl.load(in_ptr1 + r0, None)
    tmp8 = tl.load(in_ptr1 + (4 + r0), None)
    tmp10 = tl.load(in_ptr1 + (8 + r0), None)
    tmp12 = tl.load(in_ptr1 + (12 + r0), None)
    tmp19 = tl.load(in_ptr2 + r0, None)
    tmp20 = tl.load(in_ptr2 + (4 + r0), None)
    tmp22 = tl.load(in_ptr2 + (8 + r0), None)
    tmp24 = tl.load(in_ptr2 + (12 + r0), None)
    tmp26 = tl.load(in_ptr3 + r0, None)
    tmp27 = tl.load(in_ptr3 + (4 + r0), None)
    tmp29 = tl.load(in_ptr3 + (8 + r0), None)
    tmp31 = tl.load(in_ptr3 + (12 + r0), None)
    tmp40 = tl.load(in_ptr4 + r0, None)
    tmp41 = tl.load(in_ptr4 + (4 + r0), None)
    tmp43 = tl.load(in_ptr4 + (8 + r0), None)
    tmp45 = tl.load(in_ptr4 + (12 + r0), None)
    tmp47 = tl.load(in_ptr5 + r0, None)
    tmp48 = tl.load(in_ptr5 + (4 + r0), None)
    tmp50 = tl.load(in_ptr5 + (8 + r0), None)
    tmp52 = tl.load(in_ptr5 + (12 + r0), None)
    tmp57 = tl.load(in_ptr6 + r0, None)
    tmp58 = tl.load(in_ptr6 + (4 + r0), None)
    tmp60 = tl.load(in_ptr6 + (8 + r0), None)
    tmp62 = tl.load(in_ptr6 + (12 + r0), None)
    tmp64 = tl.load(in_ptr7 + r0, None)
    tmp65 = tl.load(in_ptr7 + (4 + r0), None)
    tmp67 = tl.load(in_ptr7 + (8 + r0), None)
    tmp69 = tl.load(in_ptr7 + (12 + r0), None)
    tmp78 = tl.load(in_ptr8 + r0, None)
    tmp79 = tl.load(in_ptr8 + (4 + r0), None)
    tmp81 = tl.load(in_ptr8 + (8 + r0), None)
    tmp83 = tl.load(in_ptr8 + (12 + r0), None)
    tmp85 = tl.load(in_ptr9 + r0, None)
    tmp86 = tl.load(in_ptr9 + (4 + r0), None)
    tmp88 = tl.load(in_ptr9 + (8 + r0), None)
    tmp90 = tl.load(in_ptr9 + (12 + r0), None)
    tmp95 = tl.load(in_ptr10 + r0, None)
    tmp96 = tl.load(in_ptr10 + (4 + r0), None)
    tmp98 = tl.load(in_ptr10 + (8 + r0), None)
    tmp100 = tl.load(in_ptr10 + (12 + r0), None)
    tmp102 = tl.load(in_ptr11 + r0, None)
    tmp103 = tl.load(in_ptr11 + (4 + r0), None)
    tmp105 = tl.load(in_ptr11 + (8 + r0), None)
    tmp107 = tl.load(in_ptr11 + (12 + r0), None)
    tmp116 = tl.load(in_ptr12 + r0, None)
    tmp117 = tl.load(in_ptr12 + (4 + r0), None)
    tmp119 = tl.load(in_ptr12 + (8 + r0), None)
    tmp121 = tl.load(in_ptr12 + (12 + r0), None)
    tmp123 = tl.load(in_ptr13 + r0, None)
    tmp124 = tl.load(in_ptr13 + (4 + r0), None)
    tmp126 = tl.load(in_ptr13 + (8 + r0), None)
    tmp128 = tl.load(in_ptr13 + (12 + r0), None)
    tmp133 = tl.load(in_ptr14 + r0, None)
    tmp134 = tl.load(in_ptr14 + (4 + r0), None)
    tmp136 = tl.load(in_ptr14 + (8 + r0), None)
    tmp138 = tl.load(in_ptr14 + (12 + r0), None)
    tmp140 = tl.load(in_ptr15 + r0, None)
    tmp141 = tl.load(in_ptr15 + (4 + r0), None)
    tmp143 = tl.load(in_ptr15 + (8 + r0), None)
    tmp145 = tl.load(in_ptr15 + (12 + r0), None)
    tmp166 = tl.load(in_out_ptr1 + 0)
    tmp167 = tl.broadcast_to(tmp166, [XBLOCK, 1])
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp9 = tmp7 + tmp8
    tmp11 = tmp9 + tmp10
    tmp13 = tmp11 + tmp12
    tmp14 = 0.0001
    tmp15 = tmp13 + tmp14
    tmp16 = tmp6 / tmp15
    tmp17 = 0.5
    tmp18 = tmp16 * tmp17
    tmp21 = tmp19 + tmp20
    tmp23 = tmp21 + tmp22
    tmp25 = tmp23 + tmp24
    tmp28 = tmp26 + tmp27
    tmp30 = tmp28 + tmp29
    tmp32 = tmp30 + tmp31
    tmp33 = tmp32 + tmp14
    tmp34 = tmp25 / tmp33
    tmp35 = tmp34 * tmp17
    tmp36 = tmp18 + tmp35
    tmp37 = tl.broadcast_to(tmp36, [XBLOCK, RBLOCK])
    tmp39 = tl.sum(tmp37, 1)[:, None]
    tmp42 = tmp40 + tmp41
    tmp44 = tmp42 + tmp43
    tmp46 = tmp44 + tmp45
    tmp49 = tmp47 + tmp48
    tmp51 = tmp49 + tmp50
    tmp53 = tmp51 + tmp52
    tmp54 = tmp53 + tmp14
    tmp55 = tmp46 / tmp54
    tmp56 = tmp55 * tmp17
    tmp59 = tmp57 + tmp58
    tmp61 = tmp59 + tmp60
    tmp63 = tmp61 + tmp62
    tmp66 = tmp64 + tmp65
    tmp68 = tmp66 + tmp67
    tmp70 = tmp68 + tmp69
    tmp71 = tmp70 + tmp14
    tmp72 = tmp63 / tmp71
    tmp73 = tmp72 * tmp17
    tmp74 = tmp56 + tmp73
    tmp75 = tl.broadcast_to(tmp74, [XBLOCK, RBLOCK])
    tmp77 = tl.sum(tmp75, 1)[:, None]
    tmp80 = tmp78 + tmp79
    tmp82 = tmp80 + tmp81
    tmp84 = tmp82 + tmp83
    tmp87 = tmp85 + tmp86
    tmp89 = tmp87 + tmp88
    tmp91 = tmp89 + tmp90
    tmp92 = tmp91 + tmp14
    tmp93 = tmp84 / tmp92
    tmp94 = tmp93 * tmp17
    tmp97 = tmp95 + tmp96
    tmp99 = tmp97 + tmp98
    tmp101 = tmp99 + tmp100
    tmp104 = tmp102 + tmp103
    tmp106 = tmp104 + tmp105
    tmp108 = tmp106 + tmp107
    tmp109 = tmp108 + tmp14
    tmp110 = tmp101 / tmp109
    tmp111 = tmp110 * tmp17
    tmp112 = tmp94 + tmp111
    tmp113 = tl.broadcast_to(tmp112, [XBLOCK, RBLOCK])
    tmp115 = tl.sum(tmp113, 1)[:, None]
    tmp118 = tmp116 + tmp117
    tmp120 = tmp118 + tmp119
    tmp122 = tmp120 + tmp121
    tmp125 = tmp123 + tmp124
    tmp127 = tmp125 + tmp126
    tmp129 = tmp127 + tmp128
    tmp130 = tmp129 + tmp14
    tmp131 = tmp122 / tmp130
    tmp132 = tmp131 * tmp17
    tmp135 = tmp133 + tmp134
    tmp137 = tmp135 + tmp136
    tmp139 = tmp137 + tmp138
    tmp142 = tmp140 + tmp141
    tmp144 = tmp142 + tmp143
    tmp146 = tmp144 + tmp145
    tmp147 = tmp146 + tmp14
    tmp148 = tmp139 / tmp147
    tmp149 = tmp148 * tmp17
    tmp150 = tmp132 + tmp149
    tmp151 = tl.broadcast_to(tmp150, [XBLOCK, RBLOCK])
    tmp153 = tl.sum(tmp151, 1)[:, None]
    tmp154 = 4.0
    tmp155 = tmp39 / tmp154
    tmp156 = 0.0
    tmp157 = tmp155 + tmp156
    tmp158 = tmp77 / tmp154
    tmp159 = tmp157 + tmp158
    tmp160 = tmp115 / tmp154
    tmp161 = tmp159 + tmp160
    tmp162 = tmp153 / tmp154
    tmp163 = tmp161 + tmp162
    tmp164 = 0.25
    tmp165 = tmp163 * tmp164
    tmp168 = 2.0
    tmp169 = tmp167 / tmp168
    tmp170 = tmp165 + tmp169
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp165, None)
    tl.debug_barrier()
    tl.store(in_out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp169, None)
    tl.store(out_ptr7 + tl.full([XBLOCK, 1], 0, tl.int32), tmp170, None)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 16, 4), (256, 64, 4, 1))
    assert_size_stride(arg2_1, (4, 4, 1), (4, 1, 1))
    assert_size_stride(arg3_1, (4, 4, 64), (256, 64, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((), (), torch.float32)
        buf9 = empty_strided_cuda((), (), torch.float32)
        buf16 = empty_strided_cuda((), (), torch.float32)
        buf23 = empty_strided_cuda((), (), torch.float32)
        buf30 = empty_strided_cuda((), (), torch.float32)
        get_raw_stream(0)
        triton_per_fused_max_0[grid(1)](arg1_1, buf0, buf9, buf16, buf23,
            buf30, 1, 1024, num_warps=8, num_stages=1)
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf10 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf12 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf11 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf13 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf17 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf19 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf18 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf20 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf24 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf26 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf25 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf27 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf31 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf33 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf32 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf34 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_per_fused__to_copy_abs_bitwise_and_div_eq_gt_le_mul_relu_repeat_sub_sum_view_1[
            grid(16)](arg1_1, arg3_1, buf0, arg2_1, arg0_1, buf9, buf16,
            buf23, buf30, buf1, buf3, buf2, buf4, buf10, buf12, buf11,
            buf13, buf17, buf19, buf18, buf20, buf24, buf26, buf25, buf27,
            buf31, buf33, buf32, buf34, 16, 64, XBLOCK=8, num_warps=4,
            num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
        del arg3_1
        del buf0
        del buf16
        buf5 = empty_strided_cuda((4,), (1,), torch.float32)
        triton_poi_fused_add_div_mul_sum_2[grid(4)](buf1, buf2, buf3, buf4,
            buf5, 4, XBLOCK=4, num_warps=1, num_stages=1)
        del buf1
        del buf2
        del buf3
        del buf4
        buf6 = torch.ops.aten.topk.default(buf5, 2)
        del buf5
        buf7 = buf6[0]
        del buf6
        buf38 = buf9
        del buf9
        triton_per_fused_mean_3[grid(1)](buf7, buf38, 1, 2, XBLOCK=1,
            num_warps=2, num_stages=1)
        del buf7
        buf15 = buf30
        del buf30
        buf37 = buf15
        del buf15
        buf39 = buf38
        del buf38
        buf40 = buf23
        del buf23
        triton_per_fused_add_div_mean_mul_sum_4[grid(1)](buf37, buf39,
            buf10, buf11, buf12, buf13, buf17, buf18, buf19, buf20, buf24,
            buf25, buf26, buf27, buf31, buf32, buf33, buf34, buf40, 1, 4,
            XBLOCK=1, num_warps=2, num_stages=1)
        del buf10
        del buf11
        del buf12
        del buf13
        del buf17
        del buf18
        del buf19
        del buf20
        del buf24
        del buf25
        del buf26
        del buf27
        del buf31
        del buf32
        del buf33
        del buf34
    return buf40, buf37, buf39


class VisErrorLossV13New(nn.Module):

    def __init__(self):
        super(VisErrorLossV13New, self).__init__()

    def compute_l1_weighted_loss(self, hm_targets, hm_preds, vismap, ohem=1.0):
        """
        :param hm_targets: [batch size, keypoint number, h, w]
        :param hm_preds: [batch size, keypoint number, h, w]
        :param vismap: [batch size, keypoint number]
        :return: 
        """
        epsilon = 0.0001
        hm_preds = F.relu(hm_preds, False)
        amplitude = torch.max(hm_targets)
        b, k, h, w = hm_targets.size()
        hm_targets = hm_targets.view(b, k, -1)
        hm_preds = hm_preds.view(b, k, -1)
        vismap = vismap.view(b, k, 1).repeat(1, 1, h * w)
        pos_ids = (hm_targets > amplitude / 10) & (vismap == 1)
        neg_ids = (hm_targets <= amplitude / 10) & (vismap == 1)
        diff = (hm_targets - hm_preds).abs()
        pos_loss = (diff * pos_ids.float()).sum(2).sum(0) / (pos_ids.float(
            ).sum(2).sum(0) + epsilon)
        neg_loss = (diff * neg_ids.float()).sum(2).sum(0) / (neg_ids.float(
            ).sum(2).sum(0) + epsilon)
        total_loss = 0.5 * pos_loss + 0.5 * neg_loss
        if ohem < 1:
            k = int(total_loss.size(0) * ohem)
            total_loss, _ = total_loss.topk(k)
        return total_loss.mean()

    def compute_l2_loss(self, hm_targets, hm_preds, vismap, ohem=1.0):
        """
        :param hm_targets: [batch size, keypoint number, h, w]
        :param hm_preds: [batch size, keypoint number, h, w]
        :param vismap: [batch size, keypoint number]
        :return: 
        """
        epsilon = 0.0001
        hm_preds = F.relu(hm_preds, False)
        b, k, h, w = hm_targets.size()
        hm_targets = hm_targets.view(b, k, -1)
        hm_preds = hm_preds.view(b, k, -1)
        vismap = vismap.view(b, k, 1).repeat(1, 1, h * w)
        ids = vismap == 1
        diff = (hm_targets - hm_preds) ** 2
        total_loss = (diff * ids.float()).sum(2).sum(0) / (ids.float().sum(
            2).sum(0) + epsilon)
        if ohem < 1:
            k = int(total_loss.size(0) * ohem)
            total_loss, _ = total_loss.topk(k)
        return total_loss.mean()

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