Skip to content

Commit fd6a3b2

Browse files
authored
CG-10395: Identify and Fix Import Loops Pytorch Repo (#32)
adding pytorch example
1 parent 63d83da commit fd6a3b2

File tree

3 files changed

+601
-4
lines changed

3 files changed

+601
-4
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ repos:
2626
types_or: [python, jupyter, pyi]
2727
exclude: *exclude_patterns
2828

29-
- repo: https://github.com/kynan/nbstripout
30-
rev: 0.8.1
31-
hooks:
32-
- id: nbstripout
29+
# - repo: https://github.com/kynan/nbstripout
30+
# rev: 0.8.1
31+
# hooks:
32+
# - id: nbstripout
3333

3434
- repo: https://github.com/pre-commit/pre-commit-hooks
3535
rev: v5.0.0
@@ -62,6 +62,8 @@ repos:
6262
additional_dependencies:
6363
- tomli
6464
files: ".*"
65+
exclude: ".*\\.ipynb"
66+
6567

6668
- repo: https://github.com/astral-sh/uv-pre-commit
6769
rev: "0.5.24"

examples/removing_import_loops_in_pytorch/import_loops.ipynb

Lines changed: 530 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
def visualize_graph(graph):
2+
"""
3+
Visualize SCC using Graphviz with a strictly enforced circular layout
4+
"""
5+
import pygraphviz as pgv
6+
7+
# Create a new pygraphviz graph directly (instead of converting)
8+
A = pgv.AGraph(strict=False, directed=True)
9+
10+
# Set graph attributes for strict circular layout
11+
A.graph_attr.update(
12+
{
13+
"layout": "circo",
14+
"root": "circle",
15+
"splines": "curved",
16+
"overlap": "false",
17+
"sep": "+25,25",
18+
"pad": "0.5",
19+
"ranksep": "2.0",
20+
"nodesep": "0.8",
21+
"mindist": "2.0",
22+
"start": "regular",
23+
"ordering": "out",
24+
"concentrate": "false",
25+
"ratio": "1.0",
26+
}
27+
)
28+
29+
# Set node attributes for consistent sizing
30+
A.node_attr.update({"shape": "circle", "fixedsize": "true", "width": "1.5", "height": "1.5", "style": "filled", "fillcolor": "lightblue", "fontsize": "11", "fontname": "Arial"})
31+
32+
# Set default edge attributes
33+
A.edge_attr.update({"penwidth": "1.5", "arrowsize": "0.8", "len": "2.0", "weight": "1", "dir": "forward"})
34+
35+
# Add nodes first
36+
for node in graph.nodes():
37+
short_name = node.split("/")[-1]
38+
A.add_node(node, label=short_name)
39+
40+
# Add edges with their attributes
41+
for u, v, key, data in graph.edges(data=True, keys=True):
42+
# Create a unique key for this edge
43+
edge_key = f"{u}_{v}_{key}"
44+
45+
# Set edge attributes based on the data
46+
edge_attrs = {
47+
"key": edge_key, # Ensure unique edge
48+
"color": "red" if data.get("color") == "red" else "#666666",
49+
"style": "dashed" if data.get("color") == "red" else "solid",
50+
"label": "dynamic" if data.get("color") == "red" else "",
51+
"fontcolor": "red" if data.get("color") == "red" else "#666666",
52+
"fontsize": "10",
53+
}
54+
55+
A.add_edge(u, v, **edge_attrs)
56+
57+
# Force circo layout with specific settings
58+
A.layout(prog="circo")
59+
60+
# Save with a larger size
61+
A.draw("import_cycle.png", format="png", prog="circo", args="-Gsize=12,12!")
62+
63+
from IPython.display import Image
64+
65+
return Image("import_cycle.png")

0 commit comments

Comments
 (0)