torsh-jit 0.1.3

JIT compilation and kernel fusion for ToRSh deep learning framework
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
//! Computation graph representation and analysis
//!
//! This module provides a comprehensive framework for representing, building, and analyzing
//! computation graphs used in JIT compilation. The module is organized into several
//! sub-modules for better maintainability:
//!
//! - [`core`]: Core graph structures and fundamental operations
//! - [`operations`]: Operation definitions and related structures
//! - [`builder`]: Graph construction utilities and builder pattern
//! - [`metadata`]: Graph metadata and configuration structures
//! - [`control_flow`]: Control flow analysis and loop detection
//!
//! # Examples
//!
//! ## Building a Simple Graph
//!
//! ```rust
//! use torsh_jit::graph::{GraphBuilder, Operation};
//! use torsh_core::{Shape, DType};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let mut builder = GraphBuilder::new();
//!
//! // Add input node
//! let input = builder.add_input(
//!     "input".to_string(),
//!     Shape::new(vec![1, 3, 224, 224]),
//!     DType::F32
//! );
//!
//! // Add ReLU operation
//! let relu = builder.add_unary_op(
//!     "relu".to_string(),
//!     Operation::Relu,
//!     input
//! )?;
//!
//! // Mark as output
//! builder.mark_output(relu)?;
//!
//! // Build the graph
//! let graph = builder.build()?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Analyzing Control Flow
//!
//! ```rust
//! use torsh_jit::graph::{ComputationGraph, ControlFlowAnalysis};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let graph = ComputationGraph::new();
//! let analysis = ControlFlowAnalysis::analyze(&graph)?;
//!
//! println!("Found {} loops and {} conditionals",
//!     analysis.stats.loop_count,
//!     analysis.stats.conditional_count
//! );
//! # Ok(())
//! # }
//! ```

pub mod builder;
pub mod control_flow;
pub mod core;
pub mod metadata;
pub mod operations;

// Re-export commonly used types for convenience
pub use core::{
    shape_from_slice, ComputationGraph, Edge, Node, NodeId, OperationCategory,
    SerializableNodeIndex,
};

pub use operations::{
    Attribute, BatchNormInfo, BlockInfo, BlockType, ConstantInfo, ConstantValue, Conv2dInfo,
    ForInfo, IfInfo, LinearInfo, MergeInfo, MergeStrategy, Operation, ParameterInfo, PoolInfo,
    SliceRange, WhileInfo,
};

pub use builder::{GraphBuilder, GraphStatistics};
pub use control_flow::{
    ConditionalInfo, ControlFlowAnalysis, ControlFlowStats, LoopInfo, LoopType,
};
pub use metadata::{GraphMetadata, OptimizationLevel};

#[cfg(test)]
mod tests {
    use super::*;
    use torsh_core::{DType, Shape};

    #[test]
    fn test_graph_creation() {
        let graph = ComputationGraph::new();
        assert!(graph.is_empty());
        assert_eq!(graph.node_count(), 0);
        assert_eq!(graph.edge_count(), 0);
    }

    #[test]
    fn test_graph_builder() {
        let mut builder = GraphBuilder::new();

        let input = builder.add_input("input".to_string(), Shape::new(vec![1, 784]), DType::F32);

        let relu = builder
            .add_unary_op("relu".to_string(), Operation::Relu, input)
            .expect("operation should succeed");

        builder
            .mark_output(relu)
            .expect("output marking should succeed");

        let graph = builder
            .build()
            .expect("build should succeed with valid configuration");
        assert_eq!(graph.node_count(), 2);
        assert_eq!(graph.edge_count(), 1);
        assert_eq!(graph.inputs.len(), 1);
        assert_eq!(graph.outputs.len(), 1);
    }

    #[test]
    fn test_operation_properties() {
        assert!(Operation::Add.is_elementwise());
        assert!(Operation::Sum {
            dims: vec![0],
            keepdim: false
        }
        .is_reduction());
        assert!(Operation::Reshape { shape: vec![1, -1] }.modifies_shape());
        assert!(Operation::Relu.is_fusible());

        assert_eq!(Operation::Add.expected_inputs(), 2);
        assert_eq!(Operation::Relu.expected_inputs(), 1);
        assert_eq!(Operation::Input.expected_inputs(), 0);
    }

    #[test]
    fn test_graph_validation() {
        let mut builder = GraphBuilder::new();

        let input = builder.add_input("input".to_string(), Shape::new(vec![1, 784]), DType::F32);

        builder
            .mark_output(input)
            .expect("output marking should succeed");

        let graph = builder
            .build()
            .expect("build should succeed with valid configuration");
        assert!(graph.validate().is_ok());
    }

    #[test]
    fn test_replace_node_with_input() {
        let mut graph = ComputationGraph::new();

        // Create a simple chain: input -> relu -> output
        let input = graph.add_node(
            Node::new(Operation::Input, "input".to_string())
                .with_output_shapes(vec![Some(Shape::new(vec![1, 10]))])
                .with_dtypes(vec![DType::F32]),
        );
        let relu = graph.add_node(
            Node::new(Operation::Relu, "relu".to_string())
                .with_output_shapes(vec![Some(Shape::new(vec![1, 10]))])
                .with_dtypes(vec![DType::F32]),
        );
        let output = graph.add_node(
            Node::new(Operation::Input, "output".to_string())
                .with_output_shapes(vec![Some(Shape::new(vec![1, 10]))])
                .with_dtypes(vec![DType::F32]),
        );

        graph.add_edge(
            input,
            relu,
            Edge {
                src_output: 0,
                dst_input: 0,
            },
        );
        graph.add_edge(
            relu,
            output,
            Edge {
                src_output: 0,
                dst_input: 0,
            },
        );
        graph.add_output(output);

        let initial_node_count = graph.node_count();

        // Replace relu with input (bypass the relu)
        let result = graph.replace_node_with_input(relu, input);
        assert!(result.is_ok());

        // One node should have been removed (relu)
        assert_eq!(graph.node_count(), initial_node_count - 1);

        // Since NodeIndex can be reused by petgraph after node removal,
        // we need to find nodes by their properties (name) rather than old indices
        let actual_output_id = graph
            .nodes()
            .find(|(_, n)| n.name == "output")
            .map(|(id, _)| id)
            .expect("Output node should exist");

        let actual_input_id = graph
            .nodes()
            .find(|(_, n)| n.name == "input")
            .map(|(id, _)| id)
            .expect("Input node should exist");

        // Verify that input now connects directly to output
        let output_predecessors: Vec<_> = graph.predecessors(actual_output_id).collect();
        assert_eq!(
            output_predecessors.len(),
            1,
            "Output should have exactly 1 predecessor"
        );
        assert_eq!(
            output_predecessors[0], actual_input_id,
            "Output's predecessor should be input"
        );
    }

    #[test]
    fn test_replace_node_with_sequence() {
        let mut graph = ComputationGraph::new();

        // Create: input -> placeholder -> output
        let input = graph.add_node(
            Node::new(Operation::Input, "input".to_string())
                .with_output_shapes(vec![Some(Shape::new(vec![1, 10]))])
                .with_dtypes(vec![DType::F32]),
        );
        let placeholder = graph.add_node(
            Node::new(Operation::Input, "placeholder".to_string())
                .with_output_shapes(vec![Some(Shape::new(vec![1, 10]))])
                .with_dtypes(vec![DType::F32]),
        );
        let output = graph.add_node(
            Node::new(Operation::Input, "output".to_string())
                .with_output_shapes(vec![Some(Shape::new(vec![1, 10]))])
                .with_dtypes(vec![DType::F32]),
        );

        graph.add_edge(
            input,
            placeholder,
            Edge {
                src_output: 0,
                dst_input: 0,
            },
        );
        graph.add_edge(
            placeholder,
            output,
            Edge {
                src_output: 0,
                dst_input: 0,
            },
        );
        graph.add_output(output);

        // Create a sequence of relu -> tanh to replace the placeholder
        let sequence = vec![
            Node::new(Operation::Relu, "relu".to_string())
                .with_output_shapes(vec![Some(Shape::new(vec![1, 10]))])
                .with_dtypes(vec![DType::F32]),
            Node::new(Operation::Tanh, "tanh".to_string())
                .with_output_shapes(vec![Some(Shape::new(vec![1, 10]))])
                .with_dtypes(vec![DType::F32]),
        ];

        let initial_node_count = graph.node_count();
        let result = graph.replace_node_with_sequence(placeholder, &sequence);
        assert!(result.is_ok());

        // Placeholder should be removed, but 2 nodes added, so net +1
        assert_eq!(graph.node_count(), initial_node_count + 1);

        // Verify graph structure: input should connect to a relu-like node,
        // and output should have a tanh-like node as predecessor
        let output_predecessors: Vec<_> = graph.predecessors(output).collect();
        assert_eq!(
            output_predecessors.len(),
            1,
            "Output should have exactly one predecessor"
        );

        // The predecessor of output should be one of the newly added nodes
        let output_pred_id = output_predecessors[0];
        let output_pred_node = graph
            .node(output_pred_id)
            .expect("Output predecessor should exist");
        // The last node in the sequence was Tanh
        assert_eq!(
            output_pred_node.operation,
            Operation::Tanh,
            "Output predecessor should be the Tanh node"
        );
    }

    #[test]
    fn test_replace_node_with_input_error_on_non_predecessor() {
        let mut graph = ComputationGraph::new();

        let node1 = graph.add_node(
            Node::new(Operation::Input, "node1".to_string())
                .with_output_shapes(vec![Some(Shape::new(vec![1, 10]))])
                .with_dtypes(vec![DType::F32]),
        );
        let node2 = graph.add_node(
            Node::new(Operation::Relu, "node2".to_string())
                .with_output_shapes(vec![Some(Shape::new(vec![1, 10]))])
                .with_dtypes(vec![DType::F32]),
        );

        // node1 is not a predecessor of node2, so this should fail
        let result = graph.replace_node_with_input(node2, node1);
        assert!(result.is_err());
    }

    #[test]
    fn test_replace_node_with_empty_sequence_error() {
        let mut graph = ComputationGraph::new();

        let node = graph.add_node(
            Node::new(Operation::Input, "node".to_string())
                .with_output_shapes(vec![Some(Shape::new(vec![1, 10]))])
                .with_dtypes(vec![DType::F32]),
        );

        // Empty sequence should fail
        let result = graph.replace_node_with_sequence(node, &[]);
        assert!(result.is_err());
    }

    #[test]
    fn test_node_categories() {
        use crate::graph::core::OperationCategory;

        let node = Node::new(Operation::Add, "add".to_string());
        assert_eq!(node.operation_category(), OperationCategory::ElementWise);

        let node = Node::new(Operation::MatMul, "matmul".to_string());
        assert_eq!(node.operation_category(), OperationCategory::LinearAlgebra);

        let node = Node::new(
            Operation::Conv2d(Conv2dInfo {
                in_channels: 3,
                out_channels: 64,
                kernel_size: (3, 3),
                stride: (1, 1),
                padding: (1, 1),
                dilation: (1, 1),
                groups: 1,
            }),
            "conv".to_string(),
        );
        assert_eq!(node.operation_category(), OperationCategory::NeuralNetwork);
    }

    #[test]
    fn test_graph_statistics() {
        let mut builder = GraphBuilder::new();

        let input = builder.add_input("input".to_string(), Shape::new(vec![1, 784]), DType::F32);

        let relu1 = builder
            .add_unary_op("relu1".to_string(), Operation::Relu, input)
            .expect("operation should succeed");

        let relu2 = builder
            .add_unary_op("relu2".to_string(), Operation::Relu, relu1)
            .expect("operation should succeed");

        builder
            .mark_output(relu2)
            .expect("output marking should succeed");

        let stats = builder.statistics();
        assert_eq!(stats.node_count, 3);
        assert_eq!(stats.edge_count, 2);
        assert_eq!(stats.input_count, 1);
        assert_eq!(stats.output_count, 1);

        // Check operation counts
        assert_eq!(stats.operation_counts.get("input"), Some(&1));
        assert_eq!(stats.operation_counts.get("relu"), Some(&2));
    }

    #[test]
    fn test_control_flow_analysis() {
        let graph = ComputationGraph::new();
        let analysis =
            ControlFlowAnalysis::analyze(&graph).expect("Control Flow Analysis should succeed");

        assert_eq!(analysis.stats.total_nodes, 0);
        assert_eq!(analysis.stats.loop_count, 0);
        assert_eq!(analysis.stats.conditional_count, 0);
    }

    #[test]
    fn test_metadata() {
        let metadata = GraphMetadata::new("test_graph".to_string())
            .with_version("1.0.0".to_string())
            .with_creator("test".to_string())
            .with_custom("key".to_string(), "value".to_string());

        assert_eq!(metadata.name, "test_graph");
        assert_eq!(metadata.version, "1.0.0");
        assert_eq!(metadata.creator, "test");
        assert_eq!(metadata.get_custom("key"), Some(&"value".to_string()));
    }
}