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
from torch.nn import Module
from torch.nn.modules.module import Module
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_abs_repeat_sgn_sub_0(in_ptr0, out_ptr0, out_ptr1,
    xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x3 = xindex % 512
    x0 = xindex % 128
    x2 = xindex // 512
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + x3, None, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (x0 + 128 * x2), None, eviction_policy=
        'evict_last')
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.abs(tmp2)
    tmp4 = tl.full([1], 0, tl.int32)
    tmp5 = tmp4 < tmp2
    tmp6 = tmp5.to(tl.int8)
    tmp7 = tmp2 < tmp4
    tmp8 = tmp7.to(tl.int8)
    tmp9 = tmp6 - tmp8
    tmp10 = tmp9.to(tmp2.dtype)
    tl.store(out_ptr0 + x4, tmp3, None)
    tl.store(out_ptr1 + x4, tmp10, None)


@triton.jit
def triton_poi_fused_gt_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = 0.0
    tmp2 = tmp0 > tmp1
    tl.store(out_ptr0 + x0, tmp2, xmask)


@triton.jit
def triton_poi_fused__softmax_mul_relu_where_2(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last').to(tl
        .int1)
    tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        ).to(tl.int1)
    tmp7 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        ).to(tl.int1)
    tmp12 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp16 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        ).to(tl.int1)
    tmp17 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp2 = tl.full([1], 0, tl.int32)
    tmp3 = triton_helpers.maximum(tmp2, tmp1)
    tmp4 = -8999999815811072.0
    tmp5 = tl.where(tmp0, tmp3, tmp4)
    tmp8 = triton_helpers.maximum(tmp2, tmp7)
    tmp9 = tl.where(tmp6, tmp8, tmp4)
    tmp10 = triton_helpers.maximum(tmp5, tmp9)
    tmp13 = triton_helpers.maximum(tmp2, tmp12)
    tmp14 = tl.where(tmp11, tmp13, tmp4)
    tmp15 = triton_helpers.maximum(tmp10, tmp14)
    tmp18 = triton_helpers.maximum(tmp2, tmp17)
    tmp19 = tl.where(tmp16, tmp18, tmp4)
    tmp20 = triton_helpers.maximum(tmp15, tmp19)
    tmp21 = tmp5 - tmp20
    tmp22 = tl_math.exp(tmp21)
    tmp23 = tmp9 - tmp20
    tmp24 = tl_math.exp(tmp23)
    tmp25 = tmp22 + tmp24
    tmp26 = tmp14 - tmp20
    tmp27 = tl_math.exp(tmp26)
    tmp28 = tmp25 + tmp27
    tmp29 = tmp19 - tmp20
    tmp30 = tl_math.exp(tmp29)
    tmp31 = tmp28 + tmp30
    tl.store(out_ptr0 + x0, tmp20, xmask)
    tl.store(out_ptr1 + x0, tmp31, xmask)


@triton.jit
def triton_poi_fused__softmax_mul_relu_threshold_backward_where_3(in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, 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
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask).to(tl.int1)
    tmp1 = tl.load(in_ptr1 + x2, xmask)
    tmp6 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.full([1], 0, tl.int32)
    tmp3 = triton_helpers.maximum(tmp2, tmp1)
    tmp4 = -8999999815811072.0
    tmp5 = tl.where(tmp0, tmp3, tmp4)
    tmp7 = tmp5 - tmp6
    tmp8 = tl_math.exp(tmp7)
    tmp10 = tmp8 / tmp9
    tmp11 = 0.0
    tmp12 = tmp3 <= tmp11
    tl.store(out_ptr0 + x2, tmp10, xmask)
    tl.store(out_ptr1 + x2, tmp12, xmask)


@triton.jit
def triton_poi_fused_relu_4(in_out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 512
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_out_ptr0 + x0, xmask)
    tmp1 = tl.full([1], 0, tl.int32)
    tmp2 = triton_helpers.maximum(tmp1, tmp0)
    tl.store(in_out_ptr0 + x0, tmp2, xmask)


