import torch
import torch.nn as nn
import torch.nn.functional as F


class DiceBCELoss(nn.Module):

    def __init__(self):
        super(DiceBCELoss, self).__init__()

    def forward(self, predicted, target):
        batch = predicted.size()[0]
        batch_loss = 0
        smooth = 1
        for index in range(batch):
            pre = predicted[index]
            tar = target[index]
            intersection = torch.mul(pre, tar).sum()
            coefficient = (2 * intersection + smooth) / (pre.sum() + tar.
                sum() + smooth)
            batch_loss += coefficient
        batch_loss = batch_loss / batch
        BCE = F.binary_cross_entropy(predicted, target)
        Dice_BCE = BCE + (1 - batch_loss)
        return Dice_BCE


def get_inputs():
    return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {}]

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_binary_cross_entropy_0(in_ptr0, in_ptr1, out_ptr0,
    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)
    tmp3 = tl.load(in_ptr1 + r0, None)
    tmp1 = 1.0
    tmp2 = tmp0 - tmp1
    tmp4 = -tmp3
    tmp5 = libdevice.log1p(tmp4)
    tmp6 = -100.0
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tmp2 * tmp7
    tmp9 = tl_math.log(tmp3)
    tmp10 = triton_helpers.maximum(tmp9, tmp6)
    tmp11 = tmp0 * tmp10
    tmp12 = tmp8 - tmp11
    tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
    tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
    tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp15, None)


@triton.jit
def triton_per_fused_add_binary_cross_entropy_div_mul_rsub_sum_1(in_out_ptr1,
    in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.load(in_ptr1 + r0, None)
    tmp12 = tl.load(in_ptr0 + (64 + r0), None)
    tmp13 = tl.load(in_ptr1 + (64 + r0), None)
    tmp24 = tl.load(in_ptr0 + (128 + r0), None)
    tmp25 = tl.load(in_ptr1 + (128 + r0), None)
    tmp36 = tl.load(in_ptr0 + (192 + r0), None)
    tmp37 = tl.load(in_ptr1 + (192 + r0), None)
    tmp75 = tl.load(in_out_ptr1 + 0)
    tmp76 = tl.broadcast_to(tmp75, [XBLOCK, 1])
    tmp2 = tmp0 * tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.sum(tmp3, 1)[:, None]
    tmp6 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp8 = tl.sum(tmp6, 1)[:, None]
    tmp9 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp11 = tl.sum(tmp9, 1)[:, None]
    tmp14 = tmp12 * tmp13
    tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK])
    tmp17 = tl.sum(tmp15, 1)[:, None]
    tmp18 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
    tmp20 = tl.sum(tmp18, 1)[:, None]
    tmp21 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK])
    tmp23 = tl.sum(tmp21, 1)[:, None]
    tmp26 = tmp24 * tmp25
    tmp27 = tl.broadcast_to(tmp26, [XBLOCK, RBLOCK])
    tmp29 = tl.sum(tmp27, 1)[:, None]
    tmp30 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK])
    tmp32 = tl.sum(tmp30, 1)[:, None]
    tmp33 = tl.broadcast_to(tmp25, [XBLOCK, RBLOCK])
    tmp35 = tl.sum(tmp33, 1)[:, None]
    tmp38 = tmp36 * tmp37
    tmp39 = tl.broadcast_to(tmp38, [XBLOCK, RBLOCK])
    tmp41 = tl.sum(tmp39, 1)[:, None]
    tmp42 = tl.broadcast_to(tmp36, [XBLOCK, RBLOCK])
    tmp44 = tl.sum(tmp42, 1)[:, None]
    tmp45 = tl.broadcast_to(tmp37, [XBLOCK, RBLOCK])
    tmp47 = tl.sum(tmp45, 1)[:, None]
    tmp48 = 2.0
    tmp49 = tmp5 * tmp48
    tmp50 = 1.0
    tmp51 = tmp49 + tmp50
    tmp52 = tmp8 + tmp11
    tmp53 = tmp52 + tmp50
    tmp54 = tmp51 / tmp53
    tmp55 = 0.0
    tmp56 = tmp54 + tmp55
    tmp57 = tmp17 * tmp48
    tmp58 = tmp57 + tmp50
    tmp59 = tmp20 + tmp23
    tmp60 = tmp59 + tmp50
    tmp61 = tmp58 / tmp60
    tmp62 = tmp56 + tmp61
    tmp63 = tmp29 * tmp48
    tmp64 = tmp63 + tmp50
    tmp65 = tmp32 + tmp35
    tmp66 = tmp65 + tmp50
    tmp67 = tmp64 / tmp66
    tmp68 = tmp62 + tmp67
    tmp69 = tmp41 * tmp48
    tmp70 = tmp69 + tmp50
    tmp71 = tmp44 + tmp47
    tmp72 = tmp71 + tmp50
    tmp73 = tmp70 / tmp72
    tmp74 = tmp68 + tmp73
    tmp77 = 256.0
    tmp78 = tmp76 / tmp77
    tmp79 = 0.25
    tmp80 = tmp74 * tmp79
    tmp81 = tmp50 - tmp80
    tmp82 = tmp78 + tmp81
    tl.debug_barrier()
    tl.store(in_out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp82, 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)
        get_raw_stream(0)
        triton_per_fused_binary_cross_entropy_0[grid(1)](arg1_1, arg0_1,
            buf0, 1, 256, num_warps=2, num_stages=1)
        buf14 = buf0
        del buf0
        triton_per_fused_add_binary_cross_entropy_div_mul_rsub_sum_1[grid(1)](
            buf14, arg0_1, arg1_1, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
    return buf14,


class DiceBCELossNew(nn.Module):

    def __init__(self):
        super(DiceBCELossNew, self).__init__()

    def forward(self, input_0, input_1):
        arg0_1 = input_0
        arg1_1 = input_1
        output = call([arg0_1, arg1_1])
        return output[0]
