torsh-fx 0.1.2

Graph-based model representation and transformation for ToRSh
Documentation
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! ONNX export functionality for FX graphs
//!
//! This module provides functionality to export FX graphs to ONNX format.
//! Currently implements a basic ONNX representation without external dependencies.

use crate::{FxGraph, Node, TorshResult};
use petgraph::graph::NodeIndex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use torsh_core::{dtype::DType, error::TorshError};

/// ONNX attribute value types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OnnxAttributeValue {
    Int(i64),
    Float(f64),
    String(String),
    Tensor(OnnxTensor),
    Ints(Vec<i64>),
    Floats(Vec<f64>),
    Strings(Vec<String>),
}

/// ONNX attribute
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OnnxAttribute {
    pub name: String,
    pub value: OnnxAttributeValue,
}

/// ONNX tensor representation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OnnxTensor {
    pub dims: Vec<i64>,
    pub data_type: i32, // ONNX data type enum
    pub data: Vec<u8>,  // Raw tensor data
    pub name: Option<String>,
}

/// ONNX value info (for inputs/outputs)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OnnxValueInfo {
    pub name: String,
    pub type_info: OnnxTypeInfo,
}

/// ONNX type information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OnnxTypeInfo {
    pub tensor_type: OnnxTensorType,
}

/// ONNX tensor type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OnnxTensorType {
    pub elem_type: i32,
    pub shape: OnnxTensorShape,
}

/// ONNX tensor shape
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OnnxTensorShape {
    pub dim: Vec<OnnxDimension>,
}

/// ONNX dimension (can be static or dynamic)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OnnxDimension {
    DimValue(i64),
    DimParam(String),
}

/// ONNX node representation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OnnxNode {
    pub input: Vec<String>,
    pub output: Vec<String>,
    pub name: Option<String>,
    pub op_type: String,
    pub domain: Option<String>,
    pub attribute: Vec<OnnxAttribute>,
}

/// ONNX graph representation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OnnxGraph {
    pub node: Vec<OnnxNode>,
    pub name: String,
    pub initializer: Vec<OnnxTensor>,
    pub input: Vec<OnnxValueInfo>,
    pub output: Vec<OnnxValueInfo>,
    pub value_info: Vec<OnnxValueInfo>,
}

/// ONNX model representation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OnnxModel {
    pub ir_version: i64,
    pub producer_name: String,
    pub producer_version: String,
    pub domain: String,
    pub model_version: i64,
    pub graph: OnnxGraph,
}

/// ONNX exporter
pub struct OnnxExporter {
    pub model_name: String,
    pub producer_name: String,
    pub producer_version: String,
    pub opset_version: i64,
}

impl Default for OnnxExporter {
    fn default() -> Self {
        Self {
            model_name: "exported_model".to_string(),
            producer_name: "torsh-fx".to_string(),
            producer_version: "0.1.0".to_string(),
            opset_version: 17, // ONNX opset version
        }
    }
}

impl OnnxExporter {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_model_name(mut self, name: String) -> Self {
        self.model_name = name;
        self
    }

    pub fn with_producer_info(mut self, name: String, version: String) -> Self {
        self.producer_name = name;
        self.producer_version = version;
        self
    }

    pub fn with_opset_version(mut self, version: i64) -> Self {
        self.opset_version = version;
        self
    }

