tenrso-ad 0.1.0-rc.1

Automatic differentiation support for TenRSo
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
//! Parallel gradient computation using Rayon
//!
//! This module provides parallel implementations of gradient operations
//! for improved performance on multi-core systems.
//!
//! # Features
//!
//! - Parallel element-wise gradient computation
//! - Parallel gradient accumulation
//! - Parallel batch gradient processing
//! - Work-stealing via Rayon thread pool
//!
//! # Performance
//!
//! Parallel operations are beneficial when:
//! - Tensor size > 10,000 elements
//! - Multiple independent gradients to compute
//! - Batch size > 4
//!
//! For smaller tensors, sequential processing may be faster due to overhead.

use anyhow::Result;
use rayon::prelude::*;
use scirs2_core::ndarray_ext::{Array, IxDyn, Zip};
use scirs2_core::numeric::Float;
use tenrso_core::DenseND;

/// Configuration for parallel gradient computation
#[derive(Debug, Clone)]
pub struct ParallelConfig {
    /// Minimum tensor size to use parallel processing (default: 10,000)
    pub min_parallel_size: usize,

    /// Number of threads to use (None = use all available cores)
    pub num_threads: Option<usize>,

    /// Chunk size for parallel iteration (None = auto)
    pub chunk_size: Option<usize>,
}

impl Default for ParallelConfig {
    fn default() -> Self {
        Self {
            min_parallel_size: 10_000,
            num_threads: None,
            chunk_size: None,
        }
    }
}

/// Parallel element-wise gradient computation
///
/// Computes `grad_x = grad_y * f'(x)` in parallel for element-wise operations.
///
/// # Arguments
///
/// * `input` - Input tensor from forward pass
/// * `output_grad` - Gradient w.r.t. output
/// * `derivative` - Derivative function f'(x)
/// * `config` - Parallel configuration
///
/// # Returns
///
/// Gradient w.r.t. input
///
/// # Example
///
/// ```rust,ignore
/// let grad = parallel_elementwise_vjp(
///     &x,
///     &grad_y,
///     |x| 2.0 * x,  // derivative of x^2
///     &ParallelConfig::default(),
/// )?;
/// ```
pub fn parallel_elementwise_vjp<T, F>(
    input: &DenseND<T>,
    output_grad: &DenseND<T>,
    derivative: F,
    config: &ParallelConfig,
) -> Result<DenseND<T>>
where
    T: Float + Send + Sync,
    F: Fn(&T) -> T + Send + Sync,
{
    if input.shape() != output_grad.shape() {
        return Err(anyhow::anyhow!(
            "Shape mismatch: input {:?} vs output_grad {:?}",
            input.shape(),
            output_grad.shape()
        ));
    }

    let input_arr = input.as_array();
    let grad_arr = output_grad.as_array();

    // Check if parallel processing is beneficial
    let total_elements = input.len();
    let use_parallel = total_elements >= config.min_parallel_size;

    if use_parallel {
        // Parallel computation using Rayon
        // First collect to vectors to ensure consistent ordering
        let shape = input.shape().to_vec();
        let input_flat: Vec<T> = input_arr.iter().copied().collect();
        let grad_flat: Vec<T> = grad_arr.iter().copied().collect();

        // Use parallel iteration over vectors which preserves order
        let grad_data: Vec<T> = input_flat
            .par_iter()
            .zip(grad_flat.par_iter())
            .map(|(x, g)| {
                let deriv = derivative(x);
                deriv * *g
            })
            .collect();

        let result_arr = Array::from_shape_vec(IxDyn(&shape), grad_data)
            .map_err(|e| anyhow::anyhow!("Failed to create result array: {}", e))?;

        Ok(DenseND::from_array(result_arr))
    } else {
        // Sequential computation for small tensors
        let result_arr = Zip::from(input_arr).and(grad_arr).map_collect(|x, g| {
            let deriv = derivative(x);
            deriv * *g
        });

        Ok(DenseND::from_array(result_arr))
    }
}

/// Parallel gradient accumulation for multiple gradients
///
/// Efficiently accumulates multiple gradients in parallel.
///
/// # Arguments
///
/// * `gradients` - Slice of gradient tensors to accumulate
/// * `config` - Parallel configuration
///
/// # Returns
///
/// Accumulated gradient
///
/// # Example
///
/// ```rust,ignore
/// let total_grad = parallel_accumulate_gradients(
///     &[grad1, grad2, grad3],
///     &ParallelConfig::default(),
/// )?;
/// ```
pub fn parallel_accumulate_gradients<T>(
    gradients: &[DenseND<T>],
    config: &ParallelConfig,
) -> Result<DenseND<T>>
where
    T: Float + Send + Sync,
{
    if gradients.is_empty() {
        return Err(anyhow::anyhow!("No gradients to accumulate"));
    }

    let shape = gradients[0].shape();

    // Verify all gradients have same shape
    for grad in gradients.iter() {
        if grad.shape() != shape {
            return Err(anyhow::anyhow!(
                "Shape mismatch in gradient accumulation: expected {:?}, got {:?}",
                shape,
                grad.shape()
            ));
        }
    }

    let total_elements = gradients[0].len();
    let use_parallel = total_elements >= config.min_parallel_size;

    if use_parallel {
        // Parallel reduction - accumulate gradients
        let mut result = gradients[0].clone();
        for grad in gradients.iter().skip(1) {
            let result_arr = result.as_array();
            let grad_arr = grad.as_array();
            let sum_arr = result_arr + grad_arr;
            result = DenseND::from_array(sum_arr);
        }
        Ok(result)
    } else {
        // Sequential accumulation
        let mut result = gradients[0].clone();
        for grad in gradients.iter().skip(1) {
            result = &result + grad;
        }
        Ok(result)
    }
}

