scirs2-neural 0.4.2

Neural network building blocks module for SciRS2 (scirs2-neural) - Minimal Version
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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
//! Flash Attention V2 implementation
//!
//! This module implements the improved Flash Attention algorithm from:
//! "FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning"
//! by Tri Dao (2023).
//!
//! Key improvements over V1:
//! - Reduced non-matmul FLOPs by tracking separate alpha/beta correction factors
//!   in the online softmax, fusing the rescale into a single multiply
//! - Better parallelism: each Q block is processed independently, enabling
//!   forward-pass parallelism over N/B_r blocks
//! - Causal masking with early-exit: skip entire KV blocks beyond the diagonal

use crate::error::{NeuralError, Result};
use crate::layers::Layer;
use scirs2_core::ndarray::{s, Array, Array2, Array4, IxDyn, ScalarOperand};
use scirs2_core::numeric::{Float, NumAssign};
use scirs2_core::random::{Rng, RngExt};
use std::fmt::Debug;

/// Configuration for Flash Attention V2
#[derive(Debug, Clone)]
pub struct FlashAttentionV2Config {
    /// Number of attention heads
    pub num_heads: usize,
    /// Dimension of each attention head
    pub head_dim: usize,
    /// Block size for query tiling (B_r in the paper)
    pub block_size_q: usize,
    /// Block size for key/value tiling (B_c in the paper)
    pub block_size_kv: usize,
    /// Whether to use causal masking
    pub causal: bool,
    /// Dropout probability
    pub dropout_prob: f64,
    /// Custom scaling factor (default: 1/sqrt(head_dim))
    pub scale: Option<f64>,
}

impl Default for FlashAttentionV2Config {
    fn default() -> Self {
        Self {
            num_heads: 8,
            head_dim: 64,
            block_size_q: 128,
            block_size_kv: 128,
            causal: false,
            dropout_prob: 0.0,
            scale: None,
        }
    }
}

impl FlashAttentionV2Config {
    /// Create a new FlashAttentionV2Config
    pub fn new(num_heads: usize, head_dim: usize) -> Self {
        Self {
            num_heads,
            head_dim,
            ..Default::default()
        }
    }

    /// Set block size for queries (B_r)
    pub fn with_block_size_q(mut self, block_size: usize) -> Self {
        self.block_size_q = block_size;
        self
    }

    /// Set block size for keys/values (B_c)
    pub fn with_block_size_kv(mut self, block_size: usize) -> Self {
        self.block_size_kv = block_size;
        self
    }

    /// Enable causal masking
    pub fn with_causal(mut self, causal: bool) -> Self {
        self.causal = causal;
        self
    }

    /// Set dropout probability
    pub fn with_dropout(mut self, dropout_prob: f64) -> Self {
        self.dropout_prob = dropout_prob;
        self
    }

    /// Set custom scale factor
    pub fn with_scale(mut self, scale: f64) -> Self {
        self.scale = Some(scale);
        self
    }
}

