Repository navigation
Expand file tree
/
Copy pathmmq_paths_test.cu
More file actions
250 lines (238 loc) · 14.5 KB
/
Copy pathmmq_paths_test.cu
File metadata and controls
250 lines (238 loc) · 14.5 KB
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
// src/poly/mmq_paths_test.cu - Mmq::quantize + Mmq::mul against a double-precision product of ggml's own dequantized
// weights, for the types strata-poly's models keep matrices in: one dense matrix, and four experts of a launch with
// uneven rows, an empty one, and their products scattered to other rows (bounds and ids, the bounds mirrored on the
// host as the engine does). The path is the card's (int8 tiles, fp16 tiles, or the BLAS path; STRATA_POLY_PROMPT_BLAS=1
// forces the BLAS path), so a run prints that path's error: run it both ways to compare them. The activations are
// normal values with a few channels 30 times larger, and rows of different sizes, as a transformer's are. The int8
// path is checked a second time against the same weights times the rows rounded as its quantizer rounds them, where
// only the float rounding of its sums may differ. Then the same products with the memory Mmq::work() asks for given
// beforehand (Mmq::give(), as Engine::alloc() does): the same numbers, made in those buffers. Where the path reads
// rows by index (Mmq::reads_by_index()), the experts' product from rows quantized once and read by index is the one
// from the same rows gathered by ids, bit for bit. On an RTX 50 card NVFP4 takes the two-term FP4 products
// (nvfp4_x2.hpp) unless STRATA_PREFILL_NVFP4 names another mode: run it with STRATA_PREFILL_NVFP4=fp16 too.
//
// usage: poly_mmq_paths_test [--turing-int8] (--turing-int8: the int8 path on Turing too, gemm_iq_i8_turing(); exit 0: every product within 2% RMS of the reference, and an int8 product within
// 1e-5 RMS of its own numbers; 77: no card)
#include "mmq.hpp"
#include "ggml.h"
#include "strata/kernels/iq_kernels.hpp"
#include <cublas_v2.h>
#include <cuda_runtime.h>
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <random>
#include <vector>
using strata::poly::Mmq;
namespace {
bool ok(cudaError_t e, const char* what) {
if (e != cudaSuccess) std::printf("%s: %s\n", what, cudaGetErrorString(e));
return e == cudaSuccess;
}
template <typename T> T* upload(const std::vector<T>& v) {
T* d = nullptr;
if (cudaMalloc(&d, v.size() * sizeof(T)) != cudaSuccess) return nullptr;
cudaMemcpy(d, v.data(), v.size() * sizeof(T), cudaMemcpyHostToDevice);
return d;
}
struct Err { double rms = 0, top = 0; };
// y against ref over the rows listed (each `cols` long, ld apart)
Err compare(const std::vector<float>& y, const std::vector<double>& ref, int64_t rows, int64_t cols, int64_t ld) {
double se = 0, sr = 0, me = 0, mr = 0;
for (int64_t r = 0; r < rows; ++r)
for (int64_t c = 0; c < cols; ++c) {
const double e = (double) y[(size_t) (r * ld + c)] - ref[(size_t) (r * cols + c)], v = ref[(size_t) (r * cols + c)];
se += e * e;
sr += v * v;
me = std::max(me, std::fabs(e));
mr = std::max(mr, std::fabs(v));
}
return {std::sqrt(se / std::max(sr, 1e-300)), me / std::max(mr, 1e-300)};
}
} // namespace
int main(int argc, char** argv) {
if (argc > 1 && std::strcmp(argv[1], "--turing-int8") == 0) strata::kernels::gemm_iq_i8_turing(true);
int cards = 0;
if (cudaGetDeviceCount(&cards) != cudaSuccess || cards == 0) {
std::puts("poly_mmq_paths_test: no card, skipped");
return 77;
}
const char* forced = std::getenv("STRATA_POLY_PROMPT_BLAS");
std::printf("path: %s\n", forced && forced[0] == '1' ? "BLAS (STRATA_POLY_PROMPT_BLAS=1)" : "the card's");
// the 32-value types, the K-quants, the i-quants, NVFP4
const int types[] = {2, 6, 7, 8, 10, 11, 12, 13, 14, 16, 17, 18, 20, 21, 22, 23, 29, 40};
constexpr int64_t kOut = 512, kIn = 2048, kTokens = 256, kExperts = 4;
std::mt19937 rng(99);
std::normal_distribution<float> normal(0.f, 1.f);
// activations: a scale a row, a few outlier channels
std::vector<float> x((size_t) (kTokens * kIn));
std::vector<float> ch((size_t) kIn, 1.f);
for (int64_t c = 0; c < kIn; c += 97) ch[(size_t) c] = 30.f;
for (int64_t r = 0; r < kTokens; ++r) {
const float s = std::exp(normal(rng));
for (int64_t c = 0; c < kIn; ++c) x[(size_t) (r * kIn + c)] = normal(rng) * s * ch[(size_t) c];
}
// the experts' rows: 100, 0, 7 and the rest; row i's product goes to row perm[i]
const int32_t bounds_h[kExperts + 1] = {0, 100, 100, 107, (int32_t) kTokens};
std::vector<int32_t> perm((size_t) kTokens), src((size_t) kTokens);
for (int64_t i = 0; i < kTokens; ++i) perm[(size_t) i] = (int32_t) ((i * 37 + 11) % kTokens);
for (int64_t i = 0; i < kTokens; ++i) src[(size_t) i] = (int32_t) ((i * 7 + 3) % 61); // a token's row read by several
// the activations as the int8 path quantizes them (gemm_iq_i8_quantize: a block of 32 values, d_inv = 127 / max|x|,
// code = round(x d_inv), scale = 1 / d_inv), in double: its products are sums of these times the weights, so
// against them it is exact but for the float rounding of its sums
std::vector<double> xi8(x.size());
for (size_t b = 0; b < x.size(); b += 32) {
float amax = 0.f;
for (size_t j = 0; j < 32; ++j) amax = std::max(amax, std::fabs(x[b + j]));
const float d_inv = 127.0f / amax, scale = amax == 0.f ? 0.f : 1.0f / d_inv;
for (size_t j = 0; j < 32; ++j) xi8[b + j] = amax == 0.f ? 0.0 : (double) std::round(x[b + j] * d_inv) * scale;
}
float* dx = upload(x);
int32_t* dperm = upload(perm);
int32_t* dsrc = upload(src);
int32_t* dbounds = upload(std::vector<int32_t>(bounds_h, bounds_h + kExperts + 1));
if (!dx || !dperm || !dsrc || !dbounds) return 1;
Mmq mmq;
mmq.mirror(dbounds, bounds_h, sizeof bounds_h);
cublasHandle_t blas = nullptr; // the caller's, as the engine's is (Mmq::give())
if (cublasCreate(&blas) != CUBLAS_STATUS_SUCCESS) return 1;
void* xq = nullptr;
float* dy = nullptr;
if (!ok(cudaMalloc(&xq, Mmq::q8_bytes(kTokens, kIn)), "cudaMalloc") ||
!ok(cudaMalloc(&dy, (size_t) (kTokens * kOut) * sizeof(float)), "cudaMalloc"))
return 1;
int bad = 0;
for (int t : types) {
const auto ty = (ggml_type) t;
if (!Mmq::fits(t, kOut, kIn)) {
std::printf("%-8s no product on this card\n", ggml_type_name(ty));
continue;
}
ggml_quantize_init(ty);
const size_t mat = ggml_row_size(ty, kIn) * kOut;
std::vector<float> wf((size_t) (kExperts * kOut * kIn)), imp((size_t) kIn);
for (auto& v : wf) v = normal(rng) * 0.02f;
for (auto& v : imp) v = 0.5f + std::fabs(normal(rng));
std::vector<uint8_t> wq(mat * kExperts);
ggml_quantize_chunk(ty, wf.data(), wq.data(), 0, kExperts * kOut, kIn, imp.data());
std::vector<float> wd(wf.size());
ggml_get_type_traits(ty)->to_float(wq.data(), wd.data(), (int64_t) wd.size());
uint8_t* dw = upload(wq);
if (!dw) return 1;
// row i of a launch times its expert's matrix, in double: the rows as they are, or as the int8 path has them
auto reference = [&](int64_t row, int e, bool as_int8, double* out) {
const float* a = &x[(size_t) (row * kIn)];
const double* a8 = &xi8[(size_t) (row * kIn)];
for (int64_t o = 0; o < kOut; ++o) {
const float* w = &wd[(size_t) ((e * kOut + o) * kIn)];
double s = 0;
for (int64_t k = 0; k < kIn; ++k) s += (as_int8 ? a8[k] : (double) a[k]) * w[k];
out[o] = s;
}
};
const bool int8 = Mmq::int8(t, kIn);
std::vector<double> ref((size_t) (kTokens * kOut)), ref8(int8 ? ref.size() : 0);
std::vector<float> y((size_t) (kTokens * kOut));
// one dense matrix: every row, in place
Mmq::quantize(dx, nullptr, xq, t, kIn, kIn, kTokens, nullptr);
mmq.mul(dw, t, kOut, kIn, 0, 1, xq, nullptr, nullptr, kTokens, kTokens, dy, kOut, nullptr);
if (!ok(cudaMemcpy(y.data(), dy, y.size() * sizeof(float), cudaMemcpyDeviceToHost), "dense product")) return 1;
for (int64_t r = 0; r < kTokens; ++r) {
reference(r, 0, false, &ref[(size_t) (r * kOut)]);
if (int8) reference(r, 0, true, &ref8[(size_t) (r * kOut)]);
}
const Err dense = compare(y, ref, kTokens, kOut, kOut);
const Err dense8 = int8 ? compare(y, ref8, kTokens, kOut, kOut) : Err{};
// four experts: rows [bounds[e], bounds[e + 1]) of xq (row i of xq is row i of x), product of row i to row perm[i]
cudaMemset(dy, 0, y.size() * sizeof(float));
Mmq::quantize(dx, nullptr, xq, t, kIn, kIn, kTokens, nullptr);
mmq.mul(dw, t, kOut, kIn, mat, kExperts, xq, dbounds, dperm, kTokens, 149, dy, kOut, nullptr);
if (!ok(cudaMemcpy(y.data(), dy, y.size() * sizeof(float), cudaMemcpyDeviceToHost), "experts' product")) return 1;
std::vector<float> back((size_t) (kTokens * kOut));
for (int e = 0; e < kExperts; ++e)
for (int64_t r = bounds_h[e]; r < bounds_h[e + 1]; ++r) {
reference(r, e, false, &ref[(size_t) (r * kOut)]);
if (int8) reference(r, e, true, &ref8[(size_t) (r * kOut)]);
std::copy_n(&y[(size_t) perm[(size_t) r] * kOut], kOut, &back[(size_t) (r * kOut)]);
}
const Err experts = compare(back, ref, kTokens, kOut, kOut);
const Err experts8 = int8 ? compare(back, ref8, kTokens, kOut, kOut) : Err{};
// The int8 path against its own numbers: a weight decoded wrong anywhere shows as an error of 1e-3 or more of
// the products' size; the float rounding of the sums is about 1e-7
bool fine = dense.rms < 0.02 && experts.rms < 0.02 && dense8.rms < 1e-5 && experts8.rms < 1e-5;
std::printf("%-8s dense: RMS error %.2e, largest %.2e experts: RMS error %.2e, largest %.2e", ggml_type_name(ty),
dense.rms, dense.top, experts.rms, experts.top);
if (int8)
std::printf(" int8 numbers: RMS error %.2e / %.2e, largest %.2e / %.2e", dense8.rms, experts8.rms, dense8.top,
experts8.top);
std::printf(" %s\n", fine ? "ok" : "WRONG");
if (Mmq::reads_by_index(t, kIn)) {
// routed row i is token src[i]: its rows gathered and quantized a row each, against the tokens' rows
// quantized once and read by index
std::vector<float> y1(y.size()), y2(y.size());
cudaMemset(dy, 0, y.size() * sizeof(float));
Mmq::quantize(dx, dsrc, xq, t, kIn, kIn, kTokens, nullptr);
mmq.mul(dw, t, kOut, kIn, mat, kExperts, xq, dbounds, dperm, kTokens, 149, dy, kOut, nullptr);
if (!ok(cudaMemcpy(y1.data(), dy, y1.size() * sizeof(float), cudaMemcpyDeviceToHost), "gathered rows")) return 1;
cudaMemset(dy, 0, y.size() * sizeof(float));
Mmq::quantize(dx, nullptr, xq, t, kIn, kIn, 61, nullptr);
mmq.mul(dw, t, kOut, kIn, mat, kExperts, xq, dbounds, dperm, 61, 149, dy, kOut, nullptr, dsrc);
if (!ok(cudaMemcpy(y2.data(), dy, y2.size() * sizeof(float), cudaMemcpyDeviceToHost), "rows by index")) return 1;
const bool same = std::memcmp(y1.data(), y2.data(), y1.size() * sizeof(float)) == 0;
std::printf("%-8s rows read by index: %s\n", ggml_type_name(ty), same ? "same products" : "OTHER PRODUCTS");
fine = fine && same;
}
// Given the memory work() asks for, the products come out the same, and in it: buffers full of 0xff that a
// product which made its own memory would leave as they were. (None is asked where the int8 tiles take it.)
const Mmq::Work one = Mmq::work(t, kOut, kIn, kTokens, false, kOut);
const Mmq::Work four = Mmq::work(t, kOut, kIn, kTokens, true, kOut);
const size_t floats = std::max(one.floats, four.floats), halves = std::max(one.halves, four.halves);
float* gy = nullptr;
void* gdq = nullptr;
if ((floats && !ok(cudaMalloc(&gy, floats * sizeof(float)), "cudaMalloc")) ||
(halves && !ok(cudaMalloc(&gdq, halves * 2), "cudaMalloc")))
return 1;
if (gy) cudaMemset(gy, 0xff, floats * sizeof(float));
if (gdq) cudaMemset(gdq, 0xff, halves * 2);
{
Mmq given;
given.mirror(dbounds, bounds_h, sizeof bounds_h);
given.give(gy, floats, gdq, halves, blas);
std::vector<float> y2(y.size()), y3(y.size());
Mmq::quantize(dx, nullptr, xq, t, kIn, kIn, kTokens, nullptr);
given.mul(dw, t, kOut, kIn, 0, 1, xq, nullptr, nullptr, kTokens, kTokens, dy, kOut, nullptr);
if (!ok(cudaMemcpy(y2.data(), dy, y2.size() * sizeof(float), cudaMemcpyDeviceToHost), "dense product")) return 1;
cudaMemset(dy, 0, y.size() * sizeof(float));
given.mul(dw, t, kOut, kIn, mat, kExperts, xq, dbounds, dperm, kTokens, 149, dy, kOut, nullptr);
if (!ok(cudaMemcpy(y3.data(), dy, y3.size() * sizeof(float), cudaMemcpyDeviceToHost), "experts' product"))
return 1;
// y has the experts' product of the instance that made its own memory; its dense one again, for this
std::vector<float> dense_y((size_t) (kTokens * kOut));
mmq.mul(dw, t, kOut, kIn, 0, 1, xq, nullptr, nullptr, kTokens, kTokens, dy, kOut, nullptr);
if (!ok(cudaMemcpy(dense_y.data(), dy, dense_y.size() * sizeof(float), cudaMemcpyDeviceToHost), "dense product"))
return 1;
uint32_t y0 = 0xffffffffu;
uint16_t q0 = 0xffff;
if (gy) cudaMemcpy(&y0, gy, sizeof y0, cudaMemcpyDeviceToHost);
if (gdq) cudaMemcpy(&q0, gdq, sizeof q0, cudaMemcpyDeviceToHost);
const bool same = std::equal(y2.begin(), y2.end(), dense_y.begin()) && std::equal(y3.begin(), y3.end(), y.begin());
// (the BLAS path takes both for the experts: a work() that asks for none would pass the rest)
const bool blas_path = forced && forced[0] == '1';
const bool used = (!gy || y0 != 0xffffffffu) && (!gdq || q0 != 0xffff) && (!blas_path || (gy && gdq));
std::printf("%-8s given %zu KiB of floats, %zu KiB of halves: %s, %s\n", ggml_type_name(ty), floats * 4 >> 10,
halves * 2 >> 10, same ? "same products" : "OTHER PRODUCTS", used ? "made in them" : "NOT IN THEM");
fine = fine && same && used;
}
cudaFree(gy);
cudaFree(gdq);
bad += !fine;
cudaFree(dw);
}
ggml_quantize_free();
cublasDestroy(blas);
std::printf("%s\n", bad ? "FAIL" : "PASS");
return bad ? 1 : 0;
}