Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "mlas_q4.h"
#include "mlas_qnbit.h"
 
#include <memory>
#include <sstream>
#include <stdexcept>
#include <vector>
#include <type_traits>
 
#include "benchmark/benchmark.h"
 
#include "bench_util.h"
#include "core/common/narrow.h"
#include "core/util/thread_utils.h"
#include "core/platform/env_var_utils.h"
 
template <typename AType, size_t BlkBitWidth>
void RunQNBitGemmBenchmark(size_t BlkLen,
                           size_t M, size_t N, size_t K,
                           size_t Threads,
                           bool Symmetric,
                           bool HasBias,
                           MLAS_QNBIT_GEMM_COMPUTE_TYPE ComputeType,
                           benchmark::State& state) {
  if (!MlasIsQNBitGemmAvailable(BlkBitWidth, BlkLen, ComputeType)) {
    state.SkipWithMessage("QNBitGemm is not available with the given configuration on the current machine.");
    return;
  }
 
  size_t QuantBDataSizeInBytes, QuantBScaleSize, QuantBZeroPointSizeInBytes;
  MlasBlockwiseQuantizedBufferSizes<BlkBitWidth>(
      static_cast<int>(BlkLen), /* columnwise */ true,
      static_cast<int>(K), static_cast<int>(N),
      QuantBDataSizeInBytes, QuantBScaleSize, &QuantBZeroPointSizeInBytes);
 
  OrtThreadPoolParams tpo;
  tpo.thread_pool_size = static_cast<int>(Threads);
  tpo.auto_set_affinity = true;
 
  std::unique_ptr<onnxruntime::concurrency::ThreadPool> tp(
      onnxruntime::concurrency::CreateThreadPool(&onnxruntime::Env::Default(),
                                                 tpo, onnxruntime::concurrency::ThreadPoolType::INTRA_OP));
 
  const auto A = RandomVectorUniform(M * K, AType(-1.0f), AType(1.0f));
  const auto B = RandomVectorUniform(K * N, AType(-1.0f), AType(1.0f));
 
  const auto Bias = HasBias ? RandomVectorUniform(N, AType(-1.0f), AType(1.0f)) : std::vector<AType>();
 
  std::vector<AType> C(static_cast<size_t>(M * N));
 
  std::vector<uint8_t> QuantBData(QuantBDataSizeInBytes);
  std::vector<AType> QuantBScale(QuantBScaleSize);
  std::vector<uint8_t> QuantBZeroPoint(Symmetric ? 0 : QuantBZeroPointSizeInBytes);
  bool has_zp_input = !Symmetric;
 
  MlasQuantizeBlockwise<AType, BlkBitWidth>(QuantBData.data(), QuantBScale.data(),
                                            Symmetric ? nullptr : QuantBZeroPoint.data(),
                                            B.data(), static_cast<int>(BlkLen), /* columnwise */ true,
                                            static_cast<int>(K), static_cast<int>(N), static_cast<int>(N),
                                            tp.get());
 
  std::unique_ptr<std::byte[]> Workspace;
  if (const auto WorkspaceSize = MlasQNBitGemmBatchWorkspaceSize(M, N, K, 1, BlkBitWidth, BlkLen, !Symmetric, ComputeType, nullptr);
      WorkspaceSize > 0) {
    Workspace = std::make_unique<std::byte[]>(WorkspaceSize);
  }
 
  std::unique_ptr<std::byte[]> PackedQuantBData;
  if (const auto PackedQuantBDataSize = MlasQNBitGemmPackQuantBDataSize(N, K, BlkBitWidth, BlkLen, !Symmetric, ComputeType, nullptr);
      PackedQuantBDataSize > 0) {
    PackedQuantBData = std::make_unique<std::byte[]>(PackedQuantBDataSize);
    MlasQNBitGemmPackQuantBData(N, K, BlkBitWidth, BlkLen, ComputeType, QuantBData.data(), PackedQuantBData.get(),
                                QuantBScale.data(), has_zp_input, QuantBZeroPoint.data(),
                                tp.get(), nullptr);
  }
 
  MLAS_QNBIT_GEMM_DATA_PARAMS<AType> params{};
  params.A = A.data();
  params.lda = K;
  if (PackedQuantBData != nullptr)
    params.QuantBDataWorkspace = PackedQuantBData.get();
  else
    params.QuantBDataWorkspace = static_cast<const void*>(QuantBData.data());
 
  params.PackedQuantBData = PackedQuantBData.get();
  params.QuantBScale = QuantBScale.data();
  params.QuantBZeroPoint = Symmetric ? nullptr : QuantBZeroPoint.data();
  params.Bias = HasBias ? Bias.data() : nullptr;
  params.C = C.data();
  params.ldc = N;
 
  // warm up run
  MlasQNBitGemmBatch(M, N, K, 1, BlkBitWidth, BlkLen, ComputeType, &params, Workspace.get(), tp.get(), nullptr);
 
  for (auto _ : state) {
    MlasQNBitGemmBatch(M, N, K, 1, BlkBitWidth, BlkLen, ComputeType, &params, Workspace.get(), tp.get(), nullptr);
  }
}
 