/// Flash Attention V2 layer
///
/// Implements the improved Flash Attention algorithm with fused online softmax.
///
/// # Algorithm (per head)
///
/// ```text
/// for each Q block i (rows [i*B_r .. (i+1)*B_r]):
///     O_i = 0, m_i = -inf, l_i = 0
///     for each KV block j (rows [j*B_c .. (j+1)*B_c]):
///         S_ij = Q_i @ K_j^T * scale
///         if causal: mask S_ij where col > row
///         m_ij = rowmax(S_ij)
///         P_ij = exp(S_ij - m_ij)
///         l_ij = rowsum(P_ij)
///         m_new = max(m_i, m_ij)
///         alpha = exp(m_i - m_new)
///         beta  = exp(m_ij - m_new)
///         l_i   = alpha * l_i + beta * l_ij
///         O_i   = alpha * O_i + beta * P_ij @ V_j
///         m_i   = m_new
///     O_i = O_i / l_i
/// ```
///
/// # Examples
///
/// ```rust
/// use scirs2_neural::layers::{FlashAttentionV2, FlashAttentionV2Config, Layer};
/// use scirs2_core::ndarray::Array3;
/// use scirs2_core::random::rng;
///
/// let mut rng = rng();
/// let config = FlashAttentionV2Config::new(4, 16).with_causal(true);
/// let attn = FlashAttentionV2::<f64>::new(64, config, &mut rng).expect("failed");
///
/// let input = Array3::<f64>::from_elem((2, 32, 64), 0.1).into_dyn();
/// let output = attn.forward(&input).expect("failed");
/// assert_eq!(output.shape(), &[2, 32, 64]);
/// ```
#[derive(Debug)]
pub struct FlashAttentionV2<F: Float + Debug + Send + Sync + NumAssign> {
    /// Model dimension
    d_model: usize,
    /// Configuration
    config: FlashAttentionV2Config,
    /// Query projection weights [d_model, d_model]
    w_query: Array<F, IxDyn>,
    /// Key projection weights [d_model, d_model]
    w_key: Array<F, IxDyn>,
    /// Value projection weights [d_model, d_model]
    w_value: Array<F, IxDyn>,
    /// Output projection weights [d_model, d_model]
    w_output: Array<F, IxDyn>,
    /// Scaling factor
    scale: F,
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + 'static + NumAssign> FlashAttentionV2<F> {
    /// Create a new Flash Attention V2 layer
    pub fn new<R: Rng>(
        d_model: usize,
        config: FlashAttentionV2Config,
        rng: &mut R,
    ) -> Result<Self> {
        let total_dim = config.num_heads * config.head_dim;
        if total_dim != d_model {
            return Err(NeuralError::InvalidArchitecture(format!(
                "num_heads * head_dim ({}) must equal d_model ({})",
                total_dim, d_model
            )));
        }

        let xavier_std = (F::from(2.0)
            .ok_or_else(|| NeuralError::InvalidArchitecture("float conversion failed".into()))?
            / F::from(d_model + d_model).ok_or_else(|| {
                NeuralError::InvalidArchitecture("float conversion failed".into())
            })?)
        .sqrt();

        let w_query = Self::init_weight(d_model, d_model, xavier_std, rng)?;
        let w_key = Self::init_weight(d_model, d_model, xavier_std, rng)?;
        let w_value = Self::init_weight(d_model, d_model, xavier_std, rng)?;
        let w_output = Self::init_weight(d_model, d_model, xavier_std, rng)?;

        let scale = config
            .scale
            .and_then(|s| F::from(s))
            .or_else(|| {
                let hd = F::from(config.head_dim)?;
                Some(F::one() / hd.sqrt())
            })
            .ok_or_else(|| NeuralError::InvalidArchitecture("Failed to compute scale".into()))?;

        Ok(Self {
            d_model,
            config,
            w_query,
            w_key,
            w_value,
            w_output,
            scale,
        })
    }

    /// Initialize a weight matrix with Box-Muller Xavier initialization
    fn init_weight<R: Rng>(
        in_dim: usize,
        out_dim: usize,
        std_val: F,
        rng: &mut R,
    ) -> Result<Array<F, IxDyn>> {
        let mut weights = Array::zeros(IxDyn(&[in_dim, out_dim]));
        for w in weights.iter_mut() {
            let u1: f64 = rng.random();
            let u2: f64 = rng.random();
            let u1_clamped = if u1 < 1e-15 { 1e-15 } else { u1 };
            let z = (-2.0 * u1_clamped.ln()).sqrt() * (2.0 * std::f64::consts::PI * u2).cos();
            *w = F::from(z)
                .ok_or_else(|| NeuralError::InvalidArchitecture("float conversion".into()))?
                * std_val;
        }
        Ok(weights)
    }

