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
//! CSR (Compressed Sparse Row) sparse tensor format

use crate::{CooTensor, SparseFormat, SparseTensor, TorshResult};
use torsh_core::{device::DeviceType, DType, Shape, TorshError};
use torsh_tensor::{creation::zeros, Tensor};

/// CSR (Compressed Sparse Row) format sparse tensor
#[derive(Debug)]
pub struct CsrTensor {
    /// Row pointers (size: rows + 1)
    row_ptr: Vec<usize>,
    /// Column indices
    col_indices: Vec<usize>,
    /// Non-zero values
    values: Vec<f32>,
    /// Shape of the tensor
    shape: Shape,
    /// Data type
    dtype: DType,
    /// Device
    device: DeviceType,
}

impl CsrTensor {
    /// Create a new CSR tensor
    pub fn new(
        row_ptr: Vec<usize>,
        col_indices: Vec<usize>,
        values: Vec<f32>,
        shape: Shape,
    ) -> TorshResult<Self> {
        // Validate inputs
        if col_indices.len() != values.len() {
            return Err(TorshError::InvalidArgument(
                "Column indices and values must have the same length".to_string(),
            ));
        }

        if shape.ndim() != 2 {
            return Err(TorshError::InvalidArgument(
                "CSR format currently only supports 2D tensors".to_string(),
            ));
        }

        let rows = shape.dims()[0];
        if row_ptr.len() != rows + 1 {
            return Err(TorshError::InvalidArgument(format!(
                "Row pointer length must be rows + 1, got {} for {} rows",
                row_ptr.len(),
                rows
            )));
        }

        // Validate column indices are within bounds
        let cols = shape.dims()[1];
        for &col in &col_indices {
            if col >= cols {
                return Err(TorshError::InvalidArgument(format!(
                    "Column index {col} out of bounds for {cols} columns"
                )));
            }
        }

        Ok(Self {
            row_ptr,
            col_indices,
            values,
            shape,
            dtype: DType::F32,
            device: DeviceType::Cpu,
        })
    }

    /// Create from raw parts (used by custom kernels)
    pub fn from_raw_parts(
        row_ptr: Vec<usize>,
        col_indices: Vec<usize>,
        values: Vec<f32>,
        shape: Shape,
    ) -> TorshResult<Self> {
        Self::new(row_ptr, col_indices, values, shape)
    }

    /// Create an empty CSR tensor with given shape
    pub fn empty(shape: Shape) -> TorshResult<Self> {
        if shape.ndim() != 2 {
            return Err(TorshError::InvalidArgument(
                "CSR format currently only supports 2D tensors".to_string(),
            ));
        }

        let rows = shape.dims()[0];
        let row_ptr = vec![0; rows + 1];
        let col_indices = Vec::new();
        let values = Vec::new();

        Ok(Self {
            row_ptr,
            col_indices,
            values,
            shape,
            dtype: DType::F32,
            device: DeviceType::Cpu,
        })
    }

    /// Create from dense tensor
    pub fn from_dense(dense: &Tensor, threshold: f32) -> TorshResult<Self> {
        let coo = CooTensor::from_dense(dense, threshold)?;
        Self::from_coo(&coo)
    }

    /// Create from COO tensor
    pub fn from_coo(coo: &CooTensor) -> TorshResult<Self> {
        let shape = coo.shape().clone();
        let rows = shape.dims()[0];

        // Get sorted triplets
        let mut coo_sorted = coo.clone();
        coo_sorted.sort_indices();
        let triplets = coo_sorted.triplets();

        // Build CSR format
        let mut row_ptr = vec![0];
        let mut col_indices = Vec::new();
        let mut values = Vec::new();

        let mut current_row = 0;

        for (row, col, val) in triplets {
            // Fill in empty rows
            while current_row < row {
                row_ptr.push(col_indices.len());
                current_row += 1;
            }

            col_indices.push(col);
            values.push(val);
        }

        // Fill in any remaining empty rows
        while current_row < rows {
            row_ptr.push(col_indices.len());
            current_row += 1;
        }

        Self::new(row_ptr, col_indices, values, shape)
    }

    /// Get values for a specific row
    pub fn get_row(&self, row: usize) -> TorshResult<(Vec<usize>, Vec<f32>)> {
        if row >= self.shape.dims()[0] {
            return Err(TorshError::InvalidArgument(format!(
                "Row index {row} out of bounds"
            )));
        }

        let start = self.row_ptr[row];
        let end = self.row_ptr[row + 1];

        let cols = self.col_indices[start..end].to_vec();
        let vals = self.values[start..end].to_vec();

        Ok((cols, vals))
    }

