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
489
490
491
492
493
494
495
496
497
498
499
500
//! Dummy executor implementation for testing.

use std::collections::HashMap;

use tensorlogic_ir::{EinsumGraph, OpType};

use crate::batch::{BatchResult, TlBatchExecutor};
use crate::capabilities::{BackendCapabilities, DType, DeviceType, Feature, TlCapabilities};
use crate::dummy_tensor::DummyTensor;
use crate::error::ExecutorError;
use crate::ops::{ElemOp, ReduceOp};
use crate::profiling::{Profiler, TlProfiledExecutor};
use crate::traits::{TlAutodiff, TlExecutor};

/// Minimal executor implementation for testing and prototyping.
///
/// This provides a simple, reference implementation that verifies
/// the execution logic without requiring heavy dependencies.
pub struct DummyExecutor {
    pub tensors: HashMap<String, DummyTensor>,
    capabilities: BackendCapabilities,
    profiler: Option<Profiler>,
}

impl DummyExecutor {
    pub fn new() -> Self {
        let capabilities = BackendCapabilities::new("DummyExecutor", "0.1.0")
            .with_device(DeviceType::CPU)
            .with_dtype(DType::F64)
            .with_feature(Feature::Autodiff)
            .with_feature(Feature::BatchExecution)
            .with_max_dims(16);

        DummyExecutor {
            tensors: HashMap::new(),
            capabilities,
            profiler: None,
        }
    }

    pub fn add_tensor(&mut self, name: impl Into<String>, tensor: DummyTensor) {
        self.tensors.insert(name.into(), tensor);
    }

    pub fn get_tensor(&self, name: &str) -> Option<&DummyTensor> {
        self.tensors.get(name)
    }
}

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

impl TlExecutor for DummyExecutor {
    type Tensor = DummyTensor;
    type Error = ExecutorError;

    fn einsum(&mut self, spec: &str, inputs: &[Self::Tensor]) -> Result<Self::Tensor, Self::Error> {
        if inputs.is_empty() {
            return Err(ExecutorError::InvalidEinsumSpec(
                "No input tensors".to_string(),
            ));
        }

        // Simple stub: just return a tensor with the same shape as the first input
        let output_shape = inputs[0].shape.clone();
        let output_size: usize = output_shape.iter().product();

        let result_data = vec![1.0; output_size];

        Ok(DummyTensor {
            name: format!("einsum({})", spec),
            shape: output_shape,
            data: result_data,
        })
    }

    fn elem_op(&mut self, op: ElemOp, x: &Self::Tensor) -> Result<Self::Tensor, Self::Error> {
        // Check if this is actually a unary operation
        match op {
            ElemOp::Relu | ElemOp::Sigmoid | ElemOp::OneMinus => {}
            _ => {
                return Err(ExecutorError::UnsupportedOperation(format!(
                    "Operation {:?} is not a unary operation",
                    op
                )))
            }
        }

        let result_data: Vec<f64> = x
            .data
            .iter()
            .map(|&val| match op {
                ElemOp::Relu => val.max(0.0),
                ElemOp::Sigmoid => 1.0 / (1.0 + (-val).exp()),
                ElemOp::OneMinus => 1.0 - val,
                _ => unreachable!(),
            })
            .collect();

        Ok(DummyTensor {
            name: format!("{:?}({})", op, x.name),
            shape: x.shape.clone(),
            data: result_data,
        })
    }

