tenferro-internal-cpu-kernels 0.9.0

Internal ordinary CPU kernel implementations for tenferro-cpu.
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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
use super::*;

/// The Rust scalar type behind a preset variant name a macro received.
macro_rules! preset_scalar {
    (F32) => {
        f32
    };
    (F64) => {
        f64
    };
    (I32) => {
        i32
    };
    (I64) => {
        i64
    };
    (Bool) => {
        bool
    };
    (C32) => {
        num_complex::Complex32
    };
    (C64) => {
        num_complex::Complex64
    };
}
mod static_replay;

#[test]
fn materialize_tensor_read_covers_all_owned_dtypes() {
    macro_rules! check {
        ($variant:ident, $ty:ty, $value:expr) => {{
            let source = Tensor::from_typed::<preset_scalar!($variant)>(
                TypedTensor::<$ty>::from_vec_col_major(vec![1], vec![$value]).unwrap(),
            );
            let mut buffers = BufferPool::new();
            let materialized = crate::materialize_tensor_read(
                &mut buffers,
                "test",
                TensorRead::from_tensor(&source),
            )
            .unwrap();
            assert_eq!(materialized.dtype(), source.dtype());
        }};
    }

    check!(F32, f32, 1.0);
    check!(F64, f64, 2.0);
    check!(I32, i32, 3);
    check!(I64, i64, 4);
    check!(Bool, bool, true);
    check!(
        C32,
        num_complex::Complex32,
        num_complex::Complex32::new(5.0, 1.0)
    );
    check!(
        C64,
        num_complex::Complex64,
        num_complex::Complex64::new(6.0, 2.0)
    );
}

#[test]
fn integer_multiplication_wraps_owned_view_and_scalar_broadcast_paths() {
    let mut buffers = BufferPool::new();
    macro_rules! check {
        ($ty:ty, $variant:ident) => {{
            let max: $ty = <$ty>::MAX;
            let expected: $ty = -2;
            let scalar = TypedTensor::<$ty>::from_vec_col_major(vec![], vec![max]).unwrap();
            let other = TypedTensor::<$ty>::from_vec_col_major(vec![], vec![2]).unwrap();
            let owned = mul_read_with_pool(
                &mut buffers,
                TensorRead::from_tensor(&Tensor::from_typed::<preset_scalar!($variant)>(
                    scalar.duplicate().unwrap(),
                )),
                TensorRead::from_tensor(&Tensor::from_typed::<preset_scalar!($variant)>(
                    other.duplicate().unwrap(),
                )),
                Exec::serial(),
            )
            .unwrap();
            assert_eq!(owned.as_slice::<$ty>().unwrap(), &[expected]);
            let view = mul_read_with_pool(
                &mut buffers,
                TensorRead::from_view(TensorView::$variant(scalar.as_view())),
                TensorRead::from_view(TensorView::$variant(other.as_view())),
                Exec::serial(),
            )
            .unwrap();
            assert_eq!(view.as_slice::<$ty>().unwrap(), &[expected]);
            let broadcast = broadcast_multiply_read_with_pool(
                &mut buffers,
                TensorRead::from_tensor(&Tensor::from_typed::<preset_scalar!($variant)>(
                    scalar.duplicate().unwrap(),
                )),
                &[],
                &[],
                TensorRead::from_tensor(&Tensor::from_typed::<preset_scalar!($variant)>(other)),
                &[],
                &[],
                Exec::serial(),
            )
            .unwrap()
            .unwrap();
            assert_eq!(broadcast.as_slice::<$ty>().unwrap(), &[expected]);

            let vector = TypedTensor::<$ty>::from_vec_col_major(vec![3], vec![2, 3, 4]).unwrap();
            let expected_vector = [
                max.wrapping_mul(2),
                max.wrapping_mul(3),
                max.wrapping_mul(4),
            ];
            let owned_scalar_vector = mul_read_with_pool(
                &mut buffers,
                TensorRead::from_tensor(&Tensor::from_typed::<preset_scalar!($variant)>(
                    scalar.duplicate().unwrap(),
                )),
                TensorRead::from_tensor(&Tensor::from_typed::<preset_scalar!($variant)>(
                    vector.duplicate().unwrap(),
                )),
                Exec::serial(),
            )
            .unwrap();
            assert_eq!(owned_scalar_vector.shape(), &[3]);
            assert_eq!(
                owned_scalar_vector.as_slice::<$ty>().unwrap(),
                &expected_vector
            );
            let owned_vector_scalar = mul_read_with_pool(
                &mut buffers,
                TensorRead::from_tensor(&Tensor::from_typed::<preset_scalar!($variant)>(
                    vector.duplicate().unwrap(),
                )),
                TensorRead::from_tensor(&Tensor::from_typed::<preset_scalar!($variant)>(
                    scalar.duplicate().unwrap(),
                )),
                Exec::serial(),
            )
            .unwrap();
            assert_eq!(owned_vector_scalar.shape(), &[3]);
            assert_eq!(
                owned_vector_scalar.as_slice::<$ty>().unwrap(),
                &expected_vector
            );

            let view_scalar_vector = mul_read_with_pool(
                &mut buffers,
                TensorRead::from_view(TensorView::$variant(scalar.as_view())),
                TensorRead::from_view(TensorView::$variant(vector.as_view())),
                Exec::serial(),
            )
            .unwrap();
            assert_eq!(view_scalar_vector.shape(), &[3]);
            assert_eq!(
                view_scalar_vector.as_slice::<$ty>().unwrap(),
                &expected_vector
            );
            let view_vector_scalar = mul_read_with_pool(
                &mut buffers,
                TensorRead::from_view(TensorView::$variant(vector.as_view())),
                TensorRead::from_view(TensorView::$variant(scalar.as_view())),
                Exec::serial(),
            )
            .unwrap();
            assert_eq!(view_vector_scalar.shape(), &[3]);
            assert_eq!(
                view_vector_scalar.as_slice::<$ty>().unwrap(),
                &expected_vector
            );

            let broadcast_scalar_vector = broadcast_multiply_read_with_pool(
                &mut buffers,
                TensorRead::from_tensor(&Tensor::from_typed::<preset_scalar!($variant)>(
                    scalar.duplicate().unwrap(),
                )),
                &[3],
                &[],
                TensorRead::from_tensor(&Tensor::from_typed::<preset_scalar!($variant)>(
                    vector.duplicate().unwrap(),
                )),
                &[3],
                &[0],
                Exec::serial(),
            )
            .unwrap()
            .unwrap();
            assert_eq!(broadcast_scalar_vector.shape(), &[3]);
            assert_eq!(
                broadcast_scalar_vector.as_slice::<$ty>().unwrap(),
                &expected_vector
            );
            let broadcast_vector_scalar = broadcast_multiply_read_with_pool(
                &mut buffers,
                TensorRead::from_tensor(&Tensor::from_typed::<preset_scalar!($variant)>(vector)),
                &[3],
                &[0],
                TensorRead::from_tensor(&Tensor::from_typed::<preset_scalar!($variant)>(scalar)),
                &[3],
                &[],
                Exec::serial(),
            )
            .unwrap()
            .unwrap();
            assert_eq!(broadcast_vector_scalar.shape(), &[3]);
            assert_eq!(
                broadcast_vector_scalar.as_slice::<$ty>().unwrap(),
                &expected_vector
            );
        }};
    }
    check!(i32, I32);
    check!(i64, I64);
}

