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
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_abs_add_clamp_div_exp_log_mean_mul_neg_pow_rsub_sigmoid_sub_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)
    tmp1 = tl.load(in_ptr1 + r0, None)
    tmp2 = tl.sigmoid(tmp1)
    tmp3 = 1.0
    tmp4 = tmp3 - tmp2
    tmp5 = tmp4 * tmp4
    tmp6 = tmp0 * tmp5
    tmp7 = 0.0
    tmp8 = triton_helpers.minimum(tmp1, tmp7)
    tmp9 = tl_math.abs(tmp1)
    tmp10 = -tmp9
    tmp11 = tl_math.exp(tmp10)
    tmp12 = tmp11 + tmp3
    tmp13 = tl_math.log(tmp12)
    tmp14 = tmp8 - tmp13
    tmp15 = triton_helpers.maximum(tmp1, tmp7)
    tmp16 = triton_helpers.minimum(tmp15, tmp7)
    tmp17 = 0.5
    tmp18 = tmp16 * tmp17
    tmp19 = tmp14 + tmp18
    tmp20 = tmp6 * tmp19
    tmp21 = tmp3 - tmp0
    tmp22 = tmp2 * tmp2
    tmp23 = tmp21 * tmp22
    tmp24 = -tmp1
    tmp25 = triton_helpers.minimum(tmp24, tmp7)
    tmp26 = tmp25 - tmp13
    tmp27 = tmp26 + tmp18
    tmp28 = tmp23 * tmp27
    tmp29 = tmp20 + tmp28
    tmp30 = -tmp29
    tmp31 = tmp6 + tmp23
    tmp32 = tl.broadcast_to(tmp31, [RBLOCK])
    tmp34 = triton_helpers.promote_to_tensor(tl.sum(tmp32, 0))
    tmp35 = 256.0
    tmp36 = tmp34 / tmp35
    tmp37 = tmp30 / tmp36
    tmp38 = tl.broadcast_to(tmp37, [RBLOCK])
    tmp40 = triton_helpers.promote_to_tensor(tl.sum(tmp38, 0))
    tmp41 = tmp40 / tmp35
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp41, 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
        buf3 = buf2
        del buf2
        get_raw_stream(0)
        triton_per_fused_abs_add_clamp_div_exp_log_mean_mul_neg_pow_rsub_sigmoid_sub_0[
            grid(1)](buf3, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
    return buf3,


def log_minus_sigmoid(x):
    return torch.clamp(-x, max=0) - torch.log(1 + torch.exp(-torch.abs(x))
        ) + 0.5 * torch.clamp(x, min=0, max=0)


def log_sigmoid(x):
    return torch.clamp(x, max=0) - torch.log(1 + torch.exp(-torch.abs(x))
        ) + 0.5 * torch.clamp(x, min=0, max=0)


class FocalLossNew(nn.Module):

    def __init__(self, gamma=2):
        super(FocalLossNew, self).__init__()
        self.gamma = gamma

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