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
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
use crate::{Result, Tensor, TensorError};
use scirs2_core::numeric::{Float, FromPrimitive, Zero};
use std::time::Instant;

use super::core::get_activation_registry;
use super::implementations::{
    gelu_parallel_f32, gelu_sequential_f32, sigmoid_vectorized, tanh_vectorized,
    ultra_relu_vectorized, ultra_sigmoid_vectorized,
};
use super::simd;
use super::strategy::{select_activation_strategy, ActivationStrategy};

/// Ultra-performance ReLU implementation for f32 with comprehensive optimizations
pub fn relu_f32(x: &Tensor<f32>) -> Result<Tensor<f32>> {
    let registry = get_activation_registry();
    let start_time = Instant::now();

    let result = match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let optimized_result = ultra_relu_vectorized(arr)?;
            Ok(Tensor::from_array(optimized_result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            registry.record_gpu();
            let result_gpu = crate::gpu::ops::execute_activation_op(
                gpu_buffer,
                crate::gpu::ops::ActivationOp::ReLU,
            )?;

            Ok(Tensor::from_gpu_buffer(result_gpu, x.shape().clone()))
        }
    };

    // Record overall function performance
    let duration = start_time.elapsed();
    registry.record_function("relu_f32", x.shape().size(), duration.as_nanos() as u64);

    result
}

/// Ultra-performance ReLU implementation for f64
pub fn relu_f64(x: &Tensor<f64>) -> Result<Tensor<f64>> {
    let registry = get_activation_registry();
    let start_time = Instant::now();

    let result = match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let optimized_result = ultra_relu_vectorized(arr)?;
            Ok(Tensor::from_array(optimized_result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            registry.record_gpu();
            let result_gpu = crate::gpu::ops::execute_activation_op(
                gpu_buffer,
                crate::gpu::ops::ActivationOp::ReLU,
            )?;

            Ok(Tensor::from_gpu_buffer(result_gpu, x.shape().clone()))
        }
    };

    // Record performance metrics
    let duration = start_time.elapsed();
    registry.record_function("relu_f64", x.shape().size(), duration.as_nanos() as u64);

    result
}

/// Ultra-performance sigmoid implementation for f32
pub fn sigmoid_f32(x: &Tensor<f32>) -> Result<Tensor<f32>> {
    let registry = get_activation_registry();
    let start_time = Instant::now();

    let result = match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let optimized_result = ultra_sigmoid_vectorized(arr)?;
            Ok(Tensor::from_array(optimized_result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            registry.record_gpu();
            let result_gpu = crate::gpu::ops::execute_activation_op(
                gpu_buffer,
                crate::gpu::ops::ActivationOp::Sigmoid,
            )?;

            let mut result = Tensor::from_gpu_buffer(result_gpu, x.shape().clone());
            result.set_requires_grad(x.requires_grad());
            Ok(result)
        }
    };

    let duration = start_time.elapsed();
    registry.record_function("sigmoid_f32", x.shape().size(), duration.as_nanos() as u64);

    result
}

/// High-performance sigmoid implementation for f64
pub fn sigmoid_f64(x: &Tensor<f64>) -> Result<Tensor<f64>> {
    match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let result = sigmoid_vectorized(arr);
            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            let result_gpu = crate::gpu::ops::execute_activation_op(
                gpu_buffer,
                crate::gpu::ops::ActivationOp::Sigmoid,
            )?;

            let mut result = Tensor::from_gpu_buffer(result_gpu, x.shape().clone());
            result.set_requires_grad(x.requires_grad());
            Ok(result)
        }
    }
}

/// High-performance tanh implementation for f32
pub fn tanh_f32(x: &Tensor<f32>) -> Result<Tensor<f32>> {
    match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let result = tanh_vectorized(arr);
            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            let result_gpu = crate::gpu::ops::execute_activation_op(
                gpu_buffer,
                crate::gpu::ops::ActivationOp::Tanh,
            )?;

            let mut result = Tensor::from_gpu_buffer(result_gpu, x.shape().clone());
            result.set_requires_grad(x.requires_grad());
            Ok(result)
        }
    }
}

/// High-performance tanh implementation for f64
pub fn tanh_f64(x: &Tensor<f64>) -> Result<Tensor<f64>> {
    match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let result = tanh_vectorized(arr);
            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            let result_gpu = crate::gpu::ops::execute_activation_op(
                gpu_buffer,
                crate::gpu::ops::ActivationOp::Tanh,
            )?;

            let mut result = Tensor::from_gpu_buffer(result_gpu, x.shape().clone());
            result.set_requires_grad(x.requires_grad());
            Ok(result)
        }
    }
}

