scirs2-linalg 0.4.2

Linear algebra module for SciRS2 (scirs2-linalg)
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
//! Specialized Attention Mechanisms
//!
//! This module provides various specialized attention mechanisms beyond
//! the standard scaled dot-product attention, including flash attention,
//! sparse attention, rotary embeddings, and more.

use scirs2_core::ndarray::{Array1, Array2, Array3, ArrayView1, ArrayView2, ArrayView3};
use scirs2_core::numeric::{Float, NumAssignOps, Zero};
use std::ops::{Add, Div, Mul, Sub};

use super::utils::{attention, AttentionMask};
use crate::error::{check_dimensions, LinalgError, LinalgResult};

/// Flash Attention - Memory-efficient attention implementation
///
/// Implements the Flash Attention algorithm which reduces memory usage by computing
/// attention in blocks, avoiding the materialization of the full attention matrix.
///
/// # Arguments
///
/// * `query` - Query tensor of shape [batchsize, seq_len_q, d_model]
/// * `key` - Key tensor of shape [batchsize, seq_len_k, d_model]
/// * `value` - Value tensor of shape [batchsize, seq_len_k, d_model]
/// * `mask` - Optional mask to apply to attention weights
/// * `scale` - Scaling factor for dot product
/// * `blocksize` - Block size for tiling (affects performance but not results)
///
/// # Returns
///
/// * Output tensor of shape [batchsize, seq_len_q, d_model]
#[allow(dead_code)]
pub fn flash_attention<F>(
    query: &ArrayView3<F>,
    key: &ArrayView3<F>,
    value: &ArrayView3<F>,
    mask: Option<&AttentionMask>,
    scale: F,
    blocksize: usize,
) -> LinalgResult<Array3<F>>
where
    F: Float + Add + Mul + Div + Sub + NumAssignOps + Zero + std::fmt::Debug,
{
    // Validate dimensions
    let (batchsize, seq_len_q, d_model) = (query.shape()[0], query.shape()[1], query.shape()[2]);
    let (batchsize_k, seq_len_k, d_model_k) = (key.shape()[0], key.shape()[1], key.shape()[2]);
    let (batchsize_v, seq_len_v, d_model_v) =
        (value.shape()[0], value.shape()[1], value.shape()[2]);

    check_dimensions(
        batchsize == batchsize_k,
        format!("Batch sizes must match: {batchsize} != {batchsize_k}"),
    )?;
    check_dimensions(
        batchsize == batchsize_v,
        format!("Batch sizes must match: {batchsize} != {batchsize_v}"),
    )?;
    check_dimensions(
        seq_len_k == seq_len_v,
        format!("Key and value sequence lengths must match: {seq_len_k} != {seq_len_v}"),
    )?;
    check_dimensions(
        d_model == d_model_k,
        format!("Query and key dimensions must match: {d_model} != {d_model_k}"),
    )?;

    // Determine block sizes
    let blocksize_q = blocksize.min(seq_len_q);
    let blocksize_k = blocksize.min(seq_len_k);

    // Initialize output
    let mut output = Array3::<F>::zeros((batchsize, seq_len_q, d_model_v));

    // Process batch by batch
    for b in 0..batchsize {
        // Process query blocks
        for q_start in (0..seq_len_q).step_by(blocksize_q) {
            let q_end = (q_start + blocksize_q).min(seq_len_q);
            let q_block = query.slice(scirs2_core::ndarray::s![b, q_start..q_end, ..]);

            // For each query block, process all key/value blocks
            let mut m_block = Array1::<F>::from_elem(q_end - q_start, F::neg_infinity());
            let mut l_block = Array1::<F>::zeros(q_end - q_start);

            for k_start in (0..seq_len_k).step_by(blocksize_k) {
                let k_end = (k_start + blocksize_k).min(seq_len_k);
                let k_block = key.slice(scirs2_core::ndarray::s![b, k_start..k_end, ..]);
                let v_block = value.slice(scirs2_core::ndarray::s![b, k_start..k_end, ..]);

                // Compute block of attention scores
                let mut scores_block = Array2::<F>::zeros((q_end - q_start, k_end - k_start));

                for i in 0..(q_end - q_start) {
                    for j in 0..(k_end - k_start) {
                        let mut dot_product = F::zero();
                        for k in 0..d_model {
                            dot_product += q_block[[i, k]] * k_block[[j, k]];
                        }
                        scores_block[[i, j]] = dot_product * scale;
                    }
                }

                // Apply mask if provided
                if let Some(mask_ref) = mask {
                    match mask_ref {
                        AttentionMask::Causal => {
                            for i in 0..(q_end - q_start) {
                                let q_idx = q_start + i;
                                for j in 0..(k_end - k_start) {
                                    let k_idx = k_start + j;
                                    if k_idx > q_idx {
                                        scores_block[[i, j]] = F::neg_infinity();
                                    }
                                }
                            }
                        }
                        // For other mask types, we would need to extract the relevant portion
                        // This is a simplified implementation for demonstration
                        _ => {
                            return Err(LinalgError::NotImplementedError(
                                "Flash attention currently only supports causal masks".to_string(),
                            ))
                        }
                    }
                }

                // Update max values and compute exponentials
                for i in 0..(q_end - q_start) {
                    let row = scores_block.slice(scirs2_core::ndarray::s![i, ..]);
                    let max_val =
                        row.fold(F::neg_infinity(), |max, &x| if x > max { x } else { max });

                    if max_val > m_block[i] {
                        // Update scaling factors
                        let m_prev = m_block[i];
                        let m_new = max_val;

                        // Update l_i <- l_i * exp(m_i - m'_i)
                        if m_prev != F::neg_infinity() {
                            l_block[i] *= (m_prev - m_new).exp();
                        }

                        // Update output with scaling
                        if l_block[i] > F::zero() {
                            let scale_factor = if l_block[i] != F::zero() {
                                (m_prev - m_new).exp() / l_block[i]
                            } else {
                                // Handle zero normalization factor
                                F::zero()
                            };
                            for j in 0..d_model_v {
                                output[[b, q_start + i, j]] *= scale_factor;
                            }
                        }

                        // Update m_i
                        m_block[i] = m_new;
                    }

                    // Compute contribution of current key-value block
                    let mut block_sum = F::zero();
                    let mut block_output = Array1::<F>::zeros(d_model_v);

                    for j in 0..(k_end - k_start) {
                        let exp_val = (scores_block[[i, j]] - m_block[i]).exp();
                        block_sum += exp_val;

                        // Update output
                        for k in 0..d_model_v {
                            block_output[k] += exp_val * v_block[[j, k]];
                        }
                    }

                    // Update l_i and output
                    l_block[i] += block_sum;
                    for j in 0..d_model_v {
                        output[[b, q_start + i, j]] += block_output[j];
                    }
                }
            }

            // Normalize output by l_block
            for i in 0..(q_end - q_start) {
                if l_block[i] > F::zero() {
                    for j in 0..d_model_v {
                        output[[b, q_start + i, j]] /= l_block[i];
                    }
                }
            }
        }
    }

    Ok(output)
}

