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
import numpy as np
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_poi_fused_mean_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x0 = xindex % 4096
    x1 = xindex // 4096
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16384 * x1), None)
    tmp1 = tl.load(in_ptr0 + (4096 + x0 + 16384 * x1), None)
    tmp3 = tl.load(in_ptr0 + (8192 + x0 + 16384 * x1), None)
    tmp5 = tl.load(in_ptr0 + (12288 + x0 + 16384 * x1), None)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = 4.0
    tmp8 = tmp6 / tmp7
    tl.store(out_ptr0 + x2, tmp8, None)


@triton.jit
def triton_red_fused_mse_loss_1(in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel,
    XBLOCK: tl.constexpr, RBLOCK: tl.constexpr):
    xnumel = 2
    rnumel = 7200
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rbase = tl.arange(0, RBLOCK)[None, :]
    x0 = xindex
    _tmp5 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp0 = tl.load(in_ptr0 + (r1 + 7200 * x0), rmask & xmask,
            eviction_policy='evict_first', other=0.0)
        tmp1 = tl.load(in_ptr1 + (r1 + 7200 * x0), rmask & xmask,
            eviction_policy='evict_first', other=0.0)
        tmp2 = tmp0 - tmp1
        tmp3 = tmp2 * tmp2
        tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
        tmp6 = _tmp5 + tmp4
        _tmp5 = tl.where(rmask & xmask, tmp6, _tmp5)
    tmp5 = tl.sum(_tmp5, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp5, xmask)


@triton.jit
def triton_per_fused_mse_loss_2(in_out_ptr0, in_ptr0, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 2
    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.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp4 = 14400.0
    tmp5 = tmp3 / tmp4
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp5, None)


def call(args):
    arg0_1, arg1_1, arg2_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 64, 64), (16384, 4096, 64, 1))
    assert_size_stride(arg1_1, (1, 1, 5, 5), (25, 25, 5, 1))
    assert_size_stride(arg2_1, (1, 1, 5, 5), (25, 25, 5, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 1, 64, 64), (4096, 4096, 64, 1),
            torch.float32)
        get_raw_stream(0)
        triton_poi_fused_mean_0[grid(16384)](arg0_1, buf0, 16384, XBLOCK=
            256, num_warps=4, num_stages=1)
        del arg0_1
        buf1 = extern_kernels.convolution(buf0, arg1_1, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf1, (4, 1, 60, 60), (3600, 3600, 60, 1))
        del arg1_1
        buf2 = extern_kernels.convolution(buf0, arg2_1, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf2, (4, 1, 60, 60), (3600, 3600, 60, 1))
        del arg2_1
        del buf0
        buf3 = empty_strided_cuda((2,), (1,), torch.float32)
        triton_red_fused_mse_loss_1[grid(2)](buf1, buf2, buf3, 2, 7200,
            XBLOCK=1, RBLOCK=2048, num_warps=16, num_stages=1)
        del buf1
        del buf2
        buf4 = empty_strided_cuda((), (), torch.float32)
        buf5 = buf4
        del buf4
        triton_per_fused_mse_loss_2[grid(1)](buf5, buf3, 1, 2, XBLOCK=1,
            num_warps=2, num_stages=1)
        del buf3
    return buf5,


class GrayscaleLayer(nn.Module):

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

    def forward(self, x):
        return torch.mean(x, 1, keepdim=True)


class StdLossNew(nn.Module):

    def __init__(self):
        """
        Loss on the variance of the image.
        Works in the grayscale.
        If the image is smooth, gets zero
        """
        super(StdLossNew, self).__init__()
        blur = 1 / 25 * np.ones((5, 5))
        blur = blur.reshape(1, 1, blur.shape[0], blur.shape[1])
        self.mse = nn.MSELoss()
        self.blur = nn.Parameter(data=torch.FloatTensor(blur),
            requires_grad=False)
        image = np.zeros((5, 5))
        image[2, 2] = 1
        image = image.reshape(1, 1, image.shape[0], image.shape[1])
        self.image = nn.Parameter(data=torch.FloatTensor(image),
            requires_grad=False)
        self.gray_scale = GrayscaleLayer()

    def forward(self, input_0):
        arg1_1 = self.blur
        arg2_1 = self.image
        arg0_1 = input_0
        output = call([arg0_1, arg1_1, arg2_1])
        return output[0]


Measured latency_ms: 0.014211