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
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_clamp_eq_log_log_sigmoid_forward_mean_mul_neg_pow_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)
    tmp4 = tl.load(in_ptr1 + r0, None)
    tmp1 = 1.0
    tmp2 = tmp0 == tmp1
    tmp3 = tmp2.to(tl.float32)
    tmp5 = tl.sigmoid(tmp4)
    tmp6 = 1e-06
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = 0.999999
    tmp9 = triton_helpers.minimum(tmp7, tmp8)
    tmp10 = tmp1 - tmp9
    tmp11 = tmp10 * tmp10
    tmp12 = tmp3 * tmp11
    tmp13 = -tmp12
    tmp14 = tl_math.log(tmp9)
    tmp15 = tmp13 * tmp14
    tmp16 = 0.0
    tmp17 = tmp0 == tmp16
    tmp18 = tmp17.to(tl.float32)
    tmp19 = tmp9 * tmp9
    tmp20 = tmp18 * tmp19
    tmp21 = -3.0
    tmp22 = tmp20 * tmp21
    tmp23 = -tmp4
    tmp24 = triton_helpers.minimum(tmp16, tmp23)
    tmp25 = tl_math.abs(tmp23)
    tmp26 = -tmp25
    tmp27 = tl_math.exp(tmp26)
    tmp28 = libdevice.log1p(tmp27)
    tmp29 = tmp24 - tmp28
    tmp30 = tmp22 * tmp29
    tmp31 = tmp15 + tmp30
    tmp32 = tl.broadcast_to(tmp31, [RBLOCK])
    tmp34 = triton_helpers.promote_to_tensor(tl.sum(tmp32, 0))
    tmp35 = 256.0
    tmp36 = tmp34 / tmp35
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp36, None)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf1 = empty_strided_cuda((), (), torch.float32)
        buf2 = buf1
        del buf1
        get_raw_stream(0)
        triton_per_fused__to_copy_add_clamp_eq_log_log_sigmoid_forward_mean_mul_neg_pow_rsub_sigmoid_0[
            grid(1)](buf2, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
    return buf2,


class BinaryFocalLossNew(nn.Module):
    """
    This is a implementation of Focal Loss with smooth label cross entropy supported which is proposed in
    'Focal Loss for Dense Object Detection. (https://arxiv.org/abs/1708.02002)'
        Focal_Loss= -1*alpha*(1-pt)*log(pt)
    :param alpha: (tensor) 3D or 4D the scalar factor for this criterion
    :param gamma: (float,double) gamma > 0 reduces the relative loss for well-classified examples (p>0.5) putting more
                    focus on hard misclassified example
    :param reduction: `none`|`mean`|`sum`
    :param **kwargs
        balance_index: (int) balance class index, should be specific when alpha is float
    """

    def __init__(self, alpha=3, gamma=2, ignore_index=None, reduction=
        'mean', **kwargs):
        super(BinaryFocalLossNew, self).__init__()
        self.alpha = alpha
        self.gamma = gamma
        self.smooth = 1e-06
        self.ignore_index = ignore_index
        self.reduction = reduction
        assert self.reduction in ['none', 'mean', 'sum']

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


Measured latency_ms: 0.015122