tensorlogic-scirs-backend 0.1.0

SciRS2-powered tensor execution backend 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
//! Execution tracing and debugging support.
//!
//! This module provides detailed logging and tracing of tensor operations,
//! enabling debugging of execution issues and performance analysis.

use scirs2_core::ndarray::ArrayD;
use std::fmt;
use std::time::{Duration, Instant};
use tensorlogic_ir::{EinsumNode, OpType};

/// Trace level for execution logging
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum TraceLevel {
    /// No tracing
    None = 0,
    /// Trace errors only
    Error = 1,
    /// Trace warnings and errors
    Warn = 2,
    /// Trace key operations
    Info = 3,
    /// Trace all operations
    Debug = 4,
    /// Trace everything including tensor values
    Trace = 5,
}

/// A single trace event during execution
#[derive(Debug, Clone)]
pub struct TraceEvent {
    /// Sequential event ID
    pub id: usize,
    /// When the event occurred (relative to trace start)
    pub timestamp: Duration,
    /// Operation being traced
    pub operation: String,
    /// Node index in the graph (if applicable)
    pub node_index: Option<usize>,
    /// Input tensor IDs
    pub inputs: Vec<String>,
    /// Output tensor IDs
    pub outputs: Vec<String>,
    /// Duration of the operation
    pub duration: Option<Duration>,
    /// Input tensor shapes
    pub input_shapes: Vec<Vec<usize>>,
    /// Output tensor shapes
    pub output_shapes: Vec<Vec<usize>>,
    /// Additional metadata
    pub metadata: Option<String>,
    /// Event level
    pub level: TraceLevel,
}

impl fmt::Display for TraceEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{:6}] {:?} {} ", self.id, self.level, self.operation)?;
        if let Some(node_idx) = self.node_index {
            write!(f, "(node {}) ", node_idx)?;
        }
        write!(f, "{:?} -> {:?}", self.inputs, self.outputs)?;
        if let Some(dur) = self.duration {
            write!(f, " [{:?}]", dur)?;
        }
        Ok(())
    }
}

/// Execution tracer for debugging and profiling
pub struct ExecutionTracer {
    /// Trace level
    level: TraceLevel,
    /// Collected trace events
    events: Vec<TraceEvent>,
    /// Start time for relative timestamps
    start_time: Instant,
    /// Next event ID
    next_id: usize,
    /// Whether tracing is enabled
    enabled: bool,
}

impl ExecutionTracer {
    /// Create a new tracer with the specified level
    pub fn new(level: TraceLevel) -> Self {
        Self {
            level,
            events: Vec::new(),
            start_time: Instant::now(),
            next_id: 0,
            enabled: level != TraceLevel::None,
        }
    }

    /// Create a disabled tracer
    pub fn disabled() -> Self {
        Self::new(TraceLevel::None)
    }

    /// Check if tracing is enabled
    pub fn is_enabled(&self) -> bool {
        self.enabled
    }

    /// Get the current trace level
    pub fn level(&self) -> TraceLevel {
        self.level
    }

    /// Set the trace level
    pub fn set_level(&mut self, level: TraceLevel) {
        self.level = level;
        self.enabled = level != TraceLevel::None;
    }

    /// Check if a specific level should be traced
    pub fn should_trace(&self, level: TraceLevel) -> bool {
        self.enabled && level <= self.level
    }

    /// Start tracing an operation
    pub fn start_operation(
        &mut self,
        operation: impl Into<String>,
        node_index: Option<usize>,
        inputs: Vec<String>,
        level: TraceLevel,
    ) -> Option<TraceHandle> {
        if !self.should_trace(level) {
            return None;
        }

        let id = self.next_id;
        self.next_id += 1;

        Some(TraceHandle {
            id,
            operation: operation.into(),
            node_index,
            inputs,
            start_time: Instant::now(),
            level,
        })
    }

    /// Finish tracing an operation
    pub fn finish_operation(
        &mut self,
        handle: TraceHandle,
        outputs: Vec<String>,
        input_shapes: Vec<Vec<usize>>,
        output_shapes: Vec<Vec<usize>>,
        metadata: Option<String>,
    ) {
        if !self.should_trace(handle.level) {
            return;
        }

        let duration = handle.start_time.elapsed();
        let timestamp = self.start_time.elapsed();

        let event = TraceEvent {
            id: handle.id,
            timestamp,
            operation: handle.operation,
            node_index: handle.node_index,
            inputs: handle.inputs,
            outputs,
            duration: Some(duration),
            input_shapes,
            output_shapes,
            metadata,
            level: handle.level,
        };

        self.events.push(event);
    }

    /// Record a simple event without timing
    pub fn record_event(
        &mut self,
        operation: impl Into<String>,
        level: TraceLevel,
        metadata: Option<String>,
    ) {
        if !self.should_trace(level) {
            return;
        }

        let id = self.next_id;
        self.next_id += 1;

        let event = TraceEvent {
            id,
            timestamp: self.start_time.elapsed(),
            operation: operation.into(),
            node_index: None,
            inputs: vec![],
            outputs: vec![],
            duration: None,
            input_shapes: vec![],
            output_shapes: vec![],
            metadata,
            level,
        };

        self.events.push(event);
    }

    /// Get all trace events
    pub fn events(&self) -> &[TraceEvent] {
        &self.events
    }

    /// Clear all trace events
    pub fn clear(&mut self) {
        self.events.clear();
        self.next_id = 0;
        self.start_time = Instant::now();
    }

