import torch
import torch.nn as nn


class DQN(nn.Module):

    def __init__(self, obs_size: 'int', num_actions: 'int', hidden_size:
        'int'=20):
        super(DQN, self).__init__()
        self.l1 = nn.Linear(obs_size, hidden_size)
        self.n1 = nn.LayerNorm(hidden_size, elementwise_affine=True)
        self.l3 = nn.Linear(hidden_size, num_actions)
        self.activ = torch.nn.LeakyReLU()

    def forward(self, x):
        hidden = self.activ(self.n1(self.l1(x)))
        output = self.l3(hidden)
        return output


def get_inputs():
    return [torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {'obs_size': 4, 'num_actions': 4}]

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.triton_helpers import libdevice
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_per_fused_leaky_relu_native_layer_norm_0(in_out_ptr0,
    in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    xnumel = 64
    rnumel = 20
    RBLOCK: tl.constexpr = 32
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    rmask = rindex < rnumel
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 20 * x0), rmask & xmask, other=0.0)
    tmp24 = tl.load(in_ptr1 + r1, rmask, eviction_policy='evict_last',
        other=0.0)
    tmp26 = tl.load(in_ptr2 + r1, rmask, eviction_policy='evict_last',
        other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tl.where(rmask & xmask, tmp1, 0)
    tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp6 = tl.where(rmask & xmask, tmp4, 0)
    tmp7 = tl.sum(tmp6, 1)[:, None]
    tmp8 = tl.full([XBLOCK, 1], 20, tl.int32)
    tmp9 = tmp8.to(tl.float32)
    tmp10 = tmp7 / tmp9
    tmp11 = tmp1 - tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
    tmp15 = tl.where(rmask & xmask, tmp13, 0)
    tmp16 = tl.sum(tmp15, 1)[:, None]
    tmp17 = 20.0
    tmp18 = tmp16 / tmp17
    tmp19 = 1e-05
    tmp20 = tmp18 + tmp19
    tmp21 = libdevice.rsqrt(tmp20)
    tmp22 = tmp0 - tmp10
    tmp23 = tmp22 * tmp21
    tmp25 = tmp23 * tmp24
    tmp27 = tmp25 + tmp26
    tmp28 = 0.0
    tmp29 = tmp27 > tmp28
    tmp30 = 0.01
    tmp31 = tmp27 * tmp30
    tmp32 = tl.where(tmp29, tmp27, tmp31)
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp21, xmask)
    tl.store(in_out_ptr1 + (r1 + 20 * x0), tmp32, rmask & xmask)
    tl.store(out_ptr0 + x0, tmp10, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7) = args
    args.clear()
    assert_size_stride(primals_1, (20, 4), (4, 1))
    assert_size_stride(primals_2, (20,), (1,))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (20,), (1,))
    assert_size_stride(primals_5, (20,), (1,))
    assert_size_stride(primals_6, (4, 20), (20, 1))
    assert_size_stride(primals_7, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 20), (20, 1), torch.float32)
        extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 
            4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 20), (1, 4), 
            0), alpha=1, beta=1, out=buf0)
        del primals_1
        del primals_2
        buf1 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32)
        buf2 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32)
        buf4 = reinterpret_tensor(buf2, (4, 4, 4, 1), (16, 4, 1, 1), 0)
        del buf2
        buf5 = empty_strided_cuda((4, 4, 4, 20), (320, 80, 20, 1), torch.
            float32)
        buf6 = buf5
        del buf5
        get_raw_stream(0)
        triton_per_fused_leaky_relu_native_layer_norm_0[grid(64)](buf4,
            buf6, buf0, primals_4, primals_5, buf1, 64, 20, XBLOCK=8,
            num_warps=2, num_stages=1)
        buf7 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_7, reinterpret_tensor(buf6, (64, 20),
            (20, 1), 0), reinterpret_tensor(primals_6, (20, 4), (1, 20), 0),
            alpha=1, beta=1, out=buf7)
        del primals_7
    return reinterpret_tensor(buf7, (4, 4, 4, 4), (64, 16, 4, 1), 0
        ), primals_4, primals_5, reinterpret_tensor(primals_3, (64, 4), (4,
        1), 0), buf0, buf1, buf4, reinterpret_tensor(buf6, (64, 20), (20, 1), 0
        ), primals_6


class DQNNew(nn.Module):

    def __init__(self, obs_size: 'int', num_actions: 'int', hidden_size:
        'int'=20):
        super(DQNNew, self).__init__()
        self.l1 = nn.Linear(obs_size, hidden_size)
        self.n1 = nn.LayerNorm(hidden_size, elementwise_affine=True)
        self.l3 = nn.Linear(hidden_size, num_actions)
        self.activ = torch.nn.LeakyReLU()

    def forward(self, input_0):
        primals_1 = self.l1.weight
        primals_2 = self.l1.bias
        primals_4 = self.n1.weight
        primals_5 = self.n1.bias
        primals_6 = self.l3.weight
        primals_7 = self.l3.bias
        primals_3 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7])
        return output[0]
