import torch
from torch._inductor.select_algorithm import extern_kernels
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.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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@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 = 64
    rnumel = 121
    RBLOCK: tl.constexpr = 128
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    rmask = rindex < rnumel
    r5 = rindex
    x4 = xindex
    r3 = rindex // 11
    x0 = xindex % 16
    r2 = rindex % 11
    x1 = xindex // 16
    tmp0 = tl.load(in_ptr0 + (r5 + 121 * x4), rmask & xmask, other=0.0)
    tmp1 = -5 + r3 + x0 // 4
    tmp2 = tl.full([1, 1], 0, tl.int64)
    tmp3 = tmp1 >= tmp2
    tmp4 = tl.full([1, 1], 4, tl.int64)
    tmp5 = tmp1 < tmp4
    tmp6 = -5 + r2 + x0 % 4
    tmp7 = tmp6 >= tmp2
    tmp8 = tmp6 < tmp4
    tmp9 = tmp3 & tmp5
    tmp10 = tmp9 & tmp7
    tmp11 = tmp10 & tmp8
    tmp12 = tl.load(in_ptr1 + (-25 + r2 + x0 + 4 * r3 + 64 * x1), rmask &
        tmp11 & xmask, other=0.0)
    tmp13 = tmp0 * tmp12
    tmp14 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK])
    tmp16 = tl.where(rmask & xmask, tmp14, 0)
    tmp17 = tl.sum(tmp16, 1)[:, None]
    tmp18 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp20 = tl.where(rmask & xmask, tmp18, 0)
    tmp21 = tl.sum(tmp20, 1)[:, None]
    tmp22 = tl.load(in_ptr1 + (-9 + r2 + x0 + 4 * r3 + 64 * x1), rmask &
        tmp11 & xmask, other=0.0)
    tmp23 = tmp0 * tmp22
    tmp24 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK])
    tmp26 = tl.where(rmask & xmask, tmp24, 0)
    tmp27 = tl.sum(tmp26, 1)[:, None]
    tmp28 = tl.load(in_ptr1 + (7 + r2 + x0 + 4 * r3 + 64 * x1), rmask &
        tmp11 & xmask, other=0.0)
    tmp29 = tmp0 * tmp28
    tmp30 = tl.broadcast_to(tmp29, [XBLOCK, RBLOCK])
    tmp32 = tl.where(rmask & xmask, tmp30, 0)
    tmp33 = tl.sum(tmp32, 1)[:, None]
    tmp34 = tl.load(in_ptr1 + (23 + r2 + x0 + 4 * r3 + 64 * x1), rmask &
        tmp11 & xmask, other=0.0)
    tmp35 = tmp0 * tmp34
    tmp36 = tl.broadcast_to(tmp35, [XBLOCK, RBLOCK])
    tmp38 = tl.where(rmask & xmask, tmp36, 0)
    tmp39 = tl.sum(tmp38, 1)[:, None]
    tl.store(out_ptr0 + x4, tmp17, xmask)
    tl.store(out_ptr1 + x4, tmp21, xmask)
    tl.store(out_ptr2 + x4, tmp27, xmask)
    tl.store(out_ptr3 + x4, tmp21, xmask)
    tl.store(out_ptr4 + x4, tmp33, xmask)
    tl.store(out_ptr5 + x4, tmp21, xmask)
    tl.store(out_ptr6 + x4, tmp39, xmask)
    tl.store(out_ptr7 + x4, tmp21, xmask)


