Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
/*++
 
Copyright (c) Microsoft Corporation. All rights reserved.
 
Licensed under the MIT License.
 
Module Name:
 
    test_layernorm.cpp
 
Abstract:
 
    Tests for MLAS LayerNorm/RMSNorm (MlasLayerNormF32).
 
    Covers:
      - Numeric parity against fp64-accumulated scalar reference
      - Reachability: asserts the AVX2 kernel dispatched (not silent fallback)
      - Edge cases: NormSize=1, denormals, large magnitudes, zero variance,
        NaN/Inf passthrough
      - Benchmark: in-process scalar-vs-kernel comparison (DISABLED by default)
 
    Tolerance: relative 0.5% (matching upstream CloseEnough) with 1e-4 absolute
    floor. On rows that meet the x86 dispatch thresholds (8 for LayerNorm, 16
    for RMSNorm), FMA reduction order can differ from the scalar fp64 reference,
    and inverse square root amplifies small variance differences. Upstream
    CloseEnough uses rel_tol=0.005; we match that convention exactly.
 
--*/
 
#include "test_util.h"
#include "mlas.h"
#include "mlas_float16.h"
#include "core/mlas/lib/mlasi.h"
 
#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <cstring>
#include <limits>
#include <numeric>
#include <sstream>
#include <vector>
 
// ---------------------------------------------------------------------------
// Capability helpers
// ---------------------------------------------------------------------------
 
// Returns true when the platform has a SIMD LayerNorm kernel registered
// (AVX2 on x86, RVV on RISC-V, etc.).  Tests that exercise the SIMD
// path must GTEST_SKIP() when this returns false so they don't break CI
// on ARM, older x86, or any future platform that hasn't wired up a kernel.
static bool HasLayerNormKernel() {
  return GetMlasPlatform().LayerNormF32Kernel != nullptr;
}
 
static bool HasLayerNormF16Kernel() {
  return GetMlasPlatform().LayerNormF16Kernel != nullptr;
}
 
// Returns true when the platform uses the **centered two-pass** kernel
// (mean = sum/n with double-precision first-pass sum, then sum((x-mean)^2)).
// The precision suites assert properties specific to that algorithm —
// tolerances, B1 regression bounds, condition-number gates — that do not
// hold for other kernels (e.g. RISC-V RVV uncentered single-pass).
//
// Keep in sync: core/mlas/lib/layernorm.cpp (production #if gate),
//               GetKernelDispatchThreshold() (same #if).
static bool HasCenteredTwoPassKernel() {
#if defined(MLAS_TARGET_AMD64) || defined(MLAS_TARGET_IX86)
  return HasLayerNormKernel();
#else
  return false;
#endif
}
 
// ---------------------------------------------------------------------------
// fp64-accumulated scalar reference (not dependent on MLAS)
// ---------------------------------------------------------------------------
//
// Variance formula: Var = E[x²] - mean²  (uncentered, single loop in fp64).
//
// This reference deliberately uses a *different* algorithm from the kernel.
// The kernel uses a centered two-pass approach: mean = sum/n (first-pass sum
// accumulated in double), then sum((x - mean)²).  This reference instead
// computes Var = E[x²] - mean² in a single pass.  The choice is intentional
// and safe for two independent reasons:
//
//   1. fp64 precision. The uncentered formula "E[x²] - mean²" is dangerous
//      in fp32 due to catastrophic cancellation. In fp64 it is sufficiently
//      accurate for the functional test cases here. The adversarial precision
//      tests below use an fp64 Welford reference instead, avoiding cancellation.
//
//   2. Independent oracle.  A reference that uses a different algorithm from
//      the kernel cross-checks the kernel's result rather than merely
//      repeating its logic.  If the reference mirrored the kernel's centered
//      two-pass equations, a shared conceptual mistake (e.g. wrong
//      accumulator width, off-by-one in the count) could cause both to
//      produce the same wrong answer and the test would not catch it.
//      The uncentered fp64 formula and the kernel's centered two-pass are
//      algebraically equivalent but computationally independent; agreement
//      between them is a meaningful check.
//
// Do NOT make this reference mirror the kernel's algorithm: the apparent
// inconsistency is intentional and is what gives the test its value.
 
static void ReferenceLayerNorm(
    const float* input,
    const float* scale,
    const float* bias,
    float* output,
    float* mean_out,
    float* inv_std_out,
    size_t norm_size,
    float epsilon,
    bool simplified) {
  double sum = 0.0;
  double sum_sq = 0.0;
  for (size_t i = 0; i < norm_size; i++) {
    double x = static_cast<double>(input[i]);
    sum += x;
    sum_sq += x * x;
  }
  double mean = sum / static_cast<double>(norm_size);
  double denom;
  if (simplified) {
    denom = std::sqrt(sum_sq / static_cast<double>(norm_size) +
                      static_cast<double>(epsilon));
  } else {
    denom = std::sqrt(sum_sq / static_cast<double>(norm_size) -
                      mean * mean + static_cast<double>(epsilon));
  }
  double inv_denom = 1.0 / denom;
 
  for (size_t i = 0; i < norm_size; i++) {
    double x = static_cast<double>(input[i]);
    double s = static_cast<double>(scale[i]);
    if (simplified) {
      output[i] = static_cast<float>(x * inv_denom * s);
    } else if (bias == nullptr) {
      output[i] = static_cast<float>((x - mean) * inv_denom * s);
    } else {
      output[i] = static_cast<float>(
          (x - mean) * inv_denom * s + static_cast<double>(bias[i]));
    }
  }
  if (mean_out) *mean_out = static_cast<float>(mean);
  if (inv_std_out) *inv_std_out = static_cast<float>(inv_denom);
}
 
TEST(MlasLayerNormF16Test, MatchesFloatReference) {
  if (!HasLayerNormF16Kernel()) {
    GTEST_SKIP() << "FP16 LayerNorm kernel is unavailable";
  }
 
  constexpr float epsilon = 1e-5f;
  std::array<uint16_t, 15> short_input{};
  std::array<float, 15> short_scale{};
  std::array<uint16_t, 15> short_output{};
  EXPECT_FALSE(MlasLayerNormF16(
      short_input.data(), short_scale.data(), nullptr, short_output.data(),
      nullptr, nullptr, short_input.size(), epsilon, false));
 
  for (const bool simplified : {false, true}) {
    for (const bool has_bias : {false, true}) {
      if (simplified && has_bias) {
        continue;
      }
 
      for (const size_t n : {size_t{16}, size_t{31}, size_t{128}}) {
        SCOPED_TRACE(::testing::Message()
                     << "simplified=" << simplified << ", has_bias=" << has_bias << ", n=" << n);
 
        std::vector<uint16_t> input(n);
        std::vector<float> input_float(n);
        std::vector<float> scale(n);
        std::vector<float> bias(n);
        for (size_t i = 0; i < n; ++i) {
          input[i] = MLAS_Float2Half(
              std::sin(static_cast<float>(i) * 0.37f) * 3.0f + static_cast<float>(i % 7) * 0.125f);
          input_float[i] = MLAS_Half2Float(input[i]);
          scale[i] = 0.5f + static_cast<float>((i * 13) % 17) / 16.0f;
          bias[i] = static_cast<float>(static_cast<int>(i % 5) - 2) * 0.2f;
        }
 
        std::vector<float> expected(n);
        float expected_mean = 0.0f;
        float expected_inv_std = 0.0f;
        ReferenceLayerNorm(input_float.data(), scale.data(), has_bias ? bias.data() : nullptr,
                           expected.data(), &expected_mean, &expected_inv_std, n, epsilon, simplified);
 
        std::vector<uint16_t> actual(n);
        float actual_mean = 0.0f;
        float actual_inv_std = 0.0f;
        ASSERT_TRUE(MlasLayerNormF16(
            input.data(), scale.data(), has_bias ? bias.data() : nullptr, actual.data(),
            &actual_mean, &actual_inv_std, n, epsilon, simplified));
 
        for (size_t i = 0; i < n; ++i) {
          const float actual_value = MLAS_Half2Float(actual[i]);
          const float tolerance = 1e-3f + std::abs(expected[i]) * 5e-3f;
          EXPECT_NEAR(actual_value, expected[i], tolerance) << "i=" << i;
        }
        EXPECT_NEAR(actual_mean, expected_mean, 1e-5f);
        EXPECT_NEAR(actual_inv_std, expected_inv_std,
                    1e-4f + std::abs(expected_inv_std) * 5e-3f);
      }
    }
  }
}
 
