vortex-tensor 0.72.0

Vortex tensor extension type
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! Inner product expression for tensor-like types.

use num_traits::Float;
use vortex_array::ArrayRef;
use vortex_array::ExecutionCtx;
use vortex_array::IntoArray;
use vortex_array::arrays::Constant;
use vortex_array::arrays::ConstantArray;
use vortex_array::arrays::Dict;
use vortex_array::arrays::Extension;
use vortex_array::arrays::ExtensionArray;
use vortex_array::arrays::FixedSizeList;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::arrays::ScalarFnArray;
use vortex_array::arrays::dict::DictArraySlotsExt;
use vortex_array::arrays::extension::ExtensionArrayExt;
use vortex_array::arrays::fixed_size_list::FixedSizeListArrayExt;
use vortex_array::arrays::scalar_fn::ExactScalarFn;
use vortex_array::arrays::scalar_fn::ScalarFnArrayView;
use vortex_array::arrays::scalar_fn::plugin::ScalarFnArrayParts;
use vortex_array::arrays::scalar_fn::plugin::ScalarFnArrayVTable;
use vortex_array::dtype::DType;
use vortex_array::dtype::NativePType;
use vortex_array::dtype::Nullability;
use vortex_array::dtype::PType;
use vortex_array::expr::Expression;
use vortex_array::expr::and;
use vortex_array::match_each_float_ptype;
use vortex_array::scalar_fn::Arity;
use vortex_array::scalar_fn::ChildName;
use vortex_array::scalar_fn::EmptyOptions;
use vortex_array::scalar_fn::ExecutionArgs;
use vortex_array::scalar_fn::ScalarFnId;
use vortex_array::scalar_fn::ScalarFnVTable;
use vortex_array::scalar_fn::TypedScalarFnInstance;
use vortex_array::serde::ArrayChildren;
use vortex_buffer::Buffer;
use vortex_buffer::BufferMut;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_session::VortexSession;

use crate::matcher::AnyTensor;
use crate::scalar_fns::l2_denorm::DenormOrientation;
use crate::scalar_fns::sorf_transform::SorfMatrix;
use crate::scalar_fns::sorf_transform::SorfTransform;
use crate::types::vector::Vector;
use crate::utils::BinaryTensorOpMetadata;
use crate::utils::extract_constant_flat_row;
use crate::utils::extract_flat_elements;
use crate::utils::extract_l2_denorm_children;
use crate::utils::validate_binary_tensor_float_inputs;

/// Inner product (dot product) between two columns.
///
/// Computes `sum(a_i * b_i)` over the flat backing buffer of each tensor or vector. For vectors
/// this is the standard dot product; for higher-rank ([`FixedShapeTensor`]) arrays this is the
/// Frobenius inner product.
///
/// Both inputs must be tensor-like extension arrays ([`FixedShapeTensor`] or [`Vector`]) with the
/// same dtype and a float element type. The output is a float column of the same float type.
///
/// [`FixedShapeTensor`]: crate::fixed_shape_tensor::FixedShapeTensor
/// [`Vector`]: crate::vector::Vector
#[derive(Clone)]
pub struct InnerProduct;

impl InnerProduct {
    /// Creates a new [`TypedScalarFnInstance`] wrapping the inner product operation.
    pub fn new() -> TypedScalarFnInstance<InnerProduct> {
        TypedScalarFnInstance::new(InnerProduct, EmptyOptions)
    }

    /// Constructs a [`ScalarFnArray`] that lazily computes the inner product between `lhs` and
    /// `rhs`.
    ///
    /// # Errors
    ///
    /// Returns an error if the [`ScalarFnArray`] cannot be constructed (e.g. due to dtype
    /// mismatches).
    pub fn try_new_array(lhs: ArrayRef, rhs: ArrayRef, len: usize) -> VortexResult<ScalarFnArray> {
        ScalarFnArray::try_new(InnerProduct::new().erased(), vec![lhs, rhs], len)
    }
}

impl ScalarFnVTable for InnerProduct {
    type Options = EmptyOptions;

    fn id(&self) -> ScalarFnId {
        ScalarFnId::new("vortex.tensor.inner_product")
    }

    fn arity(&self, _options: &Self::Options) -> Arity {
        Arity::Exact(2)
    }

    fn child_name(&self, _options: &Self::Options, child_idx: usize) -> ChildName {
        match child_idx {
            0 => ChildName::from("lhs"),
            1 => ChildName::from("rhs"),
            _ => unreachable!("InnerProduct must have exactly two children"),
        }
    }

    fn return_dtype(&self, _options: &Self::Options, arg_dtypes: &[DType]) -> VortexResult<DType> {
        let lhs = &arg_dtypes[0];
        let rhs = &arg_dtypes[1];

        // TODO(connor): relax the float-only gate once integer tensors are supported.
        let tensor_match = validate_binary_tensor_float_inputs(lhs, rhs)?;
        let ptype = tensor_match.element_ptype();
        let nullability = Nullability::from(lhs.is_nullable() || rhs.is_nullable());
        Ok(DType::Primitive(ptype, nullability))
    }

    fn execute(
        &self,
        _options: &Self::Options,
        args: &dyn ExecutionArgs,
        ctx: &mut ExecutionCtx,
    ) -> VortexResult<ArrayRef> {
        let lhs_ref = args.get(0)?;
        let rhs_ref = args.get(1)?;
        let len = args.row_count();

        // Take any L2Denorm-wrapped fast path that applies.
        match DenormOrientation::classify(&lhs_ref, &rhs_ref) {
            DenormOrientation::Both { lhs, rhs } => {
                return self.execute_both_denorm(lhs, rhs, len, ctx);
            }
            DenormOrientation::One { denorm, plain } => {
                return self.execute_one_denorm(denorm, plain, len, ctx);
            }
            DenormOrientation::Neither => {}
        }

        // Reduction case 1: `InnerProduct(SorfTransform(x), const)` rewrites to
        // `InnerProduct(x, forward_rotate(zero_pad(const)))`. Re-executes recursively so
        // case 2 can fire on the rewritten tree.
        if let Some(rewritten) = self.try_execute_sorf_constant(&lhs_ref, &rhs_ref, len, ctx)? {
            return Ok(rewritten);
        }

        // Reduction case 2: `InnerProduct(Vector[FSL(Dict(u8, f32))], const)` is computed by
        // gather-summing `q[j] * values[codes[j] as usize]` per row, reading the codebook
        // directly instead of decoding the column into dense vectors.
        if let Some(result) = self.try_execute_dict_constant(&lhs_ref, &rhs_ref, len, ctx)? {
            return Ok(result);
        }

        // Compute combined validity.
        let validity = lhs_ref.validity()?.and(rhs_ref.validity()?)?;

        // Canonicalize so we can perform the math directly.
        let lhs: ExtensionArray = lhs_ref.execute(ctx)?;
        let rhs: ExtensionArray = rhs_ref.execute(ctx)?;

        // We validated that both inputs have the same type.
        let ext = lhs.dtype().as_extension();
        let tensor_match = ext
            .metadata_opt::<AnyTensor>()
            .vortex_expect("we already validated this in `return_dtype`");
        let dimensions = tensor_match.list_size() as usize;

        // Extract the storage array from each extension input. We pass the storage (FSL) rather
        // than the extension array to avoid canonicalizing the extension wrapper.
        let lhs_storage = lhs.storage_array();
        let rhs_storage = rhs.storage_array();

        let lhs_flat = extract_flat_elements(lhs_storage, dimensions, ctx)?;
        let rhs_flat = extract_flat_elements(rhs_storage, dimensions, ctx)?;

        match_each_float_ptype!(lhs_flat.ptype(), |T| {
            let buffer: Buffer<T> = (0..len)
                .map(|i| inner_product_row(lhs_flat.row::<T>(i), rhs_flat.row::<T>(i)))
                .collect();

            // SAFETY: The buffer length equals `row_count`, which matches the source validity
            // length.
            Ok(unsafe { PrimitiveArray::new_unchecked(buffer, validity) }.into_array())
        })
    }

    fn validity(
        &self,
        _options: &Self::Options,
        expression: &Expression,
    ) -> VortexResult<Option<Expression>> {
        // The result is null if either input tensor is null.
        let lhs_validity = expression.child(0).validity()?;
        let rhs_validity = expression.child(1).validity()?;

        Ok(Some(and(lhs_validity, rhs_validity)))
    }

