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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//! Graph Manipulation Operations
//!
//! This module provides functionality for manipulating and transforming graphs,
//! including extending, merging, and replacing graph structures.
use super::core::*;
use crate::{device::Device, error::TensorError};
use std::collections::HashMap;
impl Graph {
/// Extend this graph with another graph
pub fn extend_with_graph(
&mut self,
other: &Graph,
node_prefix: Option<&str>,
) -> Result<HashMap<NodeId, NodeId>, TensorError> {
let mut id_mapping = HashMap::new();
let prefix = node_prefix.unwrap_or("");
// Add all nodes from the other graph
for node in other.nodes.values() {
let new_name = if prefix.is_empty() {
node.name.clone()
} else {
format!("{prefix}_{}", node.name)
};
let new_id = self.add_node(
new_name,
node.op_type.clone(),
node.device,
node.attributes.clone(),
)?;
id_mapping.insert(node.id, new_id);
}
// Add all edges from the other graph
for edge in other.edges.values() {
let new_from = *id_mapping
.get(&edge.from_node)
.expect("Node ID must exist in mapping after insertion");
let new_to = *id_mapping
.get(&edge.to_node)
.expect("Node ID must exist in mapping after insertion");
self.add_edge(
new_from,
new_to,
edge.from_output,
edge.to_input,
edge.dtype,
edge.shape.clone(),
edge.is_control,
)?;
}
Ok(id_mapping)
}
/// Integrate a subgraph into this graph at specific connection points
pub fn integrate_subgraph(
&mut self,
subgraph: &Graph,
input_connections: &[(NodeId, usize, NodeId, usize)], // (external_node, output_idx, subgraph_input, input_idx)
output_connections: &[(NodeId, usize, NodeId, usize)], // (subgraph_output, output_idx, external_node, input_idx)
node_prefix: Option<&str>,
) -> Result<HashMap<NodeId, NodeId>, TensorError> {
// First extend the graph with the subgraph
let id_mapping = self.extend_with_graph(subgraph, node_prefix)?;
// Create input connections
for &(external_node, output_idx, subgraph_input, input_idx) in input_connections {
if !self.nodes.contains_key(&external_node) {
return Err(TensorError::invalid_argument(format!(
"External node {} not found",
external_node
)));
}
let mapped_subgraph_node = *id_mapping.get(&subgraph_input).ok_or_else(|| {
TensorError::invalid_argument(format!(
"Subgraph input node {} not found",
subgraph_input
))
})?;
// Create edge from external node to subgraph input
// Note: This is a simplified version - in practice, we'd need to infer types and shapes
self.add_edge(
external_node,
mapped_subgraph_node,
output_idx,
input_idx,
crate::dtype::DType::Float32, // Default type
crate::shape::Shape::new(vec![]),
false,
)?;
}
// Create output connections
for &(subgraph_output, output_idx, external_node, input_idx) in output_connections {
if !self.nodes.contains_key(&external_node) {
return Err(TensorError::invalid_argument(format!(
"External node {} not found",
external_node
)));
}
let mapped_subgraph_node = *id_mapping.get(&subgraph_output).ok_or_else(|| {
TensorError::invalid_argument(format!(
"Subgraph output node {} not found",
subgraph_output
))
})?;
// Create edge from subgraph output to external node
self.add_edge(
mapped_subgraph_node,
external_node,
output_idx,
input_idx,
crate::dtype::DType::Float32, // Default type
crate::shape::Shape::new(vec![]),
false,
)?;
}
Ok(id_mapping)
}
/// Merge multiple graphs into a single graph
pub fn merge_graphs(graphs: &[&Graph]) -> Result<Graph, TensorError> {
let mut merged = Graph::new();
for (i, graph) in graphs.iter().enumerate() {
let prefix = format!("graph_{}", i);
merged.extend_with_graph(graph, Some(&prefix))?;
}
Ok(merged)
}
/// Add a node with automatically generated unique name
pub fn add_node_auto_name(
&mut self,
base_name: &str,
op_type: NodeType,
device: Device,
attributes: HashMap<String, AttributeValue>,
) -> Result<NodeId, TensorError> {
let mut counter = 0;
let mut name = base_name.to_string();
while self.name_to_node.contains_key(&name) {
counter += 1;
name = format!("{}_{}", base_name, counter);
}
self.add_node(name, op_type, device, attributes)
}
/// Add a complete operation subgraph (operation with inputs and outputs)
pub fn add_operation_subgraph(
&mut self,
op_name: &str,
inputs: &[NodeId],
output_shapes: &[crate::shape::Shape],
output_dtypes: &[crate::dtype::DType],
device: Device,
attributes: HashMap<String, AttributeValue>,
) -> Result<Vec<NodeId>, TensorError> {
// Create the operation node
let op_node = self.add_node_auto_name(
op_name,
NodeType::Operation(op_name.to_string()),
device,
attributes,
)?;
// Connect inputs to the operation
for (input_idx, &input_node) in inputs.iter().enumerate() {
if !self.nodes.contains_key(&input_node) {
return Err(TensorError::invalid_argument(format!(
"Input node {} not found",
input_node
)));
}
self.add_edge(
input_node,
op_node,
0, // Assume single output from input node
input_idx,
crate::dtype::DType::Float32, // Default - should be inferred
crate::shape::Shape::new(vec![]),
false,
)?;
}
// Create output nodes if multiple outputs
let mut output_nodes = vec![op_node];
if output_shapes.len() > 1 {
for (output_idx, (shape, dtype)) in output_shapes
.iter()
.zip(output_dtypes.iter())
.enumerate()
.skip(1)
{
let output_node_name = format!("{}_output_{}", op_name, output_idx);
let output_node = self.add_node(
output_node_name,
NodeType::Operation("Identity".to_string()),
device,
HashMap::new(),
)?;
self.add_edge(
op_node,
output_node,
output_idx,
0,
*dtype,
shape.clone(),
false,
)?;
output_nodes.push(output_node);
}
}
Ok(output_nodes)
}
/// Insert a new node between two existing connected nodes
pub fn insert_node_between(
&mut self,
from_node: NodeId,
to_node: NodeId,
new_node_name: String,
new_node_type: NodeType,
device: Device,
attributes: HashMap<String, AttributeValue>,
) -> Result<NodeId, TensorError> {
// Find the edge to replace
let edge_to_replace = self
.edges
.values()
.find(|edge| edge.from_node == from_node && edge.to_node == to_node && !edge.is_control)
.cloned();
let edge = edge_to_replace.ok_or_else(|| {
TensorError::invalid_argument(format!(
"No data edge found between nodes {} and {}",
from_node, to_node
))
})?;
// Create the new node
let new_node = self.add_node(new_node_name, new_node_type, device, attributes)?;
// Remove the original edge
self.remove_edge(edge.id)?;
// Create new edges: from_node -> new_node -> to_node
self.add_edge(
from_node,
new_node,
edge.from_output,
0,
edge.dtype,
edge.shape.clone(),
false,
)?;
self.add_edge(
new_node,
to_node,
0,
edge.to_input,
edge.dtype,
edge.shape,
false,
)?;
Ok(new_node)
}
/// Replace a node with a subgraph
pub fn replace_node_with_subgraph(
&mut self,
node_to_replace: NodeId,
replacement_graph: &Graph,
input_mapping: &HashMap<usize, NodeId>, // input_index -> replacement_node_id
output_mapping: &HashMap<usize, NodeId>, // output_index -> replacement_node_id
) -> Result<HashMap<NodeId, NodeId>, TensorError> {
let node = self
.nodes
.get(&node_to_replace)
.ok_or_else(|| {
TensorError::invalid_argument(format!("Node {} not found", node_to_replace))
})?
.clone();
// Store incoming and outgoing edges
let incoming_edges: Vec<_> = node
.inputs
.iter()
.filter_map(|&edge_id| self.edges.get(&edge_id))
.cloned()
.collect();
let outgoing_edges: Vec<_> = node
.outputs
.iter()
.filter_map(|&edge_id| self.edges.get(&edge_id))
.cloned()
.collect();
// Remove the original node
self.remove_node(node_to_replace)?;
// Add the replacement graph
let id_mapping = self.extend_with_graph(replacement_graph, Some(&node.name))?;
// Reconnect incoming edges
for edge in incoming_edges {
if let Some(&replacement_input) = input_mapping.get(&edge.to_input) {
if let Some(&mapped_node) = id_mapping.get(&replacement_input) {
self.add_edge(
edge.from_node,
mapped_node,
edge.from_output,
0, // Connect to first input of replacement node
edge.dtype,
edge.shape,
edge.is_control,
)?;
}
}
}
// Reconnect outgoing edges
for edge in outgoing_edges {
if let Some(&replacement_output) = output_mapping.get(&edge.from_output) {
if let Some(&mapped_node) = id_mapping.get(&replacement_output) {
self.add_edge(
mapped_node,
edge.to_node,
0, // Connect from first output of replacement node
edge.to_input,
edge.dtype,
edge.shape,
edge.is_control,
)?;
}
}
}
Ok(id_mapping)
}
}