// ---------------------------------------------------------------------------
// Test class
// ---------------------------------------------------------------------------
 
class MlasLayerNormTest : public MlasTestBase {
 public:
  // Minimum NormSize for SIMD dispatch.  Must mirror the production gate
  // in layernorm.cpp so the test encodes the real contract rather than
  // accepting both outcomes.
  //
  // x86 (32-bit and 64-bit): The AVX2 kernel declines NormSize < 8 for
  // LayerNorm and NormSize < 16 for RMSNorm, where SIMD setup costs exceed
  // the benefit.
  //
  // Other platforms (RISC-V RVV, future ARM SVE, etc.): variable-length
  //   vectors handle short rows natively, so the kernel dispatches for
  //   any NormSize ≥ 1.
  //
  // Keep in sync: core/mlas/lib/layernorm.cpp (production dispatch threshold).
  static constexpr size_t GetKernelDispatchThreshold(bool simplified) {
#if defined(MLAS_TARGET_AMD64) || defined(MLAS_TARGET_IX86)
    return simplified ? 16 : 8;
#else
    (void)simplified;
    return 1;
#endif
  }
 
  // Core test: numeric parity with conditional dispatch assertion.
  void Test(size_t norm_size, bool simplified, bool with_bias) {
    std::vector<float> input(norm_size);
    std::vector<float> scale(norm_size);
    std::vector<float> bias(norm_size);
    std::vector<float> output_ref(norm_size);
    std::vector<float> output_mlas(norm_size);
    float mean_ref = 0, mean_mlas = 0;
    float inv_std_ref = 0, inv_std_mlas = 0;
 
    // Deterministic fill that exercises positive, negative, and near-zero values
    for (size_t i = 0; i < norm_size; i++) {
      input[i] = (static_cast<float>(i % 127) - 63.0f) * 0.01f;
      scale[i] = 1.0f + (static_cast<float>(i % 31) - 15.0f) * 0.001f;
      bias[i] = (static_cast<float>(i % 17) - 8.0f) * 0.005f;
    }
 
    const float* bias_ptr = (with_bias && !simplified) ? bias.data() : nullptr;
 
    ReferenceLayerNorm(input.data(), scale.data(), bias_ptr,
                       output_ref.data(), &mean_ref, &inv_std_ref,
                       norm_size, 1e-5f, simplified);
 
    bool used = MlasLayerNormF32(input.data(), scale.data(), bias_ptr,
                                 output_mlas.data(), &mean_mlas, &inv_std_mlas,
                                 norm_size, 1e-5f, simplified);
 
    // DISPATCH CONTRACT: conditional on kernel availability AND NormSize.
    //   No kernel registered → MlasLayerNormF32 returns false for all N.
    //   Kernel present + NormSize >= threshold → the kernel MUST run.
    //   Kernel present + NormSize <  threshold → the kernel MUST decline.
    //   (threshold is mode/architecture-specific: 8/16 on x86, 1 elsewhere)
    const size_t dispatch_threshold = GetKernelDispatchThreshold(simplified);
    if (!HasLayerNormKernel()) {
      ASSERT_FALSE(used)
          << "MlasLayerNormF32 returned true but no kernel is registered";
      ScalarFp32Baseline(input.data(), scale.data(), bias_ptr,
                         output_mlas.data(), &mean_mlas, &inv_std_mlas,
                         norm_size, 1e-5f, simplified);
    } else if (norm_size >= dispatch_threshold) {
      ASSERT_TRUE(used)
          << "REACHABILITY FAILURE: MlasLayerNormF32 returned false for "
             "norm_size="
          << norm_size << " (>= threshold " << dispatch_threshold
          << "). The SIMD kernel must dispatch.";
    } else {
      ASSERT_FALSE(used)
          << "DISPATCH CONTRACT VIOLATION: MlasLayerNormF32 returned true for "
             "norm_size="
          << norm_size << " (< threshold " << dispatch_threshold
          << "). The kernel must decline for small N where scalar is faster.";
      ScalarFp32Baseline(input.data(), scale.data(), bias_ptr,
                         output_mlas.data(), &mean_mlas, &inv_std_mlas,
                         norm_size, 1e-5f, simplified);
    }
 
    // Use relative tolerance matching upstream's CloseEnough (rel_tol=0.005)
    // with a floor of 1e-4 absolute. The AVX2 kernel uses FMA contractions
    // that produce different rounding than the scalar fp64 reference, and
    // 1/sqrt(var+eps) amplifies small variance differences — especially for
    // small NormSize where variance is near zero.
    auto near_enough = [](float got, float ref) -> bool {
      if (std::isnan(got)) return std::isnan(ref);
      float diff = std::fabs(got - ref);
      if (diff <= 1e-4f) return true;
      float top = std::max(std::fabs(got), std::fabs(ref));
      return (top > 1e-6f) && (diff / top < 0.005f);
    };
 
    for (size_t i = 0; i < norm_size; i++) {
      ASSERT_TRUE(near_enough(output_mlas[i], output_ref[i]))
          << "output mismatch at [" << i << "], norm_size=" << norm_size
          << " simplified=" << simplified << " bias=" << with_bias
          << " got=" << output_mlas[i] << " ref=" << output_ref[i];
    }
    // Mean is not part of the RMSNorm contract (simplified mode), so only
    // check it for full LayerNorm.
    if (!simplified) {
      ASSERT_TRUE(near_enough(mean_mlas, mean_ref))
          << "mean mismatch got=" << mean_mlas << " ref=" << mean_ref;
    }
    ASSERT_TRUE(near_enough(inv_std_mlas, inv_std_ref))
        << "inv_std_dev mismatch got=" << inv_std_mlas
        << " ref=" << inv_std_ref;
  }
 
  // Edge case: all-equal input → zero variance path
  void TestZeroVariance(size_t norm_size, bool simplified) {
    std::vector<float> input(norm_size, 3.14f);
    std::vector<float> scale(norm_size, 1.0f);
    std::vector<float> output_ref(norm_size);
    std::vector<float> output_mlas(norm_size);
    float mean_ref = 0, mean_mlas = 0;
    float inv_std_ref = 0, inv_std_mlas = 0;
 
    ReferenceLayerNorm(input.data(), scale.data(), nullptr,
                       output_ref.data(), &mean_ref, &inv_std_ref,
                       norm_size, 1e-5f, simplified);
 
    bool used = MlasLayerNormF32(input.data(), scale.data(), nullptr,
                                 output_mlas.data(), &mean_mlas, &inv_std_mlas,
                                 norm_size, 1e-5f, simplified);
 
    // Apply the same conditional dispatch contract as Test().
    if (!HasLayerNormKernel()) {
      ASSERT_FALSE(used) << "No kernel registered but dispatch returned true";
      ScalarFp32Baseline(input.data(), scale.data(), nullptr,
                         output_mlas.data(), &mean_mlas, &inv_std_mlas,
                         norm_size, 1e-5f, simplified);
    } else if (norm_size >= GetKernelDispatchThreshold(simplified)) {
      ASSERT_TRUE(used) << "Kernel must dispatch for norm_size=" << norm_size;
    } else {
      ASSERT_FALSE(used) << "Kernel must decline for norm_size=" << norm_size;
      ScalarFp32Baseline(input.data(), scale.data(), nullptr,
                         output_mlas.data(), &mean_mlas, &inv_std_mlas,
                         norm_size, 1e-5f, simplified);
    }
 
    // Zero-variance: all inputs equal, so (x - mean) should be ~0.
    //
    // The kernel uses centered two-pass: mean = sum(x)/n (accumulated in
    // double), then var = sum((x - mean)^2)/n in fp32.  For constant
    // input, every (x - mean) term is exactly 0 → var = 0, inv_std =
    // 1/sqrt(eps).  Fp32 accumulation rounding in the second pass could
    // yield a tiny positive residual for large constant values, but the
    // output (x - mean) * inv_std * scale stays near zero because
    // (x - mean) ≈ 0.
    //
    // We therefore check:
    //   1. All outputs and statistics are finite (no NaN/Inf).
    //   2. Outputs match the fp64 reference within a generous tolerance
    //      that accommodates both formulations.
    auto near_enough = [](float got, float ref) -> bool {
      if (std::isnan(got)) return std::isnan(ref);
      float diff = std::fabs(got - ref);
      if (diff <= 2e-4f) return true;
      float top = std::max(std::fabs(got), std::fabs(ref));
      return (top > 1e-6f) && (diff / top < 0.005f);
    };
 
    for (size_t i = 0; i < norm_size; i++) {
      ASSERT_TRUE(std::isfinite(output_mlas[i]))
          << "Non-finite output at [" << i << "] for zero-variance input";
      ASSERT_TRUE(near_enough(output_mlas[i], output_ref[i]))
          << "Zero-variance mismatch at [" << i << "]"
          << " got=" << output_mlas[i] << " ref=" << output_ref[i];
    }
    ASSERT_TRUE(std::isfinite(inv_std_mlas)) << "inv_std_dev must be finite";
  }
 
