convert_pt2e crashes in DuplicateDQPass — GraphModule rebuild drops submodules only referenced by other submodules' internal graphs
Description
When calling convert_pt2e(prepared_model) on a model where one submodule's internal graph references another submodule (common in complex architectures like Qwen2.5), the conversion crashes inside DuplicateDQPass with:
RuntimeError: Node submod_3 target submod_1 references nonexistent attribute submod_1 of ...
Steps to reproduce
class Inner(torch.nn.Module):
"""Leaf module with a parameter — will become 'submod_1'."""
def __init__(self):
super().__init__()
self.weight = torch.nn.Parameter(torch.randn(4, 4))
def forward(self, x):
return torch.nn.functional.linear(x, self.weight)
class Block(torch.nn.Module):
"""Composite module that calls Inner — will become 'submod_3'."""
def __init__(self, inner: Inner):
super().__init__()
self.inner = inner # registered as 'submod_1'
def forward(self, x):
return self.inner(x)
def build_buggy_model():
"""
Build a model whose structure mimics the failure case:
main graph ──calls──▶ submod_3 (Block)
submod_3.graph ──calls──▶ submod_1 (Inner)
submod_1 is NOT directly called by the main graph, only by submod_3's
internal graph.
"""
inner = Inner()
block = Block(inner)
model = torch.nn.Module()
model.add_module("submod_1", inner) # registered on top level
model.add_module("submod_3", block) # registered on top level
# Build a main graph that ONLY calls submod_3
graph = Graph()
x = graph.placeholder("x")
out = graph.call_module("submod_3", args=(x,))
graph.output(out)
model.graph = graph
return model
Environment
torch version:2.9.0+cu128
torchao:0.17.0
Hardware:cpu
OS:ubuntu
Mind if I pick this up? I had a look and managed to repro it.
It happens when a while_loop body contains another while_loop. The inner loop's cond/body subgraphs only get referenced from inside the outer body's graph, and the delete_all_unused_submodules() call near the end of convert() only looks at the top-level graph, so it deletes them, and then DuplicateDQPass blows up linting the leftover reference:
RuntimeError: Node while_loop_cond_graph_0 target while_loop_cond_graph_0
references nonexistent attribute while_loop_cond_graph_0
Small repro (CPU, XNNPACKQuantizer):
class M(nn.Module):
def __init__(self): super().__init__(); self.linear = nn.Linear(5,5)
def forward(self, x):
def ib(j,y): return j+1, self.linear(y)
def ob(i,x):
_,y = while_loop(lambda j,y: j<2, ib, (torch.tensor(0), x)); return i+1, y
_,r = while_loop(lambda i,x: i<3, ob, (torch.tensor(0), x)); return r
# export -> prepare_pt2e -> convert_pt2e => crash
I've got a fix that makes the cleanup recursion-aware (keeps submodules referenced from nested subgraphs) plus a regression test, and the pt2e convert suite still passes. One question before I open a PR though, since delete_all_unused_submodules is really a torch.fx thing, do you want this worked around here in torchao or fixed upstream? Happy either way, just let me know and I'll send it.
cc @jerryzh168 — looks related to the while_loop/scan support you added in #3916 / #3882.
Hi @atharvjairath yes please feel free to submit a PR, thanks!