#[test]
fn broadcast_multiply_rank_n_outer_product_matches_reference() {
    let mut buffers = BufferPool::default();
    let lhs_data = [1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0];
    let rhs_data = [7.0_f64, 8.0, 9.0, 10.0];
    let lhs = TypedTensorView::from_slice([2, 3], [1, 2], 0, &lhs_data).unwrap();
    let rhs = TypedTensorView::from_slice([2, 2], [1, 2], 0, &rhs_data).unwrap();

    let out = broadcast_multiply_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::F64(lhs)),
        &[2, 3, 2, 2],
        &[0, 1],
        TensorRead::from_view(TensorView::F64(rhs)),
        &[2, 3, 2, 2],
        &[2, 3],
        Exec::serial(),
    )
    .unwrap()
    .expect("f64 broadcast multiply is supported");

    assert_eq!(out.shape(), &[2, 3, 2, 2]);
    let expected: Vec<f64> = (0..2)
        .flat_map(|d| {
            (0..2).flat_map(move |c| {
                (0..3).flat_map(move |b| {
                    (0..2).map(move |a| lhs_data[a + 2 * b] * rhs_data[c + 2 * d])
                })
            })
        })
        .collect();
    assert_eq!(out.as_slice::<f64>().unwrap(), expected.as_slice());
}

#[test]
fn broadcast_multiply_shared_batch_outer_product_matches_reference() {
    let mut buffers = BufferPool::default();
    let lhs_data = [1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0];
    let rhs_data = [
        7.0_f64, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0,
    ];
    let lhs = TypedTensorView::from_slice([2, 3], [1, 2], 0, &lhs_data).unwrap();
    let rhs = TypedTensorView::from_slice([4, 3], [1, 4], 0, &rhs_data).unwrap();

    let out = broadcast_multiply_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::F64(lhs)),
        &[2, 4, 3],
        &[0, 2],
        TensorRead::from_view(TensorView::F64(rhs)),
        &[2, 4, 3],
        &[1, 2],
        Exec::serial(),
    )
    .unwrap()
    .expect("f64 broadcast multiply is supported");

    assert_eq!(out.shape(), &[2, 4, 3]);
    let expected: Vec<f64> = (0..3)
        .flat_map(|t| {
            (0..4).flat_map(move |o| (0..2).map(move |j| lhs_data[j + 2 * t] * rhs_data[o + 4 * t]))
        })
        .collect();
    assert_eq!(out.as_slice::<f64>().unwrap(), expected.as_slice());
}

#[test]
fn broadcast_multiply_fallback_handles_permuted_elementwise_without_materialization() {
    let mut buffers = BufferPool::default();
    let lhs_data: Vec<f64> = (0..24).map(|i| (i + 1) as f64).collect();
    let rhs_data: Vec<f64> = (0..24).map(|i| (100 + i) as f64).collect();
    let lhs = Tensor::from_typed::<f64>(
        TypedTensor::from_vec_col_major(vec![2, 3, 4], lhs_data.clone()).unwrap(),
    );
    let rhs = Tensor::from_typed::<f64>(
        TypedTensor::from_vec_col_major(vec![4, 2, 3], rhs_data.clone()).unwrap(),
    );

    let out = broadcast_multiply_read_with_pool(
        &mut buffers,
        TensorRead::from_tensor(&lhs),
        &[2, 3, 4],
        &[0, 1, 2],
        TensorRead::from_tensor(&rhs),
        &[2, 3, 4],
        &[2, 0, 1],
        Exec::serial(),
    )
    .unwrap()
    .expect("same-rank permuted elementwise multiply should use fallback broadcast mul");

    let expected: Vec<f64> = (0..4)
        .flat_map(|k| {
            let lhs_data = &lhs_data;
            let rhs_data = &rhs_data;
            (0..3).flat_map(move |j| {
                (0..2).map(move |i| {
                    let lhs_offset = i + 2 * j + 6 * k;
                    let rhs_offset = k + 4 * i + 8 * j;
                    lhs_data[lhs_offset] * rhs_data[rhs_offset]
                })
            })
        })
        .collect();

    assert_eq!(out.shape(), &[2, 3, 4]);
    assert_eq!(out.as_slice::<f64>().unwrap(), expected.as_slice());
}

#[test]
fn broadcast_multiply_handles_scalar_full_output_pairs() {
    let mut buffers = BufferPool::default();
    let scalar =
        Tensor::from_typed::<f64>(TypedTensor::from_vec_col_major(vec![], vec![2.0]).unwrap());
    let vector = Tensor::from_typed::<f64>(
        TypedTensor::from_vec_col_major(vec![3], vec![1.0, 2.0, 3.0]).unwrap(),
    );

    let lhs_scalar = broadcast_multiply_read_with_pool(
        &mut buffers,
        TensorRead::from_tensor(&scalar),
        &[3],
        &[],
        TensorRead::from_tensor(&vector),
        &[3],
        &[0],
        Exec::serial(),
    )
    .unwrap()
    .expect("scalar lhs broadcast multiply should materialize");
    assert_eq!(lhs_scalar.as_slice::<f64>().unwrap(), &[2.0, 4.0, 6.0]);

    let rhs_scalar = broadcast_multiply_read_with_pool(
        &mut buffers,
        TensorRead::from_tensor(&vector),
        &[3],
        &[0],
        TensorRead::from_tensor(&scalar),
        &[3],
        &[],
        Exec::serial(),
    )
    .unwrap()
    .expect("scalar rhs broadcast multiply should materialize");
    assert_eq!(rhs_scalar.as_slice::<f64>().unwrap(), &[2.0, 4.0, 6.0]);

    let other_scalar =
        Tensor::from_typed::<f64>(TypedTensor::from_vec_col_major(vec![], vec![3.0]).unwrap());
    let both_scalar = broadcast_multiply_read_with_pool(
        &mut buffers,
        TensorRead::from_tensor(&scalar),
        &[3],
        &[],
        TensorRead::from_tensor(&other_scalar),
        &[3],
        &[],
        Exec::serial(),
    )
    .unwrap()
    .expect("scalar-scalar broadcast multiply should materialize");
    assert_eq!(both_scalar.as_slice::<f64>().unwrap(), &[6.0, 6.0, 6.0]);

    let complex_scalar = Tensor::from_typed::<tenferro_tensor::Complex64>(
        TypedTensor::from_vec_col_major(vec![], vec![c64(0.5, -1.5)]).unwrap(),
    );
    let complex_vector = Tensor::from_typed::<tenferro_tensor::Complex64>(
        TypedTensor::from_vec_col_major(vec![2], vec![c64(1.0, 2.0), c64(-3.0, 0.5)]).unwrap(),
    );
    let complex_value = broadcast_multiply_value_with_pool(
        &mut buffers,
        TensorRead::from_tensor(&complex_scalar),
        &[2],
        &[],
        TensorRead::from_tensor(&complex_vector),
        &[2],
        &[0],
        Exec::serial(),
    )
    .unwrap()
    .expect("complex scalar broadcast multiply should materialize");
    let complex_tensor =
        crate::materialize_tensor_read(&mut buffers, "test", complex_value.tensor_read()).unwrap();
    let complex_data = complex_tensor.as_slice::<Complex<f64>>().unwrap();
    assert_c64_close(complex_data[0], c64(3.5, -0.5));
    assert_c64_close(complex_data[1], c64(-0.75, 4.75));
}