  // Edge case: denormals — finiteness check only (not accuracy).
  // Verifies the kernel does not produce NaN/Inf on denormal inputs.
  void TestDenormals(size_t norm_size) {
    std::vector<float> input(norm_size);
    std::vector<float> scale(norm_size, 1.0f);
    std::vector<float> output_ref(norm_size);
    std::vector<float> output_mlas(norm_size);
    float mean_ref, mean_mlas, inv_std_ref, inv_std_mlas;
 
    float denorm = std::numeric_limits<float>::denorm_min();
    for (size_t i = 0; i < norm_size; i++) {
      input[i] = denorm * static_cast<float>(i + 1);
    }
 
    ReferenceLayerNorm(input.data(), scale.data(), nullptr,
                       output_ref.data(), &mean_ref, &inv_std_ref,
                       norm_size, 1e-5f, false);
 
    bool used = MlasLayerNormF32(input.data(), scale.data(), nullptr,
                                 output_mlas.data(), &mean_mlas, &inv_std_mlas,
                                 norm_size, 1e-5f, false);
    if (!HasLayerNormKernel()) {
      GTEST_SKIP() << "No SIMD LayerNorm kernel on this platform";
    }
    ASSERT_TRUE(used);
 
    for (size_t i = 0; i < norm_size; i++) {
      ASSERT_TRUE(std::isfinite(output_mlas[i]))
          << "Non-finite on denormal input at [" << i << "]";
    }
  }
 
  // Edge case: large magnitudes — finiteness check only (not accuracy).
  // Verifies the kernel does not produce NaN/Inf on ±1e30 inputs.
  void TestLargeMagnitudes(size_t norm_size) {
    std::vector<float> input(norm_size);
    std::vector<float> scale(norm_size, 1.0f);
    std::vector<float> output_ref(norm_size);
    std::vector<float> output_mlas(norm_size);
    float mean_ref, mean_mlas, inv_std_ref, inv_std_mlas;
 
    for (size_t i = 0; i < norm_size; i++) {
      input[i] = ((i % 2 == 0) ? 1.0f : -1.0f) * 1e30f;
    }
 
    ReferenceLayerNorm(input.data(), scale.data(), nullptr,
                       output_ref.data(), &mean_ref, &inv_std_ref,
                       norm_size, 1e-5f, false);
 
    bool used = MlasLayerNormF32(input.data(), scale.data(), nullptr,
                                 output_mlas.data(), &mean_mlas, &inv_std_mlas,
                                 norm_size, 1e-5f, false);
    if (!HasLayerNormKernel()) {
      GTEST_SKIP() << "No SIMD LayerNorm kernel on this platform";
    }
    ASSERT_TRUE(used);
 
    for (size_t i = 0; i < norm_size; i++) {
      ASSERT_TRUE(std::isfinite(output_mlas[i]))
          << "Non-finite on large-magnitude input at [" << i << "]";
    }
  }
 
  // Edge case: NaN/Inf passthrough — must match scalar behavior
  void TestNanInf(size_t norm_size) {
    if (norm_size < 3) return;
    std::vector<float> input(norm_size, 1.0f);
    std::vector<float> scale(norm_size, 1.0f);
    std::vector<float> output_ref(norm_size);
    std::vector<float> output_mlas(norm_size);
    float mean_ref, mean_mlas, inv_std_ref, inv_std_mlas;
 
    input[0] = std::numeric_limits<float>::quiet_NaN();
    input[1] = std::numeric_limits<float>::infinity();
    input[2] = -std::numeric_limits<float>::infinity();
 
    ReferenceLayerNorm(input.data(), scale.data(), nullptr,
                       output_ref.data(), &mean_ref, &inv_std_ref,
                       norm_size, 1e-5f, false);
 
    bool used = MlasLayerNormF32(input.data(), scale.data(), nullptr,
                                 output_mlas.data(), &mean_mlas, &inv_std_mlas,
                                 norm_size, 1e-5f, false);
    if (!HasLayerNormKernel()) {
      GTEST_SKIP() << "No SIMD LayerNorm kernel on this platform";
    }
    ASSERT_TRUE(used);
    for (size_t i = 0; i < norm_size; i++) {
      if (std::isnan(output_ref[i])) {
        ASSERT_TRUE(std::isnan(output_mlas[i]))
            << "Expected NaN at [" << i << "]";
      } else if (std::isinf(output_ref[i])) {
        ASSERT_TRUE(std::isinf(output_mlas[i]))
            << "Expected Inf at [" << i << "]";
      }
    }
  }
 
  // -----------------------------------------------------------------------
  // True scalar fp32 baseline — reproduces the fallback path from
  // onnxruntime/core/providers/cpu/nn/layer_norm_impl.cc (ComputeJob)
  // that runs when MlasLayerNormF32() returns false on x86 prior to
  // this PR. This is the code the AVX2 kernel actually replaces.
  //
  // IMPORTANT: This is fp32 throughout (no fp64 accumulation), matching
  // the production fallback. Do NOT confuse with ReferenceLayerNorm above
  // which uses fp64 accumulation for correctness testing.
  // -----------------------------------------------------------------------
  static void ScalarFp32Baseline(
      const float* input,
      const float* scale,
      const float* bias,
      float* output,
      float* mean_out,
      float* inv_std_out,
      int64_t norm_size,
      float epsilon,
      bool simplified) {
    float mean = 0.0f;
    float std_dev = 0.0f;
 
    if (simplified) {
      // RMSNorm: sum of squares, single pass
      float sum_sq = 0.0f;
      for (int64_t h = 0; h < norm_size; h++) {
        output[h] = input[h];
        sum_sq += input[h] * input[h];
      }
      std_dev = sqrt(sum_sq / norm_size + epsilon);
    } else {
      // Welford's online algorithm in fp32.
      float M2 = 0.0f;
      for (int64_t h = 0; h < norm_size; h++) {
        output[h] = input[h];
        float delta = input[h] - mean;
        mean += delta / static_cast<float>(h + 1);
        float delta2 = input[h] - mean;
        M2 += delta * delta2;
      }
      std_dev = sqrt(M2 / norm_size + epsilon);
    }
 
    for (int64_t h = 0; h < norm_size; h++) {
      if (simplified) {
        output[h] = output[h] / std_dev * scale[h];
      } else if (bias == nullptr) {
        output[h] = (output[h] - mean) / std_dev * scale[h];
      } else {
        output[h] = (output[h] - mean) / std_dev * scale[h] + bias[h];
      }
    }
 
    if (mean_out != nullptr) *mean_out = mean;
    if (inv_std_out != nullptr) *inv_std_out = 1 / std_dev;
  }
 