    fn elem_op_binary(
        &mut self,
        op: ElemOp,
        x: &Self::Tensor,
        y: &Self::Tensor,
    ) -> Result<Self::Tensor, Self::Error> {
        if x.shape != y.shape {
            return Err(ExecutorError::ShapeMismatch(format!(
                "{:?} vs {:?}",
                x.shape, y.shape
            )));
        }

        let result_data: Vec<f64> = x
            .data
            .iter()
            .zip(y.data.iter())
            .map(|(&a, &b)| match op {
                // Arithmetic operations
                ElemOp::Add => a + b,
                ElemOp::Subtract => a - b,
                ElemOp::Multiply => a * b,
                ElemOp::Divide => {
                    if b.abs() < 1e-10 {
                        0.0 // Avoid division by zero
                    } else {
                        a / b
                    }
                }
                ElemOp::Min => a.min(b),
                ElemOp::Max => a.max(b),

                // Comparison operations (return 0.0 or 1.0)
                ElemOp::Eq => {
                    if (a - b).abs() < 1e-10 {
                        1.0
                    } else {
                        0.0
                    }
                }
                ElemOp::Lt => {
                    if a < b {
                        1.0
                    } else {
                        0.0
                    }
                }
                ElemOp::Gt => {
                    if a > b {
                        1.0
                    } else {
                        0.0
                    }
                }
                ElemOp::Lte => {
                    if a <= b {
                        1.0
                    } else {
                        0.0
                    }
                }
                ElemOp::Gte => {
                    if a >= b {
                        1.0
                    } else {
                        0.0
                    }
                }

                // Extended logical operations
                ElemOp::OrMax => a.max(b),
                ElemOp::OrProbSum => a + b - a * b, // Probabilistic sum: 1 - (1-a)(1-b)
                ElemOp::Nand => 1.0 - (a * b),
                ElemOp::Nor => 1.0 - a.max(b),
                ElemOp::Xor => (a - b).abs(), // Soft XOR: |a - b|

                // Unary operations shouldn't be called on binary
                ElemOp::Relu | ElemOp::Sigmoid | ElemOp::OneMinus => {
                    unreachable!("Unary operation {:?} called on binary", op)
                }
            })
            .collect();

        Ok(DummyTensor {
            name: format!("{:?}({},{})", op, x.name, y.name),
            shape: x.shape.clone(),
            data: result_data,
        })
    }

    fn reduce(
        &mut self,
        op: ReduceOp,
        x: &Self::Tensor,
        axes: &[usize],
    ) -> Result<Self::Tensor, Self::Error> {
        if axes.is_empty() {
            return Ok(x.clone());
        }

        let rank = x.shape.len();
        let mut output_shape = x.shape.clone();
        for &axis in axes.iter().rev() {
            if axis >= rank {
                return Err(ExecutorError::InvalidAxis { axis, rank });
            }
            output_shape.remove(axis);
        }

        let output_size: usize = if output_shape.is_empty() {
            1
        } else {
            output_shape.iter().product()
        };

        let result_data = match op {
            ReduceOp::Sum => vec![x.data.iter().sum::<f64>(); output_size],
            ReduceOp::Max => {
                vec![x.data.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b)); output_size]
            }
            ReduceOp::Min => vec![x.data.iter().fold(f64::INFINITY, |a, &b| a.min(b)); output_size],
            ReduceOp::Mean => vec![x.data.iter().sum::<f64>() / x.size() as f64; output_size],
            ReduceOp::Product => vec![x.data.iter().product::<f64>(); output_size],
        };

        Ok(DummyTensor {
            name: format!("{:?}({},axes={:?})", op, x.name, axes),
            shape: if output_shape.is_empty() {
                vec![1]
            } else {
                output_shape
            },
            data: result_data,
        })
    }
}

// TlCapabilities implementation
impl TlCapabilities for DummyExecutor {
    fn capabilities(&self) -> &BackendCapabilities {
        &self.capabilities
    }

    fn supports_elem_op(&self, _op: ElemOp) -> bool {
        true // DummyExecutor supports all element ops
    }

    fn supports_reduce_op(&self, _op: ReduceOp) -> bool {
        true // DummyExecutor supports all reduce ops
    }

    fn supports_einsum(&self, _spec: &str) -> bool {
        true // DummyExecutor has basic einsum support
    }
}

// TlProfiledExecutor implementation
impl TlProfiledExecutor for DummyExecutor {
    fn profiler(&self) -> Option<&Profiler> {
        self.profiler.as_ref()
    }

    fn profiler_mut(&mut self) -> Option<&mut Profiler> {
        self.profiler.as_mut()
    }

    fn enable_profiling(&mut self) {
        let mut profiler = Profiler::new();
        profiler.start();
        self.profiler = Some(profiler);
    }

