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


@triton.jit
def triton_per_fused_abs_mean_sub_0(in_ptr0, in_ptr1, out_ptr0, 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)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.load(in_ptr1 + r0, None)
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.abs(tmp2)
    tmp4 = tl.broadcast_to(tmp3, [RBLOCK])
    tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0))
    tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp6, None)


@triton.jit
def triton_per_fused_abs_avg_pool2d_mean_sub_1(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 64
    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 % 2
    r1 = rindex // 2
    r2 = rindex
    tmp0 = tl.load(in_ptr0 + (2 * r0 + 8 * r1), None, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr0 + (1 + 2 * r0 + 8 * r1), None, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr0 + (4 + 2 * r0 + 8 * r1), None, eviction_policy=
        'evict_last')
    tmp5 = tl.load(in_ptr0 + (5 + 2 * r0 + 8 * r1), None, eviction_policy=
        'evict_last')
    tmp9 = tl.load(in_ptr1 + (2 * r0 + 8 * r1), None, eviction_policy=
        'evict_last')
    tmp10 = tl.load(in_ptr1 + (1 + 2 * r0 + 8 * r1), None, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr1 + (4 + 2 * r0 + 8 * r1), None, eviction_policy=
        'evict_last')
    tmp14 = tl.load(in_ptr1 + (5 + 2 * r0 + 8 * r1), None, eviction_policy=
        'evict_last')
    tmp2 = tmp1 + tmp0
    tmp4 = tmp3 + tmp2
    tmp6 = tmp5 + tmp4
    tmp7 = 0.25
    tmp8 = tmp6 * tmp7
    tmp11 = tmp10 + tmp9
    tmp13 = tmp12 + tmp11
    tmp15 = tmp14 + tmp13
    tmp16 = tmp15 * tmp7
    tmp17 = tmp8 - tmp16
    tmp18 = tl_math.abs(tmp17)
    tmp19 = tl.broadcast_to(tmp18, [XBLOCK, RBLOCK])
    tmp21 = tl.sum(tmp19, 1)[:, None]
    tl.store(out_ptr0 + tl.broadcast_to(r2, [XBLOCK, RBLOCK]), tmp8, None)
    tl.store(out_ptr1 + tl.broadcast_to(r2, [XBLOCK, RBLOCK]), tmp16, None)
    tl.store(out_ptr2 + tl.full([XBLOCK, 1], 0, tl.int32), tmp21, None)


@triton.jit
def triton_per_fused_abs_add_avg_pool2d_mean_mul_sub_2(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 16
    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 + 4 * r0, None, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + 4 * r0, None, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr1 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr1 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr1 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp22 = tl.load(in_out_ptr0 + 0)
    tmp23 = tl.broadcast_to(tmp22, [XBLOCK, 1])
    tmp30 = tl.load(in_ptr2 + 0)
    tmp31 = tl.broadcast_to(tmp30, [XBLOCK, 1])
    tmp2 = tmp1 + tmp0
    tmp4 = tmp3 + tmp2
    tmp6 = tmp5 + tmp4
    tmp7 = 0.25
    tmp8 = tmp6 * tmp7
    tmp11 = tmp10 + tmp9
    tmp13 = tmp12 + tmp11
    tmp15 = tmp14 + tmp13
    tmp16 = tmp15 * tmp7
    tmp17 = tmp8 - tmp16
    tmp18 = tl_math.abs(tmp17)
    tmp19 = tl.broadcast_to(tmp18, [XBLOCK, RBLOCK])
    tmp21 = tl.sum(tmp19, 1)[:, None]
    tmp24 = 256.0
    tmp25 = tmp23 / tmp24
    tmp26 = 1.0
    tmp27 = tmp25 * tmp26
    tmp28 = 0.0
    tmp29 = tmp27 + tmp28
    tmp32 = 64.0
    tmp33 = tmp31 / tmp32
    tmp34 = 0.5
    tmp35 = tmp33 * tmp34
    tmp36 = tmp29 + tmp35
    tmp37 = 16.0
    tmp38 = tmp21 / tmp37
    tmp39 = tmp38 * tmp7
    tmp40 = tmp36 + tmp39
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp40, 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((), (), torch.float32)
        get_raw_stream(0)
        triton_per_fused_abs_mean_sub_0[grid(1)](arg0_1, arg1_1, buf0, 1, 
            256, num_warps=2, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32)
        buf2 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32)
        buf3 = empty_strided_cuda((), (), torch.float32)
        triton_per_fused_abs_avg_pool2d_mean_sub_1[grid(1)](arg0_1, arg1_1,
            buf1, buf2, buf3, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        buf5 = buf0
        del buf0
        triton_per_fused_abs_add_avg_pool2d_mean_mul_sub_2[grid(1)](buf5,
            buf1, buf2, buf3, 1, 16, XBLOCK=1, num_warps=2, num_stages=1)
        del buf1
        del buf2
        del buf3
    return buf5,


class MultiscaleRecLossNew(nn.Module):

    def __init__(self, scale=3, rec_loss_type='l1', multiscale=True):
        super(MultiscaleRecLossNew, self).__init__()
        self.multiscale = multiscale
        if rec_loss_type == 'l1':
            self.criterion = nn.L1Loss()
        elif rec_loss_type == 'smoothl1':
            self.criterion = nn.SmoothL1Loss()
        elif rec_loss_type == 'l2':
            self.criterion = nn.MSELoss()
        else:
            raise NotImplementedError('Loss [{}] is not implemented'.format
                (rec_loss_type))
        self.downsample = nn.AvgPool2d(2, stride=2, count_include_pad=False)
        if self.multiscale:
            self.weights = [1.0, 1.0 / 2, 1.0 / 4]
            self.weights = self.weights[:scale]

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