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
from torch import nn
from math import sqrt as sqrt
from itertools import product as product
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_add_exp_mul_neg_smooth_l1_loss_sum_0(in_ptr0, in_ptr1,
    in_ptr2, out_ptr0, 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)
    tmp4 = tl.load(in_ptr2 + r0, None)
    tmp1 = -tmp0
    tmp2 = tl_math.exp(tmp1)
    tmp5 = tmp3 - tmp4
    tmp6 = tl_math.abs(tmp5)
    tmp7 = 1.0
    tmp8 = tmp6 < tmp7
    tmp9 = tmp6 * tmp6
    tmp10 = 0.5
    tmp11 = tmp9 * tmp10
    tmp12 = tmp11 * tmp7
    tmp13 = tmp6 - tmp10
    tmp14 = tl.where(tmp8, tmp12, tmp13)
    tmp15 = tmp2 * tmp14
    tmp16 = tmp0 * tmp10
    tmp17 = tmp15 + tmp16
    tmp18 = tl.broadcast_to(tmp17, [RBLOCK])
    tmp20 = triton_helpers.promote_to_tensor(tl.sum(tmp18, 0))
    tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp20, None)


def call(args):
    arg0_1, arg1_1, arg2_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))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((), (), torch.float32)
        get_raw_stream(0)
        triton_per_fused_add_exp_mul_neg_smooth_l1_loss_sum_0[grid(1)](arg2_1,
            arg1_1, arg0_1, buf0, 1, 256, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
    return buf0,


class KLLossNew(nn.Module):
    """
    Kl-loss function for bounding box regression from CVPR 2019 paper:
    Bounding Box Regression with Uncertainty for Accurate Object Detection
    by Yihui He, Chenchen Zhu, Jianren Wang. Marios Savvides, Xiangyu Zhang

    It is a replacement for the Smooth L1 loss often used in bounding box regression.

    The regression loss for a coordinate depends on |xg − xe| > 1 or not:

    Loss |xg − xe| ≤ 1:

        Lreg1 ∝ e^{−α} * 1/2(xg − xe)^2 + 1/2α

    and if |xg − xe| > 1, Loss:

        Lreg2 = e^{−α} (|xg − xe| − 1/2) + 1/2α

    PyTorch implementation by Jasper Bakker (JappaB @github)
    """

    def __init__(self, loc_loss_weight=1.0):
        super(KLLossNew, self).__init__()
        self.loc_loss_weight = loc_loss_weight

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


Measured latency_ms: 0.016018