@triton.jit
def triton_per_fused_abs_add_mean_rsub_sub_1(in_out_ptr0, in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 4
    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)
    tmp7 = tl.load(in_ptr2 + r0, None)
    tmp8 = tl.load(in_ptr3 + r0, None)
    tmp14 = tl.load(in_ptr4 + r0, None)
    tmp15 = tl.load(in_ptr5 + r0, None)
    tmp21 = tl.load(in_ptr6 + r0, None)
    tmp22 = tl.load(in_ptr7 + r0, None)
    tmp2 = tmp0 / tmp1
    tmp3 = tl_math.abs(tmp2)
    tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
    tmp6 = tl.sum(tmp4, 1)[:, None]
    tmp9 = tmp7 / tmp8
    tmp10 = tl_math.abs(tmp9)
    tmp11 = tl.broadcast_to(tmp10, [XBLOCK, RBLOCK])
    tmp13 = tl.sum(tmp11, 1)[:, None]
    tmp16 = tmp14 / tmp15
    tmp17 = tl_math.abs(tmp16)
    tmp18 = tl.broadcast_to(tmp17, [XBLOCK, RBLOCK])
    tmp20 = tl.sum(tmp18, 1)[:, None]
    tmp23 = tmp21 / tmp22
    tmp24 = tl_math.abs(tmp23)
    tmp25 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK])
    tmp27 = tl.sum(tmp25, 1)[:, None]
    tmp28 = 4.0
    tmp29 = tmp6 / tmp28
    tmp30 = 0.0
    tmp31 = tmp29 + tmp30
    tmp32 = tmp13 / tmp28
    tmp33 = tmp31 + tmp32
    tmp34 = tmp20 / tmp28
    tmp35 = tmp33 + tmp34
    tmp36 = tmp27 / tmp28
    tmp37 = tmp35 + tmp36
    tmp38 = tmp28 - tmp37
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp38, 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, 16, 11, 11), (1936, 121, 11, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
        buf2 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
        buf5 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
        buf7 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
        buf10 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
        buf12 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
        buf15 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
        buf17 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_mul_sum_0[grid(64)](arg1_1, arg0_1, buf0, buf2,
            buf5, buf7, buf10, buf12, buf15, buf17, 64, 121, XBLOCK=1,
            num_warps=2, num_stages=1)
        del arg1_1
        buf1 = empty_strided_cuda((4, 1, 1), (1, 1, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(arg0_1, (4, 1, 16), (64, 0, 1
            ), 0), reinterpret_tensor(buf0, (4, 16, 1), (16, 1, 1), 0), out
            =buf1)
        del buf0
        buf3 = empty_strided_cuda((4, 1, 1), (1, 1, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(arg0_1, (4, 1, 16), (64, 0, 1
            ), 0), reinterpret_tensor(buf2, (4, 16, 1), (16, 1, 1), 0), out
            =buf3)
        del buf2
        buf11 = empty_strided_cuda((4, 1, 1), (1, 1, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(arg0_1, (4, 1, 16), (64, 0, 1
            ), 32), reinterpret_tensor(buf10, (4, 16, 1), (16, 1, 1), 0),
            out=buf11)
        del buf10
        buf13 = empty_strided_cuda((4, 1, 1), (1, 1, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(arg0_1, (4, 1, 16), (64, 0, 1
            ), 32), reinterpret_tensor(buf12, (4, 16, 1), (16, 1, 1), 0),
            out=buf13)
        del buf12
        buf16 = empty_strided_cuda((4, 1, 1), (1, 1, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(arg0_1, (4, 1, 16), (64, 0, 1
            ), 48), reinterpret_tensor(buf15, (4, 16, 1), (16, 1, 1), 0),
            out=buf16)
        del buf15
        buf18 = empty_strided_cuda((4, 1, 1), (1, 1, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(arg0_1, (4, 1, 16), (64, 0, 1
            ), 48), reinterpret_tensor(buf17, (4, 16, 1), (16, 1, 1), 0),
            out=buf18)
        del buf17
        buf6 = empty_strided_cuda((4, 1, 1), (1, 1, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(arg0_1, (4, 1, 16), (64, 0, 1
            ), 16), reinterpret_tensor(buf5, (4, 16, 1), (16, 1, 1), 0),
            out=buf6)
        del buf5
        buf8 = empty_strided_cuda((4, 1, 1), (1, 1, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(arg0_1, (4, 1, 16), (64, 0, 1
            ), 16), reinterpret_tensor(buf7, (4, 16, 1), (16, 1, 1), 0),
            out=buf8)
        del arg0_1
        del buf7
        buf14 = empty_strided_cuda((), (), torch.float32)
        buf20 = buf14
        del buf14
        triton_per_fused_abs_add_mean_rsub_sub_1[grid(1)](buf20, buf1, buf3,
            buf6, buf8, buf11, buf13, buf16, buf18, 1, 4, XBLOCK=1,
            num_warps=2, num_stages=1)
        del buf1
        del buf11
        del buf13
        del buf16
        del buf18
        del buf3
        del buf6
        del buf8
    return buf20,


class NCutLossOptimizedNew(nn.Module):
    """Implementation of the continuous N-Cut loss, as in:
    'W-Net: A Deep Model for Fully Unsupervised Image Segmentation', by Xia, Kulis (2017)"""

    def __init__(self, radius: 'int'=5):
        """
        :param radius: Radius of the spatial interaction term
        """
        super(NCutLossOptimizedNew, self).__init__()
        self.radius = radius

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