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.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_mean_mul_var_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    out_ptr2, out_ptr3, out_ptr4, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 64
    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)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 64 * x0), xmask, other=0.0)
    tmp1 = tl.load(in_ptr1 + (r1 + 64 * x0), xmask, 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(tmp0, [XBLOCK, RBLOCK])
    tmp9 = tl.where(xmask, tmp7, 0)
    tmp10 = tl.sum(tmp9, 1)[:, None]
    tmp12 = tl.broadcast_to(tmp7, [XBLOCK, RBLOCK])
    tmp14 = tl.where(xmask, tmp12, 0)
    tmp15 = tl.sum(tmp14, 1)[:, None]
    tmp16 = tl.full([XBLOCK, 1], 64, tl.int32)
    tmp17 = tmp16.to(tl.float32)
    tmp18 = tmp15 / tmp17
    tmp19 = tmp7 - tmp18
    tmp20 = tmp19 * tmp19
    tmp21 = tl.broadcast_to(tmp20, [XBLOCK, RBLOCK])
    tmp23 = tl.where(xmask, tmp21, 0)
    tmp24 = tl.sum(tmp23, 1)[:, None]
    tmp25 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp27 = tl.where(xmask, tmp25, 0)
    tmp28 = tl.sum(tmp27, 1)[:, None]
    tmp30 = tl.broadcast_to(tmp25, [XBLOCK, RBLOCK])
    tmp32 = tl.where(xmask, tmp30, 0)
    tmp33 = tl.sum(tmp32, 1)[:, None]
    tmp34 = tmp33 / tmp17
    tmp35 = tmp25 - tmp34
    tmp36 = tmp35 * tmp35
    tmp37 = tl.broadcast_to(tmp36, [XBLOCK, RBLOCK])
    tmp39 = tl.where(xmask, tmp37, 0)
    tmp40 = tl.sum(tmp39, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp6, xmask)
    tl.store(out_ptr1 + x0, tmp10, xmask)
    tl.store(out_ptr2 + x0, tmp24, xmask)
    tl.store(out_ptr3 + x0, tmp28, xmask)
    tl.store(out_ptr4 + x0, tmp40, xmask)


@triton.jit
def triton_per_fused_add_div_mean_mul_pow_sub_var_1(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, in_ptr4, 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)
    tmp3 = tl.load(in_ptr1 + r0, None)
    tmp5 = tl.load(in_ptr2 + r0, None)
    tmp13 = tl.load(in_ptr3 + r0, None)
    tmp16 = tl.load(in_ptr4 + r0, None)
    tmp1 = 64.0
    tmp2 = tmp0 / tmp1
    tmp4 = tmp3 / tmp1
    tmp6 = tmp5 / tmp1
    tmp7 = tmp4 * tmp6
    tmp8 = tmp2 - tmp7
    tmp9 = 4.0
    tmp10 = tmp8 * tmp9
    tmp11 = tmp10 * tmp4
    tmp12 = tmp11 * tmp6
    tmp14 = 63.0
    tmp15 = tmp13 / tmp14
    tmp17 = tmp16 / tmp14
    tmp18 = tmp15 + tmp17
    tmp19 = tmp4 * tmp4
    tmp20 = tmp6 * tmp6
    tmp21 = tmp19 + tmp20
    tmp22 = tmp18 * tmp21
    tmp23 = tmp12 / tmp22
    tmp24 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK])
    tmp26 = tl.sum(tmp24, 1)[:, None]
    tmp27 = tmp26 / tmp9
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp27, 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,), (1,), torch.float32)
        buf1 = empty_strided_cuda((4,), (1,), torch.float32)
        buf4 = empty_strided_cuda((4,), (1,), torch.float32)
        buf2 = empty_strided_cuda((4,), (1,), torch.float32)
        buf7 = empty_strided_cuda((4,), (1,), torch.float32)
        get_raw_stream(0)
        triton_per_fused_mean_mul_var_0[grid(4)](arg0_1, arg1_1, buf0, buf1,
            buf4, buf2, buf7, 4, 64, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        buf9 = empty_strided_cuda((), (), torch.float32)
        buf10 = buf9
        del buf9
        triton_per_fused_add_div_mean_mul_pow_sub_var_1[grid(1)](buf10,
            buf0, buf1, buf2, buf4, buf7, 1, 4, XBLOCK=1, num_warps=2,
            num_stages=1)
        del buf0
        del buf1
        del buf2
        del buf4
        del buf7
    return buf10,


class Q_IndexNew(nn.Module):
    """
    Quality measurement between perturbated (image with applied noise) and denoised target image.
    This module works only for images with a single color channel (grayscale)
    """

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

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