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 libdevice, math as tl_math
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_per_fused_binary_cross_entropy_with_logits_div_mean_mul_rsub_sigmoid_0(
    in_out_ptr0, in_ptr0, in_ptr1, 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
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp3 = tl.load(in_ptr1 + r0, None)
    tmp1 = 1.0
    tmp2 = tmp1 - tmp0
    tmp4 = tmp2 * tmp3
    tmp5 = 0.0
    tmp6 = triton_helpers.minimum(tmp5, tmp3)
    tmp7 = tl_math.abs(tmp3)
    tmp8 = -tmp7
    tmp9 = tl_math.exp(tmp8)
    tmp10 = libdevice.log1p(tmp9)
    tmp11 = tmp6 - tmp10
    tmp12 = tmp4 - tmp11
    tmp13 = 2.0
    tmp14 = tmp0 * tmp13
    tmp15 = tmp1 - tmp14
    tmp16 = tmp3 * tmp15
    tmp17 = tl.sigmoid(tmp16)
    tmp18 = tmp12 * tmp17
    tmp19 = tl.broadcast_to(tmp18, [RBLOCK])
    tmp21 = triton_helpers.promote_to_tensor(tl.sum(tmp19, 0))
    tmp22 = tl.broadcast_to(tmp17, [RBLOCK])
    tmp24 = triton_helpers.promote_to_tensor(tl.sum(tmp22, 0))
    tmp25 = 256.0
    tmp26 = tmp21 / tmp25
    tmp27 = tmp24 / tmp25
    tmp28 = tmp26 / tmp27
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp28, None)


@triton.jit
def triton_per_fused_max_mean_mul_smooth_l1_loss_1(in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, in_ptr4, out_ptr0, out_ptr1, 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 % 16
    r2 = rindex // 64
    r3 = rindex
    tmp0 = tl.load(in_ptr0 + (r0 + 64 * r2), None, eviction_policy='evict_last'
        )
    tmp1 = tl.load(in_ptr0 + (16 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr0 + (32 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp5 = tl.load(in_ptr0 + (48 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp7 = tl.load(in_ptr1 + r3, None)
    tmp8 = tl.load(in_ptr2 + r3, None)
    tmp23 = tl.load(in_ptr3 + r3, None)
    tmp24 = tl.load(in_ptr4 + r3, None)
    tmp2 = triton_helpers.maximum(tmp0, tmp1)
    tmp4 = triton_helpers.maximum(tmp2, tmp3)
    tmp6 = triton_helpers.maximum(tmp4, tmp5)
    tmp9 = tmp7 - tmp8
    tmp10 = tl_math.abs(tmp9)
    tmp11 = 1.0
    tmp12 = tmp10 < tmp11
    tmp13 = tmp10 * tmp10
    tmp14 = 0.5
    tmp15 = tmp13 * tmp14
    tmp16 = tmp15 * tmp11
    tmp17 = tmp10 - tmp14
    tmp18 = tl.where(tmp12, tmp16, tmp17)
    tmp19 = tmp6 * tmp18
    tmp20 = tl.broadcast_to(tmp19, [RBLOCK])
    tmp22 = triton_helpers.promote_to_tensor(tl.sum(tmp20, 0))
    tmp25 = tmp23 - tmp24
    tmp26 = tl_math.abs(tmp25)
    tmp27 = tmp26 < tmp11
    tmp28 = tmp26 * tmp26
    tmp29 = tmp28 * tmp14
    tmp30 = tmp29 * tmp11
    tmp31 = tmp26 - tmp14
    tmp32 = tl.where(tmp27, tmp30, tmp31)
    tmp33 = tmp6 * tmp32
    tmp34 = tl.broadcast_to(tmp33, [RBLOCK])
    tmp36 = triton_helpers.promote_to_tensor(tl.sum(tmp34, 0))
    tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp22, None)
    tl.store(out_ptr1 + tl.full([1], 0, tl.int32), tmp36, None)


@triton.jit
def triton_per_fused_div_max_mean_mul_smooth_l1_loss_2(in_out_ptr0,
    in_out_ptr1, in_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 64
    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 % 16
    r1 = rindex // 16
    tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None)
    tmp1 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None)
    tmp3 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None)
    tmp5 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None)
    tmp10 = tl.load(in_out_ptr0 + 0)
    tmp11 = tl.broadcast_to(tmp10, [XBLOCK, 1])
    tmp17 = tl.load(in_out_ptr1 + 0)
    tmp18 = tl.broadcast_to(tmp17, [XBLOCK, 1])
    tmp2 = triton_helpers.maximum(tmp0, tmp1)
    tmp4 = triton_helpers.maximum(tmp2, tmp3)
    tmp6 = triton_helpers.maximum(tmp4, tmp5)
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tmp9 = tl.sum(tmp7, 1)[:, None]
    tmp12 = 256.0
    tmp13 = tmp11 / tmp12
    tmp14 = 64.0
    tmp15 = tmp9 / tmp14
    tmp16 = tmp13 / tmp15
    tmp19 = tmp18 / tmp12
    tmp20 = tmp19 / tmp15
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp16, None)
    tl.debug_barrier()
    tl.store(in_out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp20, None)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_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))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((), (), torch.float32)
        buf6 = buf0
        del buf0
        get_raw_stream(0)
        triton_per_fused_binary_cross_entropy_with_logits_div_mean_mul_rsub_sigmoid_0[
            grid(1)](buf6, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1)
        del arg1_1
        buf2 = empty_strided_cuda((), (), torch.float32)
        buf4 = empty_strided_cuda((), (), torch.float32)
        triton_per_fused_max_mean_mul_smooth_l1_loss_1[grid(1)](arg0_1,
            arg3_1, arg2_1, arg5_1, arg4_1, buf2, buf4, 1, 256, num_warps=2,
            num_stages=1)
        del arg2_1
        del arg3_1
        del arg4_1
        del arg5_1
        buf8 = buf4
        del buf4
        buf7 = buf2
        del buf2
        triton_per_fused_div_max_mean_mul_smooth_l1_loss_2[grid(1)](buf8,
            buf7, arg0_1, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
    return buf6, buf7, buf8


class DetLossNew(nn.Module):

    def __init__(self):
        super().__init__()
        self.hm_criterion = nn.BCEWithLogitsLoss(reduction='none')
        self.ori_criterion = nn.SmoothL1Loss(reduction='none')
        self.box_criterion = nn.SmoothL1Loss(reduction='none')

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


Measured latency_ms: 0.044835