    /// Flash Attention V2 core: per-head attention with fused online softmax
    fn flash_v2_forward(
        &self,
        query: &Array2<F>,
        key: &Array2<F>,
        value: &Array2<F>,
    ) -> Result<Array2<F>> {
        let seq_len_q = query.nrows();
        let seq_len_kv = key.nrows();
        let head_dim = query.ncols();

        let br = self.config.block_size_q.min(seq_len_q).max(1);
        let bc = self.config.block_size_kv.min(seq_len_kv).max(1);

        let num_blocks_q = seq_len_q.div_ceil(br);
        let num_blocks_kv = seq_len_kv.div_ceil(bc);

        let mut output = Array2::<F>::zeros((seq_len_q, head_dim));

        for qi in 0..num_blocks_q {
            let q_start = qi * br;
            let q_end = (q_start + br).min(seq_len_q);
            let q_len = q_end - q_start;

            let mut o_block = Array2::<F>::zeros((q_len, head_dim));
            let mut m_i = vec![F::neg_infinity(); q_len];
            let mut l_i = vec![F::zero(); q_len];

            let kv_limit = if self.config.causal {
                q_end.div_ceil(bc).min(num_blocks_kv)
            } else {
                num_blocks_kv
            };

            for kj in 0..kv_limit {
                let kv_start = kj * bc;
                let kv_end = (kv_start + bc).min(seq_len_kv);
                let kv_len = kv_end - kv_start;

                // S_ij = Q_block @ K_block^T * scale
                let mut s_block = Array2::<F>::zeros((q_len, kv_len));
                for i in 0..q_len {
                    for j in 0..kv_len {
                        let mut dot = F::zero();
                        for d in 0..head_dim {
                            dot += query[[q_start + i, d]] * key[[kv_start + j, d]];
                        }
                        s_block[[i, j]] = dot * self.scale;
                    }
                }

                // Causal mask
                if self.config.causal {
                    for i in 0..q_len {
                        let q_pos = q_start + i;
                        for j in 0..kv_len {
                            let k_pos = kv_start + j;
                            if k_pos > q_pos {
                                s_block[[i, j]] = F::neg_infinity();
                            }
                        }
                    }
                }

                // V2 fused online softmax update
                for i in 0..q_len {
                    let mut m_ij = F::neg_infinity();
                    for j in 0..kv_len {
                        if s_block[[i, j]] > m_ij {
                            m_ij = s_block[[i, j]];
                        }
                    }

                    let mut l_ij = F::zero();
                    let mut p_row = vec![F::zero(); kv_len];
                    for j in 0..kv_len {
                        if s_block[[i, j]] > F::neg_infinity() {
                            let p = (s_block[[i, j]] - m_ij).exp();
                            p_row[j] = p;
                            l_ij += p;
                        }
                    }

                    let m_new = if m_i[i] > m_ij { m_i[i] } else { m_ij };

                    let alpha = if m_i[i] == F::neg_infinity() {
                        F::zero()
                    } else {
                        (m_i[i] - m_new).exp()
                    };

                    let beta = if m_ij == F::neg_infinity() {
                        F::zero()
                    } else {
                        (m_ij - m_new).exp()
                    };

                    // O_i = alpha * O_i + beta * P_ij @ V_j
                    for d in 0..head_dim {
                        o_block[[i, d]] = alpha * o_block[[i, d]];
                        for j in 0..kv_len {
                            o_block[[i, d]] += beta * p_row[j] * value[[kv_start + j, d]];
                        }
                    }

                    l_i[i] = alpha * l_i[i] + beta * l_ij;
                    m_i[i] = m_new;
                }
            }

            // Final normalization
            for i in 0..q_len {
                if l_i[i] > F::zero() {
                    let inv = F::one() / l_i[i];
                    for d in 0..head_dim {
                        o_block[[i, d]] *= inv;
                    }
                }
                for d in 0..head_dim {
                    output[[q_start + i, d]] = o_block[[i, d]];
                }
            }
        }

        Ok(output)
    }

    /// Get the configuration
    pub fn config(&self) -> &FlashAttentionV2Config {
        &self.config
    }

    /// Get model dimension
    pub fn d_model(&self) -> usize {
        self.d_model
    }
}

