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_per_fused_add_binary_cross_entropy_div_mul_rsub_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)
    tmp4 = tl.load(in_ptr1 + r0, None)
    tmp1 = tl.broadcast_to(tmp0, [RBLOCK])
    tmp3 = triton_helpers.promote_to_tensor(tl.sum(tmp1, 0))
    tmp5 = tmp4 * tmp0
    tmp6 = tl.broadcast_to(tmp5, [RBLOCK])
    tmp8 = triton_helpers.promote_to_tensor(tl.sum(tmp6, 0))
    tmp9 = 1.0
    tmp10 = tmp0 - tmp9
    tmp11 = -tmp4
    tmp12 = libdevice.log1p(tmp11)
    tmp13 = -100.0
    tmp14 = triton_helpers.maximum(tmp12, tmp13)
    tmp15 = tmp10 * tmp14
    tmp16 = tl_math.log(tmp4)
    tmp17 = triton_helpers.maximum(tmp16, tmp13)
    tmp18 = tmp0 * tmp17
    tmp19 = tmp15 - tmp18
    tmp20 = tl.broadcast_to(tmp19, [RBLOCK])
    tmp22 = triton_helpers.promote_to_tensor(tl.sum(tmp20, 0))
    tmp23 = tl.broadcast_to(tmp4, [RBLOCK])
    tmp25 = triton_helpers.promote_to_tensor(tl.sum(tmp23, 0))
    tmp26 = 2.0
    tmp27 = tmp8 * tmp26
    tmp28 = tmp27 + tmp9
    tmp29 = tmp25 + tmp3
    tmp30 = tmp29 + tmp9
    tmp31 = tmp28 / tmp30
    tmp32 = tmp9 - tmp31
    tmp33 = 256.0
    tmp34 = tmp22 / tmp33
    tmp35 = tmp32 + tmp34
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp35, 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_binary_cross_entropy_div_mul_rsub_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,


class DiceLoss(nn.Module):

    def __init__(self, smooth: 'float'=1.0, apply_sigmoid: 'bool'=False):
        super().__init__()
        self.smooth = smooth
        self.apply_sigmoid = apply_sigmoid

    def forward(self, y_pred: 'torch.Tensor', y_true: 'torch.Tensor'
        ) ->torch.Tensor:
        assert y_pred.size() == y_true.size()
        if self.apply_sigmoid:
            y_pred = torch.sigmoid(y_pred)
        y_pred = y_pred.contiguous().view(-1)
        y_true = y_true.contiguous().view(-1)
        intersection = (y_pred * y_true).sum()
        dsc = (2.0 * intersection + self.smooth) / (y_pred.sum() + y_true.
            sum() + self.smooth)
        return 1.0 - dsc


class DiceBCELossNew(nn.Module):

    def __init__(self, smooth: 'float'=1.0, apply_sigmoid: 'bool'=False):
        super().__init__()
        self.dice_loss = DiceLoss(smooth)
        self.apply_sigmoid = apply_sigmoid

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