/// Sparse Attention - Implements attention with sparse patterns
///
/// Computes attention using predetermined sparse attention patterns to reduce
/// computational complexity from O(n²) to O(n * log(n)) or even O(n).
///
/// # Arguments
///
/// * `query` - Query tensor of shape [batchsize, seq_len_q, d_model]
/// * `key` - Key tensor of shape [batchsize, seq_len_k, d_model]
/// * `value` - Value tensor of shape [batchsize, seq_len_k, d_model]
/// * `pattern_mask` - Boolean mask defining the sparse attention pattern [seq_len_q, seq_len_k]
/// * `scale` - Scaling factor for dot product
///
/// # Returns
///
/// * Output tensor of shape [batchsize, seq_len_q, d_model]
#[allow(dead_code)]
pub fn sparse_attention<F>(
    query: &ArrayView3<F>,
    key: &ArrayView3<F>,
    value: &ArrayView3<F>,
    pattern_mask: &ArrayView2<bool>,
    scale: F,
) -> LinalgResult<Array3<F>>
where
    F: Float + Add + Mul + Div + Sub + NumAssignOps + Zero + std::fmt::Debug,
{
    // Validate dimensions
    let (batchsize, seq_len_q, d_model) = (query.shape()[0], query.shape()[1], query.shape()[2]);
    let (_, seq_len_k, _) = (key.shape()[0], key.shape()[1], key.shape()[2]);
    let (_, _, d_model_v) = (value.shape()[0], value.shape()[1], value.shape()[2]);

    if pattern_mask.shape() != [seq_len_q, seq_len_k] {
        return Err(LinalgError::DimensionError(format!(
            "Pattern mask shape {:?} doesn't match query and key sequence lengths [{}, {}]",
            pattern_mask.shape(),
            seq_len_q,
            seq_len_k
        )));
    }

    // Initialize output
    let mut output = Array3::<F>::zeros((batchsize, seq_len_q, d_model_v));

    // Process batch by batch
    for b in 0..batchsize {
        let q_b = query.slice(scirs2_core::ndarray::s![b, .., ..]);
        let k_b = key.slice(scirs2_core::ndarray::s![b, .., ..]);
        let v_b = value.slice(scirs2_core::ndarray::s![b, .., ..]);

        // For each query position
        for i in 0..seq_len_q {
            let q_i = q_b.slice(scirs2_core::ndarray::s![i, ..]);

            // Calculate sparse attention scores
            let mut scores = Vec::new();
            let mut indices = Vec::new();

            // Only compute scores for positions allowed by the pattern mask
            for j in 0..seq_len_k {
                if pattern_mask[[i, j]] {
                    let k_j = k_b.slice(scirs2_core::ndarray::s![j, ..]);

                    // Compute dot product
                    let mut dot_product = F::zero();
                    for k in 0..d_model {
                        dot_product += q_i[k] * k_j[k];
                    }

                    scores.push(dot_product * scale);
                    indices.push(j);
                }
            }

            // If no attention connections, continue to next query position
            if scores.is_empty() {
                continue;
            }

            // Apply softmax to the sparse scores
            let max_val = scores
                .iter()
                .fold(F::neg_infinity(), |max, &x| if x > max { x } else { max });

            let mut exp_scores = Vec::with_capacity(scores.len());
            let mut sum = F::zero();

            for &score in &scores {
                let exp_val = (score - max_val).exp();
                exp_scores.push(exp_val);
                sum += exp_val;
            }

            // Normalize scores
            if sum > F::zero() {
                for exp_score in &mut exp_scores {
                    *exp_score /= sum;
                }
            }

            // Compute weighted sum of values
            for j in 0..d_model_v {
                let mut weighted_sum = F::zero();

                for k in 0..indices.len() {
                    let v_idx = indices[k];
                    weighted_sum += exp_scores[k] * v_b[[v_idx, j]];
                }

                output[[b, i, j]] = weighted_sum;
            }
        }
    }

    Ok(output)
}