#[test]
fn lazy_outer_product_lhs_prefix_preserves_logical_output_order() {
    let mut buffers = BufferPool::default();
    let lhs_data = [1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0];
    let rhs_data = [10.0_f64, 20.0, 30.0, 40.0];
    let lhs = TypedTensorView::from_slice([2, 3], [3, 1], 0, &lhs_data).unwrap();
    let rhs = TypedTensorView::from_slice([4], [1], 0, &rhs_data).unwrap();

    let out = try_lazy_outer_product_with_pool(
        &mut buffers,
        &lhs,
        &[2, 3, 4],
        &[0, 1],
        &rhs,
        &[2, 3, 4],
        &[2],
        Exec::serial(),
    )
    .unwrap()
    .expect("non-canonical lhs physical order should use lazy outer-product output");

    assert_ne!(
        out.strides.as_slice(),
        col_major_strides(&[2, 3, 4]).unwrap().as_slice()
    );
    let value = TensorValue::from_parts(
        Tensor::from_typed::<f64>(out.base),
        vec![2, 3, 4],
        out.strides,
        0,
    )
    .unwrap();
    let tensor = crate::materialize_tensor_read(&mut buffers, "test", value.tensor_read()).unwrap();
    let expected: Vec<f64> = (0..4)
        .flat_map(|k| {
            (0..3).flat_map(move |j| (0..2).map(move |i| lhs_data[i * 3 + j] * rhs_data[k]))
        })
        .collect();
    assert_eq!(tensor.shape(), &[2, 3, 4]);
    assert_eq!(tensor.as_slice::<f64>().unwrap(), expected.as_slice());
}

#[test]
fn lazy_outer_product_rhs_prefix_preserves_logical_output_order() {
    let mut buffers = BufferPool::default();
    let lhs_data = [1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0];
    let rhs_data = [10.0_f64, 20.0, 30.0, 40.0];
    let lhs = TypedTensorView::from_slice([2, 3], [3, 1], 0, &lhs_data).unwrap();
    let rhs = TypedTensorView::from_slice([4], [1], 0, &rhs_data).unwrap();

    let out = try_lazy_outer_product_with_pool(
        &mut buffers,
        &lhs,
        &[4, 2, 3],
        &[1, 2],
        &rhs,
        &[4, 2, 3],
        &[0],
        Exec::serial(),
    )
    .unwrap()
    .expect("rhs-prefix output should still support lazy non-canonical lhs order");

    assert_ne!(
        out.strides.as_slice(),
        col_major_strides(&[4, 2, 3]).unwrap().as_slice()
    );
    let value = TensorValue::from_parts(
        Tensor::from_typed::<f64>(out.base),
        vec![4, 2, 3],
        out.strides,
        0,
    )
    .unwrap();
    let tensor = crate::materialize_tensor_read(&mut buffers, "test", value.tensor_read()).unwrap();
    let expected: Vec<f64> = (0..3)
        .flat_map(|j| {
            (0..2).flat_map(move |i| (0..4).map(move |k| rhs_data[k] * lhs_data[i * 3 + j]))
        })
        .collect();
    assert_eq!(tensor.shape(), &[4, 2, 3]);
    assert_eq!(tensor.as_slice::<f64>().unwrap(), expected.as_slice());
}

fn c32(real: f32, imag: f32) -> Complex<f32> {
    Complex::new(real, imag)
}

fn c64(real: f64, imag: f64) -> Complex<f64> {
    Complex::new(real, imag)
}

fn assert_c32_close(actual: Complex<f32>, expected: Complex<f32>) {
    assert!((actual.re - expected.re).abs() < 1.0e-5);
    assert!((actual.im - expected.im).abs() < 1.0e-5);
}

fn assert_c64_close(actual: Complex<f64>, expected: Complex<f64>) {
    assert!((actual.re - expected.re).abs() < 1.0e-12);
    assert!((actual.im - expected.im).abs() < 1.0e-12);
}

fn assert_shape_mismatch<T>(result: crate::Result<T>, op: &'static str) {
    assert!(matches!(
        result,
        Err(crate::Error::Validation { op: actual, .. }) if actual == op
    ));
}

fn assert_dtype_mismatch<T>(result: crate::Result<T>, op: &'static str) {
    assert!(matches!(
        result,
        Err(crate::Error::Validation { op: actual, .. }) if actual == op
    ));
}

fn assert_unsupported<T>(result: crate::Result<T>, op: &'static str) {
    assert!(matches!(
        result,
        Err(crate::Error::Unsupported { op: actual, .. }) if actual == op
    ));
}

fn assert_unsupported_contains<T>(result: crate::Result<T>, op: &'static str, expected: &str) {
    assert!(matches!(
        result,
        Err(crate::Error::Unsupported {
            op: actual,
            message,
        }) if actual == op && message.contains(expected)
    ));
}

#[test]
fn complex_ordered_ops_are_explicitly_rejected() {
    let lhs = Tensor::from_typed::<tenferro_tensor::Complex64>(
        TypedTensor::from_vec_col_major(vec![1], vec![c64(1.0, 0.0)]).unwrap(),
    );
    let rhs = Tensor::from_typed::<tenferro_tensor::Complex64>(
        TypedTensor::from_vec_col_major(vec![1], vec![c64(0.0, 1.0)]).unwrap(),
    );

    for (op, result) in [
        ("maximum", maximum(&lhs, &rhs)),
        ("minimum", minimum(&lhs, &rhs)),
        ("compare", compare(&lhs, &rhs, &CompareDir::Le)),
    ] {
        assert!(matches!(
            result,
            Err(crate::Error::Unsupported { op: actual, message })
                if actual == op && message.contains("total order")
        ));
    }

    assert!(matches!(
        clamp(&lhs, &rhs, &lhs),
        Err(crate::Error::Unsupported { op: "clamp", message })
            if message.contains("total order")
    ));
}

