tenflowers-core 0.1.1

Core tensor operations and execution engine for TenfloweRS
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
use crate::{Device, Result, Tensor, TensorError};
use scirs2_core::random::rand_distributions::{Distribution, Normal, Uniform as RandUniform};
use scirs2_core::random::rand_prelude::*;

#[cfg(feature = "gpu")]
use crate::gpu::GpuOps;

/// Generate a tensor with random values from a normal distribution (f32 only)
pub fn random_normal_f32(
    shape: &[usize],
    mean: f32,
    std: f32,
    seed: Option<u64>,
) -> Result<Tensor<f32>> {
    random_normal_f32_device(shape, mean, std, seed, &Device::Cpu)
}

/// Generate a tensor with random values from a normal distribution (f32 only) on specified device
pub fn random_normal_f32_device(
    shape: &[usize],
    mean: f32,
    std: f32,
    seed: Option<u64>,
    device: &Device,
) -> Result<Tensor<f32>> {
    let seed = seed.unwrap_or_else(|| {
        use std::time::{SystemTime, UNIX_EPOCH};
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system time must be after UNIX_EPOCH")
            .as_nanos() as u64
    });

    match device {
        Device::Cpu => {
            let total_elements: usize = shape.iter().product();
            let mut rng = StdRng::seed_from_u64(seed);

            let normal = Normal::new(mean, std).map_err(|e| {
                TensorError::invalid_argument(format!(
                    "Invalid normal distribution parameters: {e}"
                ))
            })?;

            let mut data = Vec::with_capacity(total_elements);
            for _ in 0..total_elements {
                data.push(normal.sample(&mut rng));
            }

            Tensor::from_vec(data, shape)
        }
        #[cfg(feature = "gpu")]
        Device::Gpu(device_id) => {
            use crate::device::get_gpu_context;
            use crate::gpu::random_ops;

            // Get GPU context
            let context = get_gpu_context(*device_id)?;

            // Calculate output length
            let output_len: usize = shape.iter().product();

            // Execute GPU random normal operation
            let gpu_buffer = random_ops::execute_random_normal::<f32>(
                context.device,
                context.queue,
                *device,
                output_len,
                mean,
                std,
                seed,
            )?;

            // Create tensor from GPU buffer
            let tensor_shape = crate::Shape::new(shape.to_vec());
            Ok(Tensor::from_gpu_buffer(gpu_buffer, tensor_shape))
        }
        #[cfg(feature = "rocm")]
        Device::Rocm(_) => {
            // ROCm random normal: Fallback to CPU implementation for now
            // Future: Implement native ROCm random number generation kernels
            eprintln!(
                "Warning: ROCm random normal using CPU fallback - native implementation pending"
            );
            random_normal_f32_device(shape, mean, std, Some(seed), &Device::Cpu)
        }
    }
}

/// Generate a tensor with random values from a normal distribution (f64 only)  
pub fn random_normal_f64(
    shape: &[usize],
    mean: f64,
    std: f64,
    seed: Option<u64>,
) -> Result<Tensor<f64>> {
    let total_elements: usize = shape.iter().product();
    let mut rng = scirs2_core::random::Random::seed(seed.unwrap_or_else(|| {
        use std::time::{SystemTime, UNIX_EPOCH};
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system time must be after UNIX_EPOCH")
            .as_nanos() as u64
    }));

    let normal = Normal::new(mean, std).map_err(|e| {
        TensorError::invalid_argument(format!("Invalid normal distribution parameters: {e}"))
    })?;

    let mut data = Vec::with_capacity(total_elements);
    for _ in 0..total_elements {
        data.push(normal.sample(&mut rng));
    }

    Tensor::from_vec(data, shape)
}

/// Generate a tensor with random values from a uniform distribution (f32 only)
pub fn random_uniform_f32(
    shape: &[usize],
    min: f32,
    max: f32,
    seed: Option<u64>,
) -> Result<Tensor<f32>> {
    random_uniform_f32_device(shape, min, max, seed, &Device::Cpu)
}

