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
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_abs_add_clamp_min_div_mul_rsub_sqrt_sub_0(in_out_ptr0,
    in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x0 = xindex
    tmp0 = tl.load(in_out_ptr0 + x0, None)
    tmp2 = tl.load(in_ptr0 + x0, None)
    tmp9 = tl.load(in_ptr1 + x0, None)
    tmp11 = tl.load(in_ptr2 + x0, None)
    tmp1 = tmp0 * tmp0
    tmp3 = tmp2 * tmp2
    tmp4 = tmp1 + tmp3
    tmp5 = 1e-06
    tmp6 = tmp4 + tmp5
    tmp7 = libdevice.sqrt(tmp6)
    tmp8 = tmp0 / tmp7
    tmp10 = tmp9 * tmp9
    tmp12 = tmp11 * tmp11
    tmp13 = tmp10 + tmp12
    tmp14 = tmp13 + tmp5
    tmp15 = libdevice.sqrt(tmp14)
    tmp16 = tmp9 / tmp15
    tmp17 = tmp8 * tmp16
    tmp18 = tmp2 / tmp7
    tmp19 = tmp11 / tmp15
    tmp20 = tmp18 * tmp19
    tmp21 = tmp17 + tmp20
    tmp22 = tl_math.abs(tmp21)
    tmp23 = 1.0
    tmp24 = tmp23 - tmp22
    tmp25 = tmp24 * tmp15
    tmp26 = 0.5
    tmp27 = tmp25 * tmp26
    tmp28 = 1.5
    tmp29 = tmp7 * tmp28
    tmp30 = tmp29 - tmp15
    tmp31 = 0.0
    tmp32 = triton_helpers.maximum(tmp30, tmp31)
    tmp33 = tmp32 * tmp26
    tmp34 = tmp27 + tmp33
    tl.store(in_out_ptr0 + x0, tmp34, None)


@triton.jit
def triton_red_fused_mean_mul_1(in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel,
    XBLOCK: tl.constexpr, RBLOCK: tl.constexpr):
    xnumel = 8
    rnumel = 8192
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rbase = tl.arange(0, RBLOCK)[None, :]
    x0 = xindex
    _tmp4 = 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 + (4096 * ((r1 + 8192 * x0) // 16384) + r1 %
            4096), rmask & xmask, eviction_policy='evict_last', other=0.0)
        tmp1 = tl.load(in_ptr1 + (r1 + 8192 * x0), rmask & xmask,
            eviction_policy='evict_first', other=0.0)
        tmp2 = tmp0 * tmp1
        tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
        tmp5 = _tmp4 + tmp3
        _tmp4 = tl.where(rmask & xmask, tmp5, _tmp4)
    tmp4 = tl.sum(_tmp4, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp4, xmask)


@triton.jit
def triton_per_fused_mean_mul_2(in_out_ptr0, in_ptr0, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 8
    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 = 65536.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, arg3_1, arg4_1 = args
    args.clear()
    assert_size_stride(arg0_1, (1, 1, 3, 3), (9, 9, 3, 1))
    assert_size_stride(arg1_1, (4, 1, 64, 64), (4096, 4096, 64, 1))
    assert_size_stride(arg2_1, (1, 1, 3, 3), (9, 9, 3, 1))
    assert_size_stride(arg3_1, (4, 1, 64, 64), (4096, 4096, 64, 1))
    assert_size_stride(arg4_1, (4, 4, 64, 64), (16384, 4096, 64, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(arg1_1, arg0_1, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 1, 64, 64), (4096, 4096, 64, 1))
        buf1 = extern_kernels.convolution(arg1_1, arg2_1, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf1, (4, 1, 64, 64), (4096, 4096, 64, 1))
        del arg1_1
        buf2 = extern_kernels.convolution(arg3_1, arg0_1, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf2, (4, 1, 64, 64), (4096, 4096, 64, 1))
        del arg0_1
        buf3 = extern_kernels.convolution(arg3_1, arg2_1, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf3, (4, 1, 64, 64), (4096, 4096, 64, 1))
        del arg2_1
        del arg3_1
        buf4 = reinterpret_tensor(buf0, (4, 1, 64, 64), (4096, 16384, 64, 1), 0
            )
        del buf0
        get_raw_stream(0)
        triton_poi_fused_abs_add_clamp_min_div_mul_rsub_sqrt_sub_0[grid(16384)
            ](buf4, buf1, buf2, buf3, 16384, XBLOCK=256, num_warps=4,
            num_stages=1)
        del buf1
        del buf2
        del buf3
        buf5 = empty_strided_cuda((8,), (1,), torch.float32)
        triton_red_fused_mean_mul_1[grid(8)](buf4, arg4_1, buf5, 8, 8192,
            XBLOCK=1, RBLOCK=2048, num_warps=16, num_stages=1)
        del arg4_1
        del buf4
        buf6 = empty_strided_cuda((), (), torch.float32)
        buf7 = buf6
        del buf6
        triton_per_fused_mean_mul_2[grid(1)](buf7, buf5, 1, 8, XBLOCK=1,
            num_warps=2, num_stages=1)
        del buf5
    return buf7,


class RefineLossNew(nn.Module):

    def __init__(self, alpha=1.5, alpha1=0.5, reduction='mean'):
        super(RefineLossNew, self).__init__()
        self.alpha = alpha
        self.alpha1 = alpha1
        self.reduction = reduction
        self.fx = nn.Conv2d(1, 1, 3, padding=1, bias=False)
        self.fy = nn.Conv2d(1, 1, 3, padding=1, bias=False)
        ngx = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]], dtype=np.float32)
        ngy = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], dtype=np.float32)
        self.fx.weight.data.copy_(torch.from_numpy(ngx))
        self.fy.weight.data.copy_(torch.from_numpy(ngy))
        for param in self.fx.parameters():
            param.requires_grad = False
        for param in self.fy.parameters():
            param.requires_grad = False

    def forward(self, input_0, input_1, input_2):
        arg0_1 = self.fx.weight
        arg2_1 = self.fy.weight
        arg1_1 = input_0
        arg3_1 = input_1
        arg4_1 = input_2
        output = call([arg0_1, arg1_1, arg2_1, arg3_1, arg4_1])
        return output[0]


Measured latency_ms: 0.07536