# AOT ID: ['0_inference']
from ctypes import c_void_p, c_long, c_int
import torch
import math
import random
import os
import tempfile
from math import inf, nan
from torch._inductor.hooks import run_intermediate_hooks
from torch._inductor.utils import maybe_profile
from torch._inductor.codegen.memory_planning import _align as align
from torch import device, empty_strided
from torch._inductor.async_compile import AsyncCompile
from torch._inductor.select_algorithm import extern_kernels
from torch._inductor.codegen.multi_kernel import MultiKernelCall
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid, split_scan_grid, grid_combo_kernels, start_graph, end_graph
from torch._C import _cuda_getCurrentRawStream as get_raw_stream

aten = torch.ops.aten
inductor_ops = torch.ops.inductor
_quantized = torch.ops._quantized
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cpu = torch._C._dynamo.guards._empty_strided_cpu
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
empty_strided_xpu = torch._C._dynamo.guards._empty_strided_xpu
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
alloc_from_pool = torch.ops.inductor._alloc_from_pool
async_compile = AsyncCompile()


# kernel path: runs/run_shard_4/inductor_cache/vw/cvww5edufkr5ujgf6bj4jckegfkgvq2h247sfq65sokeomv4ycn6.py
# Topologically Sorted Source Nodes: [gt_6, pmask_1, sum_9, num_positive_1, ratio_2, clamp_3, ratio_3, coef_3, mul_18, add_6, log_2, mul_19, mul_16, sub_4, coef_2, sub_5, mul_20, sub_6, add_7, log_3, mul_21, loss_3, mean, loss_4, gt_7, pmask_2, sum_10, num_positive_2, ratio_4, clamp_6, ratio_5, coef_5, mul_24, add_9, log_4, mul_25, mul_22, sub_7, coef_4, sub_8, mul_26, sub_9, add_10, log_5, mul_27, loss_5, mean_1, loss_6, loss_7, mul_28, pred_bm_reg, gt_iou_map, le, gt_1, and_, u_mmask, u_smmask_1, gt, u_hmask, num_h, num_m, r_m, sub, gt_3, u_smmask_2, add, le_1, gt_2, and__1, u_lmask, u_lmask_1, u_slmask_1, num_l, r_l, sub_1, gt_4, u_slmask_2, weights, mul_4, mul_5, loss, ones_like, mul_6, sum_4, mul_7, sum_5, loss_1, mul_29, add_13, gt_5, pmask, sum_6, num_positive, le_2, nmask, nmask_1, sum_7, num_entries, ratio, ratio_1, coef_1, pred_bm_cls, add_3, log, mul_11, loss_pos, mul_9, sub_2, coef_0, sub_3, add_4, log_1, mul_13, loss_neg, add_5, sum_8, mul_15, loss_2, mul_30, loss_8], Original ATen: [aten.gt, aten._to_copy, aten.sum, aten.clamp, aten.reciprocal, aten.mul, aten.add, aten.log, aten.sub, aten.div, aten.rsub, aten.mean, aten.neg, aten.clone, aten.le, aten.bitwise_and, aten.mse_loss, aten.ones_like]
# Source node to ATen node mapping:
#   add => add
#   add_10 => add_10
#   add_13 => add_13
#   add_3 => add_3
#   add_4 => add_4
#   add_5 => add_5
#   add_6 => add_6
#   add_7 => add_7
#   add_9 => add_9
#   and_ => bitwise_and
#   and__1 => bitwise_and_1
#   clamp_3 => clamp_min_3
#   clamp_6 => clamp_min_5
#   coef_0 => div_4
#   coef_1 => mul_10
#   coef_2 => div_6
#   coef_3 => mul_18
#   coef_4 => div_7
#   coef_5 => mul_25
#   gt => gt
#   gt_1 => gt_1
#   gt_2 => gt_2
#   gt_3 => gt_3
#   gt_4 => gt_4
#   gt_5 => gt_5
#   gt_6 => gt_6
#   gt_7 => gt_7
#   gt_iou_map => mul
#   le => le
#   le_1 => le_1
#   le_2 => le_2
#   log => log
#   log_1 => log_1
#   log_2 => log_2
#   log_3 => log_3
#   log_4 => log_4
#   log_5 => log_5
#   loss => mean, pow_1, sub_2
#   loss_1 => div_2
#   loss_2 => div_5
#   loss_3 => add_8
#   loss_4 => neg
#   loss_5 => add_11
#   loss_6 => neg_1
#   loss_7 => add_12
#   loss_8 => add_14
#   loss_neg => mul_14
#   loss_pos => mul_12
#   mean => mean_1
#   mean_1 => mean_2
#   mul_11 => mul_11
#   mul_13 => mul_13
#   mul_15 => mul_15
#   mul_16 => mul_17
#   mul_18 => mul_19
#   mul_19 => mul_20
#   mul_20 => mul_21
#   mul_21 => mul_22
#   mul_22 => mul_24
#   mul_24 => mul_26
#   mul_25 => mul_27
#   mul_26 => mul_28
#   mul_27 => mul_29
#   mul_28 => mul_30
#   mul_29 => mul_31
#   mul_30 => mul_32
#   mul_4 => mul_4
#   mul_5 => mul_5
#   mul_6 => mul_6
#   mul_7 => mul_7
#   mul_9 => mul_9
#   nmask => convert_element_type_6
#   nmask_1 => mul_8
#   num_entries => add_2
#   num_h => sum_1
#   num_l => sum_3
#   num_m => sum_2
#   num_positive => clamp_min
#   num_positive_1 => clamp_min_2
#   num_positive_2 => clamp_min_4
#   ones_like => full_default
#   pmask => convert_element_type_5
#   pmask_1 => convert_element_type_7
#   pmask_2 => convert_element_type_8
#   pred_bm_cls => clone_1
#   pred_bm_reg => clone
#   r_l => div_1
#   r_m => div
#   ratio => div_3
#   ratio_1 => clamp_max, clamp_min_1
#   ratio_2 => mul_16, reciprocal
#   ratio_3 => clamp_max_1
#   ratio_4 => mul_23, reciprocal_1
#   ratio_5 => clamp_max_2
#   sub => sub
#   sub_1 => sub_1
#   sub_2 => sub_3
#   sub_3 => sub_4
#   sub_4 => sub_5
#   sub_5 => sub_6
#   sub_6 => sub_7
#   sub_7 => sub_8
#   sub_8 => sub_9
#   sub_9 => sub_10
#   sum_10 => sum_10
#   sum_4 => sum_4
#   sum_5 => sum_5
#   sum_6 => sum_6
#   sum_7 => sum_7
#   sum_8 => sum_8
#   sum_9 => sum_9
#   u_hmask => convert_element_type
#   u_lmask => convert_element_type_2
#   u_lmask_1 => mul_1
#   u_mmask => convert_element_type_1
#   u_slmask_1 => mul_3
#   u_slmask_2 => convert_element_type_4
#   u_smmask_1 => mul_2
#   u_smmask_2 => convert_element_type_3
#   weights => add_1
# Graph fragment:
#   %gt_6 : [num_users=1] = call_function[target=torch.ops.aten.gt.Scalar](args = (%view, 0.5), kwargs = {})
#   %convert_element_type_7 : [num_users=3] = call_function[target=torch.ops.prims.convert_element_type.default](args = (%gt_6, torch.float32), kwargs = {})
#   %sum_9 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%convert_element_type_7,), kwargs = {})
#   %clamp_min_2 : [num_users=1] = call_function[target=torch.ops.aten.clamp_min.default](args = (%sum_9, 1), kwargs = {})
#   %reciprocal : [num_users=1] = call_function[target=torch.ops.aten.reciprocal.default](args = (%clamp_min_2,), kwargs = {})
#   %mul_16 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%reciprocal, 256), kwargs = {})
#   %clamp_min_3 : [num_users=1] = call_function[target=torch.ops.aten.clamp_min.default](args = (%mul_16, 1.05), kwargs = {})
#   %clamp_max_1 : [num_users=3] = call_function[target=torch.ops.aten.clamp_max.default](args = (%clamp_min_3, 21), kwargs = {})
#   %mul_18 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%clamp_max_1, 0.5), kwargs = {})
#   %mul_19 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_18, %convert_element_type_7), kwargs = {})
#   %add_6 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%view_1, 1e-05), kwargs = {})
#   %log_2 : [num_users=1] = call_function[target=torch.ops.aten.log.default](args = (%add_6,), kwargs = {})
#   %mul_20 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_19, %log_2), kwargs = {})
#   %mul_17 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%clamp_max_1, 0.5), kwargs = {})
#   %sub_5 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%clamp_max_1, 1), kwargs = {})
#   %div_6 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%mul_17, %sub_5), kwargs = {})
#   %sub_6 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1.0, %convert_element_type_7), kwargs = {})
#   %mul_21 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_6, %sub_6), kwargs = {})
#   %sub_7 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1.0, %view_1), kwargs = {})
#   %add_7 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sub_7, 1e-05), kwargs = {})
#   %log_3 : [num_users=1] = call_function[target=torch.ops.aten.log.default](args = (%add_7,), kwargs = {})
#   %mul_22 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_21, %log_3), kwargs = {})
#   %add_8 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul_20, %mul_22), kwargs = {})
#   %mean_1 : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%add_8,), kwargs = {})
#   %neg : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%mean_1,), kwargs = {})
#   %gt_7 : [num_users=1] = call_function[target=torch.ops.aten.gt.Scalar](args = (%view_2, 0.5), kwargs = {})
#   %convert_element_type_8 : [num_users=3] = call_function[target=torch.ops.prims.convert_element_type.default](args = (%gt_7, torch.float32), kwargs = {})
#   %sum_10 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%convert_element_type_8,), kwargs = {})
#   %clamp_min_4 : [num_users=1] = call_function[target=torch.ops.aten.clamp_min.default](args = (%sum_10, 1), kwargs = {})
#   %reciprocal_1 : [num_users=1] = call_function[target=torch.ops.aten.reciprocal.default](args = (%clamp_min_4,), kwargs = {})
#   %mul_23 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%reciprocal_1, 256), kwargs = {})
#   %clamp_min_5 : [num_users=1] = call_function[target=torch.ops.aten.clamp_min.default](args = (%mul_23, 1.05), kwargs = {})
#   %clamp_max_2 : [num_users=3] = call_function[target=torch.ops.aten.clamp_max.default](args = (%clamp_min_5, 21), kwargs = {})
#   %mul_25 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%clamp_max_2, 0.5), kwargs = {})
#   %mul_26 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_25, %convert_element_type_8), kwargs = {})
#   %add_9 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%view_3, 1e-05), kwargs = {})
#   %log_4 : [num_users=1] = call_function[target=torch.ops.aten.log.default](args = (%add_9,), kwargs = {})
#   %mul_27 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_26, %log_4), kwargs = {})
#   %mul_24 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%clamp_max_2, 0.5), kwargs = {})
#   %sub_8 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%clamp_max_2, 1), kwargs = {})
#   %div_7 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%mul_24, %sub_8), kwargs = {})
#   %sub_9 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1.0, %convert_element_type_8), kwargs = {})
#   %mul_28 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_7, %sub_9), kwargs = {})
#   %sub_10 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1.0, %view_3), kwargs = {})
#   %add_10 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sub_10, 1e-05), kwargs = {})
#   %log_5 : [num_users=1] = call_function[target=torch.ops.aten.log.default](args = (%add_10,), kwargs = {})
#   %mul_29 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_28, %log_5), kwargs = {})
#   %add_11 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul_27, %mul_29), kwargs = {})
#   %mean_2 : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%add_11,), kwargs = {})
#   %neg_1 : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%mean_2,), kwargs = {})
#   %add_12 : [num_users=2] = call_function[target=torch.ops.aten.add.Tensor](args = (%neg, %neg_1), kwargs = {})
#   %mul_30 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%add_12, 1.0), kwargs = {})
#   %clone : [num_users=1] = call_function[target=torch.ops.aten.clone.default](args = (%select,), kwargs = {memory_format: torch.contiguous_format})
#   %mul : [num_users=8] = call_function[target=torch.ops.aten.mul.Tensor](args = (%arg1_1, %arg2_1), kwargs = {})
#   %le : [num_users=1] = call_function[target=torch.ops.aten.le.Scalar](args = (%mul, 0.7), kwargs = {})
#   %gt_1 : [num_users=1] = call_function[target=torch.ops.aten.gt.Scalar](args = (%mul, 0.3), kwargs = {})
#   %bitwise_and : [num_users=1] = call_function[target=torch.ops.aten.bitwise_and.Tensor](args = (%le, %gt_1), kwargs = {})
#   %convert_element_type_1 : [num_users=2] = call_function[target=torch.ops.prims.convert_element_type.default](args = (%bitwise_and, torch.float32), kwargs = {})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%convert_element_type_1, %rand), kwargs = {})
#   %gt : [num_users=1] = call_function[target=torch.ops.aten.gt.Scalar](args = (%mul, 0.7), kwargs = {})
#   %convert_element_type : [num_users=2] = call_function[target=torch.ops.prims.convert_element_type.default](args = (%gt, torch.float32), kwargs = {})
#   %sum_1 : [num_users=2] = call_function[target=torch.ops.aten.sum.default](args = (%convert_element_type,), kwargs = {})
#   %sum_2 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%convert_element_type_1,), kwargs = {})
#   %div : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sum_1, %sum_2), kwargs = {})
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1.0, %div), kwargs = {})
#   %gt_3 : [num_users=1] = call_function[target=torch.ops.aten.gt.Tensor](args = (%mul_2, %sub), kwargs = {})
#   %convert_element_type_3 : [num_users=1] = call_function[target=torch.ops.prims.convert_element_type.default](args = (%gt_3, torch.float32), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%convert_element_type, %convert_element_type_3), kwargs = {})
#   %le_1 : [num_users=1] = call_function[target=torch.ops.aten.le.Scalar](args = (%mul, 0.3), kwargs = {})
#   %gt_2 : [num_users=1] = call_function[target=torch.ops.aten.gt.Scalar](args = (%mul, 0.0), kwargs = {})
#   %bitwise_and_1 : [num_users=1] = call_function[target=torch.ops.aten.bitwise_and.Tensor](args = (%le_1, %gt_2), kwargs = {})
#   %convert_element_type_2 : [num_users=1] = call_function[target=torch.ops.prims.convert_element_type.default](args = (%bitwise_and_1, torch.float32), kwargs = {})
#   %mul_1 : [num_users=2] = call_function[target=torch.ops.aten.mul.Tensor](args = (%convert_element_type_2, %arg2_1), kwargs = {})
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_1, %rand_1), kwargs = {})
#   %sum_3 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%mul_1,), kwargs = {})
#   %div_1 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sum_1, %sum_3), kwargs = {})
#   %sub_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1.0, %div_1), kwargs = {})
#   %gt_4 : [num_users=1] = call_function[target=torch.ops.aten.gt.Tensor](args = (%mul_3, %sub_1), kwargs = {})
#   %convert_element_type_4 : [num_users=1] = call_function[target=torch.ops.prims.convert_element_type.default](args = (%gt_4, torch.float32), kwargs = {})
#   %add_1 : [num_users=3] = call_function[target=torch.ops.aten.add.Tensor](args = (%add, %convert_element_type_4), kwargs = {})
#   %mul_4 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%clone, %add_1), kwargs = {})
#   %mul_5 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul, %add_1), kwargs = {})
#   %sub_2 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%mul_4, %mul_5), kwargs = {})
#   %pow_1 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sub_2, 2), kwargs = {})
#   %mean : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%pow_1,), kwargs = {})
#   %full_default : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([4, 4, 4, 4], 1), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %mul_6 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mean, %full_default), kwargs = {})
#   %sum_4 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%mul_6,), kwargs = {})
#   %mul_7 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sum_4, 0.5), kwargs = {})
#   %sum_5 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%add_1,), kwargs = {})
#   %div_2 : [num_users=2] = call_function[target=torch.ops.aten.div.Tensor](args = (%mul_7, %sum_5), kwargs = {})
#   %mul_31 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_2, 10.0), kwargs = {})
#   %add_13 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul_30, %mul_31), kwargs = {})
#   %gt_5 : [num_users=1] = call_function[target=torch.ops.aten.gt.Scalar](args = (%mul, 0.9), kwargs = {})
#   %convert_element_type_5 : [num_users=2] = call_function[target=torch.ops.prims.convert_element_type.default](args = (%gt_5, torch.float32), kwargs = {})
#   %sum_6 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%convert_element_type_5,), kwargs = {})
#   %clamp_min : [num_users=2] = call_function[target=torch.ops.aten.clamp_min.default](args = (%sum_6, 1), kwargs = {})
#   %le_2 : [num_users=1] = call_function[target=torch.ops.aten.le.Scalar](args = (%mul, 0.9), kwargs = {})
#   %convert_element_type_6 : [num_users=1] = call_function[target=torch.ops.prims.convert_element_type.default](args = (%le_2, torch.float32), kwargs = {})
#   %mul_8 : [num_users=2] = call_function[target=torch.ops.aten.mul.Tensor](args = (%convert_element_type_6, %arg2_1), kwargs = {})
#   %sum_7 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%mul_8,), kwargs = {})
#   %add_2 : [num_users=2] = call_function[target=torch.ops.aten.add.Tensor](args = (%clamp_min, %sum_7), kwargs = {})
#   %div_3 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%add_2, %clamp_min), kwargs = {})
#   %clamp_min_1 : [num_users=1] = call_function[target=torch.ops.aten.clamp_min.default](args = (%div_3, 1.05), kwargs = {})
#   %clamp_max : [num_users=3] = call_function[target=torch.ops.aten.clamp_max.default](args = (%clamp_min_1, 21), kwargs = {})
#   %mul_10 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%clamp_max, 0.5), kwargs = {})
#   %clone_1 : [num_users=2] = call_function[target=torch.ops.aten.clone.default](args = (%select_1,), kwargs = {memory_format: torch.contiguous_format})
#   %add_3 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%clone_1, 1e-05), kwargs = {})
#   %log : [num_users=1] = call_function[target=torch.ops.aten.log.default](args = (%add_3,), kwargs = {})
#   %mul_11 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_10, %log), kwargs = {})
#   %mul_12 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_11, %convert_element_type_5), kwargs = {})
#   %mul_9 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%clamp_max, 0.5), kwargs = {})
#   %sub_3 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%clamp_max, 1), kwargs = {})
#   %div_4 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%mul_9, %sub_3), kwargs = {})
#   %sub_4 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1.0, %clone_1), kwargs = {})
#   %add_4 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sub_4, 1e-05), kwargs = {})
#   %log_1 : [num_users=1] = call_function[target=torch.ops.aten.log.default](args = (%add_4,), kwargs = {})
#   %mul_13 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_4, %log_1), kwargs = {})
#   %mul_14 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_13, %mul_8), kwargs = {})
#   %add_5 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul_12, %mul_14), kwargs = {})
#   %sum_8 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%add_5,), kwargs = {})
#   %mul_15 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sum_8, -1), kwargs = {})
#   %div_5 : [num_users=2] = call_function[target=torch.ops.aten.div.Tensor](args = (%mul_15, %add_2), kwargs = {})
#   %mul_32 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_5, 1.0), kwargs = {})
#   %add_14 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_13, %mul_32), kwargs = {})
triton_per_fused__to_copy_add_bitwise_and_clamp_clone_div_gt_le_log_mean_mse_loss_mul_neg_ones_like_reciprocal_rsub_sub_sum_0 = async_compile.triton('triton_per_fused__to_copy_add_bitwise_and_clamp_clone_div_gt_le_log_mean_mse_loss_mul_neg_ones_like_reciprocal_rsub_sub_sum_0', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.persistent_reduction(
    size_hints=[1, 256],
    reduction_hint=ReductionHint.INNER,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: '*fp32', 5: '*fp32', 6: '*fp32', 7: '*fp32', 8: '*fp32', 9: '*fp32', 10: '*fp32', 11: '*fp32', 12: '*fp32', 13: 'i32', 14: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {13: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14), equal_to_1=(13,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused__to_copy_add_bitwise_and_clamp_clone_div_gt_le_log_mean_mse_loss_mul_neg_ones_like_reciprocal_rsub_sub_sum_0', 'mutated_arg_names': ['in_out_ptr0', 'in_out_ptr1', 'in_out_ptr2', 'in_out_ptr3'], 'no_x_dim': True, 'num_load': 10, 'num_reduction': 13, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False}
)
@triton.jit
def triton_per_fused__to_copy_add_bitwise_and_clamp_clone_div_gt_le_log_mean_mse_loss_mul_neg_ones_like_reciprocal_rsub_sub_sum_0(in_out_ptr0, in_out_ptr1, in_out_ptr2, in_out_ptr3, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, out_ptr12, xnumel, rnumel):
    xnumel = 1
    XBLOCK: tl.constexpr = 1
    rnumel = 256
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = tl.full([1], xoffset, tl.int32)
    xmask = tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    roffset = 0
    rmask = tl.full([RBLOCK], True, tl.int1)
    r0 = rindex
    r1 = rindex % 16
    r2 = (rindex // 16) % 4
    tmp0 = tl.load(in_ptr0 + (r0), None)
    tmp19 = tl.load(in_ptr1 + (r0), None)
    tmp36 = tl.load(in_ptr2 + (r0), None)
    tmp37 = tl.load(in_ptr3 + (r0), None)
    tmp62 = tl.load(in_ptr4 + (r0), None)
    tmp69 = tl.load(in_out_ptr0 + (r0), None)
    tmp76 = tl.load(in_ptr5 + (r1 + (64*r2)), None, eviction_policy='evict_last')
    tmp110 = tl.load(in_ptr5 + (16 + r1 + (64*r2)), None, eviction_policy='evict_last')
    tmp126 = tl.load(in_ptr6 + (r0), None)
    tmp139 = tl.load(in_ptr7 + (r0), None)
    tmp1 = 0.5
    tmp2 = tmp0 > tmp1
    tmp3 = tmp2.to(tl.float32)
    tmp4 = tl.broadcast_to(tmp3, [RBLOCK])
    tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0))
    tmp7 = 1.0
    tmp8 = triton_helpers.maximum(tmp6, tmp7)
    tmp9 = tl.full([1], 1, tl.int32)
    tmp10 = tmp9 / tmp8
    tmp11 = 256.0
    tmp12 = tmp10 * tmp11
    tmp13 = 1.05
    tmp14 = triton_helpers.maximum(tmp12, tmp13)
    tmp15 = 21.0
    tmp16 = triton_helpers.minimum(tmp14, tmp15)
    tmp17 = tmp16 * tmp1
    tmp18 = tmp17 * tmp3
    tmp20 = 1e-05
    tmp21 = tmp19 + tmp20
    tmp22 = tl_math.log(tmp21)
    tmp23 = tmp18 * tmp22
    tmp24 = tmp16 - tmp7
    tmp25 = tmp17 / tmp24
    tmp26 = tmp7 - tmp3
    tmp27 = tmp25 * tmp26
    tmp28 = tmp7 - tmp19
    tmp29 = tmp28 + tmp20
    tmp30 = tl_math.log(tmp29)
    tmp31 = tmp27 * tmp30
    tmp32 = tmp23 + tmp31
    tmp33 = tl.broadcast_to(tmp32, [RBLOCK])
    tmp35 = triton_helpers.promote_to_tensor(tl.sum(tmp33, 0))
    tmp38 = tmp36 * tmp37
    tmp39 = 0.7
    tmp40 = tmp38 > tmp39
    tmp41 = tmp40.to(tl.float32)
    tmp42 = tl.broadcast_to(tmp41, [RBLOCK])
    tmp44 = triton_helpers.promote_to_tensor(tl.sum(tmp42, 0))
    tmp45 = tmp38 <= tmp39
    tmp46 = 0.3
    tmp47 = tmp38 > tmp46
    tmp48 = tmp45 & tmp47
    tmp49 = tmp48.to(tl.float32)
    tmp50 = tl.broadcast_to(tmp49, [RBLOCK])
    tmp52 = triton_helpers.promote_to_tensor(tl.sum(tmp50, 0))
    tmp53 = tmp38 <= tmp46
    tmp54 = 0.0
    tmp55 = tmp38 > tmp54
    tmp56 = tmp53 & tmp55
    tmp57 = tmp56.to(tl.float32)
    tmp58 = tmp57 * tmp37
    tmp59 = tl.broadcast_to(tmp58, [RBLOCK])
    tmp61 = triton_helpers.promote_to_tensor(tl.sum(tmp59, 0))
    tmp63 = tmp49 * tmp62
    tmp64 = tmp44 / tmp52
    tmp65 = tmp7 - tmp64
    tmp66 = tmp63 > tmp65
    tmp67 = tmp66.to(tl.float32)
    tmp68 = tmp41 + tmp67
    tmp70 = tmp58 * tmp69
    tmp71 = tmp44 / tmp61
    tmp72 = tmp7 - tmp71
    tmp73 = tmp70 > tmp72
    tmp74 = tmp73.to(tl.float32)
    tmp75 = tmp68 + tmp74
    tmp77 = tmp76 * tmp75
    tmp78 = tmp38 * tmp75
    tmp79 = tmp77 - tmp78
    tmp80 = tmp79 * tmp79
    tmp81 = tl.broadcast_to(tmp80, [RBLOCK])
    tmp83 = triton_helpers.promote_to_tensor(tl.sum(tmp81, 0))
    tmp84 = 0.9
    tmp85 = tmp38 > tmp84
    tmp86 = tmp85.to(tl.float32)
    tmp87 = tl.broadcast_to(tmp86, [RBLOCK])
    tmp89 = triton_helpers.promote_to_tensor(tl.sum(tmp87, 0))
    tmp90 = tmp38 <= tmp84
    tmp91 = tmp90.to(tl.float32)
    tmp92 = tmp91 * tmp37
    tmp93 = tl.broadcast_to(tmp92, [RBLOCK])
    tmp95 = triton_helpers.promote_to_tensor(tl.sum(tmp93, 0))
    tmp96 = tl.broadcast_to(tmp75, [RBLOCK])
    tmp98 = triton_helpers.promote_to_tensor(tl.sum(tmp96, 0))
    tmp99 = tmp83 / tmp11
    tmp100 = tmp99 * tmp7
    tmp101 = tl.broadcast_to(tmp100, [RBLOCK])
    tmp103 = triton_helpers.promote_to_tensor(tl.sum(tmp101, 0))
    tmp104 = triton_helpers.maximum(tmp89, tmp7)
    tmp105 = tmp104 + tmp95
    tmp106 = tmp105 / tmp104
    tmp107 = triton_helpers.maximum(tmp106, tmp13)
    tmp108 = triton_helpers.minimum(tmp107, tmp15)
    tmp109 = tmp108 * tmp1
    tmp111 = tmp110 + tmp20
    tmp112 = tl_math.log(tmp111)
    tmp113 = tmp109 * tmp112
    tmp114 = tmp113 * tmp86
    tmp115 = tmp108 - tmp7
    tmp116 = tmp109 / tmp115
    tmp117 = tmp7 - tmp110
    tmp118 = tmp117 + tmp20
    tmp119 = tl_math.log(tmp118)
    tmp120 = tmp116 * tmp119
    tmp121 = tmp120 * tmp92
    tmp122 = tmp114 + tmp121
    tmp123 = tl.broadcast_to(tmp122, [RBLOCK])
    tmp125 = triton_helpers.promote_to_tensor(tl.sum(tmp123, 0))
    tmp127 = tmp126 > tmp1
    tmp128 = tmp127.to(tl.float32)
    tmp129 = tl.broadcast_to(tmp128, [RBLOCK])
    tmp131 = triton_helpers.promote_to_tensor(tl.sum(tmp129, 0))
    tmp132 = triton_helpers.maximum(tmp131, tmp7)
    tmp133 = tmp9 / tmp132
    tmp134 = tmp133 * tmp11
    tmp135 = triton_helpers.maximum(tmp134, tmp13)
    tmp136 = triton_helpers.minimum(tmp135, tmp15)
    tmp137 = tmp136 * tmp1
    tmp138 = tmp137 * tmp128
    tmp140 = tmp139 + tmp20
    tmp141 = tl_math.log(tmp140)
    tmp142 = tmp138 * tmp141
    tmp143 = tmp136 - tmp7
    tmp144 = tmp137 / tmp143
    tmp145 = tmp7 - tmp128
    tmp146 = tmp144 * tmp145
    tmp147 = tmp7 - tmp139
    tmp148 = tmp147 + tmp20
    tmp149 = tl_math.log(tmp148)
    tmp150 = tmp146 * tmp149
    tmp151 = tmp142 + tmp150
    tmp152 = tl.broadcast_to(tmp151, [RBLOCK])
    tmp154 = triton_helpers.promote_to_tensor(tl.sum(tmp152, 0))
    tmp155 = tmp35 / tmp11
    tmp156 = -tmp155
    tmp157 = tmp154 / tmp11
    tmp158 = -tmp157
    tmp159 = tmp156 + tmp158
    tmp160 = tmp103 * tmp1
    tmp161 = tmp160 / tmp98
    tmp162 = -1.0
    tmp163 = tmp125 * tmp162
    tmp164 = tmp163 / tmp105
    tmp165 = tmp159 * tmp7
    tmp166 = 10.0
    tmp167 = tmp161 * tmp166
    tmp168 = tmp165 + tmp167
    tmp169 = tmp164 * tmp7
    tmp170 = tmp168 + tmp169
    tl.debug_barrier()
    tl.store(in_out_ptr2 + (tl.full([1], 0, tl.int32)), tmp159, None)
    tl.debug_barrier()
    tl.store(in_out_ptr1 + (tl.full([1], 0, tl.int32)), tmp161, None)
    tl.debug_barrier()
    tl.store(in_out_ptr3 + (tl.full([1], 0, tl.int32)), tmp164, None)
    tl.store(out_ptr12 + (tl.full([1], 0, tl.int32)), tmp170, None)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1, arg6_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))
    assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 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))
    assert_size_stride(arg5_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg6_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        # Topologically Sorted Source Nodes: [u_slmask], Original ATen: [aten.rand_like]
        buf11 = torch.ops.aten.rand.default([4, 4, 4, 4], dtype=torch.float32, device=device(type='cuda', index=0), pin_memory=False)
        buf12 = buf11
        del buf11
        # Topologically Sorted Source Nodes: [u_smmask], Original ATen: [aten.rand_like]
        buf7 = torch.ops.aten.rand.default([4, 4, 4, 4], dtype=torch.float32, device=device(type='cuda', index=0), pin_memory=False)
        buf8 = buf7
        del buf7
        buf2 = empty_strided_cuda((), (), torch.float32)
        buf14 = buf12; del buf12  # reuse
        buf15 = empty_strided_cuda((), (), torch.float32)
        buf16 = buf15; del buf15  # reuse
        buf22 = empty_strided_cuda((), (), torch.float32)
        buf6 = buf2; del buf2  # reuse
        buf18 = buf16; del buf16  # reuse
        buf23 = buf22; del buf22  # reuse
        buf24 = empty_strided_cuda((), (), torch.float32)
        # Topologically Sorted Source Nodes: [gt_6, pmask_1, sum_9, num_positive_1, ratio_2, clamp_3, ratio_3, coef_3, mul_18, add_6, log_2, mul_19, mul_16, sub_4, coef_2, sub_5, mul_20, sub_6, add_7, log_3, mul_21, loss_3, mean, loss_4, gt_7, pmask_2, sum_10, num_positive_2, ratio_4, clamp_6, ratio_5, coef_5, mul_24, add_9, log_4, mul_25, mul_22, sub_7, coef_4, sub_8, mul_26, sub_9, add_10, log_5, mul_27, loss_5, mean_1, loss_6, loss_7, mul_28, pred_bm_reg, gt_iou_map, le, gt_1, and_, u_mmask, u_smmask_1, gt, u_hmask, num_h, num_m, r_m, sub, gt_3, u_smmask_2, add, le_1, gt_2, and__1, u_lmask, u_lmask_1, u_slmask_1, num_l, r_l, sub_1, gt_4, u_slmask_2, weights, mul_4, mul_5, loss, ones_like, mul_6, sum_4, mul_7, sum_5, loss_1, mul_29, add_13, gt_5, pmask, sum_6, num_positive, le_2, nmask, nmask_1, sum_7, num_entries, ratio, ratio_1, coef_1, pred_bm_cls, add_3, log, mul_11, loss_pos, mul_9, sub_2, coef_0, sub_3, add_4, log_1, mul_13, loss_neg, add_5, sum_8, mul_15, loss_2, mul_30, loss_8], Original ATen: [aten.gt, aten._to_copy, aten.sum, aten.clamp, aten.reciprocal, aten.mul, aten.add, aten.log, aten.sub, aten.div, aten.rsub, aten.mean, aten.neg, aten.clone, aten.le, aten.bitwise_and, aten.mse_loss, aten.ones_like]
        stream0 = get_raw_stream(0)
        triton_per_fused__to_copy_add_bitwise_and_clamp_clone_div_gt_le_log_mean_mse_loss_mul_neg_ones_like_reciprocal_rsub_sub_sum_0.run(buf14, buf18, buf6, buf23, arg4_1, arg3_1, arg1_1, arg2_1, buf8, arg0_1, arg6_1, arg5_1, buf24, 1, 256, grid=grid(1), stream=stream0)
        del arg0_1
        del arg1_1
        del arg2_1
        del arg3_1
        del arg4_1
        del arg5_1
        del arg6_1
        del buf14
        del buf8
    return (buf24, buf6, buf18, buf23, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    arg0_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    arg1_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    arg2_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    arg3_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    arg4_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    arg5_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    arg6_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1, arg6_1])
    return print_performance(fn, times=times, repeat=repeat)