#[test]
fn typed_view_helpers_cover_scalar_and_validation_paths() {
    let mut buffers = BufferPool::default();
    let lhs = TypedTensor::<f64>::from_vec_col_major(vec![2], vec![1.0, 4.0]).unwrap();
    let rhs = TypedTensor::<f64>::from_vec_col_major(vec![2], vec![10.0, 20.0]).unwrap();
    let scalar = TypedTensor::<f64>::from_vec_col_major(vec![], vec![2.0]).unwrap();
    let short = TypedTensor::<f64>::from_vec_col_major(vec![1], vec![99.0]).unwrap();
    let pred = TypedTensor::<bool>::from_vec_col_major(vec![2], vec![true, false]).unwrap();
    let lower = TypedTensor::<f64>::from_vec_col_major(vec![2], vec![0.0, 2.0]).unwrap();
    let upper = TypedTensor::<f64>::from_vec_col_major(vec![2], vec![3.0, 5.0]).unwrap();

    let lhs_view = lhs.as_view();
    let rhs_view = rhs.as_view();
    let scalar_view = scalar.as_view();
    let short_view = short.as_view();
    let pred_view = pred.as_view();
    let lower_view = lower.as_view();
    let upper_view = upper.as_view();

    let same_shape = zip_with_pool(
        "test_binary",
        ErasedZipOp::Add,
        &mut buffers,
        &lhs_view,
        &rhs_view,
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(same_shape.as_slice().unwrap(), &[11.0, 24.0]);

    let scalar_lhs = zip_with_pool(
        "test_binary",
        ErasedZipOp::Multiply,
        &mut buffers,
        &scalar_view,
        &rhs_view,
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(scalar_lhs.as_slice().unwrap(), &[20.0, 40.0]);

    let scalar_rhs = zip_with_pool(
        "test_binary",
        ErasedZipOp::Multiply,
        &mut buffers,
        &lhs_view,
        &scalar_view,
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(scalar_rhs.as_slice().unwrap(), &[2.0, 8.0]);

    assert_shape_mismatch(
        zip_with_pool(
            "test_binary",
            ErasedZipOp::Add,
            &mut buffers,
            &lhs_view,
            &short_view,
            Exec::serial(),
        ),
        "test_binary",
    );

    let negated = map_with_pool::<f64, f64, _>(
        "test_unary",
        ErasedMapOp::Negate,
        &mut buffers,
        &lhs_view,
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(negated.as_slice().unwrap(), &[-1.0, -4.0]);

    let compared = typed_compare_with_pool(
        &mut buffers,
        &lhs_view,
        &rhs_view,
        &CompareDir::Lt,
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(compared.as_slice().unwrap(), &[true, true]);
    assert_shape_mismatch(
        typed_compare_with_pool(
            &mut buffers,
            &lhs_view,
            &short_view,
            &CompareDir::Lt,
            Exec::serial(),
        ),
        "compare",
    );

    let selected = typed_select_with_pool(
        &mut buffers,
        &pred_view,
        &lhs_view,
        &rhs_view,
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(selected.as_slice().unwrap(), &[1.0, 20.0]);
    assert_shape_mismatch(
        typed_select_with_pool(
            &mut buffers,
            &pred_view,
            &short_view,
            &rhs_view,
            Exec::serial(),
        ),
        "select",
    );
    assert_shape_mismatch(
        typed_select_with_pool(
            &mut buffers,
            &pred_view,
            &lhs_view,
            &short_view,
            Exec::serial(),
        ),
        "select",
    );

    let clamped = typed_clamp_with_pool(
        &mut buffers,
        &lhs_view,
        &lower_view,
        &upper_view,
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(clamped.as_slice().unwrap(), &[1.0, 4.0]);

    let degenerate_lower: TypedTensor<f64> =
        TypedTensor::from_vec_col_major(vec![2], vec![5.0_f64, 5.0]).unwrap();
    let degenerate_upper: TypedTensor<f64> =
        TypedTensor::from_vec_col_major(vec![2], vec![3.0_f64, 3.0]).unwrap();
    let degenerate = typed_clamp_with_pool(
        &mut buffers,
        &lhs_view,
        &degenerate_lower.as_view(),
        &degenerate_upper.as_view(),
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(degenerate.as_slice().unwrap(), &[3.0, 3.0]);

    assert_shape_mismatch(
        typed_clamp_with_pool(
            &mut buffers,
            &lhs_view,
            &short_view,
            &upper_view,
            Exec::serial(),
        ),
        "clamp",
    );
    assert_shape_mismatch(
        typed_clamp_with_pool(
            &mut buffers,
            &lhs_view,
            &lower_view,
            &short_view,
            Exec::serial(),
        ),
        "clamp",
    );
}

#[test]
fn read_as_cpu_view_covers_tensor_and_view_variants() {
    let f32_tensor = Tensor::from_typed::<f32>(
        TypedTensor::from_vec_col_major(vec![2], vec![1.0_f32, 2.0]).unwrap(),
    );
    let f64_tensor = Tensor::from_typed::<f64>(
        TypedTensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0]).unwrap(),
    );
    let i32_tensor = Tensor::from_typed::<i32>(
        TypedTensor::from_vec_col_major(vec![2], vec![1_i32, 2]).unwrap(),
    );
    let i64_tensor = Tensor::from_typed::<i64>(
        TypedTensor::from_vec_col_major(vec![2], vec![1_i64, 2]).unwrap(),
    );
    let bool_tensor = Tensor::from_typed::<bool>(
        TypedTensor::from_vec_col_major(vec![2], vec![true, false]).unwrap(),
    );
    let c32_tensor = Tensor::from_typed::<tenferro_tensor::Complex32>(
        TypedTensor::from_vec_col_major(vec![2], vec![c32(1.0, 0.0), c32(0.0, 1.0)]).unwrap(),
    );
    let c64_tensor = Tensor::from_typed::<tenferro_tensor::Complex64>(
        TypedTensor::from_vec_col_major(vec![2], vec![c64(1.0, 0.0), c64(0.0, 1.0)]).unwrap(),
    );

    match read_as_cpu_view(TensorRead::from_tensor(&f32_tensor))
        .expect("tests exercise preset scalars")
    {
        CpuReadView::F32(view) => assert_eq!(view.shape(), &[2]),
        _ => panic!("expected f32 tensor read view"),
    }
    match read_as_cpu_view(TensorRead::from_tensor(&f64_tensor))
        .expect("tests exercise preset scalars")
    {
        CpuReadView::F64(view) => assert_eq!(view.shape(), &[2]),
        _ => panic!("expected f64 tensor read view"),
    }
    match read_as_cpu_view(TensorRead::from_tensor(&i32_tensor))
        .expect("tests exercise preset scalars")
    {
        CpuReadView::I32(view) => assert_eq!(view.shape(), &[2]),
        _ => panic!("expected i32 tensor read view"),
    }
    match read_as_cpu_view(TensorRead::from_tensor(&i64_tensor))
        .expect("tests exercise preset scalars")
    {
        CpuReadView::I64(view) => assert_eq!(view.shape(), &[2]),
        _ => panic!("expected i64 tensor read view"),
    }
    match read_as_cpu_view(TensorRead::from_tensor(&bool_tensor))
        .expect("tests exercise preset scalars")
    {
        CpuReadView::Bool(view) => assert_eq!(view.shape(), &[2]),
        _ => panic!("expected bool tensor read view"),
    }
    match read_as_cpu_view(TensorRead::from_tensor(&c32_tensor))
        .expect("tests exercise preset scalars")
    {
        CpuReadView::C32(view) => assert_eq!(view.shape(), &[2]),
        _ => panic!("expected c32 tensor read view"),
    }
    match read_as_cpu_view(TensorRead::from_tensor(&c64_tensor))
        .expect("tests exercise preset scalars")
    {
        CpuReadView::C64(view) => assert_eq!(view.shape(), &[2]),
        _ => panic!("expected c64 tensor read view"),
    }

    let f32_view_source = TypedTensor::<f32>::from_vec_col_major(vec![1], vec![3.0]).unwrap();
    let f64_view_source = TypedTensor::<f64>::from_vec_col_major(vec![1], vec![3.0]).unwrap();
    let i32_view_source = TypedTensor::<i32>::from_vec_col_major(vec![1], vec![3]).unwrap();
    let i64_view_source = TypedTensor::<i64>::from_vec_col_major(vec![1], vec![3]).unwrap();
    let bool_view_source = TypedTensor::<bool>::from_vec_col_major(vec![1], vec![true]).unwrap();
    let c32_view_source =
        TypedTensor::<Complex<f32>>::from_vec_col_major(vec![1], vec![c32(3.0, 0.0)]).unwrap();
    let c64_view_source =
        TypedTensor::<Complex<f64>>::from_vec_col_major(vec![1], vec![c64(3.0, 0.0)]).unwrap();

    match read_as_cpu_view(TensorRead::from_view(TensorView::F32(
        f32_view_source.as_view(),
    )))
    .expect("tests exercise preset scalars")
    {
        CpuReadView::F32(view) => assert_eq!(view.shape(), &[1]),
        _ => panic!("expected f32 borrowed read view"),
    }
    match read_as_cpu_view(TensorRead::from_view(TensorView::F64(
        f64_view_source.as_view(),
    )))
    .expect("tests exercise preset scalars")
    {
        CpuReadView::F64(view) => assert_eq!(view.shape(), &[1]),
        _ => panic!("expected f64 borrowed read view"),
    }
    match read_as_cpu_view(TensorRead::from_view(TensorView::I32(
        i32_view_source.as_view(),
    )))
    .expect("tests exercise preset scalars")
    {
        CpuReadView::I32(view) => assert_eq!(view.shape(), &[1]),
        _ => panic!("expected i32 borrowed read view"),
    }
    match read_as_cpu_view(TensorRead::from_view(TensorView::I64(
        i64_view_source.as_view(),
    )))
    .expect("tests exercise preset scalars")
    {
        CpuReadView::I64(view) => assert_eq!(view.shape(), &[1]),
        _ => panic!("expected i64 borrowed read view"),
    }
    match read_as_cpu_view(TensorRead::from_view(TensorView::Bool(
        bool_view_source.as_view(),
    )))
    .expect("tests exercise preset scalars")
    {
        CpuReadView::Bool(view) => assert_eq!(view.shape(), &[1]),
        _ => panic!("expected bool borrowed read view"),
    }
    match read_as_cpu_view(TensorRead::from_view(TensorView::C32(
        c32_view_source.as_view(),
    )))
    .expect("tests exercise preset scalars")
    {
        CpuReadView::C32(view) => assert_eq!(view.shape(), &[1]),
        _ => panic!("expected c32 borrowed read view"),
    }
    match read_as_cpu_view(TensorRead::from_view(TensorView::C64(
        c64_view_source.as_view(),
    )))
    .expect("tests exercise preset scalars")
    {
        CpuReadView::C64(view) => assert_eq!(view.shape(), &[1]),
        _ => panic!("expected c64 borrowed read view"),
    }
}

#[test]
fn tensor_read_elementwise_dispatch_covers_view_and_complex_scalar_branches() {
    let mut buffers = BufferPool::default();

    let f32_a = TypedTensor::<f32>::from_vec_col_major(vec![2], vec![1.0, -2.0]).unwrap();
    let f32_b = TypedTensor::<f32>::from_vec_col_major(vec![2], vec![3.0, 4.0]).unwrap();
    let f32_scalar = TypedTensor::<f32>::from_vec_col_major(vec![], vec![2.0]).unwrap();
    let f64_a = TypedTensor::<f64>::from_vec_col_major(vec![3], vec![-3.0, 0.0, 4.0]).unwrap();
    let f64_b = TypedTensor::<f64>::from_vec_col_major(vec![3], vec![1.0, 2.0, 5.0]).unwrap();
    let f64_scalar = TypedTensor::<f64>::from_vec_col_major(vec![], vec![2.0]).unwrap();
    let i32_a = TypedTensor::<i32>::from_vec_col_major(vec![2], vec![1, 4]).unwrap();
    let i32_b = TypedTensor::<i32>::from_vec_col_major(vec![2], vec![2, 3]).unwrap();
    let i64_a = TypedTensor::<i64>::from_vec_col_major(vec![2], vec![5, -1]).unwrap();
    let i64_b = TypedTensor::<i64>::from_vec_col_major(vec![2], vec![2, -1]).unwrap();
    let bool_a = TypedTensor::<bool>::from_vec_col_major(vec![2], vec![true, false]).unwrap();
    let bool_b = TypedTensor::<bool>::from_vec_col_major(vec![2], vec![false, false]).unwrap();
    let pred = TypedTensor::<bool>::from_vec_col_major(vec![2], vec![true, false]).unwrap();
    let c32_a = TypedTensor::<Complex<f32>>::from_vec_col_major(
        vec![2],
        vec![c32(3.0, 4.0), c32(0.0, 2.0)],
    )
    .unwrap();
    let c32_b = TypedTensor::<Complex<f32>>::from_vec_col_major(
        vec![2],
        vec![c32(1.0, 0.0), c32(0.0, 2.0)],
    )
    .unwrap();
    let c64_a = TypedTensor::<Complex<f64>>::from_vec_col_major(
        vec![2],
        vec![c64(3.0, 4.0), c64(0.0, 2.0)],
    )
    .unwrap();
    let c64_b = TypedTensor::<Complex<f64>>::from_vec_col_major(
        vec![2],
        vec![c64(1.0, 0.0), c64(0.0, 2.0)],
    )
    .unwrap();

    let f32_b_tensor = Tensor::from_typed::<f32>(f32_b.duplicate().unwrap());
    let f64_b_tensor = Tensor::from_typed::<f64>(f64_b.duplicate().unwrap());
    let c32_b_tensor = Tensor::from_typed::<tenferro_tensor::Complex32>(c32_b.duplicate().unwrap());
    let c64_b_tensor = Tensor::from_typed::<tenferro_tensor::Complex64>(c64_b.duplicate().unwrap());
    let f32_scalar_tensor = Tensor::from_typed::<f32>(f32_scalar.duplicate().unwrap());
    let f64_scalar_tensor = Tensor::from_typed::<f64>(f64_scalar.duplicate().unwrap());

    let add_f32 = add_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::F32(f32_a.as_view())),
        TensorRead::from_tensor(&f32_b_tensor),
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(add_f32.as_slice::<f32>().unwrap(), &[4.0, 2.0]);

    let add_c32_scalar = add_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::F32(f32_scalar.as_view())),
        TensorRead::from_tensor(&c32_b_tensor),
        Exec::serial(),
    )
    .unwrap();
    assert_c32_close(
        add_c32_scalar.as_slice::<Complex<f32>>().unwrap()[0],
        c32(3.0, 0.0),
    );

    let add_c64_scalar = add_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::C64(c64_b.as_view())),
        TensorRead::from_tensor(&f64_scalar_tensor),
        Exec::serial(),
    )
    .unwrap();
    assert_c64_close(
        add_c64_scalar.as_slice::<Complex<f64>>().unwrap()[1],
        c64(2.0, 2.0),
    );

    let mul_f64 = mul_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::F64(f64_a.as_view())),
        TensorRead::from_tensor(&f64_b_tensor),
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(mul_f64.as_slice::<f64>().unwrap(), &[-3.0, 0.0, 20.0]);

    let mul_c32_scalar = mul_read_with_pool(
        &mut buffers,
        TensorRead::from_tensor(&c32_b_tensor),
        TensorRead::from_view(TensorView::F32(f32_scalar.as_view())),
        Exec::serial(),
    )
    .unwrap();
    assert_c32_close(
        mul_c32_scalar.as_slice::<Complex<f32>>().unwrap()[1],
        c32(0.0, 4.0),
    );

    assert_dtype_mismatch(
        mul_read_with_pool(
            &mut buffers,
            TensorRead::from_view(TensorView::F32(f32_a.as_view())),
            TensorRead::from_view(TensorView::F64(f64_a.as_view())),
            Exec::serial(),
        ),
        "mul",
    );

    let div_c32 = div_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::F32(f32_scalar.as_view())),
        TensorRead::from_view(TensorView::C32(c32_b.as_view())),
        Exec::serial(),
    )
    .unwrap();
    assert_c32_close(
        div_c32.as_slice::<Complex<f32>>().unwrap()[1],
        c32(0.0, -1.0),
    );

    let div_c64 = div_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::C64(c64_a.as_view())),
        TensorRead::from_view(TensorView::F64(f64_scalar.as_view())),
        Exec::serial(),
    )
    .unwrap();
    assert_c64_close(
        div_c64.as_slice::<Complex<f64>>().unwrap()[0],
        c64(1.5, 2.0),
    );

    let div_f32 = div_read_with_pool(
        &mut buffers,
        TensorRead::from_tensor(&f32_b_tensor),
        TensorRead::from_tensor(&f32_scalar_tensor),
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(div_f32.as_slice::<f32>().unwrap(), &[1.5, 2.0]);

    let div_f64 = div_read_with_pool(
        &mut buffers,
        TensorRead::from_tensor(&f64_b_tensor),
        TensorRead::from_view(TensorView::F64(f64_scalar.as_view())),
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(div_f64.as_slice::<f64>().unwrap(), &[0.5, 1.0, 2.5]);

    let neg = neg_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::C64(c64_a.as_view())),
        Exec::serial(),
    )
    .unwrap();
    assert_c64_close(neg.as_slice::<Complex<f64>>().unwrap()[0], c64(-3.0, -4.0));
    let neg_i32 = neg_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::I32(i32_a.as_view())),
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(neg_i32.as_slice::<i32>().unwrap(), &[-1, -4]);

    let conj = conj_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::C32(c32_a.as_view())),
        Exec::serial(),
    )
    .unwrap();
    assert_c32_close(conj.as_slice::<Complex<f32>>().unwrap()[0], c32(3.0, -4.0));
    assert_unsupported(
        conj_read_with_pool(
            &mut buffers,
            TensorRead::from_view(TensorView::Bool(bool_a.as_view())),
            Exec::serial(),
        ),
        "conj",
    );

    let abs = abs_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::C64(c64_a.as_view())),
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(abs.as_slice::<f64>().unwrap()[0], 5.0);

    let sign = sign_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::F64(f64_a.as_view())),
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(sign.as_slice::<f64>().unwrap(), &[-1.0, 0.0, 1.0]);

    let sign_complex = sign_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::C32(c32_a.as_view())),
        Exec::serial(),
    )
    .unwrap();
    assert_c32_close(
        sign_complex.as_slice::<Complex<f32>>().unwrap()[0],
        c32(0.6, 0.8),
    );

    // A magnitude whose square underflows must still produce a unit phase: the
    // modulus is computed with `hypot`, not by squaring the components.
    let tiny_complex =
        TypedTensor::<Complex<f64>>::from_vec_col_major(vec![1], vec![Complex::new(1.0e-200, 0.0)])
            .unwrap();
    let tiny_sign = sign_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::C64(tiny_complex.as_view())),
        Exec::serial(),
    )
    .unwrap();
    assert_c64_close(
        tiny_sign.as_slice::<Complex<f64>>().unwrap()[0],
        c64(1.0, 0.0),
    );
    let zero_complex =
        TypedTensor::<Complex<f64>>::from_vec_col_major(vec![1], vec![Complex::new(0.0, 0.0)])
            .unwrap();
    let zero_sign = sign_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::C64(zero_complex.as_view())),
        Exec::serial(),
    )
    .unwrap();
    assert_c64_close(
        zero_sign.as_slice::<Complex<f64>>().unwrap()[0],
        c64(0.0, 0.0),
    );

    assert_unsupported_contains(
        maximum_read_with_pool(
            &mut buffers,
            TensorRead::from_view(TensorView::C32(c32_a.as_view())),
            TensorRead::from_tensor(&c32_b_tensor),
            Exec::serial(),
        ),
        "maximum",
        "total order",
    );
    assert_unsupported_contains(
        minimum_read_with_pool(
            &mut buffers,
            TensorRead::from_view(TensorView::C64(c64_a.as_view())),
            TensorRead::from_tensor(&c64_b_tensor),
            Exec::serial(),
        ),
        "minimum",
        "total order",
    );

    let cmp_i32 = compare_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::I32(i32_a.as_view())),
        TensorRead::from_view(TensorView::I32(i32_b.as_view())),
        &CompareDir::Lt,
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(cmp_i32.as_slice::<bool>().unwrap(), &[true, false]);

    let cmp_i64 = compare_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::I64(i64_a.as_view())),
        TensorRead::from_view(TensorView::I64(i64_b.as_view())),
        &CompareDir::Ge,
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(cmp_i64.as_slice::<bool>().unwrap(), &[true, true]);

    let cmp_bool = compare_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::Bool(bool_a.as_view())),
        TensorRead::from_view(TensorView::Bool(bool_b.as_view())),
        &CompareDir::Eq,
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(cmp_bool.as_slice::<bool>().unwrap(), &[false, true]);

    assert_unsupported_contains(
        compare_read_with_pool(
            &mut buffers,
            TensorRead::from_view(TensorView::C32(c32_a.as_view())),
            TensorRead::from_view(TensorView::C32(c32_b.as_view())),
            &CompareDir::Gt,
            Exec::serial(),
        ),
        "compare",
        "total order",
    );

    let select_i64 = select_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::Bool(pred.as_view())),
        TensorRead::from_view(TensorView::I64(i64_a.as_view())),
        TensorRead::from_view(TensorView::I64(i64_b.as_view())),
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(select_i64.as_slice::<i64>().unwrap(), &[5, -1]);

    let select_bool = select_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::Bool(pred.as_view())),
        TensorRead::from_view(TensorView::Bool(bool_a.as_view())),
        TensorRead::from_view(TensorView::Bool(bool_b.as_view())),
        Exec::serial(),
    )
    .unwrap();
    assert_eq!(select_bool.as_slice::<bool>().unwrap(), &[true, false]);

    let select_c32 = select_read_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::Bool(pred.as_view())),
        TensorRead::from_view(TensorView::C32(c32_a.as_view())),
        TensorRead::from_view(TensorView::C32(c32_b.as_view())),
        Exec::serial(),
    )
    .unwrap();
    assert_c32_close(
        select_c32.as_slice::<Complex<f32>>().unwrap()[1],
        c32(0.0, 2.0),
    );

    assert_dtype_mismatch(
        select_read_with_pool(
            &mut buffers,
            TensorRead::from_view(TensorView::Bool(pred.as_view())),
            TensorRead::from_view(TensorView::F32(f32_a.as_view())),
            TensorRead::from_view(TensorView::F64(f64_a.as_view())),
            Exec::serial(),
        ),
        "select",
    );
    assert_dtype_mismatch(
        select_read_with_pool(
            &mut buffers,
            TensorRead::from_view(TensorView::F32(f32_a.as_view())),
            TensorRead::from_view(TensorView::F32(f32_a.as_view())),
            TensorRead::from_view(TensorView::F32(f32_b.as_view())),
            Exec::serial(),
        ),
        "select",
    );

    assert_unsupported_contains(
        clamp_read_with_pool(
            &mut buffers,
            TensorRead::from_view(TensorView::C32(c32_a.as_view())),
            TensorRead::from_view(TensorView::C32(c32_b.as_view())),
            TensorRead::from_view(TensorView::C32(c32_a.as_view())),
            Exec::serial(),
        ),
        "clamp",
        "total order",
    );
    assert_unsupported_contains(
        clamp_read_with_pool(
            &mut buffers,
            TensorRead::from_view(TensorView::C64(c64_a.as_view())),
            TensorRead::from_view(TensorView::C64(c64_b.as_view())),
            TensorRead::from_view(TensorView::C64(c64_a.as_view())),
            Exec::serial(),
        ),
        "clamp",
        "total order",
    );

    assert_dtype_mismatch(
        clamp_read_with_pool(
            &mut buffers,
            TensorRead::from_view(TensorView::F32(f32_a.as_view())),
            TensorRead::from_view(TensorView::F32(f32_b.as_view())),
            TensorRead::from_view(TensorView::F64(f64_b.as_view())),
            Exec::serial(),
        ),
        "clamp",
    );
}

