Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
// Boundary + random-fuzz tests for the RISC-V RVV activation kernels
// (Erf / Tanh / Logistic / Exp / SiLU / GELU). These kernels diverge from
// the scalar reference path by (a) replacing libm-style exp with a packed
// 2^n + minimax polynomial approximation, (b) clamping inputs before the
// exponent-bit reconstruction, and (c) inserting explicit `(±inf) * 0`
// guards on SiLU/GELU. The existing test_activation.cpp exercises the
// MLAS_ACTIVATION_KIND surface (Identity/Relu/LeakyRelu/Tanh/Logistic/
// Clip/HardSigmoid) but not the MlasComputeUnary* entry points — these
// tests fill that gap and pin the kernels' contract under NaN, ±Inf,
// large negatives, and EXP_RECON_MIN/MAX boundary inputs.
 
#include "test_util.h"
 
#include <cmath>
#include <cstring>
#include <random>
 
class MlasActivationRvvBoundaryTest : public MlasTestBase {
 public:
  static const char* GetTestSuiteName() {
    static const std::string suite_name("ActivationRvvBoundary");
    return suite_name.c_str();
  }
 
  void ExecuteShort(void) override {
    TestBoundaryNaN();
    TestBoundaryPositiveInf();
    TestBoundaryNegativeInf();
    TestBoundarySiluGeluLargeNegative();
    TestBoundaryExpReconLimits();
    TestRandomVsScalar();
  }
 
 private:
  // ─── Boundary cases ─────────────────────────────────────────────────────
 
  void TestBoundaryNaN() {
    constexpr size_t N = 8;
    const float qnan = std::nanf("");
    for (auto& kernel : kAllKernels) {
      float in[N], out[N];
      for (size_t i = 0; i < N; ++i) in[i] = qnan;
      std::memcpy(out, in, sizeof(out));
      kernel.fn(out, out, N);
      for (size_t i = 0; i < N; ++i) {
        EXPECT_TRUE(std::isnan(out[i]))
            << kernel.name << " did not propagate NaN at i=" << i;
      }
    }
  }
 
  void TestBoundaryPositiveInf() {
    constexpr size_t N = 4;
    const float pinf = std::numeric_limits<float>::infinity();
    struct Expect {
      const char* name;
      void(MLASCALL* fn)(const float*, float*, size_t);
      float expected;
      bool allow_nan;
    } cases[] = {
        {"Erf+Inf", MlasComputeErf, 1.0f, false},
        {"Tanh+Inf", MlasComputeTanh, 1.0f, false},
        {"Logistic+Inf", MlasComputeLogistic, 1.0f, false},
        // exp(+inf) = +inf; some impls saturate to FLT_MAX, accept either.
        {"Exp+Inf", MlasComputeExp, pinf, true},
        // SiLU(+inf) = +inf * sigmoid(+inf) = +inf * 1 = +inf
        {"Silu+Inf", MlasComputeSilu, pinf, true},
    };
    for (auto& c : cases) {
      float in[N], out[N];
      for (size_t i = 0; i < N; ++i) in[i] = pinf;
      c.fn(in, out, N);
      for (size_t i = 0; i < N; ++i) {
        const bool ok = out[i] == c.expected ||
                        (c.allow_nan && std::isnan(out[i])) ||
                        (c.allow_nan && out[i] >= std::numeric_limits<float>::max() * 0.5f);
        EXPECT_TRUE(ok)
            << c.name << " at i=" << i
            << " got " << out[i] << " expected " << c.expected;
      }
    }
    // GeluErf(+inf) → +inf (or saturated to FLT_MAX)
    {
      float in[N], out[N];
      for (size_t i = 0; i < N; ++i) in[i] = pinf;
      MlasComputeGeluErf(in, out, N);
      for (size_t i = 0; i < N; ++i) {
        EXPECT_TRUE(out[i] == pinf || out[i] >= std::numeric_limits<float>::max() * 0.5f)
            << "GeluErf+Inf at i=" << i << " got " << out[i];
      }
    }
  }
 
