Deep Graph Library (DGL)
raw JSON → 2.4.0 verified Mon Apr 27 auth: no python
An open-source Python library for deep learning on graphs, supporting PyTorch, TensorFlow, and Apache MXNet. Currently at version 2.4.0, with releases every few months. Focuses on graph neural networks (GNNs) and scalable graph computation.
pip install dgl Common errors
error ImportError: cannot import name 'SAGEConv' from 'dgl' ↓
cause SAGEConv is in dgl.nn submodule, not top-level.
fix
Use 'from dgl.nn import SAGEConv'.
error AttributeError: module 'dgl' has no attribute 'graph' ↓
cause Incorrect import or outdated DGL version (pre-1.0).
fix
Ensure you have DGL >=1.0 and use 'dgl.graph(...)' (lowercase g).
error RuntimeError: expected scalar type Float but found Half ↓
cause Mixing float16 and float32 tensors in graph operations.
fix
Ensure all tensors have consistent dtype (use .float() or .half() accordingly).
Warnings
deprecated Support for PyTorch 1.13 and earlier versions is deprecated. DGL 2.1+ requires PyTorch >=2.0.0. ↓
fix Upgrade PyTorch to 2.1.0 or later (or as recommended by DGL version).
gotcha numpy 2.0.0 is not fully compatible with DGL <2.4.0. Pin numpy<2.0.0. ↓
fix pip install 'numpy<2.0.0'
breaking In DGL 2.4, distributed module is not imported by default. Users must import dgl.distributed manually. ↓
fix import dgl.distributed before using distributed features.
breaking HeteroItemSet replaces ItemSetDict. ItemSetDict is deprecated and will be removed. ↓
fix Use HeteroItemSet instead of ItemSetDict.
deprecated CUDA 11.6 support dropped in DGL 2.1.0. Supported CUDA: 11.7, 11.8, 12.1. ↓
fix Upgrade CUDA to 11.7 or later.
breaking In DGL 2.2 GraphBolt, MiniBatch now uses 'seeds' attribute instead of 'seed_nodes' and 'node_pairs'. ↓
fix Access 'seeds' attribute on MiniBatch objects.
Install
pip install dgl -f https://data.dgl.ai/wheels/cu118/repo.html pip install dgl -f https://data.dgl.ai/wheels/cu121/repo.html Imports
- dgl.graph wrong
import dgl.graphcorrectimport dgl - dgl.nn.SAGEConv wrong
from dgl import SAGEConvcorrectimport dgl; from dgl.nn import SAGEConv - dgl.dataloading.DataLoader wrong
from dgl import DataLoadercorrectfrom dgl.dataloading import DataLoader
Quickstart
import dgl
import torch
# Create a simple graph with 3 nodes and 2 edges
g = dgl.graph(([0, 1], [1, 2]), num_nodes=3)
# Assign node features
x = torch.randn(3, 5)
# Create a Graph Neural Network layer
from dgl.nn import SAGEConv
conv = SAGEConv(5, 2, 'mean')
# Forward pass
h = conv(g, x)
print(h.shape) # torch.Size([3, 2])