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 typing import List
from typing import Tuple
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_leaky_relu_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = 0.0
    tmp4 = tmp2 > tmp3
    tmp5 = 0.01
    tmp6 = tmp2 * tmp5
    tmp7 = tl.where(tmp4, tmp2, tmp6)
    tl.store(out_ptr0 + x2, tmp4, xmask)
    tl.store(out_ptr1 + x2, tmp7, xmask)


@triton.jit
def triton_poi_fused_clamp_ge_le_logical_and_1(in_ptr0, out_ptr0, out_ptr1,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = xindex // 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (4 + x0 + 8 * x1), xmask)
    tmp1 = -20.0
    tmp2 = triton_helpers.maximum(tmp0, tmp1)
    tmp3 = 2.0
    tmp4 = triton_helpers.minimum(tmp2, tmp3)
    tmp5 = tmp0 >= tmp1
    tmp6 = tmp0 <= tmp3
    tmp7 = tmp5 & tmp6
    tl.store(out_ptr0 + x2, tmp4, xmask)
    tl.store(out_ptr1 + x2, tmp7, 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, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (4,), (1,))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    assert_size_stride(primals_5, (4,), (1,))
    assert_size_stride(primals_6, (8, 4), (4, 1))
    assert_size_stride(primals_7, (8,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (4, 4),
            (1, 4), 0), out=buf0)
        del primals_2
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_leaky_relu_0[grid(16)](buf0, primals_3, buf1, buf2,
            16, XBLOCK=16, num_warps=1, num_stages=1)
        del primals_3
        buf3 = buf0
        del buf0
        extern_kernels.mm(buf2, reinterpret_tensor(primals_4, (4, 4), (1, 4
            ), 0), out=buf3)
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
        buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_leaky_relu_0[grid(16)](buf3, primals_5, buf4, buf5,
            16, XBLOCK=16, num_warps=1, num_stages=1)
        del primals_5
        buf6 = empty_strided_cuda((4, 8), (8, 1), torch.float32)
        extern_kernels.addmm(primals_7, buf5, reinterpret_tensor(primals_6,
            (4, 8), (1, 4), 0), alpha=1, beta=1, out=buf6)
        del primals_7
        buf7 = buf3
        del buf3
        buf8 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
        triton_poi_fused_clamp_ge_le_logical_and_1[grid(16)](buf6, buf7,
            buf8, 16, XBLOCK=16, num_warps=1, num_stages=1)
    return reinterpret_tensor(buf6, (4, 4), (8, 1), 0
        ), buf7, primals_1, buf1, buf2, buf4, buf5, buf8, primals_6, primals_4


class LinearNetwork(nn.Module):

    def __init__(self, input_size: 'int', output_size: 'int', hidden_layers:
        'List[int]', activation: 'nn.Module'=nn.LeakyReLU):
        super(LinearNetwork, self).__init__()
        self.input_size = input_size
        self.output_size = output_size
        self.hidden_layers = hidden_layers
        self.activation = activation
        self.n_layers = len(hidden_layers)
        self.layers = nn.Sequential()
        if self.n_layers == 0:
            self.layers.add_module('single_layer', nn.Linear(input_size,
                output_size))
        else:
            for i in range(self.n_layers + 1):
                if i == 0:
                    self.layers.add_module('input_layer', nn.Linear(
                        input_size, hidden_layers[0]))
                    self.layers.add_module('input_layer_activation', self.
                        activation())
                elif i < self.n_layers:
                    self.layers.add_module(f'hidden_layer_{i}', nn.Linear(
                        hidden_layers[i - 1], hidden_layers[i]))
                    self.layers.add_module(f'input_layer_{i}_activation',
                        self.activation())
                else:
                    self.layers.add_module('output_layer', nn.Linear(
                        hidden_layers[i - 1], output_size))

    def forward(self, x: 'torch.Tensor') ->torch.Tensor:
        assert len(x.shape) == 2
        assert x.shape[1] == self.input_size
        return self.layers(x)


class TanhGaussianPolicyNew(nn.Module):
    LOG_STD_MIN: 'float' = -20.0
    LOG_STD_MAX: 'float' = 2.0
    EPS: 'float' = 1e-06

    def __init__(self, n_agents: 'int', obs_size: 'int', act_size: 'int',
        hidden_layers: 'List[int]', activation: 'nn.Module'=nn.LeakyReLU):
        super(TanhGaussianPolicyNew, self).__init__()
        self.n_agents = n_agents
        self.obs_size = obs_size
        self.act_size = act_size
        self.hidden_layers = hidden_layers
        self.activation = activation
        self.input_size = obs_size
        self.output_size = 2 * act_size
        self.policy = LinearNetwork(self.input_size, self.output_size,
            hidden_layers, activation)

    def sample(self, obs: 'torch.Tensor') ->Tuple[torch.Tensor, torch.
        Tensor, torch.Tensor]:
        assert len(obs.shape) == 3
        assert obs.shape[1] == self.n_agents
        assert obs.shape[2] == self.obs_size
        N = obs.shape[0]
        means = torch.empty((N, self.n_agents, self.act_size))
        stds = torch.empty((N, self.n_agents, self.act_size))
        for i in range(self.n_agents):
            mean, log_std = self.forward(obs[:, i])
            std = log_std.exp()
            means[:, i] = mean
            stds[:, i] = std
        dist = torch.distributions.normal.Normal(means, stds)
        act_sampled = dist.rsample()
        act_sampled_tanh = torch.tanh(act_sampled)
        log_probs = dist.log_prob(act_sampled) - torch.log(1 -
            act_sampled_tanh.square() + self.EPS)
        entropies = -log_probs.sum(dim=-1, keepdim=False)
        return act_sampled_tanh, entropies, torch.tanh(means)

    def forward(self, input_0):
        primals_1 = self.policy.layers.input_layer.weight
        primals_3 = self.policy.layers.input_layer.bias
        primals_2 = self.policy.layers.hidden_layer_1.weight
        primals_5 = self.policy.layers.hidden_layer_1.bias
        primals_6 = self.policy.layers.output_layer.weight
        primals_7 = self.policy.layers.output_layer.bias
        primals_4 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7])
        return output[0], output[1]


Measured latency_ms: 0.067866