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_add_clamp_div_log_mean_mul_neg_rsub_sigmoid_sub_sum_0(
    in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, 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)
    tmp4 = tl.load(in_ptr2 + r0, None)
    tmp1 = -tmp0
    tmp3 = tmp1 * tmp2
    tmp5 = tl.sigmoid(tmp4)
    tmp6 = 1e-08
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = 0.99999999
    tmp9 = triton_helpers.minimum(tmp7, tmp8)
    tmp10 = tl_math.log(tmp9)
    tmp11 = tmp3 * tmp10
    tmp12 = 1.0
    tmp13 = tmp12 - tmp2
    tmp14 = tmp12 - tmp9
    tmp15 = tl_math.log(tmp14)
    tmp16 = tmp13 * tmp15
    tmp17 = tmp11 - tmp16
    tmp18 = tl.broadcast_to(tmp17, [RBLOCK])
    tmp20 = triton_helpers.promote_to_tensor(tl.sum(tmp18, 0))
    tmp21 = tmp5 * tmp2
    tmp22 = tl.broadcast_to(tmp21, [RBLOCK])
    tmp24 = triton_helpers.promote_to_tensor(tl.sum(tmp22, 0))
    tmp25 = tl.broadcast_to(tmp5, [RBLOCK])
    tmp27 = triton_helpers.promote_to_tensor(tl.sum(tmp25, 0))
    tmp28 = tl.broadcast_to(tmp2, [RBLOCK])
    tmp30 = triton_helpers.promote_to_tensor(tl.sum(tmp28, 0))
    tmp31 = 256.0
    tmp32 = tmp20 / tmp31
    tmp33 = 2.0
    tmp34 = tmp24 * tmp33
    tmp35 = tmp34 + tmp6
    tmp36 = tmp27 + tmp30
    tmp37 = tmp36 + tmp6
    tmp38 = tmp35 / tmp37
    tmp39 = tmp12 - tmp38
    tmp40 = 0.5
    tmp41 = tmp39 * tmp40
    tmp42 = tmp32 + tmp41
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp42, None)


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((), (), torch.float32)
        buf4 = buf0
        del buf0
        get_raw_stream(0)
        triton_per_fused_add_clamp_div_log_mean_mul_neg_rsub_sigmoid_sub_sum_0[
            grid(1)](buf4, arg2_1, arg1_1, arg0_1, 1, 256, num_warps=2,
            num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
    return buf4,


def dice_loss(pred, target, smooth=1e-08):
    iflat = pred.view(-1)
    tflat = target.view(-1)
    intersection = (iflat * tflat).sum()
    return 1 - (2.0 * intersection + smooth) / (iflat.sum() + tflat.sum() +
        smooth)


def weighted_binary_cross_entropy(sigmoid_x, targets, pos_weight, weight=
    None, size_average=True, reduce=True):
    """
    Args:
        sigmoid_x: predicted probability of size [N,C], N sample and C Class. Eg. Must be in range of [0,1], i.e. Output from Sigmoid.
        targets: true value, one-hot-like vector of size [N,C]
        pos_weight: Weight for postive sample
    """
    if not targets.size() == sigmoid_x.size():
        raise ValueError('Target size ({}) must be the same as input size ({})'
            .format(targets.size(), sigmoid_x.size()))
    sigmoid_x = sigmoid_x.clamp(min=1e-08, max=1 - 1e-08)
    loss = -pos_weight * targets * sigmoid_x.log() - (1 - targets) * (1 -
        sigmoid_x).log()
    if weight is not None:
        loss = loss * weight
    if not reduce:
        return loss
    elif size_average:
        return loss.mean()
    else:
        return loss.sum()


class WBCEDiceLossNew(nn.Module):

    def __init__(self, alpha=0.5):
        super(WBCEDiceLossNew, self).__init__()
        self.alpha = alpha

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