pub fn relu<T>(x: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone
        + Default
        + Zero
        + PartialOrd
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable,
{
    match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let zero = T::zero();
            let result = arr.mapv(|v| if v > zero { v } else { zero });
            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            let result_gpu = crate::gpu::ops::execute_activation_op(
                gpu_buffer,
                crate::gpu::ops::ActivationOp::ReLU,
            )?;

            Ok(Tensor::from_gpu_buffer(result_gpu, x.shape().clone()))
        }
    }
}

pub fn sigmoid<T>(x: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Float + Send + Sync + 'static + bytemuck::Pod + bytemuck::Zeroable,
{
    match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let one = T::one();
            let result = arr.mapv(|v| one / (one + (-v).exp()));
            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            let result_gpu = crate::gpu::ops::execute_activation_op(
                gpu_buffer,
                crate::gpu::ops::ActivationOp::Sigmoid,
            )?;

            let mut result = Tensor::from_gpu_buffer(result_gpu, x.shape().clone());
            result.set_requires_grad(x.requires_grad());
            Ok(result)
        }
    }
}

pub fn tanh<T>(x: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Float + Send + Sync + 'static + bytemuck::Pod + bytemuck::Zeroable,
{
    match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let result = arr.mapv(|v| v.tanh());
            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            let result_gpu = crate::gpu::ops::execute_activation_op(
                gpu_buffer,
                crate::gpu::ops::ActivationOp::Tanh,
            )?;

            let mut result = Tensor::from_gpu_buffer(result_gpu, x.shape().clone());
            result.set_requires_grad(x.requires_grad());
            Ok(result)
        }
    }
}

pub fn mish<T>(x: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Float + Send + Sync + 'static + bytemuck::Pod + bytemuck::Zeroable,
{
    match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let result = arr.mapv(|v| {
                let softplus = (T::one() + v.exp()).ln();
                v * softplus.tanh()
            });
            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            // Use GPU mish operation for f32, fall back to CPU for other types
            if std::any::type_name::<T>() == "f32" {
                let gpu_buffer_f32 = unsafe {
                    std::mem::transmute::<
                        &crate::gpu::buffer::GpuBuffer<T>,
                        &crate::gpu::buffer::GpuBuffer<f32>,
                    >(gpu_buffer)
                };

                let result_gpu_f32 = crate::gpu::ops::execute_activation_op(
                    gpu_buffer_f32,
                    crate::gpu::ops::ActivationOp::Mish,
                )?;

                let result_gpu = unsafe {
                    std::mem::transmute::<
                        crate::gpu::buffer::GpuBuffer<f32>,
                        crate::gpu::buffer::GpuBuffer<T>,
                    >(result_gpu_f32)
                };

                let mut result = Tensor::from_gpu_buffer(result_gpu, x.shape().clone());
                result.set_requires_grad(x.requires_grad());
                Ok(result)
            } else {
                // Fallback to CPU for non-f32 types
                let cpu_tensor = x.to_cpu()?;
                let result = mish(&cpu_tensor)?;
                result.to_device(x.device().clone())
            }
        }
    }
}

