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 libdevice
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_add_mul_sigmoid_sum_0(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x3 = xindex // 4
    x1 = xindex // 4 % 4
    x4 = xindex % 16
    x5 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * x3), xmask)
    tmp1 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (4 + x0 + 16 * x3), xmask)
    tmp9 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr0 + (8 + x0 + 16 * x3), xmask)
    tmp16 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp22 = tl.load(in_ptr0 + (12 + x0 + 16 * x3), xmask)
    tmp23 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp5 = tl.sigmoid(tmp4)
    tmp7 = tmp5 * tmp6
    tmp10 = tmp8 + tmp9
    tmp11 = tmp10 + tmp3
    tmp12 = tl.sigmoid(tmp11)
    tmp13 = tmp12 * tmp6
    tmp14 = tmp7 + tmp13
    tmp17 = tmp15 + tmp16
    tmp18 = tmp17 + tmp3
    tmp19 = tl.sigmoid(tmp18)
    tmp20 = tmp19 * tmp6
    tmp21 = tmp14 + tmp20
    tmp24 = tmp22 + tmp23
    tmp25 = tmp24 + tmp3
    tmp26 = tl.sigmoid(tmp25)
    tmp27 = tmp26 * tmp6
    tmp28 = tmp21 + tmp27
    tl.store(out_ptr0 + x5, tmp28, xmask)


@triton.jit
def triton_per_fused__native_batch_norm_legit_clone_1(in_out_ptr0, in_ptr0,
    in_ptr1, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r4 = rindex
    x0 = xindex
    r5 = rindex % 16
    r2 = rindex // 4 % 4
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * r4), xmask, other=0.0)
    tmp1 = tl.load(in_ptr1 + r5, None, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + (x0 + 4 * r2), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK])
    tl.where(xmask, tmp5, 0)
    tmp8 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp10 = tl.where(xmask, tmp8, 0)
    tmp11 = tl.sum(tmp10, 1)[:, None]
    tmp12 = tl.full([XBLOCK, 1], 64, tl.int32)
    tmp13 = tmp12.to(tl.float32)
    tmp14 = tmp11 / tmp13
    tmp15 = tmp5 - tmp14
    tmp16 = tmp15 * tmp15
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp19 = tl.where(xmask, tmp17, 0)
    tmp20 = tl.sum(tmp19, 1)[:, None]
    tmp21 = 64.0
    tmp22 = tmp20 / tmp21
    tmp23 = 1e-05
    tmp24 = tmp22 + tmp23
    tmp25 = libdevice.rsqrt(tmp24)
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp25, xmask)
    tl.store(out_ptr0 + x0, tmp14, xmask)


@triton.jit
def triton_per_fused__native_batch_norm_legit_clone_2(in_out_ptr0, in_ptr0,
    in_ptr1, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    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, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex % 4
    x0 = xindex
    r3 = rindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * r1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp1 = tl.load(in_ptr1 + (x0 + 4 * r3), xmask, other=0.0)
    tmp2 = tmp0 + tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tl.where(xmask, tmp3, 0)
    tmp6 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
    tmp8 = tl.where(xmask, tmp6, 0)
    tmp9 = tl.sum(tmp8, 1)[:, None]
    tmp10 = tl.full([XBLOCK, 1], 16, tl.int32)
    tmp11 = tmp10.to(tl.float32)
    tmp12 = tmp9 / tmp11
    tmp13 = tmp3 - tmp12
    tmp14 = tmp13 * tmp13
    tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK])
    tmp17 = tl.where(xmask, tmp15, 0)
    tmp18 = tl.sum(tmp17, 1)[:, None]
    tmp19 = 16.0
    tmp20 = tmp18 / tmp19
    tmp21 = 1e-05
    tmp22 = tmp20 + tmp21
    tmp23 = libdevice.rsqrt(tmp22)
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp23, xmask)
    tl.store(out_ptr0 + x0, tmp12, xmask)


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


