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 numpy as np
from torch import nn
import torch.utils.data
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_add_div_mul_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = xindex // 4
    x2 = xindex
    tmp7 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp23 = tl.load(in_ptr0 + x2, xmask)
    tmp0 = x0
    tmp1 = tl.full([1], 1, tl.int32)
    tmp2 = tmp0 == tmp1
    tmp3 = tmp1 == tmp1
    tmp4 = tl.full([1], 0, tl.int32)
    tmp5 = tmp1 == tmp4
    tmp6 = tmp4 == tmp4
    tmp8 = 1.0
    tmp9 = tmp7 + tmp8
    tmp10 = 0.5
    tmp11 = tmp9 * tmp10
    tmp12 = 4.0
    tmp13 = tmp11 * tmp12
    tmp14 = tl.where(tmp6, tmp13, tmp11)
    tmp16 = tmp15 + tmp8
    tmp17 = tmp16 * tmp10
    tmp18 = tl.where(tmp5, tmp13, tmp17)
    tmp19 = tl.where(tmp5, tmp14, tmp18)
    tmp20 = tmp19 * tmp12
    tmp21 = tl.where(tmp3, tmp20, tmp19)
    tmp22 = tmp0 == tmp4
    tmp24 = tmp23 + tmp8
    tmp25 = tmp24 * tmp10
    tmp26 = tl.where(tmp22, tmp13, tmp25)
    tmp27 = tl.where(tmp22, tmp14, tmp26)
    tmp28 = tl.where(tmp2, tmp20, tmp27)
    tmp29 = tl.where(tmp2, tmp21, tmp28)
    tmp30 = tmp29 + tmp8
    tl.store(out_ptr0 + x2, tmp30, xmask)