if __name__ == "__main__":
    from torch._inductor.wrapper_benchmark import compiled_module_main
    compiled_module_main('None', benchmark_compiled_module)

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


def binary_logistic_regression_loss(reg_score, label, threshold=0.5,
    ratio_range=(1.05, 21), eps=1e-05):
    """Binary Logistic Regression Loss."""
    label = label.view(-1)
    reg_score = reg_score.contiguous().view(-1)
    pmask = (label > threshold).float()
    num_positive = max(torch.sum(pmask), 1)
    num_entries = len(label)
    ratio = num_entries / num_positive
    ratio = min(max(ratio, ratio_range[0]), ratio_range[1])
    coef_0 = 0.5 * ratio / (ratio - 1)
    coef_1 = 0.5 * ratio
    loss = coef_1 * pmask * torch.log(reg_score + eps) + coef_0 * (1.0 - pmask
        ) * torch.log(1.0 - reg_score + eps)
    loss = -torch.mean(loss)
    return loss


class BMNLoss(nn.Module):
    """BMN Loss.

    From paper https://arxiv.org/abs/1907.09702,
    code https://github.com/JJBOY/BMN-Boundary-Matching-Network.
    It will calculate loss for BMN Model. This loss is a weighted sum of

        1) temporal evaluation loss based on confidence score of start and
        end positions.
        2) proposal evaluation regression loss based on confidence scores of
        candidate proposals.
        3) proposal evaluation classification loss based on classification
        results of candidate proposals.
    """

    @staticmethod
    def tem_loss(pred_start, pred_end, gt_start, gt_end):
        """Calculate Temporal Evaluation Module Loss.

        This function calculate the binary_logistic_regression_loss for start
        and end respectively and returns the sum of their losses.

        Args:
            pred_start (torch.Tensor): Predicted start score by BMN model.
            pred_end (torch.Tensor): Predicted end score by BMN model.
            gt_start (torch.Tensor): Groundtruth confidence score for start.
            gt_end (torch.Tensor): Groundtruth confidence score for end.

        Returns:
            torch.Tensor: Returned binary logistic loss.
        """
        loss_start = binary_logistic_regression_loss(pred_start, gt_start)
        loss_end = binary_logistic_regression_loss(pred_end, gt_end)
        loss = loss_start + loss_end
        return loss

    @staticmethod
    def pem_reg_loss(pred_score, gt_iou_map, mask,
        high_temporal_iou_threshold=0.7, low_temporal_iou_threshold=0.3):
        """Calculate Proposal Evaluation Module Regression Loss.

        Args:
            pred_score (torch.Tensor): Predicted temporal_iou score by BMN.
            gt_iou_map (torch.Tensor): Groundtruth temporal_iou score.
            mask (torch.Tensor): Boundary-Matching mask.
            high_temporal_iou_threshold (float): Higher threshold of
                temporal_iou. Default: 0.7.
            low_temporal_iou_threshold (float): Higher threshold of
                temporal_iou. Default: 0.3.

        Returns:
            torch.Tensor: Proposal evalutaion regression loss.
        """
        u_hmask = (gt_iou_map > high_temporal_iou_threshold).float()
        u_mmask = ((gt_iou_map <= high_temporal_iou_threshold) & (
            gt_iou_map > low_temporal_iou_threshold)).float()
        u_lmask = ((gt_iou_map <= low_temporal_iou_threshold) & (gt_iou_map >
            0.0)).float()
        u_lmask = u_lmask * mask
        num_h = torch.sum(u_hmask)
        num_m = torch.sum(u_mmask)
        num_l = torch.sum(u_lmask)
        r_m = num_h / num_m
        u_smmask = torch.rand_like(gt_iou_map)
        u_smmask = u_mmask * u_smmask
        u_smmask = (u_smmask > 1.0 - r_m).float()
        r_l = num_h / num_l
        u_slmask = torch.rand_like(gt_iou_map)
        u_slmask = u_lmask * u_slmask
        u_slmask = (u_slmask > 1.0 - r_l).float()
        weights = u_hmask + u_smmask + u_slmask
        loss = F.mse_loss(pred_score * weights, gt_iou_map * weights)
        loss = 0.5 * torch.sum(loss * torch.ones_like(weights)) / torch.sum(
            weights)
        return loss

    @staticmethod
    def pem_cls_loss(pred_score, gt_iou_map, mask, threshold=0.9,
        ratio_range=(1.05, 21), eps=1e-05):
        """Calculate Proposal Evaluation Module Classification Loss.

        Args:
            pred_score (torch.Tensor): Predicted temporal_iou score by BMN.
            gt_iou_map (torch.Tensor): Groundtruth temporal_iou score.
            mask (torch.Tensor): Boundary-Matching mask.
            threshold (float): Threshold of temporal_iou for positive
                instances. Default: 0.9.
            ratio_range (tuple): Lower bound and upper bound for ratio.
                Default: (1.05, 21)
            eps (float): Epsilon for small value. Default: 1e-5

        Returns:
            torch.Tensor: Proposal evalutaion classification loss.
        """
        pmask = (gt_iou_map > threshold).float()
        nmask = (gt_iou_map <= threshold).float()
        nmask = nmask * mask
        num_positive = max(torch.sum(pmask), 1)
        num_entries = num_positive + torch.sum(nmask)
        ratio = num_entries / num_positive
        ratio = torch.clamp(ratio, ratio_range[0], ratio_range[1])
        coef_0 = 0.5 * ratio / (ratio - 1)
        coef_1 = 0.5 * ratio
        loss_pos = coef_1 * torch.log(pred_score + eps) * pmask
        loss_neg = coef_0 * torch.log(1.0 - pred_score + eps) * nmask
        loss = -1 * torch.sum(loss_pos + loss_neg) / num_entries
        return loss

    def forward(self, pred_bm, pred_start, pred_end, gt_iou_map, gt_start,
        gt_end, bm_mask, weight_tem=1.0, weight_pem_reg=10.0,
        weight_pem_cls=1.0):
        """Calculate Boundary Matching Network Loss.

        Args:
            pred_bm (torch.Tensor): Predicted confidence score for boundary
                matching map.
            pred_start (torch.Tensor): Predicted confidence score for start.
            pred_end (torch.Tensor): Predicted confidence score for end.
            gt_iou_map (torch.Tensor): Groundtruth score for boundary matching
                map.
            gt_start (torch.Tensor): Groundtruth temporal_iou score for start.
            gt_end (torch.Tensor): Groundtruth temporal_iou score for end.
            bm_mask (torch.Tensor): Boundary-Matching mask.
            weight_tem (float): Weight for tem loss. Default: 1.0.
            weight_pem_reg (float): Weight for pem regression loss.
                Default: 10.0.
            weight_pem_cls (float): Weight for pem classification loss.
                Default: 1.0.

        Returns:
            tuple([torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]):
                (loss, tem_loss, pem_reg_loss, pem_cls_loss). Loss is the bmn
                loss, tem_loss is the temporal evaluation loss, pem_reg_loss is
                the proposal evaluation regression loss, pem_cls_loss is the
                proposal evaluation classification loss.
        """
        pred_bm_reg = pred_bm[:, 0].contiguous()
        pred_bm_cls = pred_bm[:, 1].contiguous()
        gt_iou_map = gt_iou_map * bm_mask
        pem_reg_loss = self.pem_reg_loss(pred_bm_reg, gt_iou_map, bm_mask)
        pem_cls_loss = self.pem_cls_loss(pred_bm_cls, gt_iou_map, bm_mask)
        tem_loss = self.tem_loss(pred_start, pred_end, gt_start, gt_end)
        loss = (weight_tem * tem_loss + weight_pem_reg * pem_reg_loss + 
            weight_pem_cls * pem_cls_loss)
        return loss, tem_loss, pem_reg_loss, pem_cls_loss


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


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