pub fn softmax<T>(x: &Tensor<T>, axis: Option<i32>) -> Result<Tensor<T>>
where
    T: Clone
        + Default
        + Float
        + std::ops::Sub<Output = T>
        + std::ops::Add<Output = T>
        + std::ops::Div<Output = T>
        + std::iter::Sum
        + Send
        + Sync
        + bytemuck::Pod,
{
    match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            use scirs2_core::ndarray::Axis;

            // Default to last axis if not specified
            let ndim = arr.ndim();
            let axis = axis.unwrap_or(-1);
            let axis = if axis < 0 {
                (ndim as i32 + axis) as usize
            } else {
                axis as usize
            };

            if axis >= ndim {
                return Err(TensorError::InvalidAxis {
                    operation: "softmax".to_string(),
                    axis: axis as i32,
                    ndim,
                    context: None,
                });
            }

            // Simpler implementation that avoids complex indexing
            // First subtract max for numerical stability
            let max_arr = arr.map_axis(Axis(axis), |view| {
                view.iter().cloned().fold(T::neg_infinity(), T::max)
            });

            // Expand max_arr to match original dimensions
            let mut shape_vec = arr.shape().to_vec();
            shape_vec[axis] = 1;
            let max_expanded = max_arr
                .into_shape_with_order(shape_vec.as_slice())
                .map_err(|e| TensorError::invalid_shape_simple(e.to_string()))?;

            // Compute exp(x - max)
            let exp_arr = arr.clone()
                - &max_expanded
                    .broadcast(arr.dim())
                    .expect("max array must be broadcastable to input shape");
            let exp_arr = exp_arr.mapv(|x| x.exp());

            // Sum along axis
            let sum_arr = exp_arr.sum_axis(Axis(axis));

            // Expand sum to match dimensions
            let sum_expanded = sum_arr
                .into_shape_with_order(shape_vec.as_slice())
                .map_err(|e| TensorError::invalid_shape_simple(e.to_string()))?;

            // Divide to get softmax
            let result = exp_arr
                / &sum_expanded
                    .broadcast(arr.dim())
                    .expect("sum array must be broadcastable to input shape");

            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            // Implement GPU softmax using axis-specific reductions
            if std::any::type_name::<T>() == "f32" {
                super::gpu::softmax_gpu_f32(x, axis)
            } else {
                // Fallback to CPU for non-f32 types
                let cpu_tensor = x.to_cpu()?;
                let result = softmax(&cpu_tensor, axis)?;
                result.to_device(x.device().clone())
            }
        }
    }
}

/// GELU (Gaussian Error Linear Unit) activation function
/// GELU(x) = x * Φ(x) where Φ is the standard Gaussian CDF
/// Approximation: GELU(x) ≈ 0.5 * x * (1 + tanh(√(2/π) * (x + 0.044715 * x³)))
pub fn gelu<T>(x: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Float + Send + Sync + bytemuck::Pod,
{
    match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let sqrt_2_over_pi = T::from(0.797_884_608)
                .expect("constant 0.797884608 must be convertible to float type"); // √(2/π)
            let coeff =
                T::from(0.044715).expect("constant 0.044715 must be convertible to float type");
            let half = T::from(0.5).expect("constant 0.5 must be convertible to float type");
            let one = T::one();

            let result = arr.mapv(|x| {
                let x_cubed = x * x * x;
                let inner = sqrt_2_over_pi * (x + coeff * x_cubed);
                half * x * (one + inner.tanh())
            });

            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            let result_gpu = crate::gpu::ops::execute_activation_op(
                gpu_buffer,
                crate::gpu::ops::ActivationOp::GELU,
            )?;

            let mut result = Tensor::from_gpu_buffer(result_gpu, x.shape().clone());
            result.set_requires_grad(x.requires_grad());
            Ok(result)
        }
    }
}

/// Ultra-performance GELU implementation (Gaussian Error Linear Unit)
pub fn gelu_f32(x: &Tensor<f32>) -> Result<Tensor<f32>> {
    let registry = get_activation_registry();
    let start_time = Instant::now();

    let result = match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let total_elements = arr.len();
            let strategy = select_activation_strategy(total_elements, true);

            let computed_result = match strategy {
                ActivationStrategy::Simd => {
                    if arr.is_standard_layout() {
                        if let Some(input_slice) = arr.as_slice() {
                            let mut output_data = vec![0.0f32; input_slice.len()];
                            match simd::simd_gelu_f32(input_slice, &mut output_data) {
                                Ok(()) => {
                                    registry.record_simd();
                                    scirs2_core::ndarray::ArrayD::from_shape_vec(
                                        arr.raw_dim(),
                                        output_data,
                                    )
                                    .map_err(|e| {
                                        TensorError::invalid_argument(format!(
                                            "SIMD GELU shape error: {}",
                                            e
                                        ))
                                    })?
                                }
                                Err(_) => {
                                    // Fallback to sequential
                                    gelu_sequential_f32(arr)
                                }
                            }
                        } else {
                            gelu_sequential_f32(arr)
                        }
                    } else {
                        gelu_sequential_f32(arr)
                    }
                }
                ActivationStrategy::Parallel => {
                    registry.record_parallel();
                    gelu_parallel_f32(arr)
                }
                _ => gelu_sequential_f32(arr),
            };

            Ok(Tensor::from_array(computed_result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            registry.record_gpu();
            // Use GPU implementation if available
            let result_gpu = crate::gpu::ops::execute_activation_op(
                gpu_buffer,
                crate::gpu::ops::ActivationOp::GELU,
            )?;
            Ok(Tensor::from_gpu_buffer(result_gpu, x.shape().clone()))
        }
    };

    let duration = start_time.elapsed();
    registry.record_function("gelu_f32", x.shape().size(), duration.as_nanos() as u64);

    result
}