/// Generate a tensor with random values from a uniform distribution (f32 only) on specified device
pub fn random_uniform_f32_device(
    shape: &[usize],
    min: f32,
    max: f32,
    seed: Option<u64>,
    device: &Device,
) -> Result<Tensor<f32>> {
    if min >= max {
        return Err(TensorError::invalid_argument(
            "min must be less than max for uniform distribution".to_string(),
        ));
    }

    let seed = seed.unwrap_or_else(|| {
        use std::time::{SystemTime, UNIX_EPOCH};
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system time must be after UNIX_EPOCH")
            .as_nanos() as u64
    });

    match device {
        Device::Cpu => {
            let total_elements: usize = shape.iter().product();
            let mut rng = StdRng::seed_from_u64(seed);

            let uniform = RandUniform::new(min, max).map_err(|e| {
                TensorError::invalid_argument(format!(
                    "Invalid uniform distribution parameters: {e}"
                ))
            })?;

            let mut data = Vec::with_capacity(total_elements);
            for _ in 0..total_elements {
                data.push(uniform.sample(&mut rng));
            }

            Tensor::from_vec(data, shape)
        }
        #[cfg(feature = "gpu")]
        Device::Gpu(device_id) => {
            use crate::device::get_gpu_context;
            use crate::gpu::random_ops;

            // Get GPU context
            let context = get_gpu_context(*device_id)?;

            // Calculate output length
            let output_len: usize = shape.iter().product();

            // Execute GPU random uniform operation
            let gpu_buffer = random_ops::execute_random_uniform::<f32>(
                context.device,
                context.queue,
                *device,
                output_len,
                min,
                max,
                seed,
            )?;

            // Create tensor from GPU buffer
            let tensor_shape = crate::Shape::new(shape.to_vec());
            Ok(Tensor::from_gpu_buffer(gpu_buffer, tensor_shape))
        }
        #[cfg(feature = "rocm")]
        Device::Rocm(_) => {
            // ROCm random uniform: Fallback to CPU implementation for now
            // Future: Implement native ROCm random number generation kernels
            eprintln!(
                "Warning: ROCm random uniform using CPU fallback - native implementation pending"
            );
            random_uniform_f32_device(shape, min, max, Some(seed), &Device::Cpu)
        }
    }
}

/// Generate a tensor with random values from a uniform distribution (f64 only)
pub fn random_uniform_f64(
    shape: &[usize],
    min: f64,
    max: f64,
    seed: Option<u64>,
) -> Result<Tensor<f64>> {
    if min >= max {
        return Err(TensorError::invalid_argument(
            "min must be less than max for uniform distribution".to_string(),
        ));
    }

    let total_elements: usize = shape.iter().product();
    let mut rng = scirs2_core::random::Random::seed(seed.unwrap_or_else(|| {
        use std::time::{SystemTime, UNIX_EPOCH};
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system time must be after UNIX_EPOCH")
            .as_nanos() as u64
    }));

    let uniform = RandUniform::new(min, max).map_err(|e| {
        TensorError::invalid_argument(format!("Invalid uniform distribution parameters: {e}"))
    })?;

    let data: Vec<f64> = (0..total_elements)
        .map(|_| uniform.sample(&mut rng))
        .collect();

    Tensor::from_vec(data, shape)
}

/// Generate a tensor with random integers from a uniform distribution
pub fn random_uniform_int(
    shape: &[usize],
    min: i64,
    max: i64,
    seed: Option<u64>,
) -> Result<Tensor<i64>> {
    if min >= max {
        return Err(TensorError::invalid_argument(
            "min must be less than max for uniform distribution".to_string(),
        ));
    }

    let total_elements: usize = shape.iter().product();
    let mut rng = scirs2_core::random::Random::seed(seed.unwrap_or_else(|| {
        use std::time::{SystemTime, UNIX_EPOCH};
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system time must be after UNIX_EPOCH")
            .as_nanos() as u64
    }));

    let uniform = RandUniform::new(min, max).map_err(|e| {
        TensorError::invalid_argument(format!("Invalid uniform distribution parameters: {e}"))
    })?;

    let data: Vec<i64> = (0..total_elements)
        .map(|_| uniform.sample(&mut rng))
        .collect();

    Tensor::from_vec(data, shape)
}

/// Generate a tensor with random values from a standard normal distribution (mean=0, std=1) for f32
pub fn randn_f32(shape: &[usize], seed: Option<u64>) -> Result<Tensor<f32>> {
    random_normal_f32(shape, 0.0, 1.0, seed)
}