    /// Get value at specific position (row, col)
    pub fn get(&self, row: usize, col: usize) -> Option<f32> {
        if row >= self.shape.dims()[0] || col >= self.shape.dims()[1] {
            return None;
        }

        let start = self.row_ptr[row];
        let end = self.row_ptr[row + 1];

        // Binary search for the column in this row
        for i in start..end {
            if self.col_indices[i] == col {
                return Some(self.values[i]);
            }
            if self.col_indices[i] > col {
                break; // Column indices are sorted
            }
        }

        None
    }

    /// Get triplets (row, col, value) for all non-zero elements
    pub fn triplets(&self) -> Vec<(usize, usize, f32)> {
        let mut result = Vec::new();

        for row in 0..self.shape.dims()[0] {
            let start = self.row_ptr[row];
            let end = self.row_ptr[row + 1];

            for i in start..end {
                result.push((row, self.col_indices[i], self.values[i]));
            }
        }

        result
    }

    /// Matrix-vector multiplication
    pub fn matvec(&self, vector: &Tensor) -> TorshResult<Tensor> {
        if vector.shape().ndim() != 1 {
            return Err(TorshError::InvalidArgument(
                "Vector must be 1-dimensional".to_string(),
            ));
        }

        if vector.shape().dims()[0] != self.shape.dims()[1] {
            return Err(TorshError::InvalidArgument(format!(
                "Vector length {} doesn't match matrix columns {}",
                vector.shape().dims()[0],
                self.shape.dims()[1]
            )));
        }

        let result = zeros::<f32>(&[self.shape.dims()[0]])?;

        for row in 0..self.shape.dims()[0] {
            let start = self.row_ptr[row];
            let end = self.row_ptr[row + 1];

            let mut sum = 0.0;
            for i in start..end {
                let col = self.col_indices[i];
                let val = self.values[i];
                sum += val * vector.get(&[col])?;
            }

            result.set(&[row], sum)?;
        }

        Ok(result)
    }

    /// Get row pointers
    pub fn row_ptr(&self) -> &[usize] {
        &self.row_ptr
    }

    /// Get column indices
    pub fn col_indices(&self) -> &[usize] {
        &self.col_indices
    }

    /// Get values
    pub fn values(&self) -> &[f32] {
        &self.values
    }