@triton.jit
def triton_poi_fused_view_1(out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 784
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = 0.0
    tl.store(out_ptr0 + x0, tmp0, xmask)


@triton.jit
def triton_poi_fused__to_copy_abs_add_index_add_mul_relu_rsub_sub_view_2(
    in_ptr0, in_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp100 = tl.load(in_ptr1 + x0, xmask)
    tmp0 = tl.full([1], 0, tl.int64)
    tmp2 = tl.full([1], 1, tl.int64)
    tmp3 = tmp0 < tmp2
    tmp4 = tl.load(in_ptr0 + (4 * (x0 % 16) + 64 * (x0 // 64)), tmp3 &
        xmask, eviction_policy='evict_last', other=0.0)
    tmp5 = 0.0
    tmp6 = triton_helpers.maximum(tmp4, tmp5)
    tmp7 = 4.9999999998
    tmp8 = triton_helpers.minimum(tmp6, tmp7)
    tmp9 = tl.full(tmp8.shape, 0.0, tmp8.dtype)
    tmp10 = tl.where(tmp3, tmp8, tmp9)
    tmp11 = tmp0 >= tmp2
    tmp12 = tl.full([1], 2, tl.int64)
    tmp13 = tmp0 < tmp12
    tmp14 = tl.load(in_ptr0 + (1 + 4 * (x0 % 16) + 64 * (x0 // 64)), tmp11 &
        xmask, eviction_policy='evict_last', other=0.0)
    tmp15 = triton_helpers.maximum(tmp14, tmp5)
    tmp16 = triton_helpers.minimum(tmp15, tmp7)
    tmp17 = tl.full(tmp16.shape, 0.0, tmp16.dtype)
    tmp18 = tl.where(tmp11, tmp16, tmp17)
    tmp19 = tl.where(tmp3, tmp10, tmp18)
    tmp20 = tmp19.to(tl.int64)
    tmp21 = tmp20 + tmp0
    tmp22 = tmp21.to(tl.float32)
    tmp23 = tmp19 - tmp22
    tmp24 = tl_math.abs(tmp23)
    tmp25 = 1.0
    tmp26 = tmp25 - tmp24
    tmp27 = tl.full([1], 0, tl.int32)
    tmp28 = triton_helpers.maximum(tmp27, tmp26)
    tmp30 = tmp2 < tmp2
    tmp31 = tl.load(in_ptr0 + (4 * (x0 % 16) + 64 * (x0 // 64)), tmp30 &
        xmask, eviction_policy='evict_last', other=0.0)
    tmp32 = triton_helpers.maximum(tmp31, tmp5)
    tmp33 = triton_helpers.minimum(tmp32, tmp7)
    tmp34 = tl.full(tmp33.shape, 0.0, tmp33.dtype)
    tmp35 = tl.where(tmp30, tmp33, tmp34)
    tmp36 = tmp2 >= tmp2
    tmp37 = tmp2 < tmp12
    tmp38 = tl.load(in_ptr0 + (1 + 4 * (x0 % 16) + 64 * (x0 // 64)), tmp36 &
        xmask, eviction_policy='evict_last', other=0.0)
    tmp39 = triton_helpers.maximum(tmp38, tmp5)
    tmp40 = triton_helpers.minimum(tmp39, tmp7)
    tmp41 = tl.full(tmp40.shape, 0.0, tmp40.dtype)
    tmp42 = tl.where(tmp36, tmp40, tmp41)
    tmp43 = tl.where(tmp30, tmp35, tmp42)
    tmp44 = tmp43.to(tl.int64)
    tmp45 = tmp44 + tmp0
    tmp46 = tmp45.to(tl.float32)
    tmp47 = tmp43 - tmp46
    tmp48 = tl_math.abs(tmp47)
    tmp49 = tmp25 - tmp48
    tmp50 = triton_helpers.maximum(tmp27, tmp49)
    tmp51 = tmp28 * tmp50
    tmp52 = x0 // 64
    tmp53 = tl.full(tmp52.shape, 0.0, tmp52.dtype)
    tmp54 = tl.where(tmp3, tmp52, tmp53)
    tmp55 = tmp11 & tmp13
    tmp56 = x0 // 16 % 4
    tmp57 = tl.full(tmp56.shape, 0.0, tmp56.dtype)
    tmp58 = tl.where(tmp55, tmp56, tmp57)
    tmp59 = tmp0 >= tmp12
    tmp60 = tl.full([1], 3, tl.int64)
    tmp61 = tmp0 < tmp60
    tmp62 = tmp59 & tmp61
    tmp63 = x0 // 4 % 4
    tmp64 = tl.full(tmp63.shape, 0.0, tmp63.dtype)
    tmp65 = tl.where(tmp62, tmp63, tmp64)
    tmp66 = tmp0 >= tmp60
    tmp67 = tl.full([1], 4, tl.int64)
    tmp69 = x0 % 4
    tmp70 = tl.full(tmp69.shape, 0.0, tmp69.dtype)
    tmp71 = tl.where(tmp66, tmp69, tmp70)
    tmp72 = tl.where(tmp62, tmp65, tmp71)
    tmp73 = tl.where(tmp55, tmp58, tmp72)
    tmp74 = tl.where(tmp3, tmp54, tmp73)
    tmp75 = tmp74 * tmp67
    tmp76 = tl.where(tmp30, tmp52, tmp53)
    tmp77 = tmp36 & tmp37
    tmp78 = tl.where(tmp77, tmp56, tmp57)
    tmp79 = tmp2 >= tmp12
    tmp80 = tmp2 < tmp60
    tmp81 = tmp79 & tmp80
    tmp82 = tl.where(tmp81, tmp63, tmp64)
    tmp83 = tmp2 >= tmp60
    tmp85 = tl.where(tmp83, tmp69, tmp70)
    tmp86 = tl.where(tmp81, tmp82, tmp85)
    tmp87 = tl.where(tmp77, tmp78, tmp86)
    tmp88 = tl.where(tmp30, tmp76, tmp87)
    tmp89 = tmp75 + tmp88
    tmp90 = tl.full([1], 7, tl.int64)
    tmp91 = tmp89 * tmp90
    tmp92 = tmp91 + tmp21
    tmp93 = tmp92 * tmp90
    tmp94 = tmp93 + tmp45
    tmp95 = tl.full([XBLOCK], 784, tl.int32)
    tmp96 = tmp94 + tmp95
    tmp97 = tmp94 < 0
    tmp98 = tl.where(tmp97, tmp96, tmp94)
    tl.device_assert((0 <= tmp98) & (tmp98 < 784) | ~xmask,
        'index out of bounds: 0 <= tmp98 < 784')
    tmp101 = tmp100 * tmp51
    tmp102 = tmp44 + tmp2
    tmp103 = tmp102.to(tl.float32)
    tmp104 = tmp43 - tmp103
    tmp105 = tl_math.abs(tmp104)
    tmp106 = tmp25 - tmp105
    tmp107 = triton_helpers.maximum(tmp27, tmp106)
    tmp108 = tmp28 * tmp107
    tmp109 = tmp93 + tmp102
    tmp110 = tmp109 + tmp95
    tmp111 = tmp109 < 0
    tmp112 = tl.where(tmp111, tmp110, tmp109)
    tl.device_assert((0 <= tmp112) & (tmp112 < 784) | ~xmask,
        'index out of bounds: 0 <= tmp112 < 784')
    tmp114 = tmp100 * tmp108
    tmp115 = tmp20 + tmp2
    tmp116 = tmp115.to(tl.float32)
    tmp117 = tmp19 - tmp116
    tmp118 = tl_math.abs(tmp117)
    tmp119 = tmp25 - tmp118
    tmp120 = triton_helpers.maximum(tmp27, tmp119)
    tmp121 = tmp120 * tmp50
    tmp122 = tmp91 + tmp115
    tmp123 = tmp122 * tmp90
    tmp124 = tmp123 + tmp45
    tmp125 = tmp124 + tmp95
    tmp126 = tmp124 < 0
    tmp127 = tl.where(tmp126, tmp125, tmp124)
    tl.device_assert((0 <= tmp127) & (tmp127 < 784) | ~xmask,
        'index out of bounds: 0 <= tmp127 < 784')
    tmp129 = tmp100 * tmp121
    tmp130 = tmp120 * tmp107
    tmp131 = tmp123 + tmp102
    tmp132 = tmp131 + tmp95
    tmp133 = tmp131 < 0
    tmp134 = tl.where(tmp133, tmp132, tmp131)
    tl.device_assert((0 <= tmp134) & (tmp134 < 784) | ~xmask,
        'index out of bounds: 0 <= tmp134 < 784')
    tmp136 = tmp100 * tmp130
    tl.atomic_add(out_ptr2 + tl.broadcast_to(tmp98, [XBLOCK]), tmp101,
        xmask, sem='relaxed')
    tl.atomic_add(out_ptr2 + tl.broadcast_to(tmp112, [XBLOCK]), tmp114,
        xmask, sem='relaxed')
    tl.atomic_add(out_ptr2 + tl.broadcast_to(tmp127, [XBLOCK]), tmp129,
        xmask, sem='relaxed')
    tl.atomic_add(out_ptr2 + tl.broadcast_to(tmp134, [XBLOCK]), tmp136,
        xmask, sem='relaxed')


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)
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_div_mul_0[grid(256)](arg1_1, buf2, 256, XBLOCK
            =256, num_warps=4, num_stages=1)
        del arg1_1
        buf5 = empty_strided_cuda((784,), (1,), torch.float32)
        triton_poi_fused_view_1[grid(784)](buf5, 784, XBLOCK=256, num_warps
            =4, num_stages=1)
        triton_poi_fused__to_copy_abs_add_index_add_mul_relu_rsub_sub_view_2[
            grid(256)](buf2, arg0_1, buf5, 256, XBLOCK=128, num_warps=4,
            num_stages=1)
        del arg0_1
        del buf2
    return reinterpret_tensor(buf5, (4, 4, 4, 4), (196, 49, 7, 1), 8),


def ravel_multi_index(indices, shape):
    indices_ravel = indices[0]
    for i in range(1, len(indices)):
        indices_ravel = indices_ravel * shape[i] + indices[i]
    return indices_ravel


def add_repeated(t, indices, values):
    shape = t.shape
    t_ravel = t.view(t.numel())
    indices_ravel = ravel_multi_index(indices, shape)
    t_ravel.index_add_(0, indices_ravel, values)


def meshgrid_tensor(*sizes, normalize=True, device='cuda:0'):
    if not normalize:
        aranges = [torch.arange(cur_size, device=device) for cur_size in sizes]
        grids = torch.meshgrid(aranges)
        grid = torch.stack(grids, dim=-1)
    else:
        aranges = [torch.arange(cur_size, device=device).float() for
            cur_size in sizes]
        grids = torch.meshgrid(aranges)
        grid = np.stack([(cur_grid / float(max(sizes[i] - 1, 1))) for i,
            cur_grid in enumerate(grids)], dim=-1)
    return grid


class InvGridSamplerNumeratorNew(nn.Module):
    eps = 1e-10

    def __init__(self, OH=None, OW=None):
        super(InvGridSamplerNumeratorNew, self).__init__()
        self.OH = OH
        self.OW = OW

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