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_mul_sum_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    out_ptr2, out_ptr3, out_ptr4, out_ptr5, out_ptr6, out_ptr7, xnumel,
    rnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r2 = rindex
    x1 = xindex // 4
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (r2 + 64 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp1 = tl.load(in_ptr1 + (r2 + 16 * x3), xmask, other=0.0)
    tmp11 = tl.load(in_ptr0 + (16 + r2 + 64 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp17 = tl.load(in_ptr0 + (32 + r2 + 64 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp23 = tl.load(in_ptr0 + (48 + r2 + 64 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp2 = tmp0 * tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.where(xmask, tmp3, 0)
    tmp6 = tl.sum(tmp5, 1)[:, None]
    tmp7 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp9 = tl.where(xmask, tmp7, 0)
    tmp10 = tl.sum(tmp9, 1)[:, None]
    tmp12 = tmp11 * tmp1
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
    tmp15 = tl.where(xmask, tmp13, 0)
    tmp16 = tl.sum(tmp15, 1)[:, None]
    tmp18 = tmp17 * tmp1
    tmp19 = tl.broadcast_to(tmp18, [XBLOCK, RBLOCK])
    tmp21 = tl.where(xmask, tmp19, 0)
    tmp22 = tl.sum(tmp21, 1)[:, None]
    tmp24 = tmp23 * tmp1
    tmp25 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK])
    tmp27 = tl.where(xmask, tmp25, 0)
    tmp28 = tl.sum(tmp27, 1)[:, None]
    tl.store(out_ptr0 + x3, tmp6, xmask)
    tl.store(out_ptr1 + x3, tmp10, xmask)
    tl.store(out_ptr2 + x3, tmp16, xmask)
    tl.store(out_ptr3 + x3, tmp10, xmask)
    tl.store(out_ptr4 + x3, tmp22, xmask)
    tl.store(out_ptr5 + x3, tmp10, xmask)
    tl.store(out_ptr6 + x3, tmp28, xmask)
    tl.store(out_ptr7 + x3, tmp10, xmask)


@triton.jit
def triton_per_fused_mul_sub_sum_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8, in_ptr9, out_ptr0,
    out_ptr1, out_ptr2, out_ptr3, 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 % 16
    r2 = rindex // 64
    r3 = rindex // 16
    r4 = rindex
    tmp0 = tl.load(in_ptr0 + (r0 + 64 * r2), None, eviction_policy='evict_last'
        )
    tmp1 = tl.load(in_ptr1 + r3, None, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + r3, None, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr3 + r4, None)
    tmp11 = tl.load(in_ptr0 + (16 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr4 + r3, None, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr5 + r3, None, eviction_policy='evict_last')
    tmp21 = tl.load(in_ptr0 + (32 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp22 = tl.load(in_ptr6 + r3, None, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr7 + r3, None, eviction_policy='evict_last')
    tmp31 = tl.load(in_ptr0 + (48 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp32 = tl.load(in_ptr8 + r3, None, eviction_policy='evict_last')
    tmp33 = tl.load(in_ptr9 + r3, None, eviction_policy='evict_last')
    tmp3 = tmp1 / tmp2
    tmp4 = tmp0 - tmp3
    tmp5 = tmp4 * tmp4
    tmp7 = tmp5 * tmp6
    tmp8 = tl.broadcast_to(tmp7, [RBLOCK])
    tmp10 = triton_helpers.promote_to_tensor(tl.sum(tmp8, 0))
    tmp14 = tmp12 / tmp13
    tmp15 = tmp11 - tmp14
    tmp16 = tmp15 * tmp15
    tmp17 = tmp16 * tmp6
    tmp18 = tl.broadcast_to(tmp17, [RBLOCK])
    tmp20 = triton_helpers.promote_to_tensor(tl.sum(tmp18, 0))
    tmp24 = tmp22 / tmp23
    tmp25 = tmp21 - tmp24
    tmp26 = tmp25 * tmp25
    tmp27 = tmp26 * tmp6
    tmp28 = tl.broadcast_to(tmp27, [RBLOCK])
    tmp30 = triton_helpers.promote_to_tensor(tl.sum(tmp28, 0))
    tmp34 = tmp32 / tmp33
    tmp35 = tmp31 - tmp34
    tmp36 = tmp35 * tmp35
    tmp37 = tmp36 * tmp6
    tmp38 = tl.broadcast_to(tmp37, [RBLOCK])
    tmp40 = triton_helpers.promote_to_tensor(tl.sum(tmp38, 0))
    tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp10, None)
    tl.store(out_ptr1 + tl.full([1], 0, tl.int32), tmp20, None)
    tl.store(out_ptr2 + tl.full([1], 0, tl.int32), tmp30, None)
    tl.store(out_ptr3 + tl.full([1], 0, tl.int32), tmp40, None)


@triton.jit
def triton_per_fused_abs_add_sub_sum_2(in_out_ptr0, in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr):
    rnumel = 192
    RBLOCK: tl.constexpr = 256
    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, :]
    rmask = rindex < rnumel
    r0 = rindex % 12
    r1 = rindex // 12
    r2 = rindex % 3
    r3 = rindex // 3
    tmp0 = tl.load(in_ptr0 + (4 + r0 + 16 * r1), rmask, other=0.0)
    tmp1 = tl.load(in_ptr0 + (r0 + 16 * r1), rmask, other=0.0)
    tmp8 = tl.load(in_ptr0 + (1 + r2 + 4 * r3), rmask, other=0.0)
    tmp9 = tl.load(in_ptr0 + (r2 + 4 * r3), rmask, other=0.0)
    tmp16 = tl.load(in_ptr1 + 0)
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, 1])
    tmp20 = tl.load(in_ptr2 + 0)
    tmp21 = tl.broadcast_to(tmp20, [XBLOCK, 1])
    tmp23 = tl.load(in_ptr3 + 0)
    tmp24 = tl.broadcast_to(tmp23, [XBLOCK, 1])
    tmp26 = tl.load(in_out_ptr0 + 0)
    tmp27 = tl.broadcast_to(tmp26, [XBLOCK, 1])
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.abs(tmp2)
    tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
    tmp6 = tl.where(rmask, tmp4, 0)
    tmp7 = tl.sum(tmp6, 1)[:, None]
    tmp10 = tmp8 - tmp9
    tmp11 = tl_math.abs(tmp10)
    tmp12 = tl.broadcast_to(tmp11, [XBLOCK, RBLOCK])
    tmp14 = tl.where(rmask, tmp12, 0)
    tmp15 = tl.sum(tmp14, 1)[:, None]
    tmp18 = 0.0
    tmp19 = tmp17 + tmp18
    tmp22 = tmp19 + tmp21
    tmp25 = tmp22 + tmp24
    tmp28 = tmp25 + tmp27
    tmp29 = tmp7 + tmp15
    tmp30 = tmp28 + tmp29
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp30, 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((4, 4), (4, 1), torch.float32)
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf9 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf10 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_mul_sum_0[grid(16)](arg1_1, arg0_1, buf0, buf1,
            buf3, buf4, buf6, buf7, buf9, buf10, 16, 16, XBLOCK=1,
            num_warps=2, num_stages=1)
        buf2 = empty_strided_cuda((), (), torch.float32)
        buf5 = empty_strided_cuda((), (), torch.float32)
        buf8 = empty_strided_cuda((), (), torch.float32)
        buf11 = empty_strided_cuda((), (), torch.float32)
        triton_per_fused_mul_sub_sum_1[grid(1)](arg1_1, buf0, buf1, arg0_1,
            buf3, buf4, buf6, buf7, buf9, buf10, buf2, buf5, buf8, buf11, 1,
            256, num_warps=2, num_stages=1)
        del arg1_1
        del buf0
        del buf1
        del buf10
        del buf3
        del buf4
        del buf6
        del buf7
        del buf9
        buf14 = buf11
        del buf11
        triton_per_fused_abs_add_sub_sum_2[grid(1)](buf14, arg0_1, buf2,
            buf5, buf8, 1, 192, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del buf2
        del buf5
        del buf8
    return buf14,


class MumfordShah_LossNew(nn.Module):

    def levelsetLoss(self, output, target, penalty='l1'):
        outshape = output.shape
        tarshape = target.shape
        self.penalty = penalty
        loss = 0.0
        for ich in range(tarshape[1]):
            target_ = torch.unsqueeze(target[:, ich], 1)
            target_ = target_.expand(tarshape[0], outshape[1], tarshape[2],
                tarshape[3])
            pcentroid = torch.sum(target_ * output, (2, 3)) / torch.sum(output,
                (2, 3))
            pcentroid = pcentroid.view(tarshape[0], outshape[1], 1, 1)
            plevel = target_ - pcentroid.expand(tarshape[0], outshape[1],
                tarshape[2], tarshape[3])
            pLoss = plevel * plevel * output
            loss += torch.sum(pLoss)
        return loss

    def gradientLoss2d(self, input):
        dH = torch.abs(input[:, :, 1:, :] - input[:, :, :-1, :])
        dW = torch.abs(input[:, :, :, 1:] - input[:, :, :, :-1])
        if self.penalty == 'l2':
            dH = dH * dH
            dW = dW * dW
        loss = torch.sum(dH) + torch.sum(dW)
        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]


Measured latency_ms: 0.046868