    /// Create CSR tensor from triplets (row_indices, col_indices, values)
    pub fn from_triplets(
        row_indices: Vec<usize>,
        col_indices: Vec<usize>,
        values: Vec<f32>,
        shape: [usize; 2],
    ) -> TorshResult<Self> {
        if row_indices.len() != col_indices.len() || row_indices.len() != values.len() {
            return Err(TorshError::InvalidArgument(
                "Row indices, column indices, and values must have the same length".to_string(),
            ));
        }

        let rows = shape[0];
        let cols = shape[1];

        // Build CSR format from triplets
        let mut row_ptr = vec![0; rows + 1];

        // Count non-zeros per row
        for &row in &row_indices {
            if row >= rows {
                return Err(TorshError::InvalidArgument(format!(
                    "Row index {} out of bounds for {} rows",
                    row, rows
                )));
            }
            row_ptr[row + 1] += 1;
        }

        // Convert counts to offsets
        for i in 1..=rows {
            row_ptr[i] += row_ptr[i - 1];
        }

        // Sort entries by row, then by column
        let mut triplets: Vec<(usize, usize, f32)> = row_indices
            .into_iter()
            .zip(col_indices.into_iter())
            .zip(values.into_iter())
            .map(|((r, c), v)| (r, c, v))
            .collect();

        triplets.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.cmp(&b.1)));

        let sorted_col_indices: Vec<usize> = triplets.iter().map(|(_, c, _)| *c).collect();
        let sorted_values: Vec<f32> = triplets.iter().map(|(_, _, v)| *v).collect();

        let shape = Shape::new(vec![rows, cols]);
        Self::new(row_ptr, sorted_col_indices, sorted_values, shape)
    }

    /// Convert CSR tensor to dense tensor
    pub fn to_dense(&self) -> TorshResult<Tensor<f32>> {
        let rows = self.shape.dims()[0];
        let cols = self.shape.dims()[1];
        let mut dense_data = vec![0.0f32; rows * cols];

        for row in 0..rows {
            let start = self.row_ptr[row];
            let end = self.row_ptr[row + 1];

            for idx in start..end {
                let col = self.col_indices[idx];
                let val = self.values[idx];
                dense_data[row * cols + col] = val;
            }
        }

        Tensor::from_data(dense_data, vec![rows, cols], self.device)
    }

    /// Get data for a specific row
    pub fn get_row_data(&self, row: usize) -> TorshResult<(Vec<usize>, Vec<f32>)> {
        if row >= self.shape.dims()[0] {
            return Err(TorshError::IndexError {
                index: row,
                size: self.shape.dims()[0],
            });
        }

        let start = self.row_ptr[row];
        let end = self.row_ptr[row + 1];

        let col_indices = self.col_indices[start..end].to_vec();
        let values = self.values[start..end].to_vec();

        Ok((col_indices, values))
    }

    /// Efficient matrix multiplication with dense tensor
    ///
    /// Implements sparse-dense matrix multiplication directly on CSR format
    /// without converting to dense, providing significant performance improvements.
    ///
    /// For CSR matrix A and dense matrix B, computes C = A × B where:
    /// - A is sparse (m × k) in CSR format
    /// - B is dense (k × n)
    /// - C is dense (m × n)
    ///
    /// Time complexity: O(nnz * n) where nnz is number of non-zeros in A
    /// Space complexity: O(m * n) for result matrix
    pub fn matmul(&self, other: &Tensor<f32>) -> TorshResult<Tensor<f32>> {
        let other_shape = other.shape();

        // Validate dimensions for matrix multiplication
        if other_shape.ndim() != 2 {
            return Err(TorshError::InvalidArgument(
                "Dense tensor must be 2D for matrix multiplication".to_string(),
            ));
        }

        let [m, k] = [self.shape.dims()[0], self.shape.dims()[1]];
        let [k_other, n] = [other_shape.dims()[0], other_shape.dims()[1]];

        if k != k_other {
            return Err(TorshError::InvalidArgument(format!(
                "Incompatible dimensions for matrix multiplication: ({}, {}) × ({}, {})",
                m, k, k_other, n
            )));
        }

        // Create result tensor with zeros
        let result_shape = vec![m, n];
        let result = zeros::<f32>(&result_shape)?;

        // Get data access for efficient computation
        let other_data = other
            .data()
            .map_err(|e| TorshError::ComputeError(e.to_string()))?;
        let mut result_data = result
            .data()
            .map_err(|e| TorshError::ComputeError(e.to_string()))?;

        // Perform sparse-dense matrix multiplication
        // For each row in the sparse matrix
        for row in 0..m {
            let row_start = self.row_ptr[row];
            let row_end = self.row_ptr[row + 1];

            // For each non-zero element in this row
            for idx in row_start..row_end {
                let col = self.col_indices[idx];
                let sparse_val = self.values[idx];

                // Multiply sparse element with corresponding row of dense matrix
                // and accumulate into result row
                for result_col in 0..n {
                    let dense_val = other_data[col * n + result_col];
                    result_data[row * n + result_col] += sparse_val * dense_val;
                }
            }
        }

        Ok(result)
    }
}

impl SparseTensor for CsrTensor {
    fn format(&self) -> SparseFormat {
        SparseFormat::Csr
    }

    fn shape(&self) -> &Shape {
        &self.shape
    }

    fn dtype(&self) -> DType {
        self.dtype
    }

    fn device(&self) -> DeviceType {
        self.device
    }

    fn nnz(&self) -> usize {
        self.values.len()
    }

    fn to_dense(&self) -> TorshResult<Tensor> {
        let dense = zeros::<f32>(self.shape.dims())?;

        for row in 0..self.shape.dims()[0] {
            let start = self.row_ptr[row];
            let end = self.row_ptr[row + 1];

            for i in start..end {
                let col = self.col_indices[i];
                let val = self.values[i];
                dense.set(&[row, col], val)?;
            }
        }

        Ok(dense)
    }

    fn to_coo(&self) -> TorshResult<CooTensor> {
        let mut row_indices = Vec::new();
        let mut col_indices = Vec::new();
        let mut values = Vec::new();

        for row in 0..self.shape.dims()[0] {
            let start = self.row_ptr[row];
            let end = self.row_ptr[row + 1];

            for i in start..end {
                row_indices.push(row);
                col_indices.push(self.col_indices[i]);
                values.push(self.values[i]);
            }
        }

        CooTensor::new(row_indices, col_indices, values, self.shape.clone())
    }

    fn to_csr(&self) -> TorshResult<CsrTensor> {
        Ok(self.clone())
    }