/// Parallel batch gradient computation
///
/// Computes gradients for a batch of inputs in parallel.
///
/// # Arguments
///
/// * `inputs` - Batch of input tensors
/// * `output_grads` - Batch of output gradients
/// * `gradient_fn` - Function to compute gradient for single input
/// * `config` - Parallel configuration
///
/// # Returns
///
/// Vector of input gradients
///
/// # Example
///
/// ```rust,ignore
/// let input_grads = parallel_batch_gradients(
///     &batch_inputs,
///     &batch_output_grads,
///     |input, output_grad| compute_single_grad(input, output_grad),
///     &ParallelConfig::default(),
/// )?;
/// ```
pub fn parallel_batch_gradients<T, F>(
    inputs: &[DenseND<T>],
    output_grads: &[DenseND<T>],
    gradient_fn: F,
    _config: &ParallelConfig,
) -> Result<Vec<DenseND<T>>>
where
    T: Float + Send + Sync,
    F: Fn(&DenseND<T>, &DenseND<T>) -> Result<DenseND<T>> + Send + Sync,
{
    if inputs.len() != output_grads.len() {
        return Err(anyhow::anyhow!(
            "Batch size mismatch: {} inputs vs {} output grads",
            inputs.len(),
            output_grads.len()
        ));
    }

    let batch_size = inputs.len();
    let use_parallel = batch_size > 1;

    if use_parallel {
        // Parallel batch processing
        let results: Result<Vec<_>> = inputs
            .par_iter()
            .zip(output_grads.par_iter())
            .map(|(input, output_grad)| gradient_fn(input, output_grad))
            .collect();

        results
    } else {
        // Sequential for single item
        inputs
            .iter()
            .zip(output_grads.iter())
            .map(|(input, output_grad)| gradient_fn(input, output_grad))
            .collect()
    }
}

/// Parallel reduction gradient computation
///
/// Computes gradients for reduction operations in parallel.
///
/// # Arguments
///
/// * `input_shape` - Shape of original input
/// * `output_grad` - Gradient w.r.t. output (reduced)
/// * `scaling` - Scaling factor (e.g., 1/n for mean)
/// * `config` - Parallel configuration
///
/// # Returns
///
/// Gradient w.r.t. input (broadcasted)
pub fn parallel_reduction_grad<T>(
    input_shape: &[usize],
    output_grad: &DenseND<T>,
    scaling: T,
    config: &ParallelConfig,
) -> Result<DenseND<T>>
where
    T: Float + Send + Sync,
{
    let total_elements: usize = input_shape.iter().product();
    let use_parallel = total_elements >= config.min_parallel_size;

    // Get the scalar gradient value
    let grad_value = if output_grad.len() == 1 {
        *output_grad
            .get(&[])
            .or_else(|| output_grad.get(&[0]))
            .ok_or_else(|| anyhow::anyhow!("Failed to get gradient value"))?
    } else {
        return Err(anyhow::anyhow!(
            "Expected scalar output gradient for full reduction"
        ));
    };

    let scaled_grad = grad_value * scaling;

    if use_parallel {
        // Parallel fill
        let grad_data: Vec<T> = (0..total_elements)
            .into_par_iter()
            .map(|_| scaled_grad)
            .collect();

        let result_arr = Array::from_shape_vec(IxDyn(input_shape), grad_data)
            .map_err(|e| anyhow::anyhow!("Failed to create result array: {}", e))?;

        Ok(DenseND::from_array(result_arr))
    } else {
        // Sequential fill
        Ok(DenseND::from_elem(input_shape, scaled_grad))
    }
}