/// Generate a tensor with random values from a standard normal distribution (mean=0, std=1) for f64
pub fn randn_f64(shape: &[usize], seed: Option<u64>) -> Result<Tensor<f64>> {
    random_normal_f64(shape, 0.0, 1.0, seed)
}

/// Generate a tensor with random values from a uniform distribution [0, 1) for f32
pub fn rand_f32(shape: &[usize], seed: Option<u64>) -> Result<Tensor<f32>> {
    random_uniform_f32(shape, 0.0, 1.0, seed)
}

/// Generate a tensor with random values from a uniform distribution [0, 1) for f64
pub fn rand_f64(shape: &[usize], seed: Option<u64>) -> Result<Tensor<f64>> {
    random_uniform_f64(shape, 0.0, 1.0, seed)
}

/// Sample from a multinomial distribution (f32 weights)
pub fn multinomial_f32(
    weights: &Tensor<f32>,
    num_samples: usize,
    seed: Option<u64>,
) -> Result<Tensor<usize>> {
    let weights_shape = weights.shape().dims();
    if weights_shape.len() != 1 {
        return Err(TensorError::InvalidShape {
            operation: "multinomial".to_string(),
            reason: "multinomial expects 1D weights tensor".to_string(),
            shape: Some(weights_shape.to_vec()),
            context: None,
        });
    }

    let weights_data = weights.as_slice().ok_or_else(|| {
        TensorError::unsupported_operation_simple("GPU multinomial not supported yet".to_string())
    })?;

    // Normalize weights to probabilities
    let total_weight: f32 = weights_data.iter().sum();
    if total_weight <= 0.0 {
        return Err(TensorError::invalid_argument(
            "weights must sum to a positive value".to_string(),
        ));
    }

    let probabilities: Vec<f64> = weights_data
        .iter()
        .map(|&w| (w / total_weight) as f64)
        .collect();

    let mut rng = scirs2_core::random::Random::seed(seed.unwrap_or_else(|| {
        use std::time::{SystemTime, UNIX_EPOCH};
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system time must be after UNIX_EPOCH")
            .as_nanos() as u64
    }));

    let mut samples = Vec::with_capacity(num_samples);

    for _ in 0..num_samples {
        let random_val: f64 = rng.random();
        let mut cumulative = 0.0;
        let mut selected_idx = probabilities.len() - 1;

        for (i, &prob) in probabilities.iter().enumerate() {
            cumulative += prob;
            if random_val < cumulative {
                selected_idx = i;
                break;
            }
        }

        samples.push(selected_idx);
    }

    Tensor::from_vec(samples, &[num_samples])
}

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

    #[test]
    fn test_random_normal() {
        let tensor =
            random_normal_f32(&[10], 0.0, 1.0, Some(42)).expect("test: operation should succeed");
        assert_eq!(tensor.shape().dims(), &[10]);

        // Test with fixed seed should be reproducible
        let tensor2 =
            random_normal_f32(&[10], 0.0, 1.0, Some(42)).expect("test: operation should succeed");
        assert_eq!(tensor.as_slice(), tensor2.as_slice());
    }

    #[test]
    fn test_random_uniform() {
        let tensor = random_uniform_f32(&[5, 2], -1.0, 1.0, Some(123))
            .expect("test: operation should succeed");
        assert_eq!(tensor.shape().dims(), &[5, 2]);

        // Check all values are in range
        if let Some(data) = tensor.as_slice() {
            for &val in data {
                assert!(val >= -1.0 && val < 1.0);
            }
        }
    }

    #[test]
    fn test_random_uniform_int() {
        let tensor =
            random_uniform_int(&[8], 0, 10, Some(456)).expect("test: operation should succeed");
        assert_eq!(tensor.shape().dims(), &[8]);

        // Check all values are in range
        if let Some(data) = tensor.as_slice() {
            for &val in data {
                assert!(val >= 0 && val < 10);
            }
        }
    }

    #[test]
    fn test_randn() {
        let tensor = randn_f32(&[3, 3], Some(789)).expect("test: operation should succeed");
        assert_eq!(tensor.shape().dims(), &[3, 3]);
    }

    #[test]
    fn test_rand() {
        let tensor = rand_f32(&[4], Some(101112)).expect("test: operation should succeed");
        assert_eq!(tensor.shape().dims(), &[4]);

        // Check all values are in [0, 1)
        if let Some(data) = tensor.as_slice() {
            for &val in data {
                assert!(val >= 0.0 && val < 1.0);
            }
        }
    }

    #[test]
    fn test_multinomial() {
        // Test with uniform weights
        let weights = Tensor::<f32>::from_vec(vec![1.0, 1.0, 1.0, 1.0], &[4])
            .expect("test: from_vec should succeed");
        let samples =
            multinomial_f32(&weights, 100, Some(131415)).expect("test: operation should succeed");

        assert_eq!(samples.shape().dims(), &[100]);

        // Check all sampled indices are valid
        if let Some(data) = samples.as_slice() {
            for &idx in data {
                assert!(idx < 4);
            }
        }

        // Test with biased weights
        let weights = Tensor::<f32>::from_vec(vec![0.1, 0.2, 0.3, 0.4], &[4])
            .expect("test: from_vec should succeed");
        let samples =
            multinomial_f32(&weights, 10, Some(161718)).expect("test: operation should succeed");
        assert_eq!(samples.shape().dims(), &[10]);
    }

    #[test]
    fn test_multinomial_errors() {
        // Test 2D weights (should fail)
        let weights = Tensor::<f32>::from_vec(vec![1.0, 1.0, 1.0, 1.0], &[2, 2])
            .expect("test: from_vec should succeed");
        assert!(multinomial_f32(&weights, 10, Some(123)).is_err());

        // Test zero weights (should fail)
        let weights = Tensor::<f32>::from_vec(vec![0.0, 0.0, 0.0], &[3])
            .expect("test: from_vec should succeed");
        assert!(multinomial_f32(&weights, 10, Some(123)).is_err());
    }

    #[test]
    #[ignore = "GPU random normal not yet implemented"]
    fn test_gpu_random_normal_f32() {
        #[cfg(feature = "gpu")]
        {
            use crate::Device;

            // Test GPU random normal generation
            let result = random_normal_f32_device(&[10, 10], 0.0, 1.0, Some(42), &Device::Gpu(0));

            // Should either work or return an error indicating GPU is not available
            match result {
                Ok(tensor) => {
                    assert_eq!(tensor.shape().dims(), &[10, 10]);
                    assert_eq!(tensor.numel(), 100);
                }
                Err(_) => {
                    // GPU might not be available in test environment
                    // This is acceptable for the test
                }
            }
        }
    }

    #[test]
    #[ignore = "GPU random uniform not yet implemented"]
    fn test_gpu_random_uniform_f32() {
        #[cfg(feature = "gpu")]
        {
            use crate::Device;

            // Test GPU random uniform generation
            let result = random_uniform_f32_device(&[5, 5], 0.0, 1.0, Some(42), &Device::Gpu(0));

            // Should either work or return an error indicating GPU is not available
            match result {
                Ok(tensor) => {
                    assert_eq!(tensor.shape().dims(), &[5, 5]);
                    assert_eq!(tensor.numel(), 25);
                }
                Err(_) => {
                    // GPU might not be available in test environment
                    // This is acceptable for the test
                }
            }
        }
    }

    #[test]
    fn test_device_aware_random_functions() {
        use crate::Device;

        // Test CPU versions work
        let cpu_normal = random_normal_f32_device(&[5, 5], 0.0, 1.0, Some(42), &Device::Cpu)
            .expect("test: operation should succeed");
        assert_eq!(cpu_normal.shape().dims(), &[5, 5]);

        let cpu_uniform = random_uniform_f32_device(&[5, 5], 0.0, 1.0, Some(42), &Device::Cpu)
            .expect("test: operation should succeed");
        assert_eq!(cpu_uniform.shape().dims(), &[5, 5]);

        // Test that CPU implementations are deterministic with same seed
        let cpu_normal2 = random_normal_f32_device(&[5, 5], 0.0, 1.0, Some(42), &Device::Cpu)
            .expect("test: operation should succeed");
        assert_eq!(cpu_normal.as_slice(), cpu_normal2.as_slice());

        let cpu_uniform2 = random_uniform_f32_device(&[5, 5], 0.0, 1.0, Some(42), &Device::Cpu)
            .expect("test: operation should succeed");
        assert_eq!(cpu_uniform.as_slice(), cpu_uniform2.as_slice());
    }
}