    fn to_csc(&self) -> TorshResult<crate::CscTensor> {
        let coo = self.to_coo()?;
        crate::CscTensor::from_coo(&coo)
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

impl Clone for CsrTensor {
    fn clone(&self) -> Self {
        Self {
            row_ptr: self.row_ptr.clone(),
            col_indices: self.col_indices.clone(),
            values: self.values.clone(),
            shape: self.shape.clone(),
            dtype: self.dtype,
            device: self.device,
        }
    }
}

impl CsrTensor {
    /// Transpose the matrix
    pub fn transpose(&self) -> TorshResult<crate::CscTensor> {
        // CSR transpose is CSC, so convert to CSC
        self.to_csc()
    }

    /// Sum all elements in the matrix
    pub fn sum(&self) -> TorshResult<f32> {
        Ok(self.values.iter().sum())
    }

    /// Scale the matrix by a scalar
    pub fn scale(&self, scalar: f32) -> TorshResult<Self> {
        let scaled_values: Vec<f32> = self.values.iter().map(|&v| v * scalar).collect();
        CsrTensor::new(
            self.row_ptr.clone(),
            self.col_indices.clone(),
            scaled_values,
            self.shape.clone(),
        )
    }

    /// Compute the norm of the matrix
    pub fn norm(&self, p: f32) -> TorshResult<f32> {
        if p == 2.0 {
            // L2 norm (Frobenius norm for matrices)
            Ok(self.values.iter().map(|&v| v * v).sum::<f32>().sqrt())
        } else if p == 1.0 {
            // L1 norm
            Ok(self.values.iter().map(|&v| v.abs()).sum())
        } else {
            // General p-norm
            Ok(self
                .values
                .iter()
                .map(|&v| v.abs().powf(p))
                .sum::<f32>()
                .powf(1.0 / p))
        }
    }

    /// Extract diagonal elements
    pub fn diagonal(&self) -> TorshResult<Vec<f32>> {
        let min_dim = self.shape.dims()[0].min(self.shape.dims()[1]);
        let mut diag = vec![0.0; min_dim];

        #[allow(clippy::needless_range_loop)]
        for row in 0..min_dim {
            let start = self.row_ptr[row];
            let end = self.row_ptr[row + 1];

            for i in start..end {
                if self.col_indices[i] == row {
                    diag[row] = self.values[i];
                    break;
                }
            }
        }

        Ok(diag)
    }

    /// Add two sparse matrices
    pub fn add(&self, other: &CsrTensor) -> TorshResult<CsrTensor> {
        if self.shape.dims() != other.shape.dims() {
            return Err(TorshError::InvalidArgument(
                "Matrices must have the same shape for addition".to_string(),
            ));
        }

        // For simplicity, convert both to COO, add, then convert back to CSR
        let coo_self = self.to_coo()?;
        let coo_other = other.to_coo()?;

        // Combine triplets
        let mut triplets = Vec::new();

        // Add triplets from self
        for i in 0..coo_self.nnz() {
            triplets.push((
                coo_self.row_indices()[i],
                coo_self.col_indices()[i],
                coo_self.values()[i],
            ));
        }

        // Add triplets from other
        for i in 0..coo_other.nnz() {
            triplets.push((
                coo_other.row_indices()[i],
                coo_other.col_indices()[i],
                coo_other.values()[i],
            ));
        }

        // Create COO from combined triplets and convert to CSR
        let shape = (self.shape.dims()[0], self.shape.dims()[1]);
        let coo_result = crate::CooTensor::from_triplets(triplets, shape)?;
        CsrTensor::from_coo(&coo_result)
    }

    /// Compute the density (fraction of non-zeros)
    pub fn density(&self) -> f32 {
        let total_elements = self.shape().numel();
        if total_elements == 0 {
            0.0
        } else {
            self.nnz() as f32 / total_elements as f32
        }
    }
}

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

    #[test]
    fn test_csr_creation() {
        // Matrix:
        // [0, 1, 0]
        // [0, 0, 2]
        // [3, 0, 0]
        let row_ptr = vec![0, 1, 2, 3];
        let col_indices = vec![1, 2, 0];
        let values = vec![1.0, 2.0, 3.0];
        let shape = Shape::new(vec![3, 3]);

        let csr = CsrTensor::new(row_ptr, col_indices, values, shape).unwrap();
        assert_eq!(csr.nnz(), 3);
    }

    #[test]
    fn test_csr_matvec() {
        let row_ptr = vec![0, 1, 2, 3];
        let col_indices = vec![1, 2, 0];
        let values = vec![1.0, 2.0, 3.0];
        let shape = Shape::new(vec![3, 3]);

        let csr = CsrTensor::new(row_ptr, col_indices, values, shape).unwrap();
        let vector = tensor_1d(&[1.0, 2.0, 3.0]).unwrap();

        let result = csr.matvec(&vector).unwrap();

        // Expected: [0*1 + 1*2 + 0*3, 0*1 + 0*2 + 2*3, 3*1 + 0*2 + 0*3] = [2, 6, 3]
        assert_eq!(result.get(&[0]).unwrap(), 2.0);
        assert_eq!(result.get(&[1]).unwrap(), 6.0);
        assert_eq!(result.get(&[2]).unwrap(), 3.0);
    }
}