  // Benchmark: SIMD kernel vs true scalar fp32 baseline.
  //
  // Scalar baseline note: Welford's online mean update performs one fp32
  // division per element (delta / (h+1)) in the reduction loop, whereas
  // the AVX2 kernel accumulates a double-precision sum and divides once.
  // This per-element division inflates the scalar timing; the reported
  // speedup therefore includes both the SIMD benefit and this algorithmic
  // difference. The scalar normalization pass also performs one division per
  // output, matching the production fallback expression order.
  void Benchmark(size_t norm_size, size_t warmup, size_t iters, bool simplified) {
    std::vector<float> input(norm_size);
    std::vector<float> scale(norm_size);
    std::vector<float> output(norm_size);
    float mean_out, inv_std_out;
 
    for (size_t i = 0; i < norm_size; i++) {
      input[i] = (static_cast<float>(i % 127) - 63.0f) * 0.01f;
      scale[i] = 1.0f + (static_cast<float>(i % 31) - 15.0f) * 0.001f;
    }
 
    // Production passes nullptr for MeanOut in simplified (RMSNorm) mode.
    // Benchmark must match production to exercise the real fast path.
    float* mean_ptr = simplified ? nullptr : &mean_out;
 
    // Warmup + measure: AVX2 kernel
    for (size_t i = 0; i < warmup; i++) {
      bool used = MlasLayerNormF32(input.data(), scale.data(), nullptr,
                                   output.data(), mean_ptr, &inv_std_out,
                                   norm_size, 1e-5f, simplified);
      if (i == 0) {
        ASSERT_TRUE(used)
            << "Benchmark requires SIMD kernel dispatch for norm_size="
            << norm_size << "; got scalar fallback.";
      }
    }
    std::vector<double> kernel_us(iters);
    for (size_t i = 0; i < iters; i++) {
      auto t0 = std::chrono::high_resolution_clock::now();
      MlasLayerNormF32(input.data(), scale.data(), nullptr,
                       output.data(), mean_ptr, &inv_std_out,
                       norm_size, 1e-5f, simplified);
      auto t1 = std::chrono::high_resolution_clock::now();
      kernel_us[i] = std::chrono::duration<double, std::micro>(t1 - t0).count();
    }
 
    // Warmup + measure: scalar fp32 baseline (the actual code being replaced)
    for (size_t i = 0; i < warmup; i++) {
      ScalarFp32Baseline(input.data(), scale.data(), nullptr,
                         output.data(), mean_ptr, &inv_std_out,
                         norm_size, 1e-5f, simplified);
    }
    std::vector<double> scalar_us(iters);
    for (size_t i = 0; i < iters; i++) {
      auto t0 = std::chrono::high_resolution_clock::now();
      ScalarFp32Baseline(input.data(), scale.data(), nullptr,
                         output.data(), mean_ptr, &inv_std_out,
                         norm_size, 1e-5f, simplified);
      auto t1 = std::chrono::high_resolution_clock::now();
      scalar_us[i] = std::chrono::duration<double, std::micro>(t1 - t0).count();
    }
 
    // Also measure the fp64 reference for context (the independent oracle baseline)
    for (size_t i = 0; i < warmup; i++) {
      ReferenceLayerNorm(input.data(), scale.data(), nullptr,
                         output.data(), mean_ptr, &inv_std_out,
                         norm_size, 1e-5f, simplified);
    }
    std::vector<double> fp64_us(iters);
    for (size_t i = 0; i < iters; i++) {
      auto t0 = std::chrono::high_resolution_clock::now();
      ReferenceLayerNorm(input.data(), scale.data(), nullptr,
                         output.data(), mean_ptr, &inv_std_out,
                         norm_size, 1e-5f, simplified);
      auto t1 = std::chrono::high_resolution_clock::now();
      fp64_us[i] = std::chrono::duration<double, std::micro>(t1 - t0).count();
    }
 
    auto stats = [](std::vector<double>& v) {
      std::sort(v.begin(), v.end());
      size_t n = v.size();
      double sum = std::accumulate(v.begin(), v.end(), 0.0);
      double mean_val = sum / static_cast<double>(n);
      double sq = 0;
      for (auto x : v) sq += (x - mean_val) * (x - mean_val);
      struct S {
        double p50, p95, mean, stdev;
      };
      return S{v[n / 2], v[static_cast<size_t>(n * 0.95)], mean_val,
               std::sqrt(sq / static_cast<double>(n))};
    };
 
    auto ks = stats(kernel_us);
    auto ss = stats(scalar_us);
    auto fs = stats(fp64_us);
    const char* mode = simplified ? "RMSNorm" : "LayerNorm";
    printf("BENCH %s norm_size=%zu iters=%zu\n", mode, norm_size, iters);
    printf("  avx2_kernel:     p50=%.3fus p95=%.3fus mean=%.3fus stdev=%.3fus\n",
           ks.p50, ks.p95, ks.mean, ks.stdev);
    printf("  scalar_fp32:     p50=%.3fus p95=%.3fus mean=%.3fus stdev=%.3fus\n",
           ss.p50, ss.p95, ss.mean, ss.stdev);
    printf("  fp64_ref:        p50=%.3fus p95=%.3fus mean=%.3fus stdev=%.3fus\n",
           fs.p50, fs.p95, fs.mean, fs.stdev);
    printf("  speedup_vs_fp32: %.2fx (p50)  %.2fx (p95)\n",
           ss.p50 / ks.p50, ss.p95 / ks.p95);
    printf("  speedup_vs_fp64: %.2fx (p50)  [inflated — NOT the true baseline]\n",
           fs.p50 / ks.p50);
    printf("\n");
  }
};
 
// ---------------------------------------------------------------------------
// Short-execute test registration (upstream convention)
// ---------------------------------------------------------------------------
 
class LayerNormShortExecuteTest : public MlasTestFixture<MlasLayerNormTest> {
 public:
  LayerNormShortExecuteTest(size_t norm_size, bool simplified, bool with_bias)
      : norm_size_(norm_size), simplified_(simplified), with_bias_(with_bias) {}
 
  void TestBody() override {
    MlasTestFixture<MlasLayerNormTest>::mlas_tester->Test(
        norm_size_, simplified_, with_bias_);
  }
 
  static size_t RegisterSingleTest(size_t norm_size, bool simplified,
                                   bool with_bias) {
    std::stringstream ss;
    ss << "/norm_size" << norm_size
       << "/simplified" << simplified
       << "/bias" << with_bias;
    auto test_name = ss.str();
 
    testing::RegisterTest(
        "LayerNorm",
        test_name.c_str(),
        nullptr,
        test_name.c_str(),
        __FILE__,
        __LINE__,
        [=]() -> MlasTestFixture<MlasLayerNormTest>* {
          return new LayerNormShortExecuteTest(norm_size, simplified, with_bias);
        });
    return 1;
  }
 
  // NormSize values deliberately span non-multiples of the 8-wide AVX2 vector
  // so the scalar tail path is exercised.
  static size_t RegisterShortExecuteTests() {
    size_t count = 0;
    for (size_t n : {1, 7, 8, 15, 16, 127, 128, 1024}) {
      for (bool simplified : {true, false}) {
        for (bool with_bias : {true, false}) {
          count += RegisterSingleTest(n, simplified, with_bias);
        }
      }
    }
    return count;
  }
 
 private:
  size_t norm_size_;
  bool simplified_;
  bool with_bias_;
};
 
// ---------------------------------------------------------------------------
// Edge-case tests registered as standalone TEST_F
// ---------------------------------------------------------------------------
 
class MlasLayerNormEdgeTest : public MlasTestFixture<MlasLayerNormTest> {};
 
TEST_F(MlasLayerNormEdgeTest, ZeroVariance) {
  for (size_t n : {1, 8, 15, 128}) {
    mlas_tester->TestZeroVariance(n, false);
    mlas_tester->TestZeroVariance(n, true);
  }
}
 
TEST_F(MlasLayerNormEdgeTest, Denormals) {
  for (size_t n : {8, 15, 128}) {
    mlas_tester->TestDenormals(n);
  }
}
 
TEST_F(MlasLayerNormEdgeTest, LargeMagnitudes) {
  for (size_t n : {8, 15, 128}) {
    mlas_tester->TestLargeMagnitudes(n);
  }
}
 
TEST_F(MlasLayerNormEdgeTest, NanInf) {
  for (size_t n : {8, 15, 128}) {
    mlas_tester->TestNanInf(n);
  }
}
 