    /// Export FX graph to ONNX model
    pub fn export(&self, graph: &FxGraph) -> TorshResult<OnnxModel> {
        let mut onnx_nodes = Vec::new();
        let value_infos = Vec::new();
        let mut inputs = Vec::new();
        let mut outputs = Vec::new();
        let mut node_name_map = HashMap::new();

        // Generate unique names for each node
        for (idx, _) in graph.nodes() {
            let node_index = idx.index();
            let name = format!("node_{node_index}");
            node_name_map.insert(idx, name);
        }

        // Process each node in the graph
        for (idx, node) in graph.nodes() {
            match node {
                Node::Input(input_name) => {
                    let value_info = OnnxValueInfo {
                        name: input_name.clone(),
                        type_info: OnnxTypeInfo {
                            tensor_type: OnnxTensorType {
                                elem_type: self.dtype_to_onnx_type(DType::F32),
                                shape: OnnxTensorShape {
                                    dim: vec![OnnxDimension::DimParam("dynamic".to_string())],
                                },
                            },
                        },
                    };
                    inputs.push(value_info);
                }

                Node::Call(op_name, args) => {
                    let onnx_node =
                        self.convert_operation_to_onnx(idx, op_name, args, graph, &node_name_map)?;
                    onnx_nodes.push(onnx_node);
                }

                Node::Output => {
                    // Find the input to this output node
                    let predecessors: Vec<_> = graph
                        .graph
                        .neighbors_directed(idx, petgraph::Direction::Incoming)
                        .collect();

                    if let Some(pred_idx) = predecessors.first() {
                        let node_index = idx.index();
                        let default_name = format!("output_{node_index}");
                        let output_name = node_name_map.get(pred_idx).unwrap_or(&default_name);

                        let value_info = OnnxValueInfo {
                            name: output_name.clone(),
                            type_info: OnnxTypeInfo {
                                tensor_type: OnnxTensorType {
                                    elem_type: self.dtype_to_onnx_type(DType::F32),
                                    shape: OnnxTensorShape {
                                        dim: vec![OnnxDimension::DimParam("dynamic".to_string())],
                                    },
                                },
                            },
                        };
                        outputs.push(value_info);
                    }
                }

                Node::Conditional {
                    condition,
                    then_branch,
                    else_branch,
                } => {
                    // ONNX doesn't have native conditionals, so we'd need to use If operator
                    // For now, create a placeholder node
                    let node_index = idx.index();
                    let onnx_node = OnnxNode {
                        input: vec![condition.clone()],
                        output: vec![node_name_map[&idx].clone()],
                        name: Some(format!("conditional_{node_index}")),
                        op_type: "If".to_string(),
                        domain: None,
                        attribute: vec![
                            OnnxAttribute {
                                name: "then_branch".to_string(),
                                value: OnnxAttributeValue::Strings(then_branch.clone()),
                            },
                            OnnxAttribute {
                                name: "else_branch".to_string(),
                                value: OnnxAttributeValue::Strings(else_branch.clone()),
                            },
                        ],
                    };
                    onnx_nodes.push(onnx_node);
                }

                Node::Loop {
                    condition,
                    body,
                    loop_vars,
                } => {
                    // ONNX has Loop operator for dynamic control flow
                    let node_index = idx.index();
                    let onnx_node = OnnxNode {
                        input: vec![condition.clone()],
                        output: vec![node_name_map[&idx].clone()],
                        name: Some(format!("loop_{node_index}")),
                        op_type: "Loop".to_string(),
                        domain: None,
                        attribute: vec![
                            OnnxAttribute {
                                name: "body".to_string(),
                                value: OnnxAttributeValue::Strings(body.clone()),
                            },
                            OnnxAttribute {
                                name: "loop_vars".to_string(),
                                value: OnnxAttributeValue::Strings(loop_vars.clone()),
                            },
                        ],
                    };
                    onnx_nodes.push(onnx_node);
                }

                Node::Merge {
                    inputs: merge_inputs,
                } => {
                    // Use Concat operator for merging
                    let node_index = idx.index();
                    let onnx_node = OnnxNode {
                        input: merge_inputs.clone(),
                        output: vec![node_name_map[&idx].clone()],
                        name: Some(format!("merge_{node_index}")),
                        op_type: "Concat".to_string(),
                        domain: None,
                        attribute: vec![OnnxAttribute {
                            name: "axis".to_string(),
                            value: OnnxAttributeValue::Int(0), // Default to axis 0
                        }],
                    };
                    onnx_nodes.push(onnx_node);
                }

                Node::GetAttr { target, attr } => {
                    // This would typically be a parameter/constant in ONNX
                    let onnx_node = OnnxNode {
                        input: vec![target.clone()],
                        output: vec![node_name_map[&idx].clone()],
                        name: Some(format!("getattr_{}_{}", target, attr)),
                        op_type: "Identity".to_string(), // Use Identity as placeholder
                        domain: None,
                        attribute: vec![OnnxAttribute {
                            name: "attribute_name".to_string(),
                            value: OnnxAttributeValue::String(attr.clone()),
                        }],
                    };
                    onnx_nodes.push(onnx_node);
                }
            }
        }

        let onnx_graph = OnnxGraph {
            node: onnx_nodes,
            name: self.model_name.clone(),
            initializer: Vec::new(), // Would contain model parameters
            input: inputs,
            output: outputs,
            value_info: value_infos,
        };

        Ok(OnnxModel {
            ir_version: 8, // ONNX IR version
            producer_name: self.producer_name.clone(),
            producer_version: self.producer_version.clone(),
            domain: "ai.torsh".to_string(),
            model_version: 1,
            graph: onnx_graph,
        })
    }