/// Attention with ALiBi (Attention with Linear Biases)
///
/// Implements attention with linear biases as described in the paper
/// "Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation"
///
/// # Arguments
///
/// * `query` - Query tensor of shape [batchsize, seq_len_q, d_model]
/// * `key` - Key tensor of shape [batchsize, seq_len_k, d_model]
/// * `value` - Value tensor of shape [batchsize, seq_len_k, d_model]
/// * `slopes` - Tensor of slope values for each attention head
/// * `scale` - Scaling factor for dot product
/// * `causal` - Whether to apply causal masking
///
/// # Returns
///
/// * Output tensor of shape [batchsize, seq_len_q, d_model]
#[allow(dead_code)]
pub fn attention_with_alibi<F>(
    query: &ArrayView3<F>,
    key: &ArrayView3<F>,
    value: &ArrayView3<F>,
    slopes: &ArrayView1<F>,
    scale: F,
    causal: bool,
) -> LinalgResult<Array3<F>>
where
    F: Float + Add + Mul + Div + Sub + NumAssignOps + Zero + std::fmt::Debug,
{
    // Validate dimensions
    let (batchsize, seq_len_q, d_model) = (query.shape()[0], query.shape()[1], query.shape()[2]);
    let (_, seq_len_k, _) = (key.shape()[0], key.shape()[1], key.shape()[2]);
    let (_, _, d_model_v) = (value.shape()[0], value.shape()[1], value.shape()[2]);

    // Calculate attention scores (QK^T)
    let mut result = Array3::<F>::zeros((batchsize, seq_len_q, d_model_v));

    for b in 0..batchsize {
        // Calculate QK^T
        let q_b = query.slice(scirs2_core::ndarray::s![b, .., ..]);
        let k_b = key.slice(scirs2_core::ndarray::s![b, .., ..]);
        let v_b = value.slice(scirs2_core::ndarray::s![b, .., ..]);

        let mut scores = Array2::<F>::zeros((seq_len_q, seq_len_k));

        // Compute dot products
        for i in 0..seq_len_q {
            for j in 0..seq_len_k {
                let mut dot_product = F::zero();
                for k in 0..d_model {
                    dot_product += q_b[[i, k]] * k_b[[j, k]];
                }
                scores[[i, j]] = dot_product * scale;
            }
        }

        // Apply ALiBi bias (linear bias based on position difference)
        for i in 0..seq_len_q {
            for j in 0..seq_len_k {
                // In ALiBi, the bias is -slope * |i - j|
                // For simplicity, we'll use a single slope here
                let pos_diff =
                    F::from((i as isize - j as isize).abs() as f64).ok_or_else(|| {
                        LinalgError::ValueError(
                            "Failed to convert position difference to target type".to_string(),
                        )
                    })?;
                let slope = slopes[0]; // Using first slope for simplicity
                scores[[i, j]] -= slope * pos_diff;
            }
        }

        // Apply causal mask if requested
        if causal {
            for i in 0..seq_len_q {
                for j in 0..seq_len_k {
                    if j > i {
                        scores[[i, j]] = F::neg_infinity();
                    }
                }
            }
        }

        // Apply softmax to each row
        for i in 0..seq_len_q {
            let mut row = scores.slice_mut(scirs2_core::ndarray::s![i, ..]);

            // Find max for numerical stability
            let max_val = row.fold(F::neg_infinity(), |max, &x| if x > max { x } else { max });

            // Compute exp and sum
            let mut sum = F::zero();
            for j in 0..seq_len_k {
                let exp_val = (row[j] - max_val).exp();
                row[j] = exp_val;
                sum += exp_val;
            }

            // Normalize
            if sum > F::zero() {
                for j in 0..seq_len_k {
                    row[j] /= sum;
                }
            }
        }

        // Compute output: scores @ value
        let mut output = Array2::<F>::zeros((seq_len_q, d_model_v));

        for i in 0..seq_len_q {
            for j in 0..d_model_v {
                let mut sum = F::zero();
                for k in 0..seq_len_k {
                    sum += scores[[i, k]] * v_b[[k, j]];
                }
                output[[i, j]] = sum;
            }
        }

        // Store result
        result
            .slice_mut(scirs2_core::ndarray::s![b, .., ..])
            .assign(&output);
    }

    Ok(result)
}