impl<F> Layer<F> for FlashAttentionV2<F>
where
    F: Float + Debug + ScalarOperand + Send + Sync + 'static + NumAssign,
{
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

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

    fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>> {
        if input.ndim() != 3 {
            return Err(NeuralError::InvalidArchitecture(format!(
                "FlashAttentionV2 expects 3D input [batch, seq_len, d_model], got {}D",
                input.ndim()
            )));
        }

        let shape = input.shape();
        let batch_size = shape[0];
        let seq_len = shape[1];
        let d_model = shape[2];

        if d_model != self.d_model {
            return Err(NeuralError::InvalidArchitecture(format!(
                "Input dim {} != model dim {}",
                d_model, self.d_model
            )));
        }

        let num_heads = self.config.num_heads;
        let head_dim = self.config.head_dim;

        let input_2d = input
            .clone()
            .into_shape_with_order(IxDyn(&[batch_size * seq_len, d_model]))
            .map_err(|e| NeuralError::InferenceError(format!("reshape input: {e}")))?;

        let input_2d_view = input_2d
            .view()
            .into_dimensionality::<scirs2_core::ndarray::Ix2>()
            .map_err(|_| NeuralError::InferenceError("to 2D failed".into()))?;

        let w_q_2d = self
            .w_query
            .view()
            .into_dimensionality::<scirs2_core::ndarray::Ix2>()
            .map_err(|_| NeuralError::InferenceError("Q weights 2D".into()))?;
        let w_k_2d = self
            .w_key
            .view()
            .into_dimensionality::<scirs2_core::ndarray::Ix2>()
            .map_err(|_| NeuralError::InferenceError("K weights 2D".into()))?;
        let w_v_2d = self
            .w_value
            .view()
            .into_dimensionality::<scirs2_core::ndarray::Ix2>()
            .map_err(|_| NeuralError::InferenceError("V weights 2D".into()))?;
        let w_o_2d = self
            .w_output
            .view()
            .into_dimensionality::<scirs2_core::ndarray::Ix2>()
            .map_err(|_| NeuralError::InferenceError("O weights 2D".into()))?;

        let q_proj = input_2d_view.dot(&w_q_2d);
        let k_proj = input_2d_view.dot(&w_k_2d);
        let v_proj = input_2d_view.dot(&w_v_2d);

        let q_4d = q_proj
            .into_shape_with_order((batch_size, seq_len, num_heads, head_dim))
            .map_err(|e| NeuralError::InferenceError(format!("reshape Q: {e}")))?;
        let k_4d = k_proj
            .into_shape_with_order((batch_size, seq_len, num_heads, head_dim))
            .map_err(|e| NeuralError::InferenceError(format!("reshape K: {e}")))?;
        let v_4d = v_proj
            .into_shape_with_order((batch_size, seq_len, num_heads, head_dim))
            .map_err(|e| NeuralError::InferenceError(format!("reshape V: {e}")))?;

        let mut output_4d = Array4::<F>::zeros((batch_size, seq_len, num_heads, head_dim));

        for b in 0..batch_size {
            for h in 0..num_heads {
                let q_head: Array2<F> = q_4d
                    .slice(s![b, .., h, ..])
                    .to_owned()
                    .into_shape_with_order((seq_len, head_dim))
                    .map_err(|e| NeuralError::InferenceError(format!("Q head: {e}")))?;
                let k_head: Array2<F> = k_4d
                    .slice(s![b, .., h, ..])
                    .to_owned()
                    .into_shape_with_order((seq_len, head_dim))
                    .map_err(|e| NeuralError::InferenceError(format!("K head: {e}")))?;
                let v_head: Array2<F> = v_4d
                    .slice(s![b, .., h, ..])
                    .to_owned()
                    .into_shape_with_order((seq_len, head_dim))
                    .map_err(|e| NeuralError::InferenceError(format!("V head: {e}")))?;

                let attn_out = self.flash_v2_forward(&q_head, &k_head, &v_head)?;

                for i in 0..seq_len {
                    for d in 0..head_dim {
                        output_4d[[b, i, h, d]] = attn_out[[i, d]];
                    }
                }
            }
        }

        let output_3d = output_4d
            .into_shape_with_order((batch_size, seq_len, d_model))
            .map_err(|e| NeuralError::InferenceError(format!("reshape output: {e}")))?;

        let output_2d = output_3d
            .into_shape_with_order((batch_size * seq_len, d_model))
            .map_err(|e| NeuralError::InferenceError(format!("reshape O proj: {e}")))?;

        let final_output = output_2d.dot(&w_o_2d);

        let result = final_output
            .into_shape_with_order((batch_size, seq_len, d_model))
            .map_err(|e| NeuralError::InferenceError(format!("reshape final: {e}")))?;

        Ok(result.into_dyn())
    }

    fn backward(
        &self,
        _input: &Array<F, IxDyn>,
        _grad_output: &Array<F, IxDyn>,
    ) -> Result<Array<F, IxDyn>> {
        Err(NeuralError::NotImplementedError(
            "Flash Attention V2 backward not yet implemented".into(),
        ))
    }

    fn update(&mut self, _learning_rate: F) -> Result<()> {
        Ok(())
    }

    fn layer_type(&self) -> &str {
        "FlashAttentionV2"
    }
}