/// Swish activation function (also known as SiLU - Sigmoid Linear Unit)
/// Swish(x) = x * sigmoid(x) = x / (1 + exp(-x))
pub fn swish<T>(x: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Float + Send + Sync + bytemuck::Pod,
{
    match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let one = T::one();
            let result = arr.mapv(|x| {
                let sigmoid_x = one / (one + (-x).exp());
                x * sigmoid_x
            });

            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            let result_gpu = crate::gpu::ops::execute_activation_op(
                gpu_buffer,
                crate::gpu::ops::ActivationOp::Swish,
            )?;

            let mut result = Tensor::from_gpu_buffer(result_gpu, x.shape().clone());
            result.set_requires_grad(x.requires_grad());
            Ok(result)
        }
    }
}

/// ELU (Exponential Linear Unit) activation function
/// ELU(x) = x if x > 0, α * (exp(x) - 1) if x <= 0
pub fn elu<T>(x: &Tensor<T>, alpha: T) -> Result<Tensor<T>>
where
    T: Clone + Default + Float + PartialOrd + Send + Sync + bytemuck::Pod,
{
    match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let zero = T::zero();
            let one = T::one();

            let result = arr.mapv(|x| if x > zero { x } else { alpha * (x.exp() - one) });

            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            let result_gpu = crate::gpu::ops::execute_activation_op(
                gpu_buffer,
                crate::gpu::ops::ActivationOp::ELU,
            )?;

            let mut result = Tensor::from_gpu_buffer(result_gpu, x.shape().clone());
            result.set_requires_grad(x.requires_grad());
            Ok(result)
        }
    }
}

/// LeakyReLU activation function
/// LeakyReLU(x) = max(αx, x) where α is typically 0.01
pub fn leaky_relu<T>(x: &Tensor<T>, alpha: T) -> Result<Tensor<T>>
where
    T: Clone + Default + Float + PartialOrd + Send + Sync + bytemuck::Pod,
{
    match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let zero = T::zero();

            let result = arr.mapv(|x| if x > zero { x } else { alpha * x });

            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            let result_gpu = crate::gpu::ops::execute_activation_op(
                gpu_buffer,
                crate::gpu::ops::ActivationOp::LeakyReLU,
            )?;

            let mut result = Tensor::from_gpu_buffer(result_gpu, x.shape().clone());
            result.set_requires_grad(x.requires_grad());
            Ok(result)
        }
    }
}

/// HardSwish activation function
/// HardSwish(x) = x * ReLU6(x + 3) / 6
/// where ReLU6(x) = min(max(x, 0), 6)
pub fn hard_swish<T>(x: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone
        + Default
        + Float
        + PartialOrd
        + Send
        + Sync
        + bytemuck::Pod
        + bytemuck::Zeroable
        + 'static,
{
    match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let zero = T::zero();
            let three = T::from(3).expect("constant 3 must be convertible to float type");
            let six = T::from(6).expect("constant 6 must be convertible to float type");

            let result = arr.mapv(|x| {
                let x_plus_3 = x + three;
                let relu6_val = if x_plus_3 < zero {
                    zero
                } else if x_plus_3 > six {
                    six
                } else {
                    x_plus_3
                };
                x * relu6_val / six
            });

            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(gpu_buffer) => {
            let result_gpu = crate::gpu::ops::execute_activation_op(
                gpu_buffer,
                crate::gpu::ops::ActivationOp::HardSwish,
            )?;

            let mut result = Tensor::from_gpu_buffer(result_gpu, x.shape().clone());
            result.set_requires_grad(x.requires_grad());
            Ok(result)
        }
    }
}

