Исходный код СУБД 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
/*++
 
Copyright (c) Microsoft Corporation. All rights reserved.
 
Licensed under the MIT License.
 
Module Name:
 
    test_blockq4.cpp
 
Abstract:
 
    Tests for MLAS blockwise int4 quantization and dequantization code.
 
--*/
 
#ifndef ORT_MINIMAL_BUILD
 
#include "test_util.h"
#include "mlas_q4.h"
#include "core/mlas/lib/mlasi.h"
 
template <int qbits>
int GetElem(int v, int idx) {
  return (v >> (qbits * idx)) & ((1 << qbits) - 1);
}
 
template <int qbits>
int SetElem(int v, int idx, int value) {
  v &= ~(((1 << qbits) - 1) << (qbits * idx));
  v |= (value & ((1 << qbits) - 1)) << (qbits * idx);
  return v;
}
 
template <typename T, int qbits>
class MlasBlockwiseQdqTest : public MlasTestBase {
 private:
  std::random_device rd;
  std::mt19937 gen{192373};
  std::uniform_int_distribution<int> dist_int{0, (1 << qbits) - 1};
  std::uniform_real_distribution<float> dist_float{-1.f, 1.f};
  MatrixGuardBuffer<T> FpBuf;
  MatrixGuardBuffer<T> FpBuf2;
  MatrixGuardBuffer<T> FpBuf3;
  MatrixGuardBuffer<uint8_t> InputElements;
  MatrixGuardBuffer<T> InputScales;
  MatrixGuardBuffer<uint8_t> InputOffsets;
  MatrixGuardBuffer<uint8_t> QDQOutputElements;
  MatrixGuardBuffer<T> QDQOutputScales;
  MatrixGuardBuffer<uint8_t> QDQOutputOffsets;
  MatrixGuardBuffer<uint8_t> QDQTransposedOutputElements;
  MatrixGuardBuffer<T> QDQTransposedOutputScales;
  MatrixGuardBuffer<uint8_t> QDQTransposedOutputOffsets;
  constexpr static float err_ = qbits == 8 ? 1e-2f : qbits == 4 ? 6e-2f
                                                                : 2e-1f;
  constexpr static float rel_ = qbits == 8 ? 5e-2f : qbits == 4 ? 2e-1f
                                                                : 5e-1f;
 
  bool FloatEqual(T a, T b, float err = err_, float rel = rel_) {
    float va = static_cast<float>(a);
    float vb = static_cast<float>(b);
    return std::abs(va - vb) < err + std::abs(va) * rel;
  }
 
