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
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_add_mul_sub_sum_1(in_out_ptr0, in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8, in_ptr9,
    in_ptr10, 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)
    r3 = rindex
    r0 = rindex % 16
    r2 = rindex // 64
    r4 = rindex // 16
    tmp0 = tl.load(in_ptr0 + r3, None)
    tmp1 = tl.load(in_ptr1 + (r0 + 64 * r2), None, eviction_policy='evict_last'
        )
    tmp2 = tl.load(in_ptr2 + r4, None, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr3 + r4, None, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr4 + r3, None)
    tmp13 = tl.load(in_ptr1 + (16 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp14 = tl.load(in_ptr5 + r4, None, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr6 + r4, None, eviction_policy='evict_last')
    tmp24 = tl.load(in_ptr1 + (32 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp25 = tl.load(in_ptr7 + r4, None, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr8 + r4, None, eviction_policy='evict_last')
    tmp35 = tl.load(in_ptr1 + (48 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp36 = tl.load(in_ptr9 + r4, None, eviction_policy='evict_last')
    tmp37 = tl.load(in_ptr10 + r4, None, eviction_policy='evict_last')
    tmp4 = tmp2 / tmp3
    tmp5 = tmp1 - tmp4
    tmp6 = tmp0 * tmp5
    tmp7 = tmp6 * tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tl.broadcast_to(tmp9, [RBLOCK])
    tmp12 = triton_helpers.promote_to_tensor(tl.sum(tmp10, 0))
    tmp16 = tmp14 / tmp15
    tmp17 = tmp13 - tmp16
    tmp18 = tmp0 * tmp17
    tmp19 = tmp18 * tmp17
    tmp20 = tmp19 * tmp8
    tmp21 = tl.broadcast_to(tmp20, [RBLOCK])
    tmp23 = triton_helpers.promote_to_tensor(tl.sum(tmp21, 0))
    tmp27 = tmp25 / tmp26
    tmp28 = tmp24 - tmp27
    tmp29 = tmp0 * tmp28
    tmp30 = tmp29 * tmp28
    tmp31 = tmp30 * tmp8
    tmp32 = tl.broadcast_to(tmp31, [RBLOCK])
    tmp34 = triton_helpers.promote_to_tensor(tl.sum(tmp32, 0))
    tmp38 = tmp36 / tmp37
    tmp39 = tmp35 - tmp38
    tmp40 = tmp0 * tmp39
    tmp41 = tmp40 * tmp39
    tmp42 = tmp41 * tmp8
    tmp43 = tl.broadcast_to(tmp42, [RBLOCK])
    tmp45 = triton_helpers.promote_to_tensor(tl.sum(tmp43, 0))
    tmp46 = 0.0
    tmp47 = tmp12 + tmp46
    tmp48 = tmp47 + tmp23
    tmp49 = tmp48 + tmp34
    tmp50 = tmp49 + tmp45
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp50, None)


def call(args):
    arg0_1, arg1_1, arg2_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))
    assert_size_stride(arg2_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)
        buf11 = empty_strided_cuda((), (), torch.float32)
        buf12 = buf11
        del buf11
        triton_per_fused_add_mul_sub_sum_1[grid(1)](buf12, arg2_1, arg1_1,
            buf0, buf1, arg0_1, buf3, buf4, buf6, buf7, buf9, buf10, 1, 256,
            num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
        del buf0
        del buf1
        del buf10
        del buf3
        del buf4
        del buf6
        del buf7
        del buf9
    return buf12,


class region_levelsetNew(nn.Module):
    """
    the mian of leveset function
    """

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

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


Measured latency_ms: 0.041902