  void TestBoundaryNegativeInf() {
    constexpr size_t N = 4;
    const float ninf = -std::numeric_limits<float>::infinity();
    struct Expect {
      const char* name;
      void(MLASCALL* fn)(const float*, float*, size_t);
      float expected;
    } cases[] = {
        {"Erf-Inf", MlasComputeErf, -1.0f},
        {"Tanh-Inf", MlasComputeTanh, -1.0f},
        {"Logistic-Inf", MlasComputeLogistic, 0.0f},
        // exp(-inf) = 0 (after clamp inside exp_f32m4)
        {"Exp-Inf", MlasComputeExp, 0.0f},
    };
    for (auto& c : cases) {
      float in[N], out[N];
      for (size_t i = 0; i < N; ++i) in[i] = ninf;
      c.fn(in, out, N);
      for (size_t i = 0; i < N; ++i) {
        EXPECT_EQ(out[i], c.expected)
            << c.name << " at i=" << i << " got " << out[i];
      }
    }
  }
 
  void TestBoundarySiluGeluLargeNegative() {
    // SiLU(x) = x * sigmoid(x). For very negative x, sigmoid(x) → 0,
    // so SiLU(x) → 0 (well-defined). The RVV kernel adds a LOGISTIC_CLAMP
    // and an `(±inf) * 0 → NaN` guard so naive (-inf) * 0 does not leak.
    // Same idea for GELU(x) ≈ 0 for very negative x via the SMALLEST_NORMAL_F32
    // floor.
    constexpr size_t N = 8;
    const float large_neg[N] = {-1000.0f, -500.0f, -200.0f, -100.0f,
                                -88.5f, -90.0f, -120.0f, -50.0f};
    float out_silu[N], out_gelu[N];
    MlasComputeSilu(large_neg, out_silu, N);
    MlasComputeGeluErf(large_neg, out_gelu, N);
    for (size_t i = 0; i < N; ++i) {
      EXPECT_FALSE(std::isnan(out_silu[i]))
          << "SiLU(" << large_neg[i] << ") produced NaN — inf*0 guard failed";
      EXPECT_LE(std::fabs(out_silu[i]), 1e-3f)
          << "SiLU(" << large_neg[i] << ") should be ~0, got " << out_silu[i];
      EXPECT_FALSE(std::isnan(out_gelu[i]))
          << "GeluErf(" << large_neg[i] << ") produced NaN";
      EXPECT_LE(std::fabs(out_gelu[i]), 1e-3f)
          << "GeluErf(" << large_neg[i] << ") should be ~0, got " << out_gelu[i];
    }
  }
 
  void TestBoundaryExpReconLimits() {
    // exp_f32m4 uses 2^n exponent-bit reconstruction with an internal clamp.
    // Inputs at the edges of the recon range must produce finite, monotone
    // results — not Inf, NaN, or sign flips.
    constexpr size_t N = 8;
    const float boundary[N] = {-88.5f, -88.0f, -87.5f, -1.0f,
                               1.0f, 87.0f, 87.5f, 88.0f};
    float out[N];
    MlasComputeExp(boundary, out, N);
    float prev = -1.0f;
    for (size_t i = 0; i < N; ++i) {
      EXPECT_TRUE(std::isfinite(out[i]))
          << "Exp boundary[" << i << "]=" << boundary[i]
          << " produced non-finite " << out[i];
      EXPECT_GE(out[i], 0.0f)
          << "Exp should be non-negative at boundary[" << i << "]";
      EXPECT_GE(out[i], prev)
          << "Exp monotonicity broken between boundary[" << (i - 1)
          << "]=" << (i > 0 ? boundary[i - 1] : 0.0f)
          << " and boundary[" << i << "]=" << boundary[i];
      prev = out[i];
    }
  }
 
  // ─── Random fuzz against scalar reference ───────────────────────────────
 
