tensorlogic-infer 0.1.0

Execution and autodiff traits for TensorLogic inference engines
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
//! Eager mode automatic differentiation.
//!
//! This module provides eager execution with automatic differentiation,
//! similar to PyTorch's autograd or TensorFlow's eager execution.
//!
//! Unlike `TlAutodiff` which requires a full `EinsumGraph`, eager mode
//! computes gradients by building a dynamic computation graph as operations
//! are executed.
//!
//! # Example
//!
//! ```ignore
//! use tensorlogic_infer::eager::{Variable, TlEagerAutodiff};
//!
//! // Create variables
//! let x = Variable::new(tensor_x, true); // requires_grad = true
//! let y = Variable::new(tensor_y, true);
//!
//! // Execute operations eagerly
//! let z = executor.eager_add(&x, &y)?;
//! let loss = executor.eager_reduce_sum(&z)?;
//!
//! // Compute gradients
//! let grads = executor.eager_backward(&loss)?;
//! ```

use crate::ops::{ElemOp, ReduceOp};
use crate::traits::TlExecutor;
use std::collections::HashMap;

/// A variable in the eager execution graph.
///
/// Wraps a tensor and tracks whether gradients should be computed for it.
#[derive(Debug, Clone)]
pub struct Variable<T> {
    /// The tensor data
    pub tensor: T,
    /// Whether this variable requires gradient computation
    pub requires_grad: bool,
    /// Unique ID for gradient tracking
    pub id: usize,
}

impl<T> Variable<T> {
    /// Create a new variable.
    pub fn new(tensor: T, requires_grad: bool) -> Self {
        use std::sync::atomic::{AtomicUsize, Ordering};
        static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
        let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
        Variable {
            tensor,
            requires_grad,
            id,
        }
    }

    /// Create a constant (no gradient).
    pub fn constant(tensor: T) -> Self {
        Self::new(tensor, false)
    }

    /// Get a reference to the tensor.
    pub fn tensor(&self) -> &T {
        &self.tensor
    }

    /// Check if this variable requires gradients.
    pub fn requires_grad(&self) -> bool {
        self.requires_grad
    }
}

/// Gradient storage for a variable.
#[derive(Debug, Clone)]
pub struct VariableGrad<T> {
    /// The gradient tensor
    pub grad: T,
    /// Whether the gradient has been computed
    pub computed: bool,
}

impl<T> VariableGrad<T> {
    /// Create a new gradient container.
    pub fn new(grad: T) -> Self {
        VariableGrad {
            grad,
            computed: true,
        }
    }

    /// Create an uncomputed gradient placeholder.
    pub fn placeholder(grad: T) -> Self {
        VariableGrad {
            grad,
            computed: false,
        }
    }
}

/// Tape for recording eager operations and their gradients.
///
/// The tape stores the computation graph as operations are executed,
/// enabling backward pass for gradient computation.
#[derive(Debug)]
pub struct EagerTape<T> {
    /// Map from variable ID to gradient
    gradients: HashMap<usize, VariableGrad<T>>,
    /// Operations recorded on the tape
    operations: Vec<EagerOp<T>>,
}

impl<T> EagerTape<T> {
    /// Create a new empty tape.
    pub fn new() -> Self {
        EagerTape {
            gradients: HashMap::new(),
            operations: Vec::new(),
        }
    }

    /// Record an operation on the tape.
    pub fn record_op(&mut self, op: EagerOp<T>) {
        self.operations.push(op);
    }

    /// Set gradient for a variable.
    pub fn set_gradient(&mut self, var_id: usize, grad: VariableGrad<T>) {
        self.gradients.insert(var_id, grad);
    }

    /// Get gradient for a variable.
    pub fn get_gradient(&self, var_id: usize) -> Option<&VariableGrad<T>> {
        self.gradients.get(&var_id)
    }

    /// Get all gradients.
    pub fn gradients(&self) -> &HashMap<usize, VariableGrad<T>> {
        &self.gradients
    }

    /// Get all operations.
    pub fn operations(&self) -> &[EagerOp<T>] {
        &self.operations
    }

    /// Clear the tape.
    pub fn clear(&mut self) {
        self.gradients.clear();
        self.operations.clear();
    }

    /// Number of operations recorded.
    pub fn len(&self) -> usize {
        self.operations.len()
    }

    /// Check if tape is empty.
    pub fn is_empty(&self) -> bool {
        self.operations.is_empty()
    }
}

impl<T> Default for EagerTape<T> {
    fn default() -> Self {
        Self::new()
    }
}

