from torch.nn import Module
import torch
import torch.autograd
import torch.nn as nn
from torch.nn.modules.module import Module


class GraphConvolution(Module):
    """
    Simple GCN layer, similar to https://arxiv.org/abs/1609.02907.
    """

    def __init__(self, state_dim, name='', out_state_dim=None):
        super(GraphConvolution, self).__init__()
        self.state_dim = state_dim
        if out_state_dim is None:
            self.out_state_dim = state_dim
        else:
            self.out_state_dim = out_state_dim
        self.fc1 = nn.Linear(in_features=self.state_dim, out_features=self.
            out_state_dim)
        self.fc2 = nn.Linear(in_features=self.state_dim, out_features=self.
            out_state_dim)
        self.name = name

    def forward(self, input, adj):
        state_in = self.fc1(input)
        forward_input = self.fc2(torch.bmm(adj, input))
        return state_in + forward_input


class GraphResConvolution(Module):
    """
    Simple GCN layer, similar to https://arxiv.org/abs/1609.02907.
    """

    def __init__(self, state_dim, name=''):
        super(GraphResConvolution, self).__init__()
        self.state_dim = state_dim
        self.gcn_1 = GraphConvolution(state_dim, f'{name}_1')
        self.gcn_2 = GraphConvolution(state_dim, f'{name}_2')
        self.relu1 = nn.ReLU()
        self.relu2 = nn.ReLU()
        self.name = name

    def forward(self, input, adj):
        output_1 = self.gcn_1(input, adj)
        output_1_relu = self.relu1(output_1)
        output_2 = self.gcn_2(output_1_relu, adj)
        output_2_res = output_2 + input
        output = self.relu2(output_2_res)
        return output


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


def get_init_inputs():
    return [[], {'state_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
from torch.nn import Module
import torch.autograd
import torch.nn as nn
from torch.nn.modules.module import Module
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_add_relu_0(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + x2, xmask)
    tmp4 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = tmp2 + tmp5
    tmp7 = tl.full([1], 0, tl.int32)
    tmp8 = triton_helpers.maximum(tmp7, tmp6)
    tl.store(in_out_ptr0 + x2, tmp8, xmask)


@triton.jit
def triton_poi_fused_add_relu_threshold_backward_1(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + x2, xmask)
    tmp4 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr3 + x2, xmask)
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = tmp2 + tmp5
    tmp8 = tmp6 + tmp7
    tmp9 = tl.full([1], 0, tl.int32)
    tmp10 = triton_helpers.maximum(tmp9, tmp8)
    tmp11 = 0.0
    tmp12 = tmp10 <= tmp11
    tl.store(in_out_ptr0 + x2, tmp10, xmask)
    tl.store(out_ptr0 + x2, tmp12, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9, primals_10) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_4, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_5, (4, 4), (4, 1))
    assert_size_stride(primals_6, (4,), (1,))
    assert_size_stride(primals_7, (4, 4), (4, 1))
    assert_size_stride(primals_8, (4,), (1,))
    assert_size_stride(primals_9, (4, 4), (4, 1))
    assert_size_stride(primals_10, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_3, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0)
        del primals_1
        buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(primals_4, primals_3, out=buf1)
        buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf1, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), out=buf2)
        del primals_5
        buf3 = reinterpret_tensor(buf0, (4, 4, 4), (16, 4, 1), 0)
        del buf0
        get_raw_stream(0)
        triton_poi_fused_add_relu_0[grid(64)](buf3, primals_2, buf2,
            primals_6, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del primals_2
        del primals_6
        buf4 = buf2
        del buf2
        extern_kernels.mm(reinterpret_tensor(buf3, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_7, (4, 4), (1, 4), 0), out=buf4)
        buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(primals_4, buf3, out=buf5)
        buf6 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf5, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_9, (4, 4), (1, 4), 0), out=buf6)
        buf7 = reinterpret_tensor(buf4, (4, 4, 4), (16, 4, 1), 0)
        del buf4
        buf8 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool)
        triton_poi_fused_add_relu_threshold_backward_1[grid(64)](buf7,
            primals_8, buf6, primals_10, primals_3, buf8, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del buf6
        del primals_10
        del primals_8
    return buf7, reinterpret_tensor(primals_3, (16, 4), (4, 1), 0
        ), reinterpret_tensor(buf1, (16, 4), (4, 1), 0
        ), buf3, reinterpret_tensor(buf5, (16, 4), (4, 1), 0
        ), buf8, primals_9, reinterpret_tensor(primals_4, (4, 4, 4), (16, 1,
        4), 0), primals_7


class GraphConvolution(Module):
    """
    Simple GCN layer, similar to https://arxiv.org/abs/1609.02907.
    """

    def __init__(self, state_dim, name='', out_state_dim=None):
        super(GraphConvolution, self).__init__()
        self.state_dim = state_dim
        if out_state_dim is None:
            self.out_state_dim = state_dim
        else:
            self.out_state_dim = out_state_dim
        self.fc1 = nn.Linear(in_features=self.state_dim, out_features=self.
            out_state_dim)
        self.fc2 = nn.Linear(in_features=self.state_dim, out_features=self.
            out_state_dim)
        self.name = name

    def forward(self, input, adj):
        state_in = self.fc1(input)
        forward_input = self.fc2(torch.bmm(adj, input))
        return state_in + forward_input


class GraphResConvolutionNew(Module):
    """
    Simple GCN layer, similar to https://arxiv.org/abs/1609.02907.
    """

    def __init__(self, state_dim, name=''):
        super(GraphResConvolutionNew, self).__init__()
        self.state_dim = state_dim
        self.gcn_1 = GraphConvolution(state_dim, f'{name}_1')
        self.gcn_2 = GraphConvolution(state_dim, f'{name}_2')
        self.relu1 = nn.ReLU()
        self.relu2 = nn.ReLU()
        self.name = name

    def forward(self, input_0, input_1):
        primals_1 = self.gcn_1.fc1.weight
        primals_2 = self.gcn_1.fc1.bias
        primals_5 = self.gcn_1.fc2.weight
        primals_6 = self.gcn_1.fc2.bias
        primals_7 = self.gcn_2.fc1.weight
        primals_8 = self.gcn_2.fc1.bias
        primals_9 = self.gcn_2.fc2.weight
        primals_10 = self.gcn_2.fc2.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, primals_9, primals_10])
        return output[0]