/// Rotary Position Embeddings (RoPE)
///
/// Applies rotary position embeddings to query and key tensors as described in
/// "RoFormer: Enhanced Transformer with Rotary Position Embedding"
///
/// # Arguments
///
/// * `x` - Input tensor of shape [batchsize, seq_len, d_model]
/// * `freq_base` - Base frequency for the rotations (default: 10000.0)
///
/// # Returns
///
/// * Tensor with rotary position embeddings applied
#[allow(dead_code)]
pub fn rotary_embedding<F>(x: &ArrayView3<F>, freqbase: F) -> LinalgResult<Array3<F>>
where
    F: Float + Add + Mul + Div + Sub + NumAssignOps + Zero + std::fmt::Debug,
{
    let (batchsize, seq_len, d_model) = (x.shape()[0], x.shape()[1], x.shape()[2]);

    // Ensure dimension is even for proper pairing of dimensions
    if d_model % 2 != 0 {
        return Err(LinalgError::ValueError(
            "Dimension must be even for rotary embeddings".to_string(),
        ));
    }

    let mut result = Array3::<F>::zeros((batchsize, seq_len, d_model));

    // Create position frequencies
    if d_model % 2 != 0 {
        return Err(LinalgError::ValueError(
            "Model dimension must be even for rotary embeddings".to_string(),
        ));
    }
    let half_dim = d_model / 2;
    let mut freqs = Vec::with_capacity(half_dim);

    for i in 0..half_dim {
        let exponent = F::from(2.0 * i as f64 / d_model as f64).ok_or_else(|| {
            LinalgError::ValueError(
                "Failed to convert frequency exponent to target type".to_string(),
            )
        })?;
        let freq = F::one() / freqbase.powf(exponent);
        freqs.push(freq);
    }

    // Apply rotary embeddings
    for b in 0..batchsize {
        for pos in 0..seq_len {
            for (i, _) in freqs.iter().enumerate().take(half_dim) {
                let i2 = 2 * i;

                // Get current values
                let x_i = x[[b, pos, i2]];
                let x_i_plus_1 = x[[b, pos, i2 + 1]];

                // Calculate rotation
                let pos_f = F::from(pos as f64).ok_or_else(|| {
                    LinalgError::ValueError("Failed to convert position to target type".to_string())
                })?;
                let theta = pos_f * freqs[i];
                let cos_theta = theta.cos();
                let sin_theta = theta.sin();

                // Apply rotation
                result[[b, pos, i2]] = x_i * cos_theta - x_i_plus_1 * sin_theta;
                result[[b, pos, i2 + 1]] = x_i * sin_theta + x_i_plus_1 * cos_theta;
            }
        }
    }

    Ok(result)
}