/// PReLU (Parametric ReLU) activation function
/// PReLU(x) = max(0, x) + α * min(0, x)
/// where α is a learnable parameter (different for each channel)
pub fn prelu<T>(x: &Tensor<T>, alpha: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone
        + Default
        + Float
        + PartialOrd
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable,
{
    let x_shape = x.shape().dims();
    let alpha_shape = alpha.shape().dims();

    // Alpha should be either scalar or match the channel dimension
    if alpha_shape.len() != 1 && alpha_shape != [1] {
        return Err(TensorError::invalid_shape_simple(format!(
            "PReLU alpha must be 1D, got shape {alpha_shape:?}"
        )));
    }

    match (&x.storage, &alpha.storage) {
        (
            crate::tensor::TensorStorage::Cpu(x_arr),
            crate::tensor::TensorStorage::Cpu(alpha_arr),
        ) => {
            let zero = T::zero();

            // Handle broadcasting of alpha
            let result = if alpha_shape == [1] {
                // Scalar alpha
                let alpha_val = alpha_arr[[0]];
                x_arr.mapv(|x| if x > zero { x } else { alpha_val * x })
            } else {
                // Channel-wise alpha (assuming NCHW format)
                if x_shape.len() < 2 {
                    return Err(TensorError::invalid_shape_simple(
                        "PReLU with channel-wise alpha requires at least 2D input ".to_string(),
                    ));
                }

                let channels = x_shape[1];
                if alpha_shape[0] != channels {
                    return Err(TensorError::shape_mismatch(
                        "prelu",
                        &format!("alpha channels = {channels}"),
                        &format!("alpha channels = {}", alpha_shape[0]),
                    ));
                }

                let mut result = x_arr.clone();
                for (mut slice, &alpha_val) in result
                    .axis_iter_mut(scirs2_core::ndarray::Axis(1))
                    .zip(alpha_arr.iter())
                {
                    slice.mapv_inplace(|x| if x <= zero { alpha_val * x } else { x });
                }
                result
            };

            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        (
            crate::tensor::TensorStorage::Gpu(_x_buffer),
            crate::tensor::TensorStorage::Gpu(_alpha_buffer),
        ) => {
            // Use GPU binary operation for PReLU
            use crate::ops::binary::{binary_op, PReLUOp};
            binary_op(x, alpha, PReLUOp)
        }
        #[cfg(feature = "gpu")]
        _ => Err(TensorError::device_mismatch(
            "prelu",
            &x.device().to_string(),
            &alpha.device().to_string(),
        )),
    }
}

/// ReLU6 activation: min(max(x, 0), 6)
pub fn relu6<T>(x: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone
        + Default
        + Zero
        + PartialOrd
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable
        + FromPrimitive,
{
    match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let zero = T::zero();
            let six = T::from_u32(6).unwrap_or_default();
            let result = arr.mapv(|v| {
                if v <= zero {
                    zero
                } else if v >= six {
                    six
                } else {
                    v
                }
            });
            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(_gpu_buffer) => {
            // For now, fall back to CPU implementation
            let cpu_tensor = x.to_cpu()?;
            relu6(&cpu_tensor)
        }
    }
}

/// Hard Swish activation: x * relu6(x + 3) / 6
pub fn hardswish<T>(x: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone
        + Default
        + Zero
        + PartialOrd
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable
        + FromPrimitive
        + std::ops::Add<Output = T>
        + std::ops::Mul<Output = T>
        + std::ops::Div<Output = T>,
{
    match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            let zero = T::zero();
            let three = T::from_u32(3).unwrap_or_default();
            let six = T::from_u32(6).unwrap_or_default();
            let result = arr.mapv(|v| {
                let relu6_val = if v + three <= zero {
                    zero
                } else if v + three >= six {
                    six
                } else {
                    v + three
                };
                v * relu6_val / six
            });
            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(_gpu_buffer) => {
            // For now, fall back to CPU implementation
            let cpu_tensor = x.to_cpu()?;
            hardswish(&cpu_tensor)
        }
    }
}

/// Log Softmax activation: log(softmax(x))
pub fn log_softmax<T>(x: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone
        + Default
        + Zero
        + PartialOrd
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable
        + scirs2_core::num_traits::Float,
{
    // log_softmax(x) = x - log(sum(exp(x)))
    // This is numerically stable implementation
    match &x.storage {
        crate::tensor::TensorStorage::Cpu(arr) => {
            // Find max for numerical stability
            let max_val = arr.fold(
                T::neg_infinity(),
                |acc, &val| {
                    if val > acc {
                        val
                    } else {
                        acc
                    }
                },
            );

            // Compute log_softmax
            let sum_exp = arr.fold(T::zero(), |acc, &val| acc + (val - max_val).exp());
            let log_sum_exp = sum_exp.ln() + max_val;

            let result = arr.mapv(|v| v - log_sum_exp);
            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        crate::tensor::TensorStorage::Gpu(_gpu_buffer) => {
            // For now, fall back to CPU implementation
            let cpu_tensor = x.to_cpu()?;
            log_softmax(&cpu_tensor)
        }
    }
}