import torch
import torch.nn as nn
import torch.nn.functional as F


def set_init(layers):
    for layer in layers:
        nn.init.normal_(layer.weight, mean=0.0, std=0.1)
        nn.init.constant_(layer.bias, 0.0)


class DiscreteNet(nn.Module):

    def __init__(self, s_dim, a_dim):
        super(DiscreteNet, self).__init__()
        self.s_dim = s_dim
        self.a_dim = a_dim
        self.pi1 = nn.Linear(s_dim, 200)
        self.pi2 = nn.Linear(200, a_dim)
        self.v1 = nn.Linear(s_dim, 100)
        self.v2 = nn.Linear(100, 1)
        set_init([self.pi1, self.pi2, self.v1, self.v2])
        self.distribution = torch.distributions.Categorical

    def forward(self, x):
        pi1 = F.relu6(self.pi1(x))
        logits = self.pi2(pi1)
        v1 = F.relu6(self.v1(x))
        values = self.v2(v1)
        return logits, values

    def choose_action(self, s):
        self.eval()
        logits, _ = self.forward(s)
        prob = F.softmax(logits, dim=1).data
        m = self.distribution(prob)
        return m.sample().numpy()[0]

    def loss_func(self, s, a, v_t):
        self.train()
        logits, values = self.forward(s)
        td = v_t - values
        c_loss = td.pow(2)
        probs = F.softmax(logits, dim=1)
        m = self.distribution(probs)
        exp_v = m.log_prob(a) * td.detach().squeeze()
        a_loss = -exp_v
        total_loss = (c_loss + a_loss).mean()
        return total_loss


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


def get_init_inputs():
    return [[], {'s_dim': 4, 'a_dim': 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 import triton_helpers
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_hardtanh_hardtanh_backward_0(in_ptr0, in_ptr1,
    out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 12800
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 200
    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 = triton_helpers.maximum(tmp2, tmp3)
    tmp5 = 6.0
    tmp6 = triton_helpers.minimum(tmp4, tmp5)
    tmp7 = tmp2 <= tmp3
    tmp8 = tmp2 >= tmp5
    tmp9 = tmp7 | tmp8
    tl.store(out_ptr0 + x2, tmp6, xmask)
    tl.store(out_ptr1 + x2, tmp9, xmask)


@triton.jit
def triton_poi_fused_hardtanh_hardtanh_backward_1(in_ptr0, in_ptr1,
    out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 6400
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x4 = xindex
    x0 = xindex % 100
    x3 = xindex // 1600
    x5 = xindex % 1600
    tmp0 = tl.load(in_ptr0 + x4, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = 0.0
    tmp4 = triton_helpers.maximum(tmp2, tmp3)
    tmp5 = 6.0
    tmp6 = triton_helpers.minimum(tmp4, tmp5)
    tmp7 = tmp2 <= tmp3
    tmp8 = tmp2 >= tmp5
    tmp9 = tmp7 | tmp8
    tl.store(out_ptr0 + x4, tmp6, xmask)
    tl.store(out_ptr1 + (x5 + 1664 * x3), tmp9, xmask)


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, (200, 4), (4, 1))
    assert_size_stride(primals_2, (200,), (1,))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (4, 200), (200, 1))
    assert_size_stride(primals_5, (4,), (1,))
    assert_size_stride(primals_6, (100, 4), (4, 1))
    assert_size_stride(primals_7, (100,), (1,))
    assert_size_stride(primals_8, (1, 100), (100, 1))
    assert_size_stride(primals_9, (1,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 200), (200, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_1, (4, 200), (1, 4), 0), out=buf0)
        del primals_1
        buf1 = empty_strided_cuda((4, 4, 4, 200), (3200, 800, 200, 1),
            torch.float32)
        buf8 = empty_strided_cuda((4, 4, 4, 200), (3200, 800, 200, 1),
            torch.bool)
        get_raw_stream(0)
        triton_poi_fused_hardtanh_hardtanh_backward_0[grid(12800)](buf0,
            primals_2, buf1, buf8, 12800, XBLOCK=256, num_warps=4, num_stages=1
            )
        del buf0
        del primals_2
        buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 200),
            (200, 1), 0), reinterpret_tensor(primals_4, (200, 4), (1, 200),
            0), alpha=1, beta=1, out=buf2)
        del primals_5
        buf3 = empty_strided_cuda((64, 100), (100, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_6, (4, 100), (1, 4), 0), out=buf3)
        del primals_6
        buf4 = empty_strided_cuda((4, 4, 4, 100), (1600, 400, 100, 1),
            torch.float32)
        buf7 = empty_strided_cuda((4, 4, 4, 100), (1664, 400, 100, 1),
            torch.bool)
        triton_poi_fused_hardtanh_hardtanh_backward_1[grid(6400)](buf3,
            primals_7, buf4, buf7, 6400, XBLOCK=256, num_warps=4, num_stages=1)
        del buf3
        del primals_7
        buf6 = empty_strided_cuda((64, 1), (1, 1), torch.float32)
        extern_kernels.addmm(primals_9, reinterpret_tensor(buf4, (64, 100),
            (100, 1), 0), reinterpret_tensor(primals_8, (100, 1), (1, 100),
            0), alpha=1, beta=1, out=buf6)
        del primals_9
    return reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0
        ), reinterpret_tensor(buf6, (4, 4, 4, 1), (16, 4, 1, 1), 0
        ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
        ), reinterpret_tensor(buf1, (64, 200), (200, 1), 0
        ), reinterpret_tensor(buf4, (64, 100), (100, 1), 0
        ), primals_8, buf7, primals_4, buf8


def set_init(layers):
    for layer in layers:
        nn.init.normal_(layer.weight, mean=0.0, std=0.1)
        nn.init.constant_(layer.bias, 0.0)


class DiscreteNetNew(nn.Module):

    def __init__(self, s_dim, a_dim):
        super(DiscreteNetNew, self).__init__()
        self.s_dim = s_dim
        self.a_dim = a_dim
        self.pi1 = nn.Linear(s_dim, 200)
        self.pi2 = nn.Linear(200, a_dim)
        self.v1 = nn.Linear(s_dim, 100)
        self.v2 = nn.Linear(100, 1)
        set_init([self.pi1, self.pi2, self.v1, self.v2])
        self.distribution = torch.distributions.Categorical

    def choose_action(self, s):
        self.eval()
        logits, _ = self.forward(s)
        prob = F.softmax(logits, dim=1).data
        m = self.distribution(prob)
        return m.sample().numpy()[0]

    def loss_func(self, s, a, v_t):
        self.train()
        logits, values = self.forward(s)
        td = v_t - values
        c_loss = td.pow(2)
        probs = F.softmax(logits, dim=1)
        m = self.distribution(probs)
        exp_v = m.log_prob(a) * td.detach().squeeze()
        a_loss = -exp_v
        total_loss = (c_loss + a_loss).mean()
        return total_loss

    def forward(self, input_0):
        primals_1 = self.pi1.weight
        primals_2 = self.pi1.bias
        primals_4 = self.pi2.weight
        primals_5 = self.pi2.bias
        primals_6 = self.v1.weight
        primals_7 = self.v1.bias
        primals_8 = self.v2.weight
        primals_9 = self.v2.bias
        primals_3 = 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], output[1]