/// Linear Attention
///
/// Implements linear attention which reduces computational complexity from O(n²) to O(n)
/// by using a linearized kernel function.
///
/// # Arguments
///
/// * `query` - Query tensor of shape [batchsize, seq_len_q, d_model]
/// * `key` - Key tensor of shape [batchsize, seq_len_k, d_model]
/// * `value` - Value tensor of shape [batchsize, seq_len_k, d_model]
/// * `scale` - Scaling factor for dot product
///
/// # Returns
///
/// * Output tensor of shape [batchsize, seq_len_q, d_model]
#[allow(dead_code)]
pub fn linear_attention<F>(
    query: &ArrayView3<F>,
    key: &ArrayView3<F>,
    value: &ArrayView3<F>,
    scale: F,
) -> LinalgResult<Array3<F>>
where
    F: Float + Add + Mul + Div + Sub + NumAssignOps + Zero + std::fmt::Debug,
{
    let (batchsize, seq_len_q, d_model) = (query.shape()[0], query.shape()[1], query.shape()[2]);
    let (_, seq_len_k, _) = (key.shape()[0], key.shape()[1], key.shape()[2]);
    let (_, _, d_model_v) = (value.shape()[0], value.shape()[1], value.shape()[2]);

    let mut result = Array3::<F>::zeros((batchsize, seq_len_q, d_model_v));

    // Apply elu + 1 feature mapping to query and key
    for b in 0..batchsize {
        // Feature map: elu(x) + 1
        let mut q_prime = Array2::<F>::zeros((seq_len_q, d_model));
        let mut k_prime = Array2::<F>::zeros((seq_len_k, d_model));

        // Apply feature map to query
        for i in 0..seq_len_q {
            for j in 0..d_model {
                let x = query[[b, i, j]];
                q_prime[[i, j]] = if x > F::zero() {
                    x
                } else {
                    (x.exp() - F::one()) + F::one()
                };
            }
        }

        // Apply feature map to key
        for i in 0..seq_len_k {
            for j in 0..d_model {
                let x = key[[b, i, j]] * scale;
                k_prime[[i, j]] = if x > F::zero() {
                    x
                } else {
                    (x.exp() - F::one()) + F::one()
                };
            }
        }

        // Compute KV first (linear complexity)
        let mut kv = Array2::<F>::zeros((d_model, d_model_v));

        for i in 0..d_model {
            for j in 0..d_model_v {
                let mut sum = F::zero();
                for k in 0..seq_len_k {
                    sum += k_prime[[k, i]] * value[[b, k, j]];
                }
                kv[[i, j]] = sum;
            }
        }

        // Compute normalization factor
        let mut z = Array1::<F>::zeros(seq_len_q);

        for i in 0..seq_len_q {
            let mut sum = F::zero();
            for j in 0..d_model {
                let mut k_sum = F::zero();
                for k in 0..seq_len_k {
                    k_sum += k_prime[[k, j]];
                }
                sum += q_prime[[i, j]] * k_sum;
            }
            z[i] = sum;
        }

        // Compute final output: (Q·(K^T·V)) / z
        for i in 0..seq_len_q {
            for j in 0..d_model_v {
                let mut sum = F::zero();
                for k in 0..d_model {
                    sum += q_prime[[i, k]] * kv[[k, j]];
                }

                if z[i] > F::zero() {
                    result[[b, i, j]] = if z[i] != F::zero() {
                        sum / z[i]
                    } else {
                        // Handle zero normalization - typically indicates all-masked row
                        F::zero()
                    };
                }
            }
        }
    }

    Ok(result)
}

