import torch
import torch.utils.data
import torch.nn as nn
import torch.nn.parallel


class DiceLoss(nn.Module):
    """DICE loss.
    """

    def __init__(self, reduce=True, smooth=100.0, power=1):
        super(DiceLoss, self).__init__()
        self.smooth = smooth
        self.reduce = reduce
        self.power = power

    def dice_loss(self, pred, target):
        loss = 0.0
        for index in range(pred.size()[0]):
            iflat = pred[index].contiguous().view(-1)
            tflat = target[index].contiguous().view(-1)
            intersection = (iflat * tflat).sum()
            if self.power == 1:
                loss += 1 - (2.0 * intersection + self.smooth) / (iflat.sum
                    () + tflat.sum() + self.smooth)
            else:
                loss += 1 - (2.0 * intersection + self.smooth) / ((iflat **
                    self.power).sum() + (tflat ** self.power).sum() + self.
                    smooth)
        return loss / float(pred.size()[0])

    def dice_loss_batch(self, pred, target):
        iflat = pred.view(-1)
        tflat = target.view(-1)
        intersection = (iflat * tflat).sum()
        if self.power == 1:
            loss = 1 - (2.0 * intersection + self.smooth) / (iflat.sum() +
                tflat.sum() + self.smooth)
        else:
            loss = 1 - (2.0 * intersection + self.smooth) / ((iflat ** self
                .power).sum() + (tflat ** self.power).sum() + self.smooth)
        return loss

    def forward(self, pred, target, weight_mask=None):
        if not target.size() == pred.size():
            raise ValueError(
                'Target size ({}) must be the same as pred size ({})'.
                format(target.size(), pred.size()))
        if self.reduce:
            loss = self.dice_loss(pred, target)
        else:
            loss = self.dice_loss_batch(pred, target)
        return loss


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
import torch.utils.data
import torch.nn as nn
import torch.nn.parallel
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_div_mul_rsub_sum_0(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 + (192 + r0), None)
    tmp25 = tl.load(in_ptr1 + (192 + r0), None)
    tmp36 = tl.load(in_ptr0 + (128 + r0), None)
    tmp37 = tl.load(in_ptr1 + (128 + r0), None)
    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 = 100.0
    tmp51 = tmp49 + tmp50
    tmp52 = tmp8 + tmp11
    tmp53 = tmp52 + tmp50
    tmp54 = tmp51 / tmp53
    tmp55 = 1.0
    tmp56 = tmp55 - tmp54
    tmp57 = 0.0
    tmp58 = tmp56 + tmp57
    tmp59 = tmp17 * tmp48
    tmp60 = tmp59 + tmp50
    tmp61 = tmp20 + tmp23
    tmp62 = tmp61 + tmp50
    tmp63 = tmp60 / tmp62
    tmp64 = tmp55 - tmp63
    tmp65 = tmp58 + tmp64
    tmp66 = tmp41 * tmp48
    tmp67 = tmp66 + tmp50
    tmp68 = tmp44 + tmp47
    tmp69 = tmp68 + tmp50
    tmp70 = tmp67 / tmp69
    tmp71 = tmp55 - tmp70
    tmp72 = tmp65 + tmp71
    tmp73 = tmp29 * tmp48
    tmp74 = tmp73 + tmp50
    tmp75 = tmp32 + tmp35
    tmp76 = tmp75 + tmp50
    tmp77 = tmp74 / tmp76
    tmp78 = tmp55 - tmp77
    tmp79 = tmp72 + tmp78
    tmp80 = 0.25
    tmp81 = tmp79 * tmp80
    tl.debug_barrier()
    tl.store(in_out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp81, 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)
        buf10 = empty_strided_cuda((), (), torch.float32)
        buf13 = buf10
        del buf10
        get_raw_stream(0)
        triton_per_fused_add_div_mul_rsub_sum_0[grid(1)](buf13, arg1_1,
            arg0_1, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
    return buf13,


class DiceLossNew(nn.Module):
    """DICE loss.
    """

    def __init__(self, reduce=True, smooth=100.0, power=1):
        super(DiceLossNew, self).__init__()
        self.smooth = smooth
        self.reduce = reduce
        self.power = power

    def dice_loss(self, pred, target):
        loss = 0.0
        for index in range(pred.size()[0]):
            iflat = pred[index].contiguous().view(-1)
            tflat = target[index].contiguous().view(-1)
            intersection = (iflat * tflat).sum()
            if self.power == 1:
                loss += 1 - (2.0 * intersection + self.smooth) / (iflat.sum
                    () + tflat.sum() + self.smooth)
            else:
                loss += 1 - (2.0 * intersection + self.smooth) / ((iflat **
                    self.power).sum() + (tflat ** self.power).sum() + self.
                    smooth)
        return loss / float(pred.size()[0])

    def dice_loss_batch(self, pred, target):
        iflat = pred.view(-1)
        tflat = target.view(-1)
        intersection = (iflat * tflat).sum()
        if self.power == 1:
            loss = 1 - (2.0 * intersection + self.smooth) / (iflat.sum() +
                tflat.sum() + self.smooth)
        else:
            loss = 1 - (2.0 * intersection + self.smooth) / ((iflat ** self
                .power).sum() + (tflat ** self.power).sum() + self.smooth)
        return loss

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