axonml-jit 0.5.0

JIT compilation for Axonml tensor operations
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//! Operation Tracing
//!
//! # File
//! `crates/axonml-jit/src/trace.rs`
//!
//! # Author
//! Andrew Jewell Sr - AutomataNexus
//!
//! # Updated
//! March 8, 2026
//!
//! # Disclaimer
//! Use at own risk. This software is provided "as is", without warranty of any
//! kind, express or implied. The author and AutomataNexus shall not be held
//! liable for any damages arising from the use of this software.

use crate::ir::{DataType, Graph, NodeId, Op, Shape};
use std::cell::RefCell;

/// A traced value representing a node in the computation graph.
#[derive(Debug, Clone, Copy)]
pub struct TracedValue {
    /// Node ID in the graph.
    pub(crate) id: NodeId,
    /// Reference to the tracer (for chaining operations).
    #[allow(dead_code)]
    tracer_id: usize,
}

impl TracedValue {
    /// Creates a new traced value.
    fn new(id: NodeId, tracer_id: usize) -> Self {
        Self { id, tracer_id }
    }

    /// Returns the node ID.
    pub fn node_id(&self) -> NodeId {
        self.id
    }

    // Binary operations

    /// Element-wise addition.
    pub fn add(&self, other: &TracedValue) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.binary_op(
                Op::Add {
                    lhs: self.id,
                    rhs: other.id,
                },
                self.id,
                other.id,
            )
        })
    }

    /// Element-wise subtraction.
    pub fn sub(&self, other: &TracedValue) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.binary_op(
                Op::Sub {
                    lhs: self.id,
                    rhs: other.id,
                },
                self.id,
                other.id,
            )
        })
    }

    /// Element-wise multiplication.
    pub fn mul(&self, other: &TracedValue) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.binary_op(
                Op::Mul {
                    lhs: self.id,
                    rhs: other.id,
                },
                self.id,
                other.id,
            )
        })
    }

    /// Element-wise division.
    pub fn div(&self, other: &TracedValue) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.binary_op(
                Op::Div {
                    lhs: self.id,
                    rhs: other.id,
                },
                self.id,
                other.id,
            )
        })
    }

    /// Element-wise power.
    pub fn pow(&self, exp: &TracedValue) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.binary_op(
                Op::Pow {
                    base: self.id,
                    exp: exp.id,
                },
                self.id,
                exp.id,
            )
        })
    }

    /// Matrix multiplication.
    pub fn matmul(&self, other: &TracedValue) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.matmul_op(self.id, other.id)
        })
    }

    // Scalar operations

    /// Add scalar.
    pub fn add_scalar(&self, scalar: f64) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.unary_op(
                Op::AddScalar {
                    input: self.id,
                    scalar,
                },
                self.id,
            )
        })
    }

    /// Multiply by scalar.
    pub fn mul_scalar(&self, scalar: f64) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.unary_op(
                Op::MulScalar {
                    input: self.id,
                    scalar,
                },
                self.id,
            )
        })
    }

    // Unary operations

    /// Negation.
    pub fn neg(&self) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.unary_op(Op::Neg { input: self.id }, self.id)
        })
    }

    /// Absolute value.
    pub fn abs(&self) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.unary_op(Op::Abs { input: self.id }, self.id)
        })
    }

    /// Square root.
    pub fn sqrt(&self) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.unary_op(Op::Sqrt { input: self.id }, self.id)
        })
    }

    /// Exponential.
    pub fn exp(&self) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.unary_op(Op::Exp { input: self.id }, self.id)
        })
    }

    /// Natural logarithm.
    pub fn log(&self) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.unary_op(Op::Log { input: self.id }, self.id)
        })
    }

    /// Sine.
    pub fn sin(&self) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.unary_op(Op::Sin { input: self.id }, self.id)
        })
    }

    /// Cosine.
    pub fn cos(&self) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.unary_op(Op::Cos { input: self.id }, self.id)
        })
    }

    /// Hyperbolic tangent.
    pub fn tanh(&self) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.unary_op(Op::Tanh { input: self.id }, self.id)
        })
    }

    // Activation functions

    /// ReLU activation.
    pub fn relu(&self) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.unary_op(Op::Relu { input: self.id }, self.id)
        })
    }

    /// Sigmoid activation.
    pub fn sigmoid(&self) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.unary_op(Op::Sigmoid { input: self.id }, self.id)
        })
    }

    /// GELU activation.
    pub fn gelu(&self) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.unary_op(Op::Gelu { input: self.id }, self.id)
        })
    }

    /// SiLU/Swish activation.
    pub fn silu(&self) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.unary_op(Op::Silu { input: self.id }, self.id)
        })
    }

    // Reduction operations

    /// Sum over all elements.
    pub fn sum(&self) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.reduction_op(Op::Sum { input: self.id }, self.id, None, false)
        })
    }

    /// Sum over axis.
    pub fn sum_axis(&self, axis: i32, keepdim: bool) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.reduction_op(
                Op::SumAxis {
                    input: self.id,
                    axis,
                    keepdim,
                },
                self.id,
                Some(axis),
                keepdim,
            )
        })
    }

    /// Mean over all elements.
    pub fn mean(&self) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.reduction_op(Op::Mean { input: self.id }, self.id, None, false)
        })
    }

    /// Mean over axis.
    pub fn mean_axis(&self, axis: i32, keepdim: bool) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.reduction_op(
                Op::MeanAxis {
                    input: self.id,
                    axis,
                    keepdim,
                },
                self.id,
                Some(axis),
                keepdim,
            )
        })
    }

    // Shape operations

    /// Reshape tensor.
    pub fn reshape(&self, shape: &[isize]) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.reshape_op(self.id, shape)
        })
    }

    /// Transpose dimensions.
    pub fn transpose(&self, dim0: usize, dim1: usize) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.transpose_op(self.id, dim0, dim1)
        })
    }

    /// Squeeze dimension.
    pub fn squeeze(&self, dim: i32) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.squeeze_op(self.id, dim)
        })
    }

    /// Unsqueeze (add dimension).
    pub fn unsqueeze(&self, dim: i32) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            tracer.unsqueeze_op(self.id, dim)
        })
    }
}