@triton.jit
def triton_poi_fused_abs_repeat_sgn_sub_5(in_ptr0, out_ptr0, out_ptr1,
    xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x3 = xindex % 512
    x0 = xindex % 128
    x2 = xindex // 512
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + x3, None, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (x0 + 128 * x2), None, eviction_policy=
        'evict_last')
    tmp1 = tl.full([1], 0, tl.int32)
    tmp2 = triton_helpers.maximum(tmp1, tmp0)
    tmp4 = triton_helpers.maximum(tmp1, tmp3)
    tmp5 = tmp2 - tmp4
    tmp6 = tl_math.abs(tmp5)
    tmp7 = tmp1 < tmp5
    tmp8 = tmp7.to(tl.int8)
    tmp9 = tmp5 < tmp1
    tmp10 = tmp9.to(tl.int8)
    tmp11 = tmp8 - tmp10
    tmp12 = tmp11.to(tmp5.dtype)
    tl.store(out_ptr0 + x4, tmp6, None)
    tl.store(out_ptr1 + x4, tmp12, None)


@triton.jit
def triton_poi_fused_relu_threshold_backward_6(in_ptr0, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 512
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = tl.full([1], 0, tl.int32)
    tmp2 = triton_helpers.maximum(tmp1, tmp0)
    tmp3 = 0.0
    tmp4 = tmp2 <= tmp3
    tl.store(out_ptr0 + x0, tmp4, 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, 128), (128, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (128, 1), (1, 1))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    assert_size_stride(primals_5, (128, 128), (128, 1))
    assert_size_stride(primals_6, (128, 1), (1, 1))
    assert_size_stride(primals_7, (128, 128), (128, 1))
    assert_size_stride(primals_8, (128, 1), (1, 1))
    assert_size_stride(primals_9, (4, 128), (128, 1))
    assert_size_stride(primals_10, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 128), (128, 1), torch.float32)
        extern_kernels.mm(primals_2, primals_1, out=buf0)
        del primals_1
        buf1 = empty_strided_cuda((4, 4, 128), (512, 128, 1), torch.float32)
        buf33 = empty_strided_cuda((4, 4, 128), (512, 128, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_abs_repeat_sgn_sub_0[grid(2048)](buf0, buf1, buf33,
            2048, XBLOCK=128, num_warps=4, num_stages=1)
        buf2 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf1, (16, 128), (128, 1), 0),
            primals_3, out=buf2)
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
        triton_poi_fused_gt_1[grid(16)](primals_4, buf3, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del primals_4
        buf4 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        buf5 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        triton_poi_fused__softmax_mul_relu_where_2[grid(4)](buf3, buf2,
            buf4, buf5, 4, XBLOCK=4, num_warps=1, num_stages=1)
        buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf32 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
        triton_poi_fused__softmax_mul_relu_threshold_backward_where_3[grid(16)
            ](buf3, buf2, buf4, buf5, buf6, buf32, 16, XBLOCK=16, num_warps
            =1, num_stages=1)
        buf7 = empty_strided_cuda((4, 128), (128, 1), torch.float32)
        extern_kernels.mm(buf6, buf0, out=buf7)
        buf8 = buf7
        del buf7
        triton_poi_fused_relu_4[grid(512)](buf8, 512, XBLOCK=256, num_warps
            =4, num_stages=1)
        buf9 = empty_strided_cuda((4, 128), (128, 1), torch.float32)
        extern_kernels.mm(buf8, primals_5, out=buf9)
        buf10 = empty_strided_cuda((4, 4, 128), (512, 128, 1), torch.float32)
        buf31 = empty_strided_cuda((4, 4, 128), (512, 128, 1), torch.float32)
        triton_poi_fused_abs_repeat_sgn_sub_0[grid(2048)](buf9, buf10,
            buf31, 2048, XBLOCK=128, num_warps=4, num_stages=1)
        buf11 = buf2
        del buf2
        extern_kernels.mm(reinterpret_tensor(buf10, (16, 128), (128, 1), 0),
            primals_6, out=buf11)
        buf12 = buf5
        del buf5
        buf13 = buf4
        del buf4
        triton_poi_fused__softmax_mul_relu_where_2[grid(4)](buf3, buf11,
            buf12, buf13, 4, XBLOCK=4, num_warps=1, num_stages=1)
        buf14 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf30 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
        triton_poi_fused__softmax_mul_relu_threshold_backward_where_3[grid(16)
            ](buf3, buf11, buf12, buf13, buf14, buf30, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf15 = empty_strided_cuda((4, 128), (128, 1), torch.float32)
        extern_kernels.mm(buf14, buf9, out=buf15)
        buf16 = buf15
        del buf15
        triton_poi_fused_relu_4[grid(512)](buf16, 512, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf17 = empty_strided_cuda((4, 128), (128, 1), torch.float32)
        extern_kernels.mm(buf16, primals_7, out=buf17)
        buf18 = empty_strided_cuda((4, 4, 128), (512, 128, 1), torch.float32)
        buf29 = empty_strided_cuda((4, 4, 128), (512, 128, 1), torch.float32)
        triton_poi_fused_abs_repeat_sgn_sub_0[grid(2048)](buf17, buf18,
            buf29, 2048, XBLOCK=128, num_warps=4, num_stages=1)
        buf19 = buf11
        del buf11
        extern_kernels.mm(reinterpret_tensor(buf18, (16, 128), (128, 1), 0),
            primals_8, out=buf19)
        buf20 = buf13
        del buf13
        buf21 = buf12
        del buf12
        triton_poi_fused__softmax_mul_relu_where_2[grid(4)](buf3, buf19,
            buf20, buf21, 4, XBLOCK=4, num_warps=1, num_stages=1)
        buf22 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf28 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
        triton_poi_fused__softmax_mul_relu_threshold_backward_where_3[grid(16)
            ](buf3, buf19, buf20, buf21, buf22, buf28, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del buf19
        del buf20
        del buf21
        buf23 = empty_strided_cuda((4, 128), (128, 1), torch.float32)
        extern_kernels.mm(buf22, buf17, out=buf23)
        buf24 = empty_strided_cuda((4, 4, 128), (512, 128, 1), torch.float32)
        buf26 = empty_strided_cuda((4, 4, 128), (512, 128, 1), torch.float32)
        triton_poi_fused_abs_repeat_sgn_sub_5[grid(2048)](buf23, buf24,
            buf26, 2048, XBLOCK=256, num_warps=4, num_stages=1)
        buf25 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_10, reinterpret_tensor(buf24, (16, 128
            ), (128, 1), 0), reinterpret_tensor(primals_9, (128, 4), (1, 
            128), 0), alpha=1, beta=1, out=buf25)
        del primals_10
        buf27 = empty_strided_cuda((4, 128), (128, 1), torch.bool)
        triton_poi_fused_relu_threshold_backward_6[grid(512)](buf23, buf27,
            512, XBLOCK=256, num_warps=4, num_stages=1)
        del buf23
    return buf25, buf3, buf6, buf8, buf14, buf16, buf22, reinterpret_tensor(
        buf24, (16, 128), (128, 1), 0
        ), primals_9, buf26, buf27, reinterpret_tensor(buf17, (128, 4), (1,
        128), 0), buf28, reinterpret_tensor(buf18, (128, 16), (1, 128), 0
        ), reinterpret_tensor(primals_8, (1, 128), (1, 1), 0
        ), buf29, reinterpret_tensor(primals_7, (128, 128), (1, 128), 0
        ), reinterpret_tensor(buf9, (128, 4), (1, 128), 0
        ), buf30, reinterpret_tensor(buf10, (128, 16), (1, 128), 0
        ), reinterpret_tensor(primals_6, (1, 128), (1, 1), 0
        ), buf31, reinterpret_tensor(primals_5, (128, 128), (1, 128), 0
        ), reinterpret_tensor(buf0, (128, 4), (1, 128), 0
        ), buf32, reinterpret_tensor(buf1, (128, 16), (1, 128), 0
        ), reinterpret_tensor(primals_3, (1, 128), (1, 1), 0
        ), buf33, reinterpret_tensor(primals_2, (4, 4), (1, 4), 0)


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

    def __init__(self, in_features, out_features, include_adj=True, bias=True):
        super(EdgeGCN, self).__init__()
        self.include_adj = include_adj
        self.in_features = in_features + 1 if self.include_adj else in_features
        self.out_features = out_features
        self.fc = nn.Linear(self.in_features, self.out_features, bias=bias)

    def forward(self, feat, adj):
        feat_diff = (feat.unsqueeze(0).repeat(feat.shape[0], 1, 1) - feat.
            unsqueeze(1).repeat(1, feat.shape[0], 1)).abs()
        if self.include_adj:
            x = torch.cat((feat_diff, adj.unsqueeze(2)), 2).view(feat.shape
                [0] * feat.shape[0], -1)
        else:
            x = feat_diff.view(feat.shape[0] * feat.shape[0], -1)
        output = self.fc(x)
        return output

    def __repr__(self):
        return self.__class__.__name__ + ' (' + str(self.in_features
            ) + ' -> ' + str(self.out_features) + ')'


class GATLayer(nn.Module):
    """
    Simple GAT layer, similar to https://arxiv.org/abs/1710.10903
    """

    def __init__(self, in_features, out_features, dropout=0, alpha=0.2,
        concat=True):
        super(GATLayer, self).__init__()
        self.dropout = dropout
        self.in_features = in_features
        self.out_features = out_features
        self.alpha = alpha
        self.concat = concat
        self.W = nn.Parameter(torch.zeros(size=(in_features, out_features)))
        nn.init.xavier_uniform_(self.W.data, gain=1.414)
        self.a = nn.Parameter(torch.zeros(size=(out_features, 1)))
        nn.init.xavier_uniform_(self.a.data, gain=1.414)

    def forward(self, input, adj):
        h = torch.mm(input, self.W)
        h.size()[0]
        a_input = (h.unsqueeze(0).repeat(h.shape[0], 1, 1) - h.unsqueeze(1)
            .repeat(1, h.shape[0], 1)).abs()
        e = F.relu(torch.matmul(a_input, self.a).squeeze(2))
        zero_vec = -9000000000000000.0 * torch.ones_like(e)
        attention = torch.where(adj > 0, e, zero_vec)
        attention = F.softmax(attention, dim=1)
        attention = F.dropout(attention, self.dropout, training=self.training)
        h = torch.matmul(attention, h)
        if self.concat:
            return F.relu(h)
        else:
            return h

    def __repr__(self):
        return self.__class__.__name__ + ' (' + str(self.in_features
            ) + ' -> ' + str(self.out_features) + ')'


class GATNew(nn.Module):

    def __init__(self, nfeat, nclass):
        super(GATNew, self).__init__()
        self.gat1 = GATLayer(nfeat, 128)
        self.gat2 = GATLayer(128, 128)
        self.gat3 = GATLayer(128, 128)
        self.edge_gc = EdgeGCN(128, nclass, include_adj=False)

    def forward(self, input_0, input_1):
        primals_1 = self.gat1.W
        primals_3 = self.gat1.a
        primals_5 = self.gat2.W
        primals_6 = self.gat2.a
        primals_7 = self.gat3.W
        primals_8 = self.gat3.a
        primals_9 = self.edge_gc.fc.weight
        primals_10 = self.edge_gc.fc.bias
        primals_2 = 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]


Measured latency_ms: 0.024405