Repository navigation
Description
🐛 Describe the bug
With the graph count given (so no data-dependent int() happens in PyG), capturing torch_geometric.nn.radius_graph
with torch.export.export still fails in strict and non-strict mode:
radius_graph(batch_size=2) strict=True: FAIL UnsupportedOperatorException: pyg.radius.default
radius_graph(batch_size=2) strict=False: FAIL UnsupportedOperatorException: pyg.radius.default
Export and torch.compile trace operators with FakeTensors, so a C++ custom operator needs a FakeTensor kernel
(torch.library.register_fake), per the PyTorch custom C++ operators tutorial. The output size of radius (the number
of neighbour pairs) depends on the input values; the torch.library.register_fake documentation covers this case with
ctx = torch.library.get_ctx(); n = ctx.new_dynamic_size() (its custom_nonzero example). A fake kernel returning a
[2, n] int64 tensor on the input's device would make the op traceable.
This blocks exporting PyG's SchNet, DimeNet, DimeNetPlusPlus and ViSNet (PyG side: pyg-team/pytorch_geometric#10915).
Reproducer (the radius_graph(batch_size=2) lines):
"""torch.export of the 3D models (SchNet, DimeNet, DimeNet++, ViSNet) fails in radius_graph.
1. radius_graph(pos, r, batch) without batch_size calls _batch_to_ptr -> index2ptr with
size = int(batch.max()) + 1, a data-dependent specialization. The models' interaction-graph
modules call radius_graph without batch_size, so callers cannot avoid it.
2. With batch_size given, the remaining capture runs torch.ops.pyg.radius (pyg-lib).
Needs pyg-lib with the radius op (torch_geometric.typing.WITH_RADIUS).
"""
import importlib.metadata as md
import logging
import warnings
import torch
import torch_geometric.typing as pyg_typing
from torch.export import Dim
from torch_geometric.index import index2ptr
from torch_geometric.nn import radius_graph
from torch_geometric.nn.models import SchNet
warnings.filterwarnings("ignore")
logging.disable(logging.CRITICAL) # torch.export logs the partial graph on failure
versions = [f"{p} {md.version(p)}" for p in ("torch", "torch-geometric")]
try:
versions.append(f"pyg-lib {md.version('pyg-lib')}")
except md.PackageNotFoundError:
versions.append("pyg-lib not installed")
print(*versions, f"WITH_RADIUS={pyg_typing.WITH_RADIUS}", sep=", ")
def first(exc):
while exc.__cause__ is not None:
exc = exc.__cause__
return f"{type(exc).__name__}: {str(exc).splitlines()[0]}"
def capture(name, module, args, dynamic_shapes):
for strict in (True, False):
try:
torch.export.export(module, args, dynamic_shapes=dynamic_shapes, strict=strict)
print(f"{name} strict={strict}: OK")
except Exception as exc:
print(f"{name} strict={strict}: FAIL {first(exc)}")
class Index2Ptr(torch.nn.Module):
def __init__(self, size):
super().__init__()
self.size = size
def forward(self, batch):
return index2ptr(batch, self.size)
class Radius(torch.nn.Module):
def __init__(self, batch_size):
super().__init__()
self.batch_size = batch_size
def forward(self, pos, batch):
return radius_graph(pos, r=5.0, batch=batch, batch_size=self.batch_size)
torch.manual_seed(0)
nodes = Dim("num_nodes")
z = torch.randint(1, 10, (6,))
pos = torch.randn(6, 3)
batch = torch.tensor([0, 0, 0, 1, 1, 1])
capture("index2ptr(batch, size=None)", Index2Ptr(None), (batch,), ({0: nodes},))
capture("index2ptr(batch, size=2)", Index2Ptr(2), (batch,), ({0: nodes},))
if pyg_typing.WITH_RADIUS:
capture("radius_graph(batch_size=None)", Radius(None), (pos, batch), ({0: nodes}, {0: nodes}))
capture("radius_graph(batch_size=2)", Radius(2), (pos, batch), ({0: nodes}, {0: nodes}))
model = SchNet(hidden_channels=16, num_filters=16, num_interactions=2).eval()
capture("SchNet", model, (z, pos, batch), ({0: nodes}, {0: nodes}, {0: nodes}))
else:
print("radius_graph / SchNet: SKIPPED (no pyg-lib radius op)")Output (CPU):
torch 2.12.1+cpu, torch-geometric 2.8.1, pyg-lib 0.9.0+pt212cpu, WITH_RADIUS=True
index2ptr(batch, size=None) strict=True: FAIL GuardOnDataDependentSymNode: Could not extract specialized integer from data-dependent expression u0 + 1 (unhinted: u0 + 1). (Size-like symbols: none)
index2ptr(batch, size=None) strict=False: FAIL GuardOnDataDependentSymNode: Could not extract specialized integer from data-dependent expression u0 (unhinted: u0). (Size-like symbols: none)
index2ptr(batch, size=2) strict=True: OK
index2ptr(batch, size=2) strict=False: OK
radius_graph(batch_size=None) strict=True: FAIL GuardOnDataDependentSymNode: Could not extract specialized integer from data-dependent expression u0 + 1 (unhinted: u0 + 1). (Size-like symbols: none)
radius_graph(batch_size=None) strict=False: FAIL GuardOnDataDependentSymNode: Could not extract specialized integer from data-dependent expression u0 (unhinted: u0). (Size-like symbols: none)
radius_graph(batch_size=2) strict=True: FAIL UnsupportedOperatorException: pyg.radius.default
radius_graph(batch_size=2) strict=False: FAIL UnsupportedOperatorException: pyg.radius.default
SchNet strict=True: FAIL GuardOnDataDependentSymNode: Could not extract specialized integer from data-dependent expression u0 + 1 (unhinted: u0 + 1). (Size-like symbols: none)
SchNet strict=False: FAIL GuardOnDataDependentSymNode: Could not extract specialized integer from data-dependent expression u0 (unhinted: u0). (Size-like symbols: none)
Environment
pyg-lib 0.9.0+pt212cpu, torch 2.12.1+cpu, torch-geometric 2.8.1; Python 3.12; Linux x86_64.
Activity
Peppino58 commentedon Nov 25, 2026
Hi I opened #742 which should address part of the issue, several operators are still not supported and the SchNet export error remains