@triton.jit
def triton_poi_fused_add_clone_relu_4(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    in_ptr4, in_ptr5, in_ptr6, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x4 = xindex
    x5 = xindex // 4 % 16
    x0 = xindex % 4
    x2 = xindex // 16 % 4
    tmp0 = tl.load(in_ptr0 + x4, xmask)
    tmp1 = tl.load(in_ptr1 + x4, xmask)
    tmp2 = tl.load(in_ptr2 + x5, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr2 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp6 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr5 + x0, xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr6 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 - tmp6
    tmp9 = tmp7 * tmp8
    tmp11 = tmp9 * tmp10
    tmp13 = tmp11 + tmp12
    tmp14 = tl.full([1], 0, tl.int32)
    tmp15 = triton_helpers.maximum(tmp14, tmp13)
    tmp16 = tmp0 + tmp15
    tl.store(out_ptr0 + x4, tmp16, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9, primals_10, primals_11, primals_12,
        primals_13, primals_14) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    assert_size_stride(primals_4, (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,))
    assert_size_stride(primals_11, (4,), (1,))
    assert_size_stride(primals_12, (4,), (1,))
    assert_size_stride(primals_13, (4,), (1,))
    assert_size_stride(primals_14, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_4, reinterpret_tensor(primals_1, (64, 
            4), (4, 1), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0
            ), alpha=1, beta=1, out=buf0)
        del primals_3
        del primals_4
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_6, primals_2, reinterpret_tensor(
            primals_5, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf1)
        del primals_5
        del primals_6
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_8, primals_2, reinterpret_tensor(
            primals_7, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf2)
        del primals_7
        del primals_8
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_10, primals_2, reinterpret_tensor(
            primals_9, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf3)
        del primals_10
        del primals_9
        buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_mul_sigmoid_sum_0[grid(64)](buf0, buf1, buf3,
            buf4, 64, XBLOCK=64, num_warps=1, num_stages=1)
        buf5 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 1, 1), torch.float32)
        buf6 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 4, 4), torch.float32)
        buf8 = reinterpret_tensor(buf6, (1, 4, 1, 1), (4, 1, 1, 1), 0)
        del buf6
        triton_per_fused__native_batch_norm_legit_clone_1[grid(4)](buf8,
            buf0, buf1, buf5, 4, 64, XBLOCK=1, num_warps=2, num_stages=1)
        buf9 = empty_strided_cuda((1, 4, 1), (4, 1, 1), torch.float32)
        buf10 = empty_strided_cuda((1, 4, 1), (4, 1, 4), torch.float32)
        buf12 = reinterpret_tensor(buf10, (1, 4, 1), (4, 1, 1), 0)
        del buf10
        triton_per_fused__native_batch_norm_legit_clone_2[grid(4)](buf12,
            buf2, buf4, buf9, 4, 16, XBLOCK=1, num_warps=2, num_stages=1)
        buf13 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_add_clone_relu_3[grid(64)](primals_2, buf2, buf4,
            buf9, buf12, primals_13, primals_14, buf13, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del buf4
        buf14 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_add_clone_relu_4[grid(256)](primals_1, buf0, buf1,
            buf5, buf8, primals_11, primals_12, buf14, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
    return (buf13, buf14, primals_2, primals_11, primals_12, primals_13,
        primals_14, reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), buf0,
        buf1, buf2, buf3, buf5, buf8, buf9, buf12)


class BatchNormNode(nn.Module):
    """Batch normalization for node features.
    """

    def __init__(self, hidden_dim):
        super(BatchNormNode, self).__init__()
        self.batch_norm = nn.BatchNorm1d(hidden_dim, track_running_stats=False)

    def forward(self, x):
        """
        Args:
            x: Node features (batch_size, num_nodes, hidden_dim)

        Returns:
            x_bn: Node features after batch normalization (batch_size, num_nodes, hidden_dim)
        """
        x_trans = x.transpose(1, 2).contiguous()
        x_trans_bn = self.batch_norm(x_trans)
        x_bn = x_trans_bn.transpose(1, 2).contiguous()
        return x_bn


class BatchNormEdge(nn.Module):
    """Batch normalization for edge features.
    """

    def __init__(self, hidden_dim):
        super(BatchNormEdge, self).__init__()
        self.batch_norm = nn.BatchNorm2d(hidden_dim, track_running_stats=False)

    def forward(self, e):
        """
        Args:
            e: Edge features (batch_size, num_nodes, num_nodes, hidden_dim)

        Returns:
            e_bn: Edge features after batch normalization (batch_size, num_nodes, num_nodes, hidden_dim)
        """
        e_trans = e.transpose(1, 3).contiguous()
        e_trans_bn = self.batch_norm(e_trans)
        e_bn = e_trans_bn.transpose(1, 3).contiguous()
        return e_bn


class NodeFeatures(nn.Module):
    """Convnet features for nodes.
    
    Using `sum` aggregation:
        x_i = U*x_i +  sum_j [ gate_ij * (V*x_j) ]
    
    Using `mean` aggregation:
        x_i = U*x_i + ( sum_j [ gate_ij * (V*x_j) ] / sum_j [ gate_ij] )
    """

    def __init__(self, hidden_dim, aggregation='mean'):
        super(NodeFeatures, self).__init__()
        self.aggregation = aggregation
        self.U = nn.Linear(hidden_dim, hidden_dim, True)
        self.V = nn.Linear(hidden_dim, hidden_dim, True)

    def forward(self, x, edge_gate):
        """
        Args:
            x: Node features (batch_size, num_nodes, hidden_dim)
            edge_gate: Edge gate values (batch_size, num_nodes, num_nodes, hidden_dim)

        Returns:
            x_new: Convolved node features (batch_size, num_nodes, hidden_dim)
        """
        Ux = self.U(x)
        Vx = self.V(x)
        Vx = Vx.unsqueeze(1)
        gateVx = edge_gate * Vx
        if self.aggregation == 'mean':
            x_new = Ux + torch.sum(gateVx, dim=2) / (1e-20 + torch.sum(
                edge_gate, dim=2))
        elif self.aggregation == 'sum':
            x_new = Ux + torch.sum(gateVx, dim=2)
        return x_new


class EdgeFeatures(nn.Module):
    """Convnet features for edges.

    e_ij = U*e_ij + V*(x_i + x_j)
    """

    def __init__(self, hidden_dim):
        super(EdgeFeatures, self).__init__()
        self.U = nn.Linear(hidden_dim, hidden_dim, True)
        self.V = nn.Linear(hidden_dim, hidden_dim, True)

    def forward(self, x, e):
        """
        Args:
            x: Node features (batch_size, num_nodes, hidden_dim)
            e: Edge features (batch_size, num_nodes, num_nodes, hidden_dim)

        Returns:
            e_new: Convolved edge features (batch_size, num_nodes, num_nodes, hidden_dim)
        """
        Ue = self.U(e)
        Vx = self.V(x)
        Wx = Vx.unsqueeze(1)
        Vx = Vx.unsqueeze(2)
        e_new = Ue + Vx + Wx
        return e_new


class ResidualGatedGCNLayerNew(nn.Module):
    """Convnet layer with gating and residual connection.
    """

    def __init__(self, hidden_dim, aggregation='sum'):
        super(ResidualGatedGCNLayerNew, self).__init__()
        self.node_feat = NodeFeatures(hidden_dim, aggregation)
        self.edge_feat = EdgeFeatures(hidden_dim)
        self.bn_node = BatchNormNode(hidden_dim)
        self.bn_edge = BatchNormEdge(hidden_dim)

    def forward(self, input_0, input_1):
        primals_2 = self.node_feat.U.weight
        primals_4 = self.node_feat.U.bias
        primals_3 = self.node_feat.V.weight
        primals_6 = self.node_feat.V.bias
        primals_5 = self.edge_feat.U.weight
        primals_8 = self.edge_feat.U.bias
        primals_7 = self.edge_feat.V.weight
        primals_10 = self.edge_feat.V.bias
        primals_11 = self.bn_node.batch_norm.weight
        primals_12 = self.bn_node.batch_norm.bias
        primals_13 = self.bn_edge.batch_norm.weight
        primals_14 = self.bn_edge.batch_norm.bias
        primals_9 = input_0
        primals_1 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9,
            primals_10, primals_11, primals_12, primals_13, primals_14])
        return output[0], output[1]


Measured latency_ms: 0.034371