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)
    tmp8 = tl.load(in_ptr1 + x0, xmask)
    tmp26 = tl.load(in_ptr2 + x0, xmask)
    tmp1 = 0.25
    tmp2 = tmp0 * tmp1
    tmp3 = 1.0
    tmp4 = tmp3 - tmp0
    tmp5 = 0.75
    tmp6 = tmp4 * tmp5
    tmp7 = tmp2 + tmp6
    tmp9 = tl.sigmoid(tmp8)
    tmp10 = tmp3 - tmp9
    tmp11 = tmp0 * tmp10
    tmp12 = tmp4 * tmp9
    tmp13 = tmp11 + tmp12
    tmp14 = tmp13 * tmp13
    tmp15 = tmp7 * tmp14
    tmp16 = 0.0
    tmp17 = triton_helpers.maximum(tmp8, tmp16)
    tmp18 = tmp8 * tmp0
    tmp19 = tmp17 - tmp18
    tmp20 = tl_math.abs(tmp8)
    tmp21 = -tmp20
    tmp22 = tl_math.exp(tmp21)
    tmp23 = libdevice.log1p(tmp22)
    tmp24 = tmp19 + tmp23
    tmp25 = tmp15 * tmp24
    tmp27 = tmp25 * tmp26
    tl.store(out_ptr0 + x0, tmp27, 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=256,
            num_warps=4, num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
    return buf0,


class SigmoidFocalClassificationLossNew(nn.Module):
    """
    Sigmoid focal cross entropy loss.
    """

    def __init__(self, gamma: 'float'=2.0, alpha: 'float'=0.25):
        """
        Args:
            gamma: Weighting parameter to balance loss for hard and easy examples.
            alpha: Weighting parameter to balance loss for positive and negative examples.
        """
        super(SigmoidFocalClassificationLossNew, self).__init__()
        self.alpha = alpha
        self.gamma = gamma

    @staticmethod
    def sigmoid_cross_entropy_with_logits(input: 'torch.Tensor', target:
        'torch.Tensor'):
        """ PyTorch Implementation for tf.nn.sigmoid_cross_entropy_with_logits:
            max(x, 0) - x * z + log(1 + exp(-abs(x))) in
            https://www.tensorflow.org/api_docs/python/tf/nn/sigmoid_cross_entropy_with_logits

        Args:
            input: (B, #anchors, #classes) float tensor.
                Predicted logits for each class
            target: (B, #anchors, #classes) float tensor.
                One-hot encoded classification targets

        Returns:
            loss: (B, #anchors, #classes) float tensor.
                Sigmoid cross entropy loss without reduction
        """
        loss = torch.clamp(input, min=0) - input * target + torch.log1p(torch
            .exp(-torch.abs(input)))
        return loss

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