#[test]
fn ordered_compare_fixed_dispatch_preserves_owned_and_view_semantics() {
    let lhs = Tensor::from_vec_col_major(vec![3], vec![1.0_f64, 2.0, f64::NAN]).unwrap();
    let rhs = Tensor::from_vec_col_major(vec![3], vec![1.0_f64, 3.0, 4.0]).unwrap();
    let Some(lhs_typed) = lhs.as_typed::<f64>() else {
        unreachable!("f64 fixture");
    };
    let Some(rhs_typed) = rhs.as_typed::<f64>() else {
        unreachable!("f64 fixture");
    };
    let cases = [
        (CompareDir::Eq, [true, false, false]),
        (CompareDir::Lt, [false, true, false]),
        (CompareDir::Le, [true, true, false]),
        (CompareDir::Gt, [false, false, false]),
        (CompareDir::Ge, [true, false, false]),
    ];
    let mut buffers = BufferPool::new();

    for (dir, expected) in cases {
        let owned = compare_with_pool(&mut buffers, &lhs, &rhs, &dir, Exec::serial()).unwrap();
        let borrowed = compare_read_with_pool(
            &mut buffers,
            TensorRead::from_view(TensorView::F64(lhs_typed.as_view())),
            TensorRead::from_view(TensorView::F64(rhs_typed.as_view())),
            &dir,
            Exec::serial(),
        )
        .unwrap();

        assert_eq!(owned.as_slice::<bool>().unwrap(), &expected);
        assert_eq!(borrowed.as_slice::<bool>().unwrap(), &expected);
    }
}

