Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "test_util.h"
#include "core/mlas/lib/mlasi.h"
#include "core/mlas/lib/eltwise.h"
 
class MlasEltwiseAddTest : public MlasTestBase {
 private:
  MatrixGuardBuffer<float> BufferInputLeft;
  MatrixGuardBuffer<float> BufferInputRight;
  MatrixGuardBuffer<float> BufferOutput;
  MatrixGuardBuffer<float> BufferOutputReference;
  MatrixGuardBuffer<MLAS_FP16> BufferInputLeftFp16;
  MatrixGuardBuffer<MLAS_FP16> BufferInputRightFp16;
  MatrixGuardBuffer<MLAS_FP16> BufferOutputFp16;
 
  void Test(size_t N, float MinimumValue, float MaximumValue, const std::optional<float>& ScalarValue = std::nullopt) {
    float* InputLeft = BufferInputLeft.GetBuffer(N);
    float* InputRight = BufferInputRight.GetBuffer(N);
    float* Output = BufferOutput.GetBuffer(N);
    float* OutputReference = BufferOutputReference.GetBuffer(N);
 
    std::default_random_engine generator(static_cast<unsigned>(N));
    std::uniform_real_distribution<float> distribution(MinimumValue, MaximumValue);
 
    for (size_t n = 0; n < N; n++) {
      InputLeft[n] = distribution(generator);
      InputRight[n] = ScalarValue.value_or(distribution(generator));
    }
 
    for (size_t n = 0; n < N; n++) {
      OutputReference[n] = InputLeft[n] + InputRight[n];
    }
 
    MlasEltwiseAdd(InputLeft, InputRight, Output, N);
 
    constexpr float AbsoluteTolerance = 1e-6f;
    constexpr float RelativeTolerance = 1e-6f;
 
    for (size_t n = 0; n < N; n++) {
      float diff = std::fabs(Output[n] - OutputReference[n]);
      ASSERT_TRUE(diff <= AbsoluteTolerance || diff <= std::fabs(OutputReference[n]) * RelativeTolerance)
          << " @" << n << " of " << N << ", got: " << Output[n] << ", expecting: " << OutputReference[n];
    }
  }
 
#if defined(MLAS_F16VEC_INTRINSICS_SUPPORTED) && defined(MLAS_TARGET_ARM64)
 
  void TestFp16(size_t N, float MinimumValue, float MaximumValue, const std::optional<float>& ScalarValue = std::nullopt) {
    MLAS_FP16* InputLeft = BufferInputLeftFp16.GetBuffer(N);
    MLAS_FP16* InputRight = BufferInputRightFp16.GetBuffer(N);
    MLAS_FP16* Output = BufferOutputFp16.GetBuffer(N);
 
    std::default_random_engine generator(static_cast<unsigned>(N));
    std::uniform_real_distribution<float> distribution(MinimumValue, MaximumValue);
 
    for (size_t n = 0; n < N; n++) {
      InputLeft[n] = MLAS_FP16(distribution(generator));
      InputRight[n] = MLAS_FP16(ScalarValue.value_or(distribution(generator)));
    }
 
    MlasEltwiseAdd(InputLeft, InputRight, Output, N);
 
    constexpr float AbsoluteTolerance = 5e-4f;
    constexpr float RelativeTolerance = 1e-3f;
 
    for (size_t n = 0; n < N; n++) {
      float inLeft = InputLeft[n].ToFloat();
      float inRight = InputRight[n].ToFloat();
      float ref = inLeft + inRight;
      float out = Output[n].ToFloat();
      float diff = std::fabs(out - ref);
      ASSERT_TRUE(diff <= AbsoluteTolerance || diff <= std::fabs(ref) * RelativeTolerance)
          << " @ " << inLeft << ", " << inRight << ", got: " << out << ", expecting: " << ref
          << ", r-diff: " << diff / std::fabs(ref);
    }
  }
 
#endif  // defined(MLAS_F16VEC_INTRINSICS_SUPPORTED) && defined(MLAS_TARGET_ARM64)
 
 public:
  static const char* GetTestSuiteName() {
    static const std::string suite_name("Eltwise_Add");
    return suite_name.c_str();
  }
 
  void ExecuteShort(void) override {
    for (size_t n = 1; n < 128; n++) {
      Test(n, -10.f, 10.f);
      Test(n, -10.f, 10.f, -5000.f);
#if defined(MLAS_F16VEC_INTRINSICS_SUPPORTED) && defined(MLAS_TARGET_ARM64)
      TestFp16(n, -17.f, 11.f);
      TestFp16(n, -17.f, 11.f, -5000.f);
#endif  // defined(MLAS_F16VEC_INTRINSICS_SUPPORTED) && defined(MLAS_TARGET_ARM64)
    }
  }
};
 
class MlasEltwiseMulTest : public MlasTestBase {
 private:
  MatrixGuardBuffer<float> BufferInputLeft;
  MatrixGuardBuffer<float> BufferInputRight;
  MatrixGuardBuffer<float> BufferOutput;
  MatrixGuardBuffer<float> BufferOutputReference;
 
  void Test(size_t N, float MinimumValue, float MaximumValue, const std::optional<float>& ScalarValue = std::nullopt) {
    float* InputLeft = BufferInputLeft.GetBuffer(N);
    float* InputRight = BufferInputRight.GetBuffer(N);
    float* Output = BufferOutput.GetBuffer(N);
    float* OutputReference = BufferOutputReference.GetBuffer(N);
 
    std::default_random_engine generator(static_cast<unsigned>(N));
    std::uniform_real_distribution<float> distribution(MinimumValue, MaximumValue);
 
    for (size_t n = 0; n < N; n++) {
      InputLeft[n] = distribution(generator);
      InputRight[n] = ScalarValue.value_or(distribution(generator));
    }
 
    for (size_t n = 0; n < N; n++) {
      OutputReference[n] = InputLeft[n] * InputRight[n];
    }
 
    MlasEltwiseMul(InputLeft, InputRight, Output, N);
 
    constexpr float AbsoluteTolerance = 1e-6f;
    constexpr float RelativeTolerance = 1e-6f;
 
    for (size_t n = 0; n < N; n++) {
      float diff = std::fabs(Output[n] - OutputReference[n]);
      ASSERT_TRUE(diff <= AbsoluteTolerance || diff <= std::fabs(OutputReference[n]) * RelativeTolerance)
          << " @" << n << " of " << N << ", got: " << Output[n] << ", expecting: " << OutputReference[n];
    }
  }
 
 public:
  static const char* GetTestSuiteName() {
    static const std::string suite_name("Eltwise_Mul");
    return suite_name.c_str();
  }
 
  void ExecuteShort(void) override {
    for (size_t n = 1; n < 128; n++) {
      Test(n, -10.f, 10.f);
      Test(n, -10.f, 10.f, -5000.f);
    }
  }
};
 
static UNUSED_VARIABLE bool added_to_main = AddTestRegister([](bool is_short_execute) {
  size_t count = 0;
  if (is_short_execute) {
    count += MlasDirectShortExecuteTests<MlasEltwiseAddTest>::RegisterShortExecute();
    count += MlasDirectShortExecuteTests<MlasEltwiseMulTest>::RegisterShortExecute();
  }
  return count;
});