template <typename AType, size_t BlkBitWidth>
void QNBITGEMM(benchmark::State& state) {
  using onnxruntime::narrow;
 
  const auto BlkLen = narrow<size_t>(state.range(0));
  const auto M = narrow<size_t>(state.range(1));
  const auto N = narrow<size_t>(state.range(2));
  const auto K = narrow<size_t>(state.range(3));
  const auto Threads = narrow<size_t>(state.range(4));
  const auto Symmetric = narrow<bool>(state.range(5));
  const bool HasBias = narrow<bool>(state.range(6));
  const auto ComputeType = static_cast<MLAS_QNBIT_GEMM_COMPUTE_TYPE>(state.range(7));
 
  RunQNBitGemmBenchmark<AType, BlkBitWidth>(BlkLen, M, N, K, Threads, Symmetric, HasBias, ComputeType, state);
}
 
template <typename AType>
static void QNBitGemmArgs(benchmark::Benchmark* b) {
  b->ArgNames({"BlkLen", "M", "N", "K", "Threads", "Symmetric", "HasBias", "ComputeType"});
 
  b->ArgsProduct({
      {128},                            // BlkLen
      {1, 4096},                        // M
      {4096, 11008},                    // N
      {4096, 11008},                    // K
      {1, 8},                           // Threads
      {int64_t{false}, int64_t{true}},  // Symmetric
      {int64_t{false}, int64_t{true}},  // HasBias
      std::is_same_v<AType, MLAS_FP16>
          ? std::vector<int64_t>{int64_t{HQNBIT_CompFp16}}
          : std::vector<int64_t>{int64_t{SQNBIT_CompFp32}, int64_t{SQNBIT_CompInt8}},  // ComputeType
  });
}
 
// Standard sweep for the native W2 kernel. W2 has fewer free dimensions
// than W4 (symmetric-only, SQNBIT_CompInt8 only), so the grid uses fixed
// values for those axes and sweeps the rest like QNBitGemmArgs.
static void QNBit2BitArgs(benchmark::Benchmark* b) {
  b->ArgNames({"BlkLen", "M", "N", "K", "Threads", "Symmetric", "HasBias", "ComputeType"});
 
  b->ArgsProduct({
      {32, 64, 128},                    // BlkLen      (W2 native kernel supports all three)
      {1, 4096},                        // M           (decode + prefill)
      {4096, 11008},                    // N
      {4096, 11008},                    // K
      {1, 8},                           // Threads
      {int64_t{true}},                  // Symmetric   (W2 native kernel constraint)
      {int64_t{false}, int64_t{true}},  // HasBias
      {int64_t{SQNBIT_CompInt8}},       // ComputeType (W2 native kernel constraint)
  });
}
BENCHMARK(QNBITGEMM<float, 4>)->Apply(QNBitGemmArgs<float>)->UseRealTime();
BENCHMARK(QNBITGEMM<float, 8>)->Apply(QNBitGemmArgs<float>)->UseRealTime();
BENCHMARK(QNBITGEMM<MLAS_FP16, 4>)->Apply(QNBitGemmArgs<MLAS_FP16>)->UseRealTime();
BENCHMARK(QNBITGEMM<float, 2>)->Apply(QNBit2BitArgs)->UseRealTime();
 
// Representative MatMulNBits shapes mirrored at 4-bit for a head-to-head
// comparison vs the W2 LUT path (LUTGEMM_COMPUTE/RealisticShapes). These
// shapes use BlkLen=64 and reflect proportions that appear in real W2
// production models. Five distinct (K, N) pairs:
//   (K=384,  N=1024): 20 nodes
//   (K=1024, N=192):  40 nodes
//   (K=1024, N=384):  20 nodes
//   (K=1024, N=4096): 20 nodes
//   (K=4096, N=1024): 20 nodes
// Both M=1 (decode) and M=128 (prefill) are exercised — paired with the W2
// rows below so we get a 3-way (W2 / W4 / W8) comparison at each M.
static void QNBitGemmRealisticShapesArgs(benchmark::Benchmark* b) {
  b->ArgNames({"BlkLen", "M", "N", "K", "Threads", "Symmetric", "HasBias", "ComputeType"});
  const int64_t BlkLen = 64;
  const int64_t Threads = 8;
  const int64_t Symmetric = 1;
  const int64_t HasBias = 1;
  for (int64_t M : {int64_t{1}, int64_t{128}}) {
    for (auto kn : {std::pair<int64_t, int64_t>{384, 1024},
                    std::pair<int64_t, int64_t>{1024, 192},
                    std::pair<int64_t, int64_t>{1024, 384},
                    std::pair<int64_t, int64_t>{1024, 4096},
                    std::pair<int64_t, int64_t>{4096, 1024}}) {
      for (int64_t ct : {int64_t{SQNBIT_CompFp32}, int64_t{SQNBIT_CompInt8}}) {
        b->Args({BlkLen, M, kn.second, kn.first, Threads, Symmetric, HasBias, ct});
      }
    }
  }
}
 