// ---------------------------------------------------------------------------
// Adversarial numeric precision tests
//
// Purpose: compare the centered two-pass AVX2 kernel (double-precision mean,
// fp32 variance) vs scalar fp32 baseline vs fp64 reference on inputs designed
// to stress catastrophic cancellation and accumulation error.  The test prints
// a comparison table for human review and asserts a defensible tolerance.
// ---------------------------------------------------------------------------
 
class MlasLayerNormPrecisionTest : public MlasTestFixture<MlasLayerNormTest> {};
 
// Helper: compute fp64 Welford reference (gold standard)
static void WelfordFp64Reference(
    const float* input, const float* scale, const float* bias,
    double* output, double* mean_out, double* inv_std_out,
    size_t norm_size, double epsilon, bool simplified) {
  if (simplified) {
    double sum_sq = 0.0;
    for (size_t i = 0; i < norm_size; i++) {
      double x = static_cast<double>(input[i]);
      sum_sq += x * x;
    }
    double rms = std::sqrt(sum_sq / static_cast<double>(norm_size) + epsilon);
    double inv = 1.0 / rms;
    for (size_t i = 0; i < norm_size; i++) {
      output[i] = static_cast<double>(input[i]) * inv *
                  static_cast<double>(scale[i]);
    }
    *mean_out = 0.0;
    *inv_std_out = inv;
  } else {
    // Welford's in fp64
    double mean = 0.0;
    double M2 = 0.0;
    for (size_t h = 0; h < norm_size; h++) {
      double x = static_cast<double>(input[h]);
      double delta = x - mean;
      mean += delta / static_cast<double>(h + 1);
      double delta2 = x - mean;
      M2 += delta * delta2;
    }
    double var = M2 / static_cast<double>(norm_size);
    double std_dev = std::sqrt(var + epsilon);
    double inv = 1.0 / std_dev;
    for (size_t i = 0; i < norm_size; i++) {
      double x = static_cast<double>(input[i]);
      double s = static_cast<double>(scale[i]);
      if (bias) {
        output[i] = (x - mean) * inv * s + static_cast<double>(bias[i]);
      } else {
        output[i] = (x - mean) * inv * s;
      }
    }
    *mean_out = mean;
    *inv_std_out = inv;
  }
}
 
// Helper: measure max relative error of fp32 outputs vs fp64 reference
// Vector-normalised max error: ||got − ref||_∞ / ||ref||_∞.
// Unlike per-element relative error, this does not blow up when individual
// reference values are near zero (as expected in layernorm output, which is
// approximately standard normal).  Returns 1e30 if any element is non-finite.
static double MaxRelError(const float* got, const double* ref, size_t n) {
  double max_diff = 0.0;
  double max_ref = 0.0;
  for (size_t i = 0; i < n; i++) {
    if (!std::isfinite(got[i]) || !std::isfinite(ref[i])) return 1e30;
    double diff = std::fabs(static_cast<double>(got[i]) - ref[i]);
    if (diff > max_diff) max_diff = diff;
    double mag = std::fabs(ref[i]);
    if (mag > max_ref) max_ref = mag;
  }
  return (max_ref > 1e-30) ? max_diff / max_ref : max_diff;
}
 
// Run one precision scenario and print results. Returns max rel error of AVX2.
static double RunPrecisionScenario(
    const char* name,
    const float* input, const float* scale, const float* bias,
    size_t norm_size, float epsilon, bool simplified) {
  // 1. fp64 Welford reference
  std::vector<double> out_fp64(norm_size);
  double mean_fp64, inv_std_fp64;
  WelfordFp64Reference(input, scale, bias, out_fp64.data(),
                       &mean_fp64, &inv_std_fp64, norm_size, epsilon, simplified);
 
  // 2. Welford fp32 (the code being replaced)
  std::vector<float> out_welford(norm_size);
  float mean_welford, inv_std_welford;
  MlasLayerNormTest::ScalarFp32Baseline(input, scale, bias, out_welford.data(),
                                        &mean_welford, &inv_std_welford,
                                        norm_size, epsilon, simplified);
 
  // 3. Centered two-pass AVX2 kernel
  std::vector<float> out_avx2(norm_size);
  float mean_avx2, inv_std_avx2;
  bool used = MlasLayerNormF32(input, scale, bias, out_avx2.data(),
                               &mean_avx2, &inv_std_avx2,
                               norm_size, epsilon, simplified);
  EXPECT_TRUE(used) << name << ": kernel must dispatch";
 
  double err_welford = MaxRelError(out_welford.data(), out_fp64.data(), norm_size);
  double err_avx2 = MaxRelError(out_avx2.data(), out_fp64.data(), norm_size);
 
  // Mean and inv_std_dev relative error
  double mean_err_w = (std::fabs(mean_fp64) > 1e-30)
                          ? std::fabs(static_cast<double>(mean_welford) - mean_fp64) / std::fabs(mean_fp64)
                          : std::fabs(static_cast<double>(mean_welford) - mean_fp64);
  double mean_err_a = (std::fabs(mean_fp64) > 1e-30)
                          ? std::fabs(static_cast<double>(mean_avx2) - mean_fp64) / std::fabs(mean_fp64)
                          : std::fabs(static_cast<double>(mean_avx2) - mean_fp64);
  double inv_err_w = (std::fabs(inv_std_fp64) > 1e-30)
                         ? std::fabs(static_cast<double>(inv_std_welford) - inv_std_fp64) / std::fabs(inv_std_fp64)
                         : std::fabs(static_cast<double>(inv_std_welford) - inv_std_fp64);
  double inv_err_a = (std::fabs(inv_std_fp64) > 1e-30)
                         ? std::fabs(static_cast<double>(inv_std_avx2) - inv_std_fp64) / std::fabs(inv_std_fp64)
                         : std::fabs(static_cast<double>(inv_std_avx2) - inv_std_fp64);
 
  printf(
      "  %-40s N=%-6zu  welford_fp32: out=%.2e mean=%.2e inv=%.2e  |  "
      "avx2_centered: out=%.2e mean=%.2e inv=%.2e  |  ratio=%.1fx\n",
      name, norm_size,
      err_welford, mean_err_w, inv_err_w,
      err_avx2, mean_err_a, inv_err_a,
      (err_welford > 1e-30) ? err_avx2 / err_welford : 0.0);
 
  return err_avx2;
}
 