// Thread-local tracer for operation recording
thread_local! {
    static TRACER: RefCell<TracerState> = RefCell::new(TracerState::new());
}

/// Internal tracer state.
struct TracerState {
    graph: Graph,
    active: bool,
    tracer_id: usize,
}

impl TracerState {
    fn new() -> Self {
        Self {
            graph: Graph::new(),
            active: false,
            tracer_id: 0,
        }
    }

    fn unary_op(&mut self, op: Op, input: NodeId) -> TracedValue {
        let node = self.graph.node(input);
        let dtype = node.dtype;
        let shape = node.shape.clone();
        let id = self.graph.add_node(op, dtype, shape);
        TracedValue::new(id, self.tracer_id)
    }

    fn binary_op(&mut self, op: Op, lhs: NodeId, rhs: NodeId) -> TracedValue {
        let lhs_node = self.graph.node(lhs);
        let rhs_node = self.graph.node(rhs);

        // Use broadcast shape
        let shape = lhs_node
            .shape
            .broadcast_shape(&rhs_node.shape)
            .unwrap_or_else(|| lhs_node.shape.clone());
        let dtype = lhs_node.dtype; // Assume same dtype

        let id = self.graph.add_node(op, dtype, shape);
        TracedValue::new(id, self.tracer_id)
    }

    fn matmul_op(&mut self, lhs: NodeId, rhs: NodeId) -> TracedValue {
        let lhs_node = self.graph.node(lhs);
        let rhs_node = self.graph.node(rhs);

        let lhs_shape = lhs_node.shape.dims();
        let rhs_shape = rhs_node.shape.dims();

        // Compute output shape for matmul
        let mut output_shape = lhs_shape[..lhs_shape.len() - 1].to_vec();
        if rhs_shape.len() > 1 {
            output_shape.push(rhs_shape[rhs_shape.len() - 1]);
        }

        let id = self.graph.add_node(
            Op::MatMul { lhs, rhs },
            lhs_node.dtype,
            Shape::from(output_shape),
        );
        TracedValue::new(id, self.tracer_id)
    }