    fn is_null_sensitive(&self, _options: &Self::Options) -> bool {
        false
    }

    fn is_fallible(&self, _options: &Self::Options) -> bool {
        false
    }
}

impl ScalarFnArrayVTable for InnerProduct {
    fn serialize(
        &self,
        view: &ScalarFnArrayView<Self>,
        _session: &VortexSession,
    ) -> VortexResult<Option<Vec<u8>>> {
        Ok(Some(BinaryTensorOpMetadata::encode_from_view(view)?))
    }

    fn deserialize(
        &self,
        _dtype: &DType,
        len: usize,
        metadata: &[u8],
        children: &dyn ArrayChildren,
        session: &VortexSession,
    ) -> VortexResult<ScalarFnArrayParts<Self>> {
        let reconstructed =
            BinaryTensorOpMetadata::decode_children(metadata, len, children, session)?;
        Ok(ScalarFnArrayParts {
            options: EmptyOptions,
            children: reconstructed,
        })
    }
}

impl InnerProduct {
    /// Both sides are `L2Denorm`: `inner_product = s_l * s_r * dot(n_l, n_r)`.
    fn execute_both_denorm(
        &self,
        lhs_ref: &ArrayRef,
        rhs_ref: &ArrayRef,
        len: usize,
        ctx: &mut ExecutionCtx,
    ) -> VortexResult<ArrayRef> {
        let validity = lhs_ref.validity()?.and(rhs_ref.validity()?)?;

        let (normalized_l, norms_l) = extract_l2_denorm_children(lhs_ref);
        let (normalized_r, norms_r) = extract_l2_denorm_children(rhs_ref);

        let norms_l: PrimitiveArray = norms_l.execute(ctx)?;
        let norms_r: PrimitiveArray = norms_r.execute(ctx)?;

        let dot: PrimitiveArray = InnerProduct::try_new_array(normalized_l, normalized_r, len)?
            .into_array()
            .execute(ctx)?;

        match_each_float_ptype!(dot.ptype(), |T| {
            let dots = dot.as_slice::<T>();
            let nl = norms_l.as_slice::<T>();
            let nr = norms_r.as_slice::<T>();
            let buffer: Buffer<T> = (0..len).map(|i| nl[i] * nr[i] * dots[i]).collect();

            // SAFETY: The buffer length equals `len`, which matches the source validity length.
            Ok(unsafe { PrimitiveArray::new_unchecked(buffer, validity) }.into_array())
        })
    }

    /// One side is `L2Denorm`: `inner_product = s * dot(n, other)`.
    ///
    /// The caller must pass the denorm array as `denorm_ref` and the plain array as `plain_ref`.
    fn execute_one_denorm(
        &self,
        denorm_ref: &ArrayRef,
        plain_ref: &ArrayRef,
        len: usize,
        ctx: &mut ExecutionCtx,
    ) -> VortexResult<ArrayRef> {
        let validity = denorm_ref.validity()?.and(plain_ref.validity()?)?;

        let (normalized, norms) = extract_l2_denorm_children(denorm_ref);
        let denorm_norms: PrimitiveArray = norms.execute(ctx)?;

        let dot: PrimitiveArray = InnerProduct::try_new_array(normalized, plain_ref.clone(), len)?
            .into_array()
            .execute(ctx)?;

        match_each_float_ptype!(dot.ptype(), |T| {
            let dots = dot.as_slice::<T>();
            let ns = denorm_norms.as_slice::<T>();
            let buffer: Buffer<T> = (0..len).map(|i| ns[i] * dots[i]).collect();

            // SAFETY: The buffer length equals `len`, which matches the source validity length.
            Ok(unsafe { PrimitiveArray::new_unchecked(buffer, validity) }.into_array())
        })
    }

    /// Fast path when one side is `ExactScalarFn<SorfTransform>` and the other side is a
    /// constant-backed tensor-like extension. Rewrites to
    /// `InnerProduct(sorf_child, forward_rotate(zero_pad(const_query)))` because SORF is
    /// orthogonal, so `<T(R^{-1} x), c> = <x, R · zero_pad(c)>` where `T` is the truncation from
    /// `padded_dim` to `dim` applied by `SorfTransform` and `R` is the SORF forward matrix. See the
    /// proof in the crate-level docs and in the plan file.
    ///
    /// Returns `Ok(None)` if neither side matches, when the operand element type is not `F32`, or
    /// when the constant side is not a constant-backed tensor extension. The caller is expected to
    /// fall through to the standard path in that case.
    ///
    /// # F32-only
    ///
    /// TODO(connor): this rewrite is only sound for `PType::F32` because `SorfTransform` applies an
    /// `f32 -> element_ptype` cast at the end of its `execute`. For `F16`/`F64` the cast changes
    /// the inner product's rounding and the rewrite would not be semantically equivalent. Until we
    /// push the cast through `InnerProduct`, both the SorfTransform output ptype and the
    /// constant-side element ptype must be `F32` here.
    fn try_execute_sorf_constant(
        &self,
        lhs_ref: &ArrayRef,
        rhs_ref: &ArrayRef,
        len: usize,
        ctx: &mut ExecutionCtx,
    ) -> VortexResult<Option<ArrayRef>> {
        // Identify which side is the SorfTransform, if any.
        let (sorf_view, const_ref) =
            if let Some(view) = lhs_ref.as_opt::<ExactScalarFn<SorfTransform>>() {
                (view, rhs_ref)
            } else if let Some(view) = rhs_ref.as_opt::<ExactScalarFn<SorfTransform>>() {
                (view, lhs_ref)
            } else {
                return Ok(None);
            };

        if sorf_view.options.element_ptype != PType::F32 {
            return Ok(None);
        }

        // The other side must be a constant tensor.
        let Some(const_storage) = constant_tensor_storage(const_ref) else {
            return Ok(None);
        };

        let dim = sorf_view.options.dimensions as usize;
        let num_rounds = sorf_view.options.num_rounds as usize;
        let seed = sorf_view.options.seed;
        let padded_dim = dim.next_power_of_two();

        // Extract the single stored row of the constant.
        let flat = extract_constant_flat_row(&const_storage, ctx)?;
        if flat.ptype() != PType::F32 {
            return Ok(None);
        }

        // Zero-pad the query from `dim` to `padded_dim` and forward-rotate.
        let mut padded_query = vec![0.0f32; padded_dim];
        padded_query[..dim].copy_from_slice(flat.as_slice::<f32>());

        let rotation = SorfMatrix::try_new_padded(padded_dim, num_rounds, seed)?;
        let mut rotated_query = vec![0.0f32; padded_dim];
        rotation.rotate(&padded_query, &mut rotated_query);

        // Wrap the rotated query as a `Vector<padded_dim, f32>` constant broadcast to `len`
        // rows. The new extension dtype has `padded_dim` instead of `dim`, matching the
        // SorfTransform child we are about to dot it with.
        let new_constant = Vector::constant_array(&rotated_query, len)?;

        // Extract the SorfTransform child (the already-padded Vector<padded_dim, f32>).
        let sorf_child = sorf_view
            .nth_child(0)
            .vortex_expect("SorfTransform must have exactly one child");

        // Recursively execute the rewritten inner product. This allows case 2 to fire on
        // the rewritten tree if the sorf child is `Vector[FSL(Dict)]`. Termination is
        // guaranteed because the rewrite strictly removes a `SorfTransform` scalar-fn node
        // from the tree and SORFs cannot be nested.
        let rewritten = InnerProduct::try_new_array(sorf_child, new_constant, len)?
            .into_array()
            .execute(ctx)?;
        Ok(Some(rewritten))
    }