#[test]
fn ordered_compare_owned_and_read_routes_use_fixed_dispatch() {
    let source = include_str!("../elementwise.rs");
    let helper = source
        .split_once("fn typed_compare_with_pool")
        .and_then(|(_, suffix)| suffix.split_once("fn typed_select_with_pool"))
        .map(|(body, _)| body)
        .expect("fixed comparison helper must remain defined");
    assert!(
        helper.contains("erased_compare_into_uninit("),
        "fixed comparison helper must dispatch through strided erased_compare_into_uninit"
    );

    for (start, end, route) in [
        (
            "pub fn compare_with_pool(",
            "pub fn compare_read_with_pool(",
            "owned",
        ),
        (
            "pub fn compare_read_with_pool(",
            "/// Select values",
            "borrowed",
        ),
    ] {
        let body = source
            .split_once(start)
            .and_then(|(_, suffix)| suffix.split_once(end))
            .map(|(body, _)| body)
            .unwrap_or_else(|| panic!("{route} comparison route must remain defined"));
        assert!(
            body.matches("typed_compare_with_pool").count() >= 5,
            "{route} real, integer, and Bool comparisons must use fixed dispatch"
        );
    }
}

#[test]
fn broadcast_multiply_read_and_value_cover_dtypes_and_error_paths() {
    let mut buffers = BufferPool::default();
    let lhs_shape = [2, 3, 4];
    let lhs_dims = [0, 1];
    let rhs_dims = [2];

    let lhs_f32_data: Vec<f32> = (1..=6).map(|x| x as f32).collect();
    let rhs_f32_data = [10.0_f32, 20.0, 30.0, 40.0];
    let lhs_f32 = TypedTensorView::from_slice([2, 3], [3, 1], 0, &lhs_f32_data).unwrap();
    let rhs_f32 = TypedTensorView::from_slice([4], [1], 0, &rhs_f32_data).unwrap();
    let value_f32 = broadcast_multiply_value_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::F32(lhs_f32.clone())),
        &lhs_shape,
        &lhs_dims,
        TensorRead::from_view(TensorView::F32(rhs_f32)),
        &lhs_shape,
        &rhs_dims,
        Exec::serial(),
    )
    .unwrap()
    .expect("non-canonical f32 outer product should stay lazy");
    assert!(value_f32.is_view());
    assert_eq!(
        crate::materialize_tensor_read(&mut buffers, "test", value_f32.tensor_read())
            .unwrap()
            .shape(),
        &lhs_shape
    );

    let lhs_i32_data = [1_i32, 2, 3, 4, 5, 6];
    let rhs_i32_data = [2_i32, 3, 4, 5];
    let lhs_i32 = TypedTensorView::from_slice([2, 3], [3, 1], 0, &lhs_i32_data).unwrap();
    let rhs_i32 = TypedTensorView::from_slice([4], [1], 0, &rhs_i32_data).unwrap();
    let value_i32 = broadcast_multiply_value_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::I32(lhs_i32.clone())),
        &lhs_shape,
        &lhs_dims,
        TensorRead::from_view(TensorView::I32(rhs_i32)),
        &lhs_shape,
        &rhs_dims,
        Exec::serial(),
    )
    .unwrap()
    .expect("non-canonical i32 outer product should stay lazy");
    assert!(value_i32.is_view());

    let lhs_i64_data = [1_i64, 2, 3, 4, 5, 6];
    let rhs_i64_data = [2_i64, 3, 4, 5];
    let lhs_i64 = TypedTensorView::from_slice([2, 3], [3, 1], 0, &lhs_i64_data).unwrap();
    let rhs_i64 = TypedTensorView::from_slice([4], [1], 0, &rhs_i64_data).unwrap();
    let value_i64 = broadcast_multiply_value_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::I64(lhs_i64.clone())),
        &lhs_shape,
        &lhs_dims,
        TensorRead::from_view(TensorView::I64(rhs_i64)),
        &lhs_shape,
        &rhs_dims,
        Exec::serial(),
    )
    .unwrap()
    .expect("non-canonical i64 outer product should stay lazy");
    assert!(value_i64.is_view());

    let lhs_c32_data = [
        c32(1.0, 0.0),
        c32(2.0, 0.0),
        c32(3.0, 0.0),
        c32(4.0, 0.0),
        c32(5.0, 0.0),
        c32(6.0, 0.0),
    ];
    let rhs_c32_data = [c32(2.0, 0.0), c32(3.0, 0.0), c32(4.0, 0.0), c32(5.0, 0.0)];
    let lhs_c32 = TypedTensorView::from_slice([2, 3], [3, 1], 0, &lhs_c32_data).unwrap();
    let rhs_c32 = TypedTensorView::from_slice([4], [1], 0, &rhs_c32_data).unwrap();
    let value_c32 = broadcast_multiply_value_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::C32(lhs_c32.clone())),
        &lhs_shape,
        &lhs_dims,
        TensorRead::from_view(TensorView::C32(rhs_c32)),
        &lhs_shape,
        &rhs_dims,
        Exec::serial(),
    )
    .unwrap()
    .expect("non-canonical c32 outer product should stay lazy");
    assert!(value_c32.is_view());

    let lhs_c64_data = [
        c64(1.0, 0.0),
        c64(2.0, 0.0),
        c64(3.0, 0.0),
        c64(4.0, 0.0),
        c64(5.0, 0.0),
        c64(6.0, 0.0),
    ];
    let rhs_c64_data = [c64(2.0, 0.0), c64(3.0, 0.0), c64(4.0, 0.0), c64(5.0, 0.0)];
    let lhs_c64 = TypedTensorView::from_slice([2, 3], [3, 1], 0, &lhs_c64_data).unwrap();
    let rhs_c64 = TypedTensorView::from_slice([4], [1], 0, &rhs_c64_data).unwrap();
    let value_c64 = broadcast_multiply_value_with_pool(
        &mut buffers,
        TensorRead::from_view(TensorView::C64(lhs_c64.clone())),
        &lhs_shape,
        &lhs_dims,
        TensorRead::from_view(TensorView::C64(rhs_c64)),
        &lhs_shape,
        &rhs_dims,
        Exec::serial(),
    )
    .unwrap()
    .expect("non-canonical c64 outer product should stay lazy");
    assert!(value_c64.is_view());

    let same_shape_i64_lhs =
        Tensor::from_typed::<i64>(TypedTensor::from_vec_col_major(vec![2], vec![2, 3]).unwrap());
    let same_shape_i64_rhs =
        Tensor::from_typed::<i64>(TypedTensor::from_vec_col_major(vec![2], vec![4, 5]).unwrap());
    let materialized = broadcast_multiply_value_with_pool(
        &mut buffers,
        TensorRead::from_tensor(&same_shape_i64_lhs),
        &[2],
        &[0],
        TensorRead::from_tensor(&same_shape_i64_rhs),
        &[2],
        &[0],
        Exec::serial(),
    )
    .unwrap()
    .expect("same-shape multiply should materialize");
    assert!(materialized.as_tensor().is_some());
    assert_eq!(
        crate::materialize_tensor_read(&mut buffers, "test", materialized.tensor_read())
            .unwrap()
            .as_slice::<i64>()
            .unwrap(),
        &[8, 15]
    );

    let bool_lhs = Tensor::from_typed::<bool>(
        TypedTensor::from_vec_col_major(vec![2], vec![true, false]).unwrap(),
    );
    let bool_rhs = Tensor::from_typed::<bool>(
        TypedTensor::from_vec_col_major(vec![2], vec![true, true]).unwrap(),
    );
    assert!(broadcast_multiply_read_with_pool(
        &mut buffers,
        TensorRead::from_tensor(&bool_lhs),
        &[2],
        &[0],
        TensorRead::from_tensor(&bool_rhs),
        &[2],
        &[0],
        Exec::serial(),
    )
    .unwrap()
    .is_none());

    assert_shape_mismatch(
        typed_broadcast_mul_with_pool(
            &mut buffers,
            &lhs_f32,
            &[2, 3, 4],
            &[0, 1],
            &lhs_f32,
            &[2, 3, 5],
            &[0, 1],
            Exec::serial(),
        ),
        "broadcast_multiply",
    );
}