/// Standalone Flash Attention V2 compute function (no projection weights)
///
/// Computes attention using the V2 algorithm with tiling and fused online softmax.
///
/// # Arguments
/// * `query` - [batch, seq_q, head_dim]
/// * `key`   - [batch, seq_k, head_dim]
/// * `value` - [batch, seq_k, head_dim]
/// * `causal` - Whether to apply causal masking
/// * `block_size_q` - Block size for Q tiling
/// * `block_size_kv` - Block size for KV tiling
///
/// # Returns
/// Output tensor [batch, seq_q, head_dim]
pub fn flash_attention_v2_compute<F: Float + Debug + ScalarOperand + NumAssign>(
    query: &Array<F, IxDyn>,
    key: &Array<F, IxDyn>,
    value: &Array<F, IxDyn>,
    causal: bool,
    block_size_q: usize,
    block_size_kv: usize,
) -> Result<Array<F, IxDyn>> {
    if query.ndim() != 3 || key.ndim() != 3 || value.ndim() != 3 {
        return Err(NeuralError::InvalidArchitecture(
            "Q, K, V must be 3D tensors [batch, seq, dim]".into(),
        ));
    }

    let batch_size = query.shape()[0];
    let seq_len_q = query.shape()[1];
    let seq_len_kv = key.shape()[1];
    let head_dim = query.shape()[2];

    if key.shape()[2] != head_dim || value.shape()[2] != head_dim {
        return Err(NeuralError::InvalidArchitecture(
            "Q, K, V head_dim mismatch".into(),
        ));
    }

    let scale = F::one()
        / F::from(head_dim)
            .ok_or_else(|| NeuralError::InvalidArchitecture("float conv".into()))?
            .sqrt();

    let br = block_size_q.min(seq_len_q).max(1);
    let bc = block_size_kv.min(seq_len_kv).max(1);

    let mut output = Array::zeros(IxDyn(&[batch_size, seq_len_q, head_dim]));

    for b in 0..batch_size {
        let num_blocks_q = seq_len_q.div_ceil(br);
        let num_blocks_kv = seq_len_kv.div_ceil(bc);

        for qi in 0..num_blocks_q {
            let q_start = qi * br;
            let q_end = (q_start + br).min(seq_len_q);
            let q_len = q_end - q_start;

            let mut o_block = vec![F::zero(); q_len * head_dim];
            let mut m_i = vec![F::neg_infinity(); q_len];
            let mut l_i = vec![F::zero(); q_len];

            let kv_limit = if causal {
                q_end.div_ceil(bc).min(num_blocks_kv)
            } else {
                num_blocks_kv
            };

            for kj in 0..kv_limit {
                let kv_start = kj * bc;
                let kv_end = (kv_start + bc).min(seq_len_kv);
                let kv_len = kv_end - kv_start;

                for i in 0..q_len {
                    let q_pos = q_start + i;

                    let mut scores = vec![F::zero(); kv_len];
                    let mut m_ij = F::neg_infinity();

                    for (j, score) in scores.iter_mut().enumerate().take(kv_len) {
                        let k_pos = kv_start + j;
                        if causal && k_pos > q_pos {
                            *score = F::neg_infinity();
                        } else {
                            let mut dot = F::zero();
                            for d in 0..head_dim {
                                dot += query[[b, q_pos, d]] * key[[b, k_pos, d]];
                            }
                            *score = dot * scale;
                        }
                        if *score > m_ij {
                            m_ij = *score;
                        }
                    }

                    let mut l_ij = F::zero();
                    let mut p_row = vec![F::zero(); kv_len];
                    for j in 0..kv_len {
                        if scores[j] > F::neg_infinity() {
                            let p = (scores[j] - m_ij).exp();
                            p_row[j] = p;
                            l_ij += p;
                        }
                    }

                    let m_new = if m_i[i] > m_ij { m_i[i] } else { m_ij };
                    let alpha = if m_i[i] == F::neg_infinity() {
                        F::zero()
                    } else {
                        (m_i[i] - m_new).exp()
                    };
                    let beta = if m_ij == F::neg_infinity() {
                        F::zero()
                    } else {
                        (m_ij - m_new).exp()
                    };

                    for d in 0..head_dim {
                        let idx = i * head_dim + d;
                        o_block[idx] = alpha * o_block[idx];
                        for j in 0..kv_len {
                            o_block[idx] += beta * p_row[j] * value[[b, kv_start + j, d]];
                        }
                    }

                    l_i[i] = alpha * l_i[i] + beta * l_ij;
                    m_i[i] = m_new;
                }
            }

            for i in 0..q_len {
                let inv = if l_i[i] > F::zero() {
                    F::one() / l_i[i]
                } else {
                    F::zero()
                };
                for d in 0..head_dim {
                    output[[b, q_start + i, d]] = o_block[i * head_dim + d] * inv;
                }
            }
        }
    }

    Ok(output)
}