/// An operation recorded in the eager execution tape.
#[derive(Debug, Clone)]
pub enum EagerOp<T> {
    /// Element-wise unary operation
    ElemUnary {
        op: ElemOp,
        input: Variable<T>,
        output: Variable<T>,
    },
    /// Element-wise binary operation
    ElemBinary {
        op: ElemOp,
        left: Variable<T>,
        right: Variable<T>,
        output: Variable<T>,
    },
    /// Reduction operation
    Reduce {
        op: ReduceOp,
        input: Variable<T>,
        axes: Vec<usize>,
        output: Variable<T>,
    },
    /// Einsum operation
    Einsum {
        spec: String,
        inputs: Vec<Variable<T>>,
        output: Variable<T>,
    },
}

/// Trait for eager execution with automatic differentiation.
///
/// This trait extends `TlExecutor` with eager autodiff capabilities,
/// allowing gradient computation without building a full graph upfront.
pub trait TlEagerAutodiff: TlExecutor {
    /// Execute element-wise unary operation eagerly.
    ///
    /// Returns a new variable containing the result. If the input requires
    /// gradients, the operation is recorded on the tape.
    fn eager_elem_op(
        &mut self,
        op: ElemOp,
        x: &Variable<Self::Tensor>,
    ) -> Result<Variable<Self::Tensor>, Self::Error>;

    /// Execute element-wise binary operation eagerly.
    fn eager_elem_op_binary(
        &mut self,
        op: ElemOp,
        x: &Variable<Self::Tensor>,
        y: &Variable<Self::Tensor>,
    ) -> Result<Variable<Self::Tensor>, Self::Error>;

    /// Execute reduction operation eagerly.
    fn eager_reduce(
        &mut self,
        op: ReduceOp,
        x: &Variable<Self::Tensor>,
        axes: &[usize],
    ) -> Result<Variable<Self::Tensor>, Self::Error>;

    /// Execute einsum operation eagerly.
    fn eager_einsum(
        &mut self,
        spec: &str,
        inputs: &[Variable<Self::Tensor>],
    ) -> Result<Variable<Self::Tensor>, Self::Error>;

    /// Compute gradients for all variables with respect to the output.
    ///
    /// This performs backpropagation through the recorded operations
    /// to compute gradients.
    fn eager_backward(
        &mut self,
        output: &Variable<Self::Tensor>,
    ) -> Result<EagerTape<Self::Tensor>, Self::Error>;

    /// Create a new empty tape for recording operations.
    fn create_tape(&self) -> EagerTape<Self::Tensor> {
        EagerTape::new()
    }
}

/// Convenience methods for common operations.
pub trait EagerOps: TlEagerAutodiff {
    /// Add two variables.
    fn eager_add(
        &mut self,
        x: &Variable<Self::Tensor>,
        y: &Variable<Self::Tensor>,
    ) -> Result<Variable<Self::Tensor>, Self::Error> {
        self.eager_elem_op_binary(ElemOp::Add, x, y)
    }

    /// Multiply two variables.
    fn eager_mul(
        &mut self,
        x: &Variable<Self::Tensor>,
        y: &Variable<Self::Tensor>,
    ) -> Result<Variable<Self::Tensor>, Self::Error> {
        self.eager_elem_op_binary(ElemOp::Multiply, x, y)
    }

    /// Subtract two variables.
    fn eager_sub(
        &mut self,
        x: &Variable<Self::Tensor>,
        y: &Variable<Self::Tensor>,
    ) -> Result<Variable<Self::Tensor>, Self::Error> {
        self.eager_elem_op_binary(ElemOp::Subtract, x, y)
    }

    /// Apply Relu activation.
    fn eager_relu(
        &mut self,
        x: &Variable<Self::Tensor>,
    ) -> Result<Variable<Self::Tensor>, Self::Error> {
        self.eager_elem_op(ElemOp::Relu, x)
    }

    /// Apply sigmoid activation.
    fn eager_sigmoid(
        &mut self,
        x: &Variable<Self::Tensor>,
    ) -> Result<Variable<Self::Tensor>, Self::Error> {
        self.eager_elem_op(ElemOp::Sigmoid, x)
    }

    /// Apply one-minus operation (1 - x).
    fn eager_one_minus(
        &mut self,
        x: &Variable<Self::Tensor>,
    ) -> Result<Variable<Self::Tensor>, Self::Error> {
        self.eager_elem_op(ElemOp::OneMinus, x)
    }

    /// Sum reduction along axes.
    fn eager_sum(
        &mut self,
        x: &Variable<Self::Tensor>,
        axes: &[usize],
    ) -> Result<Variable<Self::Tensor>, Self::Error> {
        self.eager_reduce(ReduceOp::Sum, x, axes)
    }

    /// Mean reduction along axes.
    fn eager_mean(
        &mut self,
        x: &Variable<Self::Tensor>,
        axes: &[usize],
    ) -> Result<Variable<Self::Tensor>, Self::Error> {
        self.eager_reduce(ReduceOp::Mean, x, axes)
    }

