import torch
import torch.nn as nn


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


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


def get_init_inputs():
    return [[], {'hidden_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
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_div_mul_sum_0(in_out_ptr0, in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex // 16 % 4
    x5 = xindex % 16
    x3 = xindex // 64
    x0 = xindex % 4
    x6 = xindex
    x4 = xindex // 4 % 16
    tmp0 = tl.load(in_ptr0 + (x5 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr1 + (x5 + 64 * x3), xmask, eviction_policy=
        'evict_last')
    tmp2 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (16 + x5 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr1 + (16 + x5 + 64 * x3), xmask, eviction_policy=
        'evict_last')
    tmp10 = tl.load(in_ptr0 + (32 + x5 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp11 = tl.load(in_ptr1 + (32 + x5 + 64 * x3), xmask, eviction_policy=
        'evict_last')
    tmp15 = tl.load(in_ptr0 + (48 + x5 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp16 = tl.load(in_ptr1 + (48 + x5 + 64 * x3), xmask, eviction_policy=
        'evict_last')
    tmp20 = tl.load(in_out_ptr0 + x6, xmask)
    tmp21 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr0 + (x0 + 16 * x4), xmask, eviction_policy=
        'evict_last')
    tmp24 = tl.load(in_ptr0 + (4 + x0 + 16 * x4), xmask, eviction_policy=
        'evict_last')
    tmp26 = tl.load(in_ptr0 + (8 + x0 + 16 * x4), xmask, eviction_policy=
        'evict_last')
    tmp28 = tl.load(in_ptr0 + (12 + x0 + 16 * x4), xmask, eviction_policy=
        'evict_last')
    tmp3 = tmp1 + tmp2
    tmp4 = tmp0 * tmp3
    tmp7 = tmp6 + tmp2
    tmp8 = tmp5 * tmp7
    tmp9 = tmp4 + tmp8
    tmp12 = tmp11 + tmp2
    tmp13 = tmp10 * tmp12
    tmp14 = tmp9 + tmp13
    tmp17 = tmp16 + tmp2
    tmp18 = tmp15 * tmp17
    tmp19 = tmp14 + tmp18
    tmp22 = tmp20 + tmp21
    tmp25 = tmp23 + tmp24
    tmp27 = tmp25 + tmp26
    tmp29 = tmp27 + tmp28
    tmp30 = 1e-20
    tmp31 = tmp29 + tmp30
    tmp32 = tmp19 / tmp31
    tmp33 = tmp22 + tmp32
    tl.store(in_out_ptr0 + x6, tmp33, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = 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, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    assert_size_stride(primals_5, (4,), (1,))
    assert_size_stride(primals_6, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0)
        del primals_1
        buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1)
        del primals_4
        buf3 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf0
        get_raw_stream(0)
        triton_poi_fused_add_div_mul_sum_0[grid(256)](buf3, primals_6, buf1,
            primals_5, primals_2, 256, XBLOCK=128, num_warps=4, num_stages=1)
        del buf1
        del primals_2
        del primals_5
    return buf3, primals_6, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0)


class NodeFeaturesNew(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(NodeFeaturesNew, 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, input_0, input_1):
        primals_1 = self.U.weight
        primals_2 = self.U.bias
        primals_4 = self.V.weight
        primals_5 = self.V.bias
        primals_3 = input_0
        primals_6 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6])
        return output[0]