    fn reduction_op(
        &mut self,
        op: Op,
        input: NodeId,
        axis: Option<i32>,
        keepdim: bool,
    ) -> TracedValue {
        let node = self.graph.node(input);
        let dtype = node.dtype;

        let shape = if let Some(ax) = axis {
            let mut dims = node.shape.dims().to_vec();
            let ax = if ax < 0 {
                (dims.len() as i32 + ax) as usize
            } else {
                ax as usize
            };
            if keepdim {
                dims[ax] = 1;
            } else {
                dims.remove(ax);
            }
            Shape::from(dims)
        } else {
            // Full reduction
            if keepdim {
                Shape::from(vec![1; node.shape.ndim()])
            } else {
                Shape::from(vec![])
            }
        };

        let id = self.graph.add_node(op, dtype, shape);
        TracedValue::new(id, self.tracer_id)
    }

    fn reshape_op(&mut self, input: NodeId, new_shape: &[isize]) -> TracedValue {
        let node = self.graph.node(input);
        let dtype = node.dtype;
        let old_numel = node.shape.numel();

        // Resolve -1 in shape
        let mut shape: Vec<usize> = Vec::with_capacity(new_shape.len());
        let mut neg_idx = None;
        let mut known_numel = 1usize;

        for (i, &dim) in new_shape.iter().enumerate() {
            if dim == -1 {
                neg_idx = Some(i);
                shape.push(0); // Placeholder
            } else {
                let d = dim as usize;
                known_numel *= d;
                shape.push(d);
            }
        }

        if let Some(idx) = neg_idx {
            shape[idx] = old_numel / known_numel;
        }

        let id = self.graph.add_node(
            Op::Reshape {
                input,
                shape: new_shape.to_vec(),
            },
            dtype,
            Shape::from(shape),
        );
        TracedValue::new(id, self.tracer_id)
    }

    fn transpose_op(&mut self, input: NodeId, dim0: usize, dim1: usize) -> TracedValue {
        let node = self.graph.node(input);
        let dtype = node.dtype;

        let mut shape = node.shape.dims().to_vec();
        shape.swap(dim0, dim1);

        let id = self.graph.add_node(
            Op::Transpose { input, dim0, dim1 },
            dtype,
            Shape::from(shape),
        );
        TracedValue::new(id, self.tracer_id)
    }

    fn squeeze_op(&mut self, input: NodeId, dim: i32) -> TracedValue {
        let node = self.graph.node(input);
        let dtype = node.dtype;

        let mut shape = node.shape.dims().to_vec();
        let d = if dim < 0 {
            (shape.len() as i32 + dim) as usize
        } else {
            dim as usize
        };
        if shape[d] == 1 {
            shape.remove(d);
        }

        let id = self
            .graph
            .add_node(Op::Squeeze { input, dim }, dtype, Shape::from(shape));
        TracedValue::new(id, self.tracer_id)
    }

    fn unsqueeze_op(&mut self, input: NodeId, dim: i32) -> TracedValue {
        let node = self.graph.node(input);
        let dtype = node.dtype;

        let mut shape = node.shape.dims().to_vec();
        let d = if dim < 0 {
            (shape.len() as i32 + 1 + dim) as usize
        } else {
            dim as usize
        };
        shape.insert(d, 1);

        let id = self
            .graph
            .add_node(Op::Unsqueeze { input, dim }, dtype, Shape::from(shape));
        TracedValue::new(id, self.tracer_id)
    }
}

/// Tracer handle for recording operations.
pub struct Tracer {
    tracer_id: usize,
}