    /// Max reduction along axes.
    fn eager_max(
        &mut self,
        x: &Variable<Self::Tensor>,
        axes: &[usize],
    ) -> Result<Variable<Self::Tensor>, Self::Error> {
        self.eager_reduce(ReduceOp::Max, x, axes)
    }
}

/// Automatic implementation of EagerOps for any type implementing TlEagerAutodiff
impl<T: TlEagerAutodiff> EagerOps for T {}

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

    #[test]
    fn test_variable_creation() {
        let tensor = vec![1.0, 2.0, 3.0];
        let var = Variable::new(tensor.clone(), true);

        assert_eq!(var.tensor, tensor);
        assert!(var.requires_grad);
        // ID is assigned sequentially starting from 0
        // Just verify it's a valid ID (any value is fine)
    }

    #[test]
    fn test_variable_constant() {
        let tensor = vec![1.0, 2.0, 3.0];
        let var = Variable::constant(tensor.clone());

        assert_eq!(var.tensor, tensor);
        assert!(!var.requires_grad);
    }

    #[test]
    fn test_variable_unique_ids() {
        let var1 = Variable::new(vec![1.0], true);
        let var2 = Variable::new(vec![2.0], true);

        assert_ne!(var1.id, var2.id);
    }

    #[test]
    fn test_eager_tape_creation() {
        let tape: EagerTape<Vec<f64>> = EagerTape::new();

        assert!(tape.is_empty());
        assert_eq!(tape.len(), 0);
        assert_eq!(tape.gradients().len(), 0);
    }

    #[test]
    fn test_eager_tape_set_gradient() {
        let mut tape = EagerTape::new();
        let grad = VariableGrad::new(vec![1.0, 2.0, 3.0]);

        tape.set_gradient(1, grad);

        assert!(tape.get_gradient(1).is_some());
        assert!(tape.get_gradient(2).is_none());
    }

    #[test]
    fn test_eager_tape_clear() {
        let mut tape = EagerTape::new();
        tape.set_gradient(1, VariableGrad::new(vec![1.0]));

        assert!(!tape.is_empty() || !tape.gradients().is_empty());

        tape.clear();

        assert!(tape.is_empty());
        assert_eq!(tape.gradients().len(), 0);
    }

    #[test]
    fn test_variable_grad_creation() {
        let grad = VariableGrad::new(vec![1.0, 2.0]);

        assert!(grad.computed);
        assert_eq!(grad.grad, vec![1.0, 2.0]);
    }

    #[test]
    fn test_variable_grad_placeholder() {
        let grad = VariableGrad::placeholder(vec![0.0]);

        assert!(!grad.computed);
    }

    #[test]
    fn test_eager_op_variants() {
        let var1 = Variable::new(vec![1.0], true);
        let var2 = Variable::new(vec![2.0], true);
        let var3 = Variable::new(vec![3.0], true);

        // Test ElemUnary variant
        let _op1 = EagerOp::ElemUnary {
            op: ElemOp::OneMinus,
            input: var1.clone(),
            output: var3.clone(),
        };

        // Test ElemBinary variant
        let _op2 = EagerOp::ElemBinary {
            op: ElemOp::Add,
            left: var1.clone(),
            right: var2.clone(),
            output: var3.clone(),
        };

        // Test Reduce variant
        let _op3 = EagerOp::Reduce {
            op: ReduceOp::Sum,
            input: var1.clone(),
            axes: vec![0],
            output: var3.clone(),
        };

        // Test Einsum variant
        let _op4 = EagerOp::Einsum {
            spec: "ij,jk->ik".to_string(),
            inputs: vec![var1.clone(), var2.clone()],
            output: var3.clone(),
        };
    }

    #[test]
    fn test_tape_record_op() {
        let mut tape = EagerTape::new();
        let var1 = Variable::new(vec![1.0], true);
        let var2 = Variable::new(vec![2.0], true);

        let op = EagerOp::ElemBinary {
            op: ElemOp::Add,
            left: var1,
            right: var2.clone(),
            output: var2,
        };

        tape.record_op(op);

        assert_eq!(tape.len(), 1);
        assert!(!tape.is_empty());
    }

    #[test]
    fn test_variable_methods() {
        let tensor = vec![1.0, 2.0, 3.0];
        let var = Variable::new(tensor.clone(), true);

        assert_eq!(var.tensor(), &tensor);
        assert!(var.requires_grad());
    }

    #[test]
    fn test_tape_default() {
        let tape: EagerTape<Vec<f64>> = EagerTape::default();

        assert!(tape.is_empty());
    }
}