// ===========================================================================
// Tests
// ===========================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::layers::flash_attention::flash_attention_compute;
    use scirs2_core::ndarray::Array3;

    #[test]
    fn test_flash_v2_config() {
        let config = FlashAttentionV2Config::new(8, 64)
            .with_causal(true)
            .with_block_size_q(128)
            .with_block_size_kv(64)
            .with_dropout(0.05)
            .with_scale(0.125);

        assert_eq!(config.num_heads, 8);
        assert_eq!(config.head_dim, 64);
        assert!(config.causal);
        assert_eq!(config.block_size_q, 128);
        assert_eq!(config.block_size_kv, 64);
        assert!((config.dropout_prob - 0.05).abs() < 1e-10);
        assert!((config.scale.unwrap_or(0.0) - 0.125).abs() < 1e-10);
    }

    #[test]
    fn test_flash_v2_creation() {
        let mut rng = scirs2_core::random::rng();
        let config = FlashAttentionV2Config::new(4, 16);
        let result = FlashAttentionV2::<f64>::new(64, config, &mut rng);
        assert!(result.is_ok());
    }

    #[test]
    fn test_flash_v2_forward_shape() {
        let mut rng = scirs2_core::random::rng();
        let config = FlashAttentionV2Config::new(4, 16)
            .with_block_size_q(8)
            .with_block_size_kv(8);
        let attn = FlashAttentionV2::<f64>::new(64, config, &mut rng).expect("creation failed");

        let input = Array3::<f64>::from_elem((2, 16, 64), 0.1).into_dyn();
        let output = attn.forward(&input).expect("forward failed");
        assert_eq!(output.shape(), &[2, 16, 64]);
    }

    #[test]
    fn test_flash_v2_causal_masking() {
        let mut rng = scirs2_core::random::rng();
        let config = FlashAttentionV2Config::new(2, 8)
            .with_causal(true)
            .with_block_size_q(4)
            .with_block_size_kv(4);
        let attn = FlashAttentionV2::<f64>::new(16, config, &mut rng).expect("creation failed");

        let mut input = Array3::<f64>::zeros((1, 8, 16));
        for i in 0..8 {
            for j in 0..16 {
                input[[0, i, j]] = (i as f64 + 1.0) * 0.1 + j as f64 * 0.01;
            }
        }

        let output = attn.forward(&input.into_dyn()).expect("forward failed");
        assert_eq!(output.shape(), &[1, 8, 16]);

        for val in output.iter() {
            assert!(val.is_finite(), "causal output has non-finite value");
        }
    }

    #[test]
    fn test_flash_v2_matches_standard_attention() {
        let query = Array3::<f64>::from_elem((1, 4, 8), 0.5).into_dyn();
        let key = query.clone();
        let value = query.clone();

        let v2_output = flash_attention_v2_compute(&query, &key, &value, false, 2, 2)
            .expect("v2 compute failed");

        // Standard attention
        let scale = 1.0 / (8.0_f64).sqrt();
        let mut scores = Array2::<f64>::zeros((4, 4));
        for i in 0..4 {
            for j in 0..4 {
                let mut dot = 0.0;
                for _d in 0..8 {
                    dot += 0.5 * 0.5;
                }
                scores[[i, j]] = dot * scale;
            }
        }

        let mut attention = scores.clone();
        for i in 0..4 {
            let max_val = attention.row(i).fold(f64::NEG_INFINITY, |a, &b| a.max(b));
            let mut sum = 0.0;
            for j in 0..4 {
                let exp_val = (attention[[i, j]] - max_val).exp();
                attention[[i, j]] = exp_val;
                sum += exp_val;
            }
            for j in 0..4 {
                attention[[i, j]] /= sum;
            }
        }

        let mut standard_output = Array2::<f64>::zeros((4, 8));
        for i in 0..4 {
            for d in 0..8 {
                let mut sum = 0.0;
                for j in 0..4 {
                    sum += attention[[i, j]] * 0.5;
                }
                standard_output[[i, d]] = sum;
            }
        }

        for i in 0..4 {
            for d in 0..8 {
                assert!(
                    (v2_output[[0, i, d]] - standard_output[[i, d]]).abs() < 1e-10,
                    "V2 mismatch at [{i}, {d}]"
                );
            }
        }
    }

    #[test]
    fn test_flash_v2_different_block_sizes_same_result() {
        let mut query = Array3::<f64>::zeros((1, 6, 4)).into_dyn();
        let mut key = Array3::<f64>::zeros((1, 6, 4)).into_dyn();
        let mut value = Array3::<f64>::zeros((1, 6, 4)).into_dyn();

        for t in 0..6 {
            for d in 0..4 {
                let v = ((t * 4 + d) as f64) * 0.1;
                query[[0, t, d]] = v;
                key[[0, t, d]] = v * 0.8;
                value[[0, t, d]] = v * 0.5 + 0.1;
            }
        }

        let out_bs2 =
            flash_attention_v2_compute(&query, &key, &value, false, 2, 2).expect("bs2 failed");
        let out_bs3 =
            flash_attention_v2_compute(&query, &key, &value, false, 3, 3).expect("bs3 failed");
        let out_bs6 =
            flash_attention_v2_compute(&query, &key, &value, false, 6, 6).expect("bs6 failed");

        for t in 0..6 {
            for d in 0..4 {
                let a = out_bs2[[0, t, d]];
                let b = out_bs3[[0, t, d]];
                let c = out_bs6[[0, t, d]];
                assert!(
                    (a - b).abs() < 1e-10 && (b - c).abs() < 1e-10,
                    "block size mismatch at [{t}, {d}]: bs2={a}, bs3={b}, bs6={c}"
                );
            }
        }
    }

    #[test]
    fn test_flash_v2_causal_matches_v1_causal() {
        let mut query = Array3::<f64>::zeros((1, 5, 6)).into_dyn();
        let mut key = Array3::<f64>::zeros((1, 5, 6)).into_dyn();
        let mut value = Array3::<f64>::zeros((1, 5, 6)).into_dyn();

        for t in 0..5 {
            for d in 0..6 {
                let v = ((t + 1) as f64 * 0.15) + (d as f64 * 0.03);
                query[[0, t, d]] = v;
                key[[0, t, d]] = v * 1.1;
                value[[0, t, d]] = v * 0.7;
            }
        }

        let v1_out = flash_attention_compute(&query, &key, &value, true, 2).expect("v1 failed");
        let v2_out =
            flash_attention_v2_compute(&query, &key, &value, true, 2, 2).expect("v2 failed");

        for t in 0..5 {
            for d in 0..6 {
                assert!(
                    (v1_out[[0, t, d]] - v2_out[[0, t, d]]).abs() < 1e-10,
                    "v1 vs v2 causal mismatch at [{t}, {d}]"
                );
            }
        }
    }

    #[test]
    fn test_flash_v2_numerical_stability() {
        let mut query = Array3::<f64>::zeros((1, 4, 4)).into_dyn();
        for t in 0..4 {
            for d in 0..4 {
                query[[0, t, d]] = (t as f64 + 1.0) * 10.0;
            }
        }
        let key = query.clone();
        let value = Array3::<f64>::from_elem((1, 4, 4), 1.0).into_dyn();

        let out = flash_attention_v2_compute(&query, &key, &value, false, 2, 2).expect("failed");

        for val in out.iter() {
            assert!(val.is_finite(), "non-finite in stability test");
        }
    }

    #[test]
    fn test_flash_v2_invalid_input() {
        let q_2d = Array2::<f64>::zeros((4, 8)).into_dyn();
        let k = Array3::<f64>::zeros((1, 4, 8)).into_dyn();
        let v = k.clone();

        let result = flash_attention_v2_compute(&q_2d, &k, &v, false, 2, 2);
        assert!(result.is_err());
    }
}