impl Tracer {
    /// Creates an input placeholder.
    pub fn input(&self, name: &str, shape: &[usize]) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            let id = tracer.graph.add_node(
                Op::Input {
                    name: name.to_string(),
                },
                DataType::F32,
                Shape::new(shape),
            );
            tracer.graph.register_input(name, id);
            TracedValue::new(id, self.tracer_id)
        })
    }

    /// Creates a constant tensor.
    pub fn constant(&self, value: f64, shape: &[usize]) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            let id =
                tracer
                    .graph
                    .add_node(Op::Constant { value }, DataType::F32, Shape::new(shape));
            TracedValue::new(id, self.tracer_id)
        })
    }

    /// Marks a value as output.
    pub fn output(&self, name: &str, value: TracedValue) -> TracedValue {
        TRACER.with(|t| {
            let mut tracer = t.borrow_mut();
            let node = tracer.graph.node(value.id);
            let dtype = node.dtype;
            let shape = node.shape.clone();

            let id = tracer.graph.add_node(
                Op::Output {
                    name: name.to_string(),
                    input: value.id,
                },
                dtype,
                shape,
            );
            tracer.graph.register_output(name, id);
            TracedValue::new(id, self.tracer_id)
        })
    }
}

/// Traces operations and builds a computation graph.
///
/// # Example
///
/// ```
/// use axonml_jit::trace;
///
/// let graph = trace(|tracer| {
///     let a = tracer.input("a", &[2, 3]);
///     let b = tracer.input("b", &[2, 3]);
///     let c = a.add(&b).relu();
///     tracer.output("result", c)
/// });
///
/// assert_eq!(graph.inputs().len(), 2);
/// ```
pub fn trace<F>(f: F) -> Graph
where
    F: FnOnce(&Tracer) -> TracedValue,
{
    TRACER.with(|t| {
        // Initialize fresh graph
        let mut tracer = t.borrow_mut();
        tracer.graph = Graph::new();
        tracer.active = true;
        tracer.tracer_id += 1;
        let tracer_id = tracer.tracer_id;
        drop(tracer);

        // Run the tracing function
        let tracer_handle = Tracer { tracer_id };
        let _ = f(&tracer_handle);

        // Extract the graph
        let mut tracer = t.borrow_mut();
        tracer.active = false;
        std::mem::take(&mut tracer.graph)
    })
}

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

    #[test]
    fn test_trace_simple() {
        let graph = trace(|tracer| {
            let a = tracer.input("a", &[2, 3]);
            let b = tracer.input("b", &[2, 3]);
            let c = a.add(&b);
            tracer.output("result", c)
        });

        assert_eq!(graph.inputs().len(), 2);
        assert_eq!(graph.outputs().len(), 1);
        assert!(graph.validate().is_ok());
    }

    #[test]
    fn test_trace_chain() {
        let graph = trace(|tracer| {
            let x = tracer.input("x", &[4, 4]);
            let y = x.relu().mul_scalar(2.0).add_scalar(1.0);
            tracer.output("y", y)
        });

        assert_eq!(graph.inputs().len(), 1);
        assert_eq!(graph.len(), 5); // input, relu, mul_scalar, add_scalar, output
    }

    #[test]
    fn test_trace_matmul() {
        let graph = trace(|tracer| {
            let a = tracer.input("a", &[2, 3]);
            let b = tracer.input("b", &[3, 4]);
            let c = a.matmul(&b);
            tracer.output("c", c)
        });

        let output_id = graph.output("c").unwrap();
        let output_node = graph.node(output_id);

        // Output should be the Output node which wraps matmul
        assert!(matches!(output_node.op, Op::Output { .. }));
    }

    #[test]
    fn test_trace_reduction() {
        let graph = trace(|tracer| {
            let x = tracer.input("x", &[2, 3, 4]);
            let y = x.sum_axis(1, true);
            tracer.output("y", y)
        });

        let output_id = graph.output("y").unwrap();
        let output_node = graph.node(output_id);
        // Shape should be [2, 1, 4]
        if let Op::Output { input, .. } = &output_node.op {
            let sum_node = graph.node(*input);
            assert_eq!(sum_node.shape.dims(), &[2, 1, 4]);
        }
    }
}