onnx-graphsurgeon: Graph.toposort() hits RecursionError when a graph has a local function and an If/Loop node
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 82/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- python
- Lĩnh vực
- machine-learning
Hướng nghiên cứu
Lỗi nằm ở get_used_funcs bên trong Graph.toposort trong onnx_graphsurgeon/ir/graph.py, nơi vòng lặp duyệt qua self.subgraphs() thay vì các subgraph của từng node được truyền vào. Hãy bắt đầu bằng việc chạy script tái hiện trong issue để tái hiện RecursionError, sau đó đọc get_used_funcs và get_hierarchy_level. Hoàn thành khi việc tái hiện không còn đệ quy nữa và một trường hợp lồng nhau, trong đó hàm A chỉ gọi B bên trong nhánh If, được sắp xếp thành [B, A], đồng thời các test hồi quy đã được thêm vào tests/ir/test_graph.py.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
Graph.toposort() in ONNX-GraphSurgeon fails with RecursionError when a graph has at least one local function and at least one node with a subgraph (If, Loop, Scan). The node doesn't need to call the function.
File "onnx_graphsurgeon/ir/graph.py", line 594, in get_used_funcs
inputs.update(get_used_funcs(subgraph.nodes))
[Previous line repeated 990 more times]
RecursionError: maximum recursion depth exceeded
The problem is in get_used_funcs in onnx_graphsurgeon/ir/graph.py, which toposort() uses to order functions (default mode="full"):
def get_used_funcs(nodes):
inputs = {}
for subgraph in self.subgraphs():
inputs.update(get_used_funcs(subgraph.nodes))
...
The loop uses self.subgraphs(), the subgraphs of the outer graph, not the subgraphs of the nodes that were passed in. self is the same on every recursive call, so each call loops over the same subgraphs again and the recursion never stops.
Even when the graph has no top-level subgraphs and it doesn't crash, the function never looks inside the subgraphs of the nodes it was given. If function A calls function B only from inside an If branch, toposort() misses the dependency and returns the functions in the order [A, B] instead of [B, A].
cleanup(), fold_constants() and export_onnx() work fine on the same graph. Only toposort() fails.
Environment
TensorRT Version: N/A, ONNX-GraphSurgeon only. Reproduced on main (16ed063, onnx-graphsurgeon 0.6.2) and on onnx-graphsurgeon 0.6.1 from PyPI. The code was added in the TensorRT 10.0 release.
NVIDIA GPU: N/A
NVIDIA Driver Version: N/A
CUDA Version: N/A
CUDNN Version: N/A
Operating System: Linux
Python Version (if applicable): 3.14 (onnx 1.23.2, numpy 2.5.4)
Tensorflow Version (if applicable): N/A
PyTorch Version (if applicable): N/A
Baremetal or Container (if so, version): Baremetal
Relevant Files
Model link: N/A, the script below builds the graph directly.
Steps To Reproduce
Commands or scripts:
import numpy as np
import onnx_graphsurgeon as gs
# Any local function
x = gs.Variable("x", dtype=np.float32, shape=(1,))
y = gs.Variable("y", dtype=np.float32, shape=(1,))
func = gs.Function("MyFunc", inputs=[x], outputs=[y], nodes=[gs.Node("Identity", inputs=[x], outputs=[y])], opset=18)
# Any node with a subgraph
def branch(name):
out = gs.Variable(f"{name}_out", dtype=np.float32, shape=(1,))
const = gs.Constant(f"{name}_const", np.ones((1,), dtype=np.float32))
return gs.Graph(nodes=[gs.Node("Identity", inputs=[const], outputs=[out])], outputs=[out])
cond = gs.Variable("cond", dtype=np.bool_, shape=())
if_out = gs.Variable("if_out", dtype=np.float32, shape=(1,))
if_node = gs.Node("If", inputs=[cond], outputs=[if_out],
attrs={"then_branch": branch("then"), "else_branch": branch("else")})
graph = gs.Graph(nodes=[if_node], inputs=[cond], outputs=[if_out], functions=[func], opset=18)
graph.toposort()
Full traceback:
File "onnx_graphsurgeon/ir/graph.py", line 636, in toposort
func, level=get_hierarchy_level(func)
File "onnx_graphsurgeon/ir/graph.py", line 611, in get_hierarchy_level
for inp in get_inputs(node_or_func)
File "onnx_graphsurgeon/ir/graph.py", line 604, in get_inputs
inputs = get_used_funcs(node_or_func.nodes).values()
File "onnx_graphsurgeon/ir/graph.py", line 594, in get_used_funcs
inputs.update(get_used_funcs(subgraph.nodes))
File "onnx_graphsurgeon/ir/graph.py", line 594, in get_used_funcs
inputs.update(get_used_funcs(subgraph.nodes))
[Previous line repeated 990 more times]
File "onnx_graphsurgeon/ir/graph.py", line 593, in get_used_funcs
for subgraph in self.subgraphs():
File "onnx_graphsurgeon/ir/graph.py", line 381, in subgraphs
for subgraph in node.subgraphs(recursive=recursive):
RecursionError: maximum recursion depth exceeded
Have you tried the latest release?: Yes. It reproduces on the latest onnx-graphsurgeon from PyPI (0.6.1) and on current main.
Can this model run on other frameworks?: N/A. This is a bug in graph manipulation, not in inference. onnx.checker.check_model accepts the exported graph.
Proposed fix
Recurse into the subgraphs of each node in nodes:
def get_used_funcs(nodes):
inputs = {}
for node in nodes:
for subgraph in node.subgraphs():
inputs.update(get_used_funcs(subgraph.nodes))
func_id = (node.domain, node.op)
if func_id in func_id_to_func:
inputs[func_id] = func_id_to_func[func_id]
return inputs
With this change, the repro above passes and the nested A/B case is sorted as [B, A]. I'd like to send a PR with this fix and regression tests in tests/ir/test_graph.py.
- Ngôn ngữ chính
- C++
- Star
- 13.4k
- Fork
- 2.4k
- Merge trung bình
- 15 phút
- Pull request đã merge (30 ngày)
- 2
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của NVIDIA/TensorRT
-
Module:Polygraphy
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
EmbLayerNormVarSeqlenPlugin: dangling pointer and out-of-bounds read during serializationCó thể đã có người làm @gabortar đã nhận 10 ngày trước. Đang mởModule:Plugins
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Polygraphy parse_meta accepts a shape with a missing bracketCó thể đã có người làm @SashaMIT đã nhận 23 ngày trước. Đang mởModule:Polygraphy
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
Maintainer thường phản hồi trong vòng 1 ngày
-
onnx_graphsurgeon: Constant with float8_e4m3fnuz values is exported as FLOAT8E4M3FNCó thể đã có người làm @luicarus đã nhận 32 ngày trước. Đang mởModule:GraphSurgeon
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Deprecate demoDiffusionCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mởModule:Demo
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của NVIDIA/TensorRT
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
bulletphysics/bullet3#4819 ·
-
[YSQL] Local-node initdb reads postgres.bki, but YB clusters bootstrap only from yb_postgres.bkiĐang mởarea/ysql kind/enhancement priority/low status/awaiting-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
yugabyte/yugabyte-db#35763 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[mimalloc] update to 3.6.0Đang mởcategory:port-update
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
Maintainer thường phản hồi trong vòng 2 ngày
-
MinGW build faillureĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
facebookresearch/faiss#5775 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Eval bug: minimax-m3: build_moe_ffn() drops the per-expert NVFP4 scale → garbled output (M3.1 Flash)Đang mởbug-unconfirmed
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày