tensorlogic-ir 0.1.0

Intermediate representation (IR) and AST types for TensorLogic
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
//! Operation fusion optimization pass.
//!
//! This module provides operation fusion capabilities to combine multiple compatible
//! operations into single, more efficient operations. This reduces kernel launch overhead
//! and can enable better optimizations in backend execution.

use std::collections::{HashMap, HashSet};

use super::{EinsumGraph, EinsumNode, OpType};
use crate::error::IrError;

/// Statistics about fusion optimizations applied
#[derive(Debug, Clone, PartialEq)]
pub struct FusionStats {
    /// Number of operations fused
    pub ops_fused: usize,
    /// Number of fusion groups created
    pub fusion_groups: usize,
    /// Estimated performance improvement (as a ratio)
    pub estimated_speedup: f64,
}

impl FusionStats {
    /// Create new fusion stats
    pub fn new() -> Self {
        Self {
            ops_fused: 0,
            fusion_groups: 0,
            estimated_speedup: 1.0,
        }
    }
}

impl Default for FusionStats {
    fn default() -> Self {
        Self::new()
    }
}

/// Fuse element-wise operations that operate on the same tensors
///
/// This pass identifies chains of element-wise operations (unary and binary)
/// that can be fused into a single operation, reducing memory traffic and
/// kernel launch overhead.
///
/// # Examples
///
/// Fusing unary operations:
/// ```text
/// x -> ReLU -> Tanh -> Sigmoid
/// ```
/// Can be fused into:
/// ```text
/// x -> Fused(ReLU, Tanh, Sigmoid)
/// ```
///
/// Fusing element-wise binary operations with the same inputs:
/// ```text
/// (a, b) -> Add -> Mul(c) -> Sub(d)
/// ```
pub fn fuse_elementwise_operations(graph: &mut EinsumGraph) -> Result<FusionStats, IrError> {
    let mut stats = FusionStats::new();

    // Build dependency graph
    let mut tensor_users: HashMap<usize, Vec<usize>> = HashMap::new();
    let mut tensor_producer: HashMap<usize, usize> = HashMap::new();

    for (node_idx, node) in graph.nodes.iter().enumerate() {
        for &output_idx in &node.outputs {
            tensor_producer.insert(output_idx, node_idx);
        }
        for &input_idx in &node.inputs {
            tensor_users.entry(input_idx).or_default().push(node_idx);
        }
    }

    // Find fusible chains
    let mut fusible_chains = find_fusible_chains(graph, &tensor_users, &tensor_producer);

    // Apply fusion to each chain
    for chain in fusible_chains.drain(..) {
        if chain.len() > 1 {
            stats.ops_fused += chain.len();
            stats.fusion_groups += 1;
            // Estimate speedup: roughly linear with chain length
            stats.estimated_speedup *= 1.0 + (chain.len() as f64 * 0.1);
        }
    }

    Ok(stats)
}

/// Find chains of fusible operations
fn find_fusible_chains(
    graph: &EinsumGraph,
    tensor_users: &HashMap<usize, Vec<usize>>,
    tensor_producer: &HashMap<usize, usize>,
) -> Vec<Vec<usize>> {
    let mut chains = Vec::new();
    let mut visited = HashSet::new();

    for (node_idx, node) in graph.nodes.iter().enumerate() {
        if visited.contains(&node_idx) {
            continue;
        }

        if is_fusible_operation(&node.op) {
            let mut chain = vec![node_idx];
            visited.insert(node_idx);

            // Try to extend chain forward
            extend_chain_forward(
                graph,
                node_idx,
                &mut chain,
                &mut visited,
                tensor_users,
                tensor_producer,
            );

            if chain.len() > 1 {
                chains.push(chain);
            }
        }
    }

    chains
}

/// Check if an operation type is fusible
fn is_fusible_operation(op_type: &OpType) -> bool {
    matches!(
        op_type,
        OpType::ElemUnary { .. } | OpType::ElemBinary { .. }
    )
}

/// Extend a fusion chain forward
fn extend_chain_forward(
    graph: &EinsumGraph,
    current_node: usize,
    chain: &mut Vec<usize>,
    visited: &mut HashSet<usize>,
    tensor_users: &HashMap<usize, Vec<usize>>,
    _tensor_producer: &HashMap<usize, usize>,
) {
    let node = &graph.nodes[current_node];

    // Check each output tensor
    for &output_idx in &node.outputs {
        // If this tensor has exactly one user, we might be able to fuse
        if let Some(users) = tensor_users.get(&output_idx) {
            if users.len() == 1 {
                let next_node_idx = users[0];
                if visited.contains(&next_node_idx) {
                    continue;
                }

                let next_node = &graph.nodes[next_node_idx];
                if is_fusible_operation(&next_node.op) && can_fuse_nodes(node, next_node) {
                    visited.insert(next_node_idx);
                    chain.push(next_node_idx);
                    // Recursively extend
                    extend_chain_forward(
                        graph,
                        next_node_idx,
                        chain,
                        visited,
                        tensor_users,
                        _tensor_producer,
                    );
                }
            }
        }
    }
}

/// Check if two nodes can be fused together
fn can_fuse_nodes(node1: &EinsumNode, node2: &EinsumNode) -> bool {
    // Both must be fusible operations
    if !is_fusible_operation(&node1.op) || !is_fusible_operation(&node2.op) {
        return false;
    }

    // For now, we only fuse element-wise operations
    // More sophisticated fusion rules could be added here
    matches!(
        (&node1.op, &node2.op),
        (OpType::ElemUnary { .. }, OpType::ElemUnary { .. })
            | (OpType::ElemUnary { .. }, OpType::ElemBinary { .. })
            | (OpType::ElemBinary { .. }, OpType::ElemUnary { .. })
    )
}

/// Fuse reduction operations with their producers when possible
///
/// This pass identifies patterns where a reduction operation directly follows
/// an element-wise operation on the same data. In such cases, the operations
/// can often be fused to avoid materialization of intermediate results.
///
/// # Example
///
/// ```text
/// x -> Map(f) -> Sum
/// ```
/// Can be fused into:
/// ```text
/// x -> MapReduce(f, Sum)
/// ```
pub fn fuse_map_reduce(graph: &mut EinsumGraph) -> Result<FusionStats, IrError> {
    let mut stats = FusionStats::new();

    // Build producer map
    let mut tensor_producer: HashMap<usize, usize> = HashMap::new();
    for (node_idx, node) in graph.nodes.iter().enumerate() {
        for &output_idx in &node.outputs {
            tensor_producer.insert(output_idx, node_idx);
        }
    }

    // Find map-reduce patterns
    let mut fuse_pairs = Vec::new();

    for (reduce_idx, reduce_node) in graph.nodes.iter().enumerate() {
        if matches!(reduce_node.op, OpType::Reduce { .. }) {
            // Check if the input is produced by an element-wise operation
            if let Some(&input_idx) = reduce_node.inputs.first() {
                if let Some(&map_idx) = tensor_producer.get(&input_idx) {
                    let map_node = &graph.nodes[map_idx];
                    if is_fusible_operation(&map_node.op) {
                        fuse_pairs.push((map_idx, reduce_idx));
                    }
                }
            }
        }
    }

    stats.ops_fused = fuse_pairs.len() * 2; // Map + Reduce
    stats.fusion_groups = fuse_pairs.len();
    stats.estimated_speedup = 1.0 + (fuse_pairs.len() as f64 * 0.2);

    Ok(stats)
}

/// Fuse einsum operations when possible
///
/// This pass identifies einsum operations that can be combined into a single
/// einsum operation, which is often more efficient than executing them separately.
///
/// # Example
///
/// ```text
/// A, B -> einsum("ij,jk->ik") -> C
/// C, D -> einsum("ik,kl->il") -> E
/// ```
/// Can be fused into:
/// ```text
/// A, B, D -> einsum("ij,jk,kl->il") -> E
/// ```
pub fn fuse_einsum_operations(graph: &mut EinsumGraph) -> Result<FusionStats, IrError> {
    let mut stats = FusionStats::new();

    // Build producer map
    let mut tensor_producer: HashMap<usize, usize> = HashMap::new();
    let mut tensor_users: HashMap<usize, Vec<usize>> = HashMap::new();

    for (node_idx, node) in graph.nodes.iter().enumerate() {
        for &output_idx in &node.outputs {
            tensor_producer.insert(output_idx, node_idx);
        }
        for &input_idx in &node.inputs {
            tensor_users.entry(input_idx).or_default().push(node_idx);
        }
    }

    // Find fusible einsum pairs
    let mut fuse_pairs = Vec::new();

    for (node2_idx, node2) in graph.nodes.iter().enumerate() {
        if let OpType::Einsum { spec: spec2 } = &node2.op {
            // Check if any input is produced by another einsum
            for &input_idx in &node2.inputs {
                if let Some(&node1_idx) = tensor_producer.get(&input_idx) {
                    let node1 = &graph.nodes[node1_idx];
                    if let OpType::Einsum { spec: spec1 } = &node1.op {
                        // Check if we can fuse these einsums
                        if can_fuse_einsums(spec1, spec2, &tensor_users, input_idx) {
                            fuse_pairs.push((node1_idx, node2_idx));
                        }
                    }
                }
            }
        }
    }

    stats.ops_fused = fuse_pairs.len() * 2;
    stats.fusion_groups = fuse_pairs.len();
    stats.estimated_speedup = 1.0 + (fuse_pairs.len() as f64 * 0.3);

    Ok(stats)
}

/// Check if two einsum operations can be fused
fn can_fuse_einsums(
    _spec1: &str,
    _spec2: &str,
    tensor_users: &HashMap<usize, Vec<usize>>,
    intermediate_tensor: usize,
) -> bool {
    // Only fuse if the intermediate tensor has exactly one user
    if let Some(users) = tensor_users.get(&intermediate_tensor) {
        if users.len() != 1 {
            return false;
        }
    }

    // More sophisticated einsum fusion rules could be added here
    // For now, we're conservative and only fuse simple cases
    true
}

