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 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_cat_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 25856
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 404
    x3 = xindex // 404
    x2 = xindex // 1616
    x4 = xindex % 1616
    tmp0 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 400, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (400 * x3 + x0), tmp4 & xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp6 = tl.load(in_ptr1 + x0, tmp4 & xmask, eviction_policy='evict_last',
        other=0.0)
    tmp7 = tmp5 + tmp6
    tmp8 = tl.full([1], 0, tl.int32)
    tmp9 = triton_helpers.maximum(tmp8, tmp7)
    tmp10 = tl.full(tmp9.shape, 0.0, tmp9.dtype)
    tmp11 = tl.where(tmp4, tmp9, tmp10)
    tmp12 = tmp0 >= tmp3
    tl.full([1], 404, tl.int64)
    tmp15 = tl.load(in_ptr2 + (4 * x3 + (-400 + x0)), tmp12 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp16 = tl.where(tmp4, tmp11, tmp15)
    tl.store(out_ptr0 + (x4 + 1632 * x2), tmp16, xmask)


@triton.jit
def triton_poi_fused_cat_view_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 25856
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 404
    x1 = xindex // 404
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 404 * (x1 % 4) + 1632 * (x1 // 4)), xmask)
    tl.store(out_ptr0 + x2, tmp0, xmask)


@triton.jit
def triton_poi_fused_relu_threshold_backward_2(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 19200
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x4 = xindex
    x0 = xindex % 300
    x2 = xindex // 1200
    x3 = xindex % 1200
    tmp0 = tl.load(in_ptr0 + x4, xmask)
    tmp1 = tl.load(in_ptr1 + x0, 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(out_ptr0 + (x3 + 1216 * x2), tmp4, xmask)
    tl.store(out_ptr1 + (x3 + 1280 * x2), tmp6, xmask)


@triton.jit
def triton_poi_fused_relu_view_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 19200
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 300
    x1 = xindex // 300
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 300 * (x1 % 4) + 1216 * (x1 // 4)), xmask)
    tl.store(out_ptr0 + x2, tmp0, xmask)


@triton.jit
def triton_poi_fused_relu_threshold_backward_4(in_ptr0, in_ptr1, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 25600
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x0 = xindex % 400
    x2 = xindex // 1600
    x4 = xindex % 1600
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr1 + x0, 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(out_ptr0 + (x4 + 1664 * x2), tmp6, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8) = args
    args.clear()
    assert_size_stride(primals_1, (400, 4), (4, 1))
    assert_size_stride(primals_2, (400,), (1,))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_5, (300, 404), (404, 1))
    assert_size_stride(primals_6, (300,), (1,))
    assert_size_stride(primals_7, (1, 300), (300, 1))
    assert_size_stride(primals_8, (1,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 400), (400, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_1, (4, 400), (1, 4), 0), out=buf0)
        del primals_1
        buf1 = empty_strided_cuda((4, 4, 4, 404), (6528, 1632, 404, 1),
            torch.float32)
        get_raw_stream(0)
        triton_poi_fused_cat_0[grid(25856)](buf0, primals_2, primals_4,
            buf1, 25856, XBLOCK=256, num_warps=4, num_stages=1)
        del primals_4
        buf2 = empty_strided_cuda((64, 404), (404, 1), torch.float32)
        triton_poi_fused_cat_view_1[grid(25856)](buf1, buf2, 25856, XBLOCK=
            256, num_warps=4, num_stages=1)
        del buf1
        buf3 = empty_strided_cuda((64, 300), (300, 1), torch.float32)
        extern_kernels.mm(buf2, reinterpret_tensor(primals_5, (404, 300), (
            1, 404), 0), out=buf3)
        buf4 = empty_strided_cuda((4, 4, 4, 300), (4864, 1216, 300, 1),
            torch.float32)
        buf8 = empty_strided_cuda((4, 4, 4, 300), (5120, 1280, 300, 1),
            torch.bool)
        triton_poi_fused_relu_threshold_backward_2[grid(19200)](buf3,
            primals_6, buf4, buf8, 19200, XBLOCK=256, num_warps=4, num_stages=1
            )
        del primals_6
        buf5 = buf3
        del buf3
        triton_poi_fused_relu_view_3[grid(19200)](buf4, buf5, 19200, XBLOCK
            =256, num_warps=4, num_stages=1)
        del buf4
        buf7 = empty_strided_cuda((64, 1), (1, 1), torch.float32)
        extern_kernels.addmm(primals_8, buf5, reinterpret_tensor(primals_7,
            (300, 1), (1, 300), 0), alpha=1, beta=1, out=buf7)
        del primals_8
        buf9 = empty_strided_cuda((4, 4, 4, 400), (6656, 1664, 400, 1),
            torch.bool)
        triton_poi_fused_relu_threshold_backward_4[grid(25600)](buf0,
            primals_2, buf9, 25600, XBLOCK=256, num_warps=4, num_stages=1)
        del buf0
        del primals_2
    return reinterpret_tensor(buf7, (4, 4, 4, 1), (16, 4, 1, 1), 0
        ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
        ), buf2, buf5, primals_7, buf8, primals_5, buf9


def hidden_init(layer):
    fan_in = layer.weight.data.size()[0]
    lim = 1.0 / np.sqrt(fan_in)
    return -lim, lim


class CriticNew(nn.Module):
    """Critic (Value) Model."""

    def __init__(self, state_size, action_size, seed, fcs1_units=400,
        fc2_units=300):
        """Initialize parameters and build model.
        Params
        ======
            state_size (int): Dimension of each state
            action_size (int): Dimension of each action
            seed (int): Random seed
            fcs1_units (int): Number of nodes in the first hidden layer
            fc2_units (int): Number of nodes in the second hidden layer
        """
        super(CriticNew, self).__init__()
        self.seed = torch.manual_seed(seed)
        self.fcs1 = nn.Linear(state_size, fcs1_units)
        self.fc2 = nn.Linear(fcs1_units + action_size, fc2_units)
        self.fc3 = nn.Linear(fc2_units, 1)
        self.reset_parameters()

    def reset_parameters(self):
        self.fcs1.weight.data.uniform_(*hidden_init(self.fcs1))
        self.fc2.weight.data.uniform_(*hidden_init(self.fc2))
        self.fc3.weight.data.uniform_(-0.003, 0.003)

    def forward(self, input_0, input_1):
        primals_1 = self.fcs1.weight
        primals_2 = self.fcs1.bias
        primals_5 = self.fc2.weight
        primals_6 = self.fc2.bias
        primals_7 = self.fc3.weight
        primals_8 = self.fc3.bias
        primals_3 = input_0
        primals_4 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8])
        return output[0]


Measured latency_ms: 0.090535