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
import torch.nn.functional as F
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_clamp_div_exp_log_log_sigmoid_forward_mean_mul_neg_sub_sum_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)
    tmp2 = tl.load(in_ptr1 + r0, None)
    tmp1 = -tmp0
    tmp3 = 2.0
    tmp4 = tmp2 * tmp3
    tmp5 = 1.0
    tmp6 = tmp4 - tmp5
    tmp7 = tmp1 * tmp6
    tmp8 = 0.0
    tmp9 = triton_helpers.minimum(tmp8, tmp7)
    tmp10 = tl_math.abs(tmp7)
    tmp11 = -tmp10
    tmp12 = tl_math.exp(tmp11)
    tmp13 = libdevice.log1p(tmp12)
    tmp14 = tmp9 - tmp13
    tmp15 = 4.0
    tmp16 = tmp14 * tmp15
    tmp17 = tl_math.exp(tmp16)
    tmp18 = tmp0 * tmp2
    tmp19 = tmp0 - tmp18
    tmp20 = triton_helpers.maximum(tmp1, tmp8)
    tmp21 = tmp19 + tmp20
    tmp22 = -tmp20
    tmp23 = tl_math.exp(tmp22)
    tmp24 = tmp1 - tmp20
    tmp25 = tl_math.exp(tmp24)
    tmp26 = tmp23 + tmp25
    tmp27 = tl_math.log(tmp26)
    tmp28 = tmp21 + tmp27
    tmp29 = tmp17 * tmp28
    tmp30 = tl.broadcast_to(tmp29, [RBLOCK])
    tmp32 = triton_helpers.promote_to_tensor(tl.sum(tmp30, 0))
    tmp33 = tl.sigmoid(tmp0)
    tmp34 = tmp33 * tmp2
    tmp35 = tl.broadcast_to(tmp34, [RBLOCK])
    tmp37 = triton_helpers.promote_to_tensor(tl.sum(tmp35, 0))
    tmp38 = tl.broadcast_to(tmp33, [RBLOCK])
    tmp40 = triton_helpers.promote_to_tensor(tl.sum(tmp38, 0))
    tmp41 = tl.broadcast_to(tmp2, [RBLOCK])
    tmp43 = triton_helpers.promote_to_tensor(tl.sum(tmp41, 0))
    tmp44 = 256.0
    tmp45 = tmp32 / tmp44
    tmp46 = tmp45 * tmp15
    tmp47 = tmp37 * tmp3
    tmp48 = tmp47 + tmp5
    tmp49 = tmp40 + tmp43
    tmp50 = tmp49 + tmp5
    tmp51 = tmp48 / tmp50
    tmp52 = tl_math.log(tmp51)
    tmp53 = tmp46 - tmp52
    tmp54 = tmp53 / tmp5
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp54, 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)
        buf0 = empty_strided_cuda((), (), torch.float32)
        buf4 = buf0
        del buf0
        get_raw_stream(0)
        triton_per_fused_add_clamp_div_exp_log_log_sigmoid_forward_mean_mul_neg_sub_sum_0[
            grid(1)](buf4, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
    return buf4,


def dice_loss(input, target):
    input = torch.sigmoid(input)
    smooth = 1.0
    iflat = input.view(-1)
    tflat = target.view(-1)
    intersection = (iflat * tflat).sum()
    return (2.0 * intersection + smooth) / (iflat.sum() + tflat.sum() + smooth)


class FocalLoss(nn.Module):

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

    def forward(self, input, target):
        if not target.size() == input.size():
            raise ValueError(
                'Target size ({}) must be the same as input size ({})'.
                format(target.size(), input.size()))
        max_val = (-input).clamp(min=0)
        loss = input - input * target + max_val + ((-max_val).exp() + (-
            input - max_val).exp()).log()
        invprobs = F.logsigmoid(-input * (target * 2.0 - 1.0))
        loss = (invprobs * self.gamma).exp() * loss
        return loss.mean()


class MixedLossNew(nn.Module):

    def __init__(self, alpha, gamma):
        super().__init__()
        self.alpha = alpha
        self.focal = FocalLoss(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.014967