    /// Fast path when one side is an extension whose storage is `FSL(Dict(u8, f32))` and
    /// the other side is a constant-backed tensor extension with an F32 element ptype.
    ///
    /// Computes each row's inner product as
    ///   `out[i] = sum_{j in 0..padded_dim} q[j] * values[codes[i * padded_dim + j] as usize]`
    /// using a direct codebook lookup in the hot loop. An explicit product table
    /// `P[j, k] = q[j] * values[k]` (size `padded_dim * num_centroids * 4B`, ~1 MiB for the
    /// common 1024/256 case) was tried and measured ~10% *slower* on the
    /// `similarity_search` bench because the 1 KiB `values` table stays in L1 across all
    /// rows, while the 1 MiB product table does not.
    ///
    /// Returns `Ok(None)` when the pattern doesn't match; the caller should fall through to
    /// the standard path.
    fn try_execute_dict_constant(
        &self,
        lhs_ref: &ArrayRef,
        rhs_ref: &ArrayRef,
        len: usize,
        ctx: &mut ExecutionCtx,
    ) -> VortexResult<Option<ArrayRef>> {
        // Try each orientation. The oriented helper navigates each side exactly once, so
        // the only redundant work here is the failed navigation of the first side when the
        // dict happens to be on the right.
        if let Some(result) = self.try_execute_dict_constant_oriented(lhs_ref, rhs_ref, len, ctx)? {
            return Ok(Some(result));
        }
        self.try_execute_dict_constant_oriented(rhs_ref, lhs_ref, len, ctx)
    }

    /// Orientation-specific helper for [`Self::try_execute_dict_constant`]. `dict_candidate`
    /// is tried as `Extension[FSL[Dict]]`; `const_candidate` is tried as a constant-backed
    /// tensor extension. Returns `Ok(None)` if either navigation fails or any gate rejects.
    fn try_execute_dict_constant_oriented(
        &self,
        dict_candidate: &ArrayRef,
        const_candidate: &ArrayRef,
        len: usize,
        ctx: &mut ExecutionCtx,
    ) -> VortexResult<Option<ArrayRef>> {
        // Navigate the dict side.
        let Some(dict_ext) = dict_candidate.as_opt::<Extension>() else {
            return Ok(None);
        };
        let Some(fsl) = dict_ext.storage_array().as_opt::<FixedSizeList>() else {
            return Ok(None);
        };
        let Some(dict) = fsl.elements().as_opt::<Dict>() else {
            return Ok(None);
        };

        // Navigate the constant side and require its scalar be non-null.
        let Some(const_storage) = constant_tensor_storage(const_candidate) else {
            return Ok(None);
        };

        // Canonicalize codes and values. Codes may be e.g. BitPacked; executing is cheaper
        // than falling through to the standard path (which would also canonicalize).
        let codes_prim: PrimitiveArray = dict.codes().clone().execute(ctx)?;
        let values_prim: PrimitiveArray = dict.values().clone().execute(ctx)?;

        // Gate: u8 codes and f32 centroids.
        if codes_prim.ptype() != PType::U8 {
            // TODO(connor): Should we support wider codes?
            return Ok(None);
        }
        if values_prim.ptype() != PType::F32 {
            // TODO(connor): direct-lookup path only supports f32 centroids. SorfTransform
            // forces f32 anyway, so this is the only shape we need for now.
            return Ok(None);
        }

        let padded_dim = usize::try_from(fsl.list_size()).vortex_expect("fsl list_size fits usize");

        let flat = extract_constant_flat_row(&const_storage, ctx)?;
        if flat.ptype() != PType::F32 {
            // TODO(connor): case 2 is f32-only. For f16/f64 we fall through to the standard
            // path, which computes the inner product with the correct element type.
            return Ok(None);
        }

        // Combine the input validities up front; the per-row arithmetic may write garbage
        // into null rows but the validity mask hides it (matching the standard path).
        let validity = dict_candidate
            .validity()?
            .and(const_candidate.validity()?)?;

        // Fast path for the empty case: skip allocating and touching the codes buffer.
        if len == 0 {
            let empty = PrimitiveArray::empty::<f32>(validity.nullability());
            return Ok(Some(empty.into_array()));
        }

        let q: &[f32] = flat.as_slice::<f32>();
        debug_assert_eq!(q.len(), padded_dim);
        let codes: &[u8] = codes_prim.as_slice::<u8>();
        let values: &[f32] = values_prim.as_slice::<f32>();
        debug_assert_eq!(codes.len(), len * padded_dim);

        // The hot loop is extracted into [`execute_dict_constant_inner_product`] so the compiler
        // can prove the chunked indices stay in bounds and vectorize the inner gather-accumulate.
        let out = execute_dict_constant_inner_product(q, values, codes, len, padded_dim);

        // SAFETY: the buffer length equals `len`, which matches the validity length.
        let result = unsafe { PrimitiveArray::new_unchecked(out.freeze(), validity) }.into_array();
        Ok(Some(result))
    }
}

/// Return the storage constant for a canonical tensor-like constant query.
fn constant_tensor_storage(array: &ArrayRef) -> Option<ArrayRef> {
    let constant = array.as_opt::<Constant>()?;
    if constant.scalar().is_null() {
        return None;
    }
    let ext_scalar = constant.scalar().as_extension_opt()?;
    Some(ConstantArray::new(ext_scalar.to_storage_scalar(), array.len()).into_array())
}

/// Computes the inner product (dot product) of two equal-length float slices.
///
/// Returns `sum(a_i * b_i)`.
fn inner_product_row<T: Float + NativePType>(a: &[T], b: &[T]) -> T {
    a.iter()
        .zip(b.iter())
        .map(|(&x, &y)| x * y)
        .fold(T::zero(), |acc, v| acc + v)
}

/// Compute inner products between a constant query vector and dictionary-encoded rows.
///
/// For each row, computes `sum(q[j] * values[codes[row * dim + j]])` using the codebook `values`
/// directly instead of decoding the dictionary into dense vectors.
///
/// The inner loop uses `PARTIAL_SUMS` independent accumulators so the CPU can pipeline FP additions
/// instead of waiting for each `fadd` to retire before starting the next.
fn execute_dict_constant_inner_product(
    q: &[f32],
    values: &[f32],
    codes: &[u8],
    num_rows: usize,
    dim: usize,
) -> BufferMut<f32> {
    let mut out = BufferMut::<f32>::with_capacity(num_rows);

    const PARTIAL_SUMS: usize = 8;

    for row_codes in codes.chunks_exact(dim) {
        let mut acc = [0.0f32; PARTIAL_SUMS];

        let code_chunks = row_codes.chunks_exact(PARTIAL_SUMS);
        let q_chunks = q.chunks_exact(PARTIAL_SUMS);
        let code_rem = code_chunks.remainder();
        let q_rem = q_chunks.remainder();

        for (cc, qd) in code_chunks.zip(q_chunks) {
            for i in 0..PARTIAL_SUMS {
                acc[i] = qd[i].mul_add(values[cc[i] as usize], acc[i]);
            }
        }

        for (&code, &q_val) in code_rem.iter().zip(q_rem.iter()) {
            acc[0] = q_val.mul_add(values[code as usize], acc[0]);
        }

        // SAFETY: we reserved `num_rows` slots and push exactly once per row.
        unsafe { out.push_unchecked(acc.iter().sum::<f32>()) };
    }

    out
}

#[cfg(test)]
mod tests {

    use rstest::rstest;
    use vortex_array::ArrayPlugin;
    use vortex_array::ArrayRef;
    use vortex_array::IntoArray;
    use vortex_array::VortexSessionExecute;
    use vortex_array::arrays::MaskedArray;
    use vortex_array::arrays::PrimitiveArray;
    use vortex_array::arrays::ScalarFnArray;
    use vortex_array::arrays::scalar_fn::plugin::ScalarFnArrayPlugin;
    use vortex_array::validity::Validity;
    use vortex_error::VortexResult;

    use crate::scalar_fns::inner_product::InnerProduct;
    use crate::scalar_fns::l2_denorm::L2Denorm;
    use crate::tests::SESSION;
    use crate::utils::test_helpers::assert_close;
    use crate::utils::test_helpers::l2_denorm_array;
    use crate::utils::test_helpers::tensor_array;
    use crate::utils::test_helpers::vector_array;

    /// Evaluates inner product between two tensor arrays and returns the result as `Vec<f64>`.
    fn eval_inner_product(lhs: ArrayRef, rhs: ArrayRef, len: usize) -> VortexResult<Vec<f64>> {
        let scalar_fn = InnerProduct::new().erased();
        let result = ScalarFnArray::try_new(scalar_fn, vec![lhs, rhs], len)?;
        let mut ctx = SESSION.create_execution_ctx();
        let prim: PrimitiveArray = result.into_array().execute(&mut ctx)?;
        Ok(prim.as_slice::<f64>().to_vec())
    }

