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
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_div_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
    tmp0 = tl.load(in_ptr0 + x0, None)
    tmp1 = 0.00392156862745098
    tmp2 = tmp0 * tmp1
    tl.store(out_ptr0 + x0, tmp2, None)


@triton.jit
def triton_poi_fused_convolution_relu_1(in_out_ptr0, in_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 156800
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 1225 % 32
    tmp0 = tl.load(in_out_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tl.store(in_out_ptr0 + x3, tmp4, xmask)


@triton.jit
def triton_poi_fused_convolution_relu_2(in_out_ptr0, in_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x3 = xindex
    x1 = xindex // 256 % 64
    tmp0 = tl.load(in_out_ptr0 + x3, None)
    tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tl.store(in_out_ptr0 + x3, tmp4, None)


@triton.jit
def triton_poi_fused_convolution_relu_threshold_backward_3(in_out_ptr0,
    in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 50176
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 196 % 64
    tmp0 = tl.load(in_out_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tmp5 = 0.0
    tmp6 = tmp4 <= tmp5
    tl.store(in_out_ptr0 + x3, tmp4, xmask)
    tl.store(out_ptr0 + x3, tmp6, xmask)


@triton.jit
def triton_poi_fused_relu_threshold_backward_4(in_out_ptr0, 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)
    x2 = xindex
    x0 = xindex % 512
    tmp0 = tl.load(in_out_ptr0 + x2, None)
    tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tmp5 = 0.0
    tmp6 = tmp4 <= tmp5
    tl.store(in_out_ptr0 + x2, tmp4, None)
    tl.store(out_ptr0 + x2, tmp6, None)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 144, 144), (82944, 20736, 144, 1))
    assert_size_stride(primals_2, (32, 4, 8, 8), (256, 64, 8, 1))
    assert_size_stride(primals_3, (32,), (1,))
    assert_size_stride(primals_4, (64, 32, 4, 4), (512, 16, 4, 1))
    assert_size_stride(primals_5, (64,), (1,))
    assert_size_stride(primals_6, (64, 64, 3, 3), (576, 9, 3, 1))
    assert_size_stride(primals_7, (64,), (1,))
    assert_size_stride(primals_8, (512, 3136), (3136, 1))
    assert_size_stride(primals_9, (512,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 144, 144), (82944, 20736, 144, 1),
            torch.float32)
        get_raw_stream(0)
        triton_poi_fused_div_0[grid(331776)](primals_1, buf0, 331776,
            XBLOCK=1024, num_warps=4, num_stages=1)
        del primals_1
        buf1 = extern_kernels.convolution(buf0, primals_2, stride=(4, 4),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf1, (4, 32, 35, 35), (39200, 1225, 35, 1))
        buf2 = buf1
        del buf1
        triton_poi_fused_convolution_relu_1[grid(156800)](buf2, primals_3, 
            156800, XBLOCK=1024, num_warps=4, num_stages=1)
        del primals_3
        buf3 = extern_kernels.convolution(buf2, primals_4, stride=(2, 2),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf3, (4, 64, 16, 16), (16384, 256, 16, 1))
        buf4 = buf3
        del buf3
        triton_poi_fused_convolution_relu_2[grid(65536)](buf4, primals_5, 
            65536, XBLOCK=512, num_warps=4, num_stages=1)
        del primals_5
        buf5 = extern_kernels.convolution(buf4, primals_6, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf5, (4, 64, 14, 14), (12544, 196, 14, 1))
        buf6 = buf5
        del buf5
        buf10 = empty_strided_cuda((4, 64, 14, 14), (12544, 196, 14, 1),
            torch.bool)
        triton_poi_fused_convolution_relu_threshold_backward_3[grid(50176)](
            buf6, primals_7, buf10, 50176, XBLOCK=512, num_warps=4,
            num_stages=1)
        del primals_7
        buf7 = empty_strided_cuda((16, 512), (512, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf6, (16, 3136), (3136, 1), 0
            ), reinterpret_tensor(primals_8, (3136, 512), (1, 3136), 0),
            out=buf7)
        buf8 = buf7
        del buf7
        buf9 = empty_strided_cuda((16, 512), (512, 1), torch.bool)
        triton_poi_fused_relu_threshold_backward_4[grid(8192)](buf8,
            primals_9, buf9, 8192, XBLOCK=256, num_warps=4, num_stages=1)
        del primals_9
    return (buf8, primals_2, primals_4, primals_6, buf0, buf2, buf4,
        reinterpret_tensor(buf6, (16, 3136), (3136, 1), 0), buf9, primals_8,
        buf10)


class DQNFeatureNetworkNew(nn.Module):

    def __init__(self, input_shape, output_shape, **kwargs):
        super().__init__()
        n_input = input_shape[0]
        self._h1 = nn.Conv2d(n_input, 32, kernel_size=8, stride=4)
        self._h2 = nn.Conv2d(32, 64, kernel_size=4, stride=2)
        self._h3 = nn.Conv2d(64, 64, kernel_size=3, stride=1)
        self._h4 = nn.Linear(3136, 512)
        nn.init.xavier_uniform_(self._h1.weight, gain=nn.init.
            calculate_gain('relu'))
        nn.init.xavier_uniform_(self._h2.weight, gain=nn.init.
            calculate_gain('relu'))
        nn.init.xavier_uniform_(self._h3.weight, gain=nn.init.
            calculate_gain('relu'))
        nn.init.xavier_uniform_(self._h4.weight, gain=nn.init.
            calculate_gain('relu'))

    def forward(self, input_0):
        primals_2 = self._h1.weight
        primals_3 = self._h1.bias
        primals_4 = self._h2.weight
        primals_5 = self._h2.bias
        primals_6 = self._h3.weight
        primals_7 = self._h3.bias
        primals_8 = self._h4.weight
        primals_9 = self._h4.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9])
        return output[0]


Measured latency_ms: 0.057756