/// Exercises the erased elementwise tables through their public entry points, one case per preset
/// dtype, so the tag arms the shared dispatch introduced are covered rather than merely compiled.
///
/// Operations a dtype does not admit are called too: the refusal arm is part of the table, and the
/// test only asserts the dtype when the call succeeds.
#[test]
fn erased_tables_cover_every_preset_dtype() {
    macro_rules! cases {
        ($scalar:ty, $lhs:expr, $rhs:expr) => {{
            let make = |value: $scalar| {
                Tensor::from_typed(
                    TypedTensor::<$scalar>::from_vec_col_major(vec![1], vec![value]).unwrap(),
                )
            };
            let lhs = make($lhs);
            let rhs = make($rhs);
            // Arithmetic between one dtype and itself keeps that dtype.
            for (name, result) in [
                ("add", add(&lhs, &rhs)),
                ("sub", sub(&lhs, &rhs)),
                ("mul", mul(&lhs, &rhs)),
                ("div", div(&lhs, &rhs)),
                ("rem", rem(&lhs, &rhs)),
            ] {
                match result {
                    Ok(value) => assert_eq!(value.dtype(), lhs.dtype(), "{name} keeps the dtype"),
                    Err(error) => assert!(!error.to_string().is_empty(), "{name} refusal"),
                }
            }
            // Ordering and unary tables may widen: `abs` of a complex operand is its real magnitude.
            for (name, result) in [
                ("maximum", maximum(&lhs, &rhs)),
                ("minimum", minimum(&lhs, &rhs)),
            ] {
                if let Ok(value) = result {
                    assert!(!value.shape().is_empty(), "{name} result");
                }
            }
            for (name, result) in [
                ("neg", neg(&lhs)),
                ("conj", conj(&lhs)),
                ("abs", abs(&lhs)),
                ("sign", sign(&lhs)),
                ("compare", compare(&lhs, &rhs, &CompareDir::Lt)),
            ] {
                if let Ok(value) = result {
                    assert_eq!(value.shape().to_vec(), lhs.shape().to_vec(), "{name} shape");
                }
            }
        }};
    }

    cases!(f32, 3.0, 2.0);
    cases!(f64, 3.0, 2.0);
    cases!(i32, 3, 2);
    cases!(i64, 3, 2);
    cases!(bool, true, false);
    cases!(
        num_complex::Complex32,
        num_complex::Complex32::new(3.0, 0.0),
        num_complex::Complex32::new(2.0, 0.0)
    );
    cases!(
        num_complex::Complex64,
        num_complex::Complex64::new(3.0, 0.0),
        num_complex::Complex64::new(2.0, 0.0)
    );

    // The ternary table needs a boolean predicate and the operands it selects between.
    let pred =
        Tensor::from_typed(TypedTensor::<bool>::from_vec_col_major(vec![1], vec![true]).unwrap());
    let on_true =
        Tensor::from_typed(TypedTensor::<f64>::from_vec_col_major(vec![1], vec![1.0]).unwrap());
    let on_false =
        Tensor::from_typed(TypedTensor::<f64>::from_vec_col_major(vec![1], vec![2.0]).unwrap());
    let selected = select(&pred, &on_true, &on_false).expect("f64 select");
    assert_eq!(selected.dtype(), DType::F64);
}