  void Test(int rows, int columns, int block_size, bool columnwise, bool symmetric) {
    constexpr int packSize = 8 / qbits;
    T* input = FpBuf.GetFilledBuffer(rows * columns, [this](T* start, size_t size) {
      for (size_t i = 0; i < size; i++) {
        start[i] = T(this->dist_float(this->gen));
      }
    });
    T* dequant = FpBuf2.GetBuffer(rows * columns, true);
    T* transposed = FpBuf3.GetBuffer(rows * columns, true);
    size_t scale_size = (rows + block_size - 1) / block_size * columns;
    size_t zp_size = (scale_size + packSize - 1) / packSize;
    MLAS_THREADPOOL* threadpool_ptr = GetMlasThreadPool();
 
    int meta_rows;
    int meta_cols;
    MlasBlockwiseQuantMetaShape<T, qbits>(block_size, columnwise, rows, columns, meta_rows, meta_cols);
 
    int q_rows;
    int q_cols;
    MlasBlockwiseQuantizedShape<T, qbits>(block_size, columnwise, rows, columns, q_rows, q_cols);
 
    size_t q_data_size_in_bytes, q_scale_size, q_zp_size_in_bytes;
    MlasBlockwiseQuantizedBufferSizes<qbits>(block_size, columnwise, rows, columns,
                                             q_data_size_in_bytes, q_scale_size, &q_zp_size_in_bytes);
 
    uint8_t* elements = InputElements.GetBuffer(q_data_size_in_bytes, true);  // after quantize
    uint8_t* qdq_weights;
    uint8_t* qdq_weights_T;
    if constexpr (qbits == 4) {
      qdq_weights = QDQOutputElements.GetBuffer((rows * columns + packSize - 1) / packSize, true);
      qdq_weights_T = QDQTransposedOutputElements.GetBuffer(q_data_size_in_bytes, true);
    }
 
    T* scales = InputScales.GetBuffer(q_scale_size, true);
    uint8_t* zp = symmetric ? nullptr : InputOffsets.GetBuffer(q_zp_size_in_bytes, true);
    T* qdq_scales;
    T* qdq_scales_T;
    uint8_t* qdq_zp;
    uint8_t* qdq_zp_T;
    if constexpr (qbits == 4) {
      qdq_scales = QDQOutputScales.GetBuffer(scale_size, true);
      qdq_scales_T = QDQTransposedOutputScales.GetBuffer(q_scale_size, true);
      qdq_zp = symmetric ? nullptr : QDQOutputOffsets.GetBuffer(zp_size, true);
      qdq_zp_T = symmetric ? nullptr : QDQTransposedOutputOffsets.GetBuffer(q_zp_size_in_bytes, true);
    }
 
    MlasQuantizeBlockwise<T, qbits>(elements, scales, zp, input, block_size,
                                    columnwise, rows, columns, columns, threadpool_ptr);
 
    MlasDequantizeBlockwise<T, qbits>(dequant, elements, scales, zp, block_size,
                                      columnwise, rows, columns, threadpool_ptr);
 
    MlasTranspose(dequant, transposed, columns, rows, threadpool_ptr);
 
    if constexpr (qbits == 4) {
      if (columnwise) {
        bool signed_quant = MlasQDQQuantizeBlockwise<T, qbits>(
            input, qdq_scales, qdq_zp, qdq_weights,
            true, rows, columns, block_size, threadpool_ptr);
 
        ASSERT_EQ(symmetric, signed_quant) << "symmetric quantization should be signed";
 
        if (symmetric) {
          MlasQDQTransposeBlockwiseQuantized<T, qbits, true>(
              qdq_weights, qdq_scales, qdq_zp, qdq_weights_T, qdq_scales_T, qdq_zp_T,
              true, rows, columns, block_size, threadpool_ptr);
 
        } else {
          MlasQDQTransposeBlockwiseQuantized<T, qbits, false>(
              qdq_weights, qdq_scales, qdq_zp, qdq_weights_T, qdq_scales_T, qdq_zp_T,
              true, rows, columns, block_size, threadpool_ptr);
        }
      }
    }
 
    for (int r = 0; r < rows; r++) {
      for (int c = 0; c < columns; c++) {
        int idx = r * columns + c;
        ASSERT_TRUE(FloatEqual(input[idx], transposed[idx]))
            << " input: " << input[idx] << ", transposed: " << transposed[idx]
            << ", index=[" << r << "x" << c << "], shape=[" << rows << "x" << columns
            << "] block: " << block_size << ", symmetric: " << symmetric << ", columnwise: " << columnwise;
      }
    }
 
    if (columnwise && qbits == 4) {
      for (int c = 0; c < columns; c++) {
        for (int r = 0; r < rows; r += packSize) {
          int idx = c * q_rows + r / packSize;
          for (int l = 0; l < packSize && l + r < rows; ++l) {
            ASSERT_EQ(GetElem<qbits>(qdq_weights_T[idx], l), GetElem<qbits>(elements[idx], l))
                << ", qdq index=[" << r + l << "x" << c << "], shape=[" << rows << "x" << columns
                << "] block: " << block_size << ", symmetric: " << symmetric << ", columnwise: " << columnwise;
          }
        }
      }
 
      for (int c = 0; c < meta_cols; c++) {
        for (int r = 0; r < meta_rows; r++) {
          int idx = c * meta_rows + r;
          ASSERT_TRUE(FloatEqual(qdq_scales_T[idx], scales[idx]))
              << ", qdq index=" << r << "x" << c << ", shape=[" << rows << "x" << columns
              << "] block: " << block_size << ", symmetric: " << symmetric << ", columnwise: " << columnwise;
        }
      }
 
      if (symmetric) return;
      for (int c = 0; c < meta_cols; c++) {
        for (int r = 0; r < meta_rows; r += packSize) {
          int idx = c * ((meta_rows + packSize - 1) / packSize) + r / packSize;
          for (int l = 0; l < packSize && r + l < meta_rows; ++l) {
            ASSERT_EQ(GetElem<qbits>(qdq_zp_T[idx], l), GetElem<qbits>(zp[idx], l))
                << ", qdq index=" << r + l << "x" << c << ", shape=[" << rows << "x" << columns
                << "] block: " << block_size << ", symmetric: " << symmetric << ", columnwise: " << columnwise;
          }
        }
      }
    }
  }
 