    fn disable_profiling(&mut self) {
        if let Some(mut profiler) = self.profiler.take() {
            profiler.stop();
        }
    }
}

// TlBatchExecutor implementation
impl TlBatchExecutor for DummyExecutor {
    type Tensor = DummyTensor;
    type Error = ExecutorError;

    fn execute_batch(
        &mut self,
        graph: &EinsumGraph,
        batch_inputs: Vec<Vec<Self::Tensor>>,
    ) -> Result<BatchResult<Self::Tensor>, Self::Error> {
        if batch_inputs.is_empty() {
            return Err(ExecutorError::EmptyInput(
                "Batch inputs cannot be empty".to_string(),
            ));
        }

        let mut outputs = Vec::with_capacity(batch_inputs.len());
        for inputs in batch_inputs {
            let output = self.execute_graph_internal(graph, &inputs)?;
            outputs.push(output);
        }

        Ok(BatchResult::new(outputs))
    }

    fn execute_batch_parallel(
        &mut self,
        graph: &EinsumGraph,
        batch_inputs: Vec<Vec<Self::Tensor>>,
        _num_threads: Option<usize>,
    ) -> Result<BatchResult<Self::Tensor>, Self::Error> {
        // DummyExecutor doesn't support true parallel execution
        // Fall back to sequential execution
        self.execute_batch(graph, batch_inputs)
    }

    fn optimal_batch_size(&self) -> usize {
        16 // Conservative batch size for dummy executor
    }
}

// TlAutodiff implementation
impl TlAutodiff for DummyExecutor {
    type Tape = HashMap<usize, DummyTensor>;

    fn forward(&mut self, graph: &EinsumGraph) -> Result<Self::Tensor, Self::Error> {
        if graph.nodes.is_empty() {
            return Err(ExecutorError::EmptyInput(
                "Graph has no nodes to execute".to_string(),
            ));
        }

        // Execute the graph and return the last tensor
        let mut tensors: HashMap<usize, DummyTensor> = HashMap::new();

        // Initialize input tensors (first N tensors in the graph)
        // Note: In a real implementation, these would be provided as inputs
        for (idx, tensor_name) in graph.tensors.iter().enumerate() {
            // Create dummy tensors with default shape [10]
            tensors.insert(idx, DummyTensor::ones(tensor_name.clone(), vec![10]));
        }

        // Execute each node
        for (node_idx, node) in graph.nodes.iter().enumerate() {
            let output_idx = graph.tensors.len() + node_idx;
            let output = self.execute_node_internal(node, &tensors)?;
            tensors.insert(output_idx, output);
        }

        // Return the last computed tensor (or from outputs if specified)
        let output_idx = if graph.outputs.is_empty() {
            graph.tensors.len() + graph.nodes.len() - 1
        } else {
            graph.outputs[0]
        };

        tensors
            .remove(&output_idx)
            .ok_or_else(|| ExecutorError::TensorNotFound("Output tensor".to_string()))
    }

    fn backward(
        &mut self,
        graph: &EinsumGraph,
        _loss: &Self::Tensor,
    ) -> Result<Self::Tape, Self::Error> {
        // Simplified backward pass: just return unit gradients for all tensors
        let mut gradients = HashMap::new();

        for (idx, tensor_name) in graph.tensors.iter().enumerate() {
            gradients.insert(
                idx,
                DummyTensor::ones(format!("grad_{}", tensor_name), vec![10]),
            );
        }

        Ok(gradients)
    }
}

// Helper methods for DummyExecutor
impl DummyExecutor {
    fn execute_graph_internal(
        &mut self,
        graph: &EinsumGraph,
        _inputs: &[DummyTensor],
    ) -> Result<DummyTensor, ExecutorError> {
        // Simplified: just execute forward pass
        self.forward(graph)
    }

