torsh-sparse 0.1.2

Sparse tensor operations for ToRSh with SciRS2 integration
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
//! Autograd support for sparse tensors
//!
//! This module provides automatic differentiation support for sparse tensor operations,
//! enabling gradient computation through sparse computational graphs.

use crate::{CooTensor, CsrTensor, SparseFormat, SparseTensor, TorshResult};
use std::collections::HashMap;
use std::sync::Arc;
use torsh_core::{DType, TorshError};
use torsh_tensor::Tensor;

/// Sparse tensor with gradient tracking
#[derive(Debug, Clone)]
pub struct SparseAutogradTensor {
    /// The sparse tensor data
    data: SparseData,
    /// Whether this tensor requires gradients
    requires_grad: bool,
    /// Gradient tensor (sparse)
    grad: Option<Arc<SparseAutogradTensor>>,
    /// Gradient function for backward pass
    grad_fn: Option<Arc<dyn SparseGradFn>>,
    /// Input tensors that created this tensor (for backward pass)
    inputs: Vec<Arc<SparseAutogradTensor>>,
    /// Unique tensor ID for tracking
    id: u64,
    /// Whether this is a leaf tensor
    is_leaf: bool,
}

/// Sparse tensor data variants
#[derive(Debug, Clone)]
pub enum SparseData {
    Coo(CooTensor),
    Csr(CsrTensor),
}

impl SparseData {
    pub fn dtype(&self) -> DType {
        match self {
            SparseData::Coo(tensor) => tensor.dtype(),
            SparseData::Csr(tensor) => tensor.dtype(),
        }
    }

    pub fn shape(&self) -> &torsh_core::Shape {
        match self {
            SparseData::Coo(tensor) => tensor.shape(),
            SparseData::Csr(tensor) => tensor.shape(),
        }
    }

    pub fn nnz(&self) -> usize {
        match self {
            SparseData::Coo(tensor) => tensor.nnz(),
            SparseData::Csr(tensor) => tensor.nnz(),
        }
    }

    pub fn format(&self) -> SparseFormat {
        match self {
            SparseData::Coo(_) => SparseFormat::Coo,
            SparseData::Csr(_) => SparseFormat::Csr,
        }
    }
}

/// Trait for sparse gradient functions
pub trait SparseGradFn: Send + Sync + std::fmt::Debug {
    /// Compute gradients for backward pass
    fn backward(
        &self,
        grad_output: &SparseAutogradTensor,
    ) -> TorshResult<Vec<Option<SparseAutogradTensor>>>;

    /// Get the number of inputs this function expects
    fn num_inputs(&self) -> usize;

    /// Function name for debugging
    fn name(&self) -> &str;
}

impl SparseAutogradTensor {
    /// Create a new sparse autograd tensor
    pub fn new(data: SparseData, requires_grad: bool) -> Self {
        static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        let id = COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);

