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 math as tl_math
import torch.nn as nn
import torch.nn.functional as F
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 = 25088
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 196 % 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)
    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_4(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)
    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)
    tl.store(in_out_ptr0 + x2, tmp4, None)


@triton.jit
def triton_poi_fused__softmax_5(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tmp0 - tmp7
    tmp9 = tl_math.exp(tmp8)
    tl.store(out_ptr0 + x2, tmp9, xmask)


@triton.jit
def triton_poi_fused__softmax_6(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tl.store(out_ptr0 + x2, tmp8, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9, primals_10, primals_11, primals_12,
        primals_13) = 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, (32, 64, 3, 3), (576, 9, 3, 1))
    assert_size_stride(primals_7, (32,), (1,))
    assert_size_stride(primals_8, (512, 1568), (1568, 1))
    assert_size_stride(primals_9, (512,), (1,))
    assert_size_stride(primals_10, (1, 512), (512, 1))
    assert_size_stride(primals_11, (1,), (1,))
    assert_size_stride(primals_12, (4, 512), (512, 1))
    assert_size_stride(primals_13, (4,), (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=512, num_warps=8, 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, 32, 14, 14), (6272, 196, 14, 1))
        buf6 = buf5
        del buf5
        buf14 = empty_strided_cuda((4, 32, 14, 14), (6272, 196, 14, 1),
            torch.bool)
        triton_poi_fused_convolution_relu_threshold_backward_3[grid(25088)](
            buf6, primals_7, buf14, 25088, XBLOCK=256, 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, 1568), (1568, 1), 0
            ), reinterpret_tensor(primals_8, (1568, 512), (1, 1568), 0),
            out=buf7)
        buf8 = buf7
        del buf7
        triton_poi_fused_relu_4[grid(8192)](buf8, primals_9, 8192, XBLOCK=
            256, num_warps=4, num_stages=1)
        del primals_9
        buf10 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.addmm(primals_11, buf8, reinterpret_tensor(
            primals_10, (512, 1), (1, 512), 0), alpha=1, beta=1, out=buf10)
        del primals_11
        buf11 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_13, buf8, reinterpret_tensor(
            primals_12, (512, 4), (1, 512), 0), alpha=1, beta=1, out=buf11)
        del primals_13
        buf12 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        triton_poi_fused__softmax_5[grid(64)](buf11, buf12, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf13 = buf11
        del buf11
        triton_poi_fused__softmax_6[grid(64)](buf12, buf13, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del buf12
    return (buf10, buf13, primals_2, primals_4, primals_6, buf0, buf2, buf4,
        reinterpret_tensor(buf6, (16, 1568), (1568, 1), 0), buf8, buf13,
        primals_12, primals_10, primals_8, buf14)


class DeepMind(nn.Module):

    def __init__(self):
        super(DeepMind, self).__init__()
        self.conv1 = nn.Conv2d(4, 32, 8, stride=4)
        self.conv2 = nn.Conv2d(32, 64, 4, stride=2)
        self.conv3 = nn.Conv2d(64, 32, 3, stride=1)
        self.fc1 = nn.Linear(32 * 7 * 7, 512)
        nn.init.orthogonal_(self.conv1.weight.data, gain=nn.init.
            calculate_gain('relu'))
        nn.init.orthogonal_(self.conv2.weight.data, gain=nn.init.
            calculate_gain('relu'))
        nn.init.orthogonal_(self.conv3.weight.data, gain=nn.init.
            calculate_gain('relu'))
        nn.init.orthogonal_(self.fc1.weight.data, gain=nn.init.
            calculate_gain('relu'))
        nn.init.constant_(self.conv1.bias.data, 0)
        nn.init.constant_(self.conv2.bias.data, 0)
        nn.init.constant_(self.conv3.bias.data, 0)
        nn.init.constant_(self.fc1.bias.data, 0)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.relu(self.conv2(x))
        x = F.relu(self.conv3(x))
        x = x.view(-1, 32 * 7 * 7)
        x = F.relu(self.fc1(x))
        return x


class NetNew(nn.Module):

    def __init__(self, num_actions):
        super(NetNew, self).__init__()
        self.cnn_layer = DeepMind()
        self.critic = nn.Linear(512, 1)
        self.actor = nn.Linear(512, num_actions)
        nn.init.orthogonal_(self.critic.weight.data)
        nn.init.constant_(self.critic.bias.data, 0)
        nn.init.orthogonal_(self.actor.weight.data, gain=0.01)
        nn.init.constant_(self.actor.bias.data, 0)

    def forward(self, input_0):
        primals_2 = self.cnn_layer.conv1.weight
        primals_3 = self.cnn_layer.conv1.bias
        primals_4 = self.cnn_layer.conv2.weight
        primals_5 = self.cnn_layer.conv2.bias
        primals_6 = self.cnn_layer.conv3.weight
        primals_7 = self.cnn_layer.conv3.bias
        primals_8 = self.cnn_layer.fc1.weight
        primals_9 = self.cnn_layer.fc1.bias
        primals_10 = self.critic.weight
        primals_11 = self.critic.bias
        primals_12 = self.actor.weight
        primals_13 = self.actor.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,
            primals_10, primals_11, primals_12, primals_13])
        return output[0], output[1]


Measured latency_ms: 0.172561