    fn execute_node_internal(
        &mut self,
        node: &tensorlogic_ir::EinsumNode,
        tensors: &HashMap<usize, DummyTensor>,
    ) -> Result<DummyTensor, ExecutorError> {
        match &node.op {
            OpType::Einsum { spec } => {
                let inputs: Vec<DummyTensor> =
                    node.inputs
                        .iter()
                        .map(|&idx| {
                            tensors.get(&idx).cloned().ok_or_else(|| {
                                ExecutorError::TensorNotFound(format!("Tensor {}", idx))
                            })
                        })
                        .collect::<Result<Vec<_>, _>>()?;

                self.einsum(spec, &inputs)
            }
            OpType::ElemUnary { op } => {
                if node.inputs.is_empty() {
                    return Err(ExecutorError::EmptyInput(
                        "ElemUnary requires an input".to_string(),
                    ));
                }
                let input = tensors.get(&node.inputs[0]).ok_or_else(|| {
                    ExecutorError::TensorNotFound(format!("Tensor {}", node.inputs[0]))
                })?;
                let elem_op = Self::parse_elem_op(op)?;
                self.elem_op(elem_op, input)
            }
            OpType::ElemBinary { op } => {
                if node.inputs.len() < 2 {
                    return Err(ExecutorError::EmptyInput(
                        "ElemBinary requires two inputs".to_string(),
                    ));
                }
                let input1 = tensors.get(&node.inputs[0]).ok_or_else(|| {
                    ExecutorError::TensorNotFound(format!("Tensor {}", node.inputs[0]))
                })?;
                let input2 = tensors.get(&node.inputs[1]).ok_or_else(|| {
                    ExecutorError::TensorNotFound(format!("Tensor {}", node.inputs[1]))
                })?;
                let elem_op = Self::parse_elem_op(op)?;
                self.elem_op_binary(elem_op, input1, input2)
            }
            OpType::Reduce { op, axes } => {
                if node.inputs.is_empty() {
                    return Err(ExecutorError::EmptyInput(
                        "Reduce requires an input".to_string(),
                    ));
                }
                let input = tensors.get(&node.inputs[0]).ok_or_else(|| {
                    ExecutorError::TensorNotFound(format!("Tensor {}", node.inputs[0]))
                })?;
                let reduce_op = Self::parse_reduce_op(op)?;
                self.reduce(reduce_op, input, axes)
            }
        }
    }

    fn parse_elem_op(op_str: &str) -> Result<ElemOp, ExecutorError> {
        match op_str.to_lowercase().as_str() {
            "relu" => Ok(ElemOp::Relu),
            "sigmoid" => Ok(ElemOp::Sigmoid),
            "oneminus" | "one_minus" => Ok(ElemOp::OneMinus),
            "add" => Ok(ElemOp::Add),
            "subtract" | "sub" => Ok(ElemOp::Subtract),
            "multiply" | "mul" => Ok(ElemOp::Multiply),
            "divide" | "div" => Ok(ElemOp::Divide),
            "eq" | "equal" => Ok(ElemOp::Eq),
            "lt" | "less" => Ok(ElemOp::Lt),
            "gt" | "greater" => Ok(ElemOp::Gt),
            "lte" | "le" => Ok(ElemOp::Lte),
            "gte" | "ge" => Ok(ElemOp::Gte),
            "ormax" | "or_max" => Ok(ElemOp::OrMax),
            "orprobsum" | "or_prob_sum" => Ok(ElemOp::OrProbSum),
            "nand" => Ok(ElemOp::Nand),
            "nor" => Ok(ElemOp::Nor),
            "xor" => Ok(ElemOp::Xor),
            _ => Err(ExecutorError::UnsupportedOperation(format!(
                "Unknown element operation: {}",
                op_str
            ))),
        }
    }

    fn parse_reduce_op(op_str: &str) -> Result<ReduceOp, ExecutorError> {
        match op_str.to_lowercase().as_str() {
            "sum" => Ok(ReduceOp::Sum),
            "max" => Ok(ReduceOp::Max),
            "min" => Ok(ReduceOp::Min),
            "mean" => Ok(ReduceOp::Mean),
            "product" | "prod" => Ok(ReduceOp::Product),
            _ => Err(ExecutorError::UnsupportedOperation(format!(
                "Unknown reduce operation: {}",
                op_str
            ))),
        }
    }
}