Berlin Tech Meetup: The Future of Relational Foundation Models, Systems, and Real-World Applications

Register now:
PyG/Layer8 min read

DirGNNConv: Respecting Edge Direction in Graph Neural Networks

Most GNN layers treat edges as undirected, losing information about who points to whom. DirGNNConv separates incoming from outgoing messages and learns how to balance them. It is especially effective on heterophilic graphs where directional patterns carry important signal.

PyTorch Geometric

TL;DR

  • 1DirGNNConv separates incoming and outgoing edges, running a base GNN layer on each direction independently and combining with a learnable weight alpha.
  • 2Wraps any base layer (GCNConv, GATConv, SAGEConv). Adds directionality to any existing GNN architecture with a single wrapper.
  • 3Especially effective on heterophilic graphs where connected nodes have different labels. Directional information helps distinguish different node roles.
  • 4The learnable alpha parameter automatically discovers the optimal balance: alpha near 1 means incoming edges dominate, near 0 means outgoing.
  • 5KumoRFM naturally handles directed relationships in database schemas (customer purchases product is directional) through its type-aware message passing.

Original Paper

Edge Directionality Improves Learning on Heterophilic Graphs

Rossi et al. (2023). LoG 2023

Read paper →

What DirGNNConv does

DirGNNConv is a meta-layer that adds directionality to any GNN:

  1. Split edges into incoming (j → i) and outgoing (i → j)
  2. Run the base layer separately on incoming and outgoing subgraphs
  3. Combine: h_i' = alpha * h_in + (1 - alpha) * h_out, where alpha is learnable

The math (simplified)

DirGNNConv formula
# Separate message passing by direction
h_i^in  = Conv(x, edge_index_in)   # messages from predecessors
h_i^out = Conv(x, edge_index_out)  # messages from successors

# Learnable directional balance
h_i' = alpha · h_i^in + (1 - alpha) · h_i^out

Where:
  edge_index_in  = edges pointing TO node i
  edge_index_out = edges pointing FROM node i
  Conv           = any base GNN layer (GCN, GAT, SAGE, etc.)
  alpha          = learnable scalar in [0, 1]

When alpha = 0.5: symmetric (equivalent to undirected)
When alpha = 1.0: only incoming edges (who references me?)
When alpha = 0.0: only outgoing edges (who do I reference?)

The learned alpha reveals the directional bias: in citation networks, incoming citations (being cited) typically matter more than outgoing ones.

PyG implementation

dirgnn_model.py
import torch
import torch.nn.functional as F
from torch_geometric.nn import DirGNNConv, GCNConv

class DirGNN(torch.nn.Module):
    def __init__(self, in_channels, hidden, out_channels):
        super().__init__()
        # Wrap GCNConv with directional splitting
        self.conv1 = DirGNNConv(GCNConv(in_channels, hidden))
        self.conv2 = DirGNNConv(GCNConv(hidden, out_channels))

    def forward(self, x, edge_index):
        x = F.relu(self.conv1(x, edge_index))
        x = self.conv2(x, edge_index)
        return x

# Works with any base layer
from torch_geometric.nn import GATConv, SAGEConv

dir_gat = DirGNNConv(GATConv(64, 32, heads=4))
dir_sage = DirGNNConv(SAGEConv(64, 32))

model = DirGNN(dataset.num_features, 64, dataset.num_classes)

DirGNNConv wraps any MessagePassing layer. The base layer handles message computation; DirGNNConv handles directional splitting and combination.

When to use DirGNNConv

  • Directed graphs with semantic direction. Citation networks (citing vs being cited), web graphs (linking vs being linked), follower networks (following vs being followed).
  • Heterophilic graphs. When connected nodes tend to have different labels, directional information helps distinguish different roles (e.g., hub vs authority in web graphs).
  • When symmetrization hurts. If converting your directed graph to undirected degrades performance, DirGNNConv recovers the lost directional signal.
  • Adding directionality to existing models. Wrap your existing GCNConv/GATConv model with DirGNNConv to see if directional information helps, without rewriting the architecture.

When not to use DirGNNConv

  • Naturally undirected graphs. Molecular bonds, co-purchase edges, and co-authorship edges are inherently symmetric. Direction adds complexity without benefit.
  • When symmetrization already works well. On many benchmarks, undirected GCN performs nearly as well as directional models. Check if directionality helps before committing.

Frequently asked questions

What is DirGNNConv in PyTorch Geometric?

DirGNNConv implements the Directional GNN from Rossi et al. (2023). It separates incoming and outgoing edges during message passing, applying different transformations to messages from predecessors (nodes pointing to you) and successors (nodes you point to). This captures directional information that undirected GNNs lose.

Why does edge direction matter?

In many graphs, edge direction carries important semantic information. In a citation network, citing a paper (outgoing) is different from being cited (incoming). In a web graph, linking to a page is different from being linked to. Standard GCNConv symmetrizes the adjacency matrix, losing this directional signal.

How does DirGNNConv handle directed edges?

DirGNNConv wraps any base layer (GCNConv, GATConv, etc.) and runs it twice: once on incoming edges and once on outgoing edges. The two outputs are combined with a learnable weight alpha: h_i' = alpha * h_in + (1-alpha) * h_out. This learns the optimal balance between incoming and outgoing information.

What are heterophilic graphs?

Heterophilic graphs are graphs where connected nodes tend to have different labels or features (unlike homophilic graphs where neighbors are similar). Examples: web graphs (different types of pages link to each other), some biological networks, transaction networks. DirGNNConv excels on these because directional information helps distinguish different roles.

Can DirGNNConv wrap any GNN layer?

Yes. DirGNNConv is a meta-layer that wraps any PyG message-passing layer. You can use GCNConv, GATConv, SAGEConv, or any other layer as the base. DirGNNConv handles the directional splitting and combination, while the base layer handles the actual message passing.

Learn more about graph ML

PyTorch Geometric is the open-source foundation for graph neural networks. Explore more layers, concepts, and production patterns.