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_poi_fused_abs_add_clamp_exp_log1p_mul_neg_pow_rsub_sigmoid_sub_0(
    in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    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_ptr1 + x0, xmask)
    tmp27 = tl.load(in_ptr2 + x0, xmask)
    tmp2 = tl.sigmoid(tmp1)
    tmp3 = tmp0 * tmp2
    tmp4 = 1.0
    tmp5 = tmp4 - tmp0
    tmp6 = tmp4 - tmp2
    tmp7 = tmp5 * tmp6
    tmp8 = tmp3 + tmp7
    tmp9 = tmp4 - tmp8
    tmp10 = tmp9 * tmp9
    tmp11 = 0.25
    tmp12 = tmp0 * tmp11
    tmp13 = 0.75
    tmp14 = tmp5 * tmp13
    tmp15 = tmp12 + tmp14
    tmp16 = tmp10 * tmp15
    tmp17 = 0.0
    tmp18 = triton_helpers.maximum(tmp1, tmp17)
    tmp19 = tmp1 * tmp0
    tmp20 = tmp18 - tmp19
    tmp21 = tl_math.abs(tmp1)
    tmp22 = -tmp21
    tmp23 = tl_math.exp(tmp22)
    tmp24 = libdevice.log1p(tmp23)
    tmp25 = tmp20 + tmp24
    tmp26 = tmp16 * tmp25
    tmp28 = tmp26 * tmp27
    tl.store(out_ptr0 + x0, tmp28, xmask)


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((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_abs_add_clamp_exp_log1p_mul_neg_pow_rsub_sigmoid_sub_0[
            grid(256)](arg1_1, arg0_1, arg2_1, buf0, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
    return buf0,


def _sigmoid_cross_entropy_with_logits(logits, labels):
    loss = torch.clamp(logits, min=0) - logits * labels.type_as(logits)
    loss += torch.log1p(torch.exp(-torch.abs(logits)))
    return loss


class SigmoidFocalClassificationLossNew(nn.Module):
    """Sigmoid focal cross entropy loss.
      Focal loss down-weights well classified examples and focusses on the hard
      examples. See https://arxiv.org/pdf/1708.02002.pdf for the loss definition.
    """

    def __init__(self, gamma=2.0, alpha=0.25):
        """Constructor.
        Args:
            gamma: exponent of the modulating factor (1 - p_t) ^ gamma.
            alpha: optional alpha weighting factor to balance positives vs negatives.
            all_zero_negative: bool. if True, will treat all zero as background.
            else, will treat first label as background. only affect alpha.
        """
        super().__init__()
        self._alpha = alpha
        self._gamma = gamma

    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.015951