// DISABLED by default — run manually with --gtest_also_run_disabled_tests.
// This is a measurement/reporting tool, not a correctness gate.
// Prints a full comparison table including catastrophic-cancellation scenarios
// where two-pass is known to degrade.
TEST_F(MlasLayerNormPrecisionTest, DISABLED_AdversarialPrecisionReport) {
  if (!HasCenteredTwoPassKernel()) {
    GTEST_SKIP() << "No centered two-pass kernel on this platform (x86 only)";
  }
  printf("\n");
  printf("======================================================================\n");
  printf("  ADVERSARIAL PRECISION: centered two-pass AVX2 vs fp64 ref\n");
  printf("  All values are MAX RELATIVE ERROR vs fp64 reference.\n");
  printf("======================================================================\n");
 
  const float eps = 1e-5f;
  double worst_avx2 = 0.0;
  double worst_catastrophic = 0.0;  // tracked separately for extreme cond#
 
  // -------------------------------------------------------------------
  // SCENARIO 1: Large N with benign data
  // -------------------------------------------------------------------
  printf("\n--- Scenario 1: Large N, benign data ---\n");
  for (size_t N : {4096, 16384, 65536}) {
    std::vector<float> input(N), scale(N, 1.0f);
    for (size_t i = 0; i < N; i++) {
      input[i] = (static_cast<float>(i % 127) - 63.0f) * 0.01f;
    }
    double e = RunPrecisionScenario("large_N_benign", input.data(),
                                    scale.data(), nullptr, N, eps, false);
    worst_avx2 = std::max(worst_avx2, e);
  }
 
  // -------------------------------------------------------------------
  // SCENARIO 2: High dynamic range — mixed tiny and huge values
  // -------------------------------------------------------------------
  printf("\n--- Scenario 2: High dynamic range ---\n");
  for (size_t N : {256, 4096}) {
    std::vector<float> input(N), scale(N, 1.0f);
    for (size_t i = 0; i < N; i++) {
      // Alternate between 1e-6 and 1e6
      input[i] = (i % 2 == 0) ? 1e-6f : 1e6f;
      // Add small perturbation
      input[i] *= (1.0f + static_cast<float>(i % 37) * 1e-4f);
    }
    double e = RunPrecisionScenario("high_dynamic_range", input.data(),
                                    scale.data(), nullptr, N, eps, false);
    worst_avx2 = std::max(worst_avx2, e);
  }
 
  // -------------------------------------------------------------------
  // SCENARIO 3: CATASTROPHIC CANCELLATION — large mean, tiny variance
  // This is THE critical case for testing the kernel's numerical
  // stability. The fp32 second-pass subtraction (x - mean) in the
  // centered two-pass loses precision when mean is huge and
  // perturbations are tiny (condition number ≫ 1e7). The kernel's
  // double-precision first-pass sum gives an accurate mean, keeping
  // the second-pass subtraction viable up to much higher condition
  // numbers than a scalar fp32 Welford.
  // -------------------------------------------------------------------
  printf("\n--- Scenario 3: CATASTROPHIC CANCELLATION (large mean, tiny var) ---\n");
  for (size_t N : {256, 1024, 4096}) {
    std::vector<float> input(N), scale(N, 1.0f);
    float base = 1e6f;
    for (size_t i = 0; i < N; i++) {
      // Values near 1e6 with spread ~1e-3 → condition number ~1e9.
      // At this condition number, fp32 (x - mean) subtraction loses
      // all significant bits of the perturbation. The double-precision
      // mean helps but cannot save the fp32 second pass entirely.
      input[i] = base + (static_cast<float>(i % 100) - 50.0f) * 1e-3f;
    }
    double e = RunPrecisionScenario("catastrophic_cancel_1e6", input.data(),
                                    scale.data(), nullptr, N, eps, false);
    worst_catastrophic = std::max(worst_catastrophic, e);
  }
 
  // Even more extreme: base = 1e7
  for (size_t N : {256, 1024}) {
    std::vector<float> input(N), scale(N, 1.0f);
    float base = 1e7f;
    for (size_t i = 0; i < N; i++) {
      input[i] = base + (static_cast<float>(i % 100) - 50.0f) * 1e-2f;
    }
    double e = RunPrecisionScenario("catastrophic_cancel_1e7", input.data(),
                                    scale.data(), nullptr, N, eps, false);
    worst_catastrophic = std::max(worst_catastrophic, e);
  }
 
  // -------------------------------------------------------------------
  // SCENARIO 4: Near-zero variance at large magnitude
  // All values the same large number + tiny epsilon perturbation
  // -------------------------------------------------------------------
  printf("\n--- Scenario 4: Near-zero variance at large magnitude ---\n");
  for (size_t N : {256, 1024}) {
    std::vector<float> input(N), scale(N, 1.0f);
    float base = 1e5f;
    for (size_t i = 0; i < N; i++) {
      input[i] = base + (i == 0 ? 1e-4f : 0.0f);
    }
    double e = RunPrecisionScenario("near_zero_var_large_mag", input.data(),
                                    scale.data(), nullptr, N, eps, false);
    worst_avx2 = std::max(worst_avx2, e);
  }
 
  // -------------------------------------------------------------------
  // SCENARIO 5: Denormals mixed with normal values
  // -------------------------------------------------------------------
  printf("\n--- Scenario 5: Denormals mixed ---\n");
  for (size_t N : {256, 1024}) {
    std::vector<float> input(N), scale(N, 1.0f);
    float denorm = std::numeric_limits<float>::denorm_min();
    for (size_t i = 0; i < N; i++) {
      input[i] = (i % 4 == 0) ? denorm * static_cast<float>(i + 1)
                              : static_cast<float>(i % 17) * 0.1f;
    }
    double e = RunPrecisionScenario("denormals_mixed", input.data(),
                                    scale.data(), nullptr, N, eps, false);
    worst_avx2 = std::max(worst_avx2, e);
  }
 
  // -------------------------------------------------------------------
  // SCENARIO 6: Near FP32 max — EXCLUDED.
  // Values near FLT_MAX produce sum(x²) that overflows fp32 (and even
  // fp64 in some formulations), yielding Inf/NaN.  This is inherent to
  // any algorithm that accumulates squares of near-max floats and is
  // not a kernel defect.  Keeping the scenario would cause 100% error
  // and make the test un-enableable.
  // -------------------------------------------------------------------
 
  // -------------------------------------------------------------------
  // SCENARIO 7: Realistic LLM hidden-state distributions
  // Typical transformer hidden states: mean ~0, std ~1-5, dim 768-4096
  // -------------------------------------------------------------------
  printf("\n--- Scenario 7: Realistic LLM activations ---\n");
  for (size_t N : {768, 1024, 2048, 4096}) {
    std::vector<float> input(N), scale(N);
    // Pseudo-Gaussian via simple deterministic hash
    for (size_t i = 0; i < N; i++) {
      // Simple deterministic "random" in [-3, 3] range (typical activation)
      uint32_t h = static_cast<uint32_t>(i * 2654435761u);
      float u = static_cast<float>(h & 0xFFFFFF) / static_cast<float>(0xFFFFFF);
      input[i] = (u - 0.5f) * 6.0f;  // range [-3, 3]
      scale[i] = 0.9f + 0.2f * static_cast<float>(i % 10) / 9.0f;
    }
    double e = RunPrecisionScenario("llm_activations", input.data(),
                                    scale.data(), nullptr, N, eps, false);
    worst_avx2 = std::max(worst_avx2, e);
  }
 
  // Also RMSNorm (simplified) for large-mean case — should be unaffected
  printf("\n--- Scenario 8: RMSNorm (simplified) with large values ---\n");
  for (size_t N : {256, 1024, 4096}) {
    std::vector<float> input(N), scale(N, 1.0f);
    for (size_t i = 0; i < N; i++) {
      input[i] = 1e6f + (static_cast<float>(i % 100) - 50.0f) * 1e-3f;
    }
    double e = RunPrecisionScenario("rmsnorm_large_mean", input.data(),
                                    scale.data(), nullptr, N, eps, true);
    worst_avx2 = std::max(worst_avx2, e);
  }
 
  printf("\n======================================================================\n");
  printf("  SUMMARY: worst AVX2 centered two-pass rel error = %.6e\n", worst_avx2);
  printf("  (catastrophic-cancellation scenarios: %.6e — tracked separately)\n",
         worst_catastrophic);
  printf("======================================================================\n\n");
 
  // Non-catastrophic scenarios must stay within 0.5% of fp64 reference.
  EXPECT_LT(worst_avx2, 0.005)
      << "Centered two-pass AVX2 kernel exceeds 0.5% max relative error vs fp64 "
         "reference. See the printed table above for per-scenario "
         "breakdown.";
 
  // Catastrophic-cancellation scenarios (condition number >= 1e8) are
  // expected to lose precision in fp32 regardless of algorithm.  The
  // centered two-pass kernel is ~10× better than scalar Welford fp32
  // here.  Gate at 10% to detect regressions without failing on
  // inherent fp32 limits.
  EXPECT_LT(worst_catastrophic, 0.1)
      << "Catastrophic-cancellation scenarios exceed 10% rel error vs fp64. "
         "Scalar Welford is ~95%; current = "
      << worst_catastrophic << ".";
}
 
// Passing test: realistic LLM activation distributions stay within tolerance.
TEST_F(MlasLayerNormPrecisionTest, RealisticLLMPrecision) {
  if (!HasCenteredTwoPassKernel()) {
    GTEST_SKIP() << "No centered two-pass kernel on this platform (x86 only)";
  }
  const float eps = 1e-5f;
  double worst = 0.0;
  for (size_t N : {768, 1024, 2048, 4096, 16384}) {
    std::vector<float> input(N), scale(N);
    for (size_t i = 0; i < N; i++) {
      uint32_t h = static_cast<uint32_t>(i * 2654435761u);
      float u = static_cast<float>(h & 0xFFFFFF) / static_cast<float>(0xFFFFFF);
      input[i] = (u - 0.5f) * 6.0f;
      scale[i] = 0.9f + 0.2f * static_cast<float>(i % 10) / 9.0f;
    }
    double e = RunPrecisionScenario("llm_realistic", input.data(),
                                    scale.data(), nullptr, N, eps, false);
    worst = std::max(worst, e);
  }
  EXPECT_LT(worst, 1e-4)
      << "AVX2 centered two-pass exceeds 0.01% rel error on realistic LLM activations";
}
 