  void TestRandomVsScalar() {
    // 1000 random inputs in [-50, 50] — a realistic production range for
    // activation inputs (Transformer/CNN intermediate features) — each
    // compared against the scalar libm reference. RVV kernels approximate
    // with a 6th-order minimax polynomial + range reduction, so we accept
    // up to ~32 ULP for transcendentals, matching what AVX2 specializations
    // tolerate in the existing test infrastructure.
    constexpr size_t N = 1000;
    std::mt19937 gen(0xC0FFEE);
    std::uniform_real_distribution<float> dist(-50.0f, 50.0f);
 
    std::vector<float> in(N);
    for (size_t i = 0; i < N; ++i) in[i] = dist(gen);
 
    auto CheckRel = [&in](const char* name,
                          const std::vector<float>& fast,
                          const std::vector<float>& ref,
                          float abs_tol,
                          float rel_tol) {
      ASSERT_EQ(fast.size(), ref.size()) << name;
      for (size_t i = 0; i < fast.size(); ++i) {
        const float a = fast[i];
        const float b = ref[i];
        if (!std::isfinite(b)) continue;  // skip overflow points
        const float diff = std::fabs(a - b);
        const float scale = std::max(std::fabs(b), 1.0f);
        EXPECT_LE(diff, abs_tol + rel_tol * scale)
            << name << " at i=" << i << " in=" << in[i]
            << " fast=" << a << " ref=" << b << " diff=" << diff;
      }
    };
 
    std::vector<float> out(N), ref(N);
 
    // Erf
    MlasComputeErf(in.data(), out.data(), N);
    for (size_t i = 0; i < N; ++i) ref[i] = std::erf(in[i]);
    CheckRel("Erf", out, ref, 5e-5f, 1e-5f);
 
    // Tanh
    MlasComputeTanh(in.data(), out.data(), N);
    for (size_t i = 0; i < N; ++i) ref[i] = std::tanh(in[i]);
    CheckRel("Tanh", out, ref, 5e-5f, 1e-5f);
 
    // Logistic (sigmoid)
    MlasComputeLogistic(in.data(), out.data(), N);
    for (size_t i = 0; i < N; ++i) ref[i] = 1.0f / (1.0f + std::exp(-in[i]));
    CheckRel("Logistic", out, ref, 5e-5f, 1e-5f);
 
    // Exp — confine to non-overflowing range
    {
      std::uniform_real_distribution<float> exp_dist(-30.0f, 30.0f);
      std::vector<float> exp_in(N), exp_out(N), exp_ref(N);
      for (size_t i = 0; i < N; ++i) exp_in[i] = exp_dist(gen);
      MlasComputeExp(exp_in.data(), exp_out.data(), N);
      for (size_t i = 0; i < N; ++i) exp_ref[i] = std::exp(exp_in[i]);
      // Exp grows fast: use a relative-only tolerance
      for (size_t i = 0; i < N; ++i) {
        if (!std::isfinite(exp_ref[i])) continue;
        const float rel = std::fabs(exp_out[i] - exp_ref[i]) /
                          std::max(std::fabs(exp_ref[i]), 1e-12f);
        EXPECT_LE(rel, 5e-5f)
            << "Exp rel err at i=" << i << " in=" << exp_in[i]
            << " fast=" << exp_out[i] << " ref=" << exp_ref[i];
      }
    }
 
    // SiLU(x) = x * sigmoid(x)
    MlasComputeSilu(in.data(), out.data(), N);
    for (size_t i = 0; i < N; ++i) {
      ref[i] = in[i] / (1.0f + std::exp(-in[i]));
    }
    CheckRel("Silu", out, ref, 1e-4f, 1e-5f);
 
    // GeluErf(x) = 0.5 * x * (1 + erf(x / sqrt(2)))
    MlasComputeGeluErf(in.data(), out.data(), N);
    for (size_t i = 0; i < N; ++i) {
      ref[i] = 0.5f * in[i] * (1.0f + std::erf(in[i] * 0.7071067811865476f));
    }
    CheckRel("GeluErf", out, ref, 1e-4f, 1e-5f);
  }
 
  struct KernelEntry {
    const char* name;
    void(MLASCALL* fn)(const float*, float*, size_t);
  };
 
  static constexpr KernelEntry kAllKernels[] = {
      {"Erf", MlasComputeErf},
      {"Tanh", MlasComputeTanh},
      {"Logistic", MlasComputeLogistic},
      {"Exp", MlasComputeExp},
      {"Silu", MlasComputeSilu},
      {"GeluErf", MlasComputeGeluErf},
  };
};
 
constexpr MlasActivationRvvBoundaryTest::KernelEntry
    MlasActivationRvvBoundaryTest::kAllKernels[];
 
static UNUSED_VARIABLE bool added_rvv_boundary_to_main = AddTestRegister(
    [](bool is_short_execute) {
      return is_short_execute
                 ? MlasDirectShortExecuteTests<MlasActivationRvvBoundaryTest>::RegisterShortExecute()
                 : 0;
    });