/// Relative Position Attention
///
/// Implements attention with relative position encodings as described in
/// "Self-Attention with Relative Position Representations"
///
/// # Arguments
///
/// * `query` - Query tensor of shape [batchsize, seq_len_q, d_model]
/// * `key` - Key tensor of shape [batchsize, seq_len_k, d_model]
/// * `value` - Value tensor of shape [batchsize, seq_len_k, d_model]
/// * `rel_emb` - Relative position embeddings of shape [2*max_len-1, d_model]
/// * `scale` - Scaling factor for dot product
///
/// # Returns
///
/// * Output tensor of shape [batchsize, seq_len_q, d_model]
#[allow(dead_code)]
pub fn relative_position_attention<F>(
    query: &ArrayView3<F>,
    key: &ArrayView3<F>,
    value: &ArrayView3<F>,
    rel_emb: &ArrayView2<F>,
    scale: F,
) -> LinalgResult<Array3<F>>
where
    F: Float + Add + Mul + Div + Sub + NumAssignOps + Zero + std::fmt::Debug,
{
    let (batchsize, seq_len_q, d_model) = (query.shape()[0], query.shape()[1], query.shape()[2]);
    let (_, seq_len_k, _) = (key.shape()[0], key.shape()[1], key.shape()[2]);
    let (_, _, d_model_v) = (value.shape()[0], value.shape()[1], value.shape()[2]);

    // Validate relative embedding dimensions
    let expected_rel_emb_len = 2 * seq_len_k.max(seq_len_q) - 1;
    if rel_emb.shape()[0] != expected_rel_emb_len || rel_emb.shape()[1] != d_model {
        return Err(LinalgError::DimensionError(format!(
            "Relative embedding shape should be [{}, {}], got {:?}",
            expected_rel_emb_len,
            d_model,
            rel_emb.shape()
        )));
    }

    let mut result = Array3::<F>::zeros((batchsize, seq_len_q, d_model_v));

    // Process batch by batch
    for b in 0..batchsize {
        // Calculate content-content attention: QK^T
        let mut content_scores = Array2::<F>::zeros((seq_len_q, seq_len_k));

        for i in 0..seq_len_q {
            for j in 0..seq_len_k {
                let mut dot_product = F::zero();
                for k in 0..d_model {
                    dot_product += query[[b, i, k]] * key[[b, j, k]];
                }
                content_scores[[i, j]] = dot_product * scale;
            }
        }

        // Calculate content-position attention: QR^T
        let mut pos_scores = Array2::<F>::zeros((seq_len_q, seq_len_k));

        for i in 0..seq_len_q {
            for j in 0..seq_len_k {
                let rel_pos = (seq_len_k - 1) + i - j; // Offset for zero-indexing
                let mut dot_product = F::zero();
                for k in 0..d_model {
                    dot_product += query[[b, i, k]] * rel_emb[[rel_pos, k]];
                }
                pos_scores[[i, j]] = dot_product * scale;
            }
        }

        // Combine scores and apply softmax
        let mut combined_scores = Array2::<F>::zeros((seq_len_q, seq_len_k));

        for i in 0..seq_len_q {
            // Combine content and position scores
            for j in 0..seq_len_k {
                combined_scores[[i, j]] = content_scores[[i, j]] + pos_scores[[i, j]];
            }

            // Apply softmax
            let mut row = combined_scores.slice_mut(scirs2_core::ndarray::s![i, ..]);
            let max_val = row.fold(F::neg_infinity(), |max, &x| if x > max { x } else { max });

            let mut sum = F::zero();
            for j in 0..seq_len_k {
                let exp_val = (row[j] - max_val).exp();
                row[j] = exp_val;
                sum += exp_val;
            }

            // Normalize
            if sum > F::zero() {
                for j in 0..seq_len_k {
                    row[j] /= sum;
                }
            }
        }

        // Compute output: combined_scores @ value
        let mut output = Array2::<F>::zeros((seq_len_q, d_model_v));

        for i in 0..seq_len_q {
            for j in 0..d_model_v {
                let mut sum = F::zero();
                for k in 0..seq_len_k {
                    sum += combined_scores[[i, k]] * value[[b, k, j]];
                }
                output[[i, j]] = sum;
            }
        }

        // Store the result for this batch
        result
            .slice_mut(scirs2_core::ndarray::s![b, .., ..])
            .assign(&output);
    }

    Ok(result)
}