/// Calls the tables with every ordered pair of preset dtypes, so the promotion arms and the refusal
/// arms execute rather than only the same-dtype arms the per-dtype test covers.
#[test]
fn erased_tables_cover_every_dtype_pair() {
    macro_rules! tensor {
        ($scalar:ty, $value:expr) => {
            Tensor::from_typed(
                TypedTensor::<$scalar>::from_vec_col_major(vec![2], vec![$value, $value]).unwrap(),
            )
        };
    }

    let scalars = vec![
        tensor!(f32, 3.0),
        tensor!(f64, 3.0),
        tensor!(i32, 3),
        tensor!(i64, 3),
        tensor!(bool, true),
        tensor!(
            num_complex::Complex32,
            num_complex::Complex32::new(3.0, 1.0)
        ),
        tensor!(
            num_complex::Complex64,
            num_complex::Complex64::new(3.0, 1.0)
        ),
    ];

    for lhs in &scalars {
        for rhs in &scalars {
            for (name, result) in [
                ("add", add(lhs, rhs)),
                ("sub", sub(lhs, rhs)),
                ("mul", mul(lhs, rhs)),
                ("div", div(lhs, rhs)),
                ("rem", rem(lhs, rhs)),
                ("maximum", maximum(lhs, rhs)),
                ("minimum", minimum(lhs, rhs)),
                ("compare", compare(lhs, rhs, &CompareDir::Lt)),
            ] {
                match result {
                    Ok(value) => assert_eq!(
                        value.shape().to_vec(),
                        lhs.shape().to_vec(),
                        "{name} on {:?}, {:?} keeps the shape",
                        lhs.dtype(),
                        rhs.dtype()
                    ),
                    Err(error) => assert!(
                        !error.to_string().is_empty(),
                        "{name} on {:?}, {:?} must refuse with a message",
                        lhs.dtype(),
                        rhs.dtype()
                    ),
                }
            }
            let selected = select(lhs, lhs, rhs);
            match selected {
                Ok(value) => assert_eq!(value.shape().to_vec(), lhs.shape().to_vec()),
                Err(error) => assert!(!error.to_string().is_empty()),
            }
        }
    }
}