 public:
  static const char* GetTestSuiteName() {
    static const std::string suite_name("BlockQ" + std::to_string(qbits));
    return suite_name.c_str();
  }
 
  void ExecuteShort(void) override {
    Test(20, 1, 32, true, false);
    Test(20, 1, 32, true, true);
    Test(1, 20, 32, false, false);
    Test(1, 20, 32, false, true);
    Test(52, 1, 32, true, false);
    Test(52, 1, 32, true, true);
    Test(1, 52, 32, false, false);
    Test(1, 52, 32, false, true);
    Test(20, 3, 32, true, false);
    Test(20, 3, 32, true, true);
    Test(3, 20, 32, false, false);
    Test(3, 20, 32, false, true);
    Test(52, 3, 32, true, false);
    Test(52, 3, 32, true, true);
    Test(3, 52, 32, false, false);
    Test(3, 52, 32, false, true);
    Test(52, 3, 64, true, false);
    Test(52, 3, 64, true, true);
    Test(3, 52, 64, false, false);
    Test(3, 52, 64, false, true);
    Test(32 * 9 + 17, 41, 32, true, false);
    Test(32 * 9 + 17, 41, 32, true, true);
    Test(41, 32 * 9 + 17, 32, false, false);
    Test(41, 32 * 9 + 17, 32, false, true);
    Test(32 * 9 + 17, 41, 64, true, false);
    Test(32 * 9 + 17, 41, 64, true, true);
    Test(41, 32 * 9 + 17, 64, false, false);
    Test(41, 32 * 9 + 17, 64, false, true);
    Test(32 * 15 + 17, 63, 128, true, false);
    Test(32 * 15 + 17, 63, 128, true, true);
    Test(63, 32 * 15 + 17, 128, false, false);
    Test(63, 32 * 15 + 17, 128, false, true);
 
    Test(256, 256, 32, true, false);
    Test(256, 256, 32, true, true);
    Test(256, 256, 32, false, false);
    Test(256, 256, 32, false, true);
  }
 
  MlasBlockwiseQdqTest() = default;
};
 
static UNUSED_VARIABLE bool added_to_main = AddTestRegister([](bool is_short_execute) {
  size_t count = 0;
  if (is_short_execute) {
    count += MlasDirectShortExecuteTests<MlasBlockwiseQdqTest<float, 2>>::RegisterShortExecute();
    count += MlasDirectShortExecuteTests<MlasBlockwiseQdqTest<float, 4>>::RegisterShortExecute();
    count += MlasDirectShortExecuteTests<MlasBlockwiseQdqTest<float, 8>>::RegisterShortExecute();
  }
  return count;
});
 
TEST(MlasBlockwiseQdqTest, RejectsShapesOutsideInt32IndexDomain) {
  float value = 0.0f;
  uint8_t packed_value = 0;
 
  EXPECT_FALSE(MlasQDQBlockwiseShapeIsValid(2147483648LL, 1, 16, 4, false, false));
  EXPECT_FALSE(MlasQDQBlockwiseShapeIsValid(2147483632LL, 1, 16, 4, true, false));
  EXPECT_THROW((MlasQDQQuantizeBlockwise<float, 4>(
                   &value, &value, nullptr, &packed_value, true, 46341, 46341, 16, nullptr)),
               onnxruntime::OnnxRuntimeException);
  EXPECT_THROW((MlasQDQTransposeBlockwiseQuantized<float, 4, true>(
                   &packed_value, &value, nullptr, &packed_value, &value, nullptr,
                   true, 1, 16777216, 256, nullptr)),
               onnxruntime::OnnxRuntimeException);
}
 
#endif  // ORT_MINIMAL_BUILD