/// Attention with Relative Position Encodings (RPE)
///
/// Flexible interface for relative position encoding that supports
/// multiple implementation variants.
///
/// # Arguments
///
/// * `query` - Query tensor [batchsize, seq_len_q, d_model]
/// * `key` - Key tensor [batchsize, seq_len_k, d_model]
/// * `value` - Value tensor [batchsize, seq_len_k, d_model]
/// * `rel_emb` - Relative embeddings tensor
/// * `scale` - Scaling factor for attention
/// * `use_xpos` - Whether to use the xPos style of relative positioning
///
/// # Returns
///
/// * Output tensor of shape [batchsize, seq_len_q, d_model]
#[allow(dead_code)]
pub fn attention_with_rpe<F>(
    query: &ArrayView3<F>,
    key: &ArrayView3<F>,
    value: &ArrayView3<F>,
    rel_emb: &ArrayView2<F>,
    scale: F,
    use_xpos: bool,
) -> LinalgResult<Array3<F>>
where
    F: Float + Add + Mul + Div + Sub + NumAssignOps + Zero + std::fmt::Debug,
{
    // If using xPos, apply the specialized implementation
    if use_xpos {
        // This is a simplified implementation of xPos relative position encoding
        // In a full implementation, we would implement the complete xPos algorithm

        let (batchsize, seq_len_q, d_model) =
            (query.shape()[0], query.shape()[1], query.shape()[2]);
        let (_, seq_len_k, _) = (key.shape()[0], key.shape()[1], key.shape()[2]);
        let (_, _, _d_model_v) = (value.shape()[0], value.shape()[1], value.shape()[2]); // For future use

        // Create scaled arrays for computation

        // Apply xPos scaling to query and key based on position
        let mut q_scaled = Array3::<F>::zeros((batchsize, seq_len_q, d_model));
        let mut k_scaled = Array3::<F>::zeros((batchsize, seq_len_k, d_model));

        // Apply rotary-style position encoding with xPos modifications
        for b in 0..batchsize {
            for i in 0..seq_len_q {
                let pos_i = F::from(i as f64 + 1.0).expect("Operation failed"); // 1-indexed position
                for j in 0..d_model {
                    // Apply position-dependent scaling
                    let dim_factor = F::from(j as f64 / d_model as f64).expect("Operation failed");
                    let scale_factor = F::one() / pos_i.powf(dim_factor);
                    q_scaled[[b, i, j]] = query[[b, i, j]] * scale_factor;
                }
            }

            for i in 0..seq_len_k {
                let pos_i = F::from(i as f64 + 1.0).expect("Operation failed"); // 1-indexed position
                for j in 0..d_model {
                    // Apply position-dependent scaling
                    let dim_factor = F::from(j as f64 / d_model as f64).expect("Operation failed");
                    let scale_factor = F::one() / pos_i.powf(dim_factor);
                    k_scaled[[b, i, j]] = key[[b, i, j]] * scale_factor;
                }
            }
        }

        // Now compute attention with the scaled tensors
        return attention(&q_scaled.view(), &k_scaled.view(), value, None, scale);
    }

    // Otherwise use standard relative position embeddings
    relative_position_attention(query, key, value, rel_emb, scale)
}