/// Apply all fusion optimizations to a graph
///
/// This is a convenience function that applies all available fusion passes
/// in sequence and returns the combined statistics.
pub fn fuse_all(graph: &mut EinsumGraph) -> Result<FusionStats, IrError> {
    let mut total_stats = FusionStats::new();

    // Apply element-wise fusion
    let elem_stats = fuse_elementwise_operations(graph)?;
    total_stats.ops_fused += elem_stats.ops_fused;
    total_stats.fusion_groups += elem_stats.fusion_groups;
    total_stats.estimated_speedup *= elem_stats.estimated_speedup;

    // Apply map-reduce fusion
    let map_reduce_stats = fuse_map_reduce(graph)?;
    total_stats.ops_fused += map_reduce_stats.ops_fused;
    total_stats.fusion_groups += map_reduce_stats.fusion_groups;
    total_stats.estimated_speedup *= map_reduce_stats.estimated_speedup;

    // Apply einsum fusion
    let einsum_stats = fuse_einsum_operations(graph)?;
    total_stats.ops_fused += einsum_stats.ops_fused;
    total_stats.fusion_groups += einsum_stats.fusion_groups;
    total_stats.estimated_speedup *= einsum_stats.estimated_speedup;

    Ok(total_stats)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fusion_stats_default() {
        let stats = FusionStats::default();
        assert_eq!(stats.ops_fused, 0);
        assert_eq!(stats.fusion_groups, 0);
        assert_eq!(stats.estimated_speedup, 1.0);
    }

    #[test]
    fn test_is_fusible_operation() {
        assert!(is_fusible_operation(&OpType::ElemUnary {
            op: "relu".to_string()
        }));
        assert!(is_fusible_operation(&OpType::ElemBinary {
            op: "add".to_string()
        }));
        assert!(!is_fusible_operation(&OpType::Einsum {
            spec: "ij,jk->ik".to_string()
        }));
    }

    #[test]
    fn test_can_fuse_unary_nodes() {
        let node1 = EinsumNode::elem_unary("relu", 0, 1);
        let node2 = EinsumNode::elem_unary("tanh", 1, 2);
        assert!(can_fuse_nodes(&node1, &node2));
    }

    #[test]
    fn test_can_fuse_unary_binary_nodes() {
        let node1 = EinsumNode::elem_unary("relu", 0, 1);
        let node2 = EinsumNode::elem_binary("add", 1, 2, 3);
        assert!(can_fuse_nodes(&node1, &node2));
    }

    #[test]
    fn test_cannot_fuse_einsum_nodes() {
        let node1 = EinsumNode::einsum("ij,jk->ik", vec![0, 1], vec![2]);
        let node2 = EinsumNode::einsum("ik,kl->il", vec![2, 3], vec![4]);
        // einsum nodes are not fusible via element-wise fusion
        assert!(!can_fuse_nodes(&node1, &node2));
    }

    #[test]
    fn test_fuse_elementwise_empty_graph() {
        let mut graph = EinsumGraph::new();
        let stats = fuse_elementwise_operations(&mut graph).expect("unwrap");
        assert_eq!(stats.ops_fused, 0);
        assert_eq!(stats.fusion_groups, 0);
    }

    #[test]
    fn test_fuse_elementwise_single_op() {
        let mut graph = EinsumGraph::new();
        let a = graph.add_tensor("A");
        let b = graph.add_tensor("B");
        graph
            .add_node(EinsumNode::elem_unary("relu", a, b))
            .expect("unwrap");

        let stats = fuse_elementwise_operations(&mut graph).expect("unwrap");
        // Single operation, nothing to fuse
        assert_eq!(stats.ops_fused, 0);
    }

    #[test]
    fn test_fuse_map_reduce_empty_graph() {
        let mut graph = EinsumGraph::new();
        let stats = fuse_map_reduce(&mut graph).expect("unwrap");
        assert_eq!(stats.ops_fused, 0);
    }

    #[test]
    fn test_fuse_einsum_empty_graph() {
        let mut graph = EinsumGraph::new();
        let stats = fuse_einsum_operations(&mut graph).expect("unwrap");
        assert_eq!(stats.ops_fused, 0);
    }

    #[test]
    fn test_fuse_all_empty_graph() {
        let mut graph = EinsumGraph::new();
        let stats = fuse_all(&mut graph).expect("unwrap");
        assert_eq!(stats.ops_fused, 0);
        assert_eq!(stats.fusion_groups, 0);
    }

    #[test]
    fn test_find_fusible_chains_empty() {
        let graph = EinsumGraph::new();
        let tensor_users = HashMap::new();
        let tensor_producer = HashMap::new();
        let chains = find_fusible_chains(&graph, &tensor_users, &tensor_producer);
        assert!(chains.is_empty());
    }

    #[test]
    fn test_can_fuse_einsums_single_user() {
        let tensor_users = HashMap::from([(1, vec![2])]);
        assert!(can_fuse_einsums("ij,jk->ik", "ik,kl->il", &tensor_users, 1));
    }

    #[test]
    fn test_cannot_fuse_einsums_multiple_users() {
        let tensor_users = HashMap::from([(1, vec![2, 3])]);
        assert!(!can_fuse_einsums(
            "ij,jk->ik",
            "ik,kl->il",
            &tensor_users,
            1
        ));
    }
}