oxicuda-blas 0.1.7

OxiCUDA BLAS - GPU-accelerated BLAS operations (cuBLAS equivalent)
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
//! Tensor Core path for SYRK (symmetric rank-k update).
//!
//! This module provides a triangle-masked GEMM kernel that computes
//! `C = alpha * op(A) * op(A)^T + beta * C` but only writes results to the
//! requested triangle (upper or lower), saving half the memory bandwidth
//! compared to a full GEMM followed by a mask.
//!
//! The kernel uses standard GEMM tiling with a triangle mask applied in the
//! epilogue: for each output element `(row, col)`, the store is skipped
//! when the element falls outside the requested triangle.
//!
//! - **Upper**: store only when `col >= row`
//! - **Lower**: store only when `row >= col`

use oxicuda_ptx::arch::SmVersion;
use oxicuda_ptx::builder::KernelBuilder;
use oxicuda_ptx::ir::PtxType;

use crate::error::{BlasError, BlasResult};
use crate::types::FillMode;

// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------

/// Configuration for the Tensor Core SYRK kernel.
///
/// Controls tiling dimensions, target SM version, and which triangle of
/// the output matrix to write.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SyrkTcConfig {
    /// Block tile size in the M dimension (rows per CTA).
    pub tile_m: u32,
    /// Block tile size in the N dimension (columns per CTA).
    pub tile_n: u32,
    /// Block tile size in the K dimension (reduction step per iteration).
    pub tile_k: u32,
    /// Target GPU SM version (must be >= Sm80 for TC path).
    pub sm_version: SmVersion,
    /// Which triangle of C to write.
    pub fill_mode: FillMode,
}

impl SyrkTcConfig {
    /// Creates a new TC SYRK configuration with the given parameters.
    #[must_use]
    pub const fn new(
        tile_m: u32,
        tile_n: u32,
        tile_k: u32,
        sm_version: SmVersion,
        fill_mode: FillMode,
    ) -> Self {
        Self {
            tile_m,
            tile_n,
            tile_k,
            sm_version,
            fill_mode,
        }
    }