import torch
from torch import device
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
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_per_fused__to_copy_add_bitwise_and_clamp_clone_div_gt_le_log_mean_mse_loss_mul_neg_ones_like_reciprocal_rsub_sub_sum_0(
    in_out_ptr0, in_out_ptr1, in_out_ptr2, in_out_ptr3, in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, out_ptr12, xnumel,
    rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 256
    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
    r1 = rindex % 16
    r2 = rindex // 16 % 4
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp19 = tl.load(in_ptr1 + r0, None)
    tmp36 = tl.load(in_ptr2 + r0, None)
    tmp37 = tl.load(in_ptr3 + r0, None)
    tmp62 = tl.load(in_ptr4 + r0, None)
    tmp69 = tl.load(in_out_ptr0 + r0, None)
    tmp76 = tl.load(in_ptr5 + (r1 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp110 = tl.load(in_ptr5 + (16 + r1 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp126 = tl.load(in_ptr6 + r0, None)
    tmp139 = tl.load(in_ptr7 + r0, None)
    tmp1 = 0.5
    tmp2 = tmp0 > tmp1
    tmp3 = tmp2.to(tl.float32)
    tmp4 = tl.broadcast_to(tmp3, [RBLOCK])
    tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0))
    tmp7 = 1.0
    tmp8 = triton_helpers.maximum(tmp6, tmp7)
    tmp9 = tl.full([1], 1, tl.int32)
    tmp10 = tmp9 / tmp8
    tmp11 = 256.0
    tmp12 = tmp10 * tmp11
    tmp13 = 1.05
    tmp14 = triton_helpers.maximum(tmp12, tmp13)
    tmp15 = 21.0
    tmp16 = triton_helpers.minimum(tmp14, tmp15)
    tmp17 = tmp16 * tmp1
    tmp18 = tmp17 * tmp3
    tmp20 = 1e-05
    tmp21 = tmp19 + tmp20
    tmp22 = tl_math.log(tmp21)
    tmp23 = tmp18 * tmp22
    tmp24 = tmp16 - tmp7
    tmp25 = tmp17 / tmp24
    tmp26 = tmp7 - tmp3
    tmp27 = tmp25 * tmp26
    tmp28 = tmp7 - tmp19
    tmp29 = tmp28 + tmp20
    tmp30 = tl_math.log(tmp29)
    tmp31 = tmp27 * tmp30
    tmp32 = tmp23 + tmp31
    tmp33 = tl.broadcast_to(tmp32, [RBLOCK])
    tmp35 = triton_helpers.promote_to_tensor(tl.sum(tmp33, 0))
    tmp38 = tmp36 * tmp37
    tmp39 = 0.7
    tmp40 = tmp38 > tmp39
    tmp41 = tmp40.to(tl.float32)
    tmp42 = tl.broadcast_to(tmp41, [RBLOCK])
    tmp44 = triton_helpers.promote_to_tensor(tl.sum(tmp42, 0))
    tmp45 = tmp38 <= tmp39
    tmp46 = 0.3
    tmp47 = tmp38 > tmp46
    tmp48 = tmp45 & tmp47
    tmp49 = tmp48.to(tl.float32)
    tmp50 = tl.broadcast_to(tmp49, [RBLOCK])
    tmp52 = triton_helpers.promote_to_tensor(tl.sum(tmp50, 0))
    tmp53 = tmp38 <= tmp46
    tmp54 = 0.0
    tmp55 = tmp38 > tmp54
    tmp56 = tmp53 & tmp55
    tmp57 = tmp56.to(tl.float32)
    tmp58 = tmp57 * tmp37
    tmp59 = tl.broadcast_to(tmp58, [RBLOCK])
    tmp61 = triton_helpers.promote_to_tensor(tl.sum(tmp59, 0))
    tmp63 = tmp49 * tmp62
    tmp64 = tmp44 / tmp52
    tmp65 = tmp7 - tmp64
    tmp66 = tmp63 > tmp65
    tmp67 = tmp66.to(tl.float32)
    tmp68 = tmp41 + tmp67
    tmp70 = tmp58 * tmp69
    tmp71 = tmp44 / tmp61
    tmp72 = tmp7 - tmp71
    tmp73 = tmp70 > tmp72
    tmp74 = tmp73.to(tl.float32)
    tmp75 = tmp68 + tmp74
    tmp77 = tmp76 * tmp75
    tmp78 = tmp38 * tmp75
    tmp79 = tmp77 - tmp78
    tmp80 = tmp79 * tmp79
    tmp81 = tl.broadcast_to(tmp80, [RBLOCK])
    tmp83 = triton_helpers.promote_to_tensor(tl.sum(tmp81, 0))
    tmp84 = 0.9
    tmp85 = tmp38 > tmp84
    tmp86 = tmp85.to(tl.float32)
    tmp87 = tl.broadcast_to(tmp86, [RBLOCK])
    tmp89 = triton_helpers.promote_to_tensor(tl.sum(tmp87, 0))
    tmp90 = tmp38 <= tmp84
    tmp91 = tmp90.to(tl.float32)
    tmp92 = tmp91 * tmp37
    tmp93 = tl.broadcast_to(tmp92, [RBLOCK])
    tmp95 = triton_helpers.promote_to_tensor(tl.sum(tmp93, 0))
    tmp96 = tl.broadcast_to(tmp75, [RBLOCK])
    tmp98 = triton_helpers.promote_to_tensor(tl.sum(tmp96, 0))
    tmp99 = tmp83 / tmp11
    tmp100 = tmp99 * tmp7
    tmp101 = tl.broadcast_to(tmp100, [RBLOCK])
    tmp103 = triton_helpers.promote_to_tensor(tl.sum(tmp101, 0))
    tmp104 = triton_helpers.maximum(tmp89, tmp7)
    tmp105 = tmp104 + tmp95
    tmp106 = tmp105 / tmp104
    tmp107 = triton_helpers.maximum(tmp106, tmp13)
    tmp108 = triton_helpers.minimum(tmp107, tmp15)
    tmp109 = tmp108 * tmp1
    tmp111 = tmp110 + tmp20
    tmp112 = tl_math.log(tmp111)
    tmp113 = tmp109 * tmp112
    tmp114 = tmp113 * tmp86
    tmp115 = tmp108 - tmp7
    tmp116 = tmp109 / tmp115
    tmp117 = tmp7 - tmp110
    tmp118 = tmp117 + tmp20
    tmp119 = tl_math.log(tmp118)
    tmp120 = tmp116 * tmp119
    tmp121 = tmp120 * tmp92
    tmp122 = tmp114 + tmp121
    tmp123 = tl.broadcast_to(tmp122, [RBLOCK])
    tmp125 = triton_helpers.promote_to_tensor(tl.sum(tmp123, 0))
    tmp127 = tmp126 > tmp1
    tmp128 = tmp127.to(tl.float32)
    tmp129 = tl.broadcast_to(tmp128, [RBLOCK])
    tmp131 = triton_helpers.promote_to_tensor(tl.sum(tmp129, 0))
    tmp132 = triton_helpers.maximum(tmp131, tmp7)
    tmp133 = tmp9 / tmp132
    tmp134 = tmp133 * tmp11
    tmp135 = triton_helpers.maximum(tmp134, tmp13)
    tmp136 = triton_helpers.minimum(tmp135, tmp15)
    tmp137 = tmp136 * tmp1
    tmp138 = tmp137 * tmp128
    tmp140 = tmp139 + tmp20
    tmp141 = tl_math.log(tmp140)
    tmp142 = tmp138 * tmp141
    tmp143 = tmp136 - tmp7
    tmp144 = tmp137 / tmp143
    tmp145 = tmp7 - tmp128
    tmp146 = tmp144 * tmp145
    tmp147 = tmp7 - tmp139
    tmp148 = tmp147 + tmp20
    tmp149 = tl_math.log(tmp148)
    tmp150 = tmp146 * tmp149
    tmp151 = tmp142 + tmp150
    tmp152 = tl.broadcast_to(tmp151, [RBLOCK])
    tmp154 = triton_helpers.promote_to_tensor(tl.sum(tmp152, 0))
    tmp155 = tmp35 / tmp11
    tmp156 = -tmp155
    tmp157 = tmp154 / tmp11
    tmp158 = -tmp157
    tmp159 = tmp156 + tmp158
    tmp160 = tmp103 * tmp1
    tmp161 = tmp160 / tmp98
    tmp162 = -1.0
    tmp163 = tmp125 * tmp162
    tmp164 = tmp163 / tmp105
    tmp165 = tmp159 * tmp7
    tmp166 = 10.0
    tmp167 = tmp161 * tmp166
    tmp168 = tmp165 + tmp167
    tmp169 = tmp164 * tmp7
    tmp170 = tmp168 + tmp169
    tl.debug_barrier()
    tl.store(in_out_ptr2 + tl.full([1], 0, tl.int32), tmp159, None)
    tl.debug_barrier()
    tl.store(in_out_ptr1 + tl.full([1], 0, tl.int32), tmp161, None)
    tl.debug_barrier()
    tl.store(in_out_ptr3 + tl.full([1], 0, tl.int32), tmp164, None)
    tl.store(out_ptr12 + tl.full([1], 0, tl.int32), tmp170, None)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1, arg6_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))
    assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 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))
    assert_size_stride(arg5_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg6_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf11 = torch.ops.aten.rand.default([4, 4, 4, 4], dtype=torch.
            float32, device=device(type='cuda', index=0), pin_memory=False)
        buf12 = buf11
        del buf11
        buf7 = torch.ops.aten.rand.default([4, 4, 4, 4], dtype=torch.
            float32, device=device(type='cuda', index=0), pin_memory=False)
        buf8 = buf7
        del buf7
        buf2 = empty_strided_cuda((), (), torch.float32)
        buf14 = buf12
        del buf12
        buf15 = empty_strided_cuda((), (), torch.float32)
        buf16 = buf15
        del buf15
        buf22 = empty_strided_cuda((), (), torch.float32)
        buf6 = buf2
        del buf2
        buf18 = buf16
        del buf16
        buf23 = buf22
        del buf22
        buf24 = empty_strided_cuda((), (), torch.float32)
        get_raw_stream(0)
        triton_per_fused__to_copy_add_bitwise_and_clamp_clone_div_gt_le_log_mean_mse_loss_mul_neg_ones_like_reciprocal_rsub_sub_sum_0[
            grid(1)](buf14, buf18, buf6, buf23, arg4_1, arg3_1, arg1_1,
            arg2_1, buf8, arg0_1, arg6_1, arg5_1, buf24, 1, 256, num_warps=
            2, num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
        del arg3_1
        del arg4_1
        del arg5_1
        del arg6_1
        del buf14
        del buf8
    return buf24, buf6, buf18, buf23


def binary_logistic_regression_loss(reg_score, label, threshold=0.5,
    ratio_range=(1.05, 21), eps=1e-05):
    """Binary Logistic Regression Loss."""
    label = label.view(-1)
    reg_score = reg_score.contiguous().view(-1)
    pmask = (label > threshold).float()
    num_positive = max(torch.sum(pmask), 1)
    num_entries = len(label)
    ratio = num_entries / num_positive
    ratio = min(max(ratio, ratio_range[0]), ratio_range[1])
    coef_0 = 0.5 * ratio / (ratio - 1)
    coef_1 = 0.5 * ratio
    loss = coef_1 * pmask * torch.log(reg_score + eps) + coef_0 * (1.0 - pmask
        ) * torch.log(1.0 - reg_score + eps)
    loss = -torch.mean(loss)
    return loss


class BMNLossNew(nn.Module):
    """BMN Loss.

    From paper https://arxiv.org/abs/1907.09702,
    code https://github.com/JJBOY/BMN-Boundary-Matching-Network.
    It will calculate loss for BMN Model. This loss is a weighted sum of

        1) temporal evaluation loss based on confidence score of start and
        end positions.
        2) proposal evaluation regression loss based on confidence scores of
        candidate proposals.
        3) proposal evaluation classification loss based on classification
        results of candidate proposals.
    """

    @staticmethod
    def tem_loss(pred_start, pred_end, gt_start, gt_end):
        """Calculate Temporal Evaluation Module Loss.

        This function calculate the binary_logistic_regression_loss for start
        and end respectively and returns the sum of their losses.

        Args:
            pred_start (torch.Tensor): Predicted start score by BMN model.
            pred_end (torch.Tensor): Predicted end score by BMN model.
            gt_start (torch.Tensor): Groundtruth confidence score for start.
            gt_end (torch.Tensor): Groundtruth confidence score for end.

        Returns:
            torch.Tensor: Returned binary logistic loss.
        """
        loss_start = binary_logistic_regression_loss(pred_start, gt_start)
        loss_end = binary_logistic_regression_loss(pred_end, gt_end)
        loss = loss_start + loss_end
        return loss

    @staticmethod
    def pem_reg_loss(pred_score, gt_iou_map, mask,
        high_temporal_iou_threshold=0.7, low_temporal_iou_threshold=0.3):
        """Calculate Proposal Evaluation Module Regression Loss.

        Args:
            pred_score (torch.Tensor): Predicted temporal_iou score by BMN.
            gt_iou_map (torch.Tensor): Groundtruth temporal_iou score.
            mask (torch.Tensor): Boundary-Matching mask.
            high_temporal_iou_threshold (float): Higher threshold of
                temporal_iou. Default: 0.7.
            low_temporal_iou_threshold (float): Higher threshold of
                temporal_iou. Default: 0.3.

        Returns:
            torch.Tensor: Proposal evalutaion regression loss.
        """
        u_hmask = (gt_iou_map > high_temporal_iou_threshold).float()
        u_mmask = ((gt_iou_map <= high_temporal_iou_threshold) & (
            gt_iou_map > low_temporal_iou_threshold)).float()
        u_lmask = ((gt_iou_map <= low_temporal_iou_threshold) & (gt_iou_map >
            0.0)).float()
        u_lmask = u_lmask * mask
        num_h = torch.sum(u_hmask)
        num_m = torch.sum(u_mmask)
        num_l = torch.sum(u_lmask)
        r_m = num_h / num_m
        u_smmask = torch.rand_like(gt_iou_map)
        u_smmask = u_mmask * u_smmask
        u_smmask = (u_smmask > 1.0 - r_m).float()
        r_l = num_h / num_l
        u_slmask = torch.rand_like(gt_iou_map)
        u_slmask = u_lmask * u_slmask
        u_slmask = (u_slmask > 1.0 - r_l).float()
        weights = u_hmask + u_smmask + u_slmask
        loss = F.mse_loss(pred_score * weights, gt_iou_map * weights)
        loss = 0.5 * torch.sum(loss * torch.ones_like(weights)) / torch.sum(
            weights)
        return loss

    @staticmethod
    def pem_cls_loss(pred_score, gt_iou_map, mask, threshold=0.9,
        ratio_range=(1.05, 21), eps=1e-05):
        """Calculate Proposal Evaluation Module Classification Loss.

        Args:
            pred_score (torch.Tensor): Predicted temporal_iou score by BMN.
            gt_iou_map (torch.Tensor): Groundtruth temporal_iou score.
            mask (torch.Tensor): Boundary-Matching mask.
            threshold (float): Threshold of temporal_iou for positive
                instances. Default: 0.9.
            ratio_range (tuple): Lower bound and upper bound for ratio.
                Default: (1.05, 21)
            eps (float): Epsilon for small value. Default: 1e-5

        Returns:
            torch.Tensor: Proposal evalutaion classification loss.
        """
        pmask = (gt_iou_map > threshold).float()
        nmask = (gt_iou_map <= threshold).float()
        nmask = nmask * mask
        num_positive = max(torch.sum(pmask), 1)
        num_entries = num_positive + torch.sum(nmask)
        ratio = num_entries / num_positive
        ratio = torch.clamp(ratio, ratio_range[0], ratio_range[1])
        coef_0 = 0.5 * ratio / (ratio - 1)
        coef_1 = 0.5 * ratio
        loss_pos = coef_1 * torch.log(pred_score + eps) * pmask
        loss_neg = coef_0 * torch.log(1.0 - pred_score + eps) * nmask
        loss = -1 * torch.sum(loss_pos + loss_neg) / num_entries
        return loss

    def forward(self, input_0, input_1, input_2, input_3, input_4, input_5,
        input_6):
        arg0_1 = input_0
        arg1_1 = input_1
        arg2_1 = input_2
        arg3_1 = input_3
        arg4_1 = input_4
        arg5_1 = input_5
        arg6_1 = input_6
        output = call([arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1, arg6_1])
        return output[0], output[1], output[2], output[3]