    /// Convert a torsh operation to ONNX node
    fn convert_operation_to_onnx(
        &self,
        node_idx: NodeIndex,
        op_name: &str,
        args: &[String],
        graph: &FxGraph,
        node_name_map: &HashMap<NodeIndex, String>,
    ) -> TorshResult<OnnxNode> {
        let node_name = &node_name_map[&node_idx];

        // Get input names from predecessor nodes
        let predecessors: Vec<_> = graph
            .graph
            .neighbors_directed(node_idx, petgraph::Direction::Incoming)
            .collect();

        let inputs: Vec<String> = predecessors
            .iter()
            .map(|&pred_idx| node_name_map[&pred_idx].clone())
            .collect();

        let (onnx_op_type, attributes) = self.map_operation_to_onnx(op_name, args)?;

        Ok(OnnxNode {
            input: inputs,
            output: vec![node_name.clone()],
            name: Some(format!("{}_{}", op_name, node_idx.index())),
            op_type: onnx_op_type,
            domain: None,
            attribute: attributes,
        })
    }

    /// Map torsh operations to ONNX operations
    fn map_operation_to_onnx(
        &self,
        op_name: &str,
        args: &[String],
    ) -> TorshResult<(String, Vec<OnnxAttribute>)> {
        match op_name {
            // Activation functions
            "relu" => Ok(("Relu".to_string(), vec![])),
            "sigmoid" => Ok(("Sigmoid".to_string(), vec![])),
            "tanh" => Ok(("Tanh".to_string(), vec![])),
            "gelu" => Ok(("Gelu".to_string(), vec![])),
            "softmax" => Ok((
                "Softmax".to_string(),
                vec![OnnxAttribute {
                    name: "axis".to_string(),
                    value: OnnxAttributeValue::Int(-1), // Default to last axis
                }],
            )),

            // Math operations
            "add" => Ok(("Add".to_string(), vec![])),
            "sub" => Ok(("Sub".to_string(), vec![])),
            "mul" => Ok(("Mul".to_string(), vec![])),
            "div" => Ok(("Div".to_string(), vec![])),
            "matmul" => Ok(("MatMul".to_string(), vec![])),

            // Tensor operations
            "reshape" => {
                // Extract shape from args
                let shape_args = if args.len() > 1 {
                    args[1..]
                        .iter()
                        .filter_map(|s| s.parse::<i64>().ok())
                        .collect()
                } else {
                    vec![-1] // Dynamic reshape
                };

                Ok((
                    "Reshape".to_string(),
                    vec![OnnxAttribute {
                        name: "shape".to_string(),
                        value: OnnxAttributeValue::Ints(shape_args),
                    }],
                ))
            }

            "transpose" => {
                let perm = if args.len() > 1 {
                    args[1..]
                        .iter()
                        .filter_map(|s| s.parse::<i64>().ok())
                        .collect()
                } else {
                    vec![] // Default transpose
                };

                Ok((
                    "Transpose".to_string(),
                    vec![OnnxAttribute {
                        name: "perm".to_string(),
                        value: OnnxAttributeValue::Ints(perm),
                    }],
                ))
            }

            "squeeze" => Ok(("Squeeze".to_string(), vec![])),
            "unsqueeze" => Ok(("Unsqueeze".to_string(), vec![])),

            // Neural network operations
            "conv2d" => Ok((
                "Conv".to_string(),
                vec![
                    OnnxAttribute {
                        name: "kernel_shape".to_string(),
                        value: OnnxAttributeValue::Ints(vec![3, 3]), // Default 3x3
                    },
                    OnnxAttribute {
                        name: "pads".to_string(),
                        value: OnnxAttributeValue::Ints(vec![1, 1, 1, 1]), // Default padding
                    },
                ],
            )),

            "max_pool2d" => Ok((
                "MaxPool".to_string(),
                vec![OnnxAttribute {
                    name: "kernel_shape".to_string(),
                    value: OnnxAttributeValue::Ints(vec![2, 2]), // Default 2x2
                }],
            )),

            "avg_pool2d" => Ok((
                "AveragePool".to_string(),
                vec![OnnxAttribute {
                    name: "kernel_shape".to_string(),
                    value: OnnxAttributeValue::Ints(vec![2, 2]), // Default 2x2
                }],
            )),

            "batch_norm" => Ok(("BatchNormalization".to_string(), vec![])),
            "dropout" => Ok(("Dropout".to_string(), vec![])),

            // Linear/Dense layer
            "linear" => Ok(("MatMul".to_string(), vec![])), // Linear is often just MatMul + Add

            // Default case
            _ => Ok(("Identity".to_string(), vec![])), // Use Identity as fallback
        }
    }

