-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_plotter.py
More file actions
224 lines (183 loc) · 7.91 KB
/
Copy pathgraph_plotter.py
File metadata and controls
224 lines (183 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""
NeRArchSim Graph Plotter
Utilities for visualizing operator graphs from neural rendering pipelines.
Supports DAG visualization with operator types, hardware assignments, and data flow.
"""
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import networkx as nx
import numpy as np
from typing import Dict, List, Tuple, Optional, Any
from pathlib import Path
import json
class OperatorGraphPlotter:
"""Visualizes operator graphs with hardware assignments and data flow."""
# Color scheme for different operator types
OPERATOR_COLORS = {
'HASH_ENCODE': '#FF6B6B', # Red
'FIELD_COMPUTATION': '#4ECDC4', # Teal
'SAMPLING': '#45B7D1', # Blue
'BLENDING': '#96CEB4', # Green
'VOLUME_RENDERING': '#FFEAA7', # Yellow
'MLP': '#DDA0DD', # Plum
'ATTENTION': '#F39C12', # Orange
'UNKNOWN': '#95A5A6' # Gray
}
# Hardware unit shapes
HW_SHAPES = {
'systolic_array': 's', # Square
'hash_unit': 'o', # Circle
'mlp_engine': '^', # Triangle
'rendering_unit': 'D', # Diamond
'memory_controller': 'h', # Hexagon
'default': 'o' # Circle
}
def __init__(self):
"""Initialize the graph plotter."""
self.figure_size = (12, 8)
self.dpi = 300
def plot_operator_graph(self,
graph_data: Dict[str, Any],
output_path: str,
title: str = "Neural Rendering Operator Graph",
show_hardware_assignments: bool = True,
show_data_flow: bool = True,
layout: str = 'hierarchical') -> None:
"""
Plot operator graph with optional hardware assignments and data flow.
"""
# Create NetworkX graph
G = self._build_networkx_graph(graph_data)
# Set up the plot
fig, ax = plt.subplots(figsize=self.figure_size, dpi=self.dpi)
# Choose layout algorithm
if layout == 'hierarchical':
pos = self._hierarchical_layout(G)
elif layout == 'spring':
pos = nx.spring_layout(G, k=2, iterations=50)
elif layout == 'circular':
pos = nx.circular_layout(G)
else:
pos = nx.spring_layout(G)
# Draw edges (data flow)
if show_data_flow:
self._draw_edges(G, pos, ax)
# Draw nodes (operators)
self._draw_nodes(G, pos, ax, show_hardware_assignments)
# Add labels
self._draw_labels(G, pos, ax)
# Add legend
self._add_legend(ax, show_hardware_assignments)
# Format plot
ax.set_title(title, fontsize=16, fontweight='bold', pad=20)
ax.set_aspect('equal')
ax.axis('off')
plt.tight_layout()
plt.savefig(output_path, dpi=self.dpi, bbox_inches='tight')
plt.close()
print(f"✅ Operator graph saved to: {output_path}")
def _build_networkx_graph(self, graph_data: Dict[str, Any]) -> nx.DiGraph:
"""Build NetworkX graph from graph data."""
G = nx.DiGraph()
# Handle different input formats
if 'operators' in graph_data:
# Mapped IR format
operators = graph_data['operators']
for op in operators:
G.add_node(op['op_id'], **op)
# Add edges if dependency information exists
if 'dependencies' in graph_data:
for dep in graph_data['dependencies']:
G.add_edge(dep['from'], dep['to'])
elif 'nodes' in graph_data:
# Direct graph format
for node_id, node_data in graph_data['nodes'].items():
G.add_node(node_id, **node_data)
for edge in graph_data.get('edges', []):
G.add_edge(edge['source'], edge['target'])
return G
def _hierarchical_layout(self, G: nx.DiGraph) -> Dict[str, Tuple[float, float]]:
"""Create hierarchical layout based on topological sorting."""
try:
# Topological sort to get hierarchy levels
topo_order = list(nx.topological_sort(G))
levels = {}
# Assign levels using longest path
for node in topo_order:
if not list(G.predecessors(node)):
levels[node] = 0
else:
levels[node] = max(levels[pred] for pred in G.predecessors(node)) + 1
# Group nodes by level
level_groups = {}
for node, level in levels.items():
if level not in level_groups:
level_groups[level] = []
level_groups[level].append(node)
# Position nodes
pos = {}
for level, nodes in level_groups.items():
for i, node in enumerate(nodes):
x = level * 2
y = (i - len(nodes)/2) * 1.5
pos[node] = (x, y)
return pos
except nx.NetworkXError:
# Fallback to spring layout if graph has cycles
return nx.spring_layout(G, k=2)
def _draw_edges(self, G: nx.DiGraph, pos: Dict, ax: plt.Axes) -> None:
"""Draw edges with arrows indicating data flow."""
nx.draw_networkx_edges(
G, pos, ax=ax,
edge_color='#666666',
arrows=True,
arrowsize=20,
arrowstyle='->',
width=1.5,
alpha=0.7
)
def _draw_nodes(self, G: nx.DiGraph, pos: Dict, ax: plt.Axes,
show_hardware_assignments: bool) -> None:
"""Draw nodes with colors based on operator type and shapes for HW units."""
for node in G.nodes():
node_data = G.nodes[node]
# Get operator type and color
op_type = node_data.get('op_type', 'UNKNOWN')
color = self.OPERATOR_COLORS.get(op_type, self.OPERATOR_COLORS['UNKNOWN'])
# Get hardware unit and shape
hw_unit = node_data.get('hw_unit', 'default')
shape = self.HW_SHAPES.get(hw_unit, self.HW_SHAPES['default'])
# Draw node
nx.draw_networkx_nodes(
G, pos, nodelist=[node], ax=ax,
node_color=color,
node_shape=shape,
node_size=800,
alpha=0.8,
edgecolors='black',
linewidths=2
)
def _draw_labels(self, G: nx.DiGraph, pos: Dict, ax: plt.Axes) -> None:
"""Draw node labels."""
labels = {}
for node in G.nodes():
node_data = G.nodes[node]
# Create compact label
op_type = node_data.get('op_type', 'UNK')[:4] # First 4 chars
labels[node] = f"{node[:6]}\n{op_type}" # Node ID + op type
nx.draw_networkx_labels(
G, pos, labels, ax=ax,
font_size=8,
font_weight='bold'
)
def _add_legend(self, ax: plt.Axes, show_hardware_assignments: bool) -> None:
"""Add legend for operator types and hardware units."""
# Operator type legend
op_patches = []
for op_type, color in self.OPERATOR_COLORS.items():
if op_type != 'UNKNOWN': # Skip unknown for cleaner legend
patch = mpatches.Patch(color=color, label=op_type.replace('_', ' ').title())
op_patches.append(patch)
legend1 = ax.legend(handles=op_patches, loc='upper left',
title='Operator Types', fontsize=9)
legend1.get_title().set_fontweight('bold')