BENCHMARK(QNBITGEMM<float, 4>)->Apply(QNBitGemmRealisticShapesArgs)->UseRealTime();
 
// 2-bit weight rows for the same representative shapes. Exercises the AVX-512
// W2 native path (VNNI variant on AVX-512-VNNI hosts; non-VNNI variant on
// AVX-512BW hosts). W2 is registered only for SQNBIT_CompInt8 and BlkLen=64,
// so we emit just that one ComputeType. Covers both M=1 (decode) and M=128
// (prefill).
static void QNBit2BitRealisticShapesArgs(benchmark::Benchmark* b) {
  b->ArgNames({"BlkLen", "M", "N", "K", "Threads", "Symmetric", "HasBias", "ComputeType"});
  const int64_t BlkLen = 64;
  const int64_t Threads = 8;
  const int64_t Symmetric = 1;  // W2 native path is symmetric-only.
  const int64_t HasBias = 1;
  for (int64_t M : {int64_t{1}, int64_t{128}}) {
    for (auto kn : {std::pair<int64_t, int64_t>{384, 1024},
                    std::pair<int64_t, int64_t>{1024, 192},
                    std::pair<int64_t, int64_t>{1024, 384},
                    std::pair<int64_t, int64_t>{1024, 4096},
                    std::pair<int64_t, int64_t>{4096, 1024}}) {
      b->Args({BlkLen, M, kn.second, kn.first, Threads, Symmetric, HasBias, int64_t{SQNBIT_CompInt8}});
    }
  }
}
 
BENCHMARK(QNBITGEMM<float, 2>)->Apply(QNBit2BitRealisticShapesArgs)->UseRealTime();
 
// This test gets benchmark arguments from environment variables.
template <typename AType, size_t BlkBitWidth>
void QNBITGEMM_ENV(benchmark::State& state) {
  using onnxruntime::ParseEnvironmentVariableWithDefault;
 
  const auto BlkLen = ParseEnvironmentVariableWithDefault<size_t>("ORT_QNBITGEMM_BLKLEN", 32);
  const auto M = ParseEnvironmentVariableWithDefault<size_t>("ORT_QNBITGEMM_M", 1);
  const auto N = ParseEnvironmentVariableWithDefault<size_t>("ORT_QNBITGEMM_N", 4096);
  const auto K = ParseEnvironmentVariableWithDefault<size_t>("ORT_QNBITGEMM_K", 4096);
  const auto Threads = ParseEnvironmentVariableWithDefault<size_t>("ORT_QNBITGEMM_THREADS", 1);
  const auto Symmetric = ParseEnvironmentVariableWithDefault<bool>("ORT_QNBITGEMM_SYMMETRIC", true);
  const auto HasBias = ParseEnvironmentVariableWithDefault<bool>("ORT_QNBITGEMM_HAS_BIAS", false);
  const auto ComputeType = ParseEnvironmentVariableWithDefault<int32_t>("ORT_QNBITGEMM_COMPUTE_TYPE",
                                                                        static_cast<int32_t>(SQNBIT_CompFp32));
 
  RunQNBitGemmBenchmark<AType, BlkBitWidth>(BlkLen, M, N, K, Threads, Symmetric, HasBias,
                                            static_cast<MLAS_QNBIT_GEMM_COMPUTE_TYPE>(ComputeType),
                                            state);
 
  std::ostringstream s;
  s << "BlkBitWidth:" << BlkBitWidth << "/BlkLen:" << BlkLen
    << "/M:" << M << "/N:" << N << "/K:" << K
    << "/Threads:" << Threads << "/Symmetric:" << Symmetric << "/HasBias:" << HasBias
    << "/ComputeType:" << ComputeType;
  state.SetLabel(s.str());
}
 
BENCHMARK(QNBITGEMM_ENV<float, 4>)->UseRealTime();