    /// Single-row inner product for various vector pairs.
    #[rstest]
    // Orthogonal: [1, 0] . [0, 1] = 0.
    #[case::orthogonal(&[2], &[1.0, 0.0], &[0.0, 1.0], &[0.0])]
    // Parallel: [3, 4] . [3, 4] = 9 + 16 = 25.
    #[case::parallel(&[2], &[3.0, 4.0], &[3.0, 4.0], &[25.0])]
    // Antiparallel: [1, 2] . [-1, -2] = -1 + -4 = -5.
    #[case::antiparallel(&[2], &[1.0, 2.0], &[-1.0, -2.0], &[-5.0])]
    // Scaled: [2, 0] . [3, 0] = 6.
    #[case::scaled(&[2], &[2.0, 0.0], &[3.0, 0.0], &[6.0])]
    fn single_row(
        #[case] shape: &[usize],
        #[case] lhs_elems: &[f64],
        #[case] rhs_elems: &[f64],
        #[case] expected: &[f64],
    ) -> VortexResult<()> {
        let lhs = tensor_array(shape, lhs_elems)?;
        let rhs = tensor_array(shape, rhs_elems)?;
        assert_close(&eval_inner_product(lhs, rhs, 1)?, expected);
        Ok(())
    }

    #[test]
    fn multiple_rows() -> VortexResult<()> {
        let lhs = tensor_array(
            &[3],
            &[
                1.0, 0.0, 0.0, // tensor 0
                3.0, 4.0, 0.0, // tensor 1
                1.0, 1.0, 1.0, // tensor 2
            ],
        )?;
        let rhs = tensor_array(
            &[3],
            &[
                0.0, 1.0, 0.0, // tensor 0: dot = 0
                3.0, 4.0, 0.0, // tensor 1: dot = 25
                2.0, 2.0, 2.0, // tensor 2: dot = 6
            ],
        )?;
        assert_close(&eval_inner_product(lhs, rhs, 3)?, &[0.0, 25.0, 6.0]);
        Ok(())
    }

    #[test]
    fn vector_inner_product() -> VortexResult<()> {
        let lhs = vector_array(
            2,
            &[
                3.0, 4.0, // vector 0
                1.0, 0.0, // vector 1
            ],
        )?;
        let rhs = vector_array(
            2,
            &[
                3.0, 4.0, // vector 0: dot = 25
                0.0, 1.0, // vector 1: dot = 0
            ],
        )?;
        assert_close(&eval_inner_product(lhs, rhs, 2)?, &[25.0, 0.0]);
        Ok(())
    }

    #[test]
    fn null_input_row() -> VortexResult<()> {
        // 3 rows of dim-2 vectors. Row 1 of lhs is masked as null.
        let lhs = tensor_array(&[2], &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0])?;
        let rhs = tensor_array(&[2], &[7.0, 8.0, 9.0, 10.0, 11.0, 12.0])?;
        let lhs = MaskedArray::try_new(lhs, Validity::from_iter([true, false, true]))?.into_array();

        let scalar_fn = InnerProduct::new().erased();
        let result = ScalarFnArray::try_new(scalar_fn, vec![lhs, rhs], 3)?;
        let mut ctx = SESSION.create_execution_ctx();
        let prim: PrimitiveArray = result.into_array().execute(&mut ctx)?;

