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
from typing import *
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__to_copy_add_binary_cross_entropy_div_mul_rsub_sum_0(
    in_ptr0, in_ptr1, out_ptr4, 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))
    tmp16 = tmp3 * tmp0
    tmp17 = tl.broadcast_to(tmp16, [RBLOCK])
    tmp19 = triton_helpers.promote_to_tensor(tl.sum(tmp17, 0))
    tmp20 = tl.broadcast_to(tmp3, [RBLOCK])
    tmp22 = triton_helpers.promote_to_tensor(tl.sum(tmp20, 0))
    tmp23 = tl.broadcast_to(tmp0, [RBLOCK])
    tmp25 = triton_helpers.promote_to_tensor(tl.sum(tmp23, 0))
    tmp26 = 256.0
    tmp27 = tmp15 / tmp26
    tmp28 = tmp27.to(tl.float64)
    tmp29 = 2.0
    tmp30 = tmp19 * tmp29
    tmp31 = tmp30 + tmp1
    tmp32 = tmp22 + tmp25
    tmp33 = tmp32 + tmp1
    tmp34 = tmp31 / tmp33
    tmp35 = tmp1 - tmp34
    tmp36 = tmp35.to(tl.float64)
    tmp37 = tmp28 + tmp36
    tl.store(out_ptr4 + tl.full([1], 0, tl.int32), tmp37, 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)
        buf4 = empty_strided_cuda((), (), torch.float64)
        get_raw_stream(0)
        triton_per_fused__to_copy_add_binary_cross_entropy_div_mul_rsub_sum_0[
            grid(1)](arg1_1, arg0_1, buf4, 1, 256, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
    return buf4,


def dice_coeff(input, target, smooth=1.0):
    input_flat = input.view(-1)
    target_flat = target.view(-1)
    intersection = (input_flat * target_flat).sum()
    return (2.0 * intersection + smooth) / (input_flat.sum() + target_flat.
        sum() + smooth)


class DiceLoss(nn.Module):

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

    def forward(self, input, target):
        dcoeff = dice_coeff(input, target)
        return 1 - dcoeff


class BCEDiceLossNew(nn.Module):

    def __init__(self):
        super(BCEDiceLossNew, self).__init__()
        self.bce_loss = nn.BCELoss()
        self.dice_loss = DiceLoss()

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