import torch
import torch.nn as nn
import torch.nn.parallel
import torch.utils.data


class _netD_Q(nn.Module):
    """
    Second part of auxiliary network Q
    """

    def __init__(self, nd=10):
        super(_netD_Q, self).__init__()
        self.linear = nn.Linear(128, nd, bias=True)
        self.softmax = nn.LogSoftmax()
        self.nd = nd

    def forward(self, x):
        x = x.view(-1, 128)
        x = self.linear(x)
        x = torch.nn.functional.log_softmax(x, -1)
        return x.view(-1, self.nd, 1, 1)


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


def get_init_inputs():
    return [[], {}]

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.parallel
import torch.utils.data
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__log_softmax__log_softmax_backward_data_0(in_ptr0,
    out_ptr2, out_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 2
    rnumel = 10
    RBLOCK: tl.constexpr = 16
    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 + 10 * x0), rmask & xmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(rmask & xmask, tmp1, float('-inf'))
    tmp4 = triton_helpers.max2(tmp3, 1)[:, None]
    tmp5 = tmp0 - tmp4
    tmp6 = tl_math.exp(tmp5)
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tmp9 = tl.where(rmask & xmask, tmp7, 0)
    tmp10 = tl.sum(tmp9, 1)[:, None]
    tmp11 = tl_math.log(tmp10)
    tmp12 = tmp5 - tmp11
    tmp13 = tl_math.exp(tmp12)
    tl.store(out_ptr2 + (r1 + 10 * x0), tmp12, rmask & xmask)
    tl.store(out_ptr3 + (r1 + 10 * x0), tmp13, rmask & xmask)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (10, 128), (128, 1))
    assert_size_stride(primals_3, (10,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((2, 10), (10, 1), torch.float32)
        extern_kernels.addmm(primals_3, reinterpret_tensor(primals_1, (2, 
            128), (128, 1), 0), reinterpret_tensor(primals_2, (128, 10), (1,
            128), 0), alpha=1, beta=1, out=buf0)
        del primals_2
        del primals_3
        buf3 = empty_strided_cuda((2, 10), (10, 1), torch.float32)
        buf4 = empty_strided_cuda((2, 10), (10, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused__log_softmax__log_softmax_backward_data_0[grid(2)](
            buf0, buf3, buf4, 2, 10, XBLOCK=1, num_warps=2, num_stages=1)
        del buf0
    return reinterpret_tensor(buf3, (2, 10, 1, 1), (10, 1, 1, 1), 0
        ), reinterpret_tensor(primals_1, (2, 128), (128, 1), 0), buf4


class _netD_QNew(nn.Module):
    """
    Second part of auxiliary network Q
    """

    def __init__(self, nd=10):
        super(_netD_QNew, self).__init__()
        self.linear = nn.Linear(128, nd, bias=True)
        self.softmax = nn.LogSoftmax()
        self.nd = nd

    def forward(self, input_0):
        primals_2 = self.linear.weight
        primals_3 = self.linear.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3])
        return output[0]