// Passing test: large N with benign data stays within tolerance.
TEST_F(MlasLayerNormPrecisionTest, LargeNBenignPrecision) {
  if (!HasCenteredTwoPassKernel()) {
    GTEST_SKIP() << "No centered two-pass kernel on this platform (x86 only)";
  }
  const float eps = 1e-5f;
  double worst = 0.0;
  for (size_t N : {4096, 16384, 65536}) {
    std::vector<float> input(N), scale(N, 1.0f);
    for (size_t i = 0; i < N; i++) {
      input[i] = (static_cast<float>(i % 127) - 63.0f) * 0.01f;
    }
    double e = RunPrecisionScenario("large_N_benign", input.data(),
                                    scale.data(), nullptr, N, eps, false);
    worst = std::max(worst, e);
  }
  EXPECT_LT(worst, 1e-3)
      << "AVX2 centered two-pass exceeds 0.1% rel error on large-N benign data";
}
 
// Passing test: high dynamic range stays within tolerance.
TEST_F(MlasLayerNormPrecisionTest, HighDynamicRangePrecision) {
  if (!HasCenteredTwoPassKernel()) {
    GTEST_SKIP() << "No centered two-pass kernel on this platform (x86 only)";
  }
  const float eps = 1e-5f;
  double worst = 0.0;
  for (size_t N : {256, 4096}) {
    std::vector<float> input(N), scale(N, 1.0f);
    for (size_t i = 0; i < N; i++) {
      input[i] = (i % 2 == 0) ? 1e-6f : 1e6f;
      input[i] *= (1.0f + static_cast<float>(i % 37) * 1e-4f);
    }
    double e = RunPrecisionScenario("high_dynamic_range", input.data(),
                                    scale.data(), nullptr, N, eps, false);
    worst = std::max(worst, e);
  }
  EXPECT_LT(worst, 1e-4)
      << "AVX2 centered two-pass exceeds 0.01% rel error on high dynamic range data";
}
 
// Catastrophic cancellation stress test: inputs with large base offset and
// tiny spread.  The centered two-pass kernel with double-precision first-pass
// sum must:
//   1. Produce no NaN/Inf (all scenarios)
//   2. For scenarios where condition number < 1e7 (within fp32 range),
//      match the fp64 oracle to within 0.1% max relative error
//   3. For extreme condition numbers (≥ 1e7), only finiteness is asserted
//      because fp32 second-pass subtraction inherently loses precision
TEST_F(MlasLayerNormPrecisionTest, CatastrophicCancellationPasses) {
  if (!HasCenteredTwoPassKernel()) {
    GTEST_SKIP() << "No centered two-pass kernel on this platform (x86 only)";
  }
  const float eps = 1e-5f;
 
  struct Scenario {
    const char* name;
    float base;
    float spread;
  };
  Scenario scenarios[] = {
      // condition = 1e4 — well within fp32 range, accuracy is checkable
      {"catastrophic_1e4_cond1e4", 1e4f, 1.0f},
      // condition = 1e5 — moderate cancellation stress
      {"catastrophic_1e5_cond1e5", 1e5f, 1.0f},
      // condition = 1e9 — beyond fp32 precision; only finiteness is checked
      {"catastrophic_1e6", 1e6f, 1e-3f},
      // condition = 1e9 — beyond fp32 precision; only finiteness is checked
      {"catastrophic_1e7", 1e7f, 1e-2f},
  };
 
  for (const auto& sc : scenarios) {
    for (size_t N : {256, 1024}) {
      std::vector<float> input(N), scale(N, 1.0f);
      for (size_t i = 0; i < N; i++) {
        input[i] = sc.base + (static_cast<float>(i % 100) - 50.0f) * sc.spread;
      }
 
      // Centered two-pass SIMD kernel
      std::vector<float> out_avx2(N);
      float mean_avx2, inv_std_avx2;
      bool used = MlasLayerNormF32(input.data(), scale.data(), nullptr,
                                   out_avx2.data(), &mean_avx2, &inv_std_avx2,
                                   N, eps, false);
      ASSERT_TRUE(used) << sc.name << " N=" << N << ": kernel must dispatch";
 
      // 1. No NaN/Inf — the critical improvement over uncentered E[x²]-mean²
      for (size_t i = 0; i < N; i++) {
        ASSERT_TRUE(std::isfinite(out_avx2[i]))
            << sc.name << " N=" << N << ": NaN/Inf at output[" << i << "]";
      }
      ASSERT_TRUE(std::isfinite(mean_avx2))
          << sc.name << " N=" << N << ": mean is NaN/Inf";
      ASSERT_TRUE(std::isfinite(inv_std_avx2))
          << sc.name << " N=" << N << ": inv_std_dev is NaN/Inf";
 
      // 2. For condition numbers within fp32 range (base/spread < ~1e7),
      //    also check accuracy vs fp64 oracle.  At base=1e6/spread=1e-3
      //    the condition number (~1e9) exceeds fp32's ~7 digits, so the
      //    second-pass subtraction x-mean loses all precision in fp32.
      //    This is inherent to fp32 arithmetic, not a kernel bug.
      double condition = static_cast<double>(sc.base) / static_cast<double>(sc.spread);
      if (condition < 1e7) {
        std::vector<double> out_fp64(N);
        double mean_fp64, inv_std_fp64;
        WelfordFp64Reference(input.data(), scale.data(), nullptr,
                             out_fp64.data(), &mean_fp64, &inv_std_fp64,
                             N, static_cast<double>(eps), false);
 
        double fp64_err = MaxRelError(out_avx2.data(), out_fp64.data(), N);
        EXPECT_LT(fp64_err, 1e-3)
            << sc.name << " N=" << N
            << ": kernel diverges from fp64 oracle (fp64_err="
            << fp64_err << ")";
 
        printf("  %-45s N=%-6zu  finite=OK  fp64_err=%.2e\n",
               sc.name, N, fp64_err);
      } else {
        printf("  %-45s N=%-6zu  finite=OK  (cond=%.0e, fp32 limit)\n",
               sc.name, N, condition);
      }
    }
  }
}
 
// ---------------------------------------------------------------------------
// N5/N6: fp64 parity sweep — reviewer-mandated grid
//
// Measures MlasLayerNormF32 output against a fp64-accumulated reference
// for every combination in the specified grid.  Implementation-agnostic:
// any correct reduction must pass; any regression of the magnitude seen
// in B1 (scalar Welford 0.94 vs kernel centered two-pass 0.033) must fail.
//
// Effective range after the condition-number gate (base/spread < 1e6):
//   Of 16 (base, spread) pairs, 6 pass the gate:
//     {1e3}/{1.0, 0.1, 0.01}, {1e4}/{1.0, 0.1}, {1e5}/{1.0}
//   The remaining 10 pairs (cond ≥ 1e6) are skipped because fp32
//   second-pass subtraction loses all perturbation precision there,
//   making accuracy a test of float limits, not kernel correctness.
//   Total: 6 pairs × 3 epsilons × 10 NormSizes = 180 cases.
// ---------------------------------------------------------------------------
 