    /// Get statistics about traced operations
    pub fn stats(&self) -> TraceStats {
        let mut total_ops = 0;
        let mut total_duration = Duration::ZERO;
        let mut op_counts: std::collections::HashMap<String, usize> =
            std::collections::HashMap::new();

        for event in &self.events {
            total_ops += 1;
            if let Some(dur) = event.duration {
                total_duration += dur;
            }
            *op_counts.entry(event.operation.clone()).or_insert(0) += 1;
        }

        TraceStats {
            total_operations: total_ops,
            total_duration,
            operation_counts: op_counts,
        }
    }

    /// Format trace as a string
    pub fn format_trace(&self) -> String {
        let mut output = String::new();
        output.push_str("=== Execution Trace ===\n");
        for event in &self.events {
            output.push_str(&format!("{}\n", event));
        }
        output.push_str("\n=== Statistics ===\n");
        let stats = self.stats();
        output.push_str(&format!("{}", stats));
        output
    }
}

impl Default for ExecutionTracer {
    fn default() -> Self {
        Self::disabled()
    }
}

/// Handle for an in-progress trace operation
pub struct TraceHandle {
    id: usize,
    operation: String,
    node_index: Option<usize>,
    inputs: Vec<String>,
    start_time: Instant,
    level: TraceLevel,
}

/// Statistics collected from trace events
#[derive(Debug, Clone)]
pub struct TraceStats {
    /// Total number of operations traced
    pub total_operations: usize,
    /// Total execution time
    pub total_duration: Duration,
    /// Count of each operation type
    pub operation_counts: std::collections::HashMap<String, usize>,
}

impl fmt::Display for TraceStats {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "Total operations: {}", self.total_operations)?;
        writeln!(f, "Total duration: {:?}", self.total_duration)?;
        writeln!(f, "\nOperation counts:")?;
        let mut counts: Vec<_> = self.operation_counts.iter().collect();
        counts.sort_by_key(|(_, count)| std::cmp::Reverse(**count));
        for (op, count) in counts {
            writeln!(f, "  {}: {}", op, count)?;
        }
        Ok(())
    }
}

/// Helper to extract operation name from EinsumNode
pub fn operation_name(node: &EinsumNode) -> String {
    match &node.op {
        OpType::Einsum { spec } => format!("einsum({})", spec),
        OpType::ElemUnary { op } => format!("unary({:?})", op),
        OpType::ElemBinary { op, .. } => format!("binary({:?})", op),
        OpType::Reduce { op, axes } => format!("reduce({:?}, axes={:?})", op, axes),
    }
}

/// Helper to get tensor shape as `Vec<usize>`
pub fn tensor_shape(tensor: &ArrayD<f64>) -> Vec<usize> {
    tensor.shape().to_vec()
}

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

    #[test]
    fn test_tracer_levels() {
        let mut tracer = ExecutionTracer::new(TraceLevel::Info);
        assert!(tracer.should_trace(TraceLevel::Error));
        assert!(tracer.should_trace(TraceLevel::Warn));
        assert!(tracer.should_trace(TraceLevel::Info));
        assert!(!tracer.should_trace(TraceLevel::Debug));
        assert!(!tracer.should_trace(TraceLevel::Trace));

        tracer.set_level(TraceLevel::Debug);
        assert!(tracer.should_trace(TraceLevel::Debug));
    }

    #[test]
    fn test_trace_operation() {
        let mut tracer = ExecutionTracer::new(TraceLevel::Info);

        let handle = tracer.start_operation(
            "test_op",
            Some(0),
            vec!["input1".to_string()],
            TraceLevel::Info,
        );
        assert!(handle.is_some());

        tracer.finish_operation(
            handle.expect("unwrap"),
            vec!["output1".to_string()],
            vec![vec![2, 3]],
            vec![vec![2, 3]],
            None,
        );

        assert_eq!(tracer.events().len(), 1);
        let event = &tracer.events()[0];
        assert_eq!(event.operation, "test_op");
        assert_eq!(event.node_index, Some(0));
    }

    #[test]
    fn test_tracer_disabled() {
        let mut tracer = ExecutionTracer::disabled();
        assert!(!tracer.is_enabled());

        let handle = tracer.start_operation("test_op", None, vec![], TraceLevel::Info);
        assert!(handle.is_none());
        assert_eq!(tracer.events().len(), 0);
    }

    #[test]
    fn test_trace_stats() {
        let mut tracer = ExecutionTracer::new(TraceLevel::Info);

        for i in 0..5 {
            let handle = tracer
                .start_operation("einsum", Some(i), vec![], TraceLevel::Info)
                .expect("unwrap");
            tracer.finish_operation(handle, vec![], vec![], vec![], None);
        }

        for i in 0..3 {
            let handle = tracer
                .start_operation("reduce", Some(i + 5), vec![], TraceLevel::Info)
                .expect("unwrap");
            tracer.finish_operation(handle, vec![], vec![], vec![], None);
        }

        let stats = tracer.stats();
        assert_eq!(stats.total_operations, 8);
        assert_eq!(stats.operation_counts.get("einsum"), Some(&5));
        assert_eq!(stats.operation_counts.get("reduce"), Some(&3));
    }

    #[test]
    fn test_trace_clear() {
        let mut tracer = ExecutionTracer::new(TraceLevel::Info);

        tracer.record_event("test", TraceLevel::Info, None);
        assert_eq!(tracer.events().len(), 1);

        tracer.clear();
        assert_eq!(tracer.events().len(), 0);
    }

    #[test]
    fn test_trace_format() {
        let mut tracer = ExecutionTracer::new(TraceLevel::Info);
        tracer.record_event("test_op", TraceLevel::Info, Some("metadata".to_string()));

        let output = tracer.format_trace();
        assert!(output.contains("Execution Trace"));
        assert!(output.contains("test_op"));
        assert!(output.contains("Statistics"));
    }
}