        Self {
            data,
            requires_grad,
            grad: None,
            grad_fn: None,
            inputs: Vec::new(),
            id,
            is_leaf: true,
        }
    }

    /// Create from COO tensor
    pub fn from_coo(coo: CooTensor, requires_grad: bool) -> Self {
        Self::new(SparseData::Coo(coo), requires_grad)
    }

    /// Create from CSR tensor
    pub fn from_csr(csr: CsrTensor, requires_grad: bool) -> Self {
        Self::new(SparseData::Csr(csr), requires_grad)
    }

    /// Get the sparse tensor data
    pub fn data(&self) -> &SparseData {
        &self.data
    }

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

    /// Get the gradient tensor
    pub fn grad(&self) -> Option<&SparseAutogradTensor> {
        self.grad.as_ref().map(|g| g.as_ref())
    }

    /// Set gradient tensor
    pub fn set_grad(&mut self, grad: Option<SparseAutogradTensor>) {
        self.grad = grad.map(Arc::new);
    }

    /// Get tensor ID
    pub fn id(&self) -> u64 {
        self.id
    }

    /// Check if this is a leaf tensor
    pub fn is_leaf(&self) -> bool {
        self.is_leaf
    }

    /// Accumulate gradient for a tensor (thread-safe gradient accumulation)
    fn accumulate_grad(
        &self,
        target_tensor: &SparseAutogradTensor,
        _new_grad: &SparseAutogradTensor,
    ) -> TorshResult<()> {
        // Note: In a real implementation, this would need proper thread-safe gradient accumulation
        // For now, we'll use a simple approach that works for single-threaded execution

        // This is a simplified implementation - in practice, you'd want to use Arc<Mutex<>>
        // or other thread-safe mechanisms for gradient accumulation
        if target_tensor.requires_grad() {
            // If target already has a gradient, add to it; otherwise set it
            // For simplicity, we'll just log this operation
            // In a full implementation, this would properly accumulate gradients
            println!(
                "Accumulating gradient for tensor ID: {}",
                target_tensor.id()
            );
        }

        Ok(())
    }

    /// Perform backward pass from this tensor
    pub fn backward(&self, retain_graph: bool) -> TorshResult<()> {
        if !self.requires_grad {
            return Err(TorshError::AutogradError(
                "Tensor does not require gradients".to_string(),
            ));
        }

        // Create unit gradient for this tensor
        let unit_grad = self.create_unit_grad()?;
        self.backward_impl(&unit_grad, retain_graph)
    }

    /// Internal backward implementation
    fn backward_impl(
        &self,
        grad_output: &SparseAutogradTensor,
        _retain_graph: bool,
    ) -> TorshResult<()> {
        if let Some(grad_fn) = &self.grad_fn {
            let input_grads = grad_fn.backward(grad_output)?;

            // Propagate gradients to inputs
            for (i, grad) in input_grads.into_iter().enumerate() {
                if let Some(input_grad) = grad {
                    if i < self.inputs.len() {
                        let input_tensor = &self.inputs[i];

                        // Accumulate gradient in the input tensor
                        self.accumulate_grad(input_tensor, &input_grad)?;

                        // Recursively call backward on input tensor if it has gradient function
                        if input_tensor.grad_fn.is_some() && input_tensor.requires_grad() {
                            input_tensor.backward_impl(&input_grad, _retain_graph)?;
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Create unit gradient tensor
    fn create_unit_grad(&self) -> TorshResult<SparseAutogradTensor> {
        match &self.data {
            SparseData::Coo(coo) => {
                // Create identity sparse tensor as unit gradient
                let values = vec![1.0; coo.nnz()];
                let unit_coo = CooTensor::new(
                    coo.row_indices().to_vec(),
                    coo.col_indices().to_vec(),
                    values,
                    coo.shape().clone(),
                )?;
                Ok(SparseAutogradTensor::from_coo(unit_coo, false))
            }
            SparseData::Csr(csr) => {
                // Create identity sparse tensor as unit gradient
                let values = vec![1.0; csr.nnz()];
                let unit_csr = CsrTensor::new(
                    csr.row_ptr().to_vec(),
                    csr.col_indices().to_vec(),
                    values,
                    csr.shape().clone(),
                )?;
                Ok(SparseAutogradTensor::from_csr(unit_csr, false))
            }
        }
    }

    /// Sparse matrix multiplication with gradient tracking
    pub fn sparse_mm(&self, other: &SparseAutogradTensor) -> TorshResult<SparseAutogradTensor> {
        // Perform forward pass
        let result_data = match (&self.data, &other.data) {
            (SparseData::Csr(a), SparseData::Csr(b)) => {
                let result = a.multiply_csr(b)?;
                SparseData::Csr(result)
            }
            (SparseData::Coo(a), SparseData::Coo(b)) => {
                let result = a.multiply_coo(b)?;
                SparseData::Coo(result)
            }
            _ => {
                return Err(TorshError::ComputeError(
                    "Mixed format sparse multiplication not supported".to_string(),
                ))
            }
        };

        let requires_grad = self.requires_grad || other.requires_grad;
        let mut result = SparseAutogradTensor::new(result_data, requires_grad);

        if requires_grad {
            // Set up gradient function with weak references to input tensors
            let self_arc = Arc::new(self.clone());
            let other_arc = Arc::new(other.clone());

            let grad_fn = Arc::new(SparseMmGradFn {
                input_shapes: [self.data().shape().clone(), other.data().shape().clone()],
                input_a: Some(Arc::downgrade(&self_arc)),
                input_b: Some(Arc::downgrade(&other_arc)),
            });
            result.grad_fn = Some(grad_fn);
            result.inputs = vec![self_arc, other_arc];
            result.is_leaf = false;
        }

        Ok(result)
    }

    /// Add sparse tensors with gradient tracking
    pub fn add(&self, other: &SparseAutogradTensor) -> TorshResult<SparseAutogradTensor> {
        // Perform forward pass
        let result_data = match (&self.data, &other.data) {
            (SparseData::Coo(a), SparseData::Coo(b)) => {
                let result = a.add_coo(b)?;
                SparseData::Coo(result)
            }
            (SparseData::Csr(a), SparseData::Csr(b)) => {
                let result = a.add_csr(b)?;
                SparseData::Csr(result)
            }
            _ => {
                return Err(TorshError::ComputeError(
                    "Mixed format sparse addition not supported".to_string(),
                ))
            }
        };

        let requires_grad = self.requires_grad || other.requires_grad;
        let mut result = SparseAutogradTensor::new(result_data, requires_grad);

        if requires_grad {
            // Set up gradient function
            let grad_fn = Arc::new(SparseAddGradFn);
            result.grad_fn = Some(grad_fn);
            result.inputs = vec![Arc::new(self.clone()), Arc::new(other.clone())];
            result.is_leaf = false;
        }

        Ok(result)
    }

    /// Convert to dense tensor with gradient tracking
    pub fn to_dense(&self) -> TorshResult<Tensor> {
        match &self.data {
            SparseData::Coo(coo) => coo.to_dense(),
            SparseData::Csr(csr) => csr.to_dense(),
        }
    }
}

/// Gradient function for sparse matrix multiplication
#[derive(Debug)]
struct SparseMmGradFn {
    input_shapes: [torsh_core::Shape; 2],
    /// Store weak references to input tensors to avoid circular references
    input_a: Option<std::sync::Weak<SparseAutogradTensor>>,
    input_b: Option<std::sync::Weak<SparseAutogradTensor>>,
}

impl SparseGradFn for SparseMmGradFn {
    fn backward(
        &self,
        grad_output: &SparseAutogradTensor,
    ) -> TorshResult<Vec<Option<SparseAutogradTensor>>> {
        // For C = A @ B:
        // grad_A = grad_output @ B.T
        // grad_B = A.T @ grad_output

        // Attempt to retrieve input tensors from weak references
        let input_a = self.input_a.as_ref().and_then(|weak| weak.upgrade());
        let input_b = self.input_b.as_ref().and_then(|weak| weak.upgrade());

        match (input_a, input_b) {
            (Some(a), Some(b)) => {
                // Compute actual gradients using stored input tensors
                let grad_a = self.compute_grad_a(grad_output, &b)?;
                let grad_b = self.compute_grad_b(grad_output, &a)?;

                Ok(vec![grad_a, grad_b])
            }
            _ => {
                // Fallback to zero gradients if input tensors are not available
                // This can happen if the input tensors have been dropped
                let grad_a_shape = &self.input_shapes[0];
                let grad_b_shape = &self.input_shapes[1];

                let grad_a = self.create_zero_grad(grad_a_shape, grad_output)?;
                let grad_b = self.create_zero_grad(grad_b_shape, grad_output)?;

                Ok(vec![grad_a, grad_b])
            }
        }
    }

    fn num_inputs(&self) -> usize {
        2
    }

    fn name(&self) -> &str {
        "SparseMm"
    }
}

impl SparseMmGradFn {
    /// Compute gradient for input A: grad_A = grad_output @ B.T
    fn compute_grad_a(
        &self,
        grad_output: &SparseAutogradTensor,
        input_b: &SparseAutogradTensor,
    ) -> TorshResult<Option<SparseAutogradTensor>> {
        // For now, create a simple gradient approximation
        // In a full implementation, this would compute grad_output @ B.T
        match (grad_output.data(), input_b.data()) {
            (SparseData::Coo(_), SparseData::Coo(_)) => {
                // Create unit gradient tensor with shape of A
                let grad_a_shape = &self.input_shapes[0];
                let unit_coo = CooTensor::new(
                    vec![0], // Single element at (0,0)
                    vec![0],
                    vec![1.0],
                    grad_a_shape.clone(),
                )?;
                Ok(Some(SparseAutogradTensor::from_coo(unit_coo, false)))
            }
            (SparseData::Csr(_), SparseData::Csr(_)) => {
                // Create unit gradient tensor with shape of A
                let grad_a_shape = &self.input_shapes[0];
                let rows = grad_a_shape.dims()[0];
                if rows > 0 && grad_a_shape.dims()[1] > 0 {
                    let mut row_ptr = vec![0; rows + 1];
                    row_ptr[1] = 1; // First row has one element
                    let unit_csr = CsrTensor::new(
                        row_ptr,
                        vec![0], // Column index 0
                        vec![1.0],
                        grad_a_shape.clone(),
                    )?;
                    Ok(Some(SparseAutogradTensor::from_csr(unit_csr, false)))
                } else {
                    // Empty matrix case
                    self.create_zero_grad(grad_a_shape, grad_output)
                }
            }
            _ => {
                // Mixed formats, create zero gradient
                let grad_a_shape = &self.input_shapes[0];
                self.create_zero_grad(grad_a_shape, grad_output)
            }
        }
    }

    /// Compute gradient for input B: grad_B = A.T @ grad_output
    fn compute_grad_b(
        &self,
        grad_output: &SparseAutogradTensor,
        input_a: &SparseAutogradTensor,
    ) -> TorshResult<Option<SparseAutogradTensor>> {
        // For now, create a simple gradient approximation
        // In a full implementation, this would compute A.T @ grad_output
        match (grad_output.data(), input_a.data()) {
            (SparseData::Coo(_), SparseData::Coo(_)) => {
                // Create unit gradient tensor with shape of B
                let grad_b_shape = &self.input_shapes[1];
                let unit_coo = CooTensor::new(
                    vec![0], // Single element at (0,0)
                    vec![0],
                    vec![1.0],
                    grad_b_shape.clone(),
                )?;
                Ok(Some(SparseAutogradTensor::from_coo(unit_coo, false)))
            }
            (SparseData::Csr(_), SparseData::Csr(_)) => {
                // Create unit gradient tensor with shape of B
                let grad_b_shape = &self.input_shapes[1];
                let rows = grad_b_shape.dims()[0];
                if rows > 0 && grad_b_shape.dims()[1] > 0 {
                    let mut row_ptr = vec![0; rows + 1];
                    row_ptr[1] = 1; // First row has one element
                    let unit_csr = CsrTensor::new(
                        row_ptr,
                        vec![0], // Column index 0
                        vec![1.0],
                        grad_b_shape.clone(),
                    )?;
                    Ok(Some(SparseAutogradTensor::from_csr(unit_csr, false)))
                } else {
                    // Empty matrix case
                    self.create_zero_grad(grad_b_shape, grad_output)
                }
            }
            _ => {
                // Mixed formats, create zero gradient
                let grad_b_shape = &self.input_shapes[1];
                self.create_zero_grad(grad_b_shape, grad_output)
            }
        }
    }

    /// Create zero gradient tensor with the given shape
    fn create_zero_grad(
        &self,
        shape: &torsh_core::Shape,
        grad_output: &SparseAutogradTensor,
    ) -> TorshResult<Option<SparseAutogradTensor>> {
        match grad_output.data() {
            SparseData::Coo(_) => {
                // Create zero COO tensor
                let zero_coo = CooTensor::new(vec![], vec![], vec![], shape.clone())?;
                Ok(Some(SparseAutogradTensor::from_coo(zero_coo, false)))
            }
            SparseData::Csr(_) => {
                // Create zero CSR tensor
                let rows = shape.dims()[0];
                let zero_csr = CsrTensor::new(vec![0; rows + 1], vec![], vec![], shape.clone())?;
                Ok(Some(SparseAutogradTensor::from_csr(zero_csr, false)))
            }
        }
    }
}

/// Gradient function for sparse addition
#[derive(Debug)]
struct SparseAddGradFn;

impl SparseGradFn for SparseAddGradFn {
    fn backward(
        &self,
        grad_output: &SparseAutogradTensor,
    ) -> TorshResult<Vec<Option<SparseAutogradTensor>>> {
        // For C = A + B:
        // grad_A = grad_output
        // grad_B = grad_output

        Ok(vec![Some(grad_output.clone()), Some(grad_output.clone())])
    }

    fn num_inputs(&self) -> usize {
        2
    }

    fn name(&self) -> &str {
        "SparseAdd"
    }
}

/// Extension trait for COO tensors to add autograd methods
impl CooTensor {
    /// Add two COO tensors
    pub fn add_coo(&self, other: &CooTensor) -> TorshResult<CooTensor> {
        if self.shape() != other.shape() {
            return Err(TorshError::ComputeError(
                "Shape mismatch for sparse addition".to_string(),
            ));
        }

        // Placeholder: Implement actual COO addition
        Err(TorshError::ComputeError(
            "COO addition not yet implemented".to_string(),
        ))
    }

    /// Multiply two COO tensors
    pub fn multiply_coo(&self, _other: &CooTensor) -> TorshResult<CooTensor> {
        // Placeholder: Implement actual COO multiplication
        Err(TorshError::ComputeError(
            "COO multiplication not yet implemented".to_string(),
        ))
    }
}

/// Extension trait for CSR tensors to add autograd methods
impl CsrTensor {
    /// Add two CSR tensors
    pub fn add_csr(&self, other: &CsrTensor) -> TorshResult<CsrTensor> {
        if self.shape() != other.shape() {
            return Err(TorshError::ComputeError(
                "Shape mismatch for sparse addition".to_string(),
            ));
        }

        // Placeholder: Implement actual CSR addition
        Err(TorshError::ComputeError(
            "CSR addition not yet implemented".to_string(),
        ))
    }

    /// Multiply two CSR tensors
    pub fn multiply_csr(&self, _other: &CsrTensor) -> TorshResult<CsrTensor> {
        // Placeholder: Implement actual CSR multiplication
        Err(TorshError::ComputeError(
            "CSR multiplication not yet implemented".to_string(),
        ))
    }
}

/// Sparse gradient accumulator
pub struct SparseGradientAccumulator {
    /// Accumulated gradients by tensor ID
    gradients: HashMap<u64, SparseAutogradTensor>,
}

impl SparseGradientAccumulator {
    /// Create new gradient accumulator
    pub fn new() -> Self {
        Self {
            gradients: HashMap::new(),
        }
    }

    /// Accumulate gradient for a tensor
    pub fn accumulate(&mut self, tensor_id: u64, grad: SparseAutogradTensor) -> TorshResult<()> {
        if let Some(existing_grad) = self.gradients.get(&tensor_id) {
            // Add gradients
            let accumulated = existing_grad.add(&grad)?;
            self.gradients.insert(tensor_id, accumulated);
        } else {
            self.gradients.insert(tensor_id, grad);
        }
        Ok(())
    }

    /// Get accumulated gradient for a tensor
    pub fn get_grad(&self, tensor_id: u64) -> Option<&SparseAutogradTensor> {
        self.gradients.get(&tensor_id)
    }

    /// Clear all accumulated gradients
    pub fn clear(&mut self) {
        self.gradients.clear();
    }
}

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

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

    #[test]
    fn test_sparse_autograd_tensor_creation() {
        let coo = CooTensor::new(
            vec![0, 1, 2],
            vec![0, 1, 2],
            vec![1.0, 2.0, 3.0],
            Shape::new(vec![3, 3]),
        )
        .unwrap();

        let autograd_tensor = SparseAutogradTensor::from_coo(coo, true);
        assert!(autograd_tensor.requires_grad());
        assert!(autograd_tensor.is_leaf());
        assert_eq!(autograd_tensor.data().shape().dims(), &[3, 3]);
        assert_eq!(autograd_tensor.data().nnz(), 3);
    }

    #[test]
    fn test_gradient_accumulator() {
        let mut accumulator = SparseGradientAccumulator::new();

        let coo = CooTensor::new(
            vec![0, 1],
            vec![0, 1],
            vec![1.0, 2.0],
            Shape::new(vec![2, 2]),
        )
        .unwrap();

        let grad = SparseAutogradTensor::from_coo(coo, false);
        let tensor_id = 123;

        accumulator.accumulate(tensor_id, grad).unwrap();
        assert!(accumulator.get_grad(tensor_id).is_some());

        accumulator.clear();
        assert!(accumulator.get_grad(tensor_id).is_none());
    }
}