    /// Convert torsh DType to ONNX data type enum
    fn dtype_to_onnx_type(&self, dtype: DType) -> i32 {
        match dtype {
            DType::F32 => 1,  // FLOAT
            DType::F64 => 11, // DOUBLE
            DType::I32 => 6,  // INT32
            DType::I64 => 7,  // INT64
            DType::U8 => 2,   // UINT8
            DType::I8 => 3,   // INT8
            DType::I16 => 5,  // INT16
            DType::Bool => 9, // BOOL
            _ => 1,           // Default to FLOAT
        }
    }

    /// Export model to ONNX JSON format
    pub fn export_to_json(&self, graph: &FxGraph) -> TorshResult<String> {
        let model = self.export(graph)?;
        serde_json::to_string_pretty(&model).map_err(|e| {
            TorshError::SerializationError(format!("Failed to serialize ONNX model to JSON: {}", e))
        })
    }

    /// Export model to ONNX binary format (protobuf would be used in real implementation)
    pub fn export_to_binary(&self, graph: &FxGraph) -> TorshResult<Vec<u8>> {
        let model = self.export(graph)?;
        oxicode::serde::encode_to_vec(&model, oxicode::config::standard()).map_err(|e| {
            TorshError::SerializationError(format!(
                "Failed to serialize ONNX model to binary: {}",
                e
            ))
        })
    }
}

/// Convenience function to export a graph to ONNX
pub fn export_to_onnx(graph: &FxGraph, model_name: Option<String>) -> TorshResult<OnnxModel> {
    let exporter = if let Some(name) = model_name {
        OnnxExporter::new().with_model_name(name)
    } else {
        OnnxExporter::new()
    };

    exporter.export(graph)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Edge, Node};

    #[test]
    fn test_basic_onnx_export() {
        let mut graph = FxGraph::new();
        let input = graph.graph.add_node(Node::Input("x".to_string()));
        let relu = graph
            .graph
            .add_node(Node::Call("relu".to_string(), vec!["x".to_string()]));
        let output = graph.graph.add_node(Node::Output);

        graph.graph.add_edge(
            input,
            relu,
            Edge {
                name: "x".to_string(),
            },
        );
        graph.graph.add_edge(
            relu,
            output,
            Edge {
                name: "relu_out".to_string(),
            },
        );
        graph.inputs.push(input);
        graph.outputs.push(output);

        let exporter = OnnxExporter::new();
        let onnx_model = exporter.export(&graph).unwrap();

        assert_eq!(onnx_model.producer_name, "torsh-fx");
        assert_eq!(onnx_model.graph.input.len(), 1);
        assert_eq!(onnx_model.graph.output.len(), 1);
        assert!(onnx_model.graph.node.iter().any(|n| n.op_type == "Relu"));
    }

    #[test]
    fn test_operation_mapping() {
        let exporter = OnnxExporter::new();

        let (op_type, _) = exporter.map_operation_to_onnx("relu", &[]).unwrap();
        assert_eq!(op_type, "Relu");

        let (op_type, _) = exporter.map_operation_to_onnx("add", &[]).unwrap();
        assert_eq!(op_type, "Add");

        let (op_type, attrs) = exporter.map_operation_to_onnx("softmax", &[]).unwrap();
        assert_eq!(op_type, "Softmax");
        assert!(!attrs.is_empty());
    }

    #[test]
    fn test_dtype_conversion() {
        let exporter = OnnxExporter::new();

        assert_eq!(exporter.dtype_to_onnx_type(DType::F32), 1);
        assert_eq!(exporter.dtype_to_onnx_type(DType::F64), 11);
        assert_eq!(exporter.dtype_to_onnx_type(DType::I32), 6);
    }

    #[test]
    fn test_json_export() {
        let mut graph = FxGraph::new();
        let input = graph.graph.add_node(Node::Input("x".to_string()));
        let output = graph.graph.add_node(Node::Output);

        graph.graph.add_edge(
            input,
            output,
            Edge {
                name: "x".to_string(),
            },
        );
        graph.inputs.push(input);
        graph.outputs.push(output);

        let exporter = OnnxExporter::new();
        let json = exporter.export_to_json(&graph).unwrap();

        assert!(json.contains("torsh-fx"));
        assert!(json.contains("exported_model"));
    }
}