/// Configure global Rayon thread pool
///
/// Sets the number of threads for parallel computation.
///
/// # Arguments
///
/// * `num_threads` - Number of threads (None = use all available cores)
///
/// # Example
///
/// ```rust,ignore
/// configure_thread_pool(Some(4)); // Use 4 threads
/// configure_thread_pool(None);    // Use all cores
/// ```
pub fn configure_thread_pool(num_threads: Option<usize>) -> Result<()> {
    if let Some(n) = num_threads {
        rayon::ThreadPoolBuilder::new()
            .num_threads(n)
            .build_global()
            .map_err(|e| anyhow::anyhow!("Failed to configure thread pool: {}", e))?;
    }
    Ok(())
}

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

    #[test]
    fn test_parallel_elementwise_vjp() {
        let x = DenseND::from_vec(vec![1.0, 2.0, 3.0, 4.0], &[4]).unwrap();
        let grad_y = DenseND::ones(&[4]);

        let config = ParallelConfig {
            min_parallel_size: 1, // Force parallel for testing
            ..Default::default()
        };

        // Test derivative of x^2: f'(x) = 2x
        let grad_x = parallel_elementwise_vjp(&x, &grad_y, |x| 2.0 * x, &config).unwrap();

        assert_eq!(grad_x.shape(), &[4]);

        // Check values with array access
        let grad_arr = grad_x.as_array();
        let values: Vec<f64> = grad_arr.iter().copied().collect();

        assert!(
            (values[0] - 2.0).abs() < 1e-8,
            "grad[0] = {}, expected 2.0",
            values[0]
        );
        assert!(
            (values[1] - 4.0).abs() < 1e-8,
            "grad[1] = {}, expected 4.0",
            values[1]
        );
        assert!(
            (values[2] - 6.0).abs() < 1e-8,
            "grad[2] = {}, expected 6.0",
            values[2]
        );
        assert!(
            (values[3] - 8.0).abs() < 1e-8,
            "grad[3] = {}, expected 8.0",
            values[3]
        );
    }

    #[test]
    fn test_parallel_accumulate_gradients() {
        let grad1 = DenseND::from_vec(vec![1.0, 2.0, 3.0], &[3]).unwrap();
        let grad2 = DenseND::from_vec(vec![4.0, 5.0, 6.0], &[3]).unwrap();
        let grad3 = DenseND::from_vec(vec![7.0, 8.0, 9.0], &[3]).unwrap();

        let config = ParallelConfig {
            min_parallel_size: 1,
            ..Default::default()
        };

        let result = parallel_accumulate_gradients(&[grad1, grad2, grad3], &config).unwrap();

        assert_eq!(result.shape(), &[3]);
        assert!((result.get(&[0]).unwrap() - 12.0).abs() < 1e-10);
        assert!((result.get(&[1]).unwrap() - 15.0).abs() < 1e-10);
        assert!((result.get(&[2]).unwrap() - 18.0).abs() < 1e-10);
    }

    #[test]
    fn test_parallel_batch_gradients() {
        let inputs = vec![
            DenseND::from_vec(vec![1.0, 2.0], &[2]).unwrap(),
            DenseND::from_vec(vec![3.0, 4.0], &[2]).unwrap(),
        ];
        let output_grads = vec![DenseND::ones(&[2]), DenseND::ones(&[2])];

        let config = ParallelConfig::default();

        let gradient_fn = |input: &DenseND<f64>, output_grad: &DenseND<f64>| {
            parallel_elementwise_vjp(input, output_grad, |x| 2.0 * x, &config)
        };

        let results =
            parallel_batch_gradients(&inputs, &output_grads, gradient_fn, &config).unwrap();

        assert_eq!(results.len(), 2);
        assert!((results[0].get(&[0]).unwrap() - 2.0).abs() < 1e-10);
        assert!((results[0].get(&[1]).unwrap() - 4.0).abs() < 1e-10);
        assert!((results[1].get(&[0]).unwrap() - 6.0).abs() < 1e-10);
        assert!((results[1].get(&[1]).unwrap() - 8.0).abs() < 1e-10);
    }

    #[test]
    fn test_parallel_reduction_grad() {
        let input_shape = vec![2, 3];
        let output_grad = DenseND::from_elem(&[], 5.0);
        let scaling = 0.5;

        let config = ParallelConfig {
            min_parallel_size: 1,
            ..Default::default()
        };

        let result = parallel_reduction_grad(&input_shape, &output_grad, scaling, &config).unwrap();

        assert_eq!(result.shape(), &[2, 3]);
        for i in 0..2 {
            for j in 0..3 {
                assert!((result.get(&[i, j]).unwrap() - 2.5).abs() < 1e-10);
            }
        }
    }

    #[test]
    fn test_sequential_fallback_small_tensor() {
        // Small tensor should use sequential path
        let x = DenseND::from_vec(vec![1.0, 2.0, 3.0], &[3]).unwrap();
        let grad_y = DenseND::ones(&[3]);

        let config = ParallelConfig {
            min_parallel_size: 1000, // Set high threshold
            ..Default::default()
        };

        let grad_x = parallel_elementwise_vjp(&x, &grad_y, |x| 2.0 * x, &config).unwrap();

        assert_eq!(grad_x.shape(), &[3]);
        assert!((grad_x.get(&[0]).unwrap() - 2.0).abs() < 1e-10);
    }
}