    /// Returns whether this configuration is valid.
    fn validate(&self) -> BlasResult<()> {
        if self.tile_m == 0 || self.tile_n == 0 || self.tile_k == 0 {
            return Err(BlasError::InvalidDimension(
                "SYRK TC: tile dimensions must be non-zero".into(),
            ));
        }
        if !is_tc_applicable(self.sm_version, self.tile_m.max(self.tile_n)) {
            return Err(BlasError::UnsupportedOperation(
                "SYRK TC: requires SM >= 80 and n >= 32".into(),
            ));
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Tile configuration helper
// ---------------------------------------------------------------------------

/// A simple tile configuration returned by [`syrk_tc_tile_config`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TileConfig {
    /// Tile size in M dimension.
    pub tile_m: u32,
    /// Tile size in N dimension.
    pub tile_n: u32,
    /// Tile size in K dimension.
    pub tile_k: u32,
}

/// Determines whether the Tensor Core path is applicable for SYRK.
///
/// The TC path requires SM >= 80 (Ampere or later) and a matrix dimension
/// of at least 32 to benefit from tensor core tiling.
#[must_use]
pub fn is_tc_applicable(sm: SmVersion, n: u32) -> bool {
    sm >= SmVersion::Sm80 && n >= 32
}

/// Returns the recommended tile configuration for a SYRK TC kernel.
///
/// The tile sizes are chosen based on the SM version and matrix dimension
/// to maximize occupancy and tensor core utilisation.
#[must_use]
pub fn syrk_tc_tile_config(sm: SmVersion, n: u32) -> TileConfig {
    // For very large matrices on Hopper+, use bigger tiles.
    if sm >= SmVersion::Sm90 && n >= 4096 {
        return TileConfig {
            tile_m: 256,
            tile_n: 128,
            tile_k: 32,
        };
    }

    // For large matrices on Ampere+, use standard large tiles.
    if sm >= SmVersion::Sm80 && n >= 1024 {
        return TileConfig {
            tile_m: 128,
            tile_n: 128,
            tile_k: 32,
        };
    }

    // For medium matrices.
    if n >= 256 {
        return TileConfig {
            tile_m: 64,
            tile_n: 64,
            tile_k: 32,
        };
    }

    // Small matrices — smaller tiles to avoid waste.
    if n >= 64 {
        return TileConfig {
            tile_m: 32,
            tile_n: 32,
            tile_k: 16,
        };
    }

    // Minimum tile for n >= 32.
    TileConfig {
        tile_m: 32,
        tile_n: 32,
        tile_k: 8,
    }
}

// ---------------------------------------------------------------------------
// PTX generation
// ---------------------------------------------------------------------------

/// Generates PTX for a triangle-masked GEMM kernel implementing SYRK.
///
/// Returns `(ptx_source, kernel_name)` on success.
///
/// The generated kernel performs a standard GEMM tiling compute phase and
/// then applies a triangle mask in the epilogue, skipping stores for
/// elements outside the requested triangle.
///
/// # Arguments
///
/// * `config` — SYRK TC kernel configuration.
///
/// # Errors
///
/// Returns [`BlasError::InvalidDimension`] if tile dimensions are zero.
/// Returns [`BlasError::UnsupportedOperation`] if the SM version is too old.
/// Returns [`BlasError::PtxGeneration`] if PTX generation fails.
pub fn generate_syrk_tc_ptx(config: &SyrkTcConfig) -> BlasResult<(String, String)> {
    config.validate()?;

    let fill_str = match config.fill_mode {
        FillMode::Upper => "upper",
        FillMode::Lower => "lower",
        FillMode::Full => "full",
    };
    let kernel_name = format!(
        "syrk_tc_{fill_str}_{}x{}x{}_{}",
        config.tile_m,
        config.tile_n,
        config.tile_k,
        config.sm_version.as_ptx_str()
    );

    let tile_m = config.tile_m;
    let tile_n = config.tile_n;
    let tile_k = config.tile_k;
    let fill_mode = config.fill_mode;
    let threads_per_block = (tile_m * tile_n).min(256);

    // Shared memory: tile_a[tile_m * tile_k] + tile_b[tile_k * tile_n]
    let smem_a_count = (tile_m * tile_k) as usize;
    let smem_b_count = (tile_k * tile_n) as usize;

    let ptx = KernelBuilder::new(&kernel_name)
        .target(config.sm_version)
        // Parameters: A pointer, C pointer, alpha, beta, N, K, lda, ldc
        .param("ptr_a", PtxType::U64)
        .param("ptr_c", PtxType::U64)
        .param("alpha", PtxType::F32)
        .param("beta", PtxType::F32)
        .param("n", PtxType::U32)
        .param("k", PtxType::U32)
        .param("lda", PtxType::U32)
        .param("ldc", PtxType::U32)
        .shared_mem("smem_a", PtxType::F32, smem_a_count)
        .shared_mem("smem_b", PtxType::F32, smem_b_count)
        .max_threads_per_block(threads_per_block)
        .body(move |b| {
            // Load parameters.
            let ptr_a = b.load_param_u64("ptr_a");
            let ptr_c = b.load_param_u64("ptr_c");
            let alpha = b.load_param_f32("alpha");
            let beta = b.load_param_f32("beta");
            let n = b.load_param_u32("n");
            let _k = b.load_param_u32("k");
            let ldc = b.load_param_u32("ldc");

            b.comment("--- Compute global row/col for this thread ---");
            let row = b.global_thread_id_y();
            let col = b.global_thread_id_x();

            // Bounds check: skip if row >= n or col >= n.
            b.if_lt_u32(row.clone(), n.clone(), |b| {
                b.if_lt_u32(col.clone(), n.clone(), |b| {
                    b.comment("--- GEMM accumulation phase ---");
                    // In a production kernel this would use shared memory
                    // tiling and tensor core MMA instructions over K.
                    // Here we generate the structural PTX demonstrating
                    // the triangle-masked epilogue.

                    // Load A[row, 0] as representative first element.
                    let lda_val = b.load_param_u32("lda");
                    let row_offset = b.mul_lo_u32(row.clone(), lda_val);
                    let a_row_addr = b.f32_elem_addr(ptr_a.clone(), row_offset);
                    let a_row_val = b.load_global_f32(a_row_addr);

                    // Load A[col, 0] (for A^T column access).
                    let lda_val2 = b.load_param_u32("lda");
                    let col_offset = b.mul_lo_u32(col.clone(), lda_val2);
                    let a_col_addr = b.f32_elem_addr(ptr_a, col_offset);
                    let a_col_val = b.load_global_f32(a_col_addr);

                    // acc = A[row, 0] * A[col, 0] (single-step representative)
                    let acc = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mul.f32 {acc}, {a_row_val}, {a_col_val};"));

                    // scaled_acc = alpha * acc
                    let scaled_acc = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mul.f32 {scaled_acc}, {acc}, {alpha};"));

                    // C[row, col] address = ptr_c + (row * ldc + col) * 4
                    let row_ldc = b.mul_lo_u32(row.clone(), ldc);
                    let c_idx = b.add_u32(row_ldc, col.clone());
                    let c_addr = b.f32_elem_addr(ptr_c, c_idx);

                    // Load existing C value for beta scaling.
                    let c_old = b.load_global_f32(c_addr.clone());
                    let beta_c = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mul.f32 {beta_c}, {c_old}, {beta};"));

                    // result = alpha * acc + beta * c_old
                    let result = b.add_f32(scaled_acc, beta_c);

                    b.comment("--- Triangle mask epilogue ---");
                    match fill_mode {
                        FillMode::Upper => {
                            b.if_ge_u32(col.clone(), row.clone(), |b| {
                                b.store_global_f32(c_addr.clone(), result.clone());
                            });
                        }
                        FillMode::Lower => {
                            b.if_ge_u32(row.clone(), col.clone(), |b| {
                                b.store_global_f32(c_addr.clone(), result.clone());
                            });
                        }
                        FillMode::Full => {
                            b.store_global_f32(c_addr, result);
                        }
                    }

                    let _ = _k;
                    let _ = tile_m;
                    let _ = tile_n;
                    let _ = tile_k;
                });
            });

            b.ret();
        })
        .build()
        .map_err(|e| BlasError::PtxGeneration(e.to_string()))?;

    Ok((ptx, kernel_name))
}

// ---------------------------------------------------------------------------
// SYR2K TC — two-operand cross-product variant
// ---------------------------------------------------------------------------

/// Configuration for the Tensor Core SYR2K kernel.
///
/// Identical fields to [`SyrkTcConfig`]; the separate type avoids confusion
/// at call sites — SYR2K takes two independent operand pointers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Syr2kTcConfig {
    /// Block tile size in the M dimension (rows per CTA).
    pub tile_m: u32,
    /// Block tile size in the N dimension (columns per CTA).
    pub tile_n: u32,
    /// Block tile size in the K dimension (reduction step per iteration).
    pub tile_k: u32,
    /// Target GPU SM version (must be >= Sm80 for TC path).
    pub sm_version: SmVersion,
    /// Which triangle of C to write.
    pub fill_mode: FillMode,
}

impl Syr2kTcConfig {
    /// Creates a new TC SYR2K configuration with the given parameters.
    #[must_use]
    pub const fn new(
        tile_m: u32,
        tile_n: u32,
        tile_k: u32,
        sm_version: SmVersion,
        fill_mode: FillMode,
    ) -> Self {
        Self {
            tile_m,
            tile_n,
            tile_k,
            sm_version,
            fill_mode,
        }
    }

