import torch
import torch.nn as nn


class FrequencyLoss(nn.Module):
    """Charbonnier Loss (L1)"""

    def __init__(self, eps=0.001):
        super(FrequencyLoss, self).__init__()
        self.criterion = torch.nn.L1Loss()

    def forward(self, x, y):
        x_fft = torch.fft.rfft2(x, dim=(2, 3))
        y_fft = torch.fft.rfft2(y, dim=(2, 3))
        loss = self.criterion(x_fft, y_fft)
        return loss


def get_inputs():
    return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {}]

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_0(in_out_ptr0, in_ptr0, 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
    tmp0 = tl.load(in_ptr0 + r0, rmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(rmask, tmp1, 0)
    tmp4 = tl.sum(tmp3, 1)[:, None]
    tmp5 = 192.0
    tmp6 = tmp4 / tmp5
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp6, 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 = torch.ops.aten._fft_r2c.default(arg0_1, [2, 3], 0, True)
        del arg0_1
        buf1 = buf0
        del buf0
        buf2 = torch.ops.aten._fft_r2c.default(arg1_1, [2, 3], 0, True)
        del arg1_1
        buf3 = buf2
        del buf2
        buf4 = torch.ops.aten.sub.Tensor(buf1, buf3)
        del buf1
        del buf3
        buf5 = buf4
        del buf4
        buf6 = torch.ops.aten.abs.default(buf5)
        del buf5
        buf7 = buf6
        del buf6
        buf8 = empty_strided_cuda((), (), torch.float32)
        buf9 = buf8
        del buf8
        get_raw_stream(0)
        triton_per_fused_mean_0[grid(1)](buf9, buf7, 1, 192, XBLOCK=1,
            num_warps=2, num_stages=1)
        del buf7
    return buf9,


class FrequencyLossNew(nn.Module):
    """Charbonnier Loss (L1)"""

    def __init__(self, eps=0.001):
        super(FrequencyLossNew, self).__init__()
        self.criterion = torch.nn.L1Loss()

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