        // Row 0: 1*7 + 2*8 = 23, row 1: null, row 2: 5*11 + 6*12 = 127.
        assert!(prim.is_valid(0, &mut ctx)?);
        assert!(!prim.is_valid(1, &mut ctx)?);
        assert!(prim.is_valid(2, &mut ctx)?);
        assert_close(&[prim.as_slice::<f64>()[0]], &[23.0]);
        assert_close(&[prim.as_slice::<f64>()[2]], &[127.0]);
        Ok(())
    }

    #[test]
    fn rejects_non_extension_dtype() {
        let lhs = PrimitiveArray::from_iter([1.0_f64, 2.0]).into_array();
        let rhs = PrimitiveArray::from_iter([3.0_f64, 4.0]).into_array();
        let result = InnerProduct::try_new_array(lhs, rhs, 2);
        assert!(result.is_err());
    }

    #[test]
    fn rejects_mismatched_dtypes() -> VortexResult<()> {
        let lhs = tensor_array(&[2], &[1.0_f64, 2.0])?;
        let rhs = vector_array(2, &[3.0_f64, 4.0])?;
        let result = InnerProduct::try_new_array(lhs, rhs, 1);
        assert!(result.is_err());
        Ok(())
    }

    #[test]
    fn both_denorm() -> VortexResult<()> {
        // LHS: [3.0, 4.0] = L2Denorm([0.6, 0.8], 5.0).
        // RHS: [1.0, 0.0] = L2Denorm([1.0, 0.0], 1.0).
        // dot([3.0, 4.0], [1.0, 0.0]) = 3.0.
        let mut ctx = SESSION.create_execution_ctx();
        let lhs = l2_denorm_array(&[2], &[0.6, 0.8], &[5.0], &mut ctx)?;
        let rhs = l2_denorm_array(&[2], &[1.0, 0.0], &[1.0], &mut ctx)?;

        // Expected: 5.0 * 1.0 * dot([0.6, 0.8], [1.0, 0.0]) = 5.0 * 0.6 = 3.0.
        assert_close(&eval_inner_product(lhs, rhs, 1)?, &[3.0]);
        Ok(())
    }

    #[test]
    fn both_denorm_multiple_rows() -> VortexResult<()> {
        // Row 0: [3.0, 4.0] dot [3.0, 4.0] = 25.0.
        // Row 1: [1.0, 0.0] dot [0.0, 1.0] = 0.0.
        let mut ctx = SESSION.create_execution_ctx();
        let lhs = l2_denorm_array(&[2], &[0.6, 0.8, 1.0, 0.0], &[5.0, 1.0], &mut ctx)?;
        let rhs = l2_denorm_array(&[2], &[0.6, 0.8, 0.0, 1.0], &[5.0, 1.0], &mut ctx)?;

        assert_close(&eval_inner_product(lhs, rhs, 2)?, &[25.0, 0.0]);
        Ok(())
    }

    #[test]
    fn one_side_denorm_lhs() -> VortexResult<()> {
        // LHS: L2Denorm([0.6, 0.8], 5.0) representing [3.0, 4.0].
        // RHS: plain [1.0, 2.0].
        // dot([3.0, 4.0], [1.0, 2.0]) = 3.0 + 8.0 = 11.0.
        let mut ctx = SESSION.create_execution_ctx();
        let lhs = l2_denorm_array(&[2], &[0.6, 0.8], &[5.0], &mut ctx)?;
        let rhs = tensor_array(&[2], &[1.0, 2.0])?;

        assert_close(&eval_inner_product(lhs, rhs, 1)?, &[11.0]);
        Ok(())
    }

    #[test]
    fn one_side_denorm_rhs() -> VortexResult<()> {
        // LHS: plain [1.0, 2.0].
        // RHS: L2Denorm([0.6, 0.8], 5.0) representing [3.0, 4.0].
        // dot([1.0, 2.0], [3.0, 4.0]) = 3.0 + 8.0 = 11.0.
        let mut ctx = SESSION.create_execution_ctx();
        let lhs = tensor_array(&[2], &[1.0, 2.0])?;
        let rhs = l2_denorm_array(&[2], &[0.6, 0.8], &[5.0], &mut ctx)?;

        assert_close(&eval_inner_product(lhs, rhs, 1)?, &[11.0]);
        Ok(())
    }

    #[test]
    fn both_denorm_null_norms() -> VortexResult<()> {
        // Row 0: valid, row 1: null (via nullable norms on lhs).
        let normalized_l = tensor_array(&[2], &[0.6, 0.8, 1.0, 0.0])?;
        let norms_l = PrimitiveArray::from_option_iter([Some(5.0f64), None]).into_array();
        let mut ctx = SESSION.create_execution_ctx();

        let lhs = L2Denorm::try_new_array(normalized_l, norms_l, 2, &mut ctx)?.into_array();
        let rhs = l2_denorm_array(&[2], &[0.6, 0.8, 1.0, 0.0], &[5.0, 1.0], &mut ctx)?;

        let scalar_fn = InnerProduct::new().erased();
        let result = ScalarFnArray::try_new(scalar_fn, vec![lhs, rhs], 2)?;
        let prim: PrimitiveArray = result.into_array().execute(&mut ctx)?;

        // Row 0: 5.0 * 5.0 * dot([0.6, 0.8], [0.6, 0.8]) = 25.0, row 1: null.
        assert!(prim.is_valid(0, &mut ctx)?);
        assert!(!prim.is_valid(1, &mut ctx)?);
        assert_close(&[prim.as_slice::<f64>()[0]], &[25.0]);
        Ok(())
    }

    #[rstest]
    #[case::vector(inner_product_vector_lhs(), inner_product_vector_rhs(), 2)]
    #[case::fixed_shape_tensor(inner_product_tensor_lhs(), inner_product_tensor_rhs(), 2)]
    fn serde_round_trip(
        #[case] lhs: ArrayRef,
        #[case] rhs: ArrayRef,
        #[case] len: usize,
    ) -> VortexResult<()> {
        let original = InnerProduct::try_new_array(lhs.clone(), rhs.clone(), len)?.into_array();

        let plugin = ScalarFnArrayPlugin::new(InnerProduct);
        let metadata = plugin
            .serialize(&original, &SESSION)?
            .expect("InnerProduct serialize must produce metadata");

        let children = vec![lhs, rhs];
        let recovered = plugin.deserialize(
            original.dtype(),
            original.len(),
            &metadata,
            &[],
            &children,
            &SESSION,
        )?;

        assert_eq!(recovered.dtype(), original.dtype());
        assert_eq!(recovered.len(), original.len());
        assert_eq!(recovered.encoding_id(), original.encoding_id());
        Ok(())
    }

    fn inner_product_vector_lhs() -> ArrayRef {
        vector_array(3, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).expect("valid vector array")
    }

    fn inner_product_vector_rhs() -> ArrayRef {
        vector_array(3, &[7.0, 8.0, 9.0, 10.0, 11.0, 12.0]).expect("valid vector array")
    }

    fn inner_product_tensor_lhs() -> ArrayRef {
        tensor_array(&[2], &[1.0, 2.0, 3.0, 4.0]).expect("valid tensor array")
    }

    fn inner_product_tensor_rhs() -> ArrayRef {
        tensor_array(&[2], &[5.0, 6.0, 7.0, 8.0]).expect("valid tensor array")
    }

    // ---- Tests for the `SorfTransform + constant` and `Dict + constant` fast paths ----

    #[allow(
        clippy::cast_possible_truncation,
        reason = "tests build small fixtures with deterministic in-range indices"
    )]
    mod constant_query_optimizations {
        use rstest::rstest;
        use vortex_array::ArrayRef;
        use vortex_array::IntoArray;
        use vortex_array::VortexSessionExecute;
        use vortex_array::arrays::Constant;
        use vortex_array::arrays::FixedSizeListArray;
        use vortex_array::arrays::PrimitiveArray;
        use vortex_array::arrays::ScalarFnArray;
        use vortex_array::arrays::dict::DictArray;
        use vortex_array::dtype::DType;
        use vortex_array::dtype::Nullability;
        use vortex_array::dtype::PType;
        use vortex_array::validity::Validity;
        use vortex_buffer::Buffer;
        use vortex_error::VortexResult;

        use crate::scalar_fns::inner_product::InnerProduct;
        use crate::scalar_fns::inner_product::constant_tensor_storage;
        use crate::scalar_fns::sorf_transform::SorfMatrix;
        use crate::scalar_fns::sorf_transform::SorfOptions;
        use crate::scalar_fns::sorf_transform::SorfTransform;
        use crate::tests::SESSION;
        use crate::types::vector::Vector;
        use crate::utils::extract_flat_elements;
        use crate::utils::test_helpers::literal_vector_array;
        use crate::utils::test_helpers::vector_array;

        /// Build a `Vector<list_size, f32>` whose storage is `FSL(DictArray(codes: u8, values:
        /// f32))`. This mirrors the shape that TurboQuant produces as the SorfTransform child.
        fn dict_vector_f32(list_size: u32, codes: &[u8], values: &[f32]) -> VortexResult<ArrayRef> {
            let num_rows = codes.len() / list_size as usize;
            let codes_arr =
                PrimitiveArray::new::<u8>(Buffer::copy_from(codes), Validity::NonNullable)
                    .into_array();
            let values_arr =
                PrimitiveArray::new::<f32>(Buffer::copy_from(values), Validity::NonNullable)
                    .into_array();
            let dict = DictArray::try_new(codes_arr, values_arr)?;
            let fsl = FixedSizeListArray::try_new(
                dict.into_array(),
                list_size,
                Validity::NonNullable,
                num_rows,
            )?;
            Vector::try_new_vector_array(fsl.into_array())
        }

        /// Execute an inner product and return the flat `f32` results.
        fn eval_ip_f32(lhs: ArrayRef, rhs: ArrayRef, len: usize) -> VortexResult<Vec<f32>> {
            let scalar_fn = InnerProduct::new().erased();
            let result = ScalarFnArray::try_new(scalar_fn, vec![lhs, rhs], len)?;
            let mut ctx = SESSION.create_execution_ctx();
            let prim: PrimitiveArray = result.into_array().execute(&mut ctx)?;
            Ok(prim.as_slice::<f32>().to_vec())
        }

        fn assert_close_f32(actual: &[f32], expected: &[f32], tol: f32) {
            assert_eq!(actual.len(), expected.len(), "length mismatch");
            for (i, (a, e)) in actual.iter().zip(expected).enumerate() {
                assert!(
                    (a - e).abs() < tol,
                    "row {i}: got {a}, expected {e} (diff = {})",
                    (a - e).abs()
                );
            }
        }

        /// Build a SorfTransform ScalarFnArray whose child is a `Vector<padded_dim, f32>`
        /// wrapping `FSL(Dict(codes, values))`. Returns `(sorf_array, codes, values,
        /// padded_dim)`.
        fn build_sorf_with_dict_child(
            dim: u32,
            num_rows: usize,
            seed: u64,
            num_rounds: u8,
        ) -> VortexResult<(ArrayRef, Vec<u8>, Vec<f32>, usize)> {
            let padded_dim = (dim as usize).next_power_of_two();
            // Small hand-picked codebook of 8 f32 centroids.
            let values: Vec<f32> = vec![-1.5, -1.0, -0.5, -0.1, 0.1, 0.5, 1.0, 1.5];
            // Deterministic codes in 0..values.len() covering every position.
            let codes: Vec<u8> = (0..num_rows * padded_dim)
                .map(|i| (i as u8) % (values.len() as u8))
                .collect();

            let padded_vector = dict_vector_f32(padded_dim as u32, &codes, &values)?;
            let sorf_options = SorfOptions {
                seed,
                num_rounds,
                dimensions: dim,
                element_ptype: PType::F32,
            };
            let sorf =
                SorfTransform::try_new_array(&sorf_options, padded_vector, num_rows)?.into_array();
            Ok((sorf, codes, values, padded_dim))
        }

        /// Decode a SorfTransform-wrapped dict-vector to a flat `Vec<f32>` of `num_rows *
        /// dim` post-rotation, post-truncation values. This is the ground truth against
        /// which we compare the fast-path result.
        fn decode_sorf_dict(
            codes: &[u8],
            values: &[f32],
            padded_dim: usize,
            dim: usize,
            num_rows: usize,
            seed: u64,
            num_rounds: u8,
        ) -> VortexResult<Vec<f32>> {
            let rotation = SorfMatrix::try_new_padded(padded_dim, num_rounds as usize, seed)?;
            let mut padded = vec![0.0f32; padded_dim];
            let mut rotated = vec![0.0f32; padded_dim];
            let mut out = Vec::with_capacity(num_rows * dim);
            for row in 0..num_rows {
                for j in 0..padded_dim {
                    padded[j] = values[codes[row * padded_dim + j] as usize];
                }
                rotation.inverse_rotate(&padded, &mut rotated);
                out.extend_from_slice(&rotated[..dim]);
            }
            Ok(out)
        }

        fn naive_dot(a: &[f32], b: &[f32]) -> f32 {
            a.iter().zip(b.iter()).map(|(&x, &y)| x * y).sum()
        }

        // ---- Case 1: SorfTransform + Constant pull-through ----

        #[test]
        fn constant_tensor_storage_accepts_extension_scalar_literal() -> VortexResult<()> {
            let literal = literal_vector_array(&[1.0f32, 2.0, 3.0], 5);
            let storage =
                constant_tensor_storage(&literal).expect("literal vector should be recognized");

            assert_eq!(storage.len(), 5);
            let const_storage = storage
                .as_opt::<Constant>()
                .expect("storage should remain constant-backed");
            assert!(matches!(
                const_storage.scalar().dtype(),
                DType::FixedSizeList(_, 3, Nullability::NonNullable)
            ));

            let mut ctx = SESSION.create_execution_ctx();
            let flat = extract_flat_elements(&storage, 3, &mut ctx)?;
            assert_eq!(flat.row::<f32>(0), &[1.0, 2.0, 3.0]);
            Ok(())
        }

        /// Case 1: SorfTransform on LHS, constant query on RHS, with `dim < padded_dim`
        /// so the zero-padding branch is exercised.
        #[test]
        fn case1_sorf_lhs_constant_rhs_padded_gt_dim() -> VortexResult<()> {
            let dim: u32 = 100;
            let num_rows = 7usize;
            let seed = 42u64;
            let num_rounds = 3u8;
            let padded_dim = (dim as usize).next_power_of_two();
            assert!(padded_dim > dim as usize, "test must exercise padding");

            let (sorf_lhs, codes, values, padded_dim_computed) =
                build_sorf_with_dict_child(dim, num_rows, seed, num_rounds)?;
            assert_eq!(padded_dim_computed, padded_dim);

            // Query has `dim` elements.
            let query_elems: Vec<f32> = (0..dim).map(|i| (i as f32 * 0.1).sin()).collect();
            let const_rhs = Vector::constant_array(&query_elems, num_rows)?;

            // Ground truth: decode LHS to plain f32 vectors, dot each with the query.
            let decoded = decode_sorf_dict(
                &codes,
                &values,
                padded_dim,
                dim as usize,
                num_rows,
                seed,
                num_rounds,
            )?;
            let expected: Vec<f32> = (0..num_rows)
                .map(|i| {
                    naive_dot(
                        &decoded[i * dim as usize..(i + 1) * dim as usize],
                        &query_elems,
                    )
                })
                .collect();

            let actual = eval_ip_f32(sorf_lhs, const_rhs, num_rows)?;
            assert_close_f32(&actual, &expected, 1e-3);
            Ok(())
        }

        /// Case 1: SorfTransform on RHS, constant query on LHS (mirrored).
        #[test]
        fn case1_constant_lhs_sorf_rhs_mirrored() -> VortexResult<()> {
            let dim: u32 = 100;
            let num_rows = 5usize;
            let seed = 7u64;
            let num_rounds = 3u8;

            let (sorf, codes, values, padded_dim) =
                build_sorf_with_dict_child(dim, num_rows, seed, num_rounds)?;

            let query_elems: Vec<f32> = (0..dim).map(|i| (i as f32 * 0.2).cos()).collect();
            let const_lhs = Vector::constant_array(&query_elems, num_rows)?;

            let decoded = decode_sorf_dict(
                &codes,
                &values,
                padded_dim,
                dim as usize,
                num_rows,
                seed,
                num_rounds,
            )?;
            let expected: Vec<f32> = (0..num_rows)
                .map(|i| {
                    naive_dot(
                        &decoded[i * dim as usize..(i + 1) * dim as usize],
                        &query_elems,
                    )
                })
                .collect();

            let actual = eval_ip_f32(const_lhs, sorf, num_rows)?;
            assert_close_f32(&actual, &expected, 1e-3);
            Ok(())
        }

        /// Case 1: `dim == padded_dim` (power-of-two, no zero padding).
        #[test]
        fn case1_padded_equals_dim() -> VortexResult<()> {
            let dim: u32 = 128;
            let num_rows = 4usize;
            let seed = 11u64;
            let num_rounds = 3u8;

            let (sorf, codes, values, padded_dim) =
                build_sorf_with_dict_child(dim, num_rows, seed, num_rounds)?;
            assert_eq!(padded_dim, dim as usize);

            let query_elems: Vec<f32> = (0..dim).map(|i| i as f32 * 0.01 - 0.5).collect();
            let const_rhs = Vector::constant_array(&query_elems, num_rows)?;

            let decoded = decode_sorf_dict(
                &codes,
                &values,
                padded_dim,
                dim as usize,
                num_rows,
                seed,
                num_rounds,
            )?;
            let expected: Vec<f32> = (0..num_rows)
                .map(|i| {
                    naive_dot(
                        &decoded[i * dim as usize..(i + 1) * dim as usize],
                        &query_elems,
                    )
                })
                .collect();

            let actual = eval_ip_f32(sorf, const_rhs, num_rows)?;
            assert_close_f32(&actual, &expected, 1e-3);
            Ok(())
        }

        /// Case 1: empty `len == 0`. The fast path should handle this without exploding.
        #[test]
        fn case1_empty_len_zero() -> VortexResult<()> {
            let dim: u32 = 100;
            let num_rows = 0usize;
            let seed = 42u64;
            let num_rounds = 3u8;

            let (sorf, _codes, _values, _padded_dim) =
                build_sorf_with_dict_child(dim, num_rows, seed, num_rounds)?;

            let query_elems: Vec<f32> = vec![0.0; dim as usize];
            let const_rhs = Vector::constant_array(&query_elems, num_rows)?;

            let actual = eval_ip_f32(sorf, const_rhs, num_rows)?;
            assert_eq!(actual.len(), 0);
            Ok(())
        }

        // ---- Case 2: Dict + Constant direct-lookup path ----

        /// Case 2: Vector[FSL[Dict(u8, f32)]] on LHS, constant query on RHS.
        #[test]
        fn case2_dict_lhs_constant_rhs_matches_naive() -> VortexResult<()> {
            let list_size: u32 = 8;
            let num_rows = 10usize;
            // 8 centroids, tiny table.
            let values: Vec<f32> = vec![-1.0, -0.5, -0.25, -0.1, 0.1, 0.25, 0.5, 1.0];
            // Deterministic codes.
            let codes: Vec<u8> = (0..num_rows * list_size as usize)
                .map(|i| (i as u8) % (values.len() as u8))
                .collect();
            let dict_lhs = dict_vector_f32(list_size, &codes, &values)?;

            let query: Vec<f32> = (0..list_size).map(|i| (i as f32 + 1.0) * 0.3).collect();
            let const_rhs = Vector::constant_array(&query, num_rows)?;

            let expected: Vec<f32> = (0..num_rows)
                .map(|row| {
                    let mut acc = 0.0f32;
                    for j in 0..list_size as usize {
                        let k = codes[row * list_size as usize + j] as usize;
                        acc += query[j] * values[k];
                    }
                    acc
                })
                .collect();

            let actual = eval_ip_f32(dict_lhs, const_rhs, num_rows)?;
            assert_close_f32(&actual, &expected, 1e-5);
            Ok(())
        }

        /// Case 2: constant query on LHS, dict column on RHS (mirrored).
        #[test]
        fn case2_constant_lhs_dict_rhs_mirrored() -> VortexResult<()> {
            let list_size: u32 = 4;
            let num_rows = 6usize;
            let values: Vec<f32> = vec![0.1, 0.4, 0.7, 1.0];
            let codes: Vec<u8> = (0..num_rows * list_size as usize)
                .map(|i| ((i * 3) as u8) % (values.len() as u8))
                .collect();
            let dict_rhs = dict_vector_f32(list_size, &codes, &values)?;

            let query: Vec<f32> = vec![0.5, -1.0, 2.5, -0.25];
            let const_lhs = Vector::constant_array(&query, num_rows)?;

            let expected: Vec<f32> = (0..num_rows)
                .map(|row| {
                    let mut acc = 0.0f32;
                    for j in 0..list_size as usize {
                        let k = codes[row * list_size as usize + j] as usize;
                        acc += query[j] * values[k];
                    }
                    acc
                })
                .collect();

            let actual = eval_ip_f32(const_lhs, dict_rhs, num_rows)?;
            assert_close_f32(&actual, &expected, 1e-5);
            Ok(())
        }

        /// Case 2: dict with `u16` codes (and hence more than 256 values) falls through to
        /// the standard path but still produces the correct result. The direct-lookup path
        /// only handles `u8` codes today.
        #[test]
        fn case2_u16_codes_falls_through() -> VortexResult<()> {
            let list_size: u32 = 4;
            let num_rows = 3usize;
            let num_values = 300usize;
            let values: Vec<f32> = (0..num_values).map(|i| i as f32 * 0.01).collect();
            // Codes must be u16 because 300 > 255. dict_vector_f32 only supports u8 so we
            // build the dict by hand here.
            let codes_u16: Vec<u16> = (0..(num_rows * 4))
                .map(|i| (i % num_values) as u16)
                .collect();
            let codes_arr =
                PrimitiveArray::new::<u16>(Buffer::copy_from(codes_u16), Validity::NonNullable)
                    .into_array();
            let values_arr =
                PrimitiveArray::new::<f32>(Buffer::copy_from(&values), Validity::NonNullable)
                    .into_array();
            let dict = DictArray::try_new(codes_arr, values_arr)?;
            let fsl = FixedSizeListArray::try_new(
                dict.into_array(),
                list_size,
                Validity::NonNullable,
                num_rows,
            )?;
            let dict_lhs = Vector::try_new_vector_array(fsl.into_array())?;

            let query: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0];
            let const_rhs = Vector::constant_array(&query, num_rows)?;

            // Build expected by decoding by hand.
            let expected: Vec<f32> = (0..num_rows)
                .map(|row| {
                    let mut acc = 0.0f32;
                    for j in 0..4 {
                        let code = (row * 4 + j) % num_values;
                        acc += query[j] * values[code];
                    }
                    acc
                })
                .collect();

            let actual = eval_ip_f32(dict_lhs, const_rhs, num_rows)?;
            assert_close_f32(&actual, &expected, 1e-5);
            Ok(())
        }

        /// Case 2: plain (non-dict) FSL with a constant RHS falls through to the standard
        /// path and produces the correct result.
        #[test]
        fn case2_plain_fsl_falls_through() -> VortexResult<()> {
            let dim: u32 = 4;
            let num_rows = 3usize;
            let lhs_elems: Vec<f32> = (0..num_rows * dim as usize)
                .map(|i| i as f32 * 0.25)
                .collect();
            let plain_lhs = vector_array(dim, &lhs_elems)?;

            let query: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0];
            let const_rhs = Vector::constant_array(&query, num_rows)?;

            let expected: Vec<f32> = (0..num_rows)
                .map(|row| {
                    naive_dot(
                        &lhs_elems[row * dim as usize..(row + 1) * dim as usize],
                        &query,
                    )
                })
                .collect();

            let actual = eval_ip_f32(plain_lhs, const_rhs, num_rows)?;
            assert_close_f32(&actual, &expected, 1e-5);
            Ok(())
        }

        /// Case 2: empty `len == 0` fast path returns an empty primitive array without
        /// touching the codes buffer.
        #[test]
        fn case2_empty_len_zero() -> VortexResult<()> {
            let list_size: u32 = 4;
            let num_rows = 0usize;
            let values: Vec<f32> = vec![0.0, 1.0, 2.0, 3.0];
            let codes: Vec<u8> = Vec::new();
            let dict_lhs = dict_vector_f32(list_size, &codes, &values)?;

            let query: Vec<f32> = vec![0.0; 4];
            let const_rhs = Vector::constant_array(&query, num_rows)?;

            let actual = eval_ip_f32(dict_lhs, const_rhs, num_rows)?;
            assert_eq!(actual.len(), 0);
            Ok(())
        }

        /// Case 1 + Case 2 end-to-end: the SorfTransform-wrapped dict column hits Case 1
        /// then Case 2 via recursive execution.
        #[test]
        fn end_to_end_sorf_plus_dict_cosine_path() -> VortexResult<()> {
            let dim: u32 = 100;
            let num_rows = 9usize;
            let seed = 99u64;
            let num_rounds = 3u8;

            let (sorf, codes, values, padded_dim) =
                build_sorf_with_dict_child(dim, num_rows, seed, num_rounds)?;

            let query_elems: Vec<f32> = (0..dim).map(|i| ((i as f32) * 0.15).sin() * 0.4).collect();
            let const_rhs = Vector::constant_array(&query_elems, num_rows)?;

            // Ground truth via full decode + naive dot.
            let decoded = decode_sorf_dict(
                &codes,
                &values,
                padded_dim,
                dim as usize,
                num_rows,
                seed,
                num_rounds,
            )?;
            let expected: Vec<f32> = (0..num_rows)
                .map(|i| {
                    naive_dot(
                        &decoded[i * dim as usize..(i + 1) * dim as usize],
                        &query_elems,
                    )
                })
                .collect();

            let actual = eval_ip_f32(sorf, const_rhs, num_rows)?;
            assert_close_f32(&actual, &expected, 1e-3);
            Ok(())
        }

        // ---- Additional correctness / stress tests (all with loose tolerances) ----

        /// A tiny in-place xorshift64 PRNG so these tests don't depend on `rand`. Producing
        /// deterministic pseudo-random f32 values lets the correctness checks exercise
        /// realistic data instead of smooth sin/cos patterns.
        struct XorShift64(u64);

        impl XorShift64 {
            fn new(seed: u64) -> Self {
                // Any nonzero seed is fine; xorshift fixed-points at 0.
                Self(seed.wrapping_add(0x9E37_79B9_7F4A_7C15))
            }

            fn next_u64(&mut self) -> u64 {
                let mut x = self.0;
                x ^= x << 13;
                x ^= x >> 7;
                x ^= x << 17;
                self.0 = x;
                x
            }

            /// Uniform f32 in `[-1.0, 1.0)`.
            fn next_f32(&mut self) -> f32 {
                // Top 24 bits -> mantissa in [0, 1), then shift to [-1, 1).
                let bits = (self.next_u64() >> 40) as u32; // 24 bits
                (bits as f32) / (1u32 << 24) as f32 * 2.0 - 1.0
            }
        }

        /// Case 2 stress: u8-coded dict with 200 centroids (formerly blocked by the
        /// `values.len() <= 256` gate). The direct-lookup path must now handle it.
        #[test]
        fn case2_large_u8_codebook_direct_lookup() -> VortexResult<()> {
            let list_size: u32 = 16;
            let num_rows = 20usize;
            let num_centroids = 200usize;
            assert!(num_centroids > 8 && num_centroids <= 256);

            let mut rng = XorShift64::new(0xDEAD_BEEF);
            let values: Vec<f32> = (0..num_centroids).map(|_| rng.next_f32()).collect();
            let codes: Vec<u8> = (0..num_rows * list_size as usize)
                .map(|_| (rng.next_u64() % num_centroids as u64) as u8)
                .collect();

            let dict_lhs = dict_vector_f32(list_size, &codes, &values)?;
            let query: Vec<f32> = (0..list_size).map(|_| rng.next_f32()).collect();
            let const_rhs = Vector::constant_array(&query, num_rows)?;

            let expected: Vec<f32> = (0..num_rows)
                .map(|row| {
                    let mut acc = 0.0f32;
                    for j in 0..list_size as usize {
                        let k = codes[row * list_size as usize + j] as usize;
                        acc += query[j] * values[k];
                    }
                    acc
                })
                .collect();

            let actual = eval_ip_f32(dict_lhs, const_rhs, num_rows)?;
            assert_close_f32(&actual, &expected, 1e-4);
            Ok(())
        }

        /// Parameterized sweep over the full `InnerProduct(SorfTransform(Vector[FSL(Dict)]),
        /// ConstantArray)` tree, exercising the case 1 + case 2 chain for a realistic mix
        /// of dimensions, row counts, seeds, and number of SORF rounds. Tolerance is
        /// deliberately loose because the rewrite introduces an f32-domain rotation that
        /// accumulates a small numerical drift versus a naive decode.
        #[rstest]
        #[case::small_no_pad(128, 11, 1, 1)]
        #[case::small_no_pad_rounds3(128, 23, 1_234, 3)]
        #[case::small_padded(100, 17, 42, 3)]
        #[case::mid_padded(200, 13, 2024, 3)]
        #[case::mid_power_of_two(256, 31, 7, 3)]
        #[case::larger_padded(300, 9, 99, 3)]
        #[case::max_rounds(128, 5, 31_415, 5)]
        fn case1_sorf_random_sweep(
            #[case] dim: u32,
            #[case] num_rows: usize,
            #[case] seed: u64,
            #[case] num_rounds: u8,
        ) -> VortexResult<()> {
            let (sorf, codes, values, padded_dim) =
                build_sorf_with_dict_child(dim, num_rows, seed, num_rounds)?;

            // Use a pseudo-random query with both positive and negative entries so the sum
            // has cancellation.
            let mut rng = XorShift64::new(seed ^ 0xABCD_1234);
            let query: Vec<f32> = (0..dim).map(|_| rng.next_f32()).collect();
            let const_rhs = Vector::constant_array(&query, num_rows)?;

            let decoded = decode_sorf_dict(
                &codes,
                &values,
                padded_dim,
                dim as usize,
                num_rows,
                seed,
                num_rounds,
            )?;
            let expected: Vec<f32> = (0..num_rows)
                .map(|i| naive_dot(&decoded[i * dim as usize..(i + 1) * dim as usize], &query))
                .collect();

            // Loose tolerance: the sorf transform works in f32 with a k-round butterfly, so
            // the rewrite path and the decoded path accumulate slightly different rounding
            // even though the math is equivalent in exact arithmetic.
            let actual = eval_ip_f32(sorf, const_rhs, num_rows)?;
            assert_close_f32(&actual, &expected, 1e-2);
            Ok(())
        }

        /// Parameterized sweep over plain `Vector[FSL(Dict(u8, f32))]` + constant query,
        /// without SorfTransform in the mix. This directly exercises case 2 across a
        /// variety of list sizes, num_rows, and codebook sizes including large ones that
        /// the old `<= 256` gate would have rejected.
        #[rstest]
        #[case::small(4, 7, 8)]
        #[case::medium(16, 50, 64)]
        #[case::larger(32, 100, 150)]
        #[case::very_large_codebook(8, 25, 250)]
        fn case2_random_sweep(
            #[case] list_size: u32,
            #[case] num_rows: usize,
            #[case] num_centroids: usize,
        ) -> VortexResult<()> {
            let mut rng = XorShift64::new((list_size as u64) * 31 + num_rows as u64);
            let values: Vec<f32> = (0..num_centroids).map(|_| rng.next_f32()).collect();
            assert!(num_centroids <= 256, "u8 codes cap at 256 centroids");
            let codes: Vec<u8> = (0..num_rows * list_size as usize)
                .map(|_| (rng.next_u64() % num_centroids as u64) as u8)
                .collect();

            let dict_lhs = dict_vector_f32(list_size, &codes, &values)?;
            let query: Vec<f32> = (0..list_size).map(|_| rng.next_f32()).collect();
            let const_rhs = Vector::constant_array(&query, num_rows)?;

            let expected: Vec<f32> = (0..num_rows)
                .map(|row| {
                    let mut acc = 0.0f32;
                    for j in 0..list_size as usize {
                        let k = codes[row * list_size as usize + j] as usize;
                        acc += query[j] * values[k];
                    }
                    acc
                })
                .collect();

            // Tight tolerance here because no SorfTransform rotation is involved — the
            // arithmetic should agree bit-for-bit up to float reassociation.
            let actual = eval_ip_f32(dict_lhs, const_rhs, num_rows)?;
            assert_close_f32(&actual, &expected, 1e-4);
            Ok(())
        }

        /// End-to-end regression: for a plausible vector-search configuration (SORF rounds
        /// = 3, dim = 128, num_rows = 64, u8 codes, 64 centroids), the fast-path result
        /// must track a fully naive computation within 1e-2.
        #[test]
        fn end_to_end_dim128_rows64_bit6_regression() -> VortexResult<()> {
            let dim: u32 = 128;
            let num_rows = 64usize;
            let seed = 0xFACE_F00D;
            let num_rounds = 3u8;

            // Use 64 centroids (6 bits), a typical TurboQuant configuration.
            let num_centroids = 64usize;
            let padded_dim = (dim as usize).next_power_of_two();
            let mut rng = XorShift64::new(seed);
            let values: Vec<f32> = (0..num_centroids).map(|_| rng.next_f32()).collect();
            let codes: Vec<u8> = (0..num_rows * padded_dim)
                .map(|_| (rng.next_u64() % num_centroids as u64) as u8)
                .collect();

            let padded_vector = dict_vector_f32(padded_dim as u32, &codes, &values)?;
            let sorf_options = SorfOptions {
                seed,
                num_rounds,
                dimensions: dim,
                element_ptype: PType::F32,
            };
            let sorf =
                SorfTransform::try_new_array(&sorf_options, padded_vector, num_rows)?.into_array();

            let query: Vec<f32> = (0..dim).map(|_| rng.next_f32()).collect();
            let const_rhs = Vector::constant_array(&query, num_rows)?;

            let decoded = decode_sorf_dict(
                &codes,
                &values,
                padded_dim,
                dim as usize,
                num_rows,
                seed,
                num_rounds,
            )?;
            let expected: Vec<f32> = (0..num_rows)
                .map(|i| naive_dot(&decoded[i * dim as usize..(i + 1) * dim as usize], &query))
                .collect();

            let actual = eval_ip_f32(sorf, const_rhs, num_rows)?;
            assert_close_f32(&actual, &expected, 1e-2);

            // Also verify the max relative error is small. The SORF rotation does not
            // amplify error, so both measures should be bounded.
            for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
                let denom = e.abs().max(1.0);
                let rel = (a - e).abs() / denom;
                assert!(
                    rel < 1e-3,
                    "row {i}: rel err {rel} too large (a={a}, e={e})"
                );
            }
            Ok(())
        }

        /// Case 1 + Case 2 end-to-end with varying `num_rounds`. The rotation becomes
        /// progressively more chaotic as rounds increase, so this catches any off-by-one
        /// bug in the round-indexing that would not show up in the 3-round default.
        #[rstest]
        #[case(1)]
        #[case(2)]
        #[case(3)]
        #[case(4)]
        #[case(5)]
        fn case1_various_num_rounds(#[case] num_rounds: u8) -> VortexResult<()> {
            let dim: u32 = 128;
            let num_rows = 8usize;
            let seed = 0x1234_5678;

            let (sorf, codes, values, padded_dim) =
                build_sorf_with_dict_child(dim, num_rows, seed, num_rounds)?;

            let mut rng = XorShift64::new(seed ^ (num_rounds as u64));
            let query: Vec<f32> = (0..dim).map(|_| rng.next_f32()).collect();
            let const_rhs = Vector::constant_array(&query, num_rows)?;

            let decoded = decode_sorf_dict(
                &codes,
                &values,
                padded_dim,
                dim as usize,
                num_rows,
                seed,
                num_rounds,
            )?;
            let expected: Vec<f32> = (0..num_rows)
                .map(|i| naive_dot(&decoded[i * dim as usize..(i + 1) * dim as usize], &query))
                .collect();

            let actual = eval_ip_f32(sorf, const_rhs, num_rows)?;
            assert_close_f32(&actual, &expected, 1e-2);
            Ok(())
        }

        /// Swap LHS and RHS on the full tree to prove the side-detection and the scalar
        /// argument-order handling are symmetric for both cases simultaneously.
        #[test]
        fn end_to_end_constant_lhs_sorf_rhs_mirrored() -> VortexResult<()> {
            let dim: u32 = 256;
            let num_rows = 12usize;
            let seed = 0xBEEF_CAFE;
            let num_rounds = 3u8;

            let (sorf, codes, values, padded_dim) =
                build_sorf_with_dict_child(dim, num_rows, seed, num_rounds)?;

            let mut rng = XorShift64::new(seed);
            let query: Vec<f32> = (0..dim).map(|_| rng.next_f32()).collect();
            let const_lhs = Vector::constant_array(&query, num_rows)?;

            let decoded = decode_sorf_dict(
                &codes,
                &values,
                padded_dim,
                dim as usize,
                num_rows,
                seed,
                num_rounds,
            )?;
            let expected: Vec<f32> = (0..num_rows)
                .map(|i| naive_dot(&decoded[i * dim as usize..(i + 1) * dim as usize], &query))
                .collect();

            let actual = eval_ip_f32(const_lhs, sorf, num_rows)?;
            assert_close_f32(&actual, &expected, 1e-2);
            Ok(())
        }
    }
}