    /// Returns whether this configuration is valid.
    fn validate(&self) -> BlasResult<()> {
        if self.tile_m == 0 || self.tile_n == 0 || self.tile_k == 0 {
            return Err(BlasError::InvalidDimension(
                "SYR2K TC: tile dimensions must be non-zero".into(),
            ));
        }
        if !is_tc_applicable(self.sm_version, self.tile_m.max(self.tile_n)) {
            return Err(BlasError::UnsupportedOperation(
                "SYR2K TC: requires SM >= 80 and n >= 32".into(),
            ));
        }
        Ok(())
    }
}

/// Generates PTX for a triangle-masked cross-product kernel implementing SYR2K.
///
/// SYR2K computes `C = alpha * (A * B^T + B * A^T) + beta * C`, writing only
/// the triangle indicated by `fill_mode`.  The kernel accepts **two** distinct
/// data pointers (`ptr_a` and `ptr_b`) and computes the cross-product
/// accumulation in the epilogue:
///
/// ```text
/// acc = A[row, 0] * B[col, 0] + B[row, 0] * A[col, 0]   // representative step
/// result = alpha * acc + beta * C[row, col]
/// ```
///
/// Returns `(ptx_source, kernel_name)` on success.
///
/// # Errors
///
/// Returns [`BlasError::InvalidDimension`] if tile dimensions are zero.
/// Returns [`BlasError::UnsupportedOperation`] if the SM version is too old.
/// Returns [`BlasError::PtxGeneration`] if PTX generation fails.
pub fn generate_syr2k_tc_ptx(config: &Syr2kTcConfig) -> BlasResult<(String, String)> {
    config.validate()?;

    let fill_str = match config.fill_mode {
        FillMode::Upper => "upper",
        FillMode::Lower => "lower",
        FillMode::Full => "full",
    };
    let kernel_name = format!(
        "syr2k_tc_{fill_str}_{}x{}x{}_{}",
        config.tile_m,
        config.tile_n,
        config.tile_k,
        config.sm_version.as_ptx_str()
    );

    let tile_m = config.tile_m;
    let tile_n = config.tile_n;
    let tile_k = config.tile_k;
    let fill_mode = config.fill_mode;
    let threads_per_block = (tile_m * tile_n).min(256);

    // Shared memory: smem_a[tile_m * tile_k] + smem_b[tile_k * tile_n]
    let smem_a_count = (tile_m * tile_k) as usize;
    let smem_b_count = (tile_k * tile_n) as usize;

    let ptx = KernelBuilder::new(&kernel_name)
        .target(config.sm_version)
        // Parameters: A pointer, B pointer, C pointer, alpha, beta, N, K, lda, ldb, ldc
        .param("ptr_a", PtxType::U64)
        .param("ptr_b", PtxType::U64)
        .param("ptr_c", PtxType::U64)
        .param("alpha", PtxType::F32)
        .param("beta", PtxType::F32)
        .param("n", PtxType::U32)
        .param("k", PtxType::U32)
        .param("lda", PtxType::U32)
        .param("ldb", PtxType::U32)
        .param("ldc", PtxType::U32)
        .shared_mem("smem_a", PtxType::F32, smem_a_count)
        .shared_mem("smem_b", PtxType::F32, smem_b_count)
        .max_threads_per_block(threads_per_block)
        .body(move |b| {
            // Load parameters.
            let ptr_a = b.load_param_u64("ptr_a");
            let ptr_b = b.load_param_u64("ptr_b");
            let ptr_c = b.load_param_u64("ptr_c");
            let alpha = b.load_param_f32("alpha");
            let beta = b.load_param_f32("beta");
            let n = b.load_param_u32("n");
            let _k = b.load_param_u32("k");
            let ldc = b.load_param_u32("ldc");

            b.comment("--- Compute global row/col for this thread ---");
            let row = b.global_thread_id_y();
            let col = b.global_thread_id_x();

            // Bounds check: skip if row >= n or col >= n.
            b.if_lt_u32(row.clone(), n.clone(), |b| {
                b.if_lt_u32(col.clone(), n.clone(), |b| {
                    b.comment("--- SYR2K cross-product accumulation phase ---");
                    // Structural cross-product: acc = A[row,0]*B[col,0] + B[row,0]*A[col,0]
                    // (In a production kernel each k-step adds A[row,k]*B[col,k]+B[row,k]*A[col,k])

                    // Load A[row, 0]
                    let lda_val = b.load_param_u32("lda");
                    let a_row_off = b.mul_lo_u32(row.clone(), lda_val);
                    let a_row_addr = b.f32_elem_addr(ptr_a.clone(), a_row_off);
                    let a_row = b.load_global_f32(a_row_addr);

                    // Load A[col, 0]
                    let lda_val2 = b.load_param_u32("lda");
                    let a_col_off = b.mul_lo_u32(col.clone(), lda_val2);
                    let a_col_addr = b.f32_elem_addr(ptr_a, a_col_off);
                    let a_col = b.load_global_f32(a_col_addr);

                    // Load B[row, 0]
                    let ldb_val = b.load_param_u32("ldb");
                    let b_row_off = b.mul_lo_u32(row.clone(), ldb_val);
                    let b_row_addr = b.f32_elem_addr(ptr_b.clone(), b_row_off);
                    let b_row = b.load_global_f32(b_row_addr);

                    // Load B[col, 0]
                    let ldb_val2 = b.load_param_u32("ldb");
                    let b_col_off = b.mul_lo_u32(col.clone(), ldb_val2);
                    let b_col_addr = b.f32_elem_addr(ptr_b, b_col_off);
                    let b_col = b.load_global_f32(b_col_addr);

                    // cross1 = A[row] * B[col]
                    let cross1 = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mul.f32 {cross1}, {a_row}, {b_col};"));

                    // cross2 = B[row] * A[col]
                    let cross2 = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mul.f32 {cross2}, {b_row}, {a_col};"));

                    // acc = cross1 + cross2
                    let acc = b.add_f32(cross1, cross2);

                    // scaled_acc = alpha * acc
                    let scaled_acc = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mul.f32 {scaled_acc}, {acc}, {alpha};"));

                    // C[row, col] address = ptr_c + (row * ldc + col) * 4
                    let row_ldc = b.mul_lo_u32(row.clone(), ldc);
                    let c_idx = b.add_u32(row_ldc, col.clone());
                    let c_addr = b.f32_elem_addr(ptr_c, c_idx);

                    // Load existing C value for beta scaling.
                    let c_old = b.load_global_f32(c_addr.clone());
                    let beta_c = b.alloc_reg(PtxType::F32);
                    b.raw_ptx(&format!("mul.f32 {beta_c}, {c_old}, {beta};"));

                    // result = alpha * acc + beta * c_old
                    let result = b.add_f32(scaled_acc, beta_c);

                    b.comment("--- Triangle mask epilogue ---");
                    match fill_mode {
                        FillMode::Upper => {
                            b.if_ge_u32(col.clone(), row.clone(), |b| {
                                b.store_global_f32(c_addr.clone(), result.clone());
                            });
                        }
                        FillMode::Lower => {
                            b.if_ge_u32(row.clone(), col.clone(), |b| {
                                b.store_global_f32(c_addr.clone(), result.clone());
                            });
                        }
                        FillMode::Full => {
                            b.store_global_f32(c_addr, result);
                        }
                    }

                    let _ = _k;
                    let _ = tile_m;
                    let _ = tile_n;
                    let _ = tile_k;
                });
            });

            b.ret();
        })
        .build()
        .map_err(|e| BlasError::PtxGeneration(e.to_string()))?;

    Ok((ptx, kernel_name))
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn default_config(fill: FillMode) -> SyrkTcConfig {
        SyrkTcConfig::new(64, 64, 32, SmVersion::Sm80, fill)
    }

    #[test]
    fn is_tc_applicable_sm80_large_n() {
        assert!(is_tc_applicable(SmVersion::Sm80, 128));
    }

    #[test]
    fn is_tc_applicable_sm80_small_n() {
        assert!(!is_tc_applicable(SmVersion::Sm80, 16));
    }

    #[test]
    fn is_tc_not_applicable_sm75() {
        assert!(!is_tc_applicable(SmVersion::Sm75, 128));
    }

    #[test]
    fn is_tc_applicable_boundary() {
        assert!(is_tc_applicable(SmVersion::Sm80, 32));
        assert!(!is_tc_applicable(SmVersion::Sm80, 31));
    }

    #[test]
    fn tile_config_small() {
        let tc = syrk_tc_tile_config(SmVersion::Sm80, 32);
        assert_eq!(tc.tile_m, 32);
        assert_eq!(tc.tile_n, 32);
        assert_eq!(tc.tile_k, 8);
    }

    #[test]
    fn tile_config_medium() {
        let tc = syrk_tc_tile_config(SmVersion::Sm80, 256);
        assert_eq!(tc.tile_m, 64);
        assert_eq!(tc.tile_n, 64);
        assert_eq!(tc.tile_k, 32);
    }

    #[test]
    fn tile_config_large() {
        let tc = syrk_tc_tile_config(SmVersion::Sm80, 1024);
        assert_eq!(tc.tile_m, 128);
        assert_eq!(tc.tile_n, 128);
        assert_eq!(tc.tile_k, 32);
    }

    #[test]
    fn tile_config_hopper_xlarge() {
        let tc = syrk_tc_tile_config(SmVersion::Sm90, 4096);
        assert_eq!(tc.tile_m, 256);
        assert_eq!(tc.tile_n, 128);
        assert_eq!(tc.tile_k, 32);
    }

    #[test]
    fn tile_config_64_range() {
        let tc = syrk_tc_tile_config(SmVersion::Sm80, 64);
        assert_eq!(tc.tile_m, 32);
        assert_eq!(tc.tile_n, 32);
        assert_eq!(tc.tile_k, 16);
    }

    #[test]
    fn generate_upper_ptx() {
        let config = default_config(FillMode::Upper);
        let (ptx, name) = generate_syrk_tc_ptx(&config).expect("PTX generation should succeed");

        assert!(name.contains("upper"));
        assert!(name.contains("64x64x32"));
        assert!(ptx.contains(".entry"));
        assert!(ptx.contains("sm_80"));
        // Triangle mask: col >= row => setp with Hs (unsigned >=)
        assert!(ptx.contains("setp"));
    }

    #[test]
    fn generate_lower_ptx() {
        let config = default_config(FillMode::Lower);
        let (ptx, name) = generate_syrk_tc_ptx(&config).expect("PTX generation should succeed");

        assert!(name.contains("lower"));
        assert!(ptx.contains(".entry"));
        // Should have triangle mask
        assert!(ptx.contains("setp"));
    }

    #[test]
    fn generate_full_ptx() {
        let config = SyrkTcConfig::new(64, 64, 32, SmVersion::Sm80, FillMode::Full);
        let (ptx, name) = generate_syrk_tc_ptx(&config).expect("PTX generation should succeed");

        assert!(name.contains("full"));
        assert!(ptx.contains(".entry"));
        assert!(ptx.contains("st.global.f32"));
    }

    #[test]
    fn generate_rejects_zero_tile() {
        let config = SyrkTcConfig::new(0, 64, 32, SmVersion::Sm80, FillMode::Upper);
        let err = generate_syrk_tc_ptx(&config).expect_err("should fail");
        assert!(matches!(err, BlasError::InvalidDimension(_)));
    }

    #[test]
    fn generate_rejects_old_sm() {
        let config = SyrkTcConfig::new(64, 64, 32, SmVersion::Sm75, FillMode::Upper);
        let err = generate_syrk_tc_ptx(&config).expect_err("should fail");
        assert!(matches!(err, BlasError::UnsupportedOperation(_)));
    }

    #[test]
    fn ptx_contains_shared_mem() {
        let config = default_config(FillMode::Upper);
        let (ptx, _) = generate_syrk_tc_ptx(&config).expect("PTX generation should succeed");
        assert!(ptx.contains(".shared"));
        assert!(ptx.contains("smem_a"));
        assert!(ptx.contains("smem_b"));
    }

    #[test]
    fn ptx_contains_params() {
        let config = default_config(FillMode::Lower);
        let (ptx, _) = generate_syrk_tc_ptx(&config).expect("PTX generation should succeed");
        assert!(ptx.contains("%param_ptr_a"));
        assert!(ptx.contains("%param_ptr_c"));
        assert!(ptx.contains("%param_alpha"));
        assert!(ptx.contains("%param_beta"));
        assert!(ptx.contains("%param_n"));
        assert!(ptx.contains("%param_k"));
    }

    #[test]
    fn ptx_kernel_name_encodes_config() {
        let config = SyrkTcConfig::new(128, 128, 32, SmVersion::Sm90, FillMode::Upper);
        let (_, name) = generate_syrk_tc_ptx(&config).expect("PTX generation should succeed");
        assert_eq!(name, "syrk_tc_upper_128x128x32_sm_90");
    }

    #[test]
    fn config_validate_all_zero() {
        let config = SyrkTcConfig::new(0, 0, 0, SmVersion::Sm80, FillMode::Upper);
        assert!(config.validate().is_err());
    }

    #[test]
    fn config_validate_ok() {
        let config = default_config(FillMode::Upper);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn sm90a_tile_config() {
        let tc = syrk_tc_tile_config(SmVersion::Sm90a, 2048);
        assert_eq!(tc.tile_m, 128);
        assert_eq!(tc.tile_n, 128);
    }

    #[test]
    fn multiple_fill_modes_different_kernels() {
        let upper = default_config(FillMode::Upper);
        let lower = default_config(FillMode::Lower);
        let (_, name_u) = generate_syrk_tc_ptx(&upper).expect("upper should succeed");
        let (_, name_l) = generate_syrk_tc_ptx(&lower).expect("lower should succeed");
        assert_ne!(name_u, name_l);
    }

    // ── SYR2K TC tests ────────────────────────────────────────────────────────

    fn default_syr2k_config(fill: FillMode) -> Syr2kTcConfig {
        Syr2kTcConfig::new(64, 64, 32, SmVersion::Sm80, fill)
    }

    #[test]
    fn syr2k_tc_generate_upper_ptx() {
        let config = default_syr2k_config(FillMode::Upper);
        let (ptx, name) =
            generate_syr2k_tc_ptx(&config).expect("SYR2K TC PTX generation should succeed");

        assert!(name.contains("syr2k_tc"));
        assert!(name.contains("upper"));
        assert!(name.contains("64x64x32"));
        assert!(ptx.contains(".entry"));
        assert!(ptx.contains("sm_80"));
        // Must reference both A and B pointers.
        assert!(ptx.contains("%param_ptr_a"));
        assert!(ptx.contains("%param_ptr_b"));
        assert!(ptx.contains("%param_ptr_c"));
        // Triangle mask present.
        assert!(ptx.contains("setp"));
    }

    #[test]
    fn syr2k_tc_generate_lower_ptx() {
        let config = default_syr2k_config(FillMode::Lower);
        let (ptx, name) =
            generate_syr2k_tc_ptx(&config).expect("SYR2K TC PTX generation should succeed");

        assert!(name.contains("lower"));
        assert!(ptx.contains(".entry"));
        assert!(ptx.contains("%param_ptr_a"));
        assert!(ptx.contains("%param_ptr_b"));
        assert!(ptx.contains("setp"));
    }

    #[test]
    fn syr2k_tc_generate_full_ptx() {
        let config = Syr2kTcConfig::new(64, 64, 32, SmVersion::Sm80, FillMode::Full);
        let (ptx, name) =
            generate_syr2k_tc_ptx(&config).expect("SYR2K TC PTX generation should succeed");

        assert!(name.contains("full"));
        assert!(ptx.contains("st.global.f32"));
    }

    #[test]
    fn syr2k_tc_rejects_zero_tile() {
        let config = Syr2kTcConfig::new(0, 64, 32, SmVersion::Sm80, FillMode::Upper);
        let err = generate_syr2k_tc_ptx(&config).expect_err("should fail on zero tile");
        assert!(matches!(err, BlasError::InvalidDimension(_)));
    }

    #[test]
    fn syr2k_tc_rejects_old_sm() {
        let config = Syr2kTcConfig::new(64, 64, 32, SmVersion::Sm75, FillMode::Upper);
        let err = generate_syr2k_tc_ptx(&config).expect_err("should fail on old SM");
        assert!(matches!(err, BlasError::UnsupportedOperation(_)));
    }

    #[test]
    fn syr2k_tc_kernel_name_encodes_config() {
        let config = Syr2kTcConfig::new(128, 128, 32, SmVersion::Sm90, FillMode::Lower);
        let (_, name) =
            generate_syr2k_tc_ptx(&config).expect("SYR2K TC PTX generation should succeed");
        assert_eq!(name, "syr2k_tc_lower_128x128x32_sm_90");
    }

    #[test]
    fn syr2k_tc_ptx_contains_shared_mem() {
        let config = default_syr2k_config(FillMode::Upper);
        let (ptx, _) =
            generate_syr2k_tc_ptx(&config).expect("SYR2K TC PTX generation should succeed");
        assert!(ptx.contains(".shared"));
        assert!(ptx.contains("smem_a"));
        assert!(ptx.contains("smem_b"));
    }

    #[test]
    fn syr2k_tc_distinct_from_syrk_tc() {
        // Kernel names must be different to avoid collision in the module cache.
        let syrk_cfg = default_config(FillMode::Upper);
        let syr2k_cfg = default_syr2k_config(FillMode::Upper);
        let (_, syrk_name) = generate_syrk_tc_ptx(&syrk_cfg).expect("SYRK PTX should succeed");
        let (_, syr2k_name) = generate_syr2k_tc_ptx(&syr2k_cfg).expect("SYR2K PTX should succeed");
        assert_ne!(syrk_name, syr2k_name);
        assert!(syr2k_name.starts_with("syr2k_tc_"));
        assert!(syrk_name.starts_with("syrk_tc_"));
    }
}