TEST_F(MlasLayerNormPrecisionTest, Fp64ParitySweep) {
  if (!HasCenteredTwoPassKernel()) {
    GTEST_SKIP() << "No centered two-pass kernel on this platform (x86 only)";
  }
 
  // Grid from the reviewer's specification
  const double bases[] = {1e3, 1e4, 1e5, 1e6};
  const double spreads[] = {1.0, 1e-1, 1e-2, 1e-3};
  const float epsilons[] = {1e-5f, 1e-6f, 1e-12f};
  // NormSize including non-multiples of 8
  const size_t norm_sizes[] = {9, 15, 33, 127, 255, 256, 512, 1024, 2048, 4096};
 
  // Tolerance: 3% normalised max error (||diff||_∞ / ||ref||_∞).
  // Worst observed on this kernel is ≈ 2.23e-02, giving ~35% headroom.
  // The previous 2.5e-2 threshold had only 12% headroom, too thin for
  // cross-platform / compiler variation.  At 3e-2 the B1 regression
  // guard still bites: the removed lane-parallel Welford measured
  // 2.49e-01, which is 8× above this threshold.
  constexpr double kMaxRelError = 3e-2;
 
  double overall_worst = 0.0;
  size_t total_cases = 0;
  size_t failures = 0;
 
  for (double base : bases) {
    for (double spread : spreads) {
      // Skip when condition number (base/spread) exceeds what fp32
      // can meaningfully compute.  At cond ≥ 1e6 the second-pass
      // subtraction x − mean in fp32 loses more than 1 digit of the
      // perturbation, making accuracy vs fp64 a test of float
      // precision, not kernel correctness.
      if (base / spread >= 1e6) continue;
 
      for (float eps : epsilons) {
        for (size_t N : norm_sizes) {
          // Generate input: values near `base` with perturbation ±spread
          std::vector<float> input(N), scale(N, 1.0f);
          for (size_t i = 0; i < N; i++) {
            double t = (static_cast<double>(i % 100) - 50.0) / 50.0;
            input[i] = static_cast<float>(base + t * spread);
            scale[i] = 1.0f;
          }
 
          // fp64 reference (two-pass in fp64 — algebraically exact at
          // fp32 magnitudes, algorithm-independent oracle)
          std::vector<double> out_fp64(N);
          double mean_fp64, inv_std_fp64;
          WelfordFp64Reference(input.data(), scale.data(), nullptr,
                               out_fp64.data(), &mean_fp64, &inv_std_fp64,
                               N, static_cast<double>(eps), false);
 
          // Kernel under test
          std::vector<float> out_kernel(N);
          float mean_k, inv_std_k;
          bool used = MlasLayerNormF32(input.data(), scale.data(), nullptr,
                                       out_kernel.data(), &mean_k, &inv_std_k,
                                       N, eps, false);
          ASSERT_TRUE(used) << "Kernel must dispatch for N=" << N;
 
          // Check for NaN/Inf first
          bool has_nonfinite = false;
          for (size_t i = 0; i < N; i++) {
            if (!std::isfinite(out_kernel[i])) {
              has_nonfinite = true;
              break;
            }
          }
 
          double err = has_nonfinite ? 1e30
                                     : MaxRelError(out_kernel.data(),
                                                   out_fp64.data(), N);
          overall_worst = std::max(overall_worst, err);
          total_cases++;
 
          if (err > kMaxRelError) {
            failures++;
            if (failures <= 10) {
              printf(
                  "  FAIL base=%.0e spread=%.0e eps=%.0e N=%-5zu "
                  "err=%.4e %s\n",
                  base, spread, eps, N,
                  err, has_nonfinite ? "(NaN/Inf)" : "");
            }
          }
        }
      }
    }
  }
 
  printf("\n  Fp64ParitySweep: %zu cases, %zu failures, worst=%.4e\n",
         total_cases, failures, overall_worst);
 
  // Guard against a vacuous sweep: if the condition-number gate or a
  // grid change silently drops every case, this test proves nothing.
  ASSERT_GT(total_cases, static_cast<size_t>(0))
      << "Fp64 parity sweep generated zero cases — the grid or condition-"
         "number gate is misconfigured.";
 
  EXPECT_LT(overall_worst, kMaxRelError)
      << "Fp64 parity sweep: " << failures << "/" << total_cases
      << " cases exceed " << kMaxRelError << " normalised max error. "
      << "Worst = " << overall_worst << ".";
 
  // ------------------------------------------------------------------
  // Explicit B1 regression check (base=1e5, spread=1e-2, N=1024,
  // eps=1e-6).  This case has condition number 1e7, outside the
  // sweep's cond < 1e6 gate, but it is the scenario that exposed the
  // original accuracy concern.
  //
  // Independently measured on this host (AMD EPYC 9V74, same binary):
  //   scalar Welford fp32:       0.9357  (vector-normalised max error)
  //   AVX2 centered two-pass:    0.03298
  // The kernel is ~28× more accurate than the scalar baseline here.
  //
  // We assert < 5e-2 to catch any regression back toward the scalar
  // Welford's ~0.94 while accepting the inherent fp32 limit at this
  // condition number.
  // ------------------------------------------------------------------
  {
    constexpr size_t B1_N = 1024;
    constexpr float B1_eps = 1e-6f;
    std::vector<float> b1_in(B1_N), b1_scale(B1_N, 1.0f);
    for (size_t i = 0; i < B1_N; i++) {
      double t = (static_cast<double>(i % 100) - 50.0) / 50.0;
      b1_in[i] = static_cast<float>(1e5 + t * 1e-2);
    }
    std::vector<double> b1_ref(B1_N);
    double b1_mean64, b1_inv64;
    WelfordFp64Reference(b1_in.data(), b1_scale.data(), nullptr,
                         b1_ref.data(), &b1_mean64, &b1_inv64,
                         B1_N, static_cast<double>(B1_eps), false);
    // Scalar Welford fp32 baseline (for comparison)
    std::vector<float> b1_scalar(B1_N);
    float b1_smean, b1_sinv;
    MlasLayerNormTest::ScalarFp32Baseline(
        b1_in.data(), b1_scale.data(), nullptr, b1_scalar.data(),
        &b1_smean, &b1_sinv, B1_N, B1_eps, false);
    double b1_scalar_err = MaxRelError(b1_scalar.data(), b1_ref.data(), B1_N);
    // AVX2 centered two-pass kernel
    std::vector<float> b1_out(B1_N);
    float b1_mean, b1_inv;
    MlasLayerNormF32(b1_in.data(), b1_scale.data(), nullptr,
                     b1_out.data(), &b1_mean, &b1_inv,
                     B1_N, B1_eps, false);
    double b1_err = MaxRelError(b1_out.data(), b1_ref.data(), B1_N);
    printf("  B1 regression check (base=1e5, spread=1e-2, N=1024, eps=1e-6):\n");
    printf("    scalar Welford fp32:    %.4e\n", b1_scalar_err);
    printf("    AVX2 centered two-pass: %.4e  (%.1fx better)\n",
           b1_err, b1_scalar_err / b1_err);
    EXPECT_LT(b1_err, 5e-2)
        << "B1 regression: kernel error at base=1e5, spread=1e-2, "
        << "N=1024 exceeds 5%. Scalar Welford was " << b1_scalar_err
        << "; current = " << b1_err << ".";
  }
}
 
// ---------------------------------------------------------------------------
// Benchmark (disabled by default; run with --gtest_also_run_disabled_tests)
// ---------------------------------------------------------------------------
 
class MlasLayerNormBenchTest : public MlasTestFixture<MlasLayerNormTest> {};
 
TEST_F(MlasLayerNormBenchTest, DISABLED_Benchmark) {
  // Representative shapes: threshold-aware + LLM-realistic hidden dims.
  // Sizes below the dispatch threshold are excluded because the kernel
  // declines them, and timing the scalar fallback is misleading.
  printf("\n=== LayerNorm (full) ===\n");
  for (size_t n : {15, 128, 256, 768, 1024, 2048, 4096}) {
    mlas_tester->Benchmark(n, /*warmup=*/100, /*iters=*/1000, /*simplified=*/false);
  }
  printf("\n=== RMSNorm (simplified) ===\n");
  for (size_t n : {16, 128, 256, 768, 1024, 2048, 4096}) {
    mlas_tester->Benchmark(n, /*warmup=*/100, /*iters=*/1000, /*simplified=*/true);
  }
}
 
// ---------------------------------------------------------------------------
// Registration into MLAS test harness
// ---------------------------------------------------------------------------
 
static UNUSED_VARIABLE bool added_to_main = AddTestRegister(
    [](bool is_short_execute) -> size_t {
      if (is_short_execute) {
        return LayerNormShortExecuteTest::RegisterShortExecuteTests();
      }
      return 0;
    });