ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / tests / test-backend-ops.cpp
| 1 | // This file defines tests for various GGML ops and backends. |
| 2 | // For the forward pass it asserts that the results of multiple backends computing the same GGML ops are consistent. |
| 3 | // For the backward pass it asserts that the gradients from backpropagation are consistent |
| 4 | // with the gradients obtained via the method of finite differences ("grad" mode, this is optional). |
| 5 | // It is also possible to check the performance ("perf" mode). |
| 6 | // |
| 7 | // this file has three sections: Section 1 does general setup, section 2 defines the GGML ops to be tested, |
| 8 | // and section 3 defines which tests to run. |
| 9 | // Quick start for adding a new GGML op: Go to section 2 and create a struct that inherits from test_case, |
| 10 | // then go to section 3 and add an instantiation of your struct. |
| 11 | |
| 12 | |
| 13 | // ############################## |
| 14 | // ## Section 1: General Setup ## |
| 15 | // ############################## |
| 16 | |
| 17 | |
| 18 | #include "ggml.h" |
| 19 | #include "ggml-alloc.h" |
| 20 | #include "ggml-backend.h" |
| 21 | #include "ggml-cpp.h" |
| 22 | |
| 23 | #include <algorithm> |
| 24 | #include <atomic> |
| 25 | #include <array> |
| 26 | #include <cfloat> |
| 27 | #include <cinttypes> |
| 28 | #include <cstdarg> |
| 29 | #include <cstdint> |
| 30 | #include <cstdio> |
| 31 | #include <cstdlib> |
| 32 | #include <cstring> |
| 33 | #include <ctime> |
| 34 | #include <future> |
| 35 | #include <fstream> |
| 36 | #include <memory> |
| 37 | #include <mutex> |
| 38 | #include <random> |
| 39 | #include <regex> |
| 40 | #include <set> |
| 41 | #include <sstream> |
| 42 | #include <string> |
| 43 | #include <string_view> |
| 44 | #include <thread> |
| 45 | #include <vector> |
| 46 | #include <unordered_map> |
| 47 | |
| 48 | #ifdef __EMSCRIPTEN__ |
| 49 | # define N_THREADS 1 |
| 50 | #else |
| 51 | # define N_THREADS std::thread::hardware_concurrency() |
| 52 | #endif |
| 53 | |
| 54 | static void init_tensor_uniform(ggml_tensor * tensor, float min = -1.0f, float max = 1.0f) { |
| 55 | if (ggml_is_empty(tensor)) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | size_t nels = ggml_nelements(tensor); |
| 60 | std::vector<float> data(nels); |
| 61 | { |
| 62 | // parallel initialization |
| 63 | static const size_t n_threads = std::max<size_t>(1, std::min<size_t>(nels/1024, std::min<size_t>(4, N_THREADS/2))); |
| 64 | |
| 65 | auto init_thread = [&](size_t start, size_t end) { |
| 66 | thread_local std::default_random_engine gen(std::random_device{}()); |
| 67 | std::uniform_real_distribution<float> distribution(min, max); |
| 68 | for (size_t i = start; i < end; i++) { |
| 69 | data[i] = distribution(gen); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | if (n_threads == 1) { |
| 74 | init_thread(0, nels); |
| 75 | } else { |
| 76 | std::vector<std::future<void>> tasks; |
| 77 | tasks.reserve(n_threads); |
| 78 | for (size_t i = 0; i < n_threads; i++) { |
| 79 | size_t start = i*nels/n_threads; |
| 80 | size_t end = (i+1)*nels/n_threads; |
| 81 | tasks.push_back(std::async(std::launch::async, init_thread, start, end)); |
| 82 | } |
| 83 | for (auto & t : tasks) { |
| 84 | t.get(); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (tensor->type == GGML_TYPE_F32 || tensor->type == GGML_TYPE_I32) { |
| 90 | ggml_backend_tensor_set(tensor, data.data(), 0, nels * sizeof(float)); |
| 91 | } else if (ggml_is_quantized(tensor->type) || tensor->type == GGML_TYPE_F8_E4M3 || tensor->type == GGML_TYPE_F16 || tensor->type == GGML_TYPE_BF16) { |
| 92 | GGML_ASSERT(nels % ggml_blck_size(tensor->type) == 0); |
| 93 | |
| 94 | // dummy importance matrix |
| 95 | std::vector<float> imatrix(tensor->ne[0], 1.0f); |
| 96 | const float * im = imatrix.data(); |
| 97 | if (!ggml_quantize_requires_imatrix(tensor->type)) { |
| 98 | // when the imatrix is optional, we want to test both quantization with and without imatrix |
| 99 | // use one of the random numbers to decide |
| 100 | if (data[0] > 0.5f*(min + max)) { |
| 101 | im = nullptr; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | std::vector<uint8_t> dataq(ggml_row_size(tensor->type, nels)); |
| 106 | { |
| 107 | // parallel quantization by block |
| 108 | size_t blck_size = ggml_blck_size(tensor->type); |
| 109 | size_t n_blocks = nels / blck_size; |
| 110 | |
| 111 | auto quantize_thread = [&](size_t start, size_t end) { |
| 112 | ggml_quantize_chunk(tensor->type, data.data(), dataq.data(), |
| 113 | start * blck_size, end - start, blck_size, im); |
| 114 | }; |
| 115 | |
| 116 | const size_t min_blocks_per_thread = 1; |
| 117 | const size_t n_quant_threads = std::min<size_t>(std::max<size_t>(N_THREADS/2, 1), |
| 118 | std::max<size_t>(1, n_blocks / min_blocks_per_thread)); |
| 119 | |
| 120 | if (n_quant_threads == 1) { |
| 121 | // single-threaded quantization: do all blocks in the current thread |
| 122 | quantize_thread(0, n_blocks); |
| 123 | } else { |
| 124 | std::vector<std::future<void>> tasks; |
| 125 | tasks.reserve(n_quant_threads); |
| 126 | for (size_t i = 0; i < n_quant_threads; i++) { |
| 127 | size_t start = i*n_blocks/n_quant_threads; |
| 128 | size_t end = (i+1)*n_blocks/n_quant_threads; |
| 129 | tasks.push_back(std::async(std::launch::async, quantize_thread, start, end)); |
| 130 | } |
| 131 | for (auto & t : tasks) { |
| 132 | t.get(); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | ggml_backend_tensor_set(tensor, dataq.data(), 0, dataq.size()); |
| 137 | } else if (tensor->type == GGML_TYPE_I8 || tensor->type == GGML_TYPE_I16) { |
| 138 | // This is going to create some weird integers though. |
| 139 | ggml_backend_tensor_set(tensor, data.data(), 0, nels * ggml_type_size(tensor->type)); |
| 140 | } else if (tensor->type == GGML_TYPE_I64) { |
| 141 | // Integers with a size of 8 bytes can be set by mirroring the float data, the specific values are again not really meaningful. |
| 142 | const size_t nbytes_half = nels * sizeof(float); |
| 143 | ggml_backend_tensor_set(tensor, data.data(), 0*nbytes_half, nbytes_half); |
| 144 | ggml_backend_tensor_set(tensor, data.data(), 1*nbytes_half, nbytes_half); |
| 145 | } else { |
| 146 | GGML_ABORT("fatal error"); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // generate an F16 mask where certain blocks are randomly masked with -INF value |
| 151 | static void init_tensor_kq_mask(ggml_tensor * tensor, float min = -1.0f, float max = 1.0f) { |
| 152 | GGML_ASSERT(tensor->type == GGML_TYPE_F16); |
| 153 | |
| 154 | GGML_TENSOR_LOCALS( int32_t, ne, tensor, ne); |
| 155 | |
| 156 | std::vector<float> data_f32(ne0*ne1*ne2*ne3); |
| 157 | std::vector<ggml_fp16_t> data_f16(ne0*ne1*ne2*ne3); |
| 158 | |
| 159 | std::random_device rd; |
| 160 | std::mt19937 gen(rd()); |
| 161 | std::uniform_real_distribution<float> dis(min, max); |
| 162 | |
| 163 | for (size_t i = 0; i < data_f32.size(); i++) { |
| 164 | data_f32[i] = dis(gen); |
| 165 | } |
| 166 | |
| 167 | // block size |
| 168 | const int blck0 = 128; |
| 169 | const int blck1 = 64; |
| 170 | |
| 171 | // number of INF/zero blocks |
| 172 | const int n_inf_zero_blocks = 0.2*(ne0*ne1*ne2*ne3)/(blck0*blck1); |
| 173 | |
| 174 | for (int b = 0; b < n_inf_zero_blocks; b++) { |
| 175 | const int p3 = (rd() % ne3); |
| 176 | const int p2 = (rd() % ne2); |
| 177 | const int p1 = (rd() % ne1); |
| 178 | const int p0 = (rd() % ne0); |
| 179 | |
| 180 | bool inf = rd() & 1; |
| 181 | |
| 182 | for (int i1 = 0; i1 < blck1 && p1 + i1 < ne1; i1++) { |
| 183 | const int idx = p3*ne2*ne1*ne0 + p2*ne1*ne0 + (p1 + i1)*ne0 + p0; |
| 184 | |
| 185 | for (int i0 = 0; i0 < blck0 && p0 + i0 < ne0; i0++) { |
| 186 | data_f32[idx + i0] = inf ? -INFINITY : 0.0f; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | ggml_fp32_to_fp16_row(data_f32.data(), data_f16.data(), ne0*ne1*ne2*ne3); |
| 192 | |
| 193 | ggml_backend_tensor_set(tensor, data_f16.data(), 0, data_f16.size()*sizeof(ggml_fp16_t)); |
| 194 | } |
| 195 | |
| 196 | static void init_tensor_kq_mask_sparse(ggml_tensor * tensor, int64_t n_kv_max) { |
| 197 | GGML_ASSERT(tensor->type == GGML_TYPE_F16); |
| 198 | GGML_ASSERT(n_kv_max > 0 && n_kv_max <= tensor->ne[0]); |
| 199 | |
| 200 | const int64_t ne0 = tensor->ne[0]; |
| 201 | const int64_t nrows = ggml_nrows(tensor); |
| 202 | std::vector<float> data_f32(ggml_nelements(tensor), -INFINITY); |
| 203 | std::vector<ggml_fp16_t> data_f16(ggml_nelements(tensor)); |
| 204 | std::vector<int32_t> order(ne0); |
| 205 | for (int64_t i = 0; i < ne0; ++i) { |
| 206 | order[i] = i; |
| 207 | } |
| 208 | |
| 209 | std::mt19937 gen(0x5A17); |
| 210 | for (int64_t row = 0; row < nrows; ++row) { |
| 211 | std::shuffle(order.begin(), order.end(), gen); |
| 212 | const int64_t count = n_kv_max - row % std::min<int64_t>(n_kv_max, 17); |
| 213 | std::sort(order.begin(), order.begin() + count); |
| 214 | for (int64_t i = 0; i < count; ++i) { |
| 215 | data_f32[row*ne0 + order[i]] = -0.03125f * (1 + (i + row) % 7); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | ggml_fp32_to_fp16_row(data_f32.data(), data_f16.data(), data_f16.size()); |
| 220 | ggml_backend_tensor_set(tensor, data_f16.data(), 0, data_f16.size()*sizeof(ggml_fp16_t)); |
| 221 | } |
| 222 | |
| 223 | // generate a lower triangular matrix |
| 224 | static void init_tensor_tril(ggml_tensor * tensor, float min = -1.0f, float max = 1.0f) { |
| 225 | GGML_ASSERT(tensor->type == GGML_TYPE_F32); |
| 226 | GGML_ASSERT(tensor->ne[0] == tensor->ne[1]); |
| 227 | |
| 228 | GGML_TENSOR_LOCALS(int32_t, ne, tensor, ne); |
| 229 | GGML_TENSOR_LOCALS(size_t, nb, tensor, nb); |
| 230 | |
| 231 | std::vector<float> data_f32(ne0*ne1*ne2*ne3); |
| 232 | |
| 233 | std::random_device rd; |
| 234 | std::mt19937 gen(rd()); |
| 235 | std::uniform_real_distribution<float> dis(min, max); |
| 236 | |
| 237 | for (int64_t i3 = 0; i3 < ne3; i3++) { |
| 238 | for (int64_t i2 = 0; i2 < ne2; i2++) { |
| 239 | for (int64_t i1 = 0; i1 < ne1; i1++) { |
| 240 | for (int64_t i0 = 0; i0 < ne0; i0++) { |
| 241 | int64_t idx = (i0 * nb0 + i1 * nb1 + i2 * nb2 + i3 * nb3) / sizeof(float); |
| 242 | if (i0 <= i1) { |
| 243 | data_f32[idx] = dis(gen); |
| 244 | } else { |
| 245 | data_f32[idx] = 0.0f; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | ggml_backend_tensor_set(tensor, data_f32.data(), 0, ggml_nbytes(tensor)); |
| 253 | } |
| 254 | |
| 255 | static std::vector<float> tensor_to_float(const ggml_tensor * t) { |
| 256 | std::vector<float> tv; |
| 257 | tv.reserve(ggml_nelements(t)); |
| 258 | |
| 259 | std::vector<uint8_t> buf(ggml_nbytes(t)); |
| 260 | ggml_backend_tensor_get(t, buf.data(), 0, ggml_nbytes(t)); |
| 261 | |
| 262 | const auto * tt = ggml_get_type_traits(t->type); |
| 263 | size_t bs = ggml_blck_size(t->type); |
| 264 | std::vector<float> vq(ggml_blck_size(t->type)); |
| 265 | bool uses_type_conversion = ggml_is_quantized(t->type) || t->type == GGML_TYPE_F8_E4M3; |
| 266 | |
| 267 | // access elements by index to avoid gaps in views |
| 268 | for (int64_t i3 = 0; i3 < t->ne[3]; i3++) { |
| 269 | for (int64_t i2 = 0; i2 < t->ne[2]; i2++) { |
| 270 | for (int64_t i1 = 0; i1 < t->ne[1]; i1++) { |
| 271 | for (int64_t i0 = 0; i0 < t->ne[0]; i0 += bs) { |
| 272 | size_t i = i3*t->nb[3] + i2*t->nb[2] + i1*t->nb[1] + i0/bs*t->nb[0]; |
| 273 | if (t->type == GGML_TYPE_F16) { |
| 274 | tv.push_back(ggml_fp16_to_fp32(*(ggml_fp16_t*)&buf[i])); |
| 275 | } else if (t->type == GGML_TYPE_BF16) { |
| 276 | tv.push_back(ggml_bf16_to_fp32(*(ggml_bf16_t*)&buf[i])); |
| 277 | } else if (t->type == GGML_TYPE_F32) { |
| 278 | tv.push_back(*(float *) &buf[i]); |
| 279 | } else if (t->type == GGML_TYPE_I64) { |
| 280 | tv.push_back((float)*(int64_t *) &buf[i]); |
| 281 | } else if (t->type == GGML_TYPE_I32) { |
| 282 | tv.push_back((float)*(int32_t *) &buf[i]); |
| 283 | } else if (t->type == GGML_TYPE_I16) { |
| 284 | tv.push_back((float)*(int16_t *) &buf[i]); |
| 285 | } else if (t->type == GGML_TYPE_I8) { |
| 286 | tv.push_back((float)*(int8_t *) &buf[i]); |
| 287 | } else if (uses_type_conversion) { |
| 288 | tt->to_float(&buf[i], vq.data(), bs); |
| 289 | tv.insert(tv.end(), vq.begin(), vq.end()); |
| 290 | } else { |
| 291 | GGML_ABORT("fatal error"); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | return tv; |
| 299 | } |
| 300 | |
| 301 | // normalized mean squared error = mse(a, b) / mse(a, 0) |
| 302 | static double nmse(const float * a, const float * b, size_t n) { |
| 303 | double mse_a_b = 0.0; |
| 304 | double mse_a_0 = 0.0; |
| 305 | |
| 306 | for (size_t i = 0; i < n; i++) { |
| 307 | float a_i = a[i]; |
| 308 | float b_i = b[i]; |
| 309 | |
| 310 | mse_a_b += (a_i - b_i) * (a_i - b_i); |
| 311 | mse_a_0 += a_i * a_i; |
| 312 | } |
| 313 | |
| 314 | return mse_a_b / mse_a_0; |
| 315 | } |
| 316 | |
| 317 | // difference between 2 sets (Jaccard distance, 0 - no difference, 1 - no overlap) |
| 318 | template <typename T> |
| 319 | static double jdst(const T * a, const T * b, size_t n) { |
| 320 | std::unordered_map<T, size_t> set_a; |
| 321 | std::unordered_map<T, size_t> set_b; |
| 322 | |
| 323 | for (size_t i = 0; i < n; ++i) { |
| 324 | set_a[a[i]]++; |
| 325 | set_b[b[i]]++; |
| 326 | } |
| 327 | |
| 328 | size_t diff = 0; |
| 329 | |
| 330 | for (const auto & p : set_a) { |
| 331 | const int64_t na = p.second; |
| 332 | const int64_t nb = set_b.find(p.first) != set_b.end() ? set_b.at(p.first) : 0; |
| 333 | |
| 334 | diff += std::abs(na - nb); |
| 335 | } |
| 336 | |
| 337 | for (const auto & p : set_b) { |
| 338 | if (set_a.find(p.first) == set_a.end()) { |
| 339 | diff += p.second; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return (double) diff / (2*n); |
| 344 | } |
| 345 | |
| 346 | // maximum absolute asymmetry between a and b |
| 347 | // asymmetry: (a - b) / (a + b) |
| 348 | // This is more stable than relative error if one of the values fluctuates towards zero. |
| 349 | // n: number of values to compare. |
| 350 | // expected_vals: optional vector of expected values for a. If expected_vals is not empty, filter out all comparisons where |
| 351 | // a does not match any of the expected values. Needed for noncontinuous gradients where the numerical calculation can fail. |
| 352 | static double mean_abs_asymm(const float * a, const float * b, const size_t n, const std::vector<float> & expected_vals) { |
| 353 | double sum = 0.0f; |
| 354 | |
| 355 | size_t nvalid = 0; |
| 356 | for (size_t i = 0; i < n; i++) { |
| 357 | if (!expected_vals.empty()) { |
| 358 | bool matches_any = false; |
| 359 | for (const float & ev : expected_vals) { |
| 360 | if (fabsf(a[i] - ev) < 1e-3f) { |
| 361 | matches_any = true; |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | if (!matches_any) { |
| 366 | continue; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | const float asymm = (a[i] - b[i]) / (a[i] + b[i]); |
| 371 | |
| 372 | sum += fabsf(asymm); |
| 373 | nvalid++; |
| 374 | } |
| 375 | |
| 376 | return sum/nvalid; |
| 377 | } |
| 378 | |
| 379 | // utils for printing the variables of the test cases |
| 380 | |
| 381 | static std::string var_to_str(const std::string & x) { |
| 382 | return x; |
| 383 | } |
| 384 | |
| 385 | template<typename T> |
| 386 | static std::string var_to_str(const T & x) { |
| 387 | return std::to_string(x); |
| 388 | } |
| 389 | |
| 390 | template<typename T, size_t N> |
| 391 | static std::string var_to_str(const T (&x)[N]) { |
| 392 | std::string s = "["; |
| 393 | for (size_t i = 0; i < N; i++) { |
| 394 | if (i > 0) { |
| 395 | s += ","; |
| 396 | } |
| 397 | s += var_to_str(x[i]); |
| 398 | } |
| 399 | s += "]"; |
| 400 | return s; |
| 401 | } |
| 402 | |
| 403 | template<typename T, size_t N> |
| 404 | static std::string var_to_str(const std::array<T, N> & x) { |
| 405 | std::string s = "["; |
| 406 | for (size_t i = 0; i < N; i++) { |
| 407 | if (i > 0) { |
| 408 | s += ","; |
| 409 | } |
| 410 | s += var_to_str(x[i]); |
| 411 | } |
| 412 | s += "]"; |
| 413 | return s; |
| 414 | } |
| 415 | |
| 416 | static std::string var_to_str(ggml_type type) { |
| 417 | return ggml_type_name(type); |
| 418 | } |
| 419 | |
| 420 | static std::string var_to_str(ggml_prec prec) { |
| 421 | return prec == GGML_PREC_F32 ? "f32" : "def"; |
| 422 | } |
| 423 | |
| 424 | static std::string var_to_str(ggml_op_pool pool) { |
| 425 | switch (pool) { |
| 426 | case GGML_OP_POOL_AVG: return "avg"; |
| 427 | case GGML_OP_POOL_MAX: return "max"; |
| 428 | default: return std::to_string(pool); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | static std::string var_to_str(ggml_scale_mode mode) { |
| 433 | std::string str; |
| 434 | switch (mode & 0xFF) { |
| 435 | case GGML_SCALE_MODE_NEAREST: str = "nearest"; break; |
| 436 | case GGML_SCALE_MODE_BILINEAR: str = "bilinear"; break; |
| 437 | case GGML_SCALE_MODE_BICUBIC: str = "bicubic"; break; |
| 438 | default: str = std::to_string(mode); break; |
| 439 | } |
| 440 | if (mode & GGML_SCALE_FLAG_ALIGN_CORNERS) { |
| 441 | str += "|align_corners"; |
| 442 | } |
| 443 | if (mode & GGML_SCALE_FLAG_ANTIALIAS) { |
| 444 | str += "|antialias"; |
| 445 | } |
| 446 | return str; |
| 447 | } |
| 448 | |
| 449 | #define VAR_TO_STR(x) (#x "=" + var_to_str(x)) |
| 450 | |
| 451 | #define VARS_TO_STR1(a) VAR_TO_STR(a) |
| 452 | #define VARS_TO_STR2(a, b) VAR_TO_STR(a) + "," + VAR_TO_STR(b) |
| 453 | #define VARS_TO_STR3(a, b, c) VAR_TO_STR(a) + "," + VARS_TO_STR2(b, c) |
| 454 | #define VARS_TO_STR4(a, b, c, d) VAR_TO_STR(a) + "," + VARS_TO_STR3(b, c, d) |
| 455 | #define VARS_TO_STR5(a, b, c, d, e) VAR_TO_STR(a) + "," + VARS_TO_STR4(b, c, d, e) |
| 456 | #define VARS_TO_STR6(a, b, c, d, e, f) VAR_TO_STR(a) + "," + VARS_TO_STR5(b, c, d, e, f) |
| 457 | #define VARS_TO_STR7(a, b, c, d, e, f, g) VAR_TO_STR(a) + "," + VARS_TO_STR6(b, c, d, e, f, g) |
| 458 | #define VARS_TO_STR8(a, b, c, d, e, f, g, h) VAR_TO_STR(a) + "," + VARS_TO_STR7(b, c, d, e, f, g, h) |
| 459 | #define VARS_TO_STR9(a, b, c, d, e, f, g, h, i) VAR_TO_STR(a) + "," + VARS_TO_STR8(b, c, d, e, f, g, h, i) |
| 460 | #define VARS_TO_STR10(a, b, c, d, e, f, g, h, i, j) VAR_TO_STR(a) + "," + VARS_TO_STR9(b, c, d, e, f, g, h, i, j) |
| 461 | #define VARS_TO_STR11(a, b, c, d, e, f, g, h, i, j, k) VAR_TO_STR(a) + "," + VARS_TO_STR10(b, c, d, e, f, g, h, i, j, k) |
| 462 | #define VARS_TO_STR12(a, b, c, d, e, f, g, h, i, j, k, l) VAR_TO_STR(a) + "," + VARS_TO_STR11(b, c, d, e, f, g, h, i, j, k, l) |
| 463 | #define VARS_TO_STR13(a, b, c, d, e, f, g, h, i, j, k, l, m) VAR_TO_STR(a) + "," + VARS_TO_STR12(b, c, d, e, f, g, h, i, j, k, l, m) |
| 464 | #define VARS_TO_STR14(a, b, c, d, e, f, g, h, i, j, k, l, m, n) VAR_TO_STR(a) + "," + VARS_TO_STR13(b, c, d, e, f, g, h, i, j, k, l, m, n) |
| 465 | #define VARS_TO_STR15(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) VAR_TO_STR(a) + "," + VARS_TO_STR14(b, c, d, e, f, g, h, i, j, k, l, m, n, o) |
| 466 | #define VARS_TO_STR16(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) VAR_TO_STR(a) + "," + VARS_TO_STR15(b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) |
| 467 | #define VARS_TO_STR17(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q) VAR_TO_STR(a) + "," + VARS_TO_STR16(b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q) |
| 468 | |
| 469 | // accept FLT_MAX as infinity |
| 470 | static bool isinf_or_max(float f) { |
| 471 | return std::isinf(f) || f == FLT_MAX || f == -FLT_MAX; |
| 472 | } |
| 473 | |
| 474 | static bool ggml_is_view_op(enum ggml_op op) { |
| 475 | return op == GGML_OP_VIEW || op == GGML_OP_RESHAPE || op == GGML_OP_PERMUTE || op == GGML_OP_TRANSPOSE; |
| 476 | } |
| 477 | |
| 478 | static bool backend_has_feature(ggml_backend_t backend, const char * feature_name) { |
| 479 | ggml_backend_dev_t dev = ggml_backend_get_device(backend); |
| 480 | ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(dev); |
| 481 | |
| 482 | auto get_features = (ggml_backend_get_features_t) ggml_backend_reg_get_proc_address(reg, "ggml_backend_get_features"); |
| 483 | if (!get_features) { |
| 484 | return false; |
| 485 | } |
| 486 | |
| 487 | const ggml_backend_feature * features = get_features(reg); |
| 488 | if (!features) { |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | for (const ggml_backend_feature * f = features; f->name; ++f) { |
| 493 | if (strcmp(f->name, feature_name) == 0 && strcmp(f->value, "1") == 0) { |
| 494 | return true; |
| 495 | } |
| 496 | } |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | enum test_mode { |
| 501 | MODE_TEST, |
| 502 | MODE_PERF, |
| 503 | MODE_GRAD, |
| 504 | MODE_SUPPORT, |
| 505 | }; |
| 506 | |
| 507 | // Output format support similar to llama-bench |
| 508 | enum output_formats { CONSOLE, SQL, CSV }; |
| 509 | |
| 510 | static const char * output_format_str(output_formats format) { |
| 511 | switch (format) { |
| 512 | case CONSOLE: |
| 513 | return "console"; |
| 514 | case SQL: |
| 515 | return "sql"; |
| 516 | case CSV: |
| 517 | return "csv"; |
| 518 | default: |
| 519 | GGML_ABORT("invalid output format"); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | static bool output_format_from_str(const std::string & s, output_formats & format) { |
| 524 | if (s == "console") { |
| 525 | format = CONSOLE; |
| 526 | } else if (s == "sql") { |
| 527 | format = SQL; |
| 528 | } else if (s == "csv") { |
| 529 | format = CSV; |
| 530 | } else { |
| 531 | return false; |
| 532 | } |
| 533 | return true; |
| 534 | } |
| 535 | |
| 536 | static std::string test_time_now() { |
| 537 | time_t t = time(NULL); |
| 538 | struct tm tm_buf; |
| 539 | #ifdef _WIN32 |
| 540 | if (gmtime_s(&tm_buf, &t) != 0) { |
| 541 | return ""; |
| 542 | } |
| 543 | #else |
| 544 | if (gmtime_r(&t, &tm_buf) == nullptr) { |
| 545 | return ""; |
| 546 | } |
| 547 | #endif |
| 548 | char buf[32]; |
| 549 | if (std::strftime(buf, sizeof(buf), "%FT%TZ", &tm_buf) == 0) { |
| 550 | return ""; |
| 551 | } |
| 552 | return buf; |
| 553 | } |
| 554 | |
| 555 | // Test result structure for SQL output |
| 556 | struct test_result { |
| 557 | std::string test_time; |
| 558 | std::string build_commit; |
| 559 | std::string backend_name; |
| 560 | std::string op_name; |
| 561 | std::string op_params; |
| 562 | std::string test_mode; |
| 563 | bool supported; |
| 564 | bool passed; |
| 565 | std::string error_message; |
| 566 | double time_us; |
| 567 | double flops; |
| 568 | double bandwidth_gb_s; |
| 569 | size_t memory_kb; |
| 570 | int n_runs; |
| 571 | std::string device_description; |
| 572 | std::string backend_reg_name; |
| 573 | |
| 574 | test_result() { |
| 575 | // Initialize with default values |
| 576 | time_us = 0.0; |
| 577 | flops = 0.0; |
| 578 | bandwidth_gb_s = 0.0; |
| 579 | memory_kb = 0; |
| 580 | n_runs = 0; |
| 581 | supported = false; |
| 582 | passed = false; |
| 583 | |
| 584 | test_time = test_time_now(); |
| 585 | |
| 586 | // Set build info |
| 587 | build_commit = ggml_commit(); |
| 588 | } |
| 589 | |
| 590 | test_result(const std::string & backend_name, const std::string & op_name, const std::string & op_params, |
| 591 | const std::string & test_mode, bool supported, bool passed, const std::string & error_message = "", |
| 592 | double time_us = 0.0, double flops = 0.0, double bandwidth_gb_s = 0.0, size_t memory_kb = 0, |
| 593 | int n_runs = 0, const std::string & device_description = "", const std::string & backend_reg_name = "") : |
| 594 | backend_name(backend_name), |
| 595 | op_name(op_name), |
| 596 | op_params(op_params), |
| 597 | test_mode(test_mode), |
| 598 | supported(supported), |
| 599 | passed(passed), |
| 600 | error_message(error_message), |
| 601 | time_us(time_us), |
| 602 | flops(flops), |
| 603 | bandwidth_gb_s(bandwidth_gb_s), |
| 604 | memory_kb(memory_kb), |
| 605 | n_runs(n_runs), |
| 606 | device_description(device_description), |
| 607 | backend_reg_name(backend_reg_name) { |
| 608 | test_time = test_time_now(); |
| 609 | |
| 610 | // Set build info |
| 611 | build_commit = ggml_commit(); |
| 612 | } |
| 613 | |
| 614 | static const std::vector<std::string> & get_fields() { |
| 615 | static const std::vector<std::string> fields = { |
| 616 | "test_time", "build_commit", "backend_name", "op_name", "op_params", "test_mode", "supported", |
| 617 | "passed", "error_message", "time_us", "flops", "bandwidth_gb_s", "memory_kb", "n_runs", |
| 618 | "device_description", "backend_reg_name" |
| 619 | }; |
| 620 | return fields; |
| 621 | } |
| 622 | |
| 623 | enum field_type { STRING, BOOL, INT, FLOAT }; |
| 624 | |
| 625 | static field_type get_field_type(const std::string & field) { |
| 626 | if (field == "supported" || field == "passed") { |
| 627 | return BOOL; |
| 628 | } |
| 629 | if (field == "memory_kb" || field == "n_runs") { |
| 630 | return INT; |
| 631 | } |
| 632 | if (field == "time_us" || field == "flops" || field == "bandwidth_gb_s") { |
| 633 | return FLOAT; |
| 634 | } |
| 635 | return STRING; |
| 636 | } |
| 637 | |
| 638 | std::vector<std::string> get_values() const { |
| 639 | return { test_time, |
| 640 | build_commit, |
| 641 | backend_name, |
| 642 | op_name, |
| 643 | op_params, |
| 644 | test_mode, |
| 645 | std::to_string(supported), |
| 646 | std::to_string(passed), |
| 647 | error_message, |
| 648 | std::to_string(time_us), |
| 649 | std::to_string(flops), |
| 650 | std::to_string(bandwidth_gb_s), |
| 651 | std::to_string(memory_kb), |
| 652 | std::to_string(n_runs), |
| 653 | device_description, |
| 654 | backend_reg_name }; |
| 655 | } |
| 656 | }; |
| 657 | |
| 658 | // Printer classes for different output formats |
| 659 | enum class test_status_t { NOT_SUPPORTED, OK, FAIL, SKIPPED }; |
| 660 | |
| 661 | struct test_operation_info { |
| 662 | std::string op_name; |
| 663 | std::string op_params; |
| 664 | std::string backend_name; |
| 665 | test_status_t status = test_status_t::OK; |
| 666 | std::string failure_reason; |
| 667 | |
| 668 | // Additional information fields that were previously in separate structs |
| 669 | std::string error_component; |
| 670 | std::string error_details; |
| 671 | |
| 672 | // Gradient info |
| 673 | int64_t gradient_index = -1; |
| 674 | std::string gradient_param_name; |
| 675 | float gradient_value = 0.0f; |
| 676 | |
| 677 | // MAA error info |
| 678 | double maa_error = 0.0; |
| 679 | double maa_threshold = 0.0; |
| 680 | |
| 681 | // Flags for different types of information |
| 682 | bool has_error = false; |
| 683 | bool has_gradient_info = false; |
| 684 | bool has_maa_error = false; |
| 685 | bool is_compare_failure = false; |
| 686 | bool is_large_tensor_skip = false; |
| 687 | |
| 688 | test_operation_info() = default; |
| 689 | |
| 690 | test_operation_info(const std::string & op_name, const std::string & op_params, const std::string & backend_name, |
| 691 | test_status_t status = test_status_t::OK, const std::string & failure_reason = "") : |
| 692 | op_name(op_name), |
| 693 | op_params(op_params), |
| 694 | backend_name(backend_name), |
| 695 | status(status), |
| 696 | failure_reason(failure_reason) {} |
| 697 | |
| 698 | // Set error information |
| 699 | void set_error(const std::string & component, const std::string & details) { |
| 700 | has_error = true; |
| 701 | error_component = component; |
| 702 | error_details = details; |
| 703 | if (status == test_status_t::OK) { |
| 704 | status = test_status_t::FAIL; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | // Set gradient information |
| 709 | void set_gradient_info(int64_t index, const std::string & param_name, float value) { |
| 710 | has_gradient_info = true; |
| 711 | gradient_index = index; |
| 712 | gradient_param_name = param_name; |
| 713 | gradient_value = value; |
| 714 | if (status == test_status_t::OK) { |
| 715 | status = test_status_t::FAIL; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | // Set MAA error information |
| 720 | void set_maa_error(double error, double threshold) { |
| 721 | has_maa_error = true; |
| 722 | maa_error = error; |
| 723 | maa_threshold = threshold; |
| 724 | if (status == test_status_t::OK) { |
| 725 | status = test_status_t::FAIL; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | // Set compare failure |
| 730 | void set_compare_failure() { |
| 731 | is_compare_failure = true; |
| 732 | if (status == test_status_t::OK) { |
| 733 | status = test_status_t::FAIL; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | // Set large tensor skip |
| 738 | void set_large_tensor_skip() { is_large_tensor_skip = true; } |
| 739 | }; |
| 740 | |
| 741 | struct test_summary_info { |
| 742 | size_t tests_passed; |
| 743 | size_t tests_total; |
| 744 | bool is_backend_summary = false; // true for backend summary, false for test summary |
| 745 | |
| 746 | test_summary_info() = default; |
| 747 | |
| 748 | test_summary_info(size_t tests_passed, size_t tests_total, bool is_backend_summary = false) : |
| 749 | tests_passed(tests_passed), |
| 750 | tests_total(tests_total), |
| 751 | is_backend_summary(is_backend_summary) {} |
| 752 | }; |
| 753 | |
| 754 | struct testing_start_info { |
| 755 | size_t device_count; |
| 756 | |
| 757 | testing_start_info() = default; |
| 758 | |
| 759 | testing_start_info(size_t device_count) : device_count(device_count) {} |
| 760 | }; |
| 761 | |
| 762 | struct backend_init_info { |
| 763 | size_t device_index; |
| 764 | size_t total_devices; |
| 765 | std::string device_name; |
| 766 | bool skipped = false; |
| 767 | std::string skip_reason; |
| 768 | std::string description; |
| 769 | size_t memory_total_mb = 0; |
| 770 | size_t memory_free_mb = 0; |
| 771 | bool has_memory_info = false; |
| 772 | |
| 773 | backend_init_info() = default; |
| 774 | |
| 775 | backend_init_info(size_t device_index, size_t total_devices, const std::string & device_name, bool skipped = false, |
| 776 | const std::string & skip_reason = "", const std::string & description = "", |
| 777 | size_t memory_total_mb = 0, size_t memory_free_mb = 0, bool has_memory_info = false) : |
| 778 | device_index(device_index), |
| 779 | total_devices(total_devices), |
| 780 | device_name(device_name), |
| 781 | skipped(skipped), |
| 782 | skip_reason(skip_reason), |
| 783 | description(description), |
| 784 | memory_total_mb(memory_total_mb), |
| 785 | memory_free_mb(memory_free_mb), |
| 786 | has_memory_info(has_memory_info) {} |
| 787 | }; |
| 788 | |
| 789 | struct backend_status_info { |
| 790 | std::string backend_name; |
| 791 | test_status_t status; |
| 792 | |
| 793 | backend_status_info() = default; |
| 794 | |
| 795 | backend_status_info(const std::string & backend_name, test_status_t status) : |
| 796 | backend_name(backend_name), |
| 797 | status(status) {} |
| 798 | }; |
| 799 | |
| 800 | struct overall_summary_info { |
| 801 | size_t backends_passed; |
| 802 | size_t backends_total; |
| 803 | bool all_passed; |
| 804 | |
| 805 | overall_summary_info() = default; |
| 806 | |
| 807 | overall_summary_info(size_t backends_passed, size_t backends_total, bool all_passed) : |
| 808 | backends_passed(backends_passed), |
| 809 | backends_total(backends_total), |
| 810 | all_passed(all_passed) {} |
| 811 | }; |
| 812 | |
| 813 | struct printer { |
| 814 | virtual ~printer() {} |
| 815 | |
| 816 | FILE * fout = stdout; |
| 817 | |
| 818 | virtual void print_header() {} |
| 819 | |
| 820 | virtual void print_test_result(const test_result & result) = 0; |
| 821 | |
| 822 | virtual void print_footer() {} |
| 823 | |
| 824 | virtual void print_operation(const test_operation_info & info) { (void) info; } |
| 825 | |
| 826 | virtual void print_summary(const test_summary_info & info) { (void) info; } |
| 827 | |
| 828 | virtual void print_testing_start(const testing_start_info & info) { (void) info; } |
| 829 | |
| 830 | virtual void print_backend_init(const backend_init_info & info) { (void) info; } |
| 831 | |
| 832 | virtual void print_backend_status(const backend_status_info & info) { (void) info; } |
| 833 | |
| 834 | virtual void print_overall_summary(const overall_summary_info & info) { (void) info; } |
| 835 | |
| 836 | virtual void print_failed_tests(const std::vector<std::string> & failed_tests) { (void) failed_tests; } |
| 837 | }; |
| 838 | |
| 839 | struct console_printer : public printer { |
| 840 | void print_test_result(const test_result & result) override { |
| 841 | if (result.test_mode == "test") { |
| 842 | print_test_console(result); |
| 843 | } else if (result.test_mode == "perf") { |
| 844 | print_perf_console(result); |
| 845 | } else if (result.test_mode == "support") { |
| 846 | print_support_console(result); |
| 847 | } |
| 848 | fflush(stdout); |
| 849 | } |
| 850 | |
| 851 | void print_operation(const test_operation_info & info) override { |
| 852 | printf(" %s(%s): ", info.op_name.c_str(), info.op_params.c_str()); |
| 853 | fflush(stdout); |
| 854 | |
| 855 | // Handle large tensor skip first |
| 856 | if (info.is_large_tensor_skip) { |
| 857 | printf("skipping large tensors for speed \n"); |
| 858 | fflush(stdout); |
| 859 | return; |
| 860 | } |
| 861 | |
| 862 | // Handle not supported status |
| 863 | if (info.status == test_status_t::NOT_SUPPORTED) { |
| 864 | if (!info.failure_reason.empty()) { |
| 865 | printf("not supported [%s]\n", info.failure_reason.c_str()); |
| 866 | } else { |
| 867 | printf("not supported [%s]\n", info.backend_name.c_str()); |
| 868 | } |
| 869 | fflush(stdout); |
| 870 | return; |
| 871 | } |
| 872 | |
| 873 | // Handle errors and additional information |
| 874 | if (info.has_error) { |
| 875 | if (info.error_component == "allocation") { |
| 876 | fprintf(stderr, "failed to allocate tensors [%s] ", info.backend_name.c_str()); |
| 877 | } else if (info.error_component == "backend") { |
| 878 | fprintf(stderr, " Failed to initialize %s backend\n", info.backend_name.c_str()); |
| 879 | } else { |
| 880 | fprintf(stderr, "Error in %s: %s\n", info.error_component.c_str(), info.error_details.c_str()); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | // Handle gradient info |
| 885 | if (info.has_gradient_info) { |
| 886 | printf("[%s] nonfinite gradient at index %" PRId64 " (%s=%f) ", info.op_name.c_str(), info.gradient_index, |
| 887 | info.gradient_param_name.c_str(), info.gradient_value); |
| 888 | } |
| 889 | |
| 890 | // Handle MAA error |
| 891 | if (info.has_maa_error) { |
| 892 | printf("[%s] MAA = %.9f > %.9f ", info.op_name.c_str(), info.maa_error, info.maa_threshold); |
| 893 | } |
| 894 | |
| 895 | // Handle compare failure |
| 896 | if (info.is_compare_failure) { |
| 897 | printf("compare failed "); |
| 898 | } |
| 899 | |
| 900 | // Print final status |
| 901 | if (info.status == test_status_t::OK) { |
| 902 | printf("\033[1;32mOK\033[0m\n"); |
| 903 | } else { |
| 904 | printf("\033[1;31mFAIL\033[0m\n"); |
| 905 | } |
| 906 | fflush(stdout); |
| 907 | } |
| 908 | |
| 909 | void print_summary(const test_summary_info & info) override { |
| 910 | if (info.is_backend_summary) { |
| 911 | printf("%zu/%zu backends passed\n", info.tests_passed, info.tests_total); |
| 912 | } else { |
| 913 | printf(" %zu/%zu tests passed\n", info.tests_passed, info.tests_total); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | void print_backend_status(const backend_status_info & info) override { |
| 918 | printf(" Backend %s: ", info.backend_name.c_str()); |
| 919 | if (info.status == test_status_t::OK) { |
| 920 | printf("\033[1;32mOK\033[0m\n"); |
| 921 | } else { |
| 922 | printf("\033[1;31mFAIL\033[0m\n"); |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | void print_testing_start(const testing_start_info & info) override { |
| 927 | printf("Testing %zu devices\n\n", info.device_count); |
| 928 | } |
| 929 | |
| 930 | void print_backend_init(const backend_init_info & info) override { |
| 931 | printf("Backend %zu/%zu: %s\n", info.device_index + 1, info.total_devices, info.device_name.c_str()); |
| 932 | |
| 933 | if (info.skipped) { |
| 934 | printf(" %s\n", info.skip_reason.c_str()); |
| 935 | return; |
| 936 | } |
| 937 | |
| 938 | if (!info.description.empty()) { |
| 939 | printf(" Device description: %s\n", info.description.c_str()); |
| 940 | } |
| 941 | |
| 942 | if (info.has_memory_info) { |
| 943 | printf(" Device memory: %zu MB (%zu MB free)\n", info.memory_total_mb, info.memory_free_mb); |
| 944 | } |
| 945 | |
| 946 | printf("\n"); |
| 947 | } |
| 948 | |
| 949 | void print_overall_summary(const overall_summary_info & info) override { |
| 950 | printf("%zu/%zu backends passed\n", info.backends_passed, info.backends_total); |
| 951 | if (info.all_passed) { |
| 952 | printf("\033[1;32mOK\033[0m\n"); |
| 953 | } else { |
| 954 | printf("\033[1;31mFAIL\033[0m\n"); |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | void print_failed_tests(const std::vector<std::string> & failed_tests) override { |
| 959 | if (failed_tests.empty()) { |
| 960 | return; |
| 961 | } |
| 962 | |
| 963 | printf("\nFailing tests:\n"); |
| 964 | for (const auto & test_name : failed_tests) { |
| 965 | printf(" %s\n", test_name.c_str()); |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | private: |
| 970 | void print_test_console(const test_result & result) { |
| 971 | printf(" %s(%s): ", result.op_name.c_str(), result.op_params.c_str()); |
| 972 | fflush(stdout); |
| 973 | |
| 974 | if (!result.supported) { |
| 975 | printf("not supported [%s] ", result.backend_name.c_str()); |
| 976 | printf("\n"); |
| 977 | return; |
| 978 | } |
| 979 | |
| 980 | if (result.passed) { |
| 981 | printf("\033[1;32mOK\033[0m\n"); |
| 982 | } else { |
| 983 | printf("\033[1;31mFAIL\033[0m\n"); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | void print_perf_console(const test_result & result) { |
| 988 | int len = printf(" %s(%s): ", result.op_name.c_str(), result.op_params.c_str()); |
| 989 | fflush(stdout); |
| 990 | |
| 991 | if (!result.supported) { |
| 992 | printf("not supported\n"); |
| 993 | return; |
| 994 | } |
| 995 | |
| 996 | // align while also leaving some margin for variations in parameters |
| 997 | int align = 8; |
| 998 | int last = (len + align - 1) / align * align; |
| 999 | if (last - len < 5) { |
| 1000 | last += align; |
| 1001 | } |
| 1002 | printf("%*s", last - len, ""); |
| 1003 | |
| 1004 | printf(" %8d runs - %8.2f us/run - ", result.n_runs, result.time_us); |
| 1005 | |
| 1006 | if (result.flops > 0) { |
| 1007 | auto format_flops = [](double flops) -> std::string { |
| 1008 | char buf[256]; |
| 1009 | if (flops >= 1e12) { |
| 1010 | snprintf(buf, sizeof(buf), "%6.2f TFLOP", flops / 1e12); |
| 1011 | } else if (flops >= 1e9) { |
| 1012 | snprintf(buf, sizeof(buf), "%6.2f GFLOP", flops / 1e9); |
| 1013 | } else if (flops >= 1e6) { |
| 1014 | snprintf(buf, sizeof(buf), "%6.2f MFLOP", flops / 1e6); |
| 1015 | } else { |
| 1016 | snprintf(buf, sizeof(buf), "%6.2f kFLOP", flops / 1e3); |
| 1017 | } |
| 1018 | return buf; |
| 1019 | }; |
| 1020 | uint64_t op_flops_per_run = result.flops * result.time_us / 1e6; |
| 1021 | printf("%s/run - \033[1;34m%sS\033[0m", format_flops(op_flops_per_run).c_str(), |
| 1022 | format_flops(result.flops).c_str()); |
| 1023 | } else { |
| 1024 | printf("%8zu kB/run - \033[1;34m%7.2f GB/s\033[0m", result.memory_kb, result.bandwidth_gb_s); |
| 1025 | } |
| 1026 | printf("\n"); |
| 1027 | } |
| 1028 | |
| 1029 | void print_support_console(const test_result & result) { |
| 1030 | printf(" %s(%s): ", result.op_name.c_str(), result.op_params.c_str()); |
| 1031 | fflush(stdout); |
| 1032 | |
| 1033 | if (result.supported) { |
| 1034 | printf("\033[1;32mSUPPORTED\033[0m\n"); |
| 1035 | } else { |
| 1036 | printf("\033[1;31mNOT SUPPORTED\033[0m\n"); |
| 1037 | } |
| 1038 | } |
| 1039 | }; |
| 1040 | |
| 1041 | struct sql_printer : public printer { |
| 1042 | static std::string get_sql_field_type(const std::string & field) { |
| 1043 | switch (test_result::get_field_type(field)) { |
| 1044 | case test_result::STRING: |
| 1045 | return "TEXT"; |
| 1046 | case test_result::BOOL: |
| 1047 | case test_result::INT: |
| 1048 | return "INTEGER"; |
| 1049 | case test_result::FLOAT: |
| 1050 | return "REAL"; |
| 1051 | default: |
| 1052 | GGML_ABORT("invalid field type"); |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | void print_header() override { |
| 1057 | std::vector<std::string> fields = test_result::get_fields(); |
| 1058 | fprintf(fout, "CREATE TABLE IF NOT EXISTS test_backend_ops (\n"); |
| 1059 | for (size_t i = 0; i < fields.size(); i++) { |
| 1060 | fprintf(fout, " %s %s%s\n", fields[i].c_str(), get_sql_field_type(fields[i]).c_str(), |
| 1061 | i < fields.size() - 1 ? "," : ""); |
| 1062 | } |
| 1063 | fprintf(fout, ");\n\n"); |
| 1064 | } |
| 1065 | |
| 1066 | void print_test_result(const test_result & result) override { |
| 1067 | fprintf(fout, "INSERT INTO test_backend_ops ("); |
| 1068 | std::vector<std::string> fields = test_result::get_fields(); |
| 1069 | for (size_t i = 0; i < fields.size(); i++) { |
| 1070 | fprintf(fout, "%s%s", fields[i].c_str(), i < fields.size() - 1 ? ", " : ""); |
| 1071 | } |
| 1072 | fprintf(fout, ") VALUES ("); |
| 1073 | std::vector<std::string> values = result.get_values(); |
| 1074 | for (size_t i = 0; i < values.size(); i++) { |
| 1075 | fprintf(fout, "'%s'%s", values[i].c_str(), i < values.size() - 1 ? ", " : ""); |
| 1076 | } |
| 1077 | fprintf(fout, ");\n"); |
| 1078 | } |
| 1079 | }; |
| 1080 | |
| 1081 | struct csv_printer : public printer { |
| 1082 | void print_header() override { |
| 1083 | |
| 1084 | std::vector<std::string> fields = test_result::get_fields(); |
| 1085 | std::vector<std::string> fields_csv = get_fields_csv(); |
| 1086 | for (size_t i = 0; i < fields.size(); i++) { |
| 1087 | if (std::find(std::begin(fields_csv), std::end(fields_csv), fields[i]) == std::end(fields_csv)) { |
| 1088 | continue; |
| 1089 | } |
| 1090 | printf("\"%s\"%s", fields[i].c_str(), i < fields.size() - 1 ? "," : ""); |
| 1091 | } |
| 1092 | printf("\n"); |
| 1093 | } |
| 1094 | |
| 1095 | void print_test_result(const test_result & result) override { |
| 1096 | |
| 1097 | std::vector<std::string> values = result.get_values(); |
| 1098 | std::vector<std::string> fields = test_result::get_fields(); |
| 1099 | std::vector<std::string> fields_csv = get_fields_csv(); |
| 1100 | |
| 1101 | for (size_t i = 0; i < values.size(); i++) { |
| 1102 | |
| 1103 | if (std::find(std::begin(fields_csv), std::end(fields_csv), fields[i]) == std::end(fields_csv)) { |
| 1104 | continue; |
| 1105 | } |
| 1106 | |
| 1107 | // Escape quotes and wrap in quotes for CSV |
| 1108 | std::string escaped_value = values[i]; |
| 1109 | size_t pos = 0; |
| 1110 | while ((pos = escaped_value.find("\"", pos)) != std::string::npos) { |
| 1111 | escaped_value.replace(pos, 1, "\"\""); |
| 1112 | pos += 2; |
| 1113 | } |
| 1114 | printf("\"%s\"%s", escaped_value.c_str(), i < values.size() - 1 ? "," : ""); |
| 1115 | } |
| 1116 | printf("\n"); |
| 1117 | } |
| 1118 | |
| 1119 | static std::vector<std::string> get_fields_csv() { |
| 1120 | return { |
| 1121 | "op_name", |
| 1122 | "op_params", |
| 1123 | "supported", |
| 1124 | "error_message", |
| 1125 | "test_mode", |
| 1126 | "backend_reg_name", |
| 1127 | "backend_name", |
| 1128 | }; |
| 1129 | } |
| 1130 | |
| 1131 | }; |
| 1132 | |
| 1133 | static std::unique_ptr<printer> create_printer(output_formats format) { |
| 1134 | switch (format) { |
| 1135 | case CONSOLE: |
| 1136 | return std::make_unique<console_printer>(); |
| 1137 | case SQL: |
| 1138 | return std::make_unique<sql_printer>(); |
| 1139 | case CSV: |
| 1140 | return std::make_unique<csv_printer>(); |
| 1141 | } |
| 1142 | GGML_ABORT("invalid output format"); |
| 1143 | } |
| 1144 | |
| 1145 | static std::mutex g_test_output_mutex; |
| 1146 | |
| 1147 | static void print_test_result_locked(printer * output_printer, const test_result & result) { |
| 1148 | if (output_printer == nullptr) { |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | std::lock_guard<std::mutex> guard(g_test_output_mutex); |
| 1153 | output_printer->print_test_result(result); |
| 1154 | } |
| 1155 | |
| 1156 | // Splits the -o filter into comma separated entries. Commas inside parentheses |
| 1157 | // (i.e. inside a full test case string) are not treated as separators. |
| 1158 | static std::vector<std::string_view> op_filter_entries(const char * op_names_filter) { |
| 1159 | std::vector<std::string_view> entries; |
| 1160 | if (op_names_filter == nullptr) { |
| 1161 | return entries; |
| 1162 | } |
| 1163 | std::string_view filter(op_names_filter); |
| 1164 | while (!filter.empty()) { |
| 1165 | auto comma_pos = filter.find_first_of(','); |
| 1166 | const auto lparen_pos = filter.find_first_of('('); |
| 1167 | if (lparen_pos < comma_pos) { |
| 1168 | const auto rparen_pos = filter.find_first_of(')'); |
| 1169 | comma_pos = filter.find_first_of(',', rparen_pos); |
| 1170 | } |
| 1171 | entries.push_back(filter.substr(0, comma_pos)); |
| 1172 | filter = comma_pos != std::string_view::npos ? filter.substr(comma_pos + 1) : ""; |
| 1173 | } |
| 1174 | return entries; |
| 1175 | } |
| 1176 | |
| 1177 | // An entry from the -o filter matches an op if it is either |
| 1178 | // * an exact op name as given by ggml_op_desc() (e.g. "ADD"), or |
| 1179 | // * a regex that matches the op name (e.g. "DSV4.*") |
| 1180 | static bool op_filter_entry_matches(std::string_view entry, std::string_view op_name) { |
| 1181 | if (entry == op_name) { |
| 1182 | return true; |
| 1183 | } |
| 1184 | // plain op names are matched exactly, anything else is treated as a regex |
| 1185 | if (std::regex_match(std::string(entry), std::regex("[A-Z0-9_]+"))) { |
| 1186 | return false; |
| 1187 | } |
| 1188 | std::regex re; |
| 1189 | try { |
| 1190 | re = std::regex(std::string(entry)); |
| 1191 | } catch (const std::regex_error &) { |
| 1192 | return false; |
| 1193 | } |
| 1194 | return std::regex_search(op_name.data(), op_name.data() + op_name.size(), re); |
| 1195 | } |
| 1196 | |
| 1197 | // TODO: [TAG_MUL_MAT_ID_SKIP] see https://github.com/ggml-org/llama.cpp/pull/26631 |
| 1198 | static bool backend_lacks_skipped_ids(ggml_backend_t backend, ggml_cgraph * gf) { |
| 1199 | const char * name = ggml_backend_reg_name(ggml_backend_dev_backend_reg(ggml_backend_get_device(backend))); |
| 1200 | if (strcmp(name, "CUDA") != 0 && strcmp(name, "ROCm") != 0 && strcmp(name, "MUSA") != 0) { |
| 1201 | return false; |
| 1202 | } |
| 1203 | for (int i = 0; i < ggml_graph_n_nodes(gf); i++) { |
| 1204 | const ggml_tensor * node = ggml_graph_node(gf, i); |
| 1205 | if (node->op != GGML_OP_MUL_MAT_ID && node->op != GGML_OP_ADD_ID) { |
| 1206 | continue; |
| 1207 | } |
| 1208 | const ggml_tensor * ids = node->src[2]; |
| 1209 | std::vector<int32_t> row(ids->ne[0]); |
| 1210 | for (int64_t i1 = 0; i1 < ids->ne[1]; i1++) { |
| 1211 | ggml_backend_tensor_get(ids, row.data(), i1*ids->nb[1], ids->ne[0]*sizeof(int32_t)); |
| 1212 | if (std::find(row.begin(), row.end(), -1) != row.end()) { |
| 1213 | return true; |
| 1214 | } |
| 1215 | } |
| 1216 | } |
| 1217 | return false; |
| 1218 | } |
| 1219 | |
| 1220 | struct test_case { |
| 1221 | virtual ~test_case() {} |
| 1222 | |
| 1223 | virtual std::string op_desc(ggml_tensor * t) { |
| 1224 | return ggml_op_desc(t); |
| 1225 | } |
| 1226 | |
| 1227 | virtual std::string vars() { |
| 1228 | return ""; |
| 1229 | } |
| 1230 | |
| 1231 | virtual ggml_tensor * build_graph(ggml_context * ctx) = 0; |
| 1232 | virtual ggml_tensor * build_graph(ggml_context * ctx, ggml_context * ctx_weights) { |
| 1233 | GGML_UNUSED(ctx_weights); |
| 1234 | return build_graph(ctx); |
| 1235 | } |
| 1236 | |
| 1237 | virtual double max_nmse_err() { |
| 1238 | return 1e-7; |
| 1239 | } |
| 1240 | |
| 1241 | virtual double max_nmse_err(ggml_backend_t backend) { |
| 1242 | ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(ggml_backend_get_device(backend)); |
| 1243 | // See https://github.com/ggml-org/llama.cpp/pull/22976 for explanation. |
| 1244 | if (contains_f16 && strcmp(ggml_backend_reg_name(reg), "WebGPU") == 0) { |
| 1245 | return std::max(max_nmse_err(), 1e-6); |
| 1246 | } |
| 1247 | return max_nmse_err(); |
| 1248 | } |
| 1249 | |
| 1250 | virtual double max_maa_err() { |
| 1251 | return 1e-4; |
| 1252 | } |
| 1253 | |
| 1254 | virtual double max_err() { |
| 1255 | return max_nmse_err(); |
| 1256 | } |
| 1257 | |
| 1258 | virtual double max_err(ggml_backend_t backend) { |
| 1259 | return max_nmse_err(backend); |
| 1260 | } |
| 1261 | |
| 1262 | virtual double err(const float * a, const float * b, size_t n) { |
| 1263 | return nmse(a, b, n); |
| 1264 | } |
| 1265 | |
| 1266 | virtual float grad_eps() { |
| 1267 | return 1e-1f; |
| 1268 | } |
| 1269 | |
| 1270 | // If false, estimate gradient with 2 points, neglects 3rd order derivative and higher. |
| 1271 | // If true, estimate gradient with 4 points, neglects 5th order derivative and higher. |
| 1272 | virtual bool grad_precise() { |
| 1273 | return false; |
| 1274 | } |
| 1275 | |
| 1276 | // Skip gradient checks if total number of gradients to be checked is larger than this (to speed up the tests). |
| 1277 | virtual int64_t grad_nmax() { |
| 1278 | return 10000; |
| 1279 | } |
| 1280 | |
| 1281 | // No effect if empty. |
| 1282 | // If not empty, skip all gradient checks where the numerical result does not match any of the values. |
| 1283 | // Needed for dealing with noncontinuous gradients (e.g. ReLU) where estimation using finite differences is unreliable. |
| 1284 | virtual std::vector<float> grad_expect() { |
| 1285 | return {}; |
| 1286 | } |
| 1287 | |
| 1288 | virtual void initialize_tensors(ggml_context * ctx) { |
| 1289 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) { |
| 1290 | init_tensor_uniform(t); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | // re-draw data-dependent inputs between timed perf iterations |
| 1295 | virtual void reinit_perf_iter(ggml_context * ctx) { |
| 1296 | GGML_UNUSED(ctx); |
| 1297 | } |
| 1298 | |
| 1299 | virtual size_t op_size(ggml_tensor * t) { |
| 1300 | size_t size = ggml_nbytes(t); |
| 1301 | // add source tensors |
| 1302 | for (int i = 0; i < GGML_MAX_SRC; i++) { |
| 1303 | if (t->src[i] != NULL) { |
| 1304 | size += ggml_nbytes(t->src[i]); |
| 1305 | } |
| 1306 | } |
| 1307 | return size; |
| 1308 | } |
| 1309 | |
| 1310 | virtual uint64_t op_flops(ggml_tensor * t) { |
| 1311 | GGML_UNUSED(t); |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | virtual bool run_whole_graph() { return false; } |
| 1316 | virtual std::vector<ggml_tensor *> fusion_test_nodes() { return {}; } |
| 1317 | virtual bool use_weight_context() { return false; } |
| 1318 | |
| 1319 | ggml_cgraph * gf = nullptr; |
| 1320 | ggml_cgraph * gb = nullptr; |
| 1321 | |
| 1322 | static const int sentinel_size = 1024; |
| 1323 | |
| 1324 | test_mode mode; |
| 1325 | |
| 1326 | std::vector<ggml_tensor *> sentinels; |
| 1327 | |
| 1328 | std::string current_op_name; |
| 1329 | bool contains_f16 = false; |
| 1330 | |
| 1331 | // Used by the WebGPU backend to relax error thresholds on ops on f16 tensors |
| 1332 | void check_for_f16_tensor(ggml_context * ctx) { |
| 1333 | contains_f16 = false; |
| 1334 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) { |
| 1335 | if (t->type == GGML_TYPE_F16) { |
| 1336 | contains_f16 = true; |
| 1337 | break; |
| 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | void add_sentinel(ggml_context * ctx) { |
| 1343 | if (mode == MODE_PERF || mode == MODE_GRAD || mode == MODE_SUPPORT) { |
| 1344 | return; |
| 1345 | } |
| 1346 | ggml_tensor * sentinel = ::ggml_new_tensor_1d(ctx, GGML_TYPE_F32, sentinel_size); |
| 1347 | ggml_format_name(sentinel, "sent_%zu", sentinels.size()); |
| 1348 | sentinels.push_back(sentinel); |
| 1349 | } |
| 1350 | |
| 1351 | // hijack ggml_new_tensor to add sentinels after each tensor to check for overflows in the backend |
| 1352 | |
| 1353 | ggml_tensor * ggml_new_tensor(ggml_context * ctx, ggml_type type, int n_dims, const int64_t * ne) { |
| 1354 | ggml_tensor * t = ::ggml_new_tensor(ctx, type, n_dims, ne); |
| 1355 | add_sentinel(ctx); |
| 1356 | return t; |
| 1357 | } |
| 1358 | |
| 1359 | ggml_tensor * ggml_new_tensor_1d(ggml_context * ctx, ggml_type type, int64_t ne0) { |
| 1360 | ggml_tensor * t = ::ggml_new_tensor_1d(ctx, type, ne0); |
| 1361 | add_sentinel(ctx); |
| 1362 | return t; |
| 1363 | } |
| 1364 | |
| 1365 | ggml_tensor * ggml_new_tensor_2d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1) { |
| 1366 | ggml_tensor * t = ::ggml_new_tensor_2d(ctx, type, ne0, ne1); |
| 1367 | add_sentinel(ctx); |
| 1368 | return t; |
| 1369 | } |
| 1370 | |
| 1371 | ggml_tensor * ggml_new_tensor_3d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1, int64_t ne2) { |
| 1372 | ggml_tensor * t = ::ggml_new_tensor_3d(ctx, type, ne0, ne1, ne2); |
| 1373 | add_sentinel(ctx); |
| 1374 | return t; |
| 1375 | } |
| 1376 | |
| 1377 | ggml_tensor * ggml_new_tensor_4d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne3) { |
| 1378 | ggml_tensor * t = ::ggml_new_tensor_4d(ctx, type, ne0, ne1, ne2, ne3); |
| 1379 | add_sentinel(ctx); |
| 1380 | return t; |
| 1381 | } |
| 1382 | |
| 1383 | // Checks an op against the test filter, which is a comma separated list of OP names, regexes, or specific variations |
| 1384 | bool matches_filter(ggml_tensor * op, const char * op_names_filter) { |
| 1385 | if (op_names_filter == nullptr) { |
| 1386 | return true; |
| 1387 | } |
| 1388 | const auto op_name = op_desc(op); |
| 1389 | const auto op_full_name = op_name + "(" + vars() + ")"; |
| 1390 | for (const auto & entry : op_filter_entries(op_names_filter)) { |
| 1391 | if (entry.find_first_of('(') != std::string_view::npos) { |
| 1392 | // a full test case string, matched exactly |
| 1393 | if (entry == op_full_name) { |
| 1394 | return true; |
| 1395 | } |
| 1396 | } else if (op_filter_entry_matches(entry, op_name)) { |
| 1397 | return true; |
| 1398 | } |
| 1399 | } |
| 1400 | return false; |
| 1401 | } |
| 1402 | |
| 1403 | test_status_t eval(ggml_backend_t backend1, |
| 1404 | ggml_backend_t backend2, |
| 1405 | const char * op_names_filter, |
| 1406 | printer * output_printer) { |
| 1407 | mode = MODE_TEST; |
| 1408 | |
| 1409 | ggml_init_params params = { |
| 1410 | /* .mem_size = */ ggml_tensor_overhead()*128 + ggml_graph_overhead(), |
| 1411 | /* .mem_base = */ NULL, |
| 1412 | /* .no_alloc = */ true, |
| 1413 | }; |
| 1414 | const bool use_weights = use_weight_context(); |
| 1415 | |
| 1416 | ggml_context_ptr ctx(ggml_init(params)); |
| 1417 | GGML_ASSERT(ctx); |
| 1418 | ggml_context_ptr ctx_weights(use_weights ? ggml_init(params) : nullptr); |
| 1419 | GGML_ASSERT(!use_weights || ctx_weights); |
| 1420 | |
| 1421 | gf = ggml_new_graph(ctx.get()); |
| 1422 | |
| 1423 | // pre-graph sentinel |
| 1424 | add_sentinel(ctx.get()); |
| 1425 | if (ctx_weights) { |
| 1426 | add_sentinel(ctx_weights.get()); |
| 1427 | } |
| 1428 | |
| 1429 | ggml_tensor * out = build_graph(ctx.get(), ctx_weights.get()); |
| 1430 | current_op_name = op_desc(out); |
| 1431 | check_for_f16_tensor(ctx.get()); |
| 1432 | |
| 1433 | if (!matches_filter(out, op_names_filter)) { |
| 1434 | //printf(" %s: skipping\n", op_desc(out).c_str()); |
| 1435 | return test_status_t::SKIPPED; |
| 1436 | } |
| 1437 | |
| 1438 | // check if the backends support the ops |
| 1439 | bool supported = true; |
| 1440 | std::string unsupported_str; |
| 1441 | for (ggml_backend_t backend : {backend1, backend2}) { |
| 1442 | for (ggml_tensor * t = ggml_get_first_tensor(ctx.get()); t != NULL; t = ggml_get_next_tensor(ctx.get(), t)) { |
| 1443 | if (!ggml_backend_supports_op(backend, t)) { |
| 1444 | supported = false; |
| 1445 | if (unsupported_str.empty()) { |
| 1446 | unsupported_str = std::string(ggml_backend_name(backend)); |
| 1447 | } else { |
| 1448 | unsupported_str += ", " + std::string(ggml_backend_name(backend)); |
| 1449 | } |
| 1450 | } |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | if (!supported) { |
| 1455 | test_result result(unsupported_str, current_op_name, vars(), "test", |
| 1456 | false, false, "not supported"); |
| 1457 | |
| 1458 | print_test_result_locked(output_printer, result); |
| 1459 | |
| 1460 | return test_status_t::NOT_SUPPORTED; |
| 1461 | } |
| 1462 | |
| 1463 | // post-graph sentinel |
| 1464 | add_sentinel(ctx.get()); |
| 1465 | if (ctx_weights) { |
| 1466 | add_sentinel(ctx_weights.get()); |
| 1467 | } |
| 1468 | |
| 1469 | ggml_backend_buffer_ptr buf_weights(nullptr); |
| 1470 | if (ctx_weights) { |
| 1471 | buf_weights.reset(ggml_backend_alloc_ctx_tensors(ctx_weights.get(), backend1)); |
| 1472 | if (buf_weights == NULL) { |
| 1473 | printf("failed to allocate weight tensors [%s] ", ggml_backend_name(backend1)); |
| 1474 | return test_status_t::FAIL; |
| 1475 | } |
| 1476 | ggml_backend_buffer_set_usage(buf_weights.get(), GGML_BACKEND_BUFFER_USAGE_WEIGHTS); |
| 1477 | } |
| 1478 | |
| 1479 | // allocate |
| 1480 | ggml_backend_buffer_ptr buf(ggml_backend_alloc_ctx_tensors(ctx.get(), backend1)); |
| 1481 | |
| 1482 | if (buf == NULL) { |
| 1483 | printf("failed to allocate tensors [%s] ", ggml_backend_name(backend1)); |
| 1484 | return test_status_t::FAIL; |
| 1485 | } |
| 1486 | |
| 1487 | // build graph |
| 1488 | ggml_build_forward_expand(gf, out); |
| 1489 | |
| 1490 | // add sentinels as graph nodes so that they are checked in the callback |
| 1491 | for (ggml_tensor * sentinel : sentinels) { |
| 1492 | ggml_graph_add_node(gf, sentinel); |
| 1493 | } |
| 1494 | |
| 1495 | // randomize tensors |
| 1496 | initialize_tensors(ctx.get()); |
| 1497 | if (ctx_weights) { |
| 1498 | initialize_tensors(ctx_weights.get()); |
| 1499 | } |
| 1500 | |
| 1501 | // TODO: [TAG_MUL_MAT_ID_SKIP] @ngxson : remove this once CUDA implements mul_mat_id with -1 = skipped |
| 1502 | if (backend_lacks_skipped_ids(backend1, gf)) { |
| 1503 | test_result result(ggml_backend_name(backend1), current_op_name, vars(), "test", false, false, "not supported"); |
| 1504 | print_test_result_locked(output_printer, result); |
| 1505 | return test_status_t::NOT_SUPPORTED; |
| 1506 | } |
| 1507 | |
| 1508 | // compare |
| 1509 | struct callback_userdata { |
| 1510 | bool ok; |
| 1511 | test_case * tc; |
| 1512 | ggml_backend_t backend1; |
| 1513 | ggml_backend_t backend2; |
| 1514 | }; |
| 1515 | |
| 1516 | callback_userdata ud { |
| 1517 | true, |
| 1518 | this, |
| 1519 | backend1, |
| 1520 | backend2, |
| 1521 | }; |
| 1522 | |
| 1523 | auto callback = [](int index, ggml_tensor * t1, ggml_tensor * t2, void * user_data) -> bool { |
| 1524 | callback_userdata * ud = (callback_userdata *) user_data; |
| 1525 | const char * bn1 = ggml_backend_name(ud->backend1); |
| 1526 | const char * bn2 = ggml_backend_name(ud->backend2); |
| 1527 | |
| 1528 | if (t1->op == GGML_OP_NONE) { |
| 1529 | // sentinels must be unchanged |
| 1530 | std::vector<uint8_t> t1_data(ggml_nbytes(t1)); |
| 1531 | std::vector<uint8_t> t2_data(ggml_nbytes(t2)); |
| 1532 | ggml_backend_tensor_get(t1, t1_data.data(), 0, ggml_nbytes(t1)); |
| 1533 | ggml_backend_tensor_get(t2, t2_data.data(), 0, ggml_nbytes(t2)); |
| 1534 | |
| 1535 | if (memcmp(t1_data.data(), t2_data.data(), ggml_nbytes(t1)) != 0) { |
| 1536 | printf("sentinel mismatch: %s ", t1->name); |
| 1537 | ud->ok = false; |
| 1538 | return true; |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | std::vector<float> f1 = tensor_to_float(t1); |
| 1543 | std::vector<float> f2 = tensor_to_float(t2); |
| 1544 | |
| 1545 | for (size_t i = 0; i < f1.size(); i++) { |
| 1546 | // check for nans |
| 1547 | if (std::isnan(f1[i]) || std::isnan(f2[i])) { |
| 1548 | printf("[%s] NaN at index %zu (%s=%f %s=%f) ", ggml_op_desc(t1), i, bn1, f1[i], bn2, f2[i]); |
| 1549 | ud->ok = false; |
| 1550 | return true; |
| 1551 | } |
| 1552 | // check for infs: both must be inf of the same sign, or both must be finite |
| 1553 | if (isinf_or_max(f1[i]) || isinf_or_max(f2[i])) { |
| 1554 | if (isinf_or_max(f1[i]) && isinf_or_max(f2[i])) { |
| 1555 | if (std::signbit(f1[i]) != std::signbit(f2[i])) { |
| 1556 | printf("[%s] inf sign mismatch: %s=%f %s=%f ", ggml_op_desc(t1), bn1, f1[i], bn2, f2[i]); |
| 1557 | ud->ok = false; |
| 1558 | return true; |
| 1559 | } |
| 1560 | } else { |
| 1561 | printf("[%s] inf mismatch: %s=%f %s=%f ", ggml_op_desc(t1), bn1, f1[i], bn2, f2[i]); |
| 1562 | ud->ok = false; |
| 1563 | return true; |
| 1564 | } |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | double err = ud->tc->err(f1.data(), f2.data(), f1.size()); |
| 1569 | if (err > ud->tc->max_err(ud->backend1)) { |
| 1570 | printf("[%s] ERR = %.9f > %.9f ", ggml_op_desc(t1), err, ud->tc->max_err(ud->backend1)); |
| 1571 | //for (int i = 0; i < (int) f1.size(); i++) { |
| 1572 | // printf("%5d %9.6f %9.6f, diff = %9.6f\n", i, f1[i], f2[i], f1[i] - f2[i]); |
| 1573 | //} |
| 1574 | //printf("\n"); |
| 1575 | //exit(1); |
| 1576 | ud->ok = false; |
| 1577 | } |
| 1578 | return true; |
| 1579 | |
| 1580 | GGML_UNUSED(index); |
| 1581 | }; |
| 1582 | |
| 1583 | std::vector<ggml_tensor *> fused_nodes_to_verify = fusion_test_nodes(); |
| 1584 | if (fused_nodes_to_verify.size() == 0 && run_whole_graph()) { |
| 1585 | fused_nodes_to_verify.push_back(out); |
| 1586 | } |
| 1587 | const bool cmp_ok = ggml_backend_compare_graph_backend(backend1, backend2, gf, callback, &ud, |
| 1588 | run_whole_graph() ? fused_nodes_to_verify.data() : nullptr, |
| 1589 | fused_nodes_to_verify.size()); |
| 1590 | |
| 1591 | // Create test result |
| 1592 | bool test_passed = ud.ok && cmp_ok; |
| 1593 | std::string error_msg = test_passed ? "" : (!cmp_ok ? "compare failed" : "test failed"); |
| 1594 | test_result result(ggml_backend_name(backend1), current_op_name, vars(), "test", supported, test_passed, |
| 1595 | error_msg); |
| 1596 | |
| 1597 | print_test_result_locked(output_printer, result); |
| 1598 | |
| 1599 | return test_passed ? test_status_t::OK : test_status_t::FAIL; |
| 1600 | } |
| 1601 | |
| 1602 | bool eval_perf(ggml_backend_t backend, const char * op_names_filter, printer * output_printer) { |
| 1603 | mode = MODE_PERF; |
| 1604 | |
| 1605 | static const size_t graph_nodes = 8192; |
| 1606 | |
| 1607 | ggml_init_params params = { |
| 1608 | /* .mem_size = */ ggml_tensor_overhead()*128 + ggml_graph_overhead_custom(graph_nodes, false), |
| 1609 | /* .mem_base = */ NULL, |
| 1610 | /* .no_alloc = */ true, |
| 1611 | }; |
| 1612 | const bool use_weights = use_weight_context(); |
| 1613 | |
| 1614 | ggml_context_ptr ctx(ggml_init(params)); // smart ptr |
| 1615 | GGML_ASSERT(ctx); |
| 1616 | ggml_context_ptr ctx_weights(use_weights ? ggml_init(params) : nullptr); |
| 1617 | GGML_ASSERT(!use_weights || ctx_weights); |
| 1618 | |
| 1619 | ggml_tensor * out = build_graph(ctx.get(), ctx_weights.get()); |
| 1620 | current_op_name = op_desc(out); |
| 1621 | if (!matches_filter(out, op_names_filter)) { |
| 1622 | //printf(" %s: skipping\n", op_desc(out).c_str()); |
| 1623 | return true; |
| 1624 | } |
| 1625 | |
| 1626 | if (!ggml_backend_supports_op(backend, out)) { |
| 1627 | // Create test result for unsupported performance test |
| 1628 | test_result result(ggml_backend_name(backend), current_op_name, vars(), "perf", false, false, |
| 1629 | "not supported"); |
| 1630 | |
| 1631 | output_printer->print_test_result(result); |
| 1632 | |
| 1633 | return true; |
| 1634 | } |
| 1635 | |
| 1636 | ggml_backend_buffer_ptr buf_weights(nullptr); |
| 1637 | if (ctx_weights) { |
| 1638 | buf_weights.reset(ggml_backend_alloc_ctx_tensors(ctx_weights.get(), backend)); |
| 1639 | if (buf_weights == NULL) { |
| 1640 | printf("failed to allocate weight tensors\n"); |
| 1641 | return false; |
| 1642 | } |
| 1643 | ggml_backend_buffer_set_usage(buf_weights.get(), GGML_BACKEND_BUFFER_USAGE_WEIGHTS); |
| 1644 | } |
| 1645 | |
| 1646 | // allocate |
| 1647 | ggml_backend_buffer_ptr buf(ggml_backend_alloc_ctx_tensors(ctx.get(), backend)); // smart ptr |
| 1648 | |
| 1649 | if (buf == NULL) { |
| 1650 | printf("failed to allocate tensors\n"); |
| 1651 | return false; |
| 1652 | } |
| 1653 | |
| 1654 | // randomize tensors |
| 1655 | initialize_tensors(ctx.get()); |
| 1656 | if (ctx_weights) { |
| 1657 | initialize_tensors(ctx_weights.get()); |
| 1658 | } |
| 1659 | |
| 1660 | // build graph |
| 1661 | ggml_cgraph * gf = ggml_new_graph_custom(ctx.get(), graph_nodes, false); |
| 1662 | ggml_build_forward_expand(gf, out); |
| 1663 | |
| 1664 | // warmup run |
| 1665 | ggml_status status = ggml_backend_graph_compute(backend, gf); |
| 1666 | if (status != GGML_STATUS_SUCCESS) { |
| 1667 | fprintf(stderr, "%s: ggml_backend_graph_compute failed. status=%s \n", __func__, ggml_status_to_string(status)); |
| 1668 | return false; |
| 1669 | } |
| 1670 | |
| 1671 | // determine number of runs |
| 1672 | int n_runs; |
| 1673 | bool is_cpu = ggml_backend_dev_type(ggml_backend_get_device(backend)) == GGML_BACKEND_DEVICE_TYPE_CPU; |
| 1674 | if (op_flops(out) > 0) { |
| 1675 | // based on flops |
| 1676 | const uint64_t GFLOP = 1000 * 1000 * 1000; |
| 1677 | const uint64_t target_flops_cpu = 8ULL * GFLOP; |
| 1678 | const uint64_t target_flops_gpu = 100ULL * GFLOP; |
| 1679 | uint64_t target_flops = is_cpu ? target_flops_cpu : target_flops_gpu; |
| 1680 | n_runs = (int)std::min<int64_t>(ggml_graph_size(gf) - ggml_graph_n_nodes(gf), target_flops / op_flops(out)) + 1; |
| 1681 | } else { |
| 1682 | // based on memory size |
| 1683 | const size_t GB = 1ULL << 30; |
| 1684 | const size_t target_size_cpu = 8 * GB; |
| 1685 | const size_t target_size_gpu = 32 * GB; |
| 1686 | size_t target_size = is_cpu ? target_size_cpu : target_size_gpu; |
| 1687 | n_runs = (int)std::min<int64_t>(ggml_graph_size(gf) - ggml_graph_n_nodes(gf), target_size / op_size(out)) + 1; |
| 1688 | } |
| 1689 | |
| 1690 | // duplicate the op |
| 1691 | for (int i = 1; i < n_runs; i++) { |
| 1692 | ggml_graph_add_node(gf, out); |
| 1693 | } |
| 1694 | |
| 1695 | // calculate memory |
| 1696 | size_t mem = n_runs * op_size(out); |
| 1697 | auto tensor_op_size = [](ggml_tensor * t) { |
| 1698 | size_t size = ggml_nbytes(t); |
| 1699 | // add source tensors |
| 1700 | for (int i = 0; i < GGML_MAX_SRC; i++) { |
| 1701 | if (t->src[i] != NULL) { |
| 1702 | size += ggml_nbytes(t->src[i]); |
| 1703 | } |
| 1704 | } |
| 1705 | return size; |
| 1706 | }; |
| 1707 | for (int i = 0; i < ggml_graph_n_nodes(gf); ++i) { |
| 1708 | if (ggml_is_view_op(ggml_graph_node(gf, i)->op) || ggml_graph_node(gf, i) == out) { |
| 1709 | continue; |
| 1710 | } |
| 1711 | mem += tensor_op_size(ggml_graph_node(gf, i)); |
| 1712 | } |
| 1713 | |
| 1714 | // run |
| 1715 | int64_t total_time_us = 0; |
| 1716 | int64_t total_mem = 0; |
| 1717 | int total_runs = 0; |
| 1718 | do { |
| 1719 | int64_t start_time = ggml_time_us(); |
| 1720 | ggml_status status = ggml_backend_graph_compute(backend, gf); |
| 1721 | if (status != GGML_STATUS_SUCCESS) { |
| 1722 | fprintf(stderr, "%s: ggml_backend_graph_compute failed. status=%s \n", __func__, ggml_status_to_string(status)); |
| 1723 | return false; |
| 1724 | } |
| 1725 | int64_t end_time = ggml_time_us(); |
| 1726 | |
| 1727 | total_time_us += end_time - start_time; |
| 1728 | total_mem += mem; |
| 1729 | total_runs += n_runs; |
| 1730 | |
| 1731 | // re-draw any data-dependent inputs (expert ids) outside the timed region |
| 1732 | reinit_perf_iter(ctx.get()); |
| 1733 | } while (total_time_us < 1000*1000); // run for at least 1 second |
| 1734 | |
| 1735 | // Create test result |
| 1736 | double avg_time_us = (double) total_time_us / total_runs; |
| 1737 | double calculated_flops = (op_flops(out) > 0) ? (op_flops(out) * total_runs) / (total_time_us / 1e6) : 0.0; |
| 1738 | double calculated_bandwidth = |
| 1739 | (op_flops(out) == 0) ? total_mem / (total_time_us / 1e6) / 1024.0 / 1024.0 / 1024.0 : 0.0; |
| 1740 | size_t calculated_memory_kb = op_size(out) / 1024; |
| 1741 | |
| 1742 | test_result result(ggml_backend_name(backend), current_op_name, vars(), "perf", true, true, "", avg_time_us, |
| 1743 | calculated_flops, calculated_bandwidth, calculated_memory_kb, total_runs); |
| 1744 | |
| 1745 | if (output_printer) { |
| 1746 | output_printer->print_test_result(result); |
| 1747 | } |
| 1748 | |
| 1749 | return true; |
| 1750 | } |
| 1751 | |
| 1752 | bool eval_support(ggml_backend_t backend, const char * op_names_filter, printer * output_printer) { |
| 1753 | mode = MODE_SUPPORT; |
| 1754 | |
| 1755 | static const size_t graph_nodes = 8192; |
| 1756 | |
| 1757 | ggml_init_params params = { |
| 1758 | /* .mem_size = */ ggml_tensor_overhead()*128 + ggml_graph_overhead_custom(graph_nodes, false), |
| 1759 | /* .mem_base = */ NULL, |
| 1760 | /* .no_alloc = */ true, |
| 1761 | }; |
| 1762 | ggml_context_ptr ctx(ggml_init(params)); // smart ptr |
| 1763 | GGML_ASSERT(ctx); |
| 1764 | |
| 1765 | gf = ggml_new_graph_custom(ctx.get(), graph_nodes, false); |
| 1766 | |
| 1767 | ggml_tensor * out = build_graph(ctx.get()); |
| 1768 | current_op_name = op_desc(out); |
| 1769 | |
| 1770 | if (!matches_filter(out, op_names_filter)) { |
| 1771 | return true; |
| 1772 | } |
| 1773 | |
| 1774 | bool supported = ggml_backend_supports_op(backend, out); |
| 1775 | |
| 1776 | std::string device_desc = ggml_backend_dev_description(ggml_backend_get_device(backend)); |
| 1777 | std::string backend_reg_name = ggml_backend_reg_name(ggml_backend_dev_backend_reg(ggml_backend_get_device(backend))); |
| 1778 | |
| 1779 | test_result result(ggml_backend_name(backend), current_op_name, vars(), "support", supported, supported, |
| 1780 | supported ? "yes" : "no", 0.0, 0.0, 0.0, 0, 0, device_desc, backend_reg_name); |
| 1781 | |
| 1782 | output_printer->print_test_result(result); |
| 1783 | |
| 1784 | return true; |
| 1785 | } |
| 1786 | |
| 1787 | bool eval_grad(ggml_backend_t backend, const char * op_names_filter, printer * output_printer) { |
| 1788 | mode = MODE_GRAD; |
| 1789 | const std::vector<float> expect = grad_expect(); |
| 1790 | |
| 1791 | ggml_init_params params = { |
| 1792 | /* .mem_size = */ ggml_tensor_overhead()*128 + 2*ggml_graph_overhead_custom(GGML_DEFAULT_GRAPH_SIZE, true), |
| 1793 | /* .mem_base = */ NULL, |
| 1794 | /* .no_alloc = */ true, |
| 1795 | }; |
| 1796 | ggml_context_ptr ctx(ggml_init(params)); // smart ptr |
| 1797 | GGML_ASSERT(ctx); |
| 1798 | |
| 1799 | gf = ggml_new_graph_custom(ctx.get(), GGML_DEFAULT_GRAPH_SIZE, true); |
| 1800 | gb = ggml_new_graph_custom(ctx.get(), GGML_DEFAULT_GRAPH_SIZE, true); |
| 1801 | |
| 1802 | ggml_tensor * out = build_graph(ctx.get()); |
| 1803 | |
| 1804 | if (!matches_filter(out, op_names_filter) || out->op == GGML_OP_OPT_STEP_ADAMW) { |
| 1805 | return true; |
| 1806 | } |
| 1807 | |
| 1808 | if (out->type != GGML_TYPE_F32) { |
| 1809 | output_printer->print_operation(test_operation_info(op_desc(out), vars(), ggml_backend_name(backend), |
| 1810 | test_status_t::NOT_SUPPORTED, |
| 1811 | out->name + std::string("->type != FP32"))); |
| 1812 | return true; |
| 1813 | } |
| 1814 | |
| 1815 | // Print operation info first |
| 1816 | output_printer->print_operation(test_operation_info(op_desc(out), vars(), ggml_backend_name(backend))); |
| 1817 | |
| 1818 | // check if the backend supports the ops |
| 1819 | bool supported = true; |
| 1820 | bool any_params = false; |
| 1821 | std::string failure_reason; |
| 1822 | |
| 1823 | for (ggml_tensor * t = ggml_get_first_tensor(ctx.get()); t != NULL; t = ggml_get_next_tensor(ctx.get(), t)) { |
| 1824 | if (!ggml_backend_supports_op(backend, t)) { |
| 1825 | supported = false; |
| 1826 | failure_reason = ggml_backend_name(backend); |
| 1827 | break; |
| 1828 | } |
| 1829 | if ((t->flags & GGML_TENSOR_FLAG_PARAM)) { |
| 1830 | any_params = true; |
| 1831 | if (t->type != GGML_TYPE_F32) { |
| 1832 | supported = false; |
| 1833 | failure_reason = std::string(t->name) + "->type != FP32"; |
| 1834 | break; |
| 1835 | } |
| 1836 | } |
| 1837 | } |
| 1838 | if (!any_params) { |
| 1839 | supported = false; |
| 1840 | failure_reason = op_desc(out); |
| 1841 | } |
| 1842 | |
| 1843 | if (!supported) { |
| 1844 | output_printer->print_operation(test_operation_info(op_desc(out), vars(), ggml_backend_name(backend), |
| 1845 | test_status_t::NOT_SUPPORTED, failure_reason)); |
| 1846 | return true; |
| 1847 | } |
| 1848 | |
| 1849 | int64_t ngrads = 0; |
| 1850 | for (ggml_tensor * t = ggml_get_first_tensor(ctx.get()); t != NULL; t = ggml_get_next_tensor(ctx.get(), t)) { |
| 1851 | if (t->flags & GGML_TENSOR_FLAG_PARAM) { |
| 1852 | ngrads += ggml_nelements(t); |
| 1853 | } |
| 1854 | } |
| 1855 | if (ngrads > grad_nmax()) { |
| 1856 | test_operation_info info(op_desc(out), vars(), ggml_backend_name(backend)); |
| 1857 | info.set_large_tensor_skip(); |
| 1858 | output_printer->print_operation(info); |
| 1859 | return true; |
| 1860 | } |
| 1861 | |
| 1862 | |
| 1863 | if (!ggml_is_scalar(out)) { |
| 1864 | out = ggml_sum(ctx.get(), out); |
| 1865 | ggml_set_name(out, "sum_of_out"); |
| 1866 | } |
| 1867 | ggml_set_loss(out); |
| 1868 | |
| 1869 | ggml_build_forward_expand(gf, out); |
| 1870 | ggml_graph_cpy(gf, gb); |
| 1871 | ggml_build_backward_expand(ctx.get(), gb, nullptr); |
| 1872 | if (expect.size() != 1 || expect[0] != 0.0f) { |
| 1873 | GGML_ASSERT(ggml_graph_n_nodes(gb) > ggml_graph_n_nodes(gf)); |
| 1874 | for (ggml_tensor * t = ggml_get_first_tensor(ctx.get()); t != NULL; t = ggml_get_next_tensor(ctx.get(), t)) { |
| 1875 | GGML_ASSERT(!(t->flags & GGML_TENSOR_FLAG_PARAM) || ggml_graph_get_grad(gb, t)->op != GGML_OP_NONE); |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | for (ggml_tensor * t = ggml_get_first_tensor(ctx.get()); t != NULL; t = ggml_get_next_tensor(ctx.get(), t)) { |
| 1880 | if (!ggml_backend_supports_op(backend, t)) { |
| 1881 | output_printer->print_operation(test_operation_info(op_desc(out), vars(), ggml_backend_name(backend), |
| 1882 | test_status_t::NOT_SUPPORTED, |
| 1883 | ggml_backend_name(backend))); |
| 1884 | supported = false; |
| 1885 | break; |
| 1886 | } |
| 1887 | if ((t->flags & GGML_TENSOR_FLAG_PARAM) && t->type != GGML_TYPE_F32) { |
| 1888 | output_printer->print_operation(test_operation_info(op_desc(out), vars(), ggml_backend_name(backend), |
| 1889 | test_status_t::NOT_SUPPORTED, |
| 1890 | std::string(t->name) + "->type != FP32")); |
| 1891 | supported = false; |
| 1892 | break; |
| 1893 | } |
| 1894 | } |
| 1895 | if (!supported) { |
| 1896 | return true; |
| 1897 | } |
| 1898 | |
| 1899 | // allocate |
| 1900 | ggml_backend_buffer_ptr buf(ggml_backend_alloc_ctx_tensors(ctx.get(), backend)); // smart ptr |
| 1901 | if (buf == NULL) { |
| 1902 | test_operation_info info(op_desc(out), vars(), ggml_backend_name(backend)); |
| 1903 | info.set_error("allocation", ""); |
| 1904 | output_printer->print_operation(info); |
| 1905 | return false; |
| 1906 | } |
| 1907 | |
| 1908 | initialize_tensors(ctx.get()); // Randomizes all tensors (including gradients). |
| 1909 | ggml_graph_reset(gb); // Sets gradients to 1 if loss, 0 otherwise. |
| 1910 | |
| 1911 | ggml_status status = ggml_backend_graph_compute(backend, gf); |
| 1912 | if (status != GGML_STATUS_SUCCESS) { |
| 1913 | fprintf(stderr, "%s: ggml_backend_graph_compute failed. status=%s \n", __func__, ggml_status_to_string(status)); |
| 1914 | return false; |
| 1915 | } |
| 1916 | status = ggml_backend_graph_compute(backend, gb); |
| 1917 | if (status != GGML_STATUS_SUCCESS) { |
| 1918 | fprintf(stderr, "%s: ggml_backend_graph_compute failed. status=%s \n", __func__, ggml_status_to_string(status)); |
| 1919 | return false; |
| 1920 | } |
| 1921 | |
| 1922 | bool ok = true; |
| 1923 | for (struct ggml_tensor * t = ggml_get_first_tensor(ctx.get()); t != nullptr; t = ggml_get_next_tensor(ctx.get(), t)) { |
| 1924 | if (!(t->flags & GGML_TENSOR_FLAG_PARAM)) { |
| 1925 | continue; |
| 1926 | } |
| 1927 | |
| 1928 | const char * bn = ggml_backend_name(backend); |
| 1929 | const int64_t ne = ggml_nelements(t); |
| 1930 | |
| 1931 | std::vector<float> ga; |
| 1932 | struct ggml_tensor * grad = ggml_graph_get_grad(gb, t); |
| 1933 | if (grad) { |
| 1934 | ga = tensor_to_float(grad); |
| 1935 | } else { |
| 1936 | ga.resize(ne); // default value is 0.0f |
| 1937 | } |
| 1938 | |
| 1939 | for (int64_t i = 0; i < ne; ++i) { // gradient algebraic |
| 1940 | // check for nans |
| 1941 | if (!std::isfinite(ga[i])) { |
| 1942 | test_operation_info info(op_desc(out), vars(), ggml_backend_name(backend)); |
| 1943 | info.set_gradient_info(i, bn, ga[i]); |
| 1944 | output_printer->print_operation(info); |
| 1945 | ok = false; |
| 1946 | break; |
| 1947 | } |
| 1948 | } |
| 1949 | if (!ok) { |
| 1950 | break; |
| 1951 | } |
| 1952 | |
| 1953 | std::vector<float> gn(ne); // gradient numeric |
| 1954 | GGML_ASSERT(ga.size() == gn.size()); |
| 1955 | |
| 1956 | std::vector<float> x0 = tensor_to_float(t); // original t data |
| 1957 | GGML_ASSERT(ggml_is_scalar(out)); |
| 1958 | GGML_ASSERT(out->type == GGML_TYPE_F32); |
| 1959 | |
| 1960 | const float eps = grad_eps(); |
| 1961 | for (int64_t i = 0; i < ne; ++i) { |
| 1962 | const float xiu = x0[i] + 1.0f*eps; // x, index i, up |
| 1963 | const float xiuh = x0[i] + 0.5f*eps; // x, index i, up half |
| 1964 | const float xidh = x0[i] - 0.5f*eps; // x, index i, down half |
| 1965 | const float xid = x0[i] - 1.0f*eps; // x, index i, down |
| 1966 | |
| 1967 | float fu, fuh, fdh, fd; // output values for xiu, xiuh, xid, xidh |
| 1968 | |
| 1969 | ggml_backend_tensor_set(t, &xiu, i*sizeof(float), sizeof(float)); |
| 1970 | status = ggml_backend_graph_compute(backend, gf); |
| 1971 | if (status != GGML_STATUS_SUCCESS) { |
| 1972 | fprintf(stderr, "%s: ggml_backend_graph_compute failed. status=%s \n", __func__, ggml_status_to_string(status)); |
| 1973 | return false; |
| 1974 | } |
| 1975 | ggml_backend_tensor_get(out, &fu, 0, ggml_nbytes(out)); |
| 1976 | |
| 1977 | ggml_backend_tensor_set(t, &xid, i*sizeof(float), sizeof(float)); |
| 1978 | status = ggml_backend_graph_compute(backend, gf); |
| 1979 | if (status != GGML_STATUS_SUCCESS) { |
| 1980 | fprintf(stderr, "%s: ggml_backend_graph_compute failed. status=%s \n", __func__, ggml_status_to_string(status)); |
| 1981 | return false; |
| 1982 | } |
| 1983 | ggml_backend_tensor_get(out, &fd, 0, ggml_nbytes(out)); |
| 1984 | |
| 1985 | if (grad_precise()) { |
| 1986 | ggml_backend_tensor_set(t, &xiuh, i*sizeof(float), sizeof(float)); |
| 1987 | status = ggml_backend_graph_compute(backend, gf); |
| 1988 | if (status != GGML_STATUS_SUCCESS) { |
| 1989 | fprintf(stderr, "%s: ggml_backend_graph_compute failed. status=%s \n", __func__, ggml_status_to_string(status)); |
| 1990 | return false; |
| 1991 | } |
| 1992 | ggml_backend_tensor_get(out, &fuh, 0, ggml_nbytes(out)); |
| 1993 | |
| 1994 | ggml_backend_tensor_set(t, &xidh, i*sizeof(float), sizeof(float)); |
| 1995 | status = ggml_backend_graph_compute(backend, gf); |
| 1996 | if (status != GGML_STATUS_SUCCESS) { |
| 1997 | fprintf(stderr, "%s: ggml_backend_graph_compute failed. status=%s \n", __func__, ggml_status_to_string(status)); |
| 1998 | return false; |
| 1999 | } |
| 2000 | ggml_backend_tensor_get(out, &fdh, 0, ggml_nbytes(out)); |
| 2001 | |
| 2002 | gn[i] = (8.0*(double)fuh + (double)fd - (8.0*(double)fdh + (double)fu)) / (6.0*(double)eps); |
| 2003 | } else { |
| 2004 | gn[i] = (fu - fd) / (2.0f*eps); |
| 2005 | } |
| 2006 | |
| 2007 | ggml_backend_tensor_set(t, x0.data(), 0, ggml_nbytes(t)); |
| 2008 | } |
| 2009 | |
| 2010 | const double err = mean_abs_asymm(gn.data(), ga.data(), gn.size(), expect); |
| 2011 | if (err > max_maa_err()) { |
| 2012 | test_operation_info info(op_desc(out), vars(), ggml_backend_name(backend)); |
| 2013 | info.set_maa_error(err, max_maa_err()); |
| 2014 | output_printer->print_operation(info); |
| 2015 | ok = false; |
| 2016 | break; |
| 2017 | } |
| 2018 | if (!ok) { |
| 2019 | break; |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | // Create final test result |
| 2024 | test_operation_info final_info(op_desc(out), vars(), ggml_backend_name(backend)); |
| 2025 | if (!ok) { |
| 2026 | final_info.set_compare_failure(); |
| 2027 | } |
| 2028 | final_info.status = ok ? test_status_t::OK : test_status_t::FAIL; |
| 2029 | output_printer->print_operation(final_info); |
| 2030 | |
| 2031 | if (ok) { |
| 2032 | return true; |
| 2033 | } |
| 2034 | |
| 2035 | return false; |
| 2036 | } |
| 2037 | }; |
| 2038 | |
| 2039 | |
| 2040 | // #################################### |
| 2041 | // ## Section 2: GGML Op Definitions ## |
| 2042 | // #################################### |
| 2043 | |
| 2044 | |
| 2045 | // The following is an example showing the bare minimum for creating a test for a GGML op. |
| 2046 | |
| 2047 | // GGML_OP_EXAMPLE |
| 2048 | struct test_example : public test_case { |
| 2049 | // Always define these 2 or variants thereof: |
| 2050 | const ggml_type type; // The type of the input tensors. |
| 2051 | const std::array<int64_t, 4> ne; // The shape of the input tensors. |
| 2052 | // For some ops it's necessary to define multiple types or shapes for the inputs. |
| 2053 | // Or they may need additional parameters. |
| 2054 | |
| 2055 | // Put all parameters needed to fully define the test into one of the VARS_TO_STR macros. |
| 2056 | // In most cases these are just the properties of the struct that you defined above. |
| 2057 | // This is needed for info prints. |
| 2058 | std::string vars() override { |
| 2059 | return VARS_TO_STR2(type, ne); |
| 2060 | } |
| 2061 | |
| 2062 | // Define a constructor for the struct. |
| 2063 | // In most cases it will be sufficient to have the same arguments as the struct has properties |
| 2064 | // and just use initializer lists. |
| 2065 | test_example(ggml_type type = GGML_TYPE_F32, |
| 2066 | std::array<int64_t, 4> ne = {10, 5, 4, 3}) |
| 2067 | : type(type), ne(ne) {} |
| 2068 | |
| 2069 | // Define how a simple GGML compute graph can be constructed for the new GGML op. |
| 2070 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 2071 | // Step 1: create input tensors that don't depend on any other tensors: |
| 2072 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 2073 | ggml_set_name(a, "a"); // Setting names is optional but it's useful for debugging. |
| 2074 | |
| 2075 | ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 2076 | ggml_set_name(b, "b"); |
| 2077 | |
| 2078 | // Step 2: use the op that you want to test in the GGML compute graph. |
| 2079 | ggml_tensor * out = ggml_add(ctx, a, b); // For this example we're just doing a simple addition. |
| 2080 | ggml_set_name(out, "out"); |
| 2081 | |
| 2082 | // Step 3: return the output tensor. |
| 2083 | return out; |
| 2084 | } |
| 2085 | // In order to also check the gradients for your op, add calls like ggml_set_param(a) |
| 2086 | // immediately after you create the tensors. |
| 2087 | // This is optional and only makes sense if a backward pass has actually been implemented for the new op. |
| 2088 | }; |
| 2089 | |
| 2090 | |
| 2091 | // GGML_OP_UNARY |
| 2092 | struct test_unary : public test_case { |
| 2093 | const ggml_unary_op op; |
| 2094 | const ggml_type type; |
| 2095 | const std::array<int64_t, 4> ne_a; |
| 2096 | int v; // view (1 : non-contiguous a) |
| 2097 | |
| 2098 | std::string vars() override { |
| 2099 | return VARS_TO_STR3(type, ne_a, v); |
| 2100 | } |
| 2101 | |
| 2102 | double max_nmse_err(ggml_backend_t backend) override { |
| 2103 | // Metal computes f32 gelu_quick in f32, so an fp16 table in the CPU reference (NMSE about 5e-8) fails this |
| 2104 | ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(ggml_backend_get_device(backend)); |
| 2105 | if (op == GGML_UNARY_OP_GELU_QUICK && type == GGML_TYPE_F32 && strcmp(ggml_backend_reg_name(reg), "MTL") == 0) { |
| 2106 | return 1e-10; |
| 2107 | } |
| 2108 | return test_case::max_nmse_err(backend); |
| 2109 | } |
| 2110 | |
| 2111 | test_unary(ggml_unary_op op, |
| 2112 | ggml_type type = GGML_TYPE_F32, |
| 2113 | std::array<int64_t, 4> ne_a = {128, 2, 2, 2}, |
| 2114 | int v = 0) |
| 2115 | : op(op), type(type), ne_a(ne_a), v(v) {} |
| 2116 | |
| 2117 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 2118 | const bool grad_supported = op == GGML_UNARY_OP_ABS || op == GGML_UNARY_OP_SGN || op == GGML_UNARY_OP_NEG || |
| 2119 | op == GGML_UNARY_OP_STEP || op == GGML_UNARY_OP_RELU || op == GGML_UNARY_OP_SILU || |
| 2120 | op == GGML_UNARY_OP_EXPM1 || op == GGML_UNARY_OP_SOFTPLUS; |
| 2121 | |
| 2122 | ggml_tensor * a; |
| 2123 | if (v & 1) { |
| 2124 | auto ne = ne_a; |
| 2125 | ne[0] *= 3; |
| 2126 | ne[1] *= 2; |
| 2127 | ne[2] *= 5; |
| 2128 | ne[3] *= 4; |
| 2129 | a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 2130 | if (grad_supported) { |
| 2131 | ggml_set_param(a); |
| 2132 | } |
| 2133 | ggml_set_name(a, "a"); |
| 2134 | |
| 2135 | a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0); |
| 2136 | ggml_set_name(a, "view_of_a"); |
| 2137 | } else { |
| 2138 | a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 2139 | if (grad_supported) { |
| 2140 | ggml_set_param(a); |
| 2141 | } |
| 2142 | ggml_set_name(a, "a"); |
| 2143 | } |
| 2144 | |
| 2145 | ggml_tensor * out = ggml_unary(ctx, a, op); |
| 2146 | ggml_set_name(out, "out"); |
| 2147 | |
| 2148 | return out; |
| 2149 | } |
| 2150 | |
| 2151 | void initialize_tensors(ggml_context * ctx) override { |
| 2152 | float min = -150.f; |
| 2153 | float max = 150.f; |
| 2154 | |
| 2155 | // Keep FP16 exp/expm1 inputs in-range so all backends stay finite instead of |
| 2156 | // disagreeing on whether overflow saturates to max-F16 or produces +inf. |
| 2157 | if (type == GGML_TYPE_F16 && (op == GGML_UNARY_OP_EXP || op == GGML_UNARY_OP_EXPM1)) { |
| 2158 | min = -10.f; |
| 2159 | max = 10.f; |
| 2160 | } |
| 2161 | |
| 2162 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 2163 | // test extended range of values to check for NaNs in GELU |
| 2164 | init_tensor_uniform(t, min, max); |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | float grad_eps() override { |
| 2169 | return 15.0f; |
| 2170 | } |
| 2171 | |
| 2172 | std::vector<float> grad_expect() override { |
| 2173 | if (op == GGML_UNARY_OP_ABS) { |
| 2174 | return {-1.0f, 1.0f}; |
| 2175 | } |
| 2176 | if (op == GGML_UNARY_OP_SGN || op == GGML_UNARY_OP_STEP) { |
| 2177 | return {0.0f}; |
| 2178 | } |
| 2179 | if (op == GGML_UNARY_OP_RELU) { |
| 2180 | return {0.0f, 1.0f}; |
| 2181 | } |
| 2182 | return {}; |
| 2183 | } |
| 2184 | |
| 2185 | }; |
| 2186 | |
| 2187 | // GGML_OP_GLU |
| 2188 | struct test_glu : public test_case { |
| 2189 | const ggml_glu_op op; |
| 2190 | const ggml_type type; |
| 2191 | const std::array<int64_t, 4> ne_a; |
| 2192 | int v; // view (1 : non-contiguous a) |
| 2193 | bool swapped; |
| 2194 | |
| 2195 | std::string vars() override { |
| 2196 | return VARS_TO_STR4(type, ne_a, v, swapped); |
| 2197 | } |
| 2198 | |
| 2199 | double max_nmse_err(ggml_backend_t backend) override { |
| 2200 | // Metal computes f32 geglu_quick in f32, so an fp16 table in the CPU reference (NMSE about 5e-8) fails this |
| 2201 | ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(ggml_backend_get_device(backend)); |
| 2202 | if (op == GGML_GLU_OP_GEGLU_QUICK && type == GGML_TYPE_F32 && strcmp(ggml_backend_reg_name(reg), "MTL") == 0) { |
| 2203 | return 1e-10; |
| 2204 | } |
| 2205 | return test_case::max_nmse_err(backend); |
| 2206 | } |
| 2207 | |
| 2208 | test_glu(ggml_glu_op op, |
| 2209 | ggml_type type = GGML_TYPE_F32, |
| 2210 | std::array<int64_t, 4> ne_a = {128, 2, 2, 2}, |
| 2211 | int v = 0, |
| 2212 | bool swapped = false) |
| 2213 | : op(op), type(type), ne_a(ne_a), v(v), swapped(swapped) {} |
| 2214 | |
| 2215 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 2216 | ggml_tensor * a; |
| 2217 | if (v & 1) { |
| 2218 | auto ne = ne_a; ne[0] *= 3; |
| 2219 | a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 2220 | ggml_set_name(a, "a"); |
| 2221 | |
| 2222 | a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0); |
| 2223 | ggml_set_name(a, "view_of_a"); |
| 2224 | } else { |
| 2225 | a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 2226 | ggml_set_name(a, "a"); |
| 2227 | } |
| 2228 | |
| 2229 | ggml_tensor * out = ggml_glu(ctx, a, op, swapped); |
| 2230 | ggml_set_name(out, "out"); |
| 2231 | |
| 2232 | return out; |
| 2233 | } |
| 2234 | |
| 2235 | void initialize_tensors(ggml_context * ctx) override { |
| 2236 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 2237 | // test extended range of values to check for NaNs in GELU |
| 2238 | init_tensor_uniform(t, -150.f, 150.f); |
| 2239 | } |
| 2240 | } |
| 2241 | }; |
| 2242 | |
| 2243 | struct test_glu_split : public test_case { |
| 2244 | const ggml_glu_op op; |
| 2245 | const ggml_type type; |
| 2246 | const std::array<int64_t, 4> ne_a; |
| 2247 | int v; // view (1 : non-contiguous a) |
| 2248 | |
| 2249 | std::string vars() override { |
| 2250 | return VARS_TO_STR3(type, ne_a, v) + ",split"; |
| 2251 | } |
| 2252 | |
| 2253 | double max_nmse_err(ggml_backend_t backend) override { |
| 2254 | // Metal computes f32 geglu_quick in f32, so an fp16 table in the CPU reference (NMSE about 5e-8) fails this |
| 2255 | ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(ggml_backend_get_device(backend)); |
| 2256 | if (op == GGML_GLU_OP_GEGLU_QUICK && type == GGML_TYPE_F32 && strcmp(ggml_backend_reg_name(reg), "MTL") == 0) { |
| 2257 | return 1e-10; |
| 2258 | } |
| 2259 | return test_case::max_nmse_err(backend); |
| 2260 | } |
| 2261 | |
| 2262 | test_glu_split(ggml_glu_op op, |
| 2263 | ggml_type type = GGML_TYPE_F32, |
| 2264 | std::array<int64_t, 4> ne_a = {128, 2, 2, 2}, |
| 2265 | int v = 0) |
| 2266 | : op(op), type(type), ne_a(ne_a), v(v) {} |
| 2267 | |
| 2268 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 2269 | ggml_tensor * a; |
| 2270 | ggml_tensor * b; |
| 2271 | if (v & 1) { |
| 2272 | auto ne = ne_a; ne[0] *= 3; |
| 2273 | a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 2274 | ggml_set_param(a); |
| 2275 | ggml_set_name(a, "a"); |
| 2276 | |
| 2277 | a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0); |
| 2278 | ggml_set_name(a, "view_of_a"); |
| 2279 | |
| 2280 | b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 2281 | ggml_set_param(b); |
| 2282 | ggml_set_name(b, "b"); |
| 2283 | |
| 2284 | b = ggml_view_4d(ctx, b, ne_a[0], ne_a[1], ne_a[2], ne_a[3], b->nb[1], b->nb[2], b->nb[3], 0); |
| 2285 | ggml_set_name(a, "view_of_b"); |
| 2286 | } else { |
| 2287 | a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 2288 | ggml_set_param(a); |
| 2289 | ggml_set_name(a, "a"); |
| 2290 | |
| 2291 | b = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 2292 | ggml_set_param(b); |
| 2293 | ggml_set_name(b, "b"); |
| 2294 | } |
| 2295 | |
| 2296 | ggml_tensor * out = ggml_glu_split(ctx, a, b, op); |
| 2297 | ggml_set_name(out, "out"); |
| 2298 | |
| 2299 | return out; |
| 2300 | } |
| 2301 | |
| 2302 | void initialize_tensors(ggml_context * ctx) override { |
| 2303 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 2304 | // test extended range of values to check for NaNs in GELU |
| 2305 | init_tensor_uniform(t, -150.f, 150.f); |
| 2306 | } |
| 2307 | } |
| 2308 | }; |
| 2309 | |
| 2310 | struct test_swiglu_oai : public test_case { |
| 2311 | const ggml_type type; |
| 2312 | const std::array<int64_t, 4> ne_a; |
| 2313 | int v; // view (1 : non-contiguous a) |
| 2314 | float alpha; |
| 2315 | float limit; |
| 2316 | |
| 2317 | std::string vars() override { |
| 2318 | return VARS_TO_STR5(type, ne_a, v, alpha, limit); |
| 2319 | } |
| 2320 | |
| 2321 | test_swiglu_oai(ggml_type type = GGML_TYPE_F32, |
| 2322 | std::array<int64_t, 4> ne_a = {128, 2, 2, 2}, |
| 2323 | int v = 0, |
| 2324 | float alpha = 1.702f, |
| 2325 | float limit = 7.0f) |
| 2326 | : type(type), ne_a(ne_a), v(v), alpha(alpha), limit(limit) {} |
| 2327 | |
| 2328 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 2329 | ggml_tensor * a; |
| 2330 | ggml_tensor * b; |
| 2331 | if (v & 1) { |
| 2332 | auto ne = ne_a; ne[0] *= 3; |
| 2333 | a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 2334 | ggml_set_param(a); |
| 2335 | ggml_set_name(a, "a"); |
| 2336 | |
| 2337 | a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0); |
| 2338 | ggml_set_name(a, "view_of_a"); |
| 2339 | |
| 2340 | b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 2341 | ggml_set_param(b); |
| 2342 | ggml_set_name(b, "b"); |
| 2343 | |
| 2344 | b = ggml_view_4d(ctx, b, ne_a[0], ne_a[1], ne_a[2], ne_a[3], b->nb[1], b->nb[2], b->nb[3], 0); |
| 2345 | ggml_set_name(a, "view_of_b"); |
| 2346 | } else { |
| 2347 | a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 2348 | ggml_set_param(a); |
| 2349 | ggml_set_name(a, "a"); |
| 2350 | |
| 2351 | b = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 2352 | ggml_set_param(b); |
| 2353 | ggml_set_name(b, "b"); |
| 2354 | } |
| 2355 | |
| 2356 | ggml_tensor * out = ggml_swiglu_oai(ctx, a, b, alpha, limit); |
| 2357 | ggml_set_name(out, "out"); |
| 2358 | |
| 2359 | return out; |
| 2360 | } |
| 2361 | |
| 2362 | void initialize_tensors(ggml_context * ctx) override { |
| 2363 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 2364 | // test extended range of values to check for NaNs in GELU |
| 2365 | init_tensor_uniform(t, -150.f, 150.f); |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | double max_nmse_err() override { |
| 2370 | // F16 epsilon ~ 9.7e-4: relax tolerance vs the F32 default to absorb the |
| 2371 | // natural roundoff drift from the exp()/clamp() chain computed in F16. |
| 2372 | return type == GGML_TYPE_F16 ? 5e-6 : 1e-7; |
| 2373 | } |
| 2374 | }; |
| 2375 | |
| 2376 | struct test_swiglu_clamp : public test_case { |
| 2377 | const ggml_type type; |
| 2378 | const std::array<int64_t, 4> ne_a; |
| 2379 | int v; // view (1 : non-contiguous a) |
| 2380 | float limit; |
| 2381 | |
| 2382 | std::string vars() override { |
| 2383 | return VARS_TO_STR4(type, ne_a, v, limit); |
| 2384 | } |
| 2385 | |
| 2386 | test_swiglu_clamp(ggml_type type = GGML_TYPE_F32, |
| 2387 | std::array<int64_t, 4> ne_a = {128, 2, 2, 2}, |
| 2388 | int v = 0, |
| 2389 | float limit = 7.0f) |
| 2390 | : type(type), ne_a(ne_a), v(v), limit(limit) {} |
| 2391 | |
| 2392 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 2393 | ggml_tensor * a; |
| 2394 | ggml_tensor * b; |
| 2395 | if (v & 1) { |
| 2396 | auto ne = ne_a; ne[0] *= 3; |
| 2397 | a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 2398 | ggml_set_param(a); |
| 2399 | ggml_set_name(a, "a"); |
| 2400 | |
| 2401 | a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0); |
| 2402 | ggml_set_name(a, "view_of_a"); |
| 2403 | |
| 2404 | b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 2405 | ggml_set_param(b); |
| 2406 | ggml_set_name(b, "b"); |
| 2407 | |
| 2408 | b = ggml_view_4d(ctx, b, ne_a[0], ne_a[1], ne_a[2], ne_a[3], b->nb[1], b->nb[2], b->nb[3], 0); |
| 2409 | ggml_set_name(b, "view_of_b"); |
| 2410 | } else { |
| 2411 | a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 2412 | ggml_set_param(a); |
| 2413 | ggml_set_name(a, "a"); |
| 2414 | |
| 2415 | b = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 2416 | ggml_set_param(b); |
| 2417 | ggml_set_name(b, "b"); |
| 2418 | } |
| 2419 | |
| 2420 | ggml_tensor * out = ggml_swiglu_clamp(ctx, a, b, limit); |
| 2421 | ggml_set_name(out, "out"); |
| 2422 | |
| 2423 | return out; |
| 2424 | } |
| 2425 | |
| 2426 | void initialize_tensors(ggml_context * ctx) override { |
| 2427 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 2428 | init_tensor_uniform(t, -150.f, 150.f); |
| 2429 | } |
| 2430 | } |
| 2431 | |
| 2432 | double max_nmse_err() override { |
| 2433 | // F16 epsilon ~ 9.7e-4: relax tolerance vs the F32 default to absorb the |
| 2434 | // natural roundoff drift from the sigmoid()/clamp() chain computed in F16. |
| 2435 | return type == GGML_TYPE_F16 ? 5e-6 : 1e-7; |
| 2436 | } |
| 2437 | }; |
| 2438 | |
| 2439 | // GGML_OP_GET_ROWS |
| 2440 | struct test_get_rows : public test_case { |
| 2441 | const ggml_type type; |
| 2442 | const int n; // cols |
| 2443 | const int m; // rows |
| 2444 | const int r; // rows to get |
| 2445 | const int be1; // batch size |
| 2446 | const int be2; // batch size |
| 2447 | const bool v; // view src1 |
| 2448 | const bool vs0; // view src0 |
| 2449 | const int offset_cols; // // column offset of the view src0 |
| 2450 | |
| 2451 | std::string vars() override { |
| 2452 | return VARS_TO_STR9(type, n, m, r, be1, be2, v, vs0, offset_cols); |
| 2453 | } |
| 2454 | |
| 2455 | test_get_rows(ggml_type type = GGML_TYPE_F32, int n = 10, int m = 5, int r = 3, int be1 = 1, int be2 = 1, bool v = false, bool vs0 = false, int offset_cols = 0) |
| 2456 | : type(type), n(n), m(m), r(r), be1(be1), be2(be2), v(v), vs0(vs0), offset_cols(offset_cols) {} |
| 2457 | |
| 2458 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 2459 | ggml_tensor * in; |
| 2460 | if (vs0) { |
| 2461 | const int offset_rows = 3; |
| 2462 | const int padded_m = m + offset_rows; |
| 2463 | const int padded_n = n + offset_cols; |
| 2464 | ggml_tensor * in_padded = ggml_new_tensor_4d(ctx, type, padded_n, padded_m, be1, be2); |
| 2465 | ggml_set_name(in_padded, "in_padded"); |
| 2466 | in = ggml_view_4d(ctx, in_padded, n, m, be1, be2, |
| 2467 | in_padded->nb[1], in_padded->nb[2], in_padded->nb[3], |
| 2468 | offset_cols * in_padded->nb[0] + offset_rows * in_padded->nb[1]); |
| 2469 | ggml_set_name(in, "in_view"); |
| 2470 | } else { |
| 2471 | in = ggml_new_tensor_4d(ctx, type, n, m, be1, be2); |
| 2472 | ggml_set_name(in, "in"); |
| 2473 | } |
| 2474 | |
| 2475 | ggml_tensor * rows = ggml_new_tensor_3d(ctx, GGML_TYPE_I32, v ? r + 1 : r, be1, be2); |
| 2476 | ggml_set_name(rows, "rows"); |
| 2477 | if (v) { |
| 2478 | rows = ggml_view_3d(ctx, rows, r/2, be1, be2, rows->nb[1], rows->nb[2], rows->nb[0]); |
| 2479 | ggml_set_name(rows, "view_of_rows"); |
| 2480 | } |
| 2481 | |
| 2482 | const bool grad_supported = !vs0 && ggml_is_matrix(in) && ggml_is_vector(rows); |
| 2483 | if (grad_supported) { |
| 2484 | ggml_set_param(in); |
| 2485 | // rows is a constant input -> no gradients |
| 2486 | } |
| 2487 | |
| 2488 | ggml_tensor * out = ggml_get_rows(ctx, in, rows); |
| 2489 | ggml_set_name(out, "out"); |
| 2490 | |
| 2491 | return out; |
| 2492 | } |
| 2493 | |
| 2494 | void initialize_tensors(ggml_context * ctx) override { |
| 2495 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 2496 | if (ggml_is_view_op(t->op)) { |
| 2497 | continue; |
| 2498 | } |
| 2499 | if (t->type == GGML_TYPE_I32) { |
| 2500 | // rows |
| 2501 | std::vector<int> data(ggml_nelements(t)); |
| 2502 | for (size_t i = 0; i < data.size(); i++) { |
| 2503 | data[i] = rand() % m; |
| 2504 | } |
| 2505 | ggml_backend_tensor_set(t, data.data(), 0, data.size() * sizeof(int)); |
| 2506 | } else { |
| 2507 | init_tensor_uniform(t); |
| 2508 | } |
| 2509 | } |
| 2510 | } |
| 2511 | }; |
| 2512 | |
| 2513 | // GGML_OP_GET_ROWS on a weight also used by GGML_OP_MUL_MAT (tied embeddings) |
| 2514 | // that the preceding mul_mat may have put in the SYCL reorder layout. The |
| 2515 | // mul_mat runs first, so a backend that changes the weight layout in place |
| 2516 | // must still return the right rows from get_rows. |
| 2517 | struct test_get_rows_after_mul_mat : public test_case { |
| 2518 | const ggml_type type; |
| 2519 | const int n; // cols |
| 2520 | const int m; // rows |
| 2521 | const int r; // rows to get |
| 2522 | |
| 2523 | std::string vars() override { |
| 2524 | return VARS_TO_STR4(type, n, m, r); |
| 2525 | } |
| 2526 | |
| 2527 | test_get_rows_after_mul_mat(ggml_type type = GGML_TYPE_Q8_0, int n = 256, int m = 16, int r = 4) |
| 2528 | : type(type), n(n), m(m), r(r) {} |
| 2529 | |
| 2530 | double max_nmse_err() override { |
| 2531 | return 5e-4; // same as test_mul_mat |
| 2532 | } |
| 2533 | |
| 2534 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 2535 | ggml_tensor * w = ggml_new_tensor_2d(ctx, type, n, m); |
| 2536 | ggml_set_name(w, "w"); |
| 2537 | |
| 2538 | ggml_tensor * x = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n, 1); |
| 2539 | ggml_set_name(x, "x"); |
| 2540 | |
| 2541 | ggml_tensor * rows = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, r); |
| 2542 | ggml_set_name(rows, "rows"); |
| 2543 | |
| 2544 | ggml_tensor * mm = ggml_mul_mat(ctx, w, x); |
| 2545 | ggml_set_name(mm, "mm"); |
| 2546 | |
| 2547 | ggml_tensor * gr = ggml_get_rows(ctx, w, rows); |
| 2548 | ggml_set_name(gr, "gr"); |
| 2549 | |
| 2550 | // repeat(mm) is the first source, so mul_mat is computed before get_rows |
| 2551 | ggml_tensor * out = ggml_add(ctx, ggml_repeat(ctx, mm, gr), gr); |
| 2552 | ggml_set_name(out, "out"); |
| 2553 | |
| 2554 | return out; |
| 2555 | } |
| 2556 | |
| 2557 | void initialize_tensors(ggml_context * ctx) override { |
| 2558 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 2559 | if (t->type == GGML_TYPE_I32) { |
| 2560 | std::vector<int> data(ggml_nelements(t)); |
| 2561 | for (size_t i = 0; i < data.size(); i++) { |
| 2562 | data[i] = rand() % m; |
| 2563 | } |
| 2564 | ggml_backend_tensor_set(t, data.data(), 0, data.size() * sizeof(int)); |
| 2565 | } else { |
| 2566 | init_tensor_uniform(t); |
| 2567 | } |
| 2568 | } |
| 2569 | } |
| 2570 | }; |
| 2571 | |
| 2572 | // The persistent SYCL weight reorder installs on the first small-batch mul_mat; a |
| 2573 | // later >8-column mul_mat on the same weight goes through the fused-dequant GEMM / |
| 2574 | // conversion routes, which must decode the reordered layout (regression test for the |
| 2575 | // #29107 crash: the plain fused-dequant GEMM decoded standard layout unconditionally). |
| 2576 | struct test_mul_mat_reorder_then_big_batch : public test_case { |
| 2577 | const ggml_type type; |
| 2578 | const int n; // cols of the big batch |
| 2579 | const int m; // rows |
| 2580 | |
| 2581 | std::string vars() override { |
| 2582 | return VARS_TO_STR3(type, n, m); |
| 2583 | } |
| 2584 | |
| 2585 | test_mul_mat_reorder_then_big_batch(ggml_type type = GGML_TYPE_IQ3_S, int n = 64, int m = 64) |
| 2586 | : type(type), n(n), m(m) {} |
| 2587 | |
| 2588 | double max_nmse_err() override { |
| 2589 | return 5e-4; // same as test_mul_mat |
| 2590 | } |
| 2591 | |
| 2592 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 2593 | ggml_tensor * w = ggml_new_tensor_2d(ctx, type, 1*256, m); |
| 2594 | ggml_set_name(w, "w"); |
| 2595 | |
| 2596 | ggml_tensor * x1 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 1*256, 1); |
| 2597 | ggml_set_name(x1, "x1"); |
| 2598 | |
| 2599 | ggml_tensor * x2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 1*256, n); |
| 2600 | ggml_set_name(x2, "x2"); |
| 2601 | |
| 2602 | ggml_tensor * mm1 = ggml_mul_mat(ctx, w, x1); |
| 2603 | ggml_set_name(mm1, "mm1"); |
| 2604 | |
| 2605 | ggml_tensor * mm2 = ggml_mul_mat(ctx, w, x2); |
| 2606 | ggml_set_name(mm2, "mm2"); |
| 2607 | |
| 2608 | // mm1 is the first source of the final ADD, so the reorder-installing |
| 2609 | // small-batch mul_mat is computed before the big-batch one |
| 2610 | return ggml_add(ctx, ggml_repeat(ctx, mm1, mm2), mm2); |
| 2611 | } |
| 2612 | }; |
| 2613 | |
| 2614 | // GGML_OP_GET_ROWS_BACK |
| 2615 | struct test_get_rows_back : public test_case { |
| 2616 | const ggml_type type; |
| 2617 | const int n; // cols |
| 2618 | const int m; // rows |
| 2619 | const int r; // rows to get |
| 2620 | const int b; // batch size |
| 2621 | const bool v; // view (non-contiguous src1) |
| 2622 | |
| 2623 | std::string vars() override { |
| 2624 | return VARS_TO_STR6(type, n, m, r, b, v); |
| 2625 | } |
| 2626 | |
| 2627 | test_get_rows_back(ggml_type type = GGML_TYPE_F32, int n = 10, int m = 5, int r = 3, int b = 1, bool v = false) |
| 2628 | : type(type), n(n), m(m), r(r), b(b), v(v) {} |
| 2629 | |
| 2630 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 2631 | ggml_tensor * in_forward = ggml_new_tensor_3d(ctx, type, n, m, b); |
| 2632 | ggml_set_name(in_forward, "in_forward"); |
| 2633 | |
| 2634 | ggml_tensor * rows = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, r, b); |
| 2635 | ggml_set_name(rows, "rows"); |
| 2636 | if (v) { |
| 2637 | rows = ggml_view_2d(ctx, rows, r/2, b, rows->nb[1], 0); |
| 2638 | ggml_set_name(rows, "view_of_rows"); |
| 2639 | } |
| 2640 | |
| 2641 | ggml_tensor * grad = ggml_new_tensor_3d(ctx, type, n, r, b); |
| 2642 | ggml_set_name(grad, "grad"); |
| 2643 | |
| 2644 | ggml_tensor * out = ggml_get_rows_back(ctx, grad, rows, in_forward); |
| 2645 | ggml_set_name(out, "out"); |
| 2646 | |
| 2647 | return out; |
| 2648 | } |
| 2649 | |
| 2650 | void initialize_tensors(ggml_context * ctx) override { |
| 2651 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 2652 | if (t->type == GGML_TYPE_I32) { |
| 2653 | if (ggml_is_view_op(t->op)) { continue; } |
| 2654 | // rows |
| 2655 | std::vector<int> data(r*b); |
| 2656 | for (int i = 0; i < r*b; i++) { |
| 2657 | data[i] = rand() % m; |
| 2658 | } |
| 2659 | ggml_backend_tensor_set(t, data.data(), 0, r * b * sizeof(int)); |
| 2660 | } else { |
| 2661 | init_tensor_uniform(t); |
| 2662 | } |
| 2663 | } |
| 2664 | } |
| 2665 | }; |
| 2666 | |
| 2667 | static void init_set_rows_row_ids(ggml_tensor * t, int num_rows) { |
| 2668 | std::random_device rd; |
| 2669 | std::default_random_engine rng(rd()); |
| 2670 | for (int i2 = 0; i2 < t->ne[2]; i2++) { |
| 2671 | for (int i1 = 0; i1 < t->ne[1]; i1++) { |
| 2672 | // generate a shuffled subset of row indices |
| 2673 | std::vector<int64_t> data(num_rows); |
| 2674 | for (int i = 0; i < num_rows; i++) { |
| 2675 | data[i] = i; |
| 2676 | } |
| 2677 | std::shuffle(data.begin(), data.end(), rng); |
| 2678 | data.resize(t->ne[0]); |
| 2679 | |
| 2680 | const size_t offs = i1*t->nb[1] + i2*t->nb[2]; |
| 2681 | if (t->type == GGML_TYPE_I32) { |
| 2682 | // TODO: Make a template or something |
| 2683 | std::vector<int32_t> data_i32(t->ne[0]); |
| 2684 | for (int i = 0; i < t->ne[0]; i++) { |
| 2685 | data_i32[i] = static_cast<int32_t>(data[i]); |
| 2686 | } |
| 2687 | ggml_backend_tensor_set(t, data_i32.data(), offs, t->ne[0]*sizeof(int32_t)); |
| 2688 | } else { |
| 2689 | ggml_backend_tensor_set(t, data.data(), offs, t->ne[0]*sizeof(int64_t)); |
| 2690 | } |
| 2691 | } |
| 2692 | } |
| 2693 | } |
| 2694 | |
| 2695 | // GGML_OP_SET_ROWS |
| 2696 | struct test_set_rows : public test_case { |
| 2697 | const ggml_type type_src; |
| 2698 | const ggml_type type_dst; |
| 2699 | const ggml_type type_idx; |
| 2700 | const std::array<int64_t, 4> ne; |
| 2701 | const std::array<int, 2> nr23; // broadcast only dims 2 and 3 |
| 2702 | const int r; // rows to set |
| 2703 | const bool v; // view (non-contiguous src1) |
| 2704 | |
| 2705 | std::string vars() override { |
| 2706 | return VARS_TO_STR7(type_src, type_dst, type_idx, ne, nr23, r, v); |
| 2707 | } |
| 2708 | |
| 2709 | test_set_rows(ggml_type type_src, |
| 2710 | ggml_type type_dst, |
| 2711 | ggml_type type_idx, |
| 2712 | std::array<int64_t, 4> ne, |
| 2713 | std::array<int, 2> nr23, |
| 2714 | int r, bool v = false) |
| 2715 | : type_src(type_src), type_dst(type_dst), type_idx(type_idx), ne(ne), nr23(nr23), r(r), v(v) {} |
| 2716 | |
| 2717 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 2718 | ggml_tensor * dst = ggml_new_tensor_4d(ctx, type_dst, ne[0], ne[1], ne[2]*nr23[0], ne[3]*nr23[1]); |
| 2719 | ggml_set_name(dst, "dst"); |
| 2720 | |
| 2721 | ggml_tensor * src = ggml_new_tensor_4d(ctx, type_src, ne[0], r, ne[2]*nr23[0], ne[3]*nr23[1]); |
| 2722 | ggml_set_name(src, "src"); |
| 2723 | |
| 2724 | ggml_tensor * row_idxs = ggml_new_tensor_3d(ctx, type_idx, r, ne[2], ne[3]); |
| 2725 | ggml_set_name(row_idxs, "row_idxs"); |
| 2726 | |
| 2727 | if (v) { |
| 2728 | src = ggml_view_4d(ctx, src, ne[0], r/2, ne[2]*nr23[0], ne[3]*nr23[1], src->nb[1], src->nb[2], src->nb[3], 0); |
| 2729 | row_idxs = ggml_view_3d(ctx, row_idxs, r/2, ne[2], ne[3], row_idxs->nb[1], row_idxs->nb[2], 0); |
| 2730 | ggml_set_name(row_idxs, "view_of_rows"); |
| 2731 | } |
| 2732 | |
| 2733 | ggml_tensor * out = ggml_set_rows(ctx, dst, src, row_idxs); |
| 2734 | ggml_set_name(out, "out"); |
| 2735 | |
| 2736 | return out; |
| 2737 | } |
| 2738 | |
| 2739 | void initialize_tensors(ggml_context * ctx) override { |
| 2740 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 2741 | if (ggml_is_view_op(t->op)) { |
| 2742 | continue; |
| 2743 | } |
| 2744 | if (t->type == GGML_TYPE_I64 || t->type == GGML_TYPE_I32) { |
| 2745 | init_set_rows_row_ids(t, ne[1]); |
| 2746 | } else { |
| 2747 | init_tensor_uniform(t); |
| 2748 | } |
| 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | double max_nmse_err() override { |
| 2753 | if (type_dst == GGML_TYPE_Q2_0 || type_dst == GGML_TYPE_Q4_0 || type_dst == GGML_TYPE_Q4_1 || |
| 2754 | type_dst == GGML_TYPE_IQ4_NL || |
| 2755 | type_dst == GGML_TYPE_Q5_0 || type_dst == GGML_TYPE_Q5_1 || type_dst == GGML_TYPE_Q8_0) { |
| 2756 | // estimate what the max nmse error would be if one quantized value is |
| 2757 | // off by one. The test values are distributed in [-1,1], so it'll be |
| 2758 | // roughly (2.0 / 2^bits)^2, divided by the mean square value of the reference, |
| 2759 | // which is roughly 0.25 times the number of elements. |
| 2760 | double err_estimate = 1.0f/8.0f; |
| 2761 | if (type_src == GGML_TYPE_F16 && type_dst == GGML_TYPE_Q2_0) { |
| 2762 | err_estimate *= 4.0f; |
| 2763 | } |
| 2764 | if (type_dst == GGML_TYPE_Q5_0 || type_dst == GGML_TYPE_Q5_1) { |
| 2765 | err_estimate /= 2.0f; |
| 2766 | } |
| 2767 | if (type_dst == GGML_TYPE_Q8_0) { |
| 2768 | err_estimate /= 8.0f; |
| 2769 | } |
| 2770 | err_estimate *= err_estimate; |
| 2771 | if (type_src == GGML_TYPE_F16) { |
| 2772 | err_estimate *= 16.0f; |
| 2773 | } |
| 2774 | err_estimate /= 0.25f*float(ne[0] * r * ne[2]*nr23[0] * ne[3]*nr23[1]); |
| 2775 | return err_estimate; |
| 2776 | } |
| 2777 | return 1e-7; |
| 2778 | } |
| 2779 | |
| 2780 | // See dicussion here: https://github.com/ggml-org/llama.cpp/pull/23760#issuecomment-4566312209 |
| 2781 | double max_nmse_err(ggml_backend_t backend) override { |
| 2782 | ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(ggml_backend_get_device(backend)); |
| 2783 | if (type_dst == GGML_TYPE_Q8_0) { |
| 2784 | if (strcmp(ggml_backend_reg_name(reg), "WebGPU") == 0) { |
| 2785 | return std::max(test_case::max_nmse_err(backend), 2e-7); |
| 2786 | } |
| 2787 | if (strcmp(ggml_backend_reg_name(reg), "HTP") == 0) { |
| 2788 | return std::max(test_case::max_nmse_err(backend), 5e-6); |
| 2789 | } |
| 2790 | } |
| 2791 | return test_case::max_nmse_err(backend); |
| 2792 | } |
| 2793 | }; |
| 2794 | |
| 2795 | // GGML_OP_ROPE + GGML_OP_VIEW + GGML_OP_SET_ROWS |
| 2796 | struct test_rope_set_rows : public test_case { |
| 2797 | const ggml_type type; |
| 2798 | const ggml_type type_idx; |
| 2799 | const std::array<int64_t, 4> ne_a; |
| 2800 | int mode; |
| 2801 | const int n_dims; |
| 2802 | const int n_ctx{512}; |
| 2803 | |
| 2804 | std::string vars() override { |
| 2805 | return VARS_TO_STR5(type, type_idx, ne_a, mode, n_dims); |
| 2806 | } |
| 2807 | |
| 2808 | std::string op_desc(ggml_tensor * t) override { |
| 2809 | GGML_UNUSED(t); |
| 2810 | return "ROPE_SET_ROWS"; |
| 2811 | } |
| 2812 | |
| 2813 | bool run_whole_graph() override { return true; } |
| 2814 | |
| 2815 | test_rope_set_rows(ggml_type type, |
| 2816 | ggml_type type_idx, |
| 2817 | std::array<int64_t, 4> ne_a, |
| 2818 | int mode, |
| 2819 | int n_dims = 128) |
| 2820 | : type(type), type_idx(type_idx), ne_a(ne_a), mode(mode), n_dims(n_dims) {} |
| 2821 | |
| 2822 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 2823 | ggml_tensor * a = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, ne_a[0], ne_a[1], ne_a[2], 1); |
| 2824 | ggml_set_name(a, "a"); |
| 2825 | |
| 2826 | const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE; |
| 2827 | const bool is_vision = mode == GGML_ROPE_TYPE_VISION; |
| 2828 | |
| 2829 | ggml_tensor * pos; |
| 2830 | if (is_mrope || is_vision) { |
| 2831 | pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, ne_a[2] * 4); |
| 2832 | } else { |
| 2833 | pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, ne_a[2]); |
| 2834 | } |
| 2835 | ggml_set_name(pos, "pos"); |
| 2836 | |
| 2837 | float fs = 1.4245f; |
| 2838 | float ef = 0.7465f; |
| 2839 | float af = 1.4245f; |
| 2840 | ggml_tensor * freq = nullptr; |
| 2841 | |
| 2842 | ggml_tensor * rope = nullptr; |
| 2843 | if (is_mrope) { |
| 2844 | if (is_vision) { |
| 2845 | GGML_ASSERT(n_dims/4 > 0); |
| 2846 | int rope_sections[4] = {n_dims/4, n_dims/4, 0, 0}; // Vision-RoPE only use first two dimension for image (x, y) coordinate |
| 2847 | rope = ggml_rope_multi(ctx, a, pos, freq, n_dims/2, rope_sections, mode, 0, 10000.0f, fs, ef, af, 1.0f, 1.0f); |
| 2848 | } else { |
| 2849 | GGML_ASSERT(n_dims/3 > 0); |
| 2850 | int rope_sections[4] = {n_dims/3, n_dims/3, n_dims/3, 0}; |
| 2851 | rope = ggml_rope_multi(ctx, a, pos, freq, n_dims, rope_sections, mode, 0, 10000.0f, fs, ef, af, 1.0f, 1.0f); |
| 2852 | } |
| 2853 | } else { |
| 2854 | rope = ggml_rope(ctx, a, pos, n_dims, mode); |
| 2855 | } |
| 2856 | |
| 2857 | ggml_tensor * view = ggml_view_2d(ctx, rope, ne_a[0] * ne_a[1], ne_a[2], rope->nb[2], 0); |
| 2858 | |
| 2859 | ggml_tensor * dst = ggml_new_tensor_4d(ctx, type, ne_a[0] * ne_a[1], ne_a[2] * ne_a[3], 1, 1); |
| 2860 | ggml_set_name(dst, "dst"); |
| 2861 | |
| 2862 | ggml_tensor * row_idxs = ggml_new_tensor_3d(ctx, type_idx, ne_a[2], 1, 1); |
| 2863 | ggml_set_name(row_idxs, "row_idxs"); |
| 2864 | |
| 2865 | ggml_tensor * out = ggml_set_rows(ctx, dst, view, row_idxs); |
| 2866 | ggml_set_name(out, "out"); |
| 2867 | |
| 2868 | return out; |
| 2869 | } |
| 2870 | |
| 2871 | void initialize_tensors(ggml_context * ctx) override { |
| 2872 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 2873 | if (strcmp(t->name, "row_idxs") == 0) { |
| 2874 | if (ggml_is_view_op(t->op)) { |
| 2875 | continue; |
| 2876 | } |
| 2877 | init_set_rows_row_ids(t, ne_a[2]); |
| 2878 | } else if (t->type == GGML_TYPE_I32) { |
| 2879 | // pos |
| 2880 | const int num_pos_ids = (mode & GGML_ROPE_TYPE_MROPE) ? ne_a[2] * 4 : ne_a[2]; |
| 2881 | std::vector<int> data(num_pos_ids); |
| 2882 | for (int i = 0; i < num_pos_ids; i++) { |
| 2883 | data[i] = rand() % n_ctx; |
| 2884 | } |
| 2885 | ggml_backend_tensor_set(t, data.data(), 0, num_pos_ids * sizeof(int)); |
| 2886 | } else { |
| 2887 | if (t->ne[0] == n_dims/2) { |
| 2888 | // frequency factors in the range [0.9f, 1.1f] |
| 2889 | init_tensor_uniform(t, 0.9f, 1.1f); |
| 2890 | } else { |
| 2891 | init_tensor_uniform(t); |
| 2892 | } |
| 2893 | } |
| 2894 | } |
| 2895 | } |
| 2896 | }; |
| 2897 | |
| 2898 | // GGML_OP_RMS_NORM with optional GGML_OP_MUL, GGML_OP_ROPE, GGML_OP_VIEW and GGML_OP_SET_ROWS |
| 2899 | struct test_rms_norm_mul_rope : public test_case { |
| 2900 | const std::array<int64_t, 4> ne; |
| 2901 | const float eps; |
| 2902 | const bool multi_add; // test a sequence of adds feeding into rms_norm |
| 2903 | const bool mul; |
| 2904 | const bool rope; |
| 2905 | const bool set_rows; |
| 2906 | const bool broadcast; // multiply by a 1D [ne0] weight, as model norm weights are |
| 2907 | const ggml_type set_rows_type; |
| 2908 | int mode; |
| 2909 | |
| 2910 | std::string op_desc(ggml_tensor * t) override { |
| 2911 | GGML_UNUSED(t); |
| 2912 | return "RMS_NORM_MUL_ROPE"; |
| 2913 | } |
| 2914 | |
| 2915 | bool run_whole_graph() override { return true; } |
| 2916 | |
| 2917 | std::string vars() override { |
| 2918 | return VARS_TO_STR9(ne, eps, multi_add, mul, rope, set_rows, broadcast, mode, set_rows_type); |
| 2919 | } |
| 2920 | |
| 2921 | test_rms_norm_mul_rope(std::array<int64_t, 4> ne, float eps = 1e-6f, bool multi_add = false, |
| 2922 | bool set_rows = false, bool broadcast = false, int mode = GGML_ROPE_TYPE_NORMAL, |
| 2923 | bool mul = true, bool rope = true, ggml_type set_rows_type = GGML_TYPE_F16) |
| 2924 | : ne(ne), eps(eps), multi_add(multi_add), mul(mul), rope(rope), set_rows(set_rows), broadcast(broadcast), |
| 2925 | set_rows_type(set_rows_type), mode(mode) {} |
| 2926 | |
| 2927 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 2928 | ggml_tensor * a = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, ne[0], ne[1], ne[2], ne[3]); |
| 2929 | |
| 2930 | ggml_tensor * b = nullptr; |
| 2931 | ggml_tensor * c = nullptr; |
| 2932 | ggml_tensor * w = nullptr; |
| 2933 | |
| 2934 | if (multi_add || (mul && !broadcast)) { |
| 2935 | b = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, ne[0], ne[1], ne[2], 1); |
| 2936 | } |
| 2937 | if (multi_add) { |
| 2938 | c = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, ne[0], ne[1], ne[2], 1); |
| 2939 | } |
| 2940 | if (mul) { |
| 2941 | w = broadcast ? ggml_new_tensor_1d(ctx, GGML_TYPE_F32, ne[0]) : b; |
| 2942 | } |
| 2943 | |
| 2944 | if (multi_add) { |
| 2945 | a = ggml_add(ctx, ggml_add(ctx, a, b), c); |
| 2946 | } |
| 2947 | |
| 2948 | a = ggml_rms_norm(ctx, a, eps); |
| 2949 | |
| 2950 | if (mul) { |
| 2951 | a = ggml_mul(ctx, a, w); |
| 2952 | } |
| 2953 | |
| 2954 | if (rope) { |
| 2955 | const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE; |
| 2956 | ggml_tensor * pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, ne[2] * (is_mrope ? 4 : 1)); |
| 2957 | |
| 2958 | if (is_mrope) { |
| 2959 | const int n_dims = ne[0]; |
| 2960 | int sections[4] = { n_dims/3, n_dims/3, n_dims/3, 0 }; |
| 2961 | a = ggml_rope_multi(ctx, a, pos, nullptr, n_dims, sections, mode, 0, 10000.0f, 1.0f, 0.0f, 1.0f, 32.0f, 1.0f); |
| 2962 | } else { |
| 2963 | a = ggml_rope(ctx, a, pos, ne[0], mode); |
| 2964 | } |
| 2965 | } |
| 2966 | |
| 2967 | if (set_rows) { |
| 2968 | ggml_tensor * view = ggml_view_2d(ctx, a, ne[0] * ne[1], ne[2], a->nb[2], 0); |
| 2969 | |
| 2970 | ggml_tensor * dst = ggml_new_tensor_2d(ctx, set_rows_type, ne[0] * ne[1], ne[2] * 2); |
| 2971 | ggml_set_name(dst, "dst"); |
| 2972 | |
| 2973 | ggml_tensor * row_idxs = ggml_new_tensor_1d(ctx, GGML_TYPE_I64, ne[2]); |
| 2974 | ggml_set_name(row_idxs, "row_idxs"); |
| 2975 | |
| 2976 | a = ggml_set_rows(ctx, dst, view, row_idxs); |
| 2977 | } |
| 2978 | |
| 2979 | ggml_set_name(a, "out"); |
| 2980 | return a; |
| 2981 | } |
| 2982 | |
| 2983 | void initialize_tensors(ggml_context * ctx) override { |
| 2984 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 2985 | if (t->type == GGML_TYPE_I64) { |
| 2986 | init_set_rows_row_ids(t, ne[2] * 2); |
| 2987 | } else if (t->type == GGML_TYPE_I32) { |
| 2988 | std::vector<int32_t> data(ggml_nelements(t)); |
| 2989 | for (int32_t & value : data) { |
| 2990 | value = rand() % 512; |
| 2991 | } |
| 2992 | ggml_backend_tensor_set(t, data.data(), 0, ggml_nbytes(t)); |
| 2993 | } else { |
| 2994 | init_tensor_uniform(t); |
| 2995 | } |
| 2996 | } |
| 2997 | } |
| 2998 | |
| 2999 | double max_nmse_err() override { |
| 3000 | if (ne[0] == 8192) { |
| 3001 | return 5e-6; |
| 3002 | } |
| 3003 | // large positions amplify the difference between the GPU and CPU trig functions |
| 3004 | if (ne[2] > 8192) { |
| 3005 | return 1e-5; |
| 3006 | } |
| 3007 | return test_case::max_nmse_err(); |
| 3008 | } |
| 3009 | }; |
| 3010 | |
| 3011 | // GGML_OP_ARGMAX |
| 3012 | struct test_argmax : public test_case { |
| 3013 | const ggml_type type; |
| 3014 | const std::array<int64_t, 4> ne; |
| 3015 | |
| 3016 | std::string vars() override { |
| 3017 | return VARS_TO_STR2(type, ne); |
| 3018 | } |
| 3019 | |
| 3020 | test_argmax(ggml_type type = GGML_TYPE_F32, |
| 3021 | std::array<int64_t, 4> ne = {10, 100, 1, 1}) |
| 3022 | : type(type), ne(ne) {} |
| 3023 | |
| 3024 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3025 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 3026 | ggml_set_name(a, "a"); |
| 3027 | |
| 3028 | ggml_tensor * out = ggml_argmax(ctx, a); |
| 3029 | ggml_set_name(out, "out"); |
| 3030 | |
| 3031 | return out; |
| 3032 | } |
| 3033 | |
| 3034 | void initialize_tensors(ggml_context * ctx) override { |
| 3035 | std::random_device rd; |
| 3036 | std::default_random_engine rng(rd()); |
| 3037 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 3038 | if (t->type == GGML_TYPE_F32) { |
| 3039 | // initialize with unique values to avoid ties |
| 3040 | for (int64_t r = 0; r < ggml_nrows(t); r++) { |
| 3041 | std::vector<float> data(t->ne[0]); |
| 3042 | for (int i = 0; i < t->ne[0]; i++) { |
| 3043 | data[i] = i; |
| 3044 | } |
| 3045 | std::shuffle(data.begin(), data.end(), rng); |
| 3046 | ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(float)); |
| 3047 | } |
| 3048 | } else { |
| 3049 | init_tensor_uniform(t); |
| 3050 | } |
| 3051 | } |
| 3052 | } |
| 3053 | |
| 3054 | double max_nmse_err() override { |
| 3055 | return 0.0; |
| 3056 | } |
| 3057 | }; |
| 3058 | |
| 3059 | // GGML_OP_COUNT_EQUAL |
| 3060 | struct test_count_equal : public test_case { |
| 3061 | const ggml_type type; |
| 3062 | const std::array<int64_t, 4> ne; |
| 3063 | |
| 3064 | std::string vars() override { |
| 3065 | return VARS_TO_STR2(type, ne); |
| 3066 | } |
| 3067 | |
| 3068 | test_count_equal(ggml_type type = GGML_TYPE_F32, |
| 3069 | std::array<int64_t, 4> ne = {4, 500, 1, 1}) |
| 3070 | : type(type), ne(ne) {} |
| 3071 | |
| 3072 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3073 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 3074 | ggml_set_name(a, "a"); |
| 3075 | |
| 3076 | ggml_tensor * a_argmax = ggml_argmax(ctx, a); |
| 3077 | ggml_set_name(a_argmax, "a_argmax"); |
| 3078 | |
| 3079 | ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 3080 | ggml_set_name(b, "b"); |
| 3081 | |
| 3082 | ggml_tensor * b_argmax = ggml_argmax(ctx, b); |
| 3083 | ggml_set_name(b_argmax, "b_argmax"); |
| 3084 | |
| 3085 | ggml_tensor * out = ggml_count_equal(ctx, a_argmax, b_argmax); |
| 3086 | ggml_set_name(out, "out"); |
| 3087 | |
| 3088 | return out; |
| 3089 | } |
| 3090 | |
| 3091 | double max_nmse_err() override { |
| 3092 | return 0.0; |
| 3093 | } |
| 3094 | |
| 3095 | void initialize_tensors(ggml_context * ctx) override { |
| 3096 | std::random_device rd; |
| 3097 | std::default_random_engine rng(rd()); |
| 3098 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 3099 | if (t->type == GGML_TYPE_F32) { |
| 3100 | // initialize with unique values to avoid ties |
| 3101 | for (int64_t r = 0; r < ggml_nrows(t); r++) { |
| 3102 | std::vector<float> data(t->ne[0]); |
| 3103 | for (int i = 0; i < t->ne[0]; i++) { |
| 3104 | data[i] = i; |
| 3105 | } |
| 3106 | std::shuffle(data.begin(), data.end(), rng); |
| 3107 | ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(float)); |
| 3108 | } |
| 3109 | } else { |
| 3110 | init_tensor_uniform(t); |
| 3111 | } |
| 3112 | } |
| 3113 | } |
| 3114 | }; |
| 3115 | |
| 3116 | // GGML_OP_REPEAT |
| 3117 | struct test_repeat : public test_case { |
| 3118 | const ggml_type type; |
| 3119 | const std::array<int64_t, 4> ne; |
| 3120 | const std::array<int, 4> nr; |
| 3121 | |
| 3122 | std::string vars() override { |
| 3123 | return VARS_TO_STR3(type, ne, nr); |
| 3124 | } |
| 3125 | |
| 3126 | size_t op_size(ggml_tensor * t) override { |
| 3127 | return ggml_nbytes(t) * 2; |
| 3128 | } |
| 3129 | |
| 3130 | test_repeat(ggml_type type = GGML_TYPE_F32, |
| 3131 | std::array<int64_t, 4> ne = {10, 5, 4, 3}, |
| 3132 | std::array<int, 4> nr = {2, 2, 2, 2}) |
| 3133 | : type(type), ne(ne), nr(nr) {} |
| 3134 | |
| 3135 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3136 | ggml_tensor * target = ggml_new_tensor_4d(ctx, type, ne[0]*nr[0], ne[1]*nr[1], ne[2]*nr[2], ne[3]*nr[3]); |
| 3137 | ggml_set_name(target, "target"); |
| 3138 | |
| 3139 | ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 3140 | ggml_set_param(src); |
| 3141 | ggml_set_name(src, "src"); |
| 3142 | |
| 3143 | ggml_tensor * out = ggml_repeat(ctx, src, target); |
| 3144 | ggml_set_name(out, "out"); |
| 3145 | |
| 3146 | return out; |
| 3147 | } |
| 3148 | }; |
| 3149 | |
| 3150 | // GGML_OP_REPEAT_BACK |
| 3151 | struct test_repeat_back : public test_case { |
| 3152 | const ggml_type type; |
| 3153 | const std::array<int64_t, 4> ne; |
| 3154 | const std::array<int, 4> nr; |
| 3155 | const bool v; // whether src is a noncontiguous view |
| 3156 | |
| 3157 | std::string vars() override { |
| 3158 | return VARS_TO_STR4(type, ne, nr, v); |
| 3159 | } |
| 3160 | |
| 3161 | size_t op_size(ggml_tensor * t) override { |
| 3162 | return ggml_nbytes(t) * 2; |
| 3163 | } |
| 3164 | |
| 3165 | test_repeat_back(ggml_type type = GGML_TYPE_F32, |
| 3166 | std::array<int64_t, 4> ne = {8, 6, 4, 2}, |
| 3167 | std::array<int, 4> nr = {2, 2, 2, 2}, |
| 3168 | bool v = false) |
| 3169 | : type(type), ne(ne), nr(nr), v(v) {} |
| 3170 | |
| 3171 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3172 | ggml_tensor * src = ggml_new_tensor_4d(ctx, type, ne[0]*nr[0], ne[1]*nr[1], ne[2]*nr[2], ne[3]*nr[3]); |
| 3173 | ggml_set_name(src, "src"); |
| 3174 | |
| 3175 | if (v) { |
| 3176 | GGML_ASSERT(ne[0] % 2 == 0); |
| 3177 | GGML_ASSERT(ne[1] % 2 == 0); |
| 3178 | GGML_ASSERT(ne[2] % 2 == 0); |
| 3179 | GGML_ASSERT(ne[3] % 2 == 0); |
| 3180 | GGML_ASSERT(nr[0] % 2 == 0 || nr[0] == 1); |
| 3181 | GGML_ASSERT(nr[1] % 2 == 0 || nr[1] == 1); |
| 3182 | GGML_ASSERT(nr[2] % 2 == 0 || nr[2] == 1); |
| 3183 | GGML_ASSERT(nr[3] % 2 == 0 || nr[3] == 1); |
| 3184 | |
| 3185 | const int64_t ne00 = nr[0] == 1 ? src->ne[0] : src->ne[0] / 2; |
| 3186 | const int64_t ne01 = nr[1] == 1 ? src->ne[1] : src->ne[1] / 2; |
| 3187 | const int64_t ne02 = nr[2] == 1 ? src->ne[2] : src->ne[2] / 2; |
| 3188 | const int64_t ne03 = nr[3] == 1 ? src->ne[3] : src->ne[3] / 2; |
| 3189 | |
| 3190 | src = ggml_view_4d(ctx, src, ne00, ne01, ne02, ne03, src->nb[1], src->nb[2], src->nb[3], 0); |
| 3191 | } |
| 3192 | |
| 3193 | ggml_tensor * target = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 3194 | ggml_set_name(target, "target"); |
| 3195 | |
| 3196 | ggml_tensor * out = ggml_repeat_back(ctx, src, target); |
| 3197 | ggml_set_name(out, "out"); |
| 3198 | |
| 3199 | return out; |
| 3200 | } |
| 3201 | }; |
| 3202 | |
| 3203 | // GGML_OP_DUP |
| 3204 | struct test_dup : public test_case { |
| 3205 | const ggml_type type; |
| 3206 | const std::array<int64_t, 4> ne; |
| 3207 | const std::array<int64_t, 4> permute; |
| 3208 | bool _use_permute; |
| 3209 | |
| 3210 | std::string vars() override { |
| 3211 | std::string v = VARS_TO_STR2(type, ne); |
| 3212 | if (_use_permute) v += "," + VAR_TO_STR(permute); |
| 3213 | return v; |
| 3214 | } |
| 3215 | |
| 3216 | test_dup(ggml_type type = GGML_TYPE_F32, |
| 3217 | std::array<int64_t, 4> ne = {10, 10, 20, 1}, |
| 3218 | std::array<int64_t, 4> permute = {0, 0, 0, 0}) |
| 3219 | : type(type), ne(ne), permute(permute), |
| 3220 | _use_permute(permute[0] + permute[1] + permute[2] + permute[3] > 0) {} |
| 3221 | |
| 3222 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3223 | ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 3224 | ggml_set_param(src); |
| 3225 | ggml_set_name(src, "src"); |
| 3226 | |
| 3227 | if (_use_permute) { |
| 3228 | src = ggml_permute(ctx, src, permute[0], permute[1], permute[2], permute[3]); |
| 3229 | ggml_set_name(src, "src_permuted"); |
| 3230 | } |
| 3231 | |
| 3232 | ggml_tensor * out = ggml_dup(ctx, src); |
| 3233 | ggml_set_name(out, "out"); |
| 3234 | |
| 3235 | return out; |
| 3236 | } |
| 3237 | }; |
| 3238 | |
| 3239 | // GGML_OP_SET |
| 3240 | struct test_set : public test_case { |
| 3241 | const ggml_type type_src; |
| 3242 | const ggml_type type_dst; |
| 3243 | const std::array<int64_t, 4> ne; |
| 3244 | const int dim; |
| 3245 | const bool inplace; |
| 3246 | |
| 3247 | std::string vars() override { |
| 3248 | return VARS_TO_STR5(type_src, type_dst, ne, dim, inplace); |
| 3249 | } |
| 3250 | |
| 3251 | size_t op_size(ggml_tensor * t) override { |
| 3252 | return ggml_nbytes(t) + ggml_nbytes(t->src[0]); |
| 3253 | } |
| 3254 | |
| 3255 | test_set(ggml_type type_src = GGML_TYPE_F32, ggml_type type_dst = GGML_TYPE_F32, |
| 3256 | std::array<int64_t, 4> ne = {6, 5, 4, 3}, int dim = 1, bool inplace = false) |
| 3257 | : type_src(type_src), type_dst(type_dst), ne(ne), dim(dim), inplace(inplace) {} |
| 3258 | |
| 3259 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3260 | ggml_tensor * src = ggml_new_tensor(ctx, type_src, 4, ne.data()); |
| 3261 | ggml_set_param(src); |
| 3262 | ggml_set_name(src, "src"); |
| 3263 | |
| 3264 | auto ne_dst = ne; |
| 3265 | for (int i = 0; i < dim; ++i) { |
| 3266 | ne_dst[i] *= 2; |
| 3267 | } |
| 3268 | ggml_tensor * dst = ggml_new_tensor(ctx, type_dst, 4, ne_dst.data()); |
| 3269 | ggml_set_param(dst); |
| 3270 | ggml_set_name(dst, "dst"); |
| 3271 | |
| 3272 | size_t offset = 0; |
| 3273 | for (int i = 0; i < dim; ++i) { |
| 3274 | offset += ((ne_dst[i] - ne[i])/2)*dst->nb[i]; |
| 3275 | } |
| 3276 | ggml_tensor * out; |
| 3277 | if (inplace) { |
| 3278 | out = ggml_set_inplace(ctx, dst, src, |
| 3279 | // The backward pass requires setting a contiguous region: |
| 3280 | src->nb[1], src->nb[2], src->nb[3], offset); |
| 3281 | } else { |
| 3282 | out = ggml_set(ctx, dst, src, |
| 3283 | // The backward pass requires setting a contiguous region: |
| 3284 | src->nb[1], src->nb[2], src->nb[3], offset); |
| 3285 | } |
| 3286 | ggml_set_name(out, "out"); |
| 3287 | |
| 3288 | return out; |
| 3289 | } |
| 3290 | }; |
| 3291 | |
| 3292 | // GGML_OP_CPY |
| 3293 | struct test_cpy : public test_case { |
| 3294 | const ggml_type type_src; |
| 3295 | const ggml_type type_dst; |
| 3296 | const std::array<int64_t, 4> ne_src; |
| 3297 | const std::array<int64_t, 4> ne_dst; |
| 3298 | const std::array<int64_t, 4> permute_src; |
| 3299 | const std::array<int64_t, 4> permute_dst; |
| 3300 | const std::array<int64_t, 4> dst_alloc; // if set, dst is a view into a larger buffer (strided) |
| 3301 | bool _src_use_permute; |
| 3302 | bool _dst_use_permute; |
| 3303 | bool _src_transpose; |
| 3304 | bool _use_dst_shape; |
| 3305 | bool _use_dst_alloc; |
| 3306 | |
| 3307 | std::string vars() override { |
| 3308 | if (_use_dst_alloc) { |
| 3309 | return VARS_TO_STR8(type_src, type_dst, ne_src, ne_dst, permute_src, permute_dst, _src_transpose, dst_alloc); |
| 3310 | } |
| 3311 | if (_use_dst_shape) { |
| 3312 | return VARS_TO_STR7(type_src, type_dst, ne_src, ne_dst, permute_src, permute_dst, _src_transpose); |
| 3313 | } |
| 3314 | return VARS_TO_STR6(type_src, type_dst, ne_src, permute_src, permute_dst, _src_transpose); |
| 3315 | } |
| 3316 | |
| 3317 | int64_t total_elements() const { |
| 3318 | return ne_src[0] * ne_src[1] * ne_src[2] * ne_src[3]; |
| 3319 | } |
| 3320 | |
| 3321 | double max_nmse_err() override { |
| 3322 | if (type_src == type_dst) { |
| 3323 | return 0.0; |
| 3324 | } |
| 3325 | if (type_dst == GGML_TYPE_Q4_0 || type_dst == GGML_TYPE_Q4_1 || type_dst == GGML_TYPE_IQ4_NL || |
| 3326 | type_dst == GGML_TYPE_Q5_0 || type_dst == GGML_TYPE_Q5_1 || type_dst == GGML_TYPE_Q8_0) { |
| 3327 | // estimate what the max nmse error would be if one quantized value is |
| 3328 | // off by one. The test values are distributed in [-150,150], so it'll be |
| 3329 | // roughly (150*2.0 / 2^bits)^2, divided by the mean square value of the reference, |
| 3330 | // which is roughly 0.25*150^2 times the number of elements. |
| 3331 | double err_estimate = 1.0f/8.0f * 150.0f; |
| 3332 | if (type_dst == GGML_TYPE_IQ4_NL) { |
| 3333 | // iq4_nl values are a bit more spread out |
| 3334 | err_estimate *= 2.0f; |
| 3335 | } |
| 3336 | if (type_dst == GGML_TYPE_Q5_0 || type_dst == GGML_TYPE_Q5_1) { |
| 3337 | err_estimate /= 2.0f; |
| 3338 | } |
| 3339 | if (type_dst == GGML_TYPE_Q8_0) { |
| 3340 | err_estimate /= 8.0f; |
| 3341 | } |
| 3342 | err_estimate *= err_estimate; |
| 3343 | err_estimate /= (150.0f*150.0f*0.25f)*float(total_elements()); |
| 3344 | return err_estimate; |
| 3345 | } |
| 3346 | return 1e-6; |
| 3347 | } |
| 3348 | |
| 3349 | size_t op_size(ggml_tensor * t) override { |
| 3350 | return ggml_nbytes(t) + ggml_nbytes(t->src[0]); |
| 3351 | } |
| 3352 | |
| 3353 | test_cpy(ggml_type type_src = GGML_TYPE_F32, ggml_type type_dst = GGML_TYPE_F32, |
| 3354 | std::array<int64_t, 4> ne_src = {10, 10, 10, 1}, |
| 3355 | std::array<int64_t, 4> ne_dst = {-1, -1, -1, -1}, |
| 3356 | std::array<int64_t, 4> permute_src = {0, 0, 0, 0}, |
| 3357 | std::array<int64_t, 4> permute_dst = {0, 0, 0, 0}, |
| 3358 | bool transpose_src = false, |
| 3359 | std::array<int64_t, 4> dst_alloc = {0, 0, 0, 0}) |
| 3360 | : type_src(type_src), type_dst(type_dst), ne_src(ne_src), ne_dst(ne_dst), permute_src(permute_src), permute_dst(permute_dst), |
| 3361 | dst_alloc(dst_alloc), |
| 3362 | _src_use_permute(permute_src[0] + permute_src[1] + permute_src[2] + permute_src[3] > 0), |
| 3363 | _dst_use_permute(permute_dst[0] + permute_dst[1] + permute_dst[2] + permute_dst[3] > 0), |
| 3364 | _src_transpose(transpose_src), |
| 3365 | _use_dst_shape(ne_dst[0] >= 0 && ne_dst[1] >= 0 && ne_dst[2] >= 0 && ne_dst[3] >= 0), |
| 3366 | _use_dst_alloc(dst_alloc[0] > 0){} |
| 3367 | |
| 3368 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3369 | ggml_tensor * src = ggml_new_tensor(ctx, type_src, 4, ne_src.data()); |
| 3370 | ggml_set_param(src); |
| 3371 | ggml_set_name(src, "src"); |
| 3372 | |
| 3373 | if (_src_use_permute) { |
| 3374 | src = ggml_permute(ctx, src, permute_src[0], permute_src[1], permute_src[2], permute_src[3]); |
| 3375 | ggml_set_name(src, "src_permuted"); |
| 3376 | } |
| 3377 | |
| 3378 | if (_src_transpose) { |
| 3379 | src = ggml_transpose(ctx, src); |
| 3380 | ggml_set_name(src, "src_transposed"); |
| 3381 | } |
| 3382 | |
| 3383 | std::array<int64_t, 4> dst_ne = _use_dst_shape ? ne_dst : std::array<int64_t, 4>{src->ne[0], src->ne[1], src->ne[2], src->ne[3]}; |
| 3384 | ggml_tensor * dst; |
| 3385 | |
| 3386 | if (_use_dst_alloc) { |
| 3387 | // view a sub-block of a larger buffer -> strided dst |
| 3388 | ggml_tensor * dst_buf = ggml_new_tensor(ctx, type_dst, 4, dst_alloc.data()); |
| 3389 | ggml_set_name(dst_buf, "dst_buf"); |
| 3390 | dst = ggml_view_4d(ctx, dst_buf, dst_ne[0], dst_ne[1], dst_ne[2], dst_ne[3], |
| 3391 | dst_buf->nb[1], dst_buf->nb[2], dst_buf->nb[3], 0); |
| 3392 | ggml_set_name(dst, "dst_view"); |
| 3393 | } else { |
| 3394 | dst = ggml_new_tensor(ctx, type_dst, 4, dst_ne.data()); |
| 3395 | ggml_set_name(dst, "dst"); |
| 3396 | |
| 3397 | if (_dst_use_permute) { |
| 3398 | dst = ggml_permute(ctx, dst, permute_dst[0], permute_dst[1], permute_dst[2], permute_dst[3]); |
| 3399 | ggml_set_name(dst, "dst_permuted"); |
| 3400 | } |
| 3401 | } |
| 3402 | |
| 3403 | ggml_tensor * out = ggml_cpy(ctx, src, dst); |
| 3404 | ggml_set_name(out, "out"); |
| 3405 | |
| 3406 | return out; |
| 3407 | } |
| 3408 | |
| 3409 | void initialize_tensors(ggml_context * ctx) override { |
| 3410 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 3411 | // test extended range of values to check if casting between f32 and i32 is consistent |
| 3412 | init_tensor_uniform(t, -150.f, 150.f); |
| 3413 | } |
| 3414 | } |
| 3415 | }; |
| 3416 | |
| 3417 | // GGML_OP_CONT |
| 3418 | // permute = {0, 0, 0, 0} means no permutation: the source is transposed (or |
| 3419 | // view-sliced). A non-identity permute applies ggml_permute before ggml_cont. |
| 3420 | struct test_cont : public test_case { |
| 3421 | const ggml_type type; |
| 3422 | const std::array<int64_t, 4> ne; |
| 3423 | bool use_view_slice; |
| 3424 | const std::array<int64_t, 4> permute; |
| 3425 | |
| 3426 | std::string vars() override { |
| 3427 | return VARS_TO_STR4(type, ne, use_view_slice, permute); |
| 3428 | } |
| 3429 | |
| 3430 | test_cont(ggml_type type = GGML_TYPE_F32, |
| 3431 | std::array<int64_t, 4> ne = {10, 10, 10, 1}, |
| 3432 | bool use_view_slice = false, |
| 3433 | std::array<int64_t, 4> permute = {0, 0, 0, 0}) |
| 3434 | : type(type), ne(ne), use_view_slice(use_view_slice), permute(permute) {} |
| 3435 | |
| 3436 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3437 | ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 3438 | ggml_set_param(src); |
| 3439 | ggml_set_name(src, "src"); |
| 3440 | |
| 3441 | const bool permuted = permute[0] != 0 || permute[1] != 0 || permute[2] != 0 || permute[3] != 0; |
| 3442 | |
| 3443 | ggml_tensor * dst; |
| 3444 | if (permuted) { |
| 3445 | dst = ggml_permute(ctx, src, permute[0], permute[1], permute[2], permute[3]); |
| 3446 | ggml_set_name(dst, "src_permuted"); |
| 3447 | } else if (use_view_slice) { |
| 3448 | dst = ggml_view_4d(ctx, src, src->ne[0], 1, src->ne[2], src->ne[3], |
| 3449 | src->nb[1], src->nb[2], src->nb[3], src->nb[0] * (src->ne[1] - 1)); |
| 3450 | ggml_set_name(dst, "src_view_slice"); |
| 3451 | } else { |
| 3452 | dst = ggml_transpose(ctx, src); |
| 3453 | ggml_set_name(dst, "src_transposed"); |
| 3454 | } |
| 3455 | |
| 3456 | ggml_tensor * out = ggml_cont(ctx, dst); |
| 3457 | ggml_set_name(out, "out"); |
| 3458 | |
| 3459 | return out; |
| 3460 | } |
| 3461 | }; |
| 3462 | |
| 3463 | // GGML_OP_ADD |
| 3464 | // GGML_OP_SUB |
| 3465 | // GGML_OP_MUL |
| 3466 | // GGML_OP_DIV |
| 3467 | struct test_bin_bcast : public test_case { |
| 3468 | using op_t = ggml_tensor * (*) (ggml_context *, ggml_tensor *, ggml_tensor *); |
| 3469 | op_t op; |
| 3470 | const ggml_type type; |
| 3471 | const std::array<int64_t, 4> ne; |
| 3472 | const std::array<int, 4> nr; |
| 3473 | int nf; // number of fused ops, nf == 1 -> single op (no fusion) |
| 3474 | bool perm1; // permute src1? |
| 3475 | bool src_overlap; // src0 and src1 are overlapping views of the same buffer |
| 3476 | const ggml_type type_b; // type of src1, GGML_TYPE_COUNT -> same as src0 |
| 3477 | bool weights; |
| 3478 | |
| 3479 | bool run_whole_graph() override { return nf > 1; } |
| 3480 | |
| 3481 | std::string vars() override { |
| 3482 | std::string s = VARS_TO_STR6(type, ne, nr, nf, perm1, src_overlap); |
| 3483 | if (type_b != GGML_TYPE_COUNT) { |
| 3484 | s += "," + VAR_TO_STR(type_b); |
| 3485 | } |
| 3486 | if (weights) { |
| 3487 | s += ",weights=1"; |
| 3488 | } |
| 3489 | return s; |
| 3490 | } |
| 3491 | |
| 3492 | size_t op_size(ggml_tensor * t) override { |
| 3493 | return ggml_nbytes(t) * 3; |
| 3494 | } |
| 3495 | |
| 3496 | test_bin_bcast(op_t op, ggml_type type = GGML_TYPE_F32, |
| 3497 | std::array<int64_t, 4> ne = {10, 10, 1, 1}, |
| 3498 | std::array<int, 4> nr = {1, 2, 1, 1}, |
| 3499 | int nf = 1, |
| 3500 | bool perm1 = false, bool src_overlap = false, |
| 3501 | ggml_type type_b = GGML_TYPE_COUNT, bool weights = false) |
| 3502 | : op(op), type(type), ne(ne), nr(nr), nf(nf), perm1(perm1), src_overlap(src_overlap), type_b(type_b), weights(weights) {} |
| 3503 | |
| 3504 | bool use_weight_context() override { |
| 3505 | return weights; |
| 3506 | } |
| 3507 | |
| 3508 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3509 | return build_graph(ctx, ctx); |
| 3510 | } |
| 3511 | |
| 3512 | ggml_tensor * build_graph(ggml_context * ctx, ggml_context * ctx_weights) override { |
| 3513 | GGML_ASSERT(nf <= 16); |
| 3514 | |
| 3515 | ggml_tensor * a = ggml_new_tensor_4d(ctx, type, ne[0]*nr[0], ne[1]*nr[1], ne[2]*nr[2], ne[3]*nr[3]); |
| 3516 | ggml_set_name(a, "a"); |
| 3517 | |
| 3518 | const ggml_type tb = type_b == GGML_TYPE_COUNT ? type : type_b; |
| 3519 | ggml_context * ctx_b = weights ? ctx_weights : ctx; |
| 3520 | GGML_ASSERT(ctx_b); |
| 3521 | |
| 3522 | ggml_tensor * b[16]; |
| 3523 | for (int i = 0; i < nf; ++i) { |
| 3524 | if (perm1) { |
| 3525 | const int p[4] = { 1, 2, 0, 3 }; // hardcoded for now |
| 3526 | |
| 3527 | b[i] = ggml_new_tensor_4d(ctx_b, tb, ne[p[0]], ne[p[1]], ne[p[2]], ne[p[3]]); |
| 3528 | b[i] = ggml_permute(ctx_b, b[i], p[0], p[1], p[2], p[3]); |
| 3529 | } else if (src_overlap) { |
| 3530 | b[i] = ggml_view_4d(ctx, a, ne[0], ne[1], ne[2], 2 * (ne[3] / 3), a->nb[1], a->nb[2], a->nb[3], (ne[3] / 3) * a->nb[3]); |
| 3531 | } else { |
| 3532 | b[i] = ggml_new_tensor(ctx_b, tb, 4, ne.data()); |
| 3533 | } |
| 3534 | ggml_set_name(b[i], (std::string("b") + std::to_string(i)).c_str()); |
| 3535 | } |
| 3536 | |
| 3537 | // The backward pass supports broadcasting only for GGML_ADD: |
| 3538 | const bool grad_supported = op == ggml_add && ggml_are_same_shape(a, b[0]) && nf == 1 && !perm1; |
| 3539 | if (grad_supported) { |
| 3540 | ggml_set_param(a); |
| 3541 | ggml_set_param(b[0]); |
| 3542 | } |
| 3543 | |
| 3544 | ggml_tensor *out; |
| 3545 | |
| 3546 | if (src_overlap) { |
| 3547 | out = ggml_view_4d(ctx, a, ne[0], ne[1], ne[2], 2 * (ne[3] / 3), a->nb[1], a->nb[2], a->nb[3], 0); |
| 3548 | } else { |
| 3549 | out = a; |
| 3550 | } |
| 3551 | |
| 3552 | for (int i = 0; i < nf; ++i) { |
| 3553 | out = op(ctx, out, b[i]); |
| 3554 | } |
| 3555 | |
| 3556 | ggml_set_name(out, "out"); |
| 3557 | |
| 3558 | return out; |
| 3559 | } |
| 3560 | |
| 3561 | void initialize_tensors(ggml_context * ctx) override { |
| 3562 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 3563 | if (op == ggml_mul || op == ggml_div) { |
| 3564 | // MUL and DIV have numerical issues around zero: |
| 3565 | init_tensor_uniform(t, 0.9f, 1.1f); |
| 3566 | } else { |
| 3567 | init_tensor_uniform(t); |
| 3568 | } |
| 3569 | } |
| 3570 | } |
| 3571 | |
| 3572 | float grad_eps() override { |
| 3573 | return 0.1f * (op == ggml_mul ? ne[0]*ne[1]*ne[2]*ne[3] : 1); |
| 3574 | } |
| 3575 | |
| 3576 | bool grad_precise() override { |
| 3577 | return op == ggml_div; |
| 3578 | } |
| 3579 | |
| 3580 | double max_nmse_err() override { |
| 3581 | if (op == ggml_add && nf > 1) { |
| 3582 | // Fused ADDs can keep FP32 intermediates while the CPU rounds each ADD to FP16/BF16. |
| 3583 | if (type == GGML_TYPE_F16) { |
| 3584 | return 1e-6; |
| 3585 | } |
| 3586 | if (type == GGML_TYPE_BF16) { |
| 3587 | return 1e-4; |
| 3588 | } |
| 3589 | } |
| 3590 | return test_case::max_nmse_err(); |
| 3591 | } |
| 3592 | |
| 3593 | double max_maa_err() override { |
| 3594 | return op == ggml_add ? 1e-4 : 1e-3; |
| 3595 | } |
| 3596 | }; |
| 3597 | |
| 3598 | // set some ids to -1, so that MUL_MAT_ID and ADD_ID skip those slots |
| 3599 | // every 4th row skips all of its slots, the other rows skip only some of them |
| 3600 | static void mask_skipped_ids(std::vector<int32_t> & ids, int64_t row) { |
| 3601 | for (size_t i = 0; i < ids.size(); i++) { |
| 3602 | if (row % 4 == 1 || (row + (int64_t) i) % 3 == 0) { |
| 3603 | ids[i] = -1; |
| 3604 | } |
| 3605 | } |
| 3606 | } |
| 3607 | |
| 3608 | // GGML_OP_ADD_ID |
| 3609 | struct test_add_id : public test_case { |
| 3610 | const ggml_type type_a; |
| 3611 | const ggml_type type_b; |
| 3612 | const int64_t n_embd; |
| 3613 | const int64_t n_experts; |
| 3614 | const int64_t n_experts_used; |
| 3615 | const int64_t n_token; |
| 3616 | const bool skip; // set some ids to a negative value |
| 3617 | |
| 3618 | std::string vars() override { |
| 3619 | return VARS_TO_STR7(type_a, type_b, n_embd, n_experts, n_experts_used, n_token, skip); |
| 3620 | } |
| 3621 | |
| 3622 | size_t op_size(ggml_tensor * t) override { |
| 3623 | return ggml_nbytes(t) + ggml_nbytes(t->src[0]) + ggml_nbytes(t->src[2]); |
| 3624 | } |
| 3625 | |
| 3626 | test_add_id(ggml_type type_a = GGML_TYPE_F32, |
| 3627 | ggml_type type_b = GGML_TYPE_F32, |
| 3628 | int64_t n_embd = 128, |
| 3629 | int64_t n_experts = 16, |
| 3630 | int64_t n_experts_used = 8, |
| 3631 | int64_t n_token = 10, |
| 3632 | bool skip = false) |
| 3633 | : type_a(type_a), type_b(type_b), n_embd(n_embd), |
| 3634 | n_experts(n_experts), n_experts_used(n_experts_used), n_token(n_token), skip(skip) {} |
| 3635 | |
| 3636 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3637 | ggml_tensor * a = ggml_new_tensor_3d(ctx, type_a, n_embd, n_experts_used, n_token); |
| 3638 | ggml_tensor * b = ggml_new_tensor_2d(ctx, type_b, n_embd, n_experts); |
| 3639 | ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_experts, n_token); |
| 3640 | if (n_experts_used != n_experts) { |
| 3641 | ids = ggml_view_2d(ctx, ids, n_experts_used, n_token, ids->nb[1], 0); |
| 3642 | ggml_set_name(ids, "view_of_ids"); |
| 3643 | } |
| 3644 | |
| 3645 | ggml_tensor * out = ggml_add_id(ctx, a, b, ids); |
| 3646 | ggml_set_name(out, "out"); |
| 3647 | return out; |
| 3648 | } |
| 3649 | |
| 3650 | void initialize_tensors(ggml_context * ctx) override { |
| 3651 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 3652 | if (t->type == GGML_TYPE_I32) { |
| 3653 | if (ggml_is_view_op(t->op)) { continue; } |
| 3654 | std::random_device rd; |
| 3655 | std::default_random_engine rng(rd()); |
| 3656 | // ids |
| 3657 | for (int64_t r = 0; r < ggml_nrows(t); r++) { |
| 3658 | std::vector<int32_t> data(t->ne[0]); |
| 3659 | for (int i = 0; i < t->ne[0]; i++) { |
| 3660 | data[i] = i % n_experts; |
| 3661 | } |
| 3662 | std::shuffle(data.begin(), data.end(), rng); |
| 3663 | if (skip) { |
| 3664 | mask_skipped_ids(data, r); |
| 3665 | } |
| 3666 | ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(int32_t)); |
| 3667 | } |
| 3668 | } else { |
| 3669 | init_tensor_uniform(t); |
| 3670 | } |
| 3671 | } |
| 3672 | } |
| 3673 | }; |
| 3674 | |
| 3675 | // GGML_OP_SCALE |
| 3676 | struct test_scale : public test_case { |
| 3677 | const ggml_type type; |
| 3678 | const std::array<int64_t, 4> ne; |
| 3679 | float scale; |
| 3680 | float bias; |
| 3681 | bool inplace; |
| 3682 | int v; // view (1 : non-contiguous a) |
| 3683 | |
| 3684 | std::string vars() override { |
| 3685 | return VARS_TO_STR6(type, ne, scale, bias, inplace, v); |
| 3686 | } |
| 3687 | |
| 3688 | test_scale(ggml_type type = GGML_TYPE_F32, |
| 3689 | std::array<int64_t, 4> ne = {10, 10, 10, 10}, |
| 3690 | float scale = 2.0f, |
| 3691 | float bias = 0.0f, |
| 3692 | bool inplace = false, |
| 3693 | int v = 0) |
| 3694 | : type(type), ne(ne), scale(scale), bias(bias), inplace(inplace), v(v) {} |
| 3695 | |
| 3696 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3697 | auto ne_a = ne; |
| 3698 | if (v & 1) { |
| 3699 | ne_a[0] *= 3; |
| 3700 | ne_a[1] *= 2; |
| 3701 | ne_a[2] *= 5; |
| 3702 | ne_a[3] *= 4; |
| 3703 | } |
| 3704 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 3705 | ggml_set_param(a); |
| 3706 | ggml_set_name(a, "a"); |
| 3707 | |
| 3708 | if (v & 1) { |
| 3709 | a = ggml_view_4d(ctx, a, ne[0], ne[1], ne[2], ne[3], a->nb[1], a->nb[2], a->nb[3], 0); |
| 3710 | ggml_set_name(a, "view_of_a"); |
| 3711 | } |
| 3712 | |
| 3713 | ggml_tensor * out; |
| 3714 | if (inplace) { |
| 3715 | out = ggml_scale_bias_inplace(ctx, a, scale, bias); |
| 3716 | } else { |
| 3717 | out = ggml_scale_bias(ctx, a, scale, bias); |
| 3718 | } |
| 3719 | ggml_set_name(out, "out"); |
| 3720 | |
| 3721 | return out; |
| 3722 | } |
| 3723 | }; |
| 3724 | |
| 3725 | // GGML_OP_SCALE + GGML_UNARY_OP_TANH + GGML_OP_SCALE |
| 3726 | struct test_softcap : public test_case { |
| 3727 | const ggml_type type; |
| 3728 | const std::array<int64_t, 4> ne; |
| 3729 | float softcap; |
| 3730 | |
| 3731 | std::string op_desc(ggml_tensor * t) override { |
| 3732 | GGML_UNUSED(t); |
| 3733 | return "SOFTCAP"; |
| 3734 | } |
| 3735 | |
| 3736 | bool run_whole_graph() override { return true; } |
| 3737 | |
| 3738 | std::string vars() override { |
| 3739 | return VARS_TO_STR3(type, ne, softcap); |
| 3740 | } |
| 3741 | |
| 3742 | test_softcap(ggml_type type = GGML_TYPE_F32, |
| 3743 | std::array<int64_t, 4> ne = {10, 10, 10, 10}, |
| 3744 | float softcap = 30.0f) |
| 3745 | : type(type), ne(ne), softcap(softcap) {} |
| 3746 | |
| 3747 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3748 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 3749 | |
| 3750 | ggml_set_param(a); |
| 3751 | ggml_set_name(a, "a"); |
| 3752 | |
| 3753 | ggml_tensor * out = ggml_scale(ctx, ggml_tanh(ctx, ggml_scale(ctx, a, 1.0f / softcap)), softcap); |
| 3754 | ggml_set_name(out, "out"); |
| 3755 | |
| 3756 | return out; |
| 3757 | } |
| 3758 | }; |
| 3759 | |
| 3760 | // GGML_OP_SILU_BACK |
| 3761 | struct test_silu_back : public test_case { |
| 3762 | const ggml_type type; |
| 3763 | const std::array<int64_t, 4> ne; |
| 3764 | float eps; |
| 3765 | |
| 3766 | std::string vars() override { |
| 3767 | return VARS_TO_STR3(type, ne, eps); |
| 3768 | } |
| 3769 | |
| 3770 | test_silu_back(ggml_type type = GGML_TYPE_F32, |
| 3771 | std::array<int64_t, 4> ne = {64, 5, 4, 3}, |
| 3772 | float eps = 1e-6f) |
| 3773 | : type(type), ne(ne), eps(eps) {} |
| 3774 | |
| 3775 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3776 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 3777 | ggml_set_name(a, "a"); |
| 3778 | |
| 3779 | ggml_tensor * grad = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 3780 | ggml_set_name(grad, "grad"); |
| 3781 | |
| 3782 | ggml_tensor * out = ggml_silu_back(ctx, a, grad); |
| 3783 | ggml_set_name(out, "out"); |
| 3784 | |
| 3785 | return out; |
| 3786 | } |
| 3787 | |
| 3788 | bool grad_precise() override { |
| 3789 | return true; |
| 3790 | } |
| 3791 | }; |
| 3792 | |
| 3793 | // GGML_OP_NORM |
| 3794 | struct test_norm : public test_case { |
| 3795 | const ggml_type type; |
| 3796 | const std::array<int64_t, 4> ne; |
| 3797 | const bool v; // whether a is a non-contiguous view |
| 3798 | const float eps; |
| 3799 | const bool noncontig_rows; |
| 3800 | |
| 3801 | std::string vars() override { |
| 3802 | return VARS_TO_STR5(type, ne, v, eps, noncontig_rows); |
| 3803 | } |
| 3804 | |
| 3805 | test_norm(ggml_type type = GGML_TYPE_F32, |
| 3806 | std::array<int64_t, 4> ne = {64, 5, 4, 3}, |
| 3807 | bool v = false, |
| 3808 | float eps = 1e-6f, |
| 3809 | bool noncontig_rows = false) |
| 3810 | : type(type), ne(ne), v(v), eps(eps), noncontig_rows(noncontig_rows) {} |
| 3811 | |
| 3812 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3813 | const std::array<int64_t, 4> ne_a = noncontig_rows ? |
| 3814 | std::array<int64_t, 4>{ ne[1], ne[0], ne[2], ne[3] } : ne; |
| 3815 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 3816 | ggml_set_name(a, "a"); |
| 3817 | |
| 3818 | if (noncontig_rows) { |
| 3819 | a = ggml_permute(ctx, a, 1, 0, 2, 3); |
| 3820 | ggml_set_name(a, "permuted a"); |
| 3821 | } |
| 3822 | if (v) { |
| 3823 | a = ggml_view_4d(ctx, a, a->ne[0]/2, a->ne[1]/2, a->ne[2]/2, a->ne[3]/2, a->nb[1], a->nb[2], a->nb[3], 0); |
| 3824 | ggml_set_name(a, "view of a"); |
| 3825 | } |
| 3826 | |
| 3827 | ggml_tensor * out = ggml_norm(ctx, a, eps); |
| 3828 | ggml_set_name(out, "out"); |
| 3829 | |
| 3830 | return out; |
| 3831 | } |
| 3832 | }; |
| 3833 | |
| 3834 | // GGML_OP_NORM + GGML_OP_MUL + GGML_OP_ADD |
| 3835 | struct test_norm_mul_add : public test_case { |
| 3836 | const ggml_type type; |
| 3837 | const std::array<int64_t, 4> ne; |
| 3838 | float eps; |
| 3839 | const bool broadcast; |
| 3840 | |
| 3841 | std::string op_desc(ggml_tensor * t) override { |
| 3842 | GGML_UNUSED(t); |
| 3843 | return "NORM_MUL_ADD"; |
| 3844 | } |
| 3845 | |
| 3846 | bool run_whole_graph() override { return true; } |
| 3847 | |
| 3848 | std::string vars() override { |
| 3849 | return VARS_TO_STR4(type, ne, eps, broadcast); |
| 3850 | } |
| 3851 | |
| 3852 | test_norm_mul_add(ggml_type type = GGML_TYPE_F32, |
| 3853 | std::array<int64_t, 4> ne = {128, 2, 1, 1}, |
| 3854 | float eps = 1e-5f, |
| 3855 | bool broadcast = false) |
| 3856 | : type(type), ne(ne), eps(eps), broadcast(broadcast) {} |
| 3857 | |
| 3858 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3859 | std::array<int64_t, 4> broadcast_dims = {ne[0], ne[1] * 2, ne[2] * 2, ne[3] * 2}; |
| 3860 | |
| 3861 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, broadcast ? broadcast_dims.data() : ne.data()); |
| 3862 | ggml_tensor * w = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 3863 | ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 3864 | ggml_set_param(a); ggml_set_param(w); ggml_set_param(b); |
| 3865 | ggml_set_name(a, "a"); ggml_set_name(w, "w"); ggml_set_name(b, "b"); |
| 3866 | |
| 3867 | // Use a, w and b early to avoid OP_NONE in graph |
| 3868 | a = ggml_add(ctx, ggml_add(ctx, a, w), b); |
| 3869 | |
| 3870 | ggml_tensor * n = ggml_norm(ctx, a, eps); |
| 3871 | ggml_tensor * m = ggml_mul(ctx, n, w); |
| 3872 | ggml_tensor * out = ggml_add(ctx, m, b); |
| 3873 | ggml_set_name(out, "out"); |
| 3874 | return out; |
| 3875 | } |
| 3876 | }; |
| 3877 | // GGML_OP_NORM/RMS_NORM + GGML_OP_SCALE |
| 3878 | struct test_norm_scale : public test_case { |
| 3879 | const ggml_type type; |
| 3880 | const std::array<int64_t, 4> ne; |
| 3881 | const float eps; |
| 3882 | const bool rms; |
| 3883 | const float scale; |
| 3884 | |
| 3885 | std::string vars() override { |
| 3886 | return VARS_TO_STR5(type, ne, eps, rms, scale); |
| 3887 | } |
| 3888 | |
| 3889 | test_norm_scale(ggml_type type = GGML_TYPE_F32, |
| 3890 | std::array<int64_t, 4> ne = {64, 5, 4, 3}, |
| 3891 | float eps = 1e-6f, |
| 3892 | bool rms = false, |
| 3893 | float scale = 1.5f) |
| 3894 | : type(type), ne(ne), eps(eps), rms(rms), scale(scale) {} |
| 3895 | |
| 3896 | std::string op_desc(ggml_tensor * t) override { |
| 3897 | GGML_UNUSED(t); |
| 3898 | return rms ? "RMS_NORM_SCALE" : "NORM_SCALE"; |
| 3899 | } |
| 3900 | |
| 3901 | bool run_whole_graph() override { return true; } |
| 3902 | |
| 3903 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3904 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 3905 | ggml_set_name(a, "a"); |
| 3906 | |
| 3907 | ggml_tensor * n = rms ? ggml_rms_norm(ctx, a, eps) : ggml_norm(ctx, a, eps); |
| 3908 | ggml_tensor * out = ggml_scale(ctx, n, scale); |
| 3909 | ggml_set_name(out, "out"); |
| 3910 | |
| 3911 | return out; |
| 3912 | } |
| 3913 | }; |
| 3914 | |
| 3915 | // GGML_OP_RMS_NORM |
| 3916 | struct test_rms_norm : public test_case { |
| 3917 | const ggml_type type; |
| 3918 | const std::array<int64_t, 4> ne; |
| 3919 | const bool v; // whether a is a non-contiguous view |
| 3920 | const float eps; |
| 3921 | const bool inplace; // whether to do the operation inplace |
| 3922 | |
| 3923 | std::string vars() override { |
| 3924 | return VARS_TO_STR5(type, ne, v, eps, inplace); |
| 3925 | } |
| 3926 | |
| 3927 | test_rms_norm(ggml_type type = GGML_TYPE_F32, |
| 3928 | std::array<int64_t, 4> ne = {64, 5, 4, 3}, |
| 3929 | bool v = false, |
| 3930 | float eps = 1e-6f, |
| 3931 | bool inplace = false) |
| 3932 | : type(type), ne(ne), v(v), eps(eps), inplace(inplace) {} |
| 3933 | |
| 3934 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3935 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 3936 | ggml_set_param(a); |
| 3937 | ggml_set_name(a, "a"); |
| 3938 | |
| 3939 | if (v) { |
| 3940 | a = ggml_view_4d(ctx, a, a->ne[0]/2, a->ne[1]/2, a->ne[2]/2, a->ne[3]/2, a->nb[1], a->nb[2], a->nb[3], 0); |
| 3941 | ggml_set_name(a, "view of a"); |
| 3942 | } |
| 3943 | |
| 3944 | ggml_tensor * out; |
| 3945 | if (inplace) { |
| 3946 | out = ggml_rms_norm_inplace(ctx, a, eps); |
| 3947 | } else { |
| 3948 | out = ggml_rms_norm(ctx, a, eps); |
| 3949 | } |
| 3950 | ggml_set_name(out, "out"); |
| 3951 | |
| 3952 | return out; |
| 3953 | } |
| 3954 | |
| 3955 | void initialize_tensors(ggml_context * ctx) override { |
| 3956 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 3957 | init_tensor_uniform(t, -10.f, 10.f); |
| 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | float grad_eps() override { |
| 3962 | return 1.0f; |
| 3963 | } |
| 3964 | |
| 3965 | bool grad_precise() override { |
| 3966 | return true; |
| 3967 | } |
| 3968 | }; |
| 3969 | |
| 3970 | // GGML_OP_RMS_NORM_BACK |
| 3971 | struct test_rms_norm_back : public test_case { |
| 3972 | const ggml_type type; |
| 3973 | const std::array<int64_t, 4> ne; |
| 3974 | const float eps; |
| 3975 | |
| 3976 | std::string vars() override { |
| 3977 | return VARS_TO_STR3(type, ne, eps); |
| 3978 | } |
| 3979 | |
| 3980 | test_rms_norm_back(ggml_type type = GGML_TYPE_F32, |
| 3981 | std::array<int64_t, 4> ne = {64, 5, 4, 3}, |
| 3982 | float eps = 1e-6f) |
| 3983 | : type(type), ne(ne), eps(eps) {} |
| 3984 | |
| 3985 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 3986 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 3987 | ggml_set_name(a, "a"); |
| 3988 | |
| 3989 | ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 3990 | ggml_set_name(b, "b"); |
| 3991 | |
| 3992 | ggml_tensor * out = ggml_rms_norm_back(ctx, a, b, eps); |
| 3993 | ggml_set_name(out, "out"); |
| 3994 | |
| 3995 | return out; |
| 3996 | } |
| 3997 | |
| 3998 | void initialize_tensors(ggml_context * ctx) override { |
| 3999 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 4000 | init_tensor_uniform(t, -10.f, 10.f); |
| 4001 | } |
| 4002 | } |
| 4003 | }; |
| 4004 | |
| 4005 | // GGML_OP_RMS_NORM + GGML_OP_MUL + GGML_OP_ADD (+ GGML_OP_MUL) |
| 4006 | struct test_rms_norm_mul_add : public test_case { |
| 4007 | const ggml_type type; |
| 4008 | const std::array<int64_t, 4> ne; |
| 4009 | const float eps; |
| 4010 | const bool broadcast; |
| 4011 | const bool multi_add; // test a sequence of adds feeding into rms_norm |
| 4012 | const bool post_mul; |
| 4013 | const bool alias_rms_input; |
| 4014 | const bool weight_broadcast; |
| 4015 | |
| 4016 | std::string op_desc(ggml_tensor * t) override { |
| 4017 | GGML_UNUSED(t); |
| 4018 | return "RMS_NORM_MUL_ADD"; |
| 4019 | } |
| 4020 | |
| 4021 | bool run_whole_graph() override { return true; } |
| 4022 | |
| 4023 | std::string vars() override { |
| 4024 | return VARS_TO_STR8(type, ne, eps, broadcast, multi_add, post_mul, alias_rms_input, weight_broadcast); |
| 4025 | } |
| 4026 | |
| 4027 | test_rms_norm_mul_add(ggml_type type = GGML_TYPE_F32, |
| 4028 | std::array<int64_t, 4> ne = {64, 5, 4, 3}, |
| 4029 | float eps = 1e-6f, bool broadcast = false, bool multi_add = false, bool post_mul = false, |
| 4030 | bool alias_rms_input = false, bool weight_broadcast = false) |
| 4031 | : type(type), ne(ne), eps(eps), broadcast(broadcast), multi_add(multi_add), post_mul(post_mul), |
| 4032 | alias_rms_input(alias_rms_input), weight_broadcast(weight_broadcast) {} |
| 4033 | |
| 4034 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 4035 | std::array<int64_t, 4> broadcast_dims = {ne[0]*2, ne[1]*3, ne[2]*3, ne[3]*4}; |
| 4036 | |
| 4037 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, broadcast ? broadcast_dims.data() : ne.data()); |
| 4038 | ggml_tensor * b = weight_broadcast ? ggml_new_tensor_1d(ctx, type, ne[0]) : ggml_new_tensor(ctx, type, 4, ne.data()); |
| 4039 | ggml_tensor * c = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 4040 | ggml_tensor * d = nullptr; |
| 4041 | |
| 4042 | ggml_set_param(a); |
| 4043 | ggml_set_name(a, "a"); |
| 4044 | ggml_set_param(b); |
| 4045 | ggml_set_name(b, "b"); |
| 4046 | ggml_set_param(c); |
| 4047 | ggml_set_name(c, "c"); |
| 4048 | |
| 4049 | // Use a, b and c early, so we don't end up with an OP_NONE between rms_norm and mul |
| 4050 | a = ggml_add(ctx, ggml_add(ctx, a, b), c); |
| 4051 | if (post_mul) { |
| 4052 | d = ggml_new_tensor_1d(ctx, type, 1); |
| 4053 | ggml_set_param(d); |
| 4054 | ggml_set_name(d, "d"); |
| 4055 | a = ggml_add(ctx, a, d); |
| 4056 | } |
| 4057 | if (multi_add) { |
| 4058 | a = ggml_add(ctx, ggml_add(ctx, a, b), c); |
| 4059 | } |
| 4060 | ggml_tensor * mul = ggml_mul(ctx, ggml_rms_norm(ctx, a, eps), b); |
| 4061 | ggml_tensor * out = alias_rms_input ? ggml_add_inplace(ctx, a, mul) : ggml_add(ctx, mul, c); |
| 4062 | if (post_mul) { |
| 4063 | out = ggml_mul(ctx, out, d); |
| 4064 | } |
| 4065 | ggml_set_name(out, "out"); |
| 4066 | |
| 4067 | return out; |
| 4068 | } |
| 4069 | |
| 4070 | void initialize_tensors(ggml_context * ctx) override { |
| 4071 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 4072 | init_tensor_uniform(t, -10.f, 10.f); |
| 4073 | } |
| 4074 | } |
| 4075 | |
| 4076 | float grad_eps() override { |
| 4077 | return 1.0f; |
| 4078 | } |
| 4079 | |
| 4080 | bool grad_precise() override { |
| 4081 | return true; |
| 4082 | } |
| 4083 | }; |
| 4084 | |
| 4085 | // GGML_OP_ADD + GGML_OP_ADD (fused residual chain) |
| 4086 | struct test_add_add : public test_case { |
| 4087 | const ggml_type type; |
| 4088 | const ggml_type type_addend; |
| 4089 | const std::array<int64_t, 4> ne; |
| 4090 | const bool broadcast; |
| 4091 | const bool view; // non-contiguous a via view_4d |
| 4092 | |
| 4093 | std::string op_desc(ggml_tensor * t) override { |
| 4094 | GGML_UNUSED(t); |
| 4095 | return "ADD_ADD"; |
| 4096 | } |
| 4097 | |
| 4098 | bool run_whole_graph() override { return true; } |
| 4099 | |
| 4100 | std::string vars() override { |
| 4101 | return VARS_TO_STR5(type, type_addend, ne, broadcast, view); |
| 4102 | } |
| 4103 | |
| 4104 | test_add_add(ggml_type type = GGML_TYPE_F32, |
| 4105 | ggml_type type_addend = GGML_TYPE_F32, |
| 4106 | std::array<int64_t, 4> ne = {64, 5, 4, 3}, |
| 4107 | bool broadcast = false, |
| 4108 | bool view = false) |
| 4109 | : type(type), type_addend(type_addend), ne(ne), broadcast(broadcast), view(view) {} |
| 4110 | |
| 4111 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 4112 | std::array<int64_t, 4> broadcast_dims = {ne[0], 1, 1, 1}; |
| 4113 | |
| 4114 | ggml_tensor * a; |
| 4115 | if (view) { |
| 4116 | std::array<int64_t, 4> parent = { ne[0] * 3, ne[1] * 2, ne[2], ne[3] }; |
| 4117 | a = ggml_new_tensor(ctx, type, 4, parent.data()); |
| 4118 | ggml_set_name(a, "a_parent"); |
| 4119 | a = ggml_view_4d(ctx, a, ne[0], ne[1], ne[2], ne[3], a->nb[1], a->nb[2], a->nb[3], 0); |
| 4120 | ggml_set_name(a, "a"); |
| 4121 | } else { |
| 4122 | a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 4123 | ggml_set_name(a, "a"); |
| 4124 | } |
| 4125 | |
| 4126 | ggml_tensor * b = ggml_new_tensor(ctx, type_addend, 4, ne.data()); |
| 4127 | ggml_tensor * c = ggml_new_tensor(ctx, type_addend, 4, broadcast ? broadcast_dims.data() : ne.data()); |
| 4128 | |
| 4129 | ggml_set_name(b, "b"); |
| 4130 | ggml_set_name(c, "c"); |
| 4131 | |
| 4132 | ggml_tensor * out = ggml_add(ctx, ggml_add(ctx, a, b), c); |
| 4133 | ggml_set_name(out, "out"); |
| 4134 | |
| 4135 | return out; |
| 4136 | } |
| 4137 | |
| 4138 | double max_nmse_err() override { |
| 4139 | // Fused ADDs can keep FP32 intermediates while the CPU rounds each ADD to FP16/BF16. |
| 4140 | if (type == GGML_TYPE_F16) { |
| 4141 | return 1e-6; |
| 4142 | } |
| 4143 | if (type == GGML_TYPE_BF16) { |
| 4144 | return 1e-4; |
| 4145 | } |
| 4146 | return test_case::max_nmse_err(); |
| 4147 | } |
| 4148 | }; |
| 4149 | |
| 4150 | // GGML_OP_ADD + GGML_OP_RMS_NORM (fused operation) |
| 4151 | struct test_add_rms_norm : public test_case { |
| 4152 | const ggml_type type; |
| 4153 | const std::array<int64_t, 4> ne; |
| 4154 | const float eps; |
| 4155 | const bool broadcast; |
| 4156 | |
| 4157 | std::string op_desc(ggml_tensor * t) override { |
| 4158 | GGML_UNUSED(t); |
| 4159 | return "ADD_RMS_NORM"; |
| 4160 | } |
| 4161 | |
| 4162 | bool run_whole_graph() override { return true; } |
| 4163 | |
| 4164 | std::string vars() override { |
| 4165 | return VARS_TO_STR4(type, ne, eps, broadcast); |
| 4166 | } |
| 4167 | |
| 4168 | test_add_rms_norm(ggml_type type = GGML_TYPE_F32, |
| 4169 | std::array<int64_t, 4> ne = {64, 5, 4, 3}, |
| 4170 | float eps = 1e-6f, bool broadcast = false) |
| 4171 | : type(type), ne(ne), eps(eps), broadcast(broadcast) {} |
| 4172 | |
| 4173 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 4174 | std::array<int64_t, 4> broadcast_dims = {ne[0]*2, ne[1]*3, ne[2]*3, ne[3]*4}; |
| 4175 | |
| 4176 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, broadcast ? broadcast_dims.data() : ne.data()); |
| 4177 | ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 4178 | |
| 4179 | ggml_set_param(a); |
| 4180 | ggml_set_name(a, "a"); |
| 4181 | ggml_set_param(b); |
| 4182 | ggml_set_name(b, "b"); |
| 4183 | |
| 4184 | // ADD operation followed by RMS_NORM |
| 4185 | ggml_tensor * add_result = ggml_add(ctx, a, b); |
| 4186 | ggml_set_name(add_result, "add_result"); |
| 4187 | |
| 4188 | ggml_tensor * out = ggml_rms_norm(ctx, add_result, eps); |
| 4189 | ggml_set_name(out, "out"); |
| 4190 | |
| 4191 | return out; |
| 4192 | } |
| 4193 | |
| 4194 | void initialize_tensors(ggml_context * ctx) override { |
| 4195 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 4196 | init_tensor_uniform(t, -10.f, 10.f); |
| 4197 | } |
| 4198 | } |
| 4199 | |
| 4200 | float grad_eps() override { |
| 4201 | return 1.0f; |
| 4202 | } |
| 4203 | |
| 4204 | bool grad_precise() override { |
| 4205 | return true; |
| 4206 | } |
| 4207 | }; |
| 4208 | |
| 4209 | // GGML_OP_UNARY(RELU) + GGML_OP_SQR (fused operation) |
| 4210 | struct test_relu_sqr : public test_case { |
| 4211 | const ggml_type type; |
| 4212 | const std::array<int64_t, 4> ne; |
| 4213 | |
| 4214 | std::string op_desc(ggml_tensor * t) override { |
| 4215 | GGML_UNUSED(t); |
| 4216 | return "RELU_SQR"; |
| 4217 | } |
| 4218 | |
| 4219 | bool run_whole_graph() override { return true; } |
| 4220 | |
| 4221 | std::string vars() override { |
| 4222 | return VARS_TO_STR2(type, ne); |
| 4223 | } |
| 4224 | |
| 4225 | test_relu_sqr(ggml_type type = GGML_TYPE_F32, |
| 4226 | std::array<int64_t, 4> ne = {128, 2, 2, 2}) |
| 4227 | : type(type), ne(ne) {} |
| 4228 | |
| 4229 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 4230 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 4231 | ggml_set_name(a, "a"); |
| 4232 | |
| 4233 | ggml_tensor * r = ggml_relu(ctx, a); |
| 4234 | ggml_set_name(r, "relu"); |
| 4235 | |
| 4236 | ggml_tensor * out = ggml_sqr(ctx, r); |
| 4237 | ggml_set_name(out, "out"); |
| 4238 | |
| 4239 | return out; |
| 4240 | } |
| 4241 | }; |
| 4242 | |
| 4243 | // GGML_OP_UNARY(GELU|SILU|SIGMOID|SOFTPLUS) + GGML_OP_MUL (fused operation). |
| 4244 | struct test_unary_mul : public test_case { |
| 4245 | const ggml_unary_op op; |
| 4246 | const ggml_type type; |
| 4247 | const std::array<int64_t, 4> ne; |
| 4248 | const bool swap; // unary result is the second MUL operand |
| 4249 | const std::string layout; // operand layout, see build_graph() |
| 4250 | const std::string tail; // extra consumer past the MUL, see build_graph() |
| 4251 | |
| 4252 | std::string op_desc(ggml_tensor * t) override { |
| 4253 | GGML_UNUSED(t); |
| 4254 | return std::string(ggml_unary_op_name(op)) + "_MUL"; |
| 4255 | } |
| 4256 | |
| 4257 | bool run_whole_graph() override { return true; } |
| 4258 | |
| 4259 | double max_nmse_err() override { |
| 4260 | // the fused kernel elides the rounding of the unary result that the CPU chain |
| 4261 | // performs; relax the tolerance to match that drift |
| 4262 | switch (type) { |
| 4263 | case GGML_TYPE_F16: return 5e-5; |
| 4264 | case GGML_TYPE_BF16: return 1e-4; |
| 4265 | // gelu shader uses exp form, CPU uses tanhf |
| 4266 | default: return op == GGML_UNARY_OP_GELU ? 5e-7 : 1e-7; |
| 4267 | } |
| 4268 | } |
| 4269 | |
| 4270 | std::string vars() override { |
| 4271 | return VARS_TO_STR5(type, ne, swap, layout, tail); |
| 4272 | } |
| 4273 | |
| 4274 | test_unary_mul(ggml_unary_op op, |
| 4275 | ggml_type type = GGML_TYPE_F32, |
| 4276 | std::array<int64_t, 4> ne = {128, 2, 2, 2}, |
| 4277 | bool swap = false, |
| 4278 | std::string layout = "packed", |
| 4279 | std::string tail = "") |
| 4280 | : op(op), type(type), ne(ne), swap(swap), layout(std::move(layout)), tail(std::move(tail)) {} |
| 4281 | |
| 4282 | // `ne` viewed out of a wider tensor: rows stay contiguous, but the stride exceeds the width |
| 4283 | ggml_tensor * padded(ggml_context * ctx, const char * name, int64_t mul0, int64_t off0) { |
| 4284 | std::array<int64_t, 4> ne_w = ne; |
| 4285 | ne_w[0] *= mul0; |
| 4286 | ggml_tensor * base = ggml_new_tensor(ctx, type, 4, ne_w.data()); |
| 4287 | ggml_set_name(base, name); |
| 4288 | return ggml_view_4d(ctx, base, ne[0], ne[1], ne[2], ne[3], |
| 4289 | base->nb[1], base->nb[2], base->nb[3], off0 * base->nb[0]); |
| 4290 | } |
| 4291 | |
| 4292 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 4293 | ggml_tensor * a = nullptr; // unary source |
| 4294 | ggml_tensor * b = nullptr; // other MUL operand |
| 4295 | |
| 4296 | if (layout == "packed") { |
| 4297 | a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 4298 | b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 4299 | } else if (layout == "pad_unary") { |
| 4300 | a = padded(ctx, "a", 3, 0); |
| 4301 | b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 4302 | } else if (layout == "pad_other") { |
| 4303 | a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 4304 | b = padded(ctx, "b", 3, 0); |
| 4305 | } else if (layout == "halves") { |
| 4306 | // the shape the Conformer audio encoders build: one tensor split in two |
| 4307 | std::array<int64_t, 4> ne_w = ne; |
| 4308 | ne_w[0] *= 2; |
| 4309 | ggml_tensor * base = ggml_new_tensor(ctx, type, 4, ne_w.data()); |
| 4310 | ggml_set_name(base, "base"); |
| 4311 | b = ggml_view_4d(ctx, base, ne[0], ne[1], ne[2], ne[3], base->nb[1], base->nb[2], base->nb[3], 0); |
| 4312 | a = ggml_view_4d(ctx, base, ne[0], ne[1], ne[2], ne[3], base->nb[1], base->nb[2], base->nb[3], |
| 4313 | ne[0] * base->nb[0]); |
| 4314 | } else if (layout == "strided_dim1") { |
| 4315 | // contiguous rows but a strided dim 1: not ggml_is_contiguous_1, must not fuse |
| 4316 | std::array<int64_t, 4> ne_w = ne; |
| 4317 | ne_w[1] *= 3; |
| 4318 | ggml_tensor * base = ggml_new_tensor(ctx, type, 4, ne_w.data()); |
| 4319 | ggml_set_name(base, "a"); |
| 4320 | a = ggml_view_4d(ctx, base, ne[0], ne[1], ne[2], ne[3], base->nb[1], base->nb[2], base->nb[3], 0); |
| 4321 | b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 4322 | } else if (layout == "bcast") { |
| 4323 | a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 4324 | b = ggml_new_tensor_4d(ctx, type, ne[0], 1, 1, 1); |
| 4325 | } else if (layout == "rep_ne0") { |
| 4326 | // repeat on dim 0 |
| 4327 | a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 4328 | std::array<int64_t, 4> ne_b = ne; |
| 4329 | ne_b[0] /= 4; |
| 4330 | b = ggml_new_tensor(ctx, type, 4, ne_b.data()); |
| 4331 | } else if (layout == "view_mid") { |
| 4332 | // VIEW between UNARY and MUL |
| 4333 | a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 4334 | b = nullptr; |
| 4335 | } else if (layout == "gate") { |
| 4336 | // small gate on src1 |
| 4337 | const std::array<int64_t, 4> ne_gate = { 1, ne[1], ne[2], ne[3] }; |
| 4338 | a = ggml_new_tensor(ctx, type, 4, ne_gate.data()); |
| 4339 | b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 4340 | } else { |
| 4341 | GGML_ABORT("unknown layout %s", layout.c_str()); |
| 4342 | } |
| 4343 | if (a != nullptr) { |
| 4344 | ggml_set_name(a, "a"); |
| 4345 | } |
| 4346 | if (b != nullptr) { |
| 4347 | ggml_set_name(b, "b"); |
| 4348 | } |
| 4349 | |
| 4350 | ggml_tensor * u = ggml_unary(ctx, a, op); |
| 4351 | ggml_set_name(u, "unary"); |
| 4352 | |
| 4353 | // a broadcasting operand can only be the second one |
| 4354 | const bool second = layout == "gate" || (swap && layout != "bcast" && layout != "view_mid"); |
| 4355 | if (layout == "view_mid") { |
| 4356 | std::array<int64_t, 4> ne_base = ne; |
| 4357 | ne_base[0] *= 2; |
| 4358 | ggml_tensor * base = ggml_new_tensor(ctx, type, 4, ne_base.data()); |
| 4359 | ggml_set_name(base, "base"); |
| 4360 | b = ggml_view_4d(ctx, base, ne[0], ne[1], ne[2], ne[3], |
| 4361 | base->nb[1], base->nb[2], base->nb[3], 0); |
| 4362 | ggml_set_name(b, "b"); |
| 4363 | } |
| 4364 | ggml_tensor * out = second ? ggml_mul(ctx, b, u) : ggml_mul(ctx, u, b); |
| 4365 | |
| 4366 | if (tail == "reuse") { |
| 4367 | // a second read of the unary result must block the fusion |
| 4368 | ggml_set_name(out, "mul"); |
| 4369 | out = ggml_add(ctx, out, u); |
| 4370 | } else if (tail == "consumer") { |
| 4371 | // fusion still applies; catches a dispatcher that skips one node too many |
| 4372 | ggml_set_name(out, "mul"); |
| 4373 | out = ggml_add(ctx, out, b); |
| 4374 | } else if (!tail.empty()) { |
| 4375 | GGML_ABORT("unknown tail %s", tail.c_str()); |
| 4376 | } |
| 4377 | ggml_set_name(out, "out"); |
| 4378 | |
| 4379 | return out; |
| 4380 | } |
| 4381 | }; |
| 4382 | |
| 4383 | // GGML_OP_ADD + GGML_OP_UNARY(SILU|SIGMOID|SOFTPLUS) + GGML_OP_MUL with the ADD's bias and |
| 4384 | // the MUL's scale broadcast over dim 0: the delta-net alpha gate, softplus(alpha + dt) * a_coeff. |
| 4385 | struct test_add_unary_mul : public test_case { |
| 4386 | const ggml_unary_op op; |
| 4387 | const ggml_type type; |
| 4388 | const std::array<int64_t, 4> ne; |
| 4389 | const bool swap; // unary result is the second MUL operand |
| 4390 | const std::string layout; // bias/scale layout, see build_graph() |
| 4391 | const std::string tail; // extra consumer past the MUL, see build_graph() |
| 4392 | |
| 4393 | std::string op_desc(ggml_tensor * t) override { |
| 4394 | GGML_UNUSED(t); |
| 4395 | return "ADD_" + std::string(ggml_unary_op_name(op)) + "_MUL"; |
| 4396 | } |
| 4397 | bool run_whole_graph() override { return true; } |
| 4398 | |
| 4399 | double max_nmse_err() override { |
| 4400 | switch (type) { |
| 4401 | // f16 never fuses (the kernel is f32-only), so this bound is the unfused |
| 4402 | // chain's own f16 rounding drift, as in test_unary_mul |
| 4403 | case GGML_TYPE_F16: return 5e-5; |
| 4404 | // gelu never fuses either, and the backends' exp form drifts from the CPU's tanhf |
| 4405 | default: return op == GGML_UNARY_OP_GELU ? 5e-7 : 1e-7; |
| 4406 | } |
| 4407 | } |
| 4408 | |
| 4409 | std::string vars() override { |
| 4410 | return VARS_TO_STR5(type, ne, swap, layout, tail); |
| 4411 | } |
| 4412 | |
| 4413 | test_add_unary_mul(ggml_unary_op op, |
| 4414 | ggml_type type = GGML_TYPE_F32, |
| 4415 | std::array<int64_t, 4> ne = {32, 7, 1, 1}, |
| 4416 | bool swap = false, |
| 4417 | std::string layout = "bcast", |
| 4418 | std::string tail = "") |
| 4419 | : op(op), type(type), ne(ne), swap(swap), layout(std::move(layout)), tail(std::move(tail)) {} |
| 4420 | |
| 4421 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 4422 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 4423 | ggml_set_name(a, "a"); |
| 4424 | |
| 4425 | std::array<int64_t, 4> ne_v = { ne[0], 1, 1, 1 }; |
| 4426 | if (layout == "bcast") { |
| 4427 | // one ne0 row each, broadcast over the outer dims, which is the alpha-gate form |
| 4428 | } else if (layout == "same_shape") { |
| 4429 | // no broadcast at all; fuses only while the activation is a single row |
| 4430 | ne_v = ne; |
| 4431 | } else if (layout == "rep_ne0") { |
| 4432 | // repeat on dim 0, which bias[col] cannot address, so this must not fuse |
| 4433 | ne_v[0] = ne[0] / 4; |
| 4434 | } else { |
| 4435 | GGML_ABORT("unknown layout %s", layout.c_str()); |
| 4436 | } |
| 4437 | |
| 4438 | ggml_tensor * bias = ggml_new_tensor(ctx, type, 4, ne_v.data()); |
| 4439 | ggml_set_name(bias, "bias"); |
| 4440 | |
| 4441 | ggml_tensor * scale = ggml_new_tensor(ctx, type, 4, ne_v.data()); |
| 4442 | ggml_set_name(scale, "scale"); |
| 4443 | |
| 4444 | ggml_tensor * s = ggml_add(ctx, a, bias); |
| 4445 | ggml_set_name(s, "add"); |
| 4446 | |
| 4447 | ggml_tensor * u = ggml_unary(ctx, s, op); |
| 4448 | ggml_set_name(u, "unary"); |
| 4449 | |
| 4450 | // a broadcasting operand can only be the second one, so swap needs same-shape operands |
| 4451 | ggml_tensor * out = swap ? ggml_mul(ctx, scale, u) : ggml_mul(ctx, u, scale); |
| 4452 | |
| 4453 | if (tail == "reuse") { |
| 4454 | // a second read of the add result must block the fusion |
| 4455 | ggml_set_name(out, "mul"); |
| 4456 | out = ggml_add(ctx, out, s); |
| 4457 | } else if (tail == "consumer") { |
| 4458 | // fusion still applies; catches a dispatcher that skips one node too many |
| 4459 | ggml_set_name(out, "mul"); |
| 4460 | out = ggml_add(ctx, out, scale); |
| 4461 | } else if (!tail.empty()) { |
| 4462 | GGML_ABORT("unknown tail %s", tail.c_str()); |
| 4463 | } |
| 4464 | ggml_set_name(out, "out"); |
| 4465 | |
| 4466 | return out; |
| 4467 | } |
| 4468 | }; |
| 4469 | |
| 4470 | // SNAKE activation fusion: y = x + sin(a*x)^2 * inv_b |
| 4471 | // CUDA backend matches the naive 5-op chain (mul, sin, sqr, mul, add) |
| 4472 | // and dispatches a single fused kernel. |
| 4473 | struct test_snake_fuse : public test_case { |
| 4474 | const ggml_type type; |
| 4475 | const std::array<int64_t, 4> ne; // [T, C, D2, D3] |
| 4476 | |
| 4477 | std::string op_desc(ggml_tensor * t) override { |
| 4478 | GGML_UNUSED(t); |
| 4479 | return "SNAKE_FUSE"; |
| 4480 | } |
| 4481 | |
| 4482 | bool run_whole_graph() override { return true; } |
| 4483 | |
| 4484 | double max_nmse_err() override { |
| 4485 | // BF16 epsilon ~ 7.8e-3, F16 epsilon ~ 9.7e-4: relax tolerance to match |
| 4486 | // the natural roundoff drift between the naive CPU chain and the fused |
| 4487 | // CUDA kernel. F32 keeps the default tight bound. |
| 4488 | switch (type) { |
| 4489 | case GGML_TYPE_BF16: return 5e-3; |
| 4490 | case GGML_TYPE_F16: return 5e-5; |
| 4491 | default: return 1e-7; |
| 4492 | } |
| 4493 | } |
| 4494 | |
| 4495 | std::string vars() override { |
| 4496 | return VARS_TO_STR2(type, ne); |
| 4497 | } |
| 4498 | |
| 4499 | test_snake_fuse(ggml_type type = GGML_TYPE_F32, |
| 4500 | std::array<int64_t, 4> ne = {256, 192, 1, 1}) |
| 4501 | : type(type), ne(ne) {} |
| 4502 | |
| 4503 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 4504 | ggml_tensor * x = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]); |
| 4505 | ggml_set_name(x, "x"); |
| 4506 | |
| 4507 | ggml_tensor * a = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 1, ne[1]); |
| 4508 | ggml_set_name(a, "a"); |
| 4509 | |
| 4510 | ggml_tensor * inv_b = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 1, ne[1]); |
| 4511 | ggml_set_name(inv_b, "inv_b"); |
| 4512 | |
| 4513 | // exact 5-op chain that BigVGAN / Vocos frontends emit |
| 4514 | ggml_tensor * ax = ggml_mul(ctx, x, a); |
| 4515 | ggml_tensor * sin_ax = ggml_sin(ctx, ax); |
| 4516 | ggml_tensor * sin_sq = ggml_sqr(ctx, sin_ax); |
| 4517 | ggml_tensor * scaled = ggml_mul(ctx, sin_sq, inv_b); |
| 4518 | ggml_tensor * out = ggml_add(ctx, x, scaled); |
| 4519 | ggml_set_name(out, "out"); |
| 4520 | |
| 4521 | return out; |
| 4522 | } |
| 4523 | |
| 4524 | void initialize_tensors(ggml_context * ctx) override { |
| 4525 | // x in [-pi, pi] to exercise sin periodicity, params in default [-1, 1] |
| 4526 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) { |
| 4527 | const std::string name = ggml_get_name(t); |
| 4528 | if (name == "x") { |
| 4529 | init_tensor_uniform(t, -3.14159f, 3.14159f); |
| 4530 | } else { |
| 4531 | init_tensor_uniform(t); |
| 4532 | } |
| 4533 | } |
| 4534 | } |
| 4535 | }; |
| 4536 | |
| 4537 | |
| 4538 | struct test_dsv4_hc : public test_case { |
| 4539 | static constexpr int64_t hc = 4; |
| 4540 | |
| 4541 | ggml_tensor * out = nullptr; |
| 4542 | |
| 4543 | static uint32_t tensor_seed(const ggml_tensor * t) { |
| 4544 | uint32_t seed = 2166136261u; |
| 4545 | for (const char * p = ggml_get_name(t); *p; ++p) { |
| 4546 | seed ^= (uint8_t) *p; |
| 4547 | seed *= 16777619u; |
| 4548 | } |
| 4549 | for (int i = 0; i < GGML_MAX_DIMS; ++i) { |
| 4550 | seed ^= (uint32_t) t->ne[i]; |
| 4551 | seed *= 16777619u; |
| 4552 | } |
| 4553 | return seed; |
| 4554 | } |
| 4555 | |
| 4556 | static bool tensor_range(const std::string & name, float & lo, float & hi) { |
| 4557 | if (name == "mixes") { |
| 4558 | lo = -2.0f; hi = 2.0f; return true; |
| 4559 | } |
| 4560 | if (name == "scale") { |
| 4561 | lo = -0.5f; hi = 0.5f; return true; |
| 4562 | } |
| 4563 | if (name == "base") { |
| 4564 | lo = -0.25f; hi = 0.25f; return true; |
| 4565 | } |
| 4566 | if (name == "weights" || name == "comb") { |
| 4567 | lo = 0.0f; hi = 1.0f; return true; |
| 4568 | } |
| 4569 | if (name == "post") { |
| 4570 | lo = 0.0f; hi = 2.0f; return true; |
| 4571 | } |
| 4572 | if (name == "gate") { |
| 4573 | lo = -4.0f; hi = 4.0f; return true; |
| 4574 | } |
| 4575 | if (name == "x" || name == "residual") { |
| 4576 | lo = -1.0f; hi = 1.0f; return true; |
| 4577 | } |
| 4578 | return false; |
| 4579 | } |
| 4580 | |
| 4581 | void initialize_tensors(ggml_context * ctx) override { |
| 4582 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) { |
| 4583 | const std::string name = ggml_get_name(t); |
| 4584 | float lo; |
| 4585 | float hi; |
| 4586 | if (!tensor_range(name, lo, hi)) { |
| 4587 | init_tensor_uniform(t); |
| 4588 | continue; |
| 4589 | } |
| 4590 | |
| 4591 | GGML_ASSERT(t->type == GGML_TYPE_F32); |
| 4592 | std::mt19937 rng(tensor_seed(t)); |
| 4593 | std::uniform_real_distribution<float> dist(lo, hi); |
| 4594 | std::vector<float> data(ggml_nelements(t)); |
| 4595 | for (float & v : data) { |
| 4596 | v = dist(rng); |
| 4597 | } |
| 4598 | ggml_backend_tensor_set(t, data.data(), 0, data.size()*sizeof(float)); |
| 4599 | } |
| 4600 | } |
| 4601 | }; |
| 4602 | |
| 4603 | struct test_dsv4_hc_comb : public test_dsv4_hc { |
| 4604 | const int64_t n_tokens; |
| 4605 | const int32_t n_iter; |
| 4606 | const float eps; |
| 4607 | |
| 4608 | std::string op_desc(ggml_tensor * t) override { |
| 4609 | GGML_UNUSED(t); |
| 4610 | return "DSV4_HC_COMB"; |
| 4611 | } |
| 4612 | |
| 4613 | std::string vars() override { |
| 4614 | return VARS_TO_STR3(n_tokens, n_iter, eps); |
| 4615 | } |
| 4616 | |
| 4617 | test_dsv4_hc_comb(int64_t n_tokens = 17, int32_t n_iter = 4, float eps = 1e-6f) |
| 4618 | : n_tokens(n_tokens), n_iter(n_iter), eps(eps) {} |
| 4619 | |
| 4620 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 4621 | ggml_tensor * mixes = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, (2 + hc)*hc, n_tokens); |
| 4622 | ggml_set_name(mixes, "mixes"); |
| 4623 | |
| 4624 | ggml_tensor * scale = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 3); |
| 4625 | ggml_set_name(scale, "scale"); |
| 4626 | |
| 4627 | ggml_tensor * base = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, (2 + hc)*hc); |
| 4628 | ggml_set_name(base, "base"); |
| 4629 | |
| 4630 | out = ggml_dsv4_hc_comb(ctx, mixes, scale, base, eps, n_iter); |
| 4631 | ggml_set_name(out, "out"); |
| 4632 | return out; |
| 4633 | } |
| 4634 | }; |
| 4635 | |
| 4636 | struct test_dsv4_hc_pre : public test_dsv4_hc { |
| 4637 | const int64_t n_embd; |
| 4638 | const int64_t n_hc; |
| 4639 | const int64_t n_tokens; |
| 4640 | const bool gated; |
| 4641 | |
| 4642 | std::string op_desc(ggml_tensor * t) override { |
| 4643 | GGML_UNUSED(t); |
| 4644 | return "DSV4_HC_PRE"; |
| 4645 | } |
| 4646 | |
| 4647 | std::string vars() override { |
| 4648 | return VARS_TO_STR4(n_embd, n_hc, n_tokens, gated); |
| 4649 | } |
| 4650 | |
| 4651 | test_dsv4_hc_pre(int64_t n_embd = 31, int64_t n_hc = 4, int64_t n_tokens = 17, bool gated = false) |
| 4652 | : n_embd(n_embd), n_hc(n_hc), n_tokens(n_tokens), gated(gated) {} |
| 4653 | |
| 4654 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 4655 | ggml_tensor * x = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_embd, n_hc, n_tokens); |
| 4656 | ggml_set_name(x, "x"); |
| 4657 | |
| 4658 | if (gated) { |
| 4659 | ggml_tensor * gate = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_embd, n_hc, n_tokens); |
| 4660 | ggml_set_name(gate, "gate"); |
| 4661 | |
| 4662 | out = ggml_dsv4_hc_pre_gated(ctx, x, gate, 1.0f/n_hc); |
| 4663 | } else { |
| 4664 | ggml_tensor * weights = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_hc, n_tokens); |
| 4665 | ggml_set_name(weights, "weights"); |
| 4666 | |
| 4667 | out = ggml_dsv4_hc_pre(ctx, x, weights); |
| 4668 | } |
| 4669 | ggml_set_name(out, "out"); |
| 4670 | return out; |
| 4671 | } |
| 4672 | }; |
| 4673 | |
| 4674 | struct test_dsv4_hc_post : public test_dsv4_hc { |
| 4675 | const int64_t n_embd; |
| 4676 | const int64_t n_tokens; |
| 4677 | const bool identity; |
| 4678 | const bool gated; |
| 4679 | |
| 4680 | std::string op_desc(ggml_tensor * t) override { |
| 4681 | GGML_UNUSED(t); |
| 4682 | return "DSV4_HC_POST"; |
| 4683 | } |
| 4684 | |
| 4685 | std::string vars() override { |
| 4686 | return VARS_TO_STR4(n_embd, n_tokens, identity, gated); |
| 4687 | } |
| 4688 | |
| 4689 | // gated: post = 2*sigmoid(post/hc), as qwen4exp builds it, so backends can fuse the chain |
| 4690 | bool run_whole_graph() override { return gated; } |
| 4691 | |
| 4692 | test_dsv4_hc_post(int64_t n_embd = 31, int64_t n_tokens = 17, bool identity = false, bool gated = false) |
| 4693 | : n_embd(n_embd), n_tokens(n_tokens), identity(identity), gated(gated) {} |
| 4694 | |
| 4695 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 4696 | ggml_tensor * x = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_tokens); |
| 4697 | ggml_set_name(x, "x"); |
| 4698 | |
| 4699 | ggml_tensor * residual = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_embd, hc, n_tokens); |
| 4700 | ggml_set_name(residual, "residual"); |
| 4701 | |
| 4702 | ggml_tensor * post = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hc, n_tokens); |
| 4703 | ggml_set_name(post, "post"); |
| 4704 | |
| 4705 | if (gated) { |
| 4706 | post = ggml_scale(ctx, ggml_sigmoid(ctx, ggml_scale(ctx, post, 1.0f / (float) hc)), 2.0f); |
| 4707 | } |
| 4708 | |
| 4709 | ggml_tensor * comb = nullptr; |
| 4710 | if (!identity) { |
| 4711 | comb = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, hc, hc, n_tokens); |
| 4712 | ggml_set_name(comb, "comb"); |
| 4713 | } |
| 4714 | |
| 4715 | out = ggml_dsv4_hc_post(ctx, x, residual, post, comb); |
| 4716 | ggml_set_name(out, "out"); |
| 4717 | return out; |
| 4718 | } |
| 4719 | }; |
| 4720 | |
| 4721 | |
| 4722 | // GGML_OP_SSM_CONV |
| 4723 | struct test_ssm_conv : public test_case { |
| 4724 | const ggml_type type; |
| 4725 | const std::array<int64_t, 4> ne_a; |
| 4726 | const std::array<int64_t, 4> ne_b; |
| 4727 | |
| 4728 | std::string vars() override { |
| 4729 | return VARS_TO_STR3(type, ne_a, ne_b); |
| 4730 | } |
| 4731 | |
| 4732 | test_ssm_conv(ggml_type type = GGML_TYPE_F32, |
| 4733 | std::array<int64_t, 4> ne_a = {10, 10, 10, 1}, |
| 4734 | std::array<int64_t, 4> ne_b = {3, 3, 1, 1}) |
| 4735 | : type(type), ne_a(ne_a), ne_b(ne_b) {} |
| 4736 | |
| 4737 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 4738 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 4739 | ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne_b.data()); |
| 4740 | ggml_tensor * out = ggml_ssm_conv(ctx, a, b); |
| 4741 | return out; |
| 4742 | } |
| 4743 | }; |
| 4744 | |
| 4745 | // GGML_OP_SSM_CONV + GGML_OP_ADD (channel-wise bias, optional) + GGML_OP_UNARY(SILU) (fused operation) |
| 4746 | struct test_ssm_conv_bias_silu : public test_case { |
| 4747 | const ggml_type type; |
| 4748 | const std::array<int64_t, 4> ne_a; |
| 4749 | const std::array<int64_t, 4> ne_b; |
| 4750 | const bool fuse_bias; |
| 4751 | |
| 4752 | std::string op_desc(ggml_tensor * t) override { |
| 4753 | GGML_UNUSED(t); |
| 4754 | return "SSM_CONV_BIAS_SILU"; |
| 4755 | } |
| 4756 | |
| 4757 | bool run_whole_graph() override { return true; } |
| 4758 | |
| 4759 | std::string vars() override { |
| 4760 | return VARS_TO_STR4(type, ne_a, ne_b, fuse_bias); |
| 4761 | } |
| 4762 | |
| 4763 | test_ssm_conv_bias_silu(ggml_type type, std::array<int64_t, 4> ne_a, std::array<int64_t, 4> ne_b, |
| 4764 | bool fuse_bias) |
| 4765 | : type(type), ne_a(ne_a), ne_b(ne_b), fuse_bias(fuse_bias) {} |
| 4766 | |
| 4767 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 4768 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 4769 | ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne_b.data()); |
| 4770 | ggml_set_name(a, "a"); |
| 4771 | ggml_set_name(b, "b"); |
| 4772 | |
| 4773 | ggml_tensor * out = ggml_ssm_conv(ctx, a, b); |
| 4774 | |
| 4775 | if (fuse_bias) { |
| 4776 | ggml_tensor * bias = ggml_new_tensor_1d(ctx, type, out->ne[0]); |
| 4777 | ggml_set_name(bias, "bias"); |
| 4778 | out = ggml_add(ctx, out, bias); |
| 4779 | } |
| 4780 | |
| 4781 | out = ggml_silu(ctx, out); |
| 4782 | |
| 4783 | ggml_set_name(out, "out"); |
| 4784 | return out; |
| 4785 | } |
| 4786 | }; |
| 4787 | |
| 4788 | // GGML_OP_SSM_SCAN |
| 4789 | struct test_ssm_scan : public test_case { |
| 4790 | const ggml_type type; |
| 4791 | |
| 4792 | const int64_t d_state; |
| 4793 | const int64_t head_dim; |
| 4794 | const int64_t n_head; |
| 4795 | const int64_t n_group; |
| 4796 | const int64_t n_seq_tokens; |
| 4797 | const int64_t n_seqs; |
| 4798 | const bool xbc_overlap; |
| 4799 | const int64_t K; |
| 4800 | const bool weak_decay; |
| 4801 | |
| 4802 | std::string vars() override { |
| 4803 | return VARS_TO_STR10(type, d_state, head_dim, n_head, n_group, n_seq_tokens, n_seqs, xbc_overlap, K, weak_decay); |
| 4804 | } |
| 4805 | |
| 4806 | test_ssm_scan(ggml_type type = GGML_TYPE_F32, |
| 4807 | int64_t d_state = 32, |
| 4808 | int64_t head_dim = 1, // 1 = Mamba-1; > 1 = Mamba-2 (scalar A per head) |
| 4809 | int64_t n_head = 32, |
| 4810 | int64_t n_group = 1, |
| 4811 | int64_t n_seq_tokens = 32, |
| 4812 | int64_t n_seqs = 32, |
| 4813 | bool xbc_overlap = false, |
| 4814 | int64_t K = 1, |
| 4815 | bool weak_decay = false) |
| 4816 | : type(type), d_state(d_state), head_dim(head_dim), n_head(n_head), n_group(n_group), n_seq_tokens(n_seq_tokens), n_seqs(n_seqs), xbc_overlap(xbc_overlap), K(K), weak_decay(weak_decay) {} |
| 4817 | |
| 4818 | double max_nmse_err() override { |
| 4819 | // SSD path (head_dim > 1) uses FP16 intermediates (M matrix, X_dt); Mamba-1 is pure FP32. |
| 4820 | return (head_dim > 1) ? 2e-7 : 1e-7; |
| 4821 | } |
| 4822 | |
| 4823 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 4824 | ggml_tensor * s = ggml_new_tensor_4d(ctx, type, d_state, head_dim, n_head, n_seqs); |
| 4825 | ggml_tensor * dt = ggml_new_tensor_3d(ctx, type, n_head, n_seq_tokens, n_seqs); |
| 4826 | ggml_tensor * A = ggml_new_tensor_2d(ctx, type, (head_dim > 1) ? 1 : d_state, n_head); |
| 4827 | ggml_tensor * x; |
| 4828 | ggml_tensor * B; |
| 4829 | ggml_tensor * C; |
| 4830 | |
| 4831 | if (xbc_overlap) { |
| 4832 | ggml_tensor * xbc = ggml_new_tensor_4d(ctx, type, d_state, n_head, n_seq_tokens, 2 * n_seqs); |
| 4833 | x = ggml_view_4d(ctx, xbc, head_dim, n_head, n_seq_tokens, n_seqs, |
| 4834 | xbc->nb[1], xbc->nb[2], xbc->nb[3], xbc->nb[3]); |
| 4835 | B = ggml_view_4d(ctx, xbc, d_state, n_group, n_seq_tokens, n_seqs, |
| 4836 | xbc->nb[1], xbc->nb[2], xbc->nb[3], 0); |
| 4837 | C = ggml_view_4d(ctx, xbc, d_state, n_group, n_seq_tokens, n_seqs, |
| 4838 | xbc->nb[1], xbc->nb[2], xbc->nb[3], 2 * xbc->nb[3]); |
| 4839 | } else { |
| 4840 | x = ggml_new_tensor_4d(ctx, type, head_dim, n_head, n_seq_tokens, n_seqs); |
| 4841 | B = ggml_new_tensor_4d(ctx, type, d_state, n_group, n_seq_tokens, n_seqs); |
| 4842 | C = ggml_new_tensor_4d(ctx, type, d_state, n_group, n_seq_tokens, n_seqs); |
| 4843 | } |
| 4844 | ggml_tensor * ids = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, n_seqs); |
| 4845 | ggml_tensor * out = ggml_ssm_scan(ctx, s, x, dt, A, B, C, ids, K); |
| 4846 | return out; |
| 4847 | } |
| 4848 | |
| 4849 | |
| 4850 | void initialize_tensors(ggml_context * ctx) override { |
| 4851 | std::random_device rd; |
| 4852 | std::default_random_engine rng(rd()); |
| 4853 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 4854 | if (t->type == GGML_TYPE_I32) { |
| 4855 | if (ggml_is_view_op(t->op)) { continue; } |
| 4856 | // ids: permutation of [0..n_seqs) |
| 4857 | for (int64_t r = 0; r < ggml_nrows(t); r++) { |
| 4858 | std::vector<int32_t> data(t->ne[0]); |
| 4859 | for (int i = 0; i < t->ne[0]; i++) { |
| 4860 | data[i] = i; |
| 4861 | } |
| 4862 | std::shuffle(data.begin(), data.end(), rng); |
| 4863 | ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(int32_t)); |
| 4864 | } |
| 4865 | } else if (ggml_is_view_op(t->op)) { |
| 4866 | continue; |
| 4867 | } else if (t->ne[1] == n_head && t->ne[2] == 1) { |
| 4868 | // A {1 or d_state, n_head}: negative decay (2-D tensor, ne[2]==1 distinguishes from 3-D/4-D tensors) |
| 4869 | init_tensor_uniform(t, weak_decay ? -0.02f : -1.0f, weak_decay ? -0.005f : -0.5f); |
| 4870 | } else { |
| 4871 | init_tensor_uniform(t); |
| 4872 | } |
| 4873 | } |
| 4874 | } |
| 4875 | }; |
| 4876 | |
| 4877 | struct test_ssm_scan_rollback : public test_case { |
| 4878 | const ggml_type type; |
| 4879 | |
| 4880 | const int64_t d_state; |
| 4881 | const int64_t head_dim; |
| 4882 | const int64_t n_head; |
| 4883 | const int64_t n_group; |
| 4884 | const int64_t n_seq_tokens; |
| 4885 | const int64_t n_seqs; |
| 4886 | const int64_t K; |
| 4887 | |
| 4888 | std::string vars() override { |
| 4889 | return VARS_TO_STR8(type, d_state, head_dim, n_head, n_group, n_seq_tokens, n_seqs, K); |
| 4890 | } |
| 4891 | |
| 4892 | std::string op_desc(ggml_tensor * t) override { |
| 4893 | GGML_UNUSED(t); |
| 4894 | return "SSM_SCAN_ROLLBACK"; |
| 4895 | } |
| 4896 | |
| 4897 | bool run_whole_graph() override { |
| 4898 | return true; |
| 4899 | } |
| 4900 | |
| 4901 | double max_err() override { |
| 4902 | return 1e-6; |
| 4903 | } |
| 4904 | |
| 4905 | double err(const float * a, const float * b, size_t n) override { |
| 4906 | double result = 0.0; |
| 4907 | for (size_t i = 0; i < n; ++i) { |
| 4908 | result = std::max(result, (double) fabsf(a[i])); |
| 4909 | result = std::max(result, (double) fabsf(b[i])); |
| 4910 | } |
| 4911 | return result; |
| 4912 | } |
| 4913 | |
| 4914 | test_ssm_scan_rollback(ggml_type type = GGML_TYPE_F32, |
| 4915 | int64_t d_state = 32, |
| 4916 | int64_t head_dim = 64, |
| 4917 | int64_t n_head = 16, |
| 4918 | int64_t n_group = 2, |
| 4919 | int64_t n_seq_tokens = 8, |
| 4920 | int64_t n_seqs = 2, |
| 4921 | int64_t K = 3) |
| 4922 | : type(type), d_state(d_state), head_dim(head_dim), n_head(n_head), n_group(n_group), |
| 4923 | n_seq_tokens(n_seq_tokens), n_seqs(n_seqs), K(K) {} |
| 4924 | |
| 4925 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 4926 | ggml_tensor * s = ggml_new_tensor_4d(ctx, type, d_state, head_dim, n_head, n_seqs); |
| 4927 | ggml_tensor * x = ggml_new_tensor_4d(ctx, type, head_dim, n_head, n_seq_tokens, n_seqs); |
| 4928 | ggml_tensor * dt = ggml_new_tensor_3d(ctx, type, n_head, n_seq_tokens, n_seqs); |
| 4929 | ggml_tensor * A = ggml_new_tensor_2d(ctx, type, 1, n_head); |
| 4930 | ggml_tensor * B = ggml_new_tensor_4d(ctx, type, d_state, n_group, n_seq_tokens, n_seqs); |
| 4931 | ggml_tensor * C = ggml_new_tensor_4d(ctx, type, d_state, n_group, n_seq_tokens, n_seqs); |
| 4932 | ggml_tensor * ids = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, n_seqs); |
| 4933 | |
| 4934 | ggml_tensor * full = ggml_ssm_scan(ctx, s, x, dt, A, B, C, ids, K); |
| 4935 | |
| 4936 | const int64_t y_elems = head_dim * n_head * n_seq_tokens * n_seqs; |
| 4937 | const int64_t state_elems = d_state * head_dim * n_head * n_seqs; |
| 4938 | |
| 4939 | ggml_tensor * out = nullptr; |
| 4940 | for (int64_t slot = 0; slot < K; ++slot) { |
| 4941 | const int64_t prefix_tokens = n_seq_tokens - slot; |
| 4942 | |
| 4943 | ggml_tensor * x_prefix = ggml_cont(ctx, ggml_view_4d(ctx, x, head_dim, n_head, prefix_tokens, n_seqs, x->nb[1], x->nb[2], x->nb[3], 0)); |
| 4944 | ggml_tensor * dt_prefix = ggml_cont(ctx, ggml_view_3d(ctx, dt, n_head, prefix_tokens, n_seqs, dt->nb[1], dt->nb[2], 0)); |
| 4945 | ggml_tensor * B_prefix = ggml_cont(ctx, ggml_view_4d(ctx, B, d_state, n_group, prefix_tokens, n_seqs, B->nb[1], B->nb[2], B->nb[3], 0)); |
| 4946 | ggml_tensor * C_prefix = ggml_cont(ctx, ggml_view_4d(ctx, C, d_state, n_group, prefix_tokens, n_seqs, C->nb[1], C->nb[2], C->nb[3], 0)); |
| 4947 | |
| 4948 | ggml_tensor * prefix = ggml_ssm_scan(ctx, s, x_prefix, dt_prefix, A, B_prefix, C_prefix, ids, /*K=*/1); |
| 4949 | |
| 4950 | ggml_tensor * full_state = ggml_view_1d(ctx, full, state_elems, (y_elems + slot*state_elems)*ggml_element_size(full)); |
| 4951 | ggml_tensor * prefix_state = ggml_view_1d(ctx, prefix, state_elems, (head_dim*n_head*prefix_tokens*n_seqs)*ggml_element_size(prefix)); |
| 4952 | ggml_tensor * diff = ggml_sum(ctx, ggml_sqr(ctx, ggml_sub(ctx, full_state, prefix_state))); |
| 4953 | |
| 4954 | out = out == nullptr ? diff : ggml_add(ctx, out, diff); |
| 4955 | } |
| 4956 | |
| 4957 | return out; |
| 4958 | } |
| 4959 | |
| 4960 | void initialize_tensors(ggml_context * ctx) override { |
| 4961 | std::random_device rd; |
| 4962 | std::default_random_engine rng(rd()); |
| 4963 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 4964 | if (t->type == GGML_TYPE_I32) { |
| 4965 | if (ggml_is_view_op(t->op)) { continue; } |
| 4966 | for (int64_t r = 0; r < ggml_nrows(t); r++) { |
| 4967 | std::vector<int32_t> data(t->ne[0]); |
| 4968 | for (int i = 0; i < t->ne[0]; i++) { |
| 4969 | data[i] = i; |
| 4970 | } |
| 4971 | std::shuffle(data.begin(), data.end(), rng); |
| 4972 | ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(int32_t)); |
| 4973 | } |
| 4974 | } else if (ggml_is_view_op(t->op)) { |
| 4975 | continue; |
| 4976 | } else if (t->ne[1] == n_head && t->ne[2] == 1) { |
| 4977 | init_tensor_uniform(t, -1.0f, -0.5f); |
| 4978 | } else { |
| 4979 | init_tensor_uniform(t); |
| 4980 | } |
| 4981 | } |
| 4982 | } |
| 4983 | }; |
| 4984 | |
| 4985 | // GGML_OP_SSM_SCAN + GGML_OP_CPY (recurrent cache fusion) |
| 4986 | struct test_ssm_scan_cache_fusion : public test_case { |
| 4987 | const ggml_type type; |
| 4988 | |
| 4989 | const int64_t d_state; |
| 4990 | const int64_t head_dim; |
| 4991 | const int64_t n_head; |
| 4992 | const int64_t n_group; |
| 4993 | const int64_t n_seq_tokens; |
| 4994 | const int64_t n_seqs; |
| 4995 | const int64_t K; // snapshot slot count (1 = final state only) |
| 4996 | |
| 4997 | ggml_tensor * cpy_node = nullptr; |
| 4998 | |
| 4999 | std::string vars() override { |
| 5000 | return VARS_TO_STR8(type, d_state, head_dim, n_head, n_group, n_seq_tokens, n_seqs, K); |
| 5001 | } |
| 5002 | |
| 5003 | test_ssm_scan_cache_fusion(ggml_type type = GGML_TYPE_F32, |
| 5004 | int64_t d_state = 128, int64_t head_dim = 64, int64_t n_head = 16, int64_t n_group = 2, |
| 5005 | int64_t n_seq_tokens = 4, int64_t n_seqs = 1, int64_t K = 4) |
| 5006 | : type(type), d_state(d_state), head_dim(head_dim), n_head(n_head), n_group(n_group), |
| 5007 | n_seq_tokens(n_seq_tokens), n_seqs(n_seqs), K(K) {} |
| 5008 | |
| 5009 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 5010 | const int64_t D = d_state * head_dim * n_head; |
| 5011 | const int64_t n_written = std::min<int64_t>(n_seq_tokens, K); |
| 5012 | |
| 5013 | // more cache rows per slot than seqs and a non-zero first row, so a wrong slot stride or offset shows up |
| 5014 | const int64_t mem_size = n_seqs + 2; |
| 5015 | const int64_t kv_head = 1; |
| 5016 | |
| 5017 | ggml_tensor * s = ggml_new_tensor_4d(ctx, type, d_state, head_dim, n_head, n_seqs); |
| 5018 | ggml_tensor * x = ggml_new_tensor_4d(ctx, type, head_dim, n_head, n_seq_tokens, n_seqs); |
| 5019 | ggml_tensor * dt = ggml_new_tensor_3d(ctx, type, n_head, n_seq_tokens, n_seqs); |
| 5020 | ggml_tensor * A = ggml_new_tensor_2d(ctx, type, 1, n_head); |
| 5021 | ggml_tensor * B = ggml_new_tensor_4d(ctx, type, d_state, n_group, n_seq_tokens, n_seqs); |
| 5022 | ggml_tensor * C = ggml_new_tensor_4d(ctx, type, d_state, n_group, n_seq_tokens, n_seqs); |
| 5023 | ggml_tensor * ids = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, n_seqs); |
| 5024 | ggml_set_name(A, "A"); |
| 5025 | ggml_set_name(ids, "ids"); |
| 5026 | |
| 5027 | ggml_tensor * out = ggml_ssm_scan(ctx, s, x, dt, A, B, C, ids, K); |
| 5028 | ggml_set_name(out, "ssm_out"); |
| 5029 | |
| 5030 | // snapshot tail view [D, n_seqs, n_written] |
| 5031 | ggml_tensor * src = ggml_view_3d(ctx, out, |
| 5032 | D, n_seqs, n_written, |
| 5033 | ggml_row_size(out->type, D), |
| 5034 | ggml_row_size(out->type, D * n_seqs), |
| 5035 | ggml_row_size(out->type, ggml_nelements(x))); |
| 5036 | |
| 5037 | // recurrent cache view [D, n_seqs, n_written] |
| 5038 | ggml_tensor * cache = ggml_new_tensor_2d(ctx, type, D, mem_size * n_written); |
| 5039 | ggml_set_name(cache, "cache"); |
| 5040 | ggml_tensor * dst = ggml_view_3d(ctx, cache, |
| 5041 | D, n_seqs, n_written, |
| 5042 | cache->nb[1], |
| 5043 | mem_size * cache->nb[1], |
| 5044 | kv_head * cache->nb[1]); |
| 5045 | |
| 5046 | ggml_tensor * cpy = ggml_cpy(ctx, src, dst); |
| 5047 | ggml_set_name(cpy, "ssm_cache_cpy"); |
| 5048 | cpy_node = cpy; |
| 5049 | |
| 5050 | // read the cpy output so that neither the scan nor the cpy is the graph output (cont, since the cache view is strided) |
| 5051 | ggml_tensor * res = ggml_sum(ctx, ggml_cont(ctx, cpy)); |
| 5052 | return res; |
| 5053 | } |
| 5054 | |
| 5055 | std::string op_desc(ggml_tensor * t) override { |
| 5056 | GGML_UNUSED(t); |
| 5057 | return "SSM_SCAN_CACHE_FUSION"; |
| 5058 | } |
| 5059 | |
| 5060 | bool run_whole_graph() override { return true; } |
| 5061 | std::vector<ggml_tensor *> fusion_test_nodes() override { return { cpy_node }; } |
| 5062 | |
| 5063 | void initialize_tensors(ggml_context * ctx) override { |
| 5064 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) { |
| 5065 | if (ggml_is_view_op(t->op)) { continue; } |
| 5066 | if (strcmp(t->name, "ids") == 0) { |
| 5067 | std::vector<int32_t> data(t->ne[0]); |
| 5068 | for (int i = 0; i < t->ne[0]; i++) { |
| 5069 | data[i] = i; |
| 5070 | } |
| 5071 | ggml_backend_tensor_set(t, data.data(), 0, t->ne[0] * sizeof(int32_t)); |
| 5072 | } else if (strcmp(t->name, "A") == 0) { |
| 5073 | init_tensor_uniform(t, -1.0f, -0.5f); |
| 5074 | } else if (strcmp(t->name, "cache") == 0) { |
| 5075 | init_tensor_uniform(t, 0.0f, 0.0f); |
| 5076 | } else { |
| 5077 | init_tensor_uniform(t); |
| 5078 | } |
| 5079 | } |
| 5080 | } |
| 5081 | }; |
| 5082 | |
| 5083 | // GGML_OP_RWKV_WKV6 |
| 5084 | struct test_rwkv_wkv6 : public test_case { |
| 5085 | const ggml_type type; |
| 5086 | |
| 5087 | const int64_t head_count; |
| 5088 | const int64_t head_size; |
| 5089 | const int64_t n_seq_tokens; |
| 5090 | const int64_t n_seqs; |
| 5091 | |
| 5092 | std::string vars() override { |
| 5093 | return VARS_TO_STR5(type, head_count, head_size, n_seq_tokens, n_seqs); |
| 5094 | } |
| 5095 | |
| 5096 | test_rwkv_wkv6(ggml_type type = GGML_TYPE_F32, |
| 5097 | int64_t head_count = 32, int64_t head_size = 64, int64_t n_seq_tokens = 32, int64_t n_seqs = 32) |
| 5098 | : type(type), head_count(head_count), head_size(head_size), n_seq_tokens(n_seq_tokens), n_seqs(n_seqs) {} |
| 5099 | |
| 5100 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 5101 | const int64_t n_tokens = n_seq_tokens * n_seqs; |
| 5102 | ggml_tensor * r = ggml_new_tensor(ctx, type, 3, std::vector<int64_t>{ head_size, head_count, n_tokens }.data()); |
| 5103 | ggml_tensor * k = ggml_new_tensor(ctx, type, 3, std::vector<int64_t>{ head_size, head_count, n_tokens }.data()); |
| 5104 | ggml_tensor * v = ggml_new_tensor(ctx, type, 3, std::vector<int64_t>{ head_size, head_count, n_tokens }.data()); |
| 5105 | ggml_tensor * tf = ggml_new_tensor(ctx, type, 2, std::vector<int64_t>{ head_size, head_count }.data()); |
| 5106 | ggml_tensor * td = ggml_new_tensor(ctx, type, 3, std::vector<int64_t>{ head_size, head_count, n_tokens }.data()); |
| 5107 | ggml_tensor * s = ggml_new_tensor(ctx, type, 2, std::vector<int64_t>{ head_size * head_size * head_count, n_seqs }.data()); |
| 5108 | ggml_tensor * out = ggml_rwkv_wkv6(ctx, k, v, r, tf, td, s); |
| 5109 | return out; |
| 5110 | } |
| 5111 | }; |
| 5112 | |
| 5113 | // GGML_OP_GATED_DELTA_NET |
| 5114 | struct test_gated_delta_net : public test_case { |
| 5115 | const ggml_type type; |
| 5116 | |
| 5117 | const int64_t head_count; |
| 5118 | const int64_t head_size; |
| 5119 | const int64_t n_seq_tokens; |
| 5120 | const int64_t n_seqs; |
| 5121 | const int v_repeat; |
| 5122 | const bool permuted; |
| 5123 | const bool kda; |
| 5124 | const int64_t K; // snapshot slot count: 1 = final-only, >1 = last K states |
| 5125 | |
| 5126 | std::string vars() override { |
| 5127 | return VARS_TO_STR9(type, head_count, head_size, n_seq_tokens, n_seqs, v_repeat, permuted, kda, K); |
| 5128 | } |
| 5129 | |
| 5130 | test_gated_delta_net(ggml_type type = GGML_TYPE_F32, |
| 5131 | int64_t head_count = 4, int64_t head_size = 16, int64_t n_seq_tokens = 1, int64_t n_seqs = 1, |
| 5132 | int v_repeat = 1, bool permuted = false, bool kda = false, int64_t K = 1) |
| 5133 | : type(type), head_count(head_count), head_size(head_size), n_seq_tokens(n_seq_tokens), n_seqs(n_seqs), |
| 5134 | v_repeat(v_repeat), permuted(permuted), kda(kda), K(K) {} |
| 5135 | |
| 5136 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 5137 | ggml_tensor * q; |
| 5138 | ggml_tensor * k; |
| 5139 | ggml_tensor * v; |
| 5140 | if (permuted) { |
| 5141 | // create with dims 1 and 2 swapped, then permute back to get non-contiguous layout |
| 5142 | q = ggml_permute(ctx, ggml_new_tensor_4d(ctx, type, head_size, n_seq_tokens, head_count, n_seqs), 0, 2, 1, 3); |
| 5143 | k = ggml_permute(ctx, ggml_new_tensor_4d(ctx, type, head_size, n_seq_tokens, head_count, n_seqs), 0, 2, 1, 3); |
| 5144 | v = ggml_permute(ctx, ggml_new_tensor_4d(ctx, type, head_size, n_seq_tokens, head_count * v_repeat, n_seqs), 0, 2, 1, 3); |
| 5145 | } else { |
| 5146 | q = ggml_new_tensor_4d(ctx, type, head_size, head_count, n_seq_tokens, n_seqs); |
| 5147 | k = ggml_new_tensor_4d(ctx, type, head_size, head_count, n_seq_tokens, n_seqs); |
| 5148 | v = ggml_new_tensor_4d(ctx, type, head_size, head_count * v_repeat, n_seq_tokens, n_seqs); |
| 5149 | } |
| 5150 | ggml_set_name(q, "q"); |
| 5151 | ggml_set_name(k, "k"); |
| 5152 | ggml_set_name(v, "v"); |
| 5153 | const int64_t g_ne0 = kda ? head_size : 1; |
| 5154 | ggml_tensor * g = ggml_new_tensor_4d(ctx, type, g_ne0, head_count * v_repeat, n_seq_tokens, n_seqs); |
| 5155 | ggml_tensor * beta = ggml_new_tensor_4d(ctx, type, 1, head_count * v_repeat, n_seq_tokens, n_seqs); |
| 5156 | ggml_tensor * state = ggml_new_tensor_4d(ctx, type, head_size, head_size, head_count * v_repeat, n_seqs); |
| 5157 | ggml_set_name(g, "g"); |
| 5158 | ggml_set_name(beta, "beta"); |
| 5159 | ggml_set_name(state, "state"); |
| 5160 | // q/k are L2-normalised in qwen35/kimi-linear before delta_net |
| 5161 | q = ggml_l2_norm(ctx, q, 1e-6f); |
| 5162 | k = ggml_l2_norm(ctx, k, 1e-6f); |
| 5163 | ggml_tensor * out = ggml_gated_delta_net(ctx, q, k, v, g, beta, state, K); |
| 5164 | return out; |
| 5165 | } |
| 5166 | |
| 5167 | void initialize_tensors(ggml_context * ctx) override { |
| 5168 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) { |
| 5169 | if (ggml_is_view_op(t->op)) { continue; } |
| 5170 | if (strcmp(t->name, "g") == 0) { |
| 5171 | init_tensor_uniform(t, -20.0f, -1e-4f); |
| 5172 | } else if (strcmp(t->name, "beta") == 0) { |
| 5173 | init_tensor_uniform(t, 0.0f, 1.0f); |
| 5174 | } else if (strcmp(t->name, "v") == 0) { |
| 5175 | init_tensor_uniform(t, -0.3f, 5.0f); |
| 5176 | } else { |
| 5177 | init_tensor_uniform(t); |
| 5178 | } |
| 5179 | } |
| 5180 | } |
| 5181 | }; |
| 5182 | |
| 5183 | // GGML_OP_GATED_DELTA_NET + GGML_OP_CPY (recurrent cache fusion) |
| 5184 | struct test_gated_delta_net_cache_fusion : public test_case { |
| 5185 | const ggml_type type; |
| 5186 | |
| 5187 | const int64_t head_count; |
| 5188 | const int64_t head_size; |
| 5189 | const int64_t n_seq_tokens; |
| 5190 | const int64_t n_seqs; |
| 5191 | const int64_t K; // snapshot slot count (>1) |
| 5192 | |
| 5193 | ggml_tensor * cpy_node = nullptr; |
| 5194 | |
| 5195 | std::string vars() override { |
| 5196 | return VARS_TO_STR6(type, head_count, head_size, n_seq_tokens, n_seqs, K); |
| 5197 | } |
| 5198 | |
| 5199 | test_gated_delta_net_cache_fusion(ggml_type type = GGML_TYPE_F32, |
| 5200 | int64_t head_count = 4, int64_t head_size = 32, int64_t n_seq_tokens = 2, int64_t n_seqs = 1, |
| 5201 | int64_t K = 2) |
| 5202 | : type(type), head_count(head_count), head_size(head_size), n_seq_tokens(n_seq_tokens), n_seqs(n_seqs), K(K) {} |
| 5203 | |
| 5204 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 5205 | const int64_t S_v = head_size; |
| 5206 | const int64_t H_v = head_count; |
| 5207 | const int64_t H_k = head_count; |
| 5208 | const int64_t D = S_v * S_v * H_v; |
| 5209 | const int64_t n_written = std::min<int64_t>(n_seq_tokens, K); |
| 5210 | |
| 5211 | ggml_tensor * q = ggml_new_tensor_4d(ctx, type, head_size, H_k, n_seq_tokens, n_seqs); |
| 5212 | ggml_tensor * k = ggml_new_tensor_4d(ctx, type, head_size, H_k, n_seq_tokens, n_seqs); |
| 5213 | ggml_tensor * v = ggml_new_tensor_4d(ctx, type, head_size, H_v, n_seq_tokens, n_seqs); |
| 5214 | ggml_set_name(q, "q"); |
| 5215 | ggml_set_name(k, "k"); |
| 5216 | ggml_set_name(v, "v"); |
| 5217 | ggml_tensor * g = ggml_new_tensor_4d(ctx, type, 1, H_v, n_seq_tokens, n_seqs); |
| 5218 | ggml_tensor * beta = ggml_new_tensor_4d(ctx, type, 1, H_v, n_seq_tokens, n_seqs); |
| 5219 | ggml_tensor * state = ggml_new_tensor_4d(ctx, type, head_size, head_size, H_v, n_seqs); |
| 5220 | ggml_set_name(g, "g"); |
| 5221 | ggml_set_name(beta, "beta"); |
| 5222 | ggml_set_name(state, "state"); |
| 5223 | |
| 5224 | q = ggml_l2_norm(ctx, q, 1e-6f); |
| 5225 | k = ggml_l2_norm(ctx, k, 1e-6f); |
| 5226 | |
| 5227 | ggml_tensor * gdn_out = ggml_gated_delta_net(ctx, q, k, v, g, beta, state, K); |
| 5228 | ggml_set_name(gdn_out, "gdn_out"); |
| 5229 | |
| 5230 | // attn scores view (first part of the gdn output) |
| 5231 | ggml_tensor * attn = ggml_view_4d(ctx, gdn_out, |
| 5232 | S_v, H_v, n_seq_tokens, n_seqs, |
| 5233 | ggml_row_size(gdn_out->type, S_v), |
| 5234 | ggml_row_size(gdn_out->type, S_v * H_v), |
| 5235 | ggml_row_size(gdn_out->type, S_v * H_v * n_seq_tokens), 0); |
| 5236 | ggml_set_name(attn, "attn"); |
| 5237 | |
| 5238 | // snapshot tail view [D, n_seqs, n_written] |
| 5239 | const int64_t attn_score_elems = S_v * H_v * n_seq_tokens * n_seqs; |
| 5240 | ggml_tensor * src = ggml_view_3d(ctx, gdn_out, |
| 5241 | D, n_seqs, n_written, |
| 5242 | ggml_row_size(gdn_out->type, D), |
| 5243 | ggml_row_size(gdn_out->type, D * n_seqs), |
| 5244 | ggml_row_size(gdn_out->type, attn_score_elems)); |
| 5245 | |
| 5246 | // recurrent cache view [D, n_seqs, n_written] |
| 5247 | ggml_tensor * cache = ggml_new_tensor_3d(ctx, type, D, n_seqs, n_written); |
| 5248 | ggml_set_name(cache, "cache"); |
| 5249 | ggml_tensor * dst = ggml_view_3d(ctx, cache, |
| 5250 | D, n_seqs, n_written, |
| 5251 | ggml_row_size(cache->type, D), |
| 5252 | ggml_row_size(cache->type, D * n_seqs), 0); |
| 5253 | |
| 5254 | ggml_tensor * cpy = ggml_cpy(ctx, src, dst); |
| 5255 | ggml_set_name(cpy, "gdn_cache_cpy"); |
| 5256 | cpy_node = cpy; |
| 5257 | |
| 5258 | // read the cpy output (not the plain dst view, which would not pull the cpy into the graph) |
| 5259 | // so that neither the gdn nor the cpy is the graph output |
| 5260 | ggml_tensor * out = ggml_sum(ctx, cpy); |
| 5261 | return out; |
| 5262 | } |
| 5263 | |
| 5264 | std::string op_desc(ggml_tensor * t) override { |
| 5265 | GGML_UNUSED(t); |
| 5266 | return "GATED_DELTA_NET_CACHE_FUSION"; |
| 5267 | } |
| 5268 | |
| 5269 | bool run_whole_graph() override { return true; } |
| 5270 | std::vector<ggml_tensor *> fusion_test_nodes() override { return { cpy_node }; } |
| 5271 | |
| 5272 | uint64_t op_flops(ggml_tensor * t) override { |
| 5273 | GGML_UNUSED(t); |
| 5274 | const uint64_t S_v = head_size; |
| 5275 | const uint64_t H_v = head_count; |
| 5276 | const uint64_t T = n_seq_tokens; |
| 5277 | const uint64_t B = n_seqs; |
| 5278 | return (4ull*S_v + 2ull*S_v*S_v) * H_v * T * B; |
| 5279 | } |
| 5280 | |
| 5281 | void initialize_tensors(ggml_context * ctx) override { |
| 5282 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) { |
| 5283 | if (ggml_is_view_op(t->op)) { continue; } |
| 5284 | if (strcmp(t->name, "g") == 0) { |
| 5285 | init_tensor_uniform(t, -20.0f, -1e-4f); |
| 5286 | } else if (strcmp(t->name, "beta") == 0) { |
| 5287 | init_tensor_uniform(t, 0.0f, 1.0f); |
| 5288 | } else if (strcmp(t->name, "v") == 0) { |
| 5289 | init_tensor_uniform(t, -0.3f, 5.0f); |
| 5290 | } else if (strcmp(t->name, "cache") == 0) { |
| 5291 | init_tensor_uniform(t, 0.0f, 0.0f); |
| 5292 | } else { |
| 5293 | init_tensor_uniform(t); |
| 5294 | } |
| 5295 | } |
| 5296 | } |
| 5297 | }; |
| 5298 | |
| 5299 | // GGML_OP_GATED_LINEAR_ATTN |
| 5300 | struct test_gla : public test_case { |
| 5301 | const ggml_type type; |
| 5302 | |
| 5303 | const int64_t head_count; |
| 5304 | const int64_t head_size; |
| 5305 | const int64_t n_seq_tokens; |
| 5306 | const int64_t n_seqs; |
| 5307 | |
| 5308 | std::string vars() override { |
| 5309 | return VARS_TO_STR5(type, head_count, head_size, n_seq_tokens, n_seqs); |
| 5310 | } |
| 5311 | |
| 5312 | test_gla(ggml_type type = GGML_TYPE_F32, |
| 5313 | int64_t head_count = 32, int64_t head_size = 64, int64_t n_seq_tokens = 32, int64_t n_seqs = 32) |
| 5314 | : type(type), head_count(head_count), head_size(head_size), n_seq_tokens(n_seq_tokens), n_seqs(n_seqs) {} |
| 5315 | |
| 5316 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 5317 | const int64_t n_tokens = n_seq_tokens * n_seqs; |
| 5318 | ggml_tensor * q = ggml_new_tensor(ctx, type, 3, std::vector<int64_t>{ head_size, head_count, n_tokens }.data()); |
| 5319 | ggml_tensor * k = ggml_new_tensor(ctx, type, 3, std::vector<int64_t>{ head_size, head_count, n_tokens }.data()); |
| 5320 | ggml_tensor * v = ggml_new_tensor(ctx, type, 3, std::vector<int64_t>{ head_size, head_count, n_tokens }.data()); |
| 5321 | ggml_tensor * g = ggml_new_tensor(ctx, type, 3, std::vector<int64_t>{ head_size, head_count, n_tokens }.data()); |
| 5322 | ggml_tensor * s = ggml_new_tensor(ctx, type, 2, std::vector<int64_t>{ head_size * head_size * head_count, n_seqs }.data()); |
| 5323 | ggml_tensor * out = ggml_gated_linear_attn(ctx, k, v, q, g, s, pow(head_size, -0.5)); |
| 5324 | return out; |
| 5325 | } |
| 5326 | }; |
| 5327 | |
| 5328 | // GGML_OP_RWKV_WKV7 |
| 5329 | struct test_rwkv_wkv7 : public test_case { |
| 5330 | const ggml_type type; |
| 5331 | |
| 5332 | const int64_t head_count; |
| 5333 | const int64_t head_size; |
| 5334 | const int64_t n_seq_tokens; |
| 5335 | const int64_t n_seqs; |
| 5336 | |
| 5337 | std::string vars() override { |
| 5338 | return VARS_TO_STR5(type, head_count, head_size, n_seq_tokens, n_seqs); |
| 5339 | } |
| 5340 | |
| 5341 | test_rwkv_wkv7(ggml_type type = GGML_TYPE_F32, |
| 5342 | int64_t head_count = 32, int64_t head_size = 64, int64_t n_seq_tokens = 32, int64_t n_seqs = 32) |
| 5343 | : type(type), head_count(head_count), head_size(head_size), n_seq_tokens(n_seq_tokens), n_seqs(n_seqs) {} |
| 5344 | |
| 5345 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 5346 | const int64_t n_tokens = n_seq_tokens * n_seqs; |
| 5347 | ggml_tensor * r = ggml_new_tensor(ctx, type, 3, std::vector<int64_t>{ head_size, head_count, n_tokens }.data()); |
| 5348 | ggml_tensor * w = ggml_new_tensor(ctx, type, 3, std::vector<int64_t>{ head_size, head_count, n_tokens }.data()); |
| 5349 | ggml_tensor * k = ggml_new_tensor(ctx, type, 3, std::vector<int64_t>{ head_size, head_count, n_tokens }.data()); |
| 5350 | ggml_tensor * v = ggml_new_tensor(ctx, type, 3, std::vector<int64_t>{ head_size, head_count, n_tokens }.data()); |
| 5351 | ggml_tensor * a = ggml_new_tensor(ctx, type, 3, std::vector<int64_t>{ head_size, head_count, n_tokens }.data()); |
| 5352 | ggml_tensor * b = ggml_new_tensor(ctx, type, 3, std::vector<int64_t>{ head_size, head_count, n_tokens }.data()); |
| 5353 | // Outputs may become NaN with long seqlen without these normalization |
| 5354 | a = ggml_l2_norm(ctx, a, 1e-7F); |
| 5355 | b = ggml_l2_norm(ctx, b, 1e-7F); |
| 5356 | ggml_tensor * s = ggml_new_tensor(ctx, type, 2, std::vector<int64_t>{ head_size * head_size * head_count, n_seqs }.data()); |
| 5357 | ggml_tensor * out = ggml_rwkv_wkv7(ctx, r, w, k, v, a, b, s); |
| 5358 | return out; |
| 5359 | } |
| 5360 | }; |
| 5361 | |
| 5362 | static int32_t test_get_op_params_i32(const ggml_tensor * tensor, uint32_t i) { |
| 5363 | GGML_ASSERT(i < GGML_MAX_OP_PARAMS / sizeof(int32_t)); |
| 5364 | return tensor->op_params[i]; |
| 5365 | } |
| 5366 | |
| 5367 | // true if any node of the given op requests 8-bit src1 (GGML_PREC_Q8) |
| 5368 | static bool graph_mul_mat_hi_prec_act(ggml_cgraph * gf, ggml_op op) { |
| 5369 | if (gf == nullptr) { |
| 5370 | return false; |
| 5371 | } |
| 5372 | |
| 5373 | ggml_tensor ** nodes = ggml_graph_nodes(gf); |
| 5374 | for (int i = 0; i < ggml_graph_n_nodes(gf); ++i) { |
| 5375 | if (nodes[i]->op == op && test_get_op_params_i32(nodes[i], 3) == GGML_PREC_Q8) { |
| 5376 | return true; |
| 5377 | } |
| 5378 | } |
| 5379 | |
| 5380 | return false; |
| 5381 | } |
| 5382 | |
| 5383 | // GGML_OP_MUL_MAT |
| 5384 | struct test_mul_mat : public test_case { |
| 5385 | const ggml_type type_a; |
| 5386 | const ggml_type type_b; |
| 5387 | const int64_t m; |
| 5388 | const int64_t n; |
| 5389 | const int64_t k; |
| 5390 | const std::array<int64_t, 2> bs; // dims 3 and 4 |
| 5391 | const std::array<int64_t, 2> nr; // repeat in dims 3 and 4 |
| 5392 | const std::array<int64_t, 4> per; // permutation of dimensions |
| 5393 | const int64_t k_v; // size of k in memory, resulting in a non-contiguous view for k_v > k, no view for k_v == 0 |
| 5394 | const uint32_t o; // number of outputs |
| 5395 | const bool src_overlap; // a and b are overlapping views of the same tensor |
| 5396 | const int64_t m_v; // rows of a in memory, the batches of a are strided for m_v > m, no view for m_v == 0 |
| 5397 | const int64_t pad; // bytes after the m_v rows of each batch of a, so nb[2] of a is not a multiple of nb[1] |
| 5398 | |
| 5399 | std::string vars() override { |
| 5400 | return VARS_TO_STR13(type_a, type_b, m, n, k, bs, nr, per, k_v, o, src_overlap, m_v, pad); |
| 5401 | } |
| 5402 | |
| 5403 | double max_nmse_err() override { |
| 5404 | return 5e-4; |
| 5405 | } |
| 5406 | |
| 5407 | double max_nmse_err(ggml_backend_t backend) override { |
| 5408 | // for blackwell we quantize activations to mxfp4 instead of q8_1 so we add higher tolerance |
| 5409 | if ((type_a == GGML_TYPE_MXFP4 || type_a == GGML_TYPE_NVFP4) && |
| 5410 | !graph_mul_mat_hi_prec_act(gf, GGML_OP_MUL_MAT) && |
| 5411 | backend_has_feature(backend, "BLACKWELL_NATIVE_FP4")) { |
| 5412 | return 2e-2; |
| 5413 | } |
| 5414 | if (type_a == GGML_TYPE_F8_E4M3 && backend_has_feature(backend, "NATIVE_FP8")) { |
| 5415 | return 5e-3; |
| 5416 | } |
| 5417 | return max_nmse_err(); |
| 5418 | } |
| 5419 | |
| 5420 | int64_t grad_nmax() override { |
| 5421 | return 20000; |
| 5422 | } |
| 5423 | |
| 5424 | uint64_t op_flops(ggml_tensor * t) override { |
| 5425 | GGML_UNUSED(t); |
| 5426 | return 2 * m * n * k * bs[0] * nr[0] * bs[1] * nr[1]; |
| 5427 | } |
| 5428 | |
| 5429 | test_mul_mat(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32, |
| 5430 | int64_t m = 32, int64_t n = 32, int64_t k = 32, |
| 5431 | std::array<int64_t, 2> bs = {10, 10}, |
| 5432 | std::array<int64_t, 2> nr = {2, 2}, |
| 5433 | std::array<int64_t, 4> per = {0, 1, 2, 3}, |
| 5434 | int64_t k_v = 0, uint32_t o = 1, bool src_overlap = false, int64_t m_v = 0, int64_t pad = 0) |
| 5435 | : type_a(type_a), type_b(type_b), m(m), n(n), k(k), bs(bs), nr(nr), per(per), k_v(k_v), o(o), src_overlap(src_overlap), m_v(m_v), pad(pad) {} |
| 5436 | |
| 5437 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 5438 | // C^T = A * B^T: (k, m) * (k, n) => (m, n) |
| 5439 | ggml_tensor * a; |
| 5440 | ggml_tensor * b; |
| 5441 | |
| 5442 | const int npermuted = (per[0] != 0) + (per[1] != 1) + (per[2] != 2) + (per[3] != 3); |
| 5443 | if (npermuted > 0) { |
| 5444 | GGML_ASSERT(npermuted == 2); |
| 5445 | GGML_ASSERT(k_v == 0); // not handled |
| 5446 | GGML_ASSERT(m_v == 0); // not handled |
| 5447 | GGML_ASSERT(!ggml_is_quantized(type_a) || per[0] == 0); |
| 5448 | GGML_ASSERT(!ggml_is_quantized(type_b) || per[0] == 0); |
| 5449 | |
| 5450 | // Create tensors with the permuted dimensions, then permute them back to the dimensions given by m,n,k. |
| 5451 | const int64_t ne_a[4] = {k, m, bs[0], bs[1]}; |
| 5452 | const int64_t ne_b[4] = {k, n, bs[0]*nr[0], bs[1]*nr[1]}; |
| 5453 | |
| 5454 | a = ggml_new_tensor_4d(ctx, type_a, ne_a[per[0]], ne_a[per[1]], ne_a[per[2]], ne_a[per[3]]); |
| 5455 | b = ggml_new_tensor_4d(ctx, type_b, ne_b[per[0]], ne_b[per[1]], ne_b[per[2]], ne_b[per[3]]); |
| 5456 | if (!ggml_is_quantized(type_a)) { |
| 5457 | if (bs[1] == 1 && nr[1] == 1) { |
| 5458 | ggml_set_param(a); |
| 5459 | } |
| 5460 | ggml_set_param(b); |
| 5461 | } |
| 5462 | ggml_set_name(a, "a"); |
| 5463 | ggml_set_name(b, "b"); |
| 5464 | |
| 5465 | a = ggml_permute(ctx, a, per[0], per[1], per[2], per[3]); |
| 5466 | b = ggml_permute(ctx, b, per[0], per[1], per[2], per[3]); |
| 5467 | ggml_set_name(a, "a_permuted"); |
| 5468 | ggml_set_name(b, "b_permuted"); |
| 5469 | } else if (src_overlap) { |
| 5470 | GGML_ASSERT(type_a == type_b); |
| 5471 | GGML_ASSERT(k_v == 0); |
| 5472 | GGML_ASSERT(m_v == 0); |
| 5473 | |
| 5474 | // a and b are interleaved views of the same tensor: (e.g. fused QKV in MiniMax-01) |
| 5475 | ggml_tensor * base = ggml_new_tensor_4d(ctx, type_a, 2*k, std::max(m, n), bs[0]*nr[0], bs[1]*nr[1]); |
| 5476 | ggml_set_name(base, "base"); |
| 5477 | |
| 5478 | a = ggml_view_4d(ctx, base, k, m, bs[0], bs[1], base->nb[1], base->nb[2], base->nb[3], 0); |
| 5479 | b = ggml_view_4d(ctx, base, k, n, bs[0]*nr[0], bs[1]*nr[1], base->nb[1], base->nb[2], base->nb[3], k*ggml_type_size(type_a)); |
| 5480 | ggml_set_name(a, "a"); |
| 5481 | ggml_set_name(b, "b"); |
| 5482 | } else { |
| 5483 | const int64_t k_physical = k_v == 0 ? k : k_v; |
| 5484 | const int64_t m_physical = m_v == 0 ? m : m_v + (pad != 0); |
| 5485 | a = ggml_new_tensor_4d(ctx, type_a, k_physical, m_physical, bs[0], bs[1]); |
| 5486 | b = ggml_new_tensor_4d(ctx, type_b, k_physical, n, bs[0]*nr[0], bs[1]*nr[1]); |
| 5487 | |
| 5488 | if (!ggml_is_quantized(type_a)) { |
| 5489 | if (bs[1] == 1 && nr[1] == 1) { |
| 5490 | ggml_set_param(a); |
| 5491 | } |
| 5492 | ggml_set_param(b); |
| 5493 | } |
| 5494 | |
| 5495 | if (k_v != 0) { |
| 5496 | GGML_ASSERT(k_v > k); |
| 5497 | a = ggml_view_4d(ctx, a, k, m, bs[0], bs[1], a->nb[1], a->nb[2], a->nb[3], 0); |
| 5498 | b = ggml_view_4d(ctx, b, k, n, bs[0]*nr[0], bs[1]*nr[1], b->nb[1], b->nb[2], b->nb[3], 0); |
| 5499 | } |
| 5500 | if (m_v != 0) { |
| 5501 | GGML_ASSERT(m_v > m); |
| 5502 | GGML_ASSERT(pad < (int64_t) a->nb[1]); |
| 5503 | a = ggml_view_4d(ctx, a, k, m, bs[0], bs[1], a->nb[1], m_v*a->nb[1] + pad, a->nb[3], 0); |
| 5504 | } |
| 5505 | ggml_set_name(a, "a"); |
| 5506 | ggml_set_name(b, "b"); |
| 5507 | } |
| 5508 | |
| 5509 | ggml_tensor * out = ggml_mul_mat(ctx, a, b); |
| 5510 | ggml_set_name(out, "out"); |
| 5511 | for (uint32_t i = 1; i < o; ++i) { |
| 5512 | ggml_tensor * out2 = ggml_mul_mat(ctx, a, b); |
| 5513 | ggml_set_name(out2, "out2"); |
| 5514 | out = ggml_add(ctx, out, out2); |
| 5515 | } |
| 5516 | |
| 5517 | return out; |
| 5518 | } |
| 5519 | |
| 5520 | bool run_whole_graph() override { return o > 1; } |
| 5521 | |
| 5522 | std::string op_desc(ggml_tensor * t) override { |
| 5523 | GGML_UNUSED(t); |
| 5524 | return ggml_op_name(GGML_OP_MUL_MAT); |
| 5525 | } |
| 5526 | }; |
| 5527 | |
| 5528 | // GGML_HINT_SRC0_IS_HADAMARD |
| 5529 | struct test_mul_mat_hadamard : public test_mul_mat { |
| 5530 | test_mul_mat_hadamard(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32, |
| 5531 | int64_t m = 32, int64_t n = 32, int64_t k = 32, |
| 5532 | std::array<int64_t, 2> bs = {1, 1}, |
| 5533 | std::array<int64_t, 2> nr = {1, 1}) |
| 5534 | : test_mul_mat(type_a, type_b, m, n, k, bs, nr) { |
| 5535 | GGML_ASSERT(type_a == GGML_TYPE_F32); |
| 5536 | } |
| 5537 | |
| 5538 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 5539 | ggml_tensor * out = test_mul_mat::build_graph(ctx); |
| 5540 | // Find the mul_mat op in the graph and set the hint |
| 5541 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 5542 | if (t->op == GGML_OP_MUL_MAT) { |
| 5543 | ggml_mul_mat_set_hint(t, GGML_HINT_SRC0_IS_HADAMARD); |
| 5544 | } |
| 5545 | } |
| 5546 | return out; |
| 5547 | } |
| 5548 | |
| 5549 | void initialize_tensors(ggml_context * ctx) override { |
| 5550 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 5551 | if (strcmp(t->name, "a") == 0) { |
| 5552 | const int64_t n_cols = t->ne[0]; |
| 5553 | const int64_t n_rows = ggml_nrows(t); |
| 5554 | std::vector<float> data(n_cols * n_rows); |
| 5555 | float scale = 1.0f / sqrtf((float)n_cols); |
| 5556 | for (int64_t r = 0; r < n_rows; r++) { |
| 5557 | float * row_data = data.data() + r * n_cols; |
| 5558 | for (int64_t i = 0; i < n_cols; i++) { |
| 5559 | int pop = 0; |
| 5560 | int64_t val = r & i; |
| 5561 | while (val) { |
| 5562 | pop += (val & 1); |
| 5563 | val >>= 1; |
| 5564 | } |
| 5565 | row_data[i] = (pop % 2 == 0) ? scale : -scale; |
| 5566 | } |
| 5567 | } |
| 5568 | ggml_backend_tensor_set(t, data.data(), 0, data.size() * sizeof(float)); |
| 5569 | } else if (t->type == GGML_TYPE_F32 || t->type == GGML_TYPE_F16) { |
| 5570 | init_tensor_uniform(t); |
| 5571 | } |
| 5572 | } |
| 5573 | } |
| 5574 | |
| 5575 | std::string op_desc(ggml_tensor * t) override { |
| 5576 | GGML_UNUSED(t); |
| 5577 | return "MUL_MAT_HADAMARD"; |
| 5578 | } |
| 5579 | }; |
| 5580 | |
| 5581 | // FP4 W4A8 path (GGML_PREC_Q8 on src1 disallows 4-bit activations) |
| 5582 | struct test_mul_mat_w4a8 : public test_mul_mat { |
| 5583 | test_mul_mat_w4a8(ggml_type type_a = GGML_TYPE_NVFP4, ggml_type type_b = GGML_TYPE_F32, |
| 5584 | int64_t m = 32, int64_t n = 32, int64_t k = 256, |
| 5585 | std::array<int64_t, 2> bs = {1, 1}, |
| 5586 | std::array<int64_t, 2> nr = {1, 1}) |
| 5587 | : test_mul_mat(type_a, type_b, m, n, k, bs, nr) {} |
| 5588 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 5589 | ggml_tensor * out = test_mul_mat::build_graph(ctx); |
| 5590 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 5591 | if (t->op == GGML_OP_MUL_MAT) { |
| 5592 | ggml_prec_set_src(t, GGML_PREC_Q8, 1); |
| 5593 | } |
| 5594 | } |
| 5595 | return out; |
| 5596 | } |
| 5597 | std::string op_desc(ggml_tensor * t) override { |
| 5598 | GGML_UNUSED(t); |
| 5599 | return "MUL_MAT_W4A8"; |
| 5600 | } |
| 5601 | }; |
| 5602 | |
| 5603 | // FP4 native W4A4 path (default precision) |
| 5604 | struct test_mul_mat_w4a4 : public test_mul_mat { |
| 5605 | test_mul_mat_w4a4(ggml_type type_a = GGML_TYPE_NVFP4, ggml_type type_b = GGML_TYPE_F32, |
| 5606 | int64_t m = 32, int64_t n = 32, int64_t k = 256, |
| 5607 | std::array<int64_t, 2> bs = {1, 1}, |
| 5608 | std::array<int64_t, 2> nr = {1, 1}) |
| 5609 | : test_mul_mat(type_a, type_b, m, n, k, bs, nr) {} |
| 5610 | std::string op_desc(ggml_tensor * t) override { |
| 5611 | GGML_UNUSED(t); |
| 5612 | return "MUL_MAT_W4A4"; |
| 5613 | } |
| 5614 | }; |
| 5615 | |
| 5616 | static void init_mul_mat_id_ids(ggml_context * ctx, int n_mats, bool skip = false) { |
| 5617 | |
| 5618 | std::random_device rd; |
| 5619 | std::default_random_engine rng(rd()); |
| 5620 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 5621 | if (t->type != GGML_TYPE_I32 || ggml_is_view_op(t->op)) { |
| 5622 | continue; |
| 5623 | } |
| 5624 | for (int64_t r = 0; r < ggml_nrows(t); r++) { |
| 5625 | std::vector<int32_t> data(t->ne[0]); |
| 5626 | for (int i = 0; i < t->ne[0]; i++) { |
| 5627 | data[i] = i % n_mats; |
| 5628 | } |
| 5629 | std::shuffle(data.begin(), data.end(), rng); |
| 5630 | if (skip) { |
| 5631 | mask_skipped_ids(data, r); |
| 5632 | } |
| 5633 | ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(int32_t)); |
| 5634 | } |
| 5635 | } |
| 5636 | } |
| 5637 | |
| 5638 | static void init_mul_mat_id_tensors(ggml_context * ctx, int n_mats, bool skip = false) { |
| 5639 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 5640 | if (t->type != GGML_TYPE_I32) { |
| 5641 | init_tensor_uniform(t); |
| 5642 | } |
| 5643 | } |
| 5644 | init_mul_mat_id_ids(ctx, n_mats, skip); |
| 5645 | } |
| 5646 | |
| 5647 | // GGML_OP_MUL_MAT_ID |
| 5648 | struct test_mul_mat_id : public test_case { |
| 5649 | const ggml_type type_a; |
| 5650 | const ggml_type type_b; |
| 5651 | const int n_mats; |
| 5652 | const int n_used; |
| 5653 | const bool b; // broadcast b matrix |
| 5654 | const int64_t m; |
| 5655 | const int64_t n; |
| 5656 | const int64_t k; |
| 5657 | const int64_t m_v; // rows of as in memory, the experts of as are strided for m_v > m, no view for m_v == 0 |
| 5658 | const bool ids_offset; |
| 5659 | const bool skip; // set some ids to a negative value |
| 5660 | |
| 5661 | std::string vars() override { |
| 5662 | return VARS_TO_STR11(type_a, type_b, n_mats, n_used, b, m, n, k, m_v, ids_offset, skip); |
| 5663 | } |
| 5664 | |
| 5665 | double max_nmse_err() override { |
| 5666 | return 5e-4; |
| 5667 | } |
| 5668 | |
| 5669 | double max_nmse_err(ggml_backend_t backend) override { |
| 5670 | // for blackwell we quantize activations to mxfp4 instead of q8_1 so we add higher tolerance |
| 5671 | if ((type_a == GGML_TYPE_MXFP4 || type_a == GGML_TYPE_NVFP4) && |
| 5672 | !graph_mul_mat_hi_prec_act(gf, GGML_OP_MUL_MAT_ID) && |
| 5673 | backend_has_feature(backend, "BLACKWELL_NATIVE_FP4")) { |
| 5674 | return 2e-2; |
| 5675 | } |
| 5676 | if (type_a == GGML_TYPE_F8_E4M3 && backend_has_feature(backend, "NATIVE_FP8")) { |
| 5677 | return 5e-3; |
| 5678 | } |
| 5679 | return max_nmse_err(); |
| 5680 | } |
| 5681 | |
| 5682 | uint64_t op_flops(ggml_tensor * t) override { |
| 5683 | GGML_UNUSED(t); |
| 5684 | return 2 * m * k * n * n_used; |
| 5685 | } |
| 5686 | |
| 5687 | test_mul_mat_id(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32, |
| 5688 | int n_mats = 8, int n_used = 2, bool b = false, |
| 5689 | int64_t m = 32, int64_t n = 32, int64_t k = 32, |
| 5690 | int64_t m_v = 0, bool ids_offset = false, bool skip = false) |
| 5691 | : type_a(type_a), type_b(type_b), n_mats(n_mats), n_used(n_used), b(b), |
| 5692 | m(m), n(n), k(k), m_v(m_v), ids_offset(ids_offset), skip(skip) { |
| 5693 | GGML_ASSERT(n_used <= n_mats); |
| 5694 | GGML_ASSERT(m_v == 0 || m_v > m); |
| 5695 | } |
| 5696 | |
| 5697 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 5698 | // C^T = A * B^T: (k, m) * (k, n) => (m, n) |
| 5699 | ggml_tensor * as = ggml_new_tensor_3d(ctx, type_a, k, m_v == 0 ? m : m_v, n_mats); |
| 5700 | if (m_v != 0) { |
| 5701 | as = ggml_view_3d(ctx, as, k, m, n_mats, as->nb[1], as->nb[2], 0); |
| 5702 | } |
| 5703 | ggml_set_name(as, "as"); |
| 5704 | |
| 5705 | ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_mats + int(ids_offset), n); |
| 5706 | ggml_set_name(ids, "ids"); |
| 5707 | if (n_used != n_mats || ids_offset) { |
| 5708 | ids = ggml_view_2d(ctx, ids, n_used, n, ids->nb[1], ids_offset ? sizeof(int32_t) : 0); |
| 5709 | ggml_set_name(ids, "view_of_ids"); |
| 5710 | } |
| 5711 | |
| 5712 | ggml_tensor * b = ggml_new_tensor_3d(ctx, type_b, k, this->b ? 1 : n_used, n); |
| 5713 | ggml_set_name(b, "b"); |
| 5714 | |
| 5715 | ggml_tensor * out = ggml_mul_mat_id(ctx, as, b, ids); |
| 5716 | ggml_set_name(out, "out"); |
| 5717 | |
| 5718 | return out; |
| 5719 | } |
| 5720 | |
| 5721 | void initialize_tensors(ggml_context * ctx) override { |
| 5722 | init_mul_mat_id_tensors(ctx, n_mats, skip); |
| 5723 | } |
| 5724 | |
| 5725 | void reinit_perf_iter(ggml_context * ctx) override { |
| 5726 | init_mul_mat_id_ids(ctx, n_mats, skip); |
| 5727 | } |
| 5728 | }; |
| 5729 | |
| 5730 | // MUL_MAT_ID with expert ids repeated within a token's row |
| 5731 | struct test_mul_mat_id_dup : public test_mul_mat_id { |
| 5732 | using test_mul_mat_id::test_mul_mat_id; |
| 5733 | std::string vars() override { return test_mul_mat_id::vars() + ",dup=1"; } |
| 5734 | void initialize_tensors(ggml_context * ctx) override { |
| 5735 | std::default_random_engine rng(1234); |
| 5736 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 5737 | if (t->type == GGML_TYPE_I32) { |
| 5738 | if (ggml_is_view_op(t->op)) { continue; } |
| 5739 | for (int64_t r = 0; r < ggml_nrows(t); r++) { |
| 5740 | std::vector<int32_t> data(t->ne[0]); |
| 5741 | for (int i = 0; i < t->ne[0]; i++) { |
| 5742 | data[i] = (rng() % 4 == 0) ? (int32_t) (rng() % n_mats) : 0; // repeated ids, mostly expert 0 |
| 5743 | } |
| 5744 | if (r == ggml_nrows(t) - 1) { |
| 5745 | data[ids_offset ? 1 : 0] = n_mats - 1; |
| 5746 | } |
| 5747 | ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(int32_t)); |
| 5748 | } |
| 5749 | } else { |
| 5750 | init_tensor_uniform(t); |
| 5751 | } |
| 5752 | } |
| 5753 | } |
| 5754 | }; |
| 5755 | |
| 5756 | // FP4 W4A8 path on the MoE path (GGML_PREC_Q8 on src1 disallows 4-bit activations) |
| 5757 | struct test_mul_mat_id_w4a8 : public test_mul_mat_id { |
| 5758 | test_mul_mat_id_w4a8(ggml_type type_a = GGML_TYPE_NVFP4, ggml_type type_b = GGML_TYPE_F32, |
| 5759 | int n_mats = 8, int n_used = 2, bool b = false, |
| 5760 | int64_t m = 32, int64_t n = 32, int64_t k = 256) |
| 5761 | : test_mul_mat_id(type_a, type_b, n_mats, n_used, b, m, n, k) {} |
| 5762 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 5763 | ggml_tensor * out = test_mul_mat_id::build_graph(ctx); |
| 5764 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 5765 | if (t->op == GGML_OP_MUL_MAT_ID) { |
| 5766 | ggml_prec_set_src(t, GGML_PREC_Q8, 1); |
| 5767 | } |
| 5768 | } |
| 5769 | return out; |
| 5770 | } |
| 5771 | std::string op_desc(ggml_tensor * t) override { |
| 5772 | GGML_UNUSED(t); |
| 5773 | return "MUL_MAT_ID_W4A8"; |
| 5774 | } |
| 5775 | }; |
| 5776 | |
| 5777 | // FP4 native W4A4 path on the MoE path (default precision) |
| 5778 | struct test_mul_mat_id_w4a4 : public test_mul_mat_id { |
| 5779 | test_mul_mat_id_w4a4(ggml_type type_a = GGML_TYPE_NVFP4, ggml_type type_b = GGML_TYPE_F32, |
| 5780 | int n_mats = 8, int n_used = 2, bool b = false, |
| 5781 | int64_t m = 32, int64_t n = 32, int64_t k = 256) |
| 5782 | : test_mul_mat_id(type_a, type_b, n_mats, n_used, b, m, n, k) {} |
| 5783 | std::string op_desc(ggml_tensor * t) override { |
| 5784 | GGML_UNUSED(t); |
| 5785 | return "MUL_MAT_ID_W4A4"; |
| 5786 | } |
| 5787 | }; |
| 5788 | |
| 5789 | // GGML_OP_MUL_MAT_ID + GGML_OP_ADD or GGML_OP_MUL |
| 5790 | struct test_mul_mat_id_fusion : public test_case { |
| 5791 | const ggml_type type_a; |
| 5792 | const ggml_type type_b; |
| 5793 | const int n_mats; |
| 5794 | const int n_used; |
| 5795 | const bool b; // broadcast b matrix |
| 5796 | const int64_t m; |
| 5797 | const int64_t n; |
| 5798 | const int64_t k; |
| 5799 | const uint32_t o; // number of outputs |
| 5800 | const bool mul; |
| 5801 | const bool skip; // set some ids to a negative value |
| 5802 | |
| 5803 | std::string vars() override { |
| 5804 | return VARS_TO_STR11(type_a, type_b, n_mats, n_used, b, m, n, k, o, mul, skip); |
| 5805 | } |
| 5806 | |
| 5807 | double max_nmse_err() override { |
| 5808 | return 5e-4; |
| 5809 | } |
| 5810 | |
| 5811 | uint64_t op_flops(ggml_tensor * t) override { |
| 5812 | GGML_UNUSED(t); |
| 5813 | return 2 * m * k * n * n_used; |
| 5814 | } |
| 5815 | |
| 5816 | test_mul_mat_id_fusion(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32, |
| 5817 | int n_mats = 8, int n_used = 2, bool b = false, |
| 5818 | int64_t m = 32, int64_t n = 32, int64_t k = 32, uint32_t o = 1, bool mul = false, bool skip = false) |
| 5819 | : type_a(type_a), type_b(type_b), n_mats(n_mats), n_used(n_used), b(b), |
| 5820 | m(m), n(n), k(k), o(o), mul(mul), skip(skip) { |
| 5821 | GGML_ASSERT(n_used <= n_mats); |
| 5822 | } |
| 5823 | |
| 5824 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 5825 | // C^T = A * B^T: (k, m) * (k, n) => (m, n) |
| 5826 | ggml_tensor * as = ggml_new_tensor_3d(ctx, type_a, k, m, n_mats); |
| 5827 | ggml_set_name(as, "as"); |
| 5828 | |
| 5829 | ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_mats, n); |
| 5830 | ggml_set_name(ids, "ids"); |
| 5831 | if (n_used != n_mats) { |
| 5832 | ids = ggml_view_2d(ctx, ids, n_used, n, ids->nb[1], 0); |
| 5833 | ggml_set_name(ids, "view_of_ids"); |
| 5834 | } |
| 5835 | |
| 5836 | ggml_tensor * b = ggml_new_tensor_3d(ctx, type_b, k, this->b ? 1 : n_used, n); |
| 5837 | ggml_set_name(b, "b"); |
| 5838 | |
| 5839 | ggml_tensor * out = ggml_mul_mat_id(ctx, as, b, ids); |
| 5840 | ggml_set_name(out, "out"); |
| 5841 | |
| 5842 | for (uint32_t i = 1; i < o; ++i) { |
| 5843 | ggml_tensor * a2 = ggml_new_tensor_3d(ctx, type_a, k, m, n_mats); |
| 5844 | ggml_tensor * out2 = ggml_mul_mat_id(ctx, a2, b, ids); |
| 5845 | ggml_set_name(out2, "out2"); |
| 5846 | out = ggml_add(ctx, out, out2); |
| 5847 | } |
| 5848 | |
| 5849 | if (mul) { |
| 5850 | std::array<int64_t, 4> ne { 1, out->ne[1], out->ne[2], out->ne[3] }; |
| 5851 | ne[0] = 1; |
| 5852 | ggml_tensor * m = ggml_new_tensor(ctx, out->type, 4, ne.data()); |
| 5853 | out = ggml_mul(ctx, out, m); |
| 5854 | } |
| 5855 | |
| 5856 | return out; |
| 5857 | } |
| 5858 | |
| 5859 | void initialize_tensors(ggml_context * ctx) override { |
| 5860 | init_mul_mat_id_tensors(ctx, n_mats, skip); |
| 5861 | } |
| 5862 | |
| 5863 | bool run_whole_graph() override { return true; } |
| 5864 | |
| 5865 | std::string op_desc(ggml_tensor * t) override { |
| 5866 | GGML_UNUSED(t); |
| 5867 | return "MUL_MAT_ID_FUSION"; |
| 5868 | } |
| 5869 | }; |
| 5870 | |
| 5871 | struct test_mul_mat_id_shared : public test_case { |
| 5872 | const ggml_type type; |
| 5873 | const int64_t n; |
| 5874 | ggml_tensor * routed = nullptr; |
| 5875 | ggml_tensor * shared = nullptr; |
| 5876 | |
| 5877 | test_mul_mat_id_shared(ggml_type type, int64_t n) : type(type), n(n) {} |
| 5878 | |
| 5879 | std::string vars() override { return VARS_TO_STR2(type, n); } |
| 5880 | std::string op_desc(ggml_tensor *) override { return "MUL_MAT_ID_SHARED"; } |
| 5881 | bool run_whole_graph() override { return true; } |
| 5882 | double max_nmse_err() override { return 5e-4; } |
| 5883 | std::vector<ggml_tensor *> fusion_test_nodes() override { return { routed, shared }; } |
| 5884 | |
| 5885 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 5886 | ggml_tensor * input = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 512, n); |
| 5887 | ggml_tensor * input_ids = ggml_reshape_3d(ctx, input, 512, 1, n); |
| 5888 | ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, 3, n); |
| 5889 | ggml_set_name(ids, "ids"); |
| 5890 | ggml_tensor * up = ggml_new_tensor_3d(ctx, type, 512, 128, 8); |
| 5891 | ggml_tensor * gate = ggml_new_tensor_3d(ctx, type, 512, 128, 8); |
| 5892 | ggml_tensor * shared_up = ggml_new_tensor_2d(ctx, type, 512, 128); |
| 5893 | ggml_tensor * shared_gate = ggml_new_tensor_2d(ctx, type, 512, 128); |
| 5894 | routed = ggml_swiglu_split(ctx, ggml_mul_mat_id(ctx, gate, input_ids, ids), ggml_mul_mat_id(ctx, up, input_ids, ids)); |
| 5895 | shared = ggml_swiglu_split(ctx, ggml_mul_mat(ctx, shared_gate, input), ggml_mul_mat(ctx, shared_up, input)); |
| 5896 | return ggml_add(ctx, routed, ggml_reshape_3d(ctx, shared, 128, 1, n)); |
| 5897 | } |
| 5898 | |
| 5899 | void initialize_tensors(ggml_context * ctx) override { |
| 5900 | init_mul_mat_id_tensors(ctx, 8); |
| 5901 | } |
| 5902 | }; |
| 5903 | |
| 5904 | // GGML_OP_OUT_PROD |
| 5905 | struct test_out_prod : public test_case { |
| 5906 | const ggml_type type_a; |
| 5907 | const ggml_type type_b; |
| 5908 | const int64_t m; |
| 5909 | const int64_t n; |
| 5910 | const int64_t k; |
| 5911 | const std::array<int64_t, 2> bs; // dims 3 and 4 |
| 5912 | const std::array<int64_t, 2> nr; // repeat in dims 3 and 4 |
| 5913 | const bool trans_b; |
| 5914 | |
| 5915 | std::string vars() override { |
| 5916 | return VARS_TO_STR8(type_a, type_b, m, n, k, bs, nr, trans_b); |
| 5917 | } |
| 5918 | |
| 5919 | double max_nmse_err() override { |
| 5920 | return 5e-4; |
| 5921 | } |
| 5922 | |
| 5923 | test_out_prod(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32, |
| 5924 | int64_t m = 32, int64_t n = 32, int64_t k = 32, |
| 5925 | std::array<int64_t, 2> bs = {10, 10}, |
| 5926 | std::array<int64_t, 2> nr = {2, 2}, |
| 5927 | bool trans_b = false) |
| 5928 | : type_a(type_a), type_b(type_b), m(m), n(n), k(k), bs(bs), nr(nr), trans_b(trans_b) {} |
| 5929 | |
| 5930 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 5931 | ggml_tensor * a = ggml_new_tensor_4d(ctx, type_a, m, k, bs[0], bs[1]); |
| 5932 | ggml_set_name(a, "a"); |
| 5933 | |
| 5934 | ggml_tensor * b; |
| 5935 | if (trans_b) { |
| 5936 | b = ggml_new_tensor_4d(ctx, type_b, k, n, bs[0]*nr[0], bs[1]*nr[1]); |
| 5937 | b = ggml_transpose(ctx, b); |
| 5938 | } else { |
| 5939 | b = ggml_new_tensor_4d(ctx, type_b, n, k, bs[0]*nr[0], bs[1]*nr[1]); |
| 5940 | } |
| 5941 | ggml_set_name(b, "b"); |
| 5942 | |
| 5943 | ggml_tensor * out = ggml_out_prod(ctx, a, b); |
| 5944 | ggml_set_name(out, "out"); |
| 5945 | |
| 5946 | return out; |
| 5947 | } |
| 5948 | }; |
| 5949 | |
| 5950 | // GGML_OP_SQR |
| 5951 | struct test_sqr : public test_case { |
| 5952 | const ggml_type type; |
| 5953 | const std::array<int64_t, 4> ne; |
| 5954 | |
| 5955 | std::string vars() override { |
| 5956 | return VARS_TO_STR2(type, ne); |
| 5957 | } |
| 5958 | |
| 5959 | test_sqr(ggml_type type = GGML_TYPE_F32, |
| 5960 | std::array<int64_t, 4> ne = {10, 5, 4, 3}) |
| 5961 | : type(type), ne(ne) {} |
| 5962 | |
| 5963 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 5964 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 5965 | ggml_set_param(a); |
| 5966 | ggml_set_name(a, "a"); |
| 5967 | |
| 5968 | ggml_tensor * out = ggml_sqr(ctx, a); |
| 5969 | ggml_set_name(out, "out"); |
| 5970 | |
| 5971 | return out; |
| 5972 | } |
| 5973 | |
| 5974 | float grad_eps() override { |
| 5975 | return 0.1f * 0.25f*ne[0]*ne[1]*ne[2]*ne[3]; // 10% of expected value of sum. |
| 5976 | } |
| 5977 | }; |
| 5978 | |
| 5979 | // GGML_OP_SQRT |
| 5980 | struct test_sqrt : public test_case { |
| 5981 | const ggml_type type; |
| 5982 | const std::array<int64_t, 4> ne; |
| 5983 | |
| 5984 | std::string vars() override { |
| 5985 | return VARS_TO_STR2(type, ne); |
| 5986 | } |
| 5987 | |
| 5988 | test_sqrt(ggml_type type = GGML_TYPE_F32, |
| 5989 | std::array<int64_t, 4> ne = {10, 3, 3, 2}) |
| 5990 | : type(type), ne(ne) {} |
| 5991 | |
| 5992 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 5993 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 5994 | ggml_set_param(a); |
| 5995 | ggml_set_name(a, "a"); |
| 5996 | |
| 5997 | ggml_tensor * out = ggml_sqrt(ctx, a); |
| 5998 | ggml_set_name(out, "out"); |
| 5999 | |
| 6000 | return out; |
| 6001 | } |
| 6002 | |
| 6003 | void initialize_tensors(ggml_context * ctx) override { |
| 6004 | // fill with positive values |
| 6005 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 6006 | init_tensor_uniform(t, 50.0f, 100.0f); |
| 6007 | } |
| 6008 | } |
| 6009 | |
| 6010 | float grad_eps() override { |
| 6011 | return 20.0f; |
| 6012 | } |
| 6013 | |
| 6014 | bool grad_precise() override { |
| 6015 | return true; |
| 6016 | } |
| 6017 | }; |
| 6018 | |
| 6019 | // GGML_OP_LOG |
| 6020 | struct test_log : public test_case { |
| 6021 | const ggml_type type; |
| 6022 | const std::array<int64_t, 4> ne; |
| 6023 | |
| 6024 | std::string vars() override { |
| 6025 | return VARS_TO_STR2(type, ne); |
| 6026 | } |
| 6027 | |
| 6028 | test_log(ggml_type type = GGML_TYPE_F32, |
| 6029 | std::array<int64_t, 4> ne = {10, 5, 4, 3}) |
| 6030 | : type(type), ne(ne) {} |
| 6031 | |
| 6032 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6033 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 6034 | ggml_set_param(a); |
| 6035 | ggml_set_name(a, "a"); |
| 6036 | |
| 6037 | ggml_tensor * out = ggml_log(ctx, a); |
| 6038 | ggml_set_name(out, "out"); |
| 6039 | |
| 6040 | return out; |
| 6041 | } |
| 6042 | |
| 6043 | void initialize_tensors(ggml_context * ctx) override { |
| 6044 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 6045 | // log(1) == 0, cluster values there to keep the sum low for better precision in the backward pass: |
| 6046 | init_tensor_uniform(t, 0.9f, 1.1f); |
| 6047 | } |
| 6048 | } |
| 6049 | |
| 6050 | bool grad_precise() override { |
| 6051 | return true; |
| 6052 | } |
| 6053 | }; |
| 6054 | |
| 6055 | // GGML_OP_SIN |
| 6056 | struct test_sin : public test_case { |
| 6057 | const ggml_type type; |
| 6058 | const std::array<int64_t, 4> ne; |
| 6059 | |
| 6060 | std::string vars() override { |
| 6061 | return VARS_TO_STR2(type, ne); |
| 6062 | } |
| 6063 | |
| 6064 | test_sin(ggml_type type = GGML_TYPE_F32, |
| 6065 | std::array<int64_t, 4> ne = {10, 2, 2, 2}) |
| 6066 | : type(type), ne(ne) {} |
| 6067 | |
| 6068 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6069 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 6070 | ggml_set_param(a); |
| 6071 | ggml_set_name(a, "a"); |
| 6072 | |
| 6073 | ggml_tensor * out = ggml_sin(ctx, a); |
| 6074 | ggml_set_name(out, "out"); |
| 6075 | |
| 6076 | return out; |
| 6077 | } |
| 6078 | |
| 6079 | void initialize_tensors(ggml_context * ctx) override { |
| 6080 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 6081 | init_tensor_uniform(t, -6.5f, 6.5f); // Covers interval [-2*pi, 2*pi]. |
| 6082 | } |
| 6083 | } |
| 6084 | |
| 6085 | double max_maa_err() override { |
| 6086 | return 1e-3; |
| 6087 | } |
| 6088 | |
| 6089 | float grad_eps() override { |
| 6090 | return 0.2f; |
| 6091 | } |
| 6092 | |
| 6093 | bool grad_precise() override { |
| 6094 | return true; |
| 6095 | } |
| 6096 | }; |
| 6097 | |
| 6098 | // GGML_OP_COS |
| 6099 | struct test_cos : public test_case { |
| 6100 | const ggml_type type; |
| 6101 | const std::array<int64_t, 4> ne; |
| 6102 | |
| 6103 | std::string vars() override { |
| 6104 | return VARS_TO_STR2(type, ne); |
| 6105 | } |
| 6106 | |
| 6107 | test_cos(ggml_type type = GGML_TYPE_F32, |
| 6108 | std::array<int64_t, 4> ne = {10, 2, 2, 2}) |
| 6109 | : type(type), ne(ne) {} |
| 6110 | |
| 6111 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6112 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 6113 | ggml_set_param(a); |
| 6114 | ggml_set_name(a, "a"); |
| 6115 | |
| 6116 | ggml_tensor * out = ggml_cos(ctx, a); |
| 6117 | ggml_set_name(out, "out"); |
| 6118 | |
| 6119 | return out; |
| 6120 | } |
| 6121 | |
| 6122 | void initialize_tensors(ggml_context * ctx) override { |
| 6123 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 6124 | init_tensor_uniform(t, -6.5f, 6.5f); // Covers interval [-2*pi, 2*pi]. |
| 6125 | } |
| 6126 | } |
| 6127 | |
| 6128 | double max_maa_err() override { |
| 6129 | return 1e-3; |
| 6130 | } |
| 6131 | |
| 6132 | float grad_eps() override { |
| 6133 | return 0.2f; |
| 6134 | } |
| 6135 | |
| 6136 | bool grad_precise() override { |
| 6137 | return true; |
| 6138 | } |
| 6139 | }; |
| 6140 | |
| 6141 | // GGML_OP_CLAMP |
| 6142 | struct test_clamp : public test_case { |
| 6143 | const ggml_type type; |
| 6144 | const std::array<int64_t, 4> ne; |
| 6145 | float min; |
| 6146 | float max; |
| 6147 | int v; // view (1 : non-contiguous a) |
| 6148 | |
| 6149 | std::string vars() override { |
| 6150 | return VARS_TO_STR5(type, ne, min, max, v); |
| 6151 | } |
| 6152 | |
| 6153 | test_clamp(ggml_type type = GGML_TYPE_F32, |
| 6154 | std::array<int64_t, 4> ne = {10, 5, 4, 3}, |
| 6155 | float min = -0.5f, float max = 0.5f, int v = 0) |
| 6156 | : type(type), ne(ne), min(min), max(max), v(v) {} |
| 6157 | |
| 6158 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6159 | ggml_tensor * a; |
| 6160 | if (v & 1) { |
| 6161 | auto ne_a = ne; |
| 6162 | ne_a[0] *= 3; |
| 6163 | ne_a[1] *= 2; |
| 6164 | ne_a[2] *= 5; |
| 6165 | ne_a[3] *= 4; |
| 6166 | a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 6167 | ggml_set_name(a, "a"); |
| 6168 | |
| 6169 | a = ggml_view_4d(ctx, a, ne[0], ne[1], ne[2], ne[3], a->nb[1], a->nb[2], a->nb[3], 0); |
| 6170 | ggml_set_name(a, "view_of_a"); |
| 6171 | } else { |
| 6172 | a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 6173 | ggml_set_name(a, "a"); |
| 6174 | } |
| 6175 | |
| 6176 | ggml_tensor * out = ggml_clamp(ctx, a, min, max); |
| 6177 | ggml_set_name(out, "out"); |
| 6178 | |
| 6179 | return out; |
| 6180 | } |
| 6181 | |
| 6182 | float grad_eps() override { |
| 6183 | return 1e-2f; |
| 6184 | } |
| 6185 | |
| 6186 | std::vector<float> grad_expect() override { |
| 6187 | return {0.0f, 1.0f}; |
| 6188 | } |
| 6189 | }; |
| 6190 | |
| 6191 | // GGML_OP_FLOOR |
| 6192 | struct test_floor : public test_case { |
| 6193 | const ggml_type type; |
| 6194 | const std::array<int64_t, 4> ne; |
| 6195 | |
| 6196 | std::string vars() override { |
| 6197 | return VARS_TO_STR2(type, ne); |
| 6198 | } |
| 6199 | |
| 6200 | test_floor(ggml_type type = GGML_TYPE_F32, |
| 6201 | std::array<int64_t, 4> ne = {10, 2, 2, 2}) |
| 6202 | : type(type), ne(ne) {} |
| 6203 | |
| 6204 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6205 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 6206 | ggml_set_param(a); |
| 6207 | ggml_set_name(a, "a"); |
| 6208 | |
| 6209 | ggml_tensor * out = ggml_floor(ctx, a); |
| 6210 | ggml_set_name(out, "out"); |
| 6211 | |
| 6212 | return out; |
| 6213 | } |
| 6214 | |
| 6215 | void initialize_tensors(ggml_context * ctx) override { |
| 6216 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 6217 | init_tensor_uniform(t, -10.0f, 10.0f); |
| 6218 | } |
| 6219 | } |
| 6220 | }; |
| 6221 | |
| 6222 | // GGML_OP_CEIL |
| 6223 | struct test_ceil : public test_case { |
| 6224 | const ggml_type type; |
| 6225 | const std::array<int64_t, 4> ne; |
| 6226 | |
| 6227 | std::string vars() override { |
| 6228 | return VARS_TO_STR2(type, ne); |
| 6229 | } |
| 6230 | |
| 6231 | test_ceil(ggml_type type = GGML_TYPE_F32, |
| 6232 | std::array<int64_t, 4> ne = {10, 2, 2, 2}) |
| 6233 | : type(type), ne(ne) {} |
| 6234 | |
| 6235 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6236 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 6237 | ggml_set_param(a); |
| 6238 | ggml_set_name(a, "a"); |
| 6239 | |
| 6240 | ggml_tensor * out = ggml_ceil(ctx, a); |
| 6241 | ggml_set_name(out, "out"); |
| 6242 | |
| 6243 | return out; |
| 6244 | } |
| 6245 | |
| 6246 | void initialize_tensors(ggml_context * ctx) override { |
| 6247 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 6248 | init_tensor_uniform(t, -10.0f, 10.0f); |
| 6249 | } |
| 6250 | } |
| 6251 | }; |
| 6252 | |
| 6253 | // GGML_OP_ROUND |
| 6254 | struct test_round : public test_case { |
| 6255 | const ggml_type type; |
| 6256 | const std::array<int64_t, 4> ne; |
| 6257 | |
| 6258 | std::string vars() override { |
| 6259 | return VARS_TO_STR2(type, ne); |
| 6260 | } |
| 6261 | |
| 6262 | test_round(ggml_type type = GGML_TYPE_F32, |
| 6263 | std::array<int64_t, 4> ne = {10, 2, 2, 2}) |
| 6264 | : type(type), ne(ne) {} |
| 6265 | |
| 6266 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6267 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 6268 | ggml_set_param(a); |
| 6269 | ggml_set_name(a, "a"); |
| 6270 | |
| 6271 | ggml_tensor * out = ggml_round(ctx, a); |
| 6272 | ggml_set_name(out, "out"); |
| 6273 | |
| 6274 | return out; |
| 6275 | } |
| 6276 | |
| 6277 | void initialize_tensors(ggml_context * ctx) override { |
| 6278 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 6279 | init_tensor_uniform(t, -10.0f, 10.0f); |
| 6280 | } |
| 6281 | } |
| 6282 | }; |
| 6283 | |
| 6284 | // GGML_OP_TRUNC |
| 6285 | struct test_trunc : public test_case { |
| 6286 | const ggml_type type; |
| 6287 | const std::array<int64_t, 4> ne; |
| 6288 | |
| 6289 | std::string vars() override { |
| 6290 | return VARS_TO_STR2(type, ne); |
| 6291 | } |
| 6292 | |
| 6293 | test_trunc(ggml_type type = GGML_TYPE_F32, |
| 6294 | std::array<int64_t, 4> ne = {10, 2, 2, 2}) |
| 6295 | : type(type), ne(ne) {} |
| 6296 | |
| 6297 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6298 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 6299 | ggml_set_param(a); |
| 6300 | ggml_set_name(a, "a"); |
| 6301 | |
| 6302 | ggml_tensor * out = ggml_trunc(ctx, a); |
| 6303 | ggml_set_name(out, "out"); |
| 6304 | |
| 6305 | return out; |
| 6306 | } |
| 6307 | |
| 6308 | void initialize_tensors(ggml_context * ctx) override { |
| 6309 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 6310 | init_tensor_uniform(t, -10.0f, 10.0f); |
| 6311 | } |
| 6312 | } |
| 6313 | }; |
| 6314 | |
| 6315 | // GGML_OP_DIAG_MASK_INF |
| 6316 | struct test_diag_mask_inf : public test_case { |
| 6317 | const ggml_type type; |
| 6318 | const std::array<int64_t, 4> ne; |
| 6319 | const int n_past; |
| 6320 | |
| 6321 | std::string vars() override { |
| 6322 | return VARS_TO_STR3(type, ne, n_past); |
| 6323 | } |
| 6324 | |
| 6325 | test_diag_mask_inf(ggml_type type = GGML_TYPE_F32, |
| 6326 | std::array<int64_t, 4> ne = {10, 10, 3, 2}, |
| 6327 | int n_past = 5) |
| 6328 | : type(type), ne(ne), n_past(n_past) {} |
| 6329 | |
| 6330 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6331 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 6332 | ggml_set_param(a); |
| 6333 | ggml_set_name(a, "a"); |
| 6334 | |
| 6335 | ggml_tensor * out = ggml_diag_mask_inf(ctx, a, n_past); |
| 6336 | ggml_set_name(out, "out"); |
| 6337 | |
| 6338 | return out; |
| 6339 | } |
| 6340 | }; |
| 6341 | |
| 6342 | // GGML_OP_SOFT_MAX |
| 6343 | struct test_soft_max : public test_case { |
| 6344 | const ggml_type type; |
| 6345 | const std::array<int64_t, 4> ne; |
| 6346 | const bool mask; |
| 6347 | const bool sinks; |
| 6348 | const ggml_type m_prec; |
| 6349 | const std::array<int64_t, 2> nr23; // broadcast only dims 2 and 3 |
| 6350 | const float scale; |
| 6351 | const float max_bias; |
| 6352 | const bool inplace; |
| 6353 | |
| 6354 | std::string vars() override { |
| 6355 | return VARS_TO_STR9(type, ne, mask, sinks, m_prec, nr23, scale, max_bias, inplace); |
| 6356 | } |
| 6357 | |
| 6358 | // the 1024 test with bias occasionally fails: |
| 6359 | // SOFT_MAX(type=f32,ne=[1024,16,1,1],mask=1,scale=1.000000,max_bias=8.000000): [SOFT_MAX] NMSE = 0.000000103 > 0.000000100 FAIL |
| 6360 | virtual double max_nmse_err() override { |
| 6361 | return 1e-6; |
| 6362 | } |
| 6363 | |
| 6364 | test_soft_max(ggml_type type = GGML_TYPE_F32, |
| 6365 | std::array<int64_t, 4> ne = {10, 5, 4, 3}, |
| 6366 | bool mask = false, |
| 6367 | bool sinks = false, |
| 6368 | ggml_type m_prec = GGML_TYPE_F32, |
| 6369 | std::array<int64_t, 2> nr23 = {1, 1}, |
| 6370 | float scale = 1.0f, |
| 6371 | float max_bias = 0.0f, |
| 6372 | bool inplace = false) |
| 6373 | : type(type), ne(ne), mask(mask), sinks(sinks), m_prec(m_prec), nr23(nr23), scale(scale), max_bias(max_bias), inplace(inplace) {} |
| 6374 | |
| 6375 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6376 | ggml_tensor * a = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2]*nr23[0], ne[3]*nr23[1]); |
| 6377 | ggml_set_param(a); |
| 6378 | ggml_set_name(a, "a"); |
| 6379 | |
| 6380 | ggml_tensor * mask = nullptr; |
| 6381 | if (this->mask) { |
| 6382 | mask = ggml_new_tensor_4d(ctx, m_prec, ne[0], ne[1], ne[2], ne[3]); |
| 6383 | ggml_set_name(mask, "mask"); |
| 6384 | } |
| 6385 | |
| 6386 | ggml_tensor * sinks = nullptr; |
| 6387 | if (this->sinks) { |
| 6388 | sinks = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, ne[2]*nr23[0]); |
| 6389 | ggml_set_name(sinks, "sinks"); |
| 6390 | } |
| 6391 | |
| 6392 | ggml_tensor * out; |
| 6393 | if (inplace) { |
| 6394 | out = ggml_soft_max_ext_inplace(ctx, a, mask, scale, max_bias); |
| 6395 | } else { |
| 6396 | out = ggml_soft_max_ext(ctx, a, mask, scale, max_bias); |
| 6397 | } |
| 6398 | ggml_soft_max_add_sinks(out, sinks); |
| 6399 | ggml_set_name(out, "out"); |
| 6400 | |
| 6401 | return out; |
| 6402 | } |
| 6403 | |
| 6404 | bool grad_precise() override { |
| 6405 | return true; |
| 6406 | } |
| 6407 | }; |
| 6408 | |
| 6409 | // GGML_OP_SOFT_MAX_BACK |
| 6410 | struct test_soft_max_back : public test_case { |
| 6411 | const ggml_type type; |
| 6412 | const std::array<int64_t, 4> ne; |
| 6413 | const float scale; |
| 6414 | const float max_bias; |
| 6415 | |
| 6416 | std::string vars() override { |
| 6417 | return VARS_TO_STR4(type, ne, scale, max_bias); |
| 6418 | } |
| 6419 | |
| 6420 | test_soft_max_back(ggml_type type = GGML_TYPE_F32, |
| 6421 | std::array<int64_t, 4> ne = {10, 5, 4, 3}, |
| 6422 | float scale = 1.0f, |
| 6423 | float max_bias = 0.0f) |
| 6424 | : type(type), ne(ne), scale(scale), max_bias(max_bias) {} |
| 6425 | |
| 6426 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6427 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 6428 | ggml_set_name(a, "a"); |
| 6429 | |
| 6430 | ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 6431 | ggml_set_name(a, "a"); |
| 6432 | |
| 6433 | ggml_tensor * out = ggml_soft_max_ext_back(ctx, a, b, scale, max_bias); |
| 6434 | ggml_set_name(out, "out"); |
| 6435 | |
| 6436 | return out; |
| 6437 | } |
| 6438 | }; |
| 6439 | |
| 6440 | // GGML_OP_ROPE + GGML_OP_ROPE_BACK |
| 6441 | struct test_rope : public test_case { |
| 6442 | const ggml_type type; |
| 6443 | const std::array<int64_t, 4> ne_a; |
| 6444 | int n_dims; |
| 6445 | int mode; |
| 6446 | int n_ctx; // used to generate positions |
| 6447 | float fs; // freq_scale |
| 6448 | float ef; // ext_factor |
| 6449 | float af; // attn_factor |
| 6450 | bool ff; |
| 6451 | int v; // view (1 : non-contiguous a) |
| 6452 | bool forward; |
| 6453 | bool inplace; |
| 6454 | int n_offs; // offset of the rotated dims window, set via ggml_rope_set_offset() |
| 6455 | |
| 6456 | std::string vars() override { |
| 6457 | // forward can be inferred from the op, does not need to be printed |
| 6458 | return VARS_TO_STR12(type, ne_a, n_dims, mode, n_ctx, fs, ef, af, ff, v, inplace, n_offs); |
| 6459 | } |
| 6460 | |
| 6461 | test_rope(ggml_type type = GGML_TYPE_F32, |
| 6462 | std::array<int64_t, 4> ne_a = {10, 5, 3, 1}, |
| 6463 | int n_dims = 10, int mode = GGML_ROPE_TYPE_NORMAL, int n_ctx = 512, float fs = 1.0f, |
| 6464 | float ef = 0.0f, float af = 0.0f, bool ff = false, int v = 0, bool forward = true, bool inplace = false, |
| 6465 | int n_offs = 0) |
| 6466 | : type(type), ne_a(ne_a), n_dims(n_dims), mode(mode), n_ctx(n_ctx), fs(fs), ef(ef), af(af), ff(ff), v(v), forward(forward), inplace(inplace), n_offs(n_offs) {} |
| 6467 | |
| 6468 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6469 | ggml_tensor * a; |
| 6470 | if (v & 1) { |
| 6471 | auto ne = ne_a; ne[0] *= 2; ne[1] *= 4; ne[2] *= 3; |
| 6472 | a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 6473 | if (forward && n_offs == 0) { |
| 6474 | // FIXME: support gradients with n_offs > 0 |
| 6475 | ggml_set_param(a); |
| 6476 | } |
| 6477 | ggml_set_name(a, "a"); |
| 6478 | |
| 6479 | a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0); |
| 6480 | ggml_set_name(a, "view_of_a"); |
| 6481 | } else if (v == 2) { |
| 6482 | // second-half slice along dim 0 (mimics build_rope_2d in clip.cpp). |
| 6483 | // The non-zero view offset (ne_a[0] * elem_size) often produces a |
| 6484 | // non-aligned buffer offset, which exercises backends' alignment paths. |
| 6485 | auto ne = ne_a; ne[0] *= 2; |
| 6486 | a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 6487 | if (forward && n_offs == 0) { |
| 6488 | // FIXME: support gradients with n_offs > 0 |
| 6489 | ggml_set_param(a); |
| 6490 | } |
| 6491 | ggml_set_name(a, "a"); |
| 6492 | |
| 6493 | a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], |
| 6494 | a->nb[1], a->nb[2], a->nb[3], |
| 6495 | ne_a[0] * ggml_element_size(a)); |
| 6496 | ggml_set_name(a, "view_of_a"); |
| 6497 | } else { |
| 6498 | a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 6499 | if (forward && n_offs == 0) { |
| 6500 | // FIXME: support gradients with n_offs > 0 |
| 6501 | ggml_set_param(a); |
| 6502 | } |
| 6503 | ggml_set_name(a, "a"); |
| 6504 | } |
| 6505 | |
| 6506 | const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE; |
| 6507 | const bool is_vision = mode == GGML_ROPE_TYPE_VISION; |
| 6508 | |
| 6509 | ggml_tensor * pos; |
| 6510 | if (is_mrope || is_vision) { |
| 6511 | pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, ne_a[2] * 4); |
| 6512 | } else { |
| 6513 | pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, ne_a[2]); |
| 6514 | } |
| 6515 | ggml_set_name(pos, "pos"); |
| 6516 | |
| 6517 | ggml_tensor * freq = nullptr; |
| 6518 | if (ff) { |
| 6519 | freq = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_dims/2); |
| 6520 | ggml_set_name(freq, "freq"); |
| 6521 | } |
| 6522 | |
| 6523 | ggml_tensor * out; |
| 6524 | if (is_mrope) { |
| 6525 | if (is_vision) { |
| 6526 | GGML_ASSERT(n_dims/4 > 0); |
| 6527 | int rope_sections[4] = {n_dims/4, n_dims/4, 0, 0}; // Vision-RoPE only use first two dimension for image (x, y) coordinate |
| 6528 | if (forward) { |
| 6529 | if (inplace) { |
| 6530 | out = ggml_rope_multi_inplace(ctx, a, pos, freq, n_dims/2, rope_sections, mode, 0, 10000.0f, fs, ef, af, 1.0f, 1.0f); |
| 6531 | } else { |
| 6532 | out = ggml_rope_multi(ctx, a, pos, freq, n_dims/2, rope_sections, mode, 0, 10000.0f, fs, ef, af, 1.0f, 1.0f); |
| 6533 | } |
| 6534 | } else { |
| 6535 | out = ggml_rope_multi_back(ctx, a, pos, freq, n_dims/2, rope_sections, mode, 0, 10000.0f, fs, ef, af, 1.0f, 1.0f); |
| 6536 | } |
| 6537 | } else { |
| 6538 | GGML_ASSERT(n_dims/3 > 0); |
| 6539 | int rope_sections[4] = {n_dims/3, n_dims/3, n_dims/3, 0}; |
| 6540 | if (forward) { |
| 6541 | if (inplace) { |
| 6542 | out = ggml_rope_multi_inplace(ctx, a, pos, freq, n_dims, rope_sections, mode, 0, 10000.0f, fs, ef, af, 1.0f, 1.0f); |
| 6543 | } else { |
| 6544 | out = ggml_rope_multi(ctx, a, pos, freq, n_dims, rope_sections, mode, 0, 10000.0f, fs, ef, af, 1.0f, 1.0f); |
| 6545 | } |
| 6546 | } else { |
| 6547 | out = ggml_rope_multi_back(ctx, a, pos, freq, n_dims, rope_sections, mode, 0, 10000.0f, fs, ef, af, 1.0f, 1.0f); |
| 6548 | } |
| 6549 | } |
| 6550 | } else { |
| 6551 | if (forward) { |
| 6552 | if (inplace) { |
| 6553 | out = ggml_rope_ext_inplace(ctx, a, pos, freq, n_dims, mode, 0, 10000.0f, fs, ef, af, 1.0f, 1.0f); |
| 6554 | } else { |
| 6555 | out = ggml_rope_ext(ctx, a, pos, freq, n_dims, mode, 0, 10000.0f, fs, ef, af, 1.0f, 1.0f); |
| 6556 | } |
| 6557 | } else { |
| 6558 | out = ggml_rope_ext_back(ctx, a, pos, freq, n_dims, mode, 0, 10000.0f, fs, ef, af, 1.0f, 1.0f); |
| 6559 | } |
| 6560 | } |
| 6561 | if (n_offs != 0) { |
| 6562 | out = ggml_rope_set_offset(out, n_offs); |
| 6563 | } |
| 6564 | ggml_set_name(out, "out"); |
| 6565 | |
| 6566 | return out; |
| 6567 | } |
| 6568 | |
| 6569 | void initialize_tensors(ggml_context * ctx) override { |
| 6570 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 6571 | if (t->type == GGML_TYPE_I32) { |
| 6572 | // pos |
| 6573 | const int num_pos_ids = (mode & GGML_ROPE_TYPE_MROPE) ? ne_a[2] * 4 : ne_a[2]; |
| 6574 | std::vector<int> data(num_pos_ids); |
| 6575 | for (int i = 0; i < num_pos_ids; i++) { |
| 6576 | data[i] = rand() % n_ctx; |
| 6577 | } |
| 6578 | ggml_backend_tensor_set(t, data.data(), 0, num_pos_ids * sizeof(int)); |
| 6579 | } else { |
| 6580 | if (t->ne[0] == n_dims/2) { |
| 6581 | // frequency factors in the range [0.9f, 1.1f] |
| 6582 | init_tensor_uniform(t, 0.9f, 1.1f); |
| 6583 | } else { |
| 6584 | init_tensor_uniform(t); |
| 6585 | } |
| 6586 | } |
| 6587 | } |
| 6588 | } |
| 6589 | |
| 6590 | double max_maa_err() override { |
| 6591 | return 1e-3; |
| 6592 | } |
| 6593 | |
| 6594 | bool grad_precise() override { |
| 6595 | return true; |
| 6596 | } |
| 6597 | }; |
| 6598 | |
| 6599 | // GGML_OP_POOL2D |
| 6600 | struct test_pool2d : public test_case { |
| 6601 | enum ggml_op_pool pool_type; |
| 6602 | const ggml_type type_input; |
| 6603 | const std::array<int64_t, 4> ne_input; |
| 6604 | // kernel size |
| 6605 | const int k0; |
| 6606 | const int k1; |
| 6607 | // stride |
| 6608 | const int s0; |
| 6609 | const int s1; |
| 6610 | // padding |
| 6611 | const int p0; |
| 6612 | const int p1; |
| 6613 | |
| 6614 | std::string vars() override { |
| 6615 | return VARS_TO_STR9(pool_type, type_input, ne_input, k0, k1, s0, s1, p0, p1); |
| 6616 | } |
| 6617 | |
| 6618 | test_pool2d(ggml_op_pool pool_type = GGML_OP_POOL_AVG, |
| 6619 | ggml_type type_input = GGML_TYPE_F32, |
| 6620 | std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1] |
| 6621 | int k0 = 3, int k1 = 3, |
| 6622 | int s0 = 1, int s1 = 1, |
| 6623 | int p0 = 1, int p1 = 1) |
| 6624 | : pool_type(pool_type), type_input(type_input), ne_input(ne_input), k0(k0), k1(k1), s0(s0), s1(s1), p0(p0), p1(p1) {} |
| 6625 | |
| 6626 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6627 | ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data()); |
| 6628 | ggml_set_param(input); |
| 6629 | ggml_set_name(input, "input"); |
| 6630 | |
| 6631 | ggml_tensor * out = ggml_pool_2d(ctx, input, pool_type, k0, k1, s0, s1, p0, p1); |
| 6632 | ggml_set_name(out, "out"); |
| 6633 | |
| 6634 | return out; |
| 6635 | } |
| 6636 | }; |
| 6637 | |
| 6638 | // GGML_OP_POOL1D |
| 6639 | struct test_pool1d : public test_case { |
| 6640 | enum ggml_op_pool pool_type; |
| 6641 | const ggml_type type_input; |
| 6642 | const std::array<int64_t, 4> ne_input; |
| 6643 | const int k0; |
| 6644 | const int s0; |
| 6645 | const int p0; |
| 6646 | |
| 6647 | std::string vars() override { |
| 6648 | return VARS_TO_STR6(pool_type, type_input, ne_input, k0, s0, p0); |
| 6649 | } |
| 6650 | |
| 6651 | test_pool1d(ggml_op_pool pool_type = GGML_OP_POOL_AVG, |
| 6652 | ggml_type type_input = GGML_TYPE_F32, |
| 6653 | std::array<int64_t,4> ne_input = {10, 1, 1, 1}, |
| 6654 | int k0 = 3, int s0 = 3, int p0 = 0) |
| 6655 | : pool_type(pool_type), type_input(type_input), ne_input(ne_input), k0(k0), s0(s0), p0(p0) {} |
| 6656 | |
| 6657 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6658 | ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data()); |
| 6659 | ggml_set_param(input); |
| 6660 | ggml_set_name(input, "input"); |
| 6661 | |
| 6662 | ggml_tensor * out = ggml_pool_1d(ctx, input, pool_type, k0, s0, p0); |
| 6663 | ggml_set_name(out, "out"); |
| 6664 | |
| 6665 | return out; |
| 6666 | } |
| 6667 | }; |
| 6668 | |
| 6669 | // GGML_OP_CONV_TRANSPOSE_1D |
| 6670 | struct test_conv_transpose_1d : public test_case { |
| 6671 | const std::array<int64_t, 4> ne_input; |
| 6672 | const std::array<int64_t, 4> ne_kernel; |
| 6673 | |
| 6674 | const int s0; // stride |
| 6675 | const int p0; // padding |
| 6676 | const int d0; // dilation |
| 6677 | |
| 6678 | std::string vars() override { |
| 6679 | return VARS_TO_STR5(ne_input, ne_kernel, s0, p0, d0); |
| 6680 | } |
| 6681 | |
| 6682 | test_conv_transpose_1d(std::array<int64_t, 4> ne_input = {197, 32, 1, 1}, // [input_width, input_channels, 1 /* assert in cpu kernel*/, 1 (should be batch)] |
| 6683 | std::array<int64_t, 4> ne_kernel = {16, 32, 32, 1}, // [kernel_width, output_channels, input_channels, 1 (should be batch)] |
| 6684 | int s0 = 1, int p0 = 0, int d0 = 1) |
| 6685 | : ne_input(ne_input), ne_kernel(ne_kernel), s0(s0), p0(p0), d0(d0) {} |
| 6686 | |
| 6687 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6688 | ggml_tensor * input = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_input.data()); |
| 6689 | ggml_set_name(input, "input"); |
| 6690 | |
| 6691 | ggml_tensor * kernel = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_kernel.data()); |
| 6692 | ggml_set_name(kernel, "kernel"); |
| 6693 | |
| 6694 | ggml_tensor * out = ggml_conv_transpose_1d(ctx, kernel, input, s0, p0, d0); |
| 6695 | ggml_set_name(out, "out"); |
| 6696 | |
| 6697 | return out; |
| 6698 | } |
| 6699 | }; |
| 6700 | |
| 6701 | // GGML_OP_COL2IM_1D |
| 6702 | struct test_col2im_1d : public test_case { |
| 6703 | const ggml_type type; |
| 6704 | const int64_t K; // kernel size |
| 6705 | const int64_t OC; // output channels |
| 6706 | const int64_t T_in; // input length (number of columns) |
| 6707 | const int s0; // stride |
| 6708 | const int p0; // padding cropped from both sides |
| 6709 | |
| 6710 | std::string vars() override { |
| 6711 | return VARS_TO_STR6(type, K, OC, T_in, s0, p0); |
| 6712 | } |
| 6713 | |
| 6714 | double max_nmse_err() override { |
| 6715 | return type == GGML_TYPE_F32 ? 1e-7 : 5e-4; |
| 6716 | } |
| 6717 | |
| 6718 | test_col2im_1d(ggml_type type = GGML_TYPE_F32, |
| 6719 | int64_t K = 4, int64_t OC = 3, int64_t T_in = 7, |
| 6720 | int s0 = 2, int p0 = 0) |
| 6721 | : type(type), K(K), OC(OC), T_in(T_in), s0(s0), p0(p0) {} |
| 6722 | |
| 6723 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6724 | ggml_tensor * cols = ggml_new_tensor_2d(ctx, type, K*OC, T_in); |
| 6725 | ggml_set_name(cols, "cols"); |
| 6726 | |
| 6727 | ggml_tensor * out = ggml_col2im_1d(ctx, cols, s0, (int) OC, p0); |
| 6728 | ggml_set_name(out, "out"); |
| 6729 | |
| 6730 | return out; |
| 6731 | } |
| 6732 | }; |
| 6733 | |
| 6734 | // GGML_OP_CONV_TRANSPOSE_2D |
| 6735 | struct test_conv_transpose_2d : public test_case { |
| 6736 | // Dimensions |
| 6737 | const std::array<int64_t, 4> ne_input; |
| 6738 | const std::array<int64_t, 4> ne_kernel; |
| 6739 | const int stride; |
| 6740 | // Types |
| 6741 | const ggml_type kernel_type; |
| 6742 | |
| 6743 | std::string vars() override { |
| 6744 | return VARS_TO_STR4(kernel_type, ne_input, ne_kernel, stride); |
| 6745 | } |
| 6746 | |
| 6747 | double max_nmse_err() override { |
| 6748 | return 5e-4; // The default 1e-7 is too small for Vulkan. |
| 6749 | } |
| 6750 | |
| 6751 | test_conv_transpose_2d( |
| 6752 | std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1] |
| 6753 | std::array<int64_t, 4> ne_kernel = {3, 3, 3, 1}, // [kernel_width, kernel_height, input_channels, 1] |
| 6754 | int stride = 1, |
| 6755 | ggml_type kernel_type = GGML_TYPE_F16 |
| 6756 | ) : ne_input(ne_input), ne_kernel(ne_kernel), stride(stride), kernel_type(kernel_type) {} |
| 6757 | |
| 6758 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6759 | ggml_tensor * input = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_input.data()); |
| 6760 | ggml_set_name(input, "input"); |
| 6761 | |
| 6762 | ggml_tensor * kernel = ggml_new_tensor(ctx, kernel_type, 4, ne_kernel.data()); |
| 6763 | ggml_set_name(kernel, "kernel"); |
| 6764 | |
| 6765 | ggml_tensor * out = ggml_conv_transpose_2d_p0(ctx, kernel, input, stride); |
| 6766 | ggml_set_name(out, "out"); |
| 6767 | |
| 6768 | return out; |
| 6769 | } |
| 6770 | }; |
| 6771 | |
| 6772 | // GGML_OP_IM2COL |
| 6773 | struct test_im2col : public test_case { |
| 6774 | const ggml_type type_input; |
| 6775 | const ggml_type type_kernel; |
| 6776 | const ggml_type dst_type; |
| 6777 | const std::array<int64_t, 4> ne_input; |
| 6778 | const std::array<int64_t, 4> ne_kernel; |
| 6779 | // stride |
| 6780 | const int s0; |
| 6781 | const int s1; |
| 6782 | // padding |
| 6783 | const int p0; |
| 6784 | const int p1; |
| 6785 | // dilation |
| 6786 | const int d0; |
| 6787 | const int d1; |
| 6788 | // mode |
| 6789 | const bool is_2D; |
| 6790 | |
| 6791 | std::string vars() override { |
| 6792 | return VARS_TO_STR12(type_input, type_kernel, dst_type, ne_input, ne_kernel, s0, s1, p0, p1, d0, d1, is_2D); |
| 6793 | } |
| 6794 | |
| 6795 | test_im2col(ggml_type type_input = GGML_TYPE_F32, ggml_type type_kernel = GGML_TYPE_F16, ggml_type dst_type = GGML_TYPE_F32, |
| 6796 | std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1] |
| 6797 | std::array<int64_t, 4> ne_kernel = {3, 3, 3, 1}, // [kernel_width, kernel_height, input_channels, 1] |
| 6798 | int s0 = 1, int s1 = 1, |
| 6799 | int p0 = 1, int p1 = 1, |
| 6800 | int d0 = 1, int d1 = 1, |
| 6801 | bool is_2D = true) |
| 6802 | : type_input(type_input), type_kernel(type_kernel), dst_type(dst_type), ne_input(ne_input), ne_kernel(ne_kernel), s0(s0), s1(s1), p0(p0), p1(p1), d0(d0), d1(d1), is_2D(is_2D) {} |
| 6803 | |
| 6804 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6805 | ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data()); |
| 6806 | ggml_set_param(input); |
| 6807 | ggml_set_name(input, "input"); |
| 6808 | |
| 6809 | ggml_tensor * kernel = ggml_new_tensor(ctx, type_kernel, 4, ne_kernel.data()); |
| 6810 | ggml_set_name(kernel, "kernel"); |
| 6811 | |
| 6812 | ggml_tensor * out = ggml_im2col(ctx, kernel, input, s0, s1, p0, p1, d0, d1, is_2D, dst_type); |
| 6813 | ggml_set_name(out, "out"); |
| 6814 | |
| 6815 | return out; |
| 6816 | } |
| 6817 | }; |
| 6818 | |
| 6819 | // GGML_OP_IM2COL_3D |
| 6820 | struct test_im2col_3d : public test_case { |
| 6821 | const ggml_type type_input; |
| 6822 | const ggml_type type_kernel; |
| 6823 | const ggml_type dst_type; |
| 6824 | const std::array<int64_t, 4> ne_input; |
| 6825 | const std::array<int64_t, 4> ne_kernel; |
| 6826 | // stride |
| 6827 | const int s0; |
| 6828 | const int s1; |
| 6829 | const int s2; |
| 6830 | // padding |
| 6831 | const int p0; |
| 6832 | const int p1; |
| 6833 | const int p2; |
| 6834 | // dilation |
| 6835 | const int d0; |
| 6836 | const int d1; |
| 6837 | const int d2; |
| 6838 | |
| 6839 | const int64_t IC; |
| 6840 | const bool v; |
| 6841 | |
| 6842 | std::string vars() override { |
| 6843 | return VARS_TO_STR16(type_input, type_kernel, dst_type, ne_input, ne_kernel, IC, s0, s1, s2, p0, p1, p2, d0, d1, d2, v); |
| 6844 | } |
| 6845 | |
| 6846 | test_im2col_3d(ggml_type type_input = GGML_TYPE_F32, ggml_type type_kernel = GGML_TYPE_F16, ggml_type dst_type = GGML_TYPE_F32, |
| 6847 | std::array<int64_t, 4> ne_input = {10, 10, 10, 9}, // [OC*IC, KD, KH, KW] |
| 6848 | std::array<int64_t, 4> ne_kernel = {3, 3, 3, 1}, // [N*IC, ID, IH, IW] |
| 6849 | int64_t IC = 3, |
| 6850 | int s0 = 1, int s1 = 1, int s2 = 1, |
| 6851 | int p0 = 1, int p1 = 1, int p2 = 1, |
| 6852 | int d0 = 1, int d1 = 1, int d2 = 1, |
| 6853 | bool v = false) |
| 6854 | : type_input(type_input), type_kernel(type_kernel), dst_type(dst_type), ne_input(ne_input), ne_kernel(ne_kernel), s0(s0), s1(s1), s2(s2), p0(p0), p1(p1), p2(p2), d0(d0), d1(d1), d2(d2), IC(IC), v(v) {} |
| 6855 | |
| 6856 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6857 | ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data()); |
| 6858 | ggml_set_param(input); |
| 6859 | ggml_set_name(input, "input"); |
| 6860 | |
| 6861 | if (v) { |
| 6862 | input = ggml_view_4d(ctx, input, ne_input[0] - 2, ne_input[1] - 2, ne_input[2] - 2, ne_input[3] - 2, input->nb[1], input->nb[2], input->nb[3], 0); |
| 6863 | ggml_set_name(input, "view_of_input"); |
| 6864 | } |
| 6865 | |
| 6866 | ggml_tensor * kernel = ggml_new_tensor(ctx, type_kernel, 4, ne_kernel.data()); |
| 6867 | ggml_set_name(kernel, "kernel"); |
| 6868 | |
| 6869 | ggml_tensor * out = ggml_im2col_3d(ctx, kernel, input, IC, s0, s1, s2, p0, p1, p2, d0, d1, d2, dst_type); |
| 6870 | ggml_set_name(out, "out"); |
| 6871 | |
| 6872 | return out; |
| 6873 | } |
| 6874 | }; |
| 6875 | |
| 6876 | // CONV_2D |
| 6877 | struct test_conv_2d : public test_case { |
| 6878 | const std::array<int64_t, 4> ne_input; |
| 6879 | const std::array<int64_t, 4> ne_kernel; |
| 6880 | const ggml_type type_kernel; |
| 6881 | const int stride0; |
| 6882 | const int stride1; |
| 6883 | const int padding0; |
| 6884 | const int padding1; |
| 6885 | const int dilation0; |
| 6886 | const int dilation1; |
| 6887 | // Whether the inputs are contiguous in the channel dim or the width dim |
| 6888 | const bool cwhn; |
| 6889 | const int kernel_offset; |
| 6890 | |
| 6891 | std::string vars() override { |
| 6892 | return VARS_TO_STR11(ne_input, ne_kernel, type_kernel, stride0, stride1, padding0, padding1, dilation0, dilation1, cwhn, kernel_offset); |
| 6893 | } |
| 6894 | |
| 6895 | double max_nmse_err() override { |
| 6896 | return 5e-4; |
| 6897 | } |
| 6898 | |
| 6899 | uint64_t op_flops(ggml_tensor * t) override { |
| 6900 | GGML_UNUSED(t); |
| 6901 | // Just counting matmul costs: |
| 6902 | // KxCRS @ CRSxNPQ = KxNPQ --> KxNPQx(CRS+CRS-1) flops |
| 6903 | |
| 6904 | // Copied from ggml.c: int64_t ggml_calc_conv_output_size(int64_t ins, int64_t ks, int s, int p, int d) |
| 6905 | auto calc_conv_output_size = [](int64_t ins, int64_t ks, int s, int p, int d) -> int64_t { |
| 6906 | return (ins + 2 * p - d * (ks - 1) - 1) / s + 1; |
| 6907 | }; |
| 6908 | |
| 6909 | int64_t W = ne_input[0]; |
| 6910 | int64_t H = ne_input[1]; |
| 6911 | int64_t KW = ne_kernel[0]; |
| 6912 | int64_t KH = ne_kernel[1]; |
| 6913 | int64_t Cin = ne_kernel[2]; |
| 6914 | int64_t Cout = ne_kernel[3]; |
| 6915 | int64_t N = ne_input[3]; |
| 6916 | int64_t OH = calc_conv_output_size(H, KH, stride0, padding0, dilation0); |
| 6917 | int64_t OW = calc_conv_output_size(W, KW, stride0, padding0, dilation0); |
| 6918 | |
| 6919 | int64_t K = Cout; |
| 6920 | int64_t CRS = Cin * KH * KW; |
| 6921 | int64_t NPQ = N * OH * OW; |
| 6922 | |
| 6923 | return K * NPQ * (2 * CRS - 1); |
| 6924 | } |
| 6925 | |
| 6926 | test_conv_2d(std::array<int64_t, 4> ne_input = { 64, 64, 16, 1 }, |
| 6927 | std::array<int64_t, 4> ne_kernel = { 3, 3, 1, 16 }, ggml_type type_kernel = GGML_TYPE_F32, int stride0 = 1, |
| 6928 | int stride1 = 1, int padding0 = 0, int padding1 = 0, int dilation0 = 1, int dilation1 = 1, bool cwhn = false, |
| 6929 | int kernel_offset = 0) : |
| 6930 | ne_input(ne_input), |
| 6931 | ne_kernel(ne_kernel), |
| 6932 | type_kernel(type_kernel), |
| 6933 | stride0(stride0), |
| 6934 | stride1(stride1), |
| 6935 | padding0(padding0), |
| 6936 | padding1(padding1), |
| 6937 | dilation0(dilation0), |
| 6938 | dilation1(dilation1), |
| 6939 | cwhn(cwhn), |
| 6940 | kernel_offset(kernel_offset) {} |
| 6941 | |
| 6942 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 6943 | ggml_tensor * input = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_input.data()); |
| 6944 | ggml_set_name(input, "input"); |
| 6945 | |
| 6946 | ggml_tensor * kernel; |
| 6947 | if (kernel_offset == 0) { |
| 6948 | kernel = ggml_new_tensor(ctx, type_kernel, 4, ne_kernel.data()); |
| 6949 | } else { |
| 6950 | const int64_t nelem = ne_kernel[0] * ne_kernel[1] * ne_kernel[2] * ne_kernel[3]; |
| 6951 | ggml_tensor * storage = ggml_new_tensor_1d(ctx, type_kernel, nelem + kernel_offset); |
| 6952 | const size_t element_size = ggml_type_size(type_kernel); |
| 6953 | kernel = ggml_view_4d(ctx, storage, ne_kernel[0], ne_kernel[1], ne_kernel[2], ne_kernel[3], |
| 6954 | ne_kernel[0] * element_size, ne_kernel[0] * ne_kernel[1] * element_size, |
| 6955 | ne_kernel[0] * ne_kernel[1] * ne_kernel[2] * element_size, |
| 6956 | kernel_offset * element_size); |
| 6957 | } |
| 6958 | ggml_set_name(kernel, "kernel"); |
| 6959 | |
| 6960 | if (cwhn) { |
| 6961 | // change memory layout to channel-most-contiguous (CWHN), |
| 6962 | // then permute it back so NE matches the original input |
| 6963 | input = ggml_cont(ctx, ggml_permute(ctx, input, 1, 2, 0, 3)); |
| 6964 | input = ggml_permute(ctx, input, 2, 0, 1, 3); |
| 6965 | kernel = ggml_cont(ctx, ggml_permute(ctx, kernel, 2, 3, 1, 0)); |
| 6966 | kernel = ggml_permute(ctx, kernel, 3, 2, 0, 1); |
| 6967 | } |
| 6968 | |
| 6969 | ggml_tensor * out = |
| 6970 | ggml_conv_2d_direct(ctx, kernel, input, stride0, stride1, padding0, padding1, dilation0, dilation1); |
| 6971 | ggml_set_name(out, "out"); |
| 6972 | return out; |
| 6973 | } |
| 6974 | }; |
| 6975 | |
| 6976 | // GGML_OP_IM2COL + GGML_OP_MUL_MAT |
| 6977 | // the im2col of a BF16 kernel is F32, which puts F32 in src0 and BF16 in src1 of the mul_mat |
| 6978 | struct test_conv_1d_dw : public test_case { |
| 6979 | const std::array<int64_t, 4> ne_input; // T, C |
| 6980 | const std::array<int64_t, 4> ne_kernel; // K, 1, C |
| 6981 | const ggml_type type_kernel; |
| 6982 | const int stride; |
| 6983 | const int padding; |
| 6984 | const int dilation; |
| 6985 | |
| 6986 | std::string op_desc(ggml_tensor * t) override { |
| 6987 | GGML_UNUSED(t); |
| 6988 | return "CONV_1D_DW"; |
| 6989 | } |
| 6990 | |
| 6991 | std::string vars() override { |
| 6992 | return VARS_TO_STR6(ne_input, ne_kernel, type_kernel, stride, padding, dilation); |
| 6993 | } |
| 6994 | |
| 6995 | double max_nmse_err() override { |
| 6996 | return 5e-4; |
| 6997 | } |
| 6998 | |
| 6999 | test_conv_1d_dw( |
| 7000 | std::array<int64_t, 4> ne_input = {64, 16, 1, 1}, |
| 7001 | std::array<int64_t, 4> ne_kernel = {3, 1, 16, 1}, |
| 7002 | ggml_type type_kernel = GGML_TYPE_F32, |
| 7003 | int stride = 1, int padding = 0, int dilation = 1) |
| 7004 | : ne_input(ne_input), ne_kernel(ne_kernel), type_kernel(type_kernel), stride(stride), padding(padding), dilation(dilation) {} |
| 7005 | |
| 7006 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 7007 | ggml_tensor * input = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_input.data()); |
| 7008 | ggml_set_name(input, "input"); |
| 7009 | |
| 7010 | ggml_tensor * kernel = ggml_new_tensor(ctx, type_kernel, 4, ne_kernel.data()); |
| 7011 | ggml_set_name(kernel, "kernel"); |
| 7012 | |
| 7013 | ggml_tensor * out = ggml_conv_1d_dw(ctx, kernel, input, stride, padding, dilation); |
| 7014 | ggml_set_name(out, "out"); |
| 7015 | return out; |
| 7016 | } |
| 7017 | }; |
| 7018 | |
| 7019 | // GGML_OP_CONV_2D_DW |
| 7020 | struct test_conv_2d_dw : public test_case { |
| 7021 | const std::array<int64_t, 4> ne_input; |
| 7022 | const std::array<int64_t, 4> ne_kernel; |
| 7023 | const ggml_type type_kernel; |
| 7024 | const int stride; |
| 7025 | const int padding; |
| 7026 | const int dilation; |
| 7027 | const bool cwhn; |
| 7028 | |
| 7029 | std::string vars() override { |
| 7030 | return VARS_TO_STR7(ne_input, ne_kernel, type_kernel, stride, padding, dilation, cwhn); |
| 7031 | } |
| 7032 | |
| 7033 | test_conv_2d_dw( |
| 7034 | std::array<int64_t, 4> ne_input = {64, 64, 16, 1}, |
| 7035 | std::array<int64_t, 4> ne_kernel = {3, 3, 1, 16}, |
| 7036 | ggml_type type_kernel = GGML_TYPE_F32, |
| 7037 | int stride = 1, int padding = 0, int dilation = 1, bool cwhn = false) |
| 7038 | : ne_input(ne_input), ne_kernel(ne_kernel), type_kernel(type_kernel), stride(stride), padding(padding), dilation(dilation), cwhn(cwhn) {} |
| 7039 | |
| 7040 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 7041 | ggml_tensor * input = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_input.data()); |
| 7042 | ggml_set_name(input, "input"); |
| 7043 | |
| 7044 | ggml_tensor * kernel = ggml_new_tensor(ctx, type_kernel, 4, ne_kernel.data()); |
| 7045 | ggml_set_name(kernel, "kernel"); |
| 7046 | |
| 7047 | if (cwhn) { |
| 7048 | // change memory layout to channel-most-contiguous (CWHN), |
| 7049 | // then permute it back so NE matches the original input |
| 7050 | input = ggml_cont(ctx, ggml_permute(ctx, input, 1, 2, 0, 3)); |
| 7051 | input = ggml_permute(ctx, input, 2, 0, 1, 3); |
| 7052 | kernel = ggml_cont(ctx, ggml_permute(ctx, kernel, 2, 3, 1, 0)); |
| 7053 | kernel = ggml_permute(ctx, kernel, 3, 2, 0, 1); |
| 7054 | } |
| 7055 | |
| 7056 | ggml_tensor * out = ggml_conv_2d_dw_direct( |
| 7057 | ctx, kernel, input, |
| 7058 | stride, stride, padding, padding, dilation, dilation); |
| 7059 | ggml_set_name(out, "out"); |
| 7060 | return out; |
| 7061 | } |
| 7062 | }; |
| 7063 | |
| 7064 | // GGML_OP_CONV_3D |
| 7065 | struct test_conv_3d : public test_case { |
| 7066 | // Logical 5D dimensions |
| 7067 | const int64_t N, IC, ID, IH, IW; |
| 7068 | const int64_t OC, KD, KH, KW; |
| 7069 | // Conv params |
| 7070 | const int s0, s1, s2; |
| 7071 | const int p0, p1, p2; |
| 7072 | const int d0, d1, d2; |
| 7073 | // Types |
| 7074 | const ggml_type type_kernel; |
| 7075 | const int kernel_offset; |
| 7076 | |
| 7077 | std::string op_desc(ggml_tensor * t) override { |
| 7078 | GGML_UNUSED(t); |
| 7079 | return "CONV_3D"; |
| 7080 | } |
| 7081 | |
| 7082 | std::string vars() override { |
| 7083 | return VARS_TO_STR11(N, IC, ID, IH, IW, OC, KD, KH, KW, s0, s1) + "," + |
| 7084 | VARS_TO_STR9(s2, p0, p1, p2, d0, d1, d2, type_kernel, kernel_offset); |
| 7085 | } |
| 7086 | |
| 7087 | double max_nmse_err() override { |
| 7088 | return 5e-4; |
| 7089 | } |
| 7090 | |
| 7091 | uint64_t op_flops(ggml_tensor * t) override { |
| 7092 | GGML_UNUSED(t); |
| 7093 | auto calc_conv_output_size = [](int64_t ins, int64_t ks, int s, int p, int d) -> int64_t { |
| 7094 | return (ins + 2 * p - d * (ks - 1) - 1) / s + 1; |
| 7095 | }; |
| 7096 | const int64_t OD = calc_conv_output_size(ID, KD, s2, p2, d2); |
| 7097 | const int64_t OH = calc_conv_output_size(IH, KH, s1, p1, d1); |
| 7098 | const int64_t OW = calc_conv_output_size(IW, KW, s0, p0, d0); |
| 7099 | |
| 7100 | return (uint64_t)N * OC * OD * OH * OW * std::max<int64_t>(0, 2 * IC * KD * KH * KW - 1); |
| 7101 | } |
| 7102 | |
| 7103 | test_conv_3d( |
| 7104 | int64_t N, int64_t IC, int64_t ID, int64_t IH, int64_t IW, |
| 7105 | int64_t OC, int64_t KD, int64_t KH, int64_t KW, |
| 7106 | int s0, int s1, int s2, |
| 7107 | int p0, int p1, int p2, |
| 7108 | int d0, int d1, int d2, |
| 7109 | ggml_type type_kernel, int kernel_offset = 0 |
| 7110 | ) : N(N), IC(IC), ID(ID), IH(IH), IW(IW), |
| 7111 | OC(OC), KD(KD), KH(KH), KW(KW), |
| 7112 | s0(s0), s1(s1), s2(s2), |
| 7113 | p0(p0), p1(p1), p2(p2), |
| 7114 | d0(d0), d1(d1), d2(d2), |
| 7115 | type_kernel(type_kernel), kernel_offset(kernel_offset) {} |
| 7116 | |
| 7117 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 7118 | // GGML input tensor is packed as [W, H, D, C*N] |
| 7119 | const int64_t ne_input[] = {IW, IH, ID, IC * N}; |
| 7120 | ggml_tensor * input = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_input); |
| 7121 | ggml_set_name(input, "input"); |
| 7122 | |
| 7123 | // GGML kernel tensor is packed as [KW, KH, KD, IC*OC] |
| 7124 | const int64_t ne_kernel[] = {KW, KH, KD, IC * OC}; |
| 7125 | ggml_tensor * kernel; |
| 7126 | if (kernel_offset == 0) { |
| 7127 | kernel = ggml_new_tensor(ctx, type_kernel, 4, ne_kernel); |
| 7128 | } else { |
| 7129 | ggml_tensor * storage = ggml_new_tensor_1d(ctx, type_kernel, KW * KH * KD * IC * OC + kernel_offset); |
| 7130 | const size_t element_size = ggml_type_size(type_kernel); |
| 7131 | kernel = ggml_view_4d(ctx, storage, KW, KH, KD, IC * OC, |
| 7132 | KW * element_size, KW * KH * element_size, KW * KH * KD * element_size, |
| 7133 | kernel_offset * element_size); |
| 7134 | } |
| 7135 | ggml_set_name(kernel, "kernel"); |
| 7136 | |
| 7137 | ggml_tensor * out = ggml_conv_3d_direct(ctx, kernel, input, s0, s1, s2, p0, p1, p2, d0, d1, d2, (int)IC, (int)N, (int)OC); |
| 7138 | ggml_set_name(out, "out"); |
| 7139 | return out; |
| 7140 | } |
| 7141 | }; |
| 7142 | |
| 7143 | // GGML_OP_CONCAT |
| 7144 | struct test_concat : public test_case { |
| 7145 | const ggml_type type; |
| 7146 | const std::array<int64_t, 4> ne_a; |
| 7147 | const int64_t ne_b_d; |
| 7148 | const int dim; |
| 7149 | const int v; // view (1 << 0: non-cont a (first 3 dim), 1 << 1: non-cont b (first 3 dim), 1 << 2: non-cont a (last 2 dim), 1 << 3: non-cont b (last 2 dim), 1 << 4: transposed b) |
| 7150 | |
| 7151 | std::string vars() override { |
| 7152 | return VARS_TO_STR5(type, ne_a, ne_b_d, dim, v); |
| 7153 | } |
| 7154 | |
| 7155 | test_concat(ggml_type type = GGML_TYPE_F32, |
| 7156 | std::array<int64_t, 4> ne_a = {10, 5, 5, 5}, |
| 7157 | int64_t ne_b_d = 5, |
| 7158 | int dim = 2, int v = 0) |
| 7159 | : type(type), ne_a(ne_a), ne_b_d(ne_b_d), dim(dim), v(v) {} |
| 7160 | |
| 7161 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 7162 | auto ne_b = ne_a; |
| 7163 | ne_b[dim] = ne_b_d; |
| 7164 | ggml_tensor * a; |
| 7165 | if (v & 1) { |
| 7166 | auto ne = ne_a; ne[0] *= 2; ne[1] *= 4; ne[2] *= 3; |
| 7167 | a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 7168 | ggml_set_name(a, "a"); |
| 7169 | |
| 7170 | a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0); |
| 7171 | ggml_set_name(a, "view_of_a"); |
| 7172 | } else if (v & 4) { |
| 7173 | auto ne = ne_a; ne[2] *= 2; ne[3] *= 4; |
| 7174 | a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 7175 | ggml_set_name(a, "a"); |
| 7176 | |
| 7177 | a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0); |
| 7178 | ggml_set_name(a, "view_of_a"); |
| 7179 | } else { |
| 7180 | a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 7181 | ggml_set_name(a, "a"); |
| 7182 | } |
| 7183 | ggml_tensor * b; |
| 7184 | if (v & 2) { |
| 7185 | auto ne = ne_b; ne[0] *= 3; ne[1] *= 2; ne[2] *= 4; |
| 7186 | b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 7187 | ggml_set_name(b, "b"); |
| 7188 | |
| 7189 | b = ggml_view_4d(ctx, b, ne_b[0], ne_b[1], ne_b[2], ne_b[3], b->nb[1], b->nb[2], b->nb[3], 0); |
| 7190 | ggml_set_name(b, "view_of_b"); |
| 7191 | } else if (v & 8) { |
| 7192 | auto ne = ne_b; ne[2] *= 3; ne[3] *= 2; |
| 7193 | b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 7194 | ggml_set_name(b, "b"); |
| 7195 | |
| 7196 | b = ggml_view_4d(ctx, b, ne_b[0], ne_b[1], ne_b[2], ne_b[3], b->nb[1], b->nb[2], b->nb[3], 0); |
| 7197 | ggml_set_name(b, "view_of_b"); |
| 7198 | } else if (v & 16) { |
| 7199 | auto ne = ne_b; std::swap(ne[0], ne[1]); |
| 7200 | b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 7201 | ggml_set_name(b, "b"); |
| 7202 | |
| 7203 | b = ggml_transpose(ctx, b); |
| 7204 | ggml_set_name(b, "transpose_of_b"); |
| 7205 | } else { |
| 7206 | b = ggml_new_tensor(ctx, type, 4, ne_b.data()); |
| 7207 | ggml_set_name(b, "b"); |
| 7208 | } |
| 7209 | |
| 7210 | ggml_tensor * out = ggml_concat(ctx, a, b, dim); |
| 7211 | ggml_set_name(out, "out"); |
| 7212 | |
| 7213 | return out; |
| 7214 | } |
| 7215 | }; |
| 7216 | |
| 7217 | // GGML_OP_ARGSORT |
| 7218 | struct test_argsort : public test_case { |
| 7219 | const ggml_type type; |
| 7220 | const std::array<int64_t, 4> ne; |
| 7221 | ggml_sort_order order; |
| 7222 | |
| 7223 | std::string vars() override { |
| 7224 | return VARS_TO_STR3(type, ne, order); |
| 7225 | } |
| 7226 | |
| 7227 | test_argsort(ggml_type type = GGML_TYPE_F32, |
| 7228 | std::array<int64_t, 4> ne = {16, 10, 10, 10}, |
| 7229 | ggml_sort_order order = GGML_SORT_ORDER_ASC) |
| 7230 | : type(type), ne(ne), order(order) {} |
| 7231 | |
| 7232 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 7233 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 7234 | ggml_set_name(a, "a"); |
| 7235 | |
| 7236 | ggml_tensor * out = ggml_argsort(ctx, a, order); |
| 7237 | ggml_set_name(out, "out"); |
| 7238 | |
| 7239 | return out; |
| 7240 | } |
| 7241 | |
| 7242 | void initialize_tensors(ggml_context * ctx) override { |
| 7243 | std::random_device rd; |
| 7244 | std::default_random_engine rng(rd()); |
| 7245 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 7246 | if (t->type == GGML_TYPE_I32) { |
| 7247 | // indices |
| 7248 | std::vector<int> data(ggml_nelements(t)); |
| 7249 | for (int i = 0; i < ggml_nelements(t); i++) { |
| 7250 | data[i] = rand(); |
| 7251 | } |
| 7252 | std::shuffle(data.begin(), data.end(), rng); |
| 7253 | ggml_backend_tensor_set(t, data.data(), 0, ne[0]*ne[1]*ne[2]*ne[3] * sizeof(int)); |
| 7254 | } else if (t->type == GGML_TYPE_F32) { |
| 7255 | // initialize with unique values to avoid ties |
| 7256 | for (int64_t r = 0; r < ggml_nrows(t); r++) { |
| 7257 | std::vector<float> data(t->ne[0]); |
| 7258 | for (int i = 0; i < t->ne[0]; i++) { |
| 7259 | data[i] = i; |
| 7260 | } |
| 7261 | std::shuffle(data.begin(), data.end(), rng); |
| 7262 | ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(float)); |
| 7263 | } |
| 7264 | } else { |
| 7265 | GGML_ABORT("fatal error"); |
| 7266 | } |
| 7267 | } |
| 7268 | } |
| 7269 | }; |
| 7270 | |
| 7271 | // GGML_OP_TOP_K |
| 7272 | struct test_top_k : public test_case { |
| 7273 | const ggml_type type; |
| 7274 | const std::array<int64_t, 4> ne; |
| 7275 | const int k; |
| 7276 | const bool ties; |
| 7277 | ggml_tensor * input {}; |
| 7278 | |
| 7279 | std::string vars() override { |
| 7280 | return VARS_TO_STR4(type, ne, k, ties); |
| 7281 | } |
| 7282 | |
| 7283 | test_top_k(ggml_type type = GGML_TYPE_F32, |
| 7284 | std::array<int64_t, 4> ne = {16, 10, 10, 10}, |
| 7285 | int k = 4, bool ties = false) |
| 7286 | : type(type), ne(ne), k(k), ties(ties) {} |
| 7287 | |
| 7288 | double max_err() override { |
| 7289 | return 0.0; |
| 7290 | } |
| 7291 | |
| 7292 | // When there are ties, only validate the final result. |
| 7293 | // The logic in err can't handle the sentinel tensors. |
| 7294 | bool run_whole_graph() override { return ties; } |
| 7295 | |
| 7296 | double err(const float * a, const float * b, size_t n) override { |
| 7297 | // When there are no ties, we expect the exact same set of indices, |
| 7298 | // but possibly in a different order. When there are ties, the indices |
| 7299 | // can be different but the input values they correspond to should be |
| 7300 | // the same. The logic for ties could work for non-ties, but only for |
| 7301 | // the output tensor, not for the sentinel tensors. |
| 7302 | if (ties) { |
| 7303 | std::vector<float> src(ggml_nelements(input)); |
| 7304 | |
| 7305 | ggml_backend_tensor_get(input, src.data(), 0, ggml_nelements(input) * ggml_type_size(type)); |
| 7306 | |
| 7307 | double diff = 0.0f; |
| 7308 | |
| 7309 | GGML_ASSERT(n == (size_t)(ggml_nrows(input) * k)); |
| 7310 | int64_t cols = input->ne[0]; |
| 7311 | std::vector<int32_t> ia(k); |
| 7312 | std::vector<int32_t> ib(k); |
| 7313 | std::vector<float> asrc(k); |
| 7314 | std::vector<float> bsrc(k); |
| 7315 | for (int64_t r = 0; r < ggml_nrows(input); r++) { |
| 7316 | // Convert indices for the row back to integer |
| 7317 | for (int64_t c = 0; c < k; c++) { |
| 7318 | ia[c] = (int32_t)a[r * k + c]; |
| 7319 | ib[c] = (int32_t)b[r * k + c]; |
| 7320 | } |
| 7321 | // The src values for each row should match. |
| 7322 | for (int64_t c = 0; c < k; c++) { |
| 7323 | asrc[c] = src[r * cols + ia[c]]; |
| 7324 | bsrc[c] = src[r * cols + ib[c]]; |
| 7325 | } |
| 7326 | diff += jdst(asrc.data(), bsrc.data(), k); |
| 7327 | // There should be no duplicate indices |
| 7328 | std::sort(ia.begin(), ia.end()); |
| 7329 | std::sort(ib.begin(), ib.end()); |
| 7330 | if (std::adjacent_find(ia.begin(), ia.end()) != ia.end()) { |
| 7331 | diff += 1; |
| 7332 | } |
| 7333 | if (std::adjacent_find(ib.begin(), ib.end()) != ib.end()) { |
| 7334 | diff += 1; |
| 7335 | } |
| 7336 | } |
| 7337 | return diff; |
| 7338 | } else { |
| 7339 | std::vector<int32_t> ia(n); |
| 7340 | std::vector<int32_t> ib(n); |
| 7341 | |
| 7342 | double diff = 0.0f; |
| 7343 | |
| 7344 | for (size_t i = 0; i < n; i++) { |
| 7345 | ia[i] = (int32_t) a[i]; |
| 7346 | ib[i] = (int32_t) b[i]; |
| 7347 | |
| 7348 | // penalize the result if the data is not integer valued |
| 7349 | diff += std::fabs(a[i] - ia[i]); |
| 7350 | diff += std::fabs(b[i] - ib[i]); |
| 7351 | } |
| 7352 | |
| 7353 | return diff + jdst(ia.data(), ib.data(), n); |
| 7354 | } |
| 7355 | } |
| 7356 | |
| 7357 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 7358 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 7359 | ggml_set_name(a, "a"); |
| 7360 | |
| 7361 | // Save 'a' for err() |
| 7362 | input = a; |
| 7363 | |
| 7364 | ggml_tensor * out = ggml_top_k(ctx, a, k); |
| 7365 | ggml_set_name(out, "out"); |
| 7366 | |
| 7367 | return out; |
| 7368 | } |
| 7369 | |
| 7370 | void initialize_tensors(ggml_context * ctx) override { |
| 7371 | std::random_device rd; |
| 7372 | std::default_random_engine rng(rd()); |
| 7373 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 7374 | int tie_denom = std::max(1, std::min(10, k / 2)); |
| 7375 | for (int64_t r = 0; r < ggml_nrows(t); r++) { |
| 7376 | std::vector<float> data(t->ne[0]); |
| 7377 | for (int i = 0; i < t->ne[0]; i++) { |
| 7378 | if (ties) { |
| 7379 | // integer division to introduce duplicates |
| 7380 | data[i] = i / tie_denom; |
| 7381 | } else { |
| 7382 | data[i] = i; |
| 7383 | } |
| 7384 | } |
| 7385 | std::shuffle(data.begin(), data.end(), rng); |
| 7386 | ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(float)); |
| 7387 | } |
| 7388 | } |
| 7389 | } |
| 7390 | }; |
| 7391 | |
| 7392 | // top_k over rows like log-probabilities: distinct negative values, fewer |
| 7393 | // than k +inf (none for k = 1, so the expected indices are unique) and many |
| 7394 | // -inf (masked tokens) |
| 7395 | struct test_top_k_inf : public test_top_k { |
| 7396 | test_top_k_inf(std::array<int64_t, 4> ne, int k) |
| 7397 | : test_top_k(GGML_TYPE_F32, ne, k, false) {} |
| 7398 | |
| 7399 | std::string vars() override { |
| 7400 | return test_top_k::vars() + ",inf=1"; |
| 7401 | } |
| 7402 | |
| 7403 | // compare only the output: the input holds infinities, which err() would |
| 7404 | // read as indices |
| 7405 | bool run_whole_graph() override { return true; } |
| 7406 | |
| 7407 | void initialize_tensors(ggml_context * ctx) override { |
| 7408 | std::random_device rd; |
| 7409 | std::default_random_engine rng(rd()); |
| 7410 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 7411 | for (int64_t r = 0; r < ggml_nrows(t); r++) { |
| 7412 | std::vector<float> data(t->ne[0]); |
| 7413 | for (int i = 0; i < t->ne[0]; i++) { |
| 7414 | data[i] = -1.0f - i; |
| 7415 | } |
| 7416 | std::shuffle(data.begin(), data.end(), rng); |
| 7417 | const int n_pinf = k / 2; |
| 7418 | for (int i = 0; i < t->ne[0]; i++) { |
| 7419 | if (i < n_pinf) { |
| 7420 | data[i] = INFINITY; |
| 7421 | } else if (i % 3 == 0) { |
| 7422 | data[i] = -INFINITY; |
| 7423 | } |
| 7424 | } |
| 7425 | std::shuffle(data.begin(), data.end(), rng); |
| 7426 | ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(float)); |
| 7427 | } |
| 7428 | } |
| 7429 | } |
| 7430 | }; |
| 7431 | |
| 7432 | // qwen4exp QSA indexer top-k fusion: expand per-block scores to cells, add the f16 mask, top-k. |
| 7433 | struct test_topk_qsa : public test_case { |
| 7434 | const int64_t n_blocks; |
| 7435 | const int64_t n_kv; |
| 7436 | const int64_t n_tps; |
| 7437 | const int64_t n_stream; |
| 7438 | const int width; |
| 7439 | ggml_tensor * out {}; |
| 7440 | |
| 7441 | std::string op_desc(ggml_tensor * t) override { |
| 7442 | GGML_UNUSED(t); |
| 7443 | return "TOPK_QSA"; |
| 7444 | } |
| 7445 | |
| 7446 | std::string vars() override { |
| 7447 | return VARS_TO_STR5(n_blocks, n_kv, n_tps, n_stream, width); |
| 7448 | } |
| 7449 | |
| 7450 | test_topk_qsa(int64_t n_blocks = 512, int64_t n_kv = 2048, int64_t n_tps = 2, int64_t n_stream = 1, int width = 1500) |
| 7451 | : n_blocks(n_blocks), n_kv(n_kv), n_tps(n_tps), n_stream(n_stream), width(width) {} |
| 7452 | |
| 7453 | double max_err() override { return 0.0; } |
| 7454 | bool run_whole_graph() override { return true; } |
| 7455 | |
| 7456 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 7457 | ggml_tensor * score = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_blocks, n_tps, n_stream); |
| 7458 | ggml_set_name(score, "score"); |
| 7459 | ggml_tensor * cell_blk = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_kv, n_stream); |
| 7460 | ggml_set_name(cell_blk, "cell_blk"); |
| 7461 | ggml_tensor * kq_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, n_kv, n_tps, n_stream); |
| 7462 | ggml_set_name(kq_mask, "kq_mask"); |
| 7463 | |
| 7464 | ggml_tensor * a = ggml_cont(ctx, ggml_permute(ctx, score, 1, 0, 2, 3)); |
| 7465 | ggml_tensor * e = ggml_get_rows(ctx, a, cell_blk); |
| 7466 | e = ggml_cont(ctx, ggml_permute(ctx, e, 1, 0, 2, 3)); |
| 7467 | ggml_tensor * m = ggml_cast(ctx, kq_mask, GGML_TYPE_F32); |
| 7468 | e = ggml_add(ctx, e, ggml_reshape_3d(ctx, m, n_kv, n_tps, n_stream)); |
| 7469 | out = ggml_top_k(ctx, e, width); |
| 7470 | ggml_set_name(out, "out"); |
| 7471 | return out; |
| 7472 | } |
| 7473 | |
| 7474 | std::vector<ggml_tensor *> fusion_test_nodes() override { return { out }; } |
| 7475 | |
| 7476 | // distinct mask ramp + small scores keep every cell value unique, so no top-k ties |
| 7477 | void initialize_tensors(ggml_context * ctx) override { |
| 7478 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 7479 | if (t->op != GGML_OP_NONE) { |
| 7480 | continue; |
| 7481 | } |
| 7482 | if (t->type == GGML_TYPE_I32) { |
| 7483 | std::vector<int32_t> data(ggml_nelements(t)); |
| 7484 | for (auto & v : data) { v = rand() % n_blocks; } |
| 7485 | ggml_backend_tensor_set(t, data.data(), 0, data.size() * sizeof(int32_t)); |
| 7486 | } else if (t->type == GGML_TYPE_F16) { |
| 7487 | std::vector<ggml_fp16_t> data(ggml_nelements(t)); |
| 7488 | for (int64_t r = 0; r < ggml_nrows(t); r++) { |
| 7489 | for (int64_t i = 0; i < n_kv; i++) { |
| 7490 | data[r * n_kv + i] = ggml_fp32_to_fp16((float) i); |
| 7491 | } |
| 7492 | } |
| 7493 | ggml_backend_tensor_set(t, data.data(), 0, data.size() * sizeof(ggml_fp16_t)); |
| 7494 | } else { |
| 7495 | init_tensor_uniform(t, 0.0f, 0.5f); |
| 7496 | } |
| 7497 | } |
| 7498 | } |
| 7499 | |
| 7500 | // top-k output order is unspecified; compare as a set of indices |
| 7501 | double err(const float * a, const float * b, size_t n) override { |
| 7502 | std::vector<int32_t> ia(n), ib(n); |
| 7503 | double diff = 0.0; |
| 7504 | for (size_t i = 0; i < n; i++) { |
| 7505 | ia[i] = (int32_t) a[i]; |
| 7506 | ib[i] = (int32_t) b[i]; |
| 7507 | diff += std::fabs(a[i] - ia[i]) + std::fabs(b[i] - ib[i]); |
| 7508 | } |
| 7509 | return diff + jdst(ia.data(), ib.data(), n); |
| 7510 | } |
| 7511 | }; |
| 7512 | |
| 7513 | enum MoeGatingFunc { |
| 7514 | GATING_FUNC_SOFTMAX, |
| 7515 | GATING_FUNC_SIGMOID, |
| 7516 | GATING_FUNC_SOFTMAX_WEIGHT, |
| 7517 | GATING_FUNC_SQRT_SOFTPLUS, |
| 7518 | }; |
| 7519 | |
| 7520 | struct test_topk_moe : public test_case { |
| 7521 | const std::array<int64_t, 4> ne; |
| 7522 | const int n_expert_used; |
| 7523 | const bool with_norm; |
| 7524 | const bool bias_probs; |
| 7525 | const MoeGatingFunc gating_func; |
| 7526 | const float scale_w; |
| 7527 | const int n_expert_groups; |
| 7528 | ggml_tensor * weights {}; |
| 7529 | ggml_tensor * selected_experts {}; |
| 7530 | |
| 7531 | test_topk_moe(std::array<int64_t, 4> ne = { 10, 5, 1, 1 }, |
| 7532 | int n_expert_used = 1, |
| 7533 | bool with_norm = false, |
| 7534 | bool bias_probs = false, |
| 7535 | MoeGatingFunc gating_func = GATING_FUNC_SOFTMAX, |
| 7536 | float scale_w = 0.0f, |
| 7537 | int n_expert_groups = 1) : |
| 7538 | ne(ne), |
| 7539 | n_expert_used(n_expert_used), |
| 7540 | with_norm(with_norm), |
| 7541 | bias_probs(bias_probs), |
| 7542 | gating_func(gating_func), |
| 7543 | scale_w(scale_w), |
| 7544 | n_expert_groups(n_expert_groups) { |
| 7545 | GGML_ASSERT(n_expert_used <= ne[0]); |
| 7546 | GGML_ASSERT(ne[0] % n_expert_groups == 0); |
| 7547 | GGML_ASSERT(n_expert_used <= ne[0] / n_expert_groups); |
| 7548 | } |
| 7549 | |
| 7550 | std::string vars() override { return VARS_TO_STR7(ne, n_expert_used, with_norm, bias_probs, gating_func, scale_w, n_expert_groups); } |
| 7551 | |
| 7552 | std::string op_desc(ggml_tensor * t) override { |
| 7553 | GGML_UNUSED(t); |
| 7554 | return "TOPK_MOE"; |
| 7555 | } |
| 7556 | |
| 7557 | bool run_whole_graph() override { return true; } |
| 7558 | |
| 7559 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 7560 | const int n_expert = ne[0]; |
| 7561 | const int n_tokens = ne[1]; |
| 7562 | const int n_exp_per_group = n_expert / n_expert_groups; |
| 7563 | const int n_group_used = n_expert_groups / 2; |
| 7564 | |
| 7565 | ggml_tensor * logits = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne.data()); |
| 7566 | ggml_tensor * probs = |
| 7567 | (gating_func == GATING_FUNC_SOFTMAX) ? ggml_soft_max(ctx, logits) : |
| 7568 | (gating_func == GATING_FUNC_SIGMOID) ? ggml_sigmoid(ctx, logits) : |
| 7569 | (gating_func == GATING_FUNC_SQRT_SOFTPLUS) ? ggml_sqrt(ctx, ggml_softplus(ctx, logits)) : logits; |
| 7570 | ggml_set_name(probs, "probs"); |
| 7571 | |
| 7572 | ggml_tensor * selection_probs = probs; |
| 7573 | if (bias_probs) { |
| 7574 | ggml_tensor * exp_probs_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, ne[0]); |
| 7575 | ggml_set_name(exp_probs_b, "exp_probs_b"); |
| 7576 | selection_probs = ggml_add(ctx, probs, exp_probs_b); |
| 7577 | ggml_set_name(selection_probs, "selection_probs"); |
| 7578 | } |
| 7579 | |
| 7580 | if (n_expert_groups > 1) { |
| 7581 | ggml_tensor * selection_groups = ggml_reshape_3d(ctx, selection_probs, n_exp_per_group, n_expert_groups, n_tokens); // [n_exp_per_group, n_expert_groups, n_tokens] |
| 7582 | |
| 7583 | ggml_tensor * group_scores = ggml_argsort_top_k(ctx, selection_groups, 2); // [2, n_expert_groups, n_tokens] |
| 7584 | group_scores = ggml_get_rows(ctx, ggml_reshape_4d(ctx, selection_groups, 1, selection_groups->ne[0], selection_groups->ne[1], selection_groups->ne[2]), group_scores); // [1, 2, n_expert_groups, n_tokens] |
| 7585 | |
| 7586 | group_scores = ggml_sum_rows(ctx, ggml_reshape_3d(ctx, group_scores, group_scores->ne[1], group_scores->ne[2], group_scores->ne[3])); // [1, n_expert_groups, n_tokens] |
| 7587 | group_scores = ggml_reshape_2d(ctx, group_scores, group_scores->ne[1], group_scores->ne[2]); // [n_expert_groups, n_tokens] |
| 7588 | |
| 7589 | ggml_tensor * expert_groups = ggml_argsort_top_k(ctx, group_scores, n_group_used); // [n_group_used, n_tokens] |
| 7590 | ggml_set_name(expert_groups, "ffn_moe_group_topk"); |
| 7591 | |
| 7592 | selection_probs = ggml_get_rows(ctx, selection_groups, expert_groups); // [n_exp_per_group, n_group_used, n_tokens] |
| 7593 | selection_probs = ggml_set_rows(ctx, ggml_fill(ctx, selection_groups, -INFINITY), selection_probs, expert_groups); // [n_exp_per_group, n_expert_groups, n_tokens] |
| 7594 | selection_probs = ggml_reshape_2d(ctx, selection_probs, n_expert, n_tokens); // [n_expert, n_tokens] |
| 7595 | ggml_set_name(selection_probs, "ffn_moe_probs_masked"); |
| 7596 | } |
| 7597 | |
| 7598 | selected_experts = ggml_argsort_top_k(ctx, selection_probs, n_expert_used); // [n_expert_used, n_tokens] |
| 7599 | ggml_set_name(selected_experts, "selected_experts"); |
| 7600 | |
| 7601 | weights = ggml_get_rows(ctx, ggml_reshape_3d(ctx, probs, 1, n_expert, n_tokens), selected_experts); // [1, n_expert_used, n_tokens] |
| 7602 | ggml_set_name(weights, "weights"); |
| 7603 | |
| 7604 | if (gating_func == GATING_FUNC_SOFTMAX_WEIGHT) { |
| 7605 | weights = ggml_reshape_2d(ctx, weights, n_expert_used, n_tokens); |
| 7606 | weights = ggml_soft_max(ctx, weights); // [n_expert_used, n_tokens] |
| 7607 | weights = ggml_reshape_3d(ctx, weights, 1, n_expert_used, n_tokens); |
| 7608 | } |
| 7609 | |
| 7610 | if (with_norm) { |
| 7611 | weights = ggml_reshape_2d(ctx, weights, n_expert_used, n_tokens); |
| 7612 | ggml_tensor * weights_sum = ggml_sum_rows(ctx, weights); // [1, n_tokens] |
| 7613 | ggml_set_name(weights_sum, "weights_sum"); |
| 7614 | |
| 7615 | weights_sum = ggml_clamp(ctx, weights_sum, 6.103515625e-5, INFINITY); |
| 7616 | weights = ggml_div(ctx, weights, weights_sum); // [n_expert_used, n_tokens] |
| 7617 | weights = ggml_reshape_3d(ctx, weights, 1, n_expert_used, n_tokens); |
| 7618 | } |
| 7619 | |
| 7620 | if (scale_w) { |
| 7621 | weights = ggml_scale(ctx, weights, scale_w); |
| 7622 | } |
| 7623 | |
| 7624 | ggml_set_name(weights, "weights"); |
| 7625 | return weights; |
| 7626 | } |
| 7627 | // Verify two outputs |
| 7628 | std::vector<ggml_tensor *> fusion_test_nodes() override { return { selected_experts, weights }; } |
| 7629 | |
| 7630 | // allow output in arbitrary order |
| 7631 | double err(const float * a, const float * b, size_t n) override { |
| 7632 | std::vector<float> a2(n); |
| 7633 | std::vector<float> b2(n); |
| 7634 | for (size_t i = 0; i < n; ++i) { |
| 7635 | a2[i] = a[i]; |
| 7636 | b2[i] = b[i]; |
| 7637 | } |
| 7638 | std::sort(a2.begin(), a2.end()); |
| 7639 | std::sort(b2.begin(), b2.end()); |
| 7640 | return nmse(a2.data(), b2.data(), n); |
| 7641 | } |
| 7642 | }; |
| 7643 | |
| 7644 | struct test_moe_reduce : public test_case { |
| 7645 | const int64_t n_embd; |
| 7646 | const int64_t n_expert_used; |
| 7647 | const int64_t n_tokens; |
| 7648 | const bool unaligned_experts; |
| 7649 | const bool with_expert_scale; |
| 7650 | const bool interleaved_views_adds; |
| 7651 | |
| 7652 | test_moe_reduce( |
| 7653 | int64_t n_embd, int64_t n_expert_used, int64_t n_tokens, |
| 7654 | bool unaligned_experts = false, bool with_expert_scale = false, bool interleaved_views_adds = false) : |
| 7655 | n_embd(n_embd), n_expert_used(n_expert_used), n_tokens(n_tokens), |
| 7656 | unaligned_experts(unaligned_experts), with_expert_scale(with_expert_scale), |
| 7657 | interleaved_views_adds(interleaved_views_adds) {} |
| 7658 | |
| 7659 | std::string vars() override { |
| 7660 | return VARS_TO_STR6(n_embd, n_expert_used, n_tokens, unaligned_experts, with_expert_scale, interleaved_views_adds); |
| 7661 | } |
| 7662 | |
| 7663 | std::string op_desc(ggml_tensor * t) override { |
| 7664 | GGML_UNUSED(t); |
| 7665 | return "MOE_REDUCE"; |
| 7666 | } |
| 7667 | |
| 7668 | bool run_whole_graph() override { return true; } |
| 7669 | |
| 7670 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 7671 | ggml_tensor * experts; |
| 7672 | if (unaligned_experts) { |
| 7673 | ggml_tensor * storage = ggml_new_tensor_1d( |
| 7674 | ctx, GGML_TYPE_F32, n_embd * n_expert_used * n_tokens + 1); |
| 7675 | ggml_set_name(storage, "experts_storage"); |
| 7676 | experts = ggml_view_3d(ctx, storage, n_embd, n_expert_used, n_tokens, |
| 7677 | n_embd * sizeof(float), n_embd * n_expert_used * sizeof(float), sizeof(float)); |
| 7678 | } else { |
| 7679 | experts = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_embd, n_expert_used, n_tokens); |
| 7680 | } |
| 7681 | ggml_set_name(experts, "experts"); |
| 7682 | ggml_tensor * weights = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, 1, n_expert_used, n_tokens); |
| 7683 | ggml_set_name(weights, "weights"); |
| 7684 | |
| 7685 | ggml_tensor * scaled = experts; |
| 7686 | if (with_expert_scale) { |
| 7687 | ggml_tensor * expert_scale = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, 1, n_expert_used, n_tokens); |
| 7688 | ggml_set_name(expert_scale, "expert_scale"); |
| 7689 | scaled = ggml_mul(ctx, experts, expert_scale); |
| 7690 | ggml_set_name(scaled, "scaled_experts"); |
| 7691 | } |
| 7692 | |
| 7693 | ggml_tensor * weighted = ggml_mul(ctx, scaled, weights); |
| 7694 | ggml_set_name(weighted, "weighted_experts"); |
| 7695 | |
| 7696 | std::vector<ggml_tensor *> views(n_expert_used); |
| 7697 | for (int64_t expert = 0; expert < n_expert_used; ++expert) { |
| 7698 | views[expert] = ggml_view_2d( |
| 7699 | ctx, weighted, n_embd, n_tokens, weighted->nb[2], expert * weighted->nb[1]); |
| 7700 | if (!interleaved_views_adds && mode == MODE_TEST) { |
| 7701 | ggml_build_forward_expand(gf, views[expert]); |
| 7702 | } |
| 7703 | } |
| 7704 | |
| 7705 | ggml_tensor * out = views[0]; |
| 7706 | for (int64_t expert = 1; expert < n_expert_used; ++expert) { |
| 7707 | out = ggml_add(ctx, out, views[expert]); |
| 7708 | if (!interleaved_views_adds && mode == MODE_TEST) { |
| 7709 | ggml_build_forward_expand(gf, out); |
| 7710 | } |
| 7711 | } |
| 7712 | ggml_set_name(out, "moe_reduce"); |
| 7713 | return out; |
| 7714 | } |
| 7715 | }; |
| 7716 | |
| 7717 | // mul_mat with src1 in [0, 1]: a zero-mean src1 hides errors in the zero point or the min of a quantized src0 |
| 7718 | struct test_mul_mat_pos : public test_mul_mat { |
| 7719 | using test_mul_mat::test_mul_mat; |
| 7720 | |
| 7721 | std::string vars() override { |
| 7722 | return test_mul_mat::vars() + ",src1=[0,1]"; |
| 7723 | } |
| 7724 | |
| 7725 | void initialize_tensors(ggml_context * ctx) override { |
| 7726 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) { |
| 7727 | if (t->type == GGML_TYPE_F32) { |
| 7728 | init_tensor_uniform(t, 0.0f, 1.0f); |
| 7729 | } else { |
| 7730 | init_tensor_uniform(t); |
| 7731 | } |
| 7732 | } |
| 7733 | } |
| 7734 | }; |
| 7735 | |
| 7736 | enum mul_mat_add_mode { |
| 7737 | MUL_MAT_ADD_MM_RES, // mm + res |
| 7738 | MUL_MAT_ADD_RES_MM, // res + mm |
| 7739 | MUL_MAT_ADD_ROW, // mm + a one-row res, which a same-shape fusion must leave alone |
| 7740 | MUL_MAT_ADD_RES_INPLACE, // res += mm |
| 7741 | MUL_MAT_ADD_B_INPLACE, // b += mm: the sum overwrites the mat-mul input (m == k) |
| 7742 | MUL_MAT_ADD_MM_MM, // mm2 + mm: the residual is itself a mat-mul output, so both operands are MUL_MAT |
| 7743 | }; |
| 7744 | |
| 7745 | static std::string var_to_str(mul_mat_add_mode mode) { |
| 7746 | switch (mode) { |
| 7747 | case MUL_MAT_ADD_MM_RES: return "mm+res"; |
| 7748 | case MUL_MAT_ADD_RES_MM: return "res+mm"; |
| 7749 | case MUL_MAT_ADD_ROW: return "mm+row"; |
| 7750 | case MUL_MAT_ADD_RES_INPLACE: return "res+=mm"; |
| 7751 | case MUL_MAT_ADD_B_INPLACE: return "b+=mm"; |
| 7752 | case MUL_MAT_ADD_MM_MM: return "mm2+mm"; |
| 7753 | } |
| 7754 | return "unknown"; |
| 7755 | } |
| 7756 | |
| 7757 | // mul_mat followed by an add of a residual, which backends may fuse into the mat-mul |
| 7758 | struct test_mul_mat_add : public test_case { |
| 7759 | const ggml_type type_a; |
| 7760 | const int64_t m; |
| 7761 | const int64_t n; |
| 7762 | const int64_t k; |
| 7763 | const mul_mat_add_mode mode; |
| 7764 | |
| 7765 | test_mul_mat_add(ggml_type type_a, int64_t m, int64_t n, int64_t k, mul_mat_add_mode mode = MUL_MAT_ADD_MM_RES) |
| 7766 | : type_a(type_a), m(m), n(n), k(k), mode(mode) { |
| 7767 | GGML_ASSERT(mode != MUL_MAT_ADD_B_INPLACE || m == k); |
| 7768 | } |
| 7769 | |
| 7770 | std::string vars() override { |
| 7771 | return VARS_TO_STR5(type_a, m, n, k, mode); |
| 7772 | } |
| 7773 | |
| 7774 | std::string op_desc(ggml_tensor * t) override { |
| 7775 | GGML_UNUSED(t); |
| 7776 | return "MUL_MAT_ADD"; |
| 7777 | } |
| 7778 | |
| 7779 | bool run_whole_graph() override { return true; } |
| 7780 | |
| 7781 | double max_nmse_err() override { |
| 7782 | return 5e-4; |
| 7783 | } |
| 7784 | |
| 7785 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 7786 | ggml_tensor * a = ggml_new_tensor_2d(ctx, type_a, k, m); |
| 7787 | ggml_tensor * b = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, k, n); |
| 7788 | ggml_tensor * res = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, m, mode == MUL_MAT_ADD_ROW ? 1 : n); |
| 7789 | |
| 7790 | ggml_tensor * mm = ggml_mul_mat(ctx, a, b); |
| 7791 | ggml_tensor * out = nullptr; |
| 7792 | switch (mode) { |
| 7793 | case MUL_MAT_ADD_MM_RES: |
| 7794 | case MUL_MAT_ADD_ROW: out = ggml_add(ctx, mm, res); break; |
| 7795 | case MUL_MAT_ADD_RES_MM: out = ggml_add(ctx, res, mm); break; |
| 7796 | case MUL_MAT_ADD_RES_INPLACE: out = ggml_add_inplace(ctx, res, mm); break; |
| 7797 | case MUL_MAT_ADD_B_INPLACE: out = ggml_add_inplace(ctx, b, mm); break; |
| 7798 | case MUL_MAT_ADD_MM_MM: |
| 7799 | { |
| 7800 | ggml_tensor * a2 = ggml_new_tensor_2d(ctx, type_a, k, m); |
| 7801 | ggml_tensor * mm2 = ggml_mul_mat(ctx, a2, b); |
| 7802 | out = ggml_add(ctx, mm2, mm); |
| 7803 | } break; |
| 7804 | } |
| 7805 | ggml_set_name(out, "out"); |
| 7806 | |
| 7807 | return out; |
| 7808 | } |
| 7809 | }; |
| 7810 | |
| 7811 | struct test_mul_mat_vec_fusion : public test_case { |
| 7812 | const ggml_type type; |
| 7813 | const ggml_glu_op glu_op; |
| 7814 | const int64_t m; |
| 7815 | const int64_t n; |
| 7816 | const int64_t k; |
| 7817 | const bool use_id; |
| 7818 | const int n_mats; |
| 7819 | const int n_used; |
| 7820 | const bool b; // broadcast b matrix (only for use_id) |
| 7821 | const bool with_bias; |
| 7822 | const bool with_gate; |
| 7823 | const bool with_lane_scale; |
| 7824 | std::array<int64_t, 2> batch_dims; |
| 7825 | const bool skip; // set some ids to a negative value (only for use_id) |
| 7826 | |
| 7827 | test_mul_mat_vec_fusion(ggml_type type, ggml_glu_op op, int64_t m, int64_t n, int64_t k, |
| 7828 | bool use_id = false, int n_mats = 1, int n_used = 1, bool b = false, bool with_bias = false, bool with_gate = true, |
| 7829 | bool with_lane_scale = false, std::array<int64_t, 2> batch_dims = {4, 2}, bool skip = false) |
| 7830 | : type(type), glu_op(op), m(m), n(n), k(k), use_id(use_id), n_mats(n_mats), n_used(n_used), b(b), with_bias(with_bias), |
| 7831 | with_gate(with_gate), with_lane_scale(with_lane_scale), batch_dims(batch_dims), skip(skip) { |
| 7832 | if (use_id) { |
| 7833 | GGML_ASSERT(n_used <= n_mats); |
| 7834 | } |
| 7835 | } |
| 7836 | |
| 7837 | std::string vars() override { |
| 7838 | return VARS_TO_STR14(type, glu_op, m, n, k, use_id, n_mats, n_used, b, with_bias, with_gate, with_lane_scale, batch_dims, skip); |
| 7839 | } |
| 7840 | |
| 7841 | std::string op_desc(ggml_tensor * t) override { |
| 7842 | GGML_UNUSED(t); |
| 7843 | return "MUL_MAT_VEC_FUSION"; |
| 7844 | } |
| 7845 | |
| 7846 | bool run_whole_graph() override { return true; } |
| 7847 | bool use_weight_context() override { return use_id && with_lane_scale; } |
| 7848 | |
| 7849 | ggml_tensor * build_gate(ggml_context * ctx, ggml_tensor * ffn_gate, ggml_tensor * ffn_up) { |
| 7850 | ggml_tensor * out = nullptr; |
| 7851 | if (with_gate) { |
| 7852 | if (glu_op == GGML_GLU_OP_SWIGLU_OAI) { |
| 7853 | constexpr float alpha = 1.702f; |
| 7854 | constexpr float limit = 7.0f; |
| 7855 | out = ggml_swiglu_oai(ctx, ffn_gate, ffn_up, alpha, limit); |
| 7856 | } else if (glu_op == GGML_GLU_OP_SWIGLU_CLAMP) { |
| 7857 | constexpr float limit = 10.0f; |
| 7858 | out = ggml_swiglu_clamp(ctx, ffn_gate, ffn_up, limit); |
| 7859 | } else { |
| 7860 | out = ggml_glu_split(ctx, ffn_gate, ffn_up, glu_op); |
| 7861 | } |
| 7862 | } |
| 7863 | return out; |
| 7864 | } |
| 7865 | |
| 7866 | ggml_tensor * build_lane_scale_dense(ggml_context * ctx, ggml_tensor * out) { |
| 7867 | ggml_tensor * scale = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1); |
| 7868 | return ggml_mul(ctx, out, scale); |
| 7869 | } |
| 7870 | |
| 7871 | ggml_tensor * build_lane_scale_id(ggml_context * ctx, ggml_context * ctx_weights, ggml_tensor * out, ggml_tensor * ids) { |
| 7872 | GGML_ASSERT(ctx_weights); |
| 7873 | ggml_tensor * scale = ggml_new_tensor_1d(ctx_weights, GGML_TYPE_F32, n_mats); |
| 7874 | ggml_tensor * s = ggml_reshape_3d(ctx, scale, 1, n_mats, 1); |
| 7875 | s = ggml_repeat_4d(ctx, s, 1, n_mats, m, 1); |
| 7876 | s = ggml_get_rows(ctx, s, ids); |
| 7877 | return ggml_mul(ctx, out, s); |
| 7878 | } |
| 7879 | |
| 7880 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 7881 | GGML_ASSERT(!use_weight_context()); |
| 7882 | return build_graph(ctx, nullptr); |
| 7883 | } |
| 7884 | |
| 7885 | ggml_tensor * build_graph(ggml_context * ctx, ggml_context * ctx_weights) override { |
| 7886 | if (!use_id) { |
| 7887 | const int channels = batch_dims[0]; |
| 7888 | const int samples = batch_dims[1]; |
| 7889 | std::array<int64_t, 4> ne = { k, m, channels, samples }; |
| 7890 | std::array<int64_t, 4> ne0 = { k, n, channels, samples }; |
| 7891 | |
| 7892 | ggml_tensor * cur = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne.data()); |
| 7893 | ggml_tensor * gate = with_gate ? ggml_new_tensor(ctx, type, 4, ne0.data()) : nullptr; |
| 7894 | ggml_tensor * up = ggml_new_tensor(ctx, type, 4, ne0.data()); |
| 7895 | |
| 7896 | auto build_lane_up = [&]() { |
| 7897 | ggml_tensor * ffn_up = ggml_mul_mat(ctx, up, cur); |
| 7898 | if (with_lane_scale) { |
| 7899 | ffn_up = build_lane_scale_dense(ctx, ffn_up); |
| 7900 | } |
| 7901 | if (with_bias) { |
| 7902 | std::array<int64_t, 4> bias_ne = { ffn_up->ne[0], 1, channels, samples }; |
| 7903 | ggml_tensor * up_bias = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, bias_ne.data()); |
| 7904 | ffn_up = ggml_add(ctx, ffn_up, up_bias); |
| 7905 | } |
| 7906 | return ffn_up; |
| 7907 | }; |
| 7908 | |
| 7909 | auto build_lane_gate = [&]() { |
| 7910 | ggml_tensor * ffn_gate = ggml_mul_mat(ctx, gate, cur); |
| 7911 | if (with_lane_scale) { |
| 7912 | ffn_gate = build_lane_scale_dense(ctx, ffn_gate); |
| 7913 | } |
| 7914 | if (with_bias) { |
| 7915 | std::array<int64_t, 4> bias_ne = { ffn_gate->ne[0], 1, channels, samples }; |
| 7916 | ggml_tensor * gate_bias = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, bias_ne.data()); |
| 7917 | ffn_gate = ggml_add(ctx, ffn_gate, gate_bias); |
| 7918 | } |
| 7919 | return ffn_gate; |
| 7920 | }; |
| 7921 | |
| 7922 | ggml_tensor * ffn_up = build_lane_up(); |
| 7923 | ggml_tensor * ffn_gate = with_gate ? build_lane_gate() : nullptr; |
| 7924 | |
| 7925 | ggml_tensor * out = with_gate ? build_gate(ctx, ffn_gate, ffn_up) : ffn_up; |
| 7926 | |
| 7927 | std::array<int64_t, 4> bias2_ne = { out->ne[0], 1, channels, samples }; |
| 7928 | ggml_tensor * bias2 = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, bias2_ne.data()); |
| 7929 | out = ggml_add(ctx, out, bias2); |
| 7930 | |
| 7931 | ggml_set_name(out, "out"); |
| 7932 | return out; |
| 7933 | } else { |
| 7934 | ggml_tensor * gates = ggml_new_tensor_3d(ctx, type, k, n, n_mats); |
| 7935 | ggml_tensor * ups = ggml_new_tensor_3d(ctx, type, k, n, n_mats); |
| 7936 | ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_mats, m); |
| 7937 | |
| 7938 | if (n_used != n_mats) { |
| 7939 | ids = ggml_view_2d(ctx, ids, n_used, m, ids->nb[1], 0); |
| 7940 | } |
| 7941 | |
| 7942 | ggml_tensor * cur = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, k, this->b ? 1 : n_used, m); |
| 7943 | ggml_set_name(cur, "cur"); |
| 7944 | |
| 7945 | auto build_lane_up = [&]() { |
| 7946 | ggml_tensor * ffn_up = ggml_mul_mat_id(ctx, ups, cur, ids); |
| 7947 | if (with_lane_scale) { |
| 7948 | ffn_up = build_lane_scale_id(ctx, ctx_weights, ffn_up, ids); |
| 7949 | } |
| 7950 | if (with_bias) { |
| 7951 | ggml_tensor * up_bias_param = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, ffn_up->ne[0], n_mats); |
| 7952 | ffn_up = ggml_add_id(ctx, ffn_up, up_bias_param, ids); |
| 7953 | } |
| 7954 | return ffn_up; |
| 7955 | }; |
| 7956 | |
| 7957 | auto build_lane_gate = [&]() { |
| 7958 | ggml_tensor * ffn_gate = ggml_mul_mat_id(ctx, gates, cur, ids); |
| 7959 | if (with_lane_scale) { |
| 7960 | ffn_gate = build_lane_scale_id(ctx, ctx_weights, ffn_gate, ids); |
| 7961 | } |
| 7962 | if (with_bias) { |
| 7963 | ggml_tensor * gate_bias_param = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, ffn_gate->ne[0], n_mats); |
| 7964 | ffn_gate = ggml_add_id(ctx, ffn_gate, gate_bias_param, ids); |
| 7965 | } |
| 7966 | return ffn_gate; |
| 7967 | }; |
| 7968 | |
| 7969 | ggml_tensor * ffn_up = build_lane_up(); |
| 7970 | ggml_tensor * ffn_gate = with_gate ? build_lane_gate() : nullptr; |
| 7971 | |
| 7972 | ggml_tensor * out = with_gate ? build_gate(ctx, ffn_gate, ffn_up) : ffn_up; |
| 7973 | |
| 7974 | std::array<int64_t, 4> scale_ne { 1, out->ne[1], out->ne[2], out->ne[3] }; |
| 7975 | ggml_tensor * scale = ggml_new_tensor(ctx, out->type, 4, scale_ne.data()); |
| 7976 | out = ggml_mul(ctx, out, scale); |
| 7977 | |
| 7978 | ggml_set_name(out, "out"); |
| 7979 | return out; |
| 7980 | } |
| 7981 | } |
| 7982 | |
| 7983 | void initialize_tensors(ggml_context * ctx) override { |
| 7984 | if (!use_id) { |
| 7985 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 7986 | init_tensor_uniform(t); |
| 7987 | } |
| 7988 | } else { |
| 7989 | init_mul_mat_id_tensors(ctx, n_mats, skip); |
| 7990 | } |
| 7991 | } |
| 7992 | |
| 7993 | double max_nmse_err() override { |
| 7994 | return 5e-3; |
| 7995 | } |
| 7996 | }; |
| 7997 | |
| 7998 | // GGML_OP_SUM |
| 7999 | struct test_sum : public test_case { |
| 8000 | const ggml_type type; |
| 8001 | const std::array<int64_t, 4> ne; |
| 8002 | const std::array<int64_t, 4> permute; |
| 8003 | bool _use_permute; |
| 8004 | const bool slice; |
| 8005 | |
| 8006 | std::string vars() override { |
| 8007 | std::string v = VARS_TO_STR2(type, ne); |
| 8008 | if (_use_permute) v += "," + VAR_TO_STR(permute); |
| 8009 | if (slice) v += "," + VAR_TO_STR(slice); |
| 8010 | return v; |
| 8011 | } |
| 8012 | |
| 8013 | test_sum(ggml_type type = GGML_TYPE_F32, |
| 8014 | std::array<int64_t, 4> ne = {10, 5, 4, 3}, |
| 8015 | std::array<int64_t, 4> permute = {0, 0, 0, 0}, |
| 8016 | bool slice = false) |
| 8017 | : type(type), ne(ne), permute(permute), |
| 8018 | _use_permute(permute[0] + permute[1] + permute[2] + permute[3] > 0), |
| 8019 | slice(slice) {} |
| 8020 | |
| 8021 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8022 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 8023 | |
| 8024 | // the backward pass of a sliced view scatters the gradient via GGML_OP_ACC, |
| 8025 | // whose CPU implementation requires contiguous rows; a row-breaking permute |
| 8026 | // (permute[0] != 0) on top of a slice violates that, so skip grad there |
| 8027 | const bool grad_supported = !(slice && _use_permute && permute[0] != 0); |
| 8028 | if (grad_supported) { |
| 8029 | ggml_set_param(a); |
| 8030 | } |
| 8031 | |
| 8032 | ggml_set_name(a, "a"); |
| 8033 | |
| 8034 | if (slice) { |
| 8035 | a = ggml_view_4d(ctx, a, |
| 8036 | ne[0], ne[1], ne[2] / 2, ne[3] - 1, |
| 8037 | a->nb[1], a->nb[2] * 2, a->nb[3], /*offset=*/a->nb[3]); |
| 8038 | ggml_set_name(a, "a_sliced"); |
| 8039 | } |
| 8040 | |
| 8041 | if (_use_permute) { |
| 8042 | a = ggml_permute(ctx, a, permute[0], permute[1], permute[2], permute[3]); |
| 8043 | ggml_set_name(a, "a_permuted"); |
| 8044 | } |
| 8045 | |
| 8046 | ggml_tensor * out = ggml_sum(ctx, a); |
| 8047 | ggml_set_name(out, "out"); |
| 8048 | |
| 8049 | return out; |
| 8050 | } |
| 8051 | |
| 8052 | float grad_eps() override { |
| 8053 | return 0.1f * sqrtf(ne[0]*ne[1]*ne[2]*ne[3]); |
| 8054 | } |
| 8055 | |
| 8056 | // Don't center the distribution around zero. Helps to avoid catastrophic cancellation. |
| 8057 | void initialize_tensors(ggml_context * ctx) override { |
| 8058 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) { |
| 8059 | init_tensor_uniform(t, -0.9f, 1.1f); |
| 8060 | } |
| 8061 | } |
| 8062 | }; |
| 8063 | |
| 8064 | // GGML_OP_SUM_ROWS |
| 8065 | struct test_sum_rows : public test_case { |
| 8066 | const ggml_type type; |
| 8067 | const std::array<int64_t, 4> ne; |
| 8068 | const bool permute; |
| 8069 | const bool slice; |
| 8070 | |
| 8071 | std::string vars() override { |
| 8072 | return VARS_TO_STR4(type, ne, permute, slice); |
| 8073 | } |
| 8074 | |
| 8075 | test_sum_rows(ggml_type type = GGML_TYPE_F32, |
| 8076 | std::array<int64_t, 4> ne = {10, 5, 4, 3}, |
| 8077 | bool permute = false, bool slice = false) |
| 8078 | : type(type), ne(ne), permute(permute), slice(slice) {} |
| 8079 | |
| 8080 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8081 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 8082 | ggml_set_param(a); |
| 8083 | ggml_set_name(a, "a"); |
| 8084 | |
| 8085 | if (slice) { |
| 8086 | a = ggml_view_4d(ctx, a, |
| 8087 | ne[0], ne[1], ne[2] / 2, ne[3] - 1, |
| 8088 | a->nb[1], a->nb[2] * 2, a->nb[3], /*offset=*/a->nb[3]); |
| 8089 | } |
| 8090 | if (permute) { |
| 8091 | a = ggml_permute(ctx, a, 0, 2, 3, 1); |
| 8092 | } |
| 8093 | |
| 8094 | ggml_tensor * out = ggml_sum_rows(ctx, a); |
| 8095 | ggml_set_name(out, "out"); |
| 8096 | |
| 8097 | return out; |
| 8098 | } |
| 8099 | }; |
| 8100 | |
| 8101 | // GGML_OP_MEAN |
| 8102 | struct test_mean : public test_case { |
| 8103 | const ggml_type type; |
| 8104 | const std::array<int64_t, 4> ne; |
| 8105 | const bool permute; |
| 8106 | const bool slice; |
| 8107 | |
| 8108 | std::string vars() override { |
| 8109 | return VARS_TO_STR4(type, ne, permute, slice); |
| 8110 | } |
| 8111 | |
| 8112 | test_mean(ggml_type type = GGML_TYPE_F32, |
| 8113 | std::array<int64_t, 4> ne = {10, 5, 4, 3}, |
| 8114 | bool permute = false, bool slice = false) |
| 8115 | : type(type), ne(ne), permute(permute), slice(slice) {} |
| 8116 | |
| 8117 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8118 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 8119 | ggml_set_param(a); |
| 8120 | ggml_set_name(a, "a"); |
| 8121 | |
| 8122 | if (slice) { |
| 8123 | a = ggml_view_4d(ctx, a, |
| 8124 | ne[0], ne[1], ne[2] / 2, ne[3] - 1, |
| 8125 | a->nb[1], a->nb[2] * 2, a->nb[3], /*offset=*/a->nb[3]); |
| 8126 | } |
| 8127 | if (permute) { |
| 8128 | a = ggml_permute(ctx, a, 0, 2, 3, 1); |
| 8129 | } |
| 8130 | |
| 8131 | ggml_tensor * out = ggml_mean(ctx, a); |
| 8132 | ggml_set_name(out, "out"); |
| 8133 | |
| 8134 | return out; |
| 8135 | } |
| 8136 | |
| 8137 | float grad_eps() override { |
| 8138 | return 0.1f * ne[0]*ne[1]*ne[2]*ne[3]; |
| 8139 | } |
| 8140 | |
| 8141 | // Don't center the distribution around zero. Helps to avoid catastrophic cancellation. |
| 8142 | void initialize_tensors(ggml_context * ctx) override { |
| 8143 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) { |
| 8144 | init_tensor_uniform(t, -0.9f, 1.1f); |
| 8145 | } |
| 8146 | } |
| 8147 | }; |
| 8148 | |
| 8149 | // GGML_OP_UPSCALE |
| 8150 | struct test_upscale : public test_case { |
| 8151 | const ggml_type type; |
| 8152 | const std::array<int64_t, 4> ne; |
| 8153 | const int32_t scale_factor; |
| 8154 | const bool transpose; |
| 8155 | const ggml_scale_mode mode; |
| 8156 | |
| 8157 | std::string vars() override { |
| 8158 | return VARS_TO_STR5(type, ne, scale_factor, mode, transpose); |
| 8159 | } |
| 8160 | |
| 8161 | test_upscale(ggml_type type = GGML_TYPE_F32, |
| 8162 | std::array<int64_t, 4> ne = {512, 512, 3, 1}, |
| 8163 | int32_t scale_factor = 2, ggml_scale_mode mode = GGML_SCALE_MODE_NEAREST, bool transpose = false) |
| 8164 | : type(type), ne(ne), scale_factor(scale_factor), transpose(transpose), mode(mode) {} |
| 8165 | |
| 8166 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8167 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 8168 | ggml_set_name(a, "a"); |
| 8169 | |
| 8170 | if (transpose) { |
| 8171 | a = ggml_transpose(ctx, a); |
| 8172 | ggml_set_name(a, "a_transposed"); |
| 8173 | } |
| 8174 | |
| 8175 | ggml_tensor * out = ggml_upscale(ctx, a, scale_factor, mode); |
| 8176 | ggml_set_name(out, "out"); |
| 8177 | |
| 8178 | return out; |
| 8179 | } |
| 8180 | }; |
| 8181 | |
| 8182 | // GGML_OP_UPSCALE (via ggml_interpolate) |
| 8183 | struct test_interpolate : public test_case { |
| 8184 | const ggml_type type; |
| 8185 | const std::array<int64_t, 4> ne; |
| 8186 | const std::array<int64_t, 4> ne_tgt; |
| 8187 | const ggml_scale_mode mode = GGML_SCALE_MODE_NEAREST; |
| 8188 | const std::array<int64_t, 4> permute; |
| 8189 | bool _use_permute; |
| 8190 | |
| 8191 | std::string vars() override { |
| 8192 | std::string v = VARS_TO_STR4(type, ne, ne_tgt, mode); |
| 8193 | if (_use_permute) v += "," + VAR_TO_STR(permute); |
| 8194 | return v; |
| 8195 | } |
| 8196 | |
| 8197 | test_interpolate(ggml_type type = GGML_TYPE_F32, |
| 8198 | std::array<int64_t, 4> ne = {2, 5, 7, 11}, |
| 8199 | std::array<int64_t, 4> ne_tgt = {5, 7, 11, 13}, |
| 8200 | ggml_scale_mode mode = GGML_SCALE_MODE_NEAREST, |
| 8201 | std::array<int64_t, 4> permute = {0, 0, 0, 0}) |
| 8202 | : type(type), ne(ne), ne_tgt(ne_tgt), mode(mode), permute(permute), |
| 8203 | _use_permute(permute[0] + permute[1] + permute[2] + permute[3] > 0) {} |
| 8204 | |
| 8205 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8206 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 8207 | ggml_set_name(a, "a"); |
| 8208 | |
| 8209 | if (_use_permute) { |
| 8210 | a = ggml_permute(ctx, a, permute[0], permute[1], permute[2], permute[3]); |
| 8211 | ggml_set_name(a, "a_permuted"); |
| 8212 | } |
| 8213 | |
| 8214 | ggml_tensor * out = ggml_interpolate(ctx, a, ne_tgt[0], ne_tgt[1],ne_tgt[2], ne_tgt[3], mode); |
| 8215 | ggml_set_name(out, "out"); |
| 8216 | |
| 8217 | return out; |
| 8218 | } |
| 8219 | }; |
| 8220 | |
| 8221 | // GGML_OP_GROUP_NORM |
| 8222 | struct test_group_norm : public test_case { |
| 8223 | const ggml_type type; |
| 8224 | const std::array<int64_t, 4> ne; |
| 8225 | const int32_t num_groups; |
| 8226 | const float eps; |
| 8227 | |
| 8228 | std::string vars() override { |
| 8229 | return VARS_TO_STR4(type, ne, num_groups, eps); |
| 8230 | } |
| 8231 | |
| 8232 | test_group_norm(ggml_type type = GGML_TYPE_F32, |
| 8233 | std::array<int64_t, 4> ne = {64, 64, 320, 1}, |
| 8234 | int32_t num_groups = 32, |
| 8235 | float eps = 1e-6f) |
| 8236 | : type(type), ne(ne), num_groups(num_groups), eps(eps) {} |
| 8237 | |
| 8238 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8239 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 8240 | ggml_set_name(a, "a"); |
| 8241 | |
| 8242 | ggml_tensor * out = ggml_group_norm(ctx, a, num_groups, eps); |
| 8243 | ggml_set_name(out, "out"); |
| 8244 | |
| 8245 | return out; |
| 8246 | } |
| 8247 | }; |
| 8248 | |
| 8249 | // GGML_OP_GROUP_NORM + GGML_OP_MUL + GGML_OP_ADD |
| 8250 | struct test_group_norm_mul_add : public test_case { |
| 8251 | const ggml_type type; |
| 8252 | const std::array<int64_t, 4> ne; |
| 8253 | int num_groups; |
| 8254 | float eps; |
| 8255 | |
| 8256 | std::string op_desc(ggml_tensor * t) override { |
| 8257 | GGML_UNUSED(t); |
| 8258 | return "GROUP_NORM_MUL_ADD"; |
| 8259 | } |
| 8260 | |
| 8261 | bool run_whole_graph() override { return true; } |
| 8262 | |
| 8263 | std::string vars() override { |
| 8264 | return VARS_TO_STR4(type, ne, num_groups, eps); |
| 8265 | } |
| 8266 | |
| 8267 | test_group_norm_mul_add(ggml_type type = GGML_TYPE_F32, |
| 8268 | std::array<int64_t, 4> ne = {128, 1, 1, 1}, |
| 8269 | int num_groups = 4, |
| 8270 | float eps = 1e-5f) |
| 8271 | : type(type), ne(ne), num_groups(num_groups), eps(eps) {} |
| 8272 | |
| 8273 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8274 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 8275 | ggml_tensor * w = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 8276 | ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 8277 | ggml_set_param(a); ggml_set_param(w); ggml_set_param(b); |
| 8278 | ggml_set_name(a, "a"); ggml_set_name(w, "w"); ggml_set_name(b, "b"); |
| 8279 | ggml_tensor * n = ggml_group_norm(ctx, a, num_groups, eps); |
| 8280 | ggml_tensor * m = ggml_mul(ctx, n, w); |
| 8281 | ggml_tensor * out = ggml_add(ctx, m, b); |
| 8282 | ggml_set_name(out, "out"); |
| 8283 | return out; |
| 8284 | } |
| 8285 | }; |
| 8286 | |
| 8287 | // GGML_OP_L2_NORM x N: independent same-shape norms in one graph (strided qkv views or |
| 8288 | // contiguous), consuming adds nested so the norms stay adjacent in the graph. |
| 8289 | struct test_l2_norm_batch : public test_case { |
| 8290 | const ggml_type type; |
| 8291 | const std::array<int64_t, 4> ne; |
| 8292 | const int n_norms; |
| 8293 | const float eps; |
| 8294 | const bool strided; |
| 8295 | |
| 8296 | std::string vars() override { return VARS_TO_STR5(type, ne, n_norms, eps, strided); } |
| 8297 | std::string op_desc(ggml_tensor * t) override { GGML_UNUSED(t); return "L2_NORM_BATCH"; } |
| 8298 | bool run_whole_graph() override { return true; } |
| 8299 | |
| 8300 | test_l2_norm_batch(ggml_type type = GGML_TYPE_F32, std::array<int64_t, 4> ne = { 128, 16, 16, 1 }, |
| 8301 | int n_norms = 4, float eps = 1e-12f, bool strided = true) |
| 8302 | : type(type), ne(ne), n_norms(n_norms), eps(eps), strided(strided) {} |
| 8303 | |
| 8304 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8305 | GGML_ASSERT(n_norms >= 2 && n_norms <= 8); |
| 8306 | ggml_tensor * parent = nullptr; |
| 8307 | if (strided) { |
| 8308 | parent = ggml_new_tensor_4d(ctx, type, ne[0], ne[1] * n_norms, ne[2], ne[3]); // qkv buffer |
| 8309 | } |
| 8310 | ggml_tensor * norms[8] = {}; |
| 8311 | for (int t = 0; t < n_norms; ++t) { |
| 8312 | ggml_tensor * src; |
| 8313 | if (strided) { |
| 8314 | src = ggml_view_4d(ctx, parent, ne[0], ne[1], ne[2], ne[3], parent->nb[1], parent->nb[2], |
| 8315 | parent->nb[3], t * ne[1] * parent->nb[1]); |
| 8316 | } else { |
| 8317 | src = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 8318 | } |
| 8319 | norms[t] = ggml_l2_norm(ctx, src, eps); |
| 8320 | } |
| 8321 | ggml_tensor * out = norms[n_norms - 1]; |
| 8322 | for (int t = n_norms - 2; t >= 0; --t) { |
| 8323 | out = ggml_add(ctx, norms[t], out); |
| 8324 | } |
| 8325 | ggml_set_name(out, "out"); |
| 8326 | return out; |
| 8327 | } |
| 8328 | }; |
| 8329 | |
| 8330 | // GGML_OP_L2_NORM |
| 8331 | struct test_l2_norm : public test_case { |
| 8332 | const ggml_type type; |
| 8333 | const std::array<int64_t, 4> ne; |
| 8334 | const float eps; |
| 8335 | bool v; |
| 8336 | bool noncontig_rows; |
| 8337 | |
| 8338 | std::string vars() override { |
| 8339 | return VARS_TO_STR5(type, ne, eps, v, noncontig_rows); |
| 8340 | } |
| 8341 | |
| 8342 | test_l2_norm(ggml_type type = GGML_TYPE_F32, |
| 8343 | std::array<int64_t, 4> ne = {64, 64, 320, 1}, |
| 8344 | float eps = 1e-12f, |
| 8345 | bool v = false, |
| 8346 | bool noncontig_rows = false) |
| 8347 | : type(type), ne(ne), eps(eps), v(v), noncontig_rows(noncontig_rows) {} |
| 8348 | |
| 8349 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8350 | const std::array<int64_t, 4> ne_a = noncontig_rows ? |
| 8351 | std::array<int64_t, 4>{ ne[1], ne[0], ne[2], ne[3] } : ne; |
| 8352 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 8353 | ggml_set_name(a, "a"); |
| 8354 | |
| 8355 | if (noncontig_rows) { |
| 8356 | a = ggml_permute(ctx, a, 1, 0, 2, 3); |
| 8357 | ggml_set_name(a, "permuted a"); |
| 8358 | } |
| 8359 | if (v) { |
| 8360 | a = ggml_view_4d(ctx, a, a->ne[0]/2, a->ne[1]/2, a->ne[2]/2, a->ne[3]/2, a->nb[1], a->nb[2], a->nb[3], 0); |
| 8361 | ggml_set_name(a, "view of a"); |
| 8362 | } |
| 8363 | |
| 8364 | ggml_tensor * out = ggml_l2_norm(ctx, a, eps); |
| 8365 | ggml_set_name(out, "out"); |
| 8366 | |
| 8367 | return out; |
| 8368 | } |
| 8369 | }; |
| 8370 | |
| 8371 | // GGML_OP_ACC |
| 8372 | struct test_acc : public test_case { |
| 8373 | const ggml_type type; |
| 8374 | const std::array<int64_t, 4> ne_a; |
| 8375 | const std::array<int64_t, 4> ne_b; |
| 8376 | const int64_t stride_dim; |
| 8377 | |
| 8378 | std::string vars() override { |
| 8379 | return VARS_TO_STR4(type, ne_a, ne_b, stride_dim); |
| 8380 | } |
| 8381 | |
| 8382 | test_acc(ggml_type type = GGML_TYPE_F32, |
| 8383 | std::array<int64_t, 4> ne_a = {256, 17, 2, 3}, |
| 8384 | std::array<int64_t, 4> ne_b = {256, 16, 2, 3}, |
| 8385 | uint64_t stride_dim = -1) |
| 8386 | : type(type), ne_a(ne_a), ne_b(ne_b), stride_dim(stride_dim) {} |
| 8387 | |
| 8388 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8389 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 8390 | ggml_set_param(a); |
| 8391 | ggml_set_name(a, "a"); |
| 8392 | |
| 8393 | ggml_tensor * b; |
| 8394 | if (stride_dim == 1 || stride_dim == 2 || stride_dim == 3) { |
| 8395 | // Create a larger tensor and take a view at a non-zero offset. |
| 8396 | // This tests that the backend correctly handles b's data offset |
| 8397 | std::array<int64_t, 4> ne_b_pad = {ne_b[0], ne_b[1], ne_b[2], ne_b[3]}; |
| 8398 | ne_b_pad[stride_dim] += 1; |
| 8399 | ggml_tensor * b_pad = ggml_new_tensor(ctx, type, 4, ne_b_pad.data()); |
| 8400 | ggml_set_param(b_pad); |
| 8401 | ggml_set_name(b_pad, "b_pad"); |
| 8402 | // View that skips the first row, so b has a non-zero byte offset |
| 8403 | b = ggml_view_4d(ctx, b_pad, |
| 8404 | ne_b[0], ne_b[1], ne_b[2], ne_b[3], |
| 8405 | b_pad->nb[1], b_pad->nb[2], b_pad->nb[3], |
| 8406 | b_pad->nb[1]); |
| 8407 | } else { |
| 8408 | b = ggml_new_tensor(ctx, type, 4, ne_b.data()); |
| 8409 | ggml_set_param(b); |
| 8410 | } |
| 8411 | ggml_set_name(b, "b"); |
| 8412 | |
| 8413 | // When ne_b[0] < ne_a[0], a->nb[1] != b->nb[1], so the stride |
| 8414 | // parameters to ggml_acc don't match b's natural stride. |
| 8415 | ggml_tensor * out = ggml_acc(ctx, a, b, a->nb[1], a->nb[2], a->nb[3], 0); |
| 8416 | ggml_set_name(out, "out"); |
| 8417 | |
| 8418 | return out; |
| 8419 | } |
| 8420 | }; |
| 8421 | |
| 8422 | // GGML_OP_PAD |
| 8423 | struct test_pad : public test_case { |
| 8424 | const ggml_type type; |
| 8425 | const std::array<int64_t, 4> ne_a; |
| 8426 | const int pad_0; |
| 8427 | const int pad_1; |
| 8428 | const bool circular; |
| 8429 | |
| 8430 | std::string vars() override { |
| 8431 | return VARS_TO_STR5(type, ne_a, pad_0, pad_1, circular); |
| 8432 | } |
| 8433 | |
| 8434 | test_pad(ggml_type type = GGML_TYPE_F32, |
| 8435 | std::array<int64_t, 4> ne_a = {512, 512, 1, 1}, |
| 8436 | int pad_0 = 1, int pad_1 = 1, bool circular = false) |
| 8437 | : type(type), ne_a(ne_a), pad_0(pad_0), pad_1(pad_1), circular(circular) {} |
| 8438 | |
| 8439 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8440 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 8441 | ggml_set_name(a, "a"); |
| 8442 | |
| 8443 | ggml_tensor * out = circular |
| 8444 | ? ggml_pad_circular(ctx, a, pad_0, pad_1, 0, 0) |
| 8445 | : ggml_pad(ctx, a, pad_0, pad_1, 0, 0); |
| 8446 | ggml_set_name(out, "out"); |
| 8447 | |
| 8448 | return out; |
| 8449 | } |
| 8450 | }; |
| 8451 | |
| 8452 | // GGML_OP_PAD (with extension) |
| 8453 | struct test_pad_ext : public test_case { |
| 8454 | const ggml_type type; |
| 8455 | const std::array<int64_t, 4> ne_a; |
| 8456 | const int lp0; |
| 8457 | const int rp0; |
| 8458 | const int lp1; |
| 8459 | const int rp1; |
| 8460 | const int lp2; |
| 8461 | const int rp2; |
| 8462 | const int lp3; |
| 8463 | const int rp3; |
| 8464 | const int tfrm; // 0 - none, 1 - non-cont, 2 - perm |
| 8465 | const bool circular; |
| 8466 | |
| 8467 | std::string vars() override { |
| 8468 | return VARS_TO_STR12(type, ne_a, lp0, rp0, lp1, rp1, lp2, rp2, lp3, rp3, tfrm, circular); |
| 8469 | } |
| 8470 | |
| 8471 | test_pad_ext(ggml_type type = GGML_TYPE_F32, |
| 8472 | std::array<int64_t, 4> ne_a = {512, 512, 3, 1}, |
| 8473 | int lp0 = 1, int rp0 = 1, int lp1 = 1, int rp1 = 1, |
| 8474 | int lp2 = 1, int rp2 = 1, int lp3 = 1, int rp3 = 1, |
| 8475 | int tfrm = 0, bool circular = false) |
| 8476 | : type(type), ne_a(ne_a), lp0(lp0), rp0(rp0), lp1(lp1), rp1(rp1), lp2(lp2), rp2(rp2), lp3(lp3), rp3(rp3), |
| 8477 | tfrm(tfrm), circular(circular) {} |
| 8478 | |
| 8479 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8480 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 8481 | ggml_set_name(a, "a"); |
| 8482 | |
| 8483 | if (tfrm == 1) { |
| 8484 | a = ggml_view_4d(ctx, a, (a->ne[0] + 1) / 2, (a->ne[1] + 1) / 2, (a->ne[2] + 1) / 2, (a->ne[3] + 1) / 2, a->nb[1], a->nb[2], a->nb[3], 0); |
| 8485 | ggml_set_name(a, "view of a"); |
| 8486 | } else if (tfrm == 2) { |
| 8487 | a = ggml_permute(ctx, a, 2, 1, 0, 3); |
| 8488 | ggml_set_name(a, "permuted a"); |
| 8489 | } |
| 8490 | |
| 8491 | ggml_tensor * out = circular |
| 8492 | ? ggml_pad_ext_circular(ctx, a, lp0, rp0, lp1, rp1, lp2, rp2, lp3, rp3) |
| 8493 | : ggml_pad_ext (ctx, a, lp0, rp0, lp1, rp1, lp2, rp2, lp3, rp3); |
| 8494 | ggml_set_name(out, "out"); |
| 8495 | |
| 8496 | return out; |
| 8497 | } |
| 8498 | }; |
| 8499 | |
| 8500 | // GGML_OP_PAD_REFLECT_1D |
| 8501 | struct test_pad_reflect_1d : public test_case { |
| 8502 | const ggml_type type; |
| 8503 | const std::array<int64_t, 4> ne_a; |
| 8504 | const int pad_0; |
| 8505 | const int pad_1; |
| 8506 | |
| 8507 | std::string vars() override { |
| 8508 | return VARS_TO_STR4(type, ne_a, pad_0, pad_1); |
| 8509 | } |
| 8510 | |
| 8511 | test_pad_reflect_1d(ggml_type type = GGML_TYPE_F32, |
| 8512 | std::array<int64_t, 4> ne_a = {512, 34, 2, 1}, |
| 8513 | int pad_0 = 10, int pad_1 = 9) |
| 8514 | : type(type), ne_a(ne_a), pad_0(pad_0), pad_1(pad_1) {} |
| 8515 | |
| 8516 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8517 | ggml_tensor * a = ggml_new_tensor(ctx, type, 2, ne_a.data()); |
| 8518 | ggml_set_name(a, "a"); |
| 8519 | |
| 8520 | ggml_tensor * out = ggml_pad_reflect_1d(ctx, a, pad_0, pad_1); |
| 8521 | ggml_set_name(out, "out"); |
| 8522 | |
| 8523 | return out; |
| 8524 | } |
| 8525 | }; |
| 8526 | |
| 8527 | // GGML_OP_ROLL |
| 8528 | struct test_roll : public test_case { |
| 8529 | const int shift0; |
| 8530 | const int shift1; |
| 8531 | const int shift3; |
| 8532 | const int shift4; |
| 8533 | const bool permute; |
| 8534 | |
| 8535 | std::string vars() override { |
| 8536 | return VARS_TO_STR5(shift0, shift1, shift3, shift4, permute); |
| 8537 | } |
| 8538 | |
| 8539 | test_roll(int shift0 = 3, int shift1 = -2, int shift3 = 1, int shift4 = -1, bool permute = false) |
| 8540 | : shift0(shift0), shift1(shift1), shift3(shift3), shift4(shift4), permute(permute) {} |
| 8541 | |
| 8542 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8543 | int64_t ne[4] = {10, 5, 4, 3}; |
| 8544 | ggml_tensor * a = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne); |
| 8545 | ggml_set_name(a, "a"); |
| 8546 | |
| 8547 | if (permute) { |
| 8548 | // ggml_roll only requires nb[0] == type size, so a permuted src is valid |
| 8549 | a = ggml_permute(ctx, a, 0, 2, 1, 3); |
| 8550 | ggml_set_name(a, "a_permuted"); |
| 8551 | } |
| 8552 | |
| 8553 | ggml_tensor * out = ggml_roll(ctx, a, shift0, shift1, shift3, shift4); |
| 8554 | ggml_set_name(out, "out"); |
| 8555 | |
| 8556 | return out; |
| 8557 | } |
| 8558 | }; |
| 8559 | |
| 8560 | // GGML_OP_ARANGE |
| 8561 | struct test_arange : public test_case { |
| 8562 | const ggml_type type; |
| 8563 | const float start; |
| 8564 | const float stop; |
| 8565 | const float step; |
| 8566 | |
| 8567 | std::string vars() override { |
| 8568 | return VARS_TO_STR4(type, start, stop, step); |
| 8569 | } |
| 8570 | |
| 8571 | test_arange(ggml_type type = GGML_TYPE_F32, |
| 8572 | float start = 0.f, float stop = 10.f, float step = 1.f) |
| 8573 | : type(type), start(start), stop(stop), step(step) {} |
| 8574 | |
| 8575 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8576 | ggml_tensor * out = ggml_arange(ctx, start, stop, step); |
| 8577 | ggml_set_name(out, "out"); |
| 8578 | |
| 8579 | return out; |
| 8580 | } |
| 8581 | }; |
| 8582 | |
| 8583 | // GGML_OP_TIMESTEP_EMBEDDING |
| 8584 | struct test_timestep_embedding : public test_case { |
| 8585 | const ggml_type type; |
| 8586 | const std::array<int64_t, 4> ne_a; |
| 8587 | const int dim; |
| 8588 | const int max_period; |
| 8589 | |
| 8590 | std::string vars() override { |
| 8591 | return VARS_TO_STR4(type, ne_a, dim, max_period); |
| 8592 | } |
| 8593 | |
| 8594 | test_timestep_embedding(ggml_type type = GGML_TYPE_F32, |
| 8595 | std::array<int64_t, 4> ne_a = {2, 1, 1, 1}, |
| 8596 | int dim = 320, int max_period=10000) |
| 8597 | : type(type), ne_a(ne_a), dim(dim), max_period(max_period) {} |
| 8598 | |
| 8599 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8600 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 8601 | ggml_set_name(a, "a"); |
| 8602 | |
| 8603 | ggml_tensor * out = ggml_timestep_embedding(ctx, a, dim, max_period); |
| 8604 | ggml_set_name(out, "out"); |
| 8605 | |
| 8606 | return out; |
| 8607 | } |
| 8608 | }; |
| 8609 | |
| 8610 | // GGML_OP_LEAKY_RELU |
| 8611 | struct test_leaky_relu : public test_case { |
| 8612 | const ggml_type type; |
| 8613 | const std::array<int64_t, 4> ne_a; |
| 8614 | const float negative_slope; |
| 8615 | |
| 8616 | std::string vars() override { |
| 8617 | return VARS_TO_STR3(type, ne_a, negative_slope); |
| 8618 | } |
| 8619 | |
| 8620 | test_leaky_relu(ggml_type type = GGML_TYPE_F32, |
| 8621 | std::array<int64_t, 4> ne_a = {10, 5, 4, 3}, |
| 8622 | float negative_slope = 0.1f) |
| 8623 | : type(type), ne_a(ne_a), negative_slope(negative_slope) {} |
| 8624 | |
| 8625 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8626 | ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data()); |
| 8627 | ggml_set_name(a, "a"); |
| 8628 | |
| 8629 | ggml_tensor * out = ggml_leaky_relu(ctx, a, negative_slope, true); |
| 8630 | ggml_set_name(out, "out"); |
| 8631 | |
| 8632 | return out; |
| 8633 | } |
| 8634 | }; |
| 8635 | |
| 8636 | // GGML_OP_FLASH_ATTN_EXT |
| 8637 | struct test_flash_attn_ext : public test_case { |
| 8638 | const int64_t hsk; // K head size |
| 8639 | const int64_t hsv; // V head size |
| 8640 | const int64_t nh; // num heads |
| 8641 | const std::array<int64_t, 2> nr23; // repeat in dim 2 and 3, tests for grouped-query attention |
| 8642 | const int64_t kv; // kv size |
| 8643 | const int64_t nb; // batch size |
| 8644 | |
| 8645 | const bool mask; // use mask |
| 8646 | const bool sinks; // use sinks |
| 8647 | |
| 8648 | const float max_bias; // ALiBi |
| 8649 | const float logit_softcap; // Gemma 2 |
| 8650 | |
| 8651 | const ggml_prec prec; |
| 8652 | const ggml_type type_K; |
| 8653 | const ggml_type type_V; |
| 8654 | std::array<int32_t, 4> permute; |
| 8655 | const bool kv_view; // create K/V as views of a larger buffer (like a KV cache) |
| 8656 | const bool v_is_view_of_k; |
| 8657 | const int64_t n_kv_max; |
| 8658 | |
| 8659 | std::string vars() override { |
| 8660 | return VARS_TO_STR17(hsk, hsv, nh, nr23, kv, nb, mask, sinks, max_bias, logit_softcap, prec, type_K, type_V, permute, kv_view, v_is_view_of_k, n_kv_max); |
| 8661 | } |
| 8662 | |
| 8663 | double max_nmse_err() override { |
| 8664 | return 5e-4; |
| 8665 | } |
| 8666 | |
| 8667 | uint64_t op_flops(ggml_tensor * t) override { |
| 8668 | GGML_UNUSED(t); |
| 8669 | // Just counting matmul costs: |
| 8670 | // Q*K^T is nb x hsk x kv, P*V is nb x kv x hsv, per head |
| 8671 | return (2 * nh*nr23[0] * nb * (hsk + hsv) * kv)*nr23[1]; |
| 8672 | } |
| 8673 | |
| 8674 | test_flash_attn_ext(int64_t hsk = 128, int64_t hsv = 128, int64_t nh = 32, std::array<int64_t, 2> nr23 = {1, 1}, int64_t kv = 96, int64_t nb = 8, |
| 8675 | bool mask = true, bool sinks = false, float max_bias = 0.0f, float logit_softcap = 0.0f, ggml_prec prec = GGML_PREC_F32, |
| 8676 | ggml_type type_K = GGML_TYPE_F16, ggml_type type_V = GGML_TYPE_F16, std::array<int32_t, 4> permute = {0, 1, 2, 3}, |
| 8677 | bool kv_view = true, bool v_is_view_of_k = false, int64_t n_kv_max = 0) |
| 8678 | : hsk(hsk), hsv(hsv), nh(nh), nr23(nr23), kv(kv), nb(nb), mask(mask), sinks(sinks), max_bias(max_bias), logit_softcap(logit_softcap), prec(prec), |
| 8679 | type_K(type_K), type_V(type_V), permute(permute), kv_view(kv_view), v_is_view_of_k(v_is_view_of_k), n_kv_max(n_kv_max) {} |
| 8680 | |
| 8681 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8682 | const int64_t hsk_padded = GGML_PAD(hsk, ggml_blck_size(type_K)); |
| 8683 | const int64_t hsv_padded = GGML_PAD(hsv, ggml_blck_size(type_V)); |
| 8684 | |
| 8685 | auto const &create_permuted = [&](ggml_type type, int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne3, bool is_view) -> ggml_tensor * { |
| 8686 | int64_t ne[4] = {ne0, ne1, ne2, ne3}; |
| 8687 | int64_t ne_perm[4]; |
| 8688 | for (int i = 0; i < 4; ++i) { |
| 8689 | ne_perm[permute[i]] = ne[i]; |
| 8690 | } |
| 8691 | ggml_tensor * t; |
| 8692 | if (is_view) { |
| 8693 | ggml_tensor * t0 = ggml_new_tensor_4d(ctx, type, ne_perm[0], 2*ne_perm[1], ne_perm[2], ne_perm[3]); |
| 8694 | t = ggml_view_4d(ctx, t0, ne_perm[0], ne_perm[1], ne_perm[2], ne_perm[3], t0->nb[1], t0->nb[2], t0->nb[3], 0); |
| 8695 | } else { |
| 8696 | t = ggml_new_tensor_4d(ctx, type, ne_perm[0], ne_perm[1], ne_perm[2], ne_perm[3]); |
| 8697 | } |
| 8698 | if (permute != std::array<int32_t, 4>{0, 1, 2, 3}) { |
| 8699 | t = ggml_permute(ctx, t, permute[0], permute[1], permute[2], permute[3]); |
| 8700 | } |
| 8701 | return t; |
| 8702 | }; |
| 8703 | |
| 8704 | ggml_tensor * q = create_permuted(GGML_TYPE_F32, hsk_padded, nb, nh*nr23[0], nr23[1], false); |
| 8705 | ggml_set_name(q, "q"); |
| 8706 | |
| 8707 | ggml_tensor * k = create_permuted(type_K, hsk_padded, kv, nh, nr23[1], kv_view); // the K tensor is usually a view of the K cache |
| 8708 | ggml_set_name(k, "k"); |
| 8709 | |
| 8710 | ggml_tensor * v = nullptr; |
| 8711 | if (v_is_view_of_k) { |
| 8712 | // the V cache is a sub-view of the K cache. this is used by some MLA-based models |
| 8713 | // for more info: |
| 8714 | // - https://github.com/ggml-org/llama.cpp/pull/13435 |
| 8715 | // - https://github.com/ggml-org/llama.cpp/pull/18953#issuecomment-3774948392 |
| 8716 | // - https://github.com/ggml-org/llama.cpp/pull/18986 |
| 8717 | GGML_ASSERT(type_K == type_V && hsv_padded <= hsk_padded); |
| 8718 | |
| 8719 | v = ggml_view_4d(ctx, k, hsv_padded, kv, nh, nr23[1], k->nb[1], k->nb[2], k->nb[3], 0); |
| 8720 | } else { |
| 8721 | v = create_permuted(type_V, hsv_padded, kv, nh, nr23[1], kv_view); // the V tensor is usually a view of the V cache |
| 8722 | } |
| 8723 | ggml_set_name(v, "v"); |
| 8724 | |
| 8725 | ggml_tensor * m = nullptr; |
| 8726 | if (mask) { |
| 8727 | m = ggml_new_tensor_4d(ctx, GGML_TYPE_F16, kv, nb, 1, nr23[1]); |
| 8728 | ggml_set_name(m, "m"); |
| 8729 | } |
| 8730 | |
| 8731 | ggml_tensor * s = nullptr; |
| 8732 | if (sinks) { |
| 8733 | s = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, q->ne[2]); |
| 8734 | ggml_set_name(s, "s"); |
| 8735 | } |
| 8736 | |
| 8737 | ggml_tensor * out = ggml_flash_attn_ext(ctx, q, k, v, m, 1.0f/sqrtf(hsk), max_bias, logit_softcap); |
| 8738 | ggml_flash_attn_ext_add_sinks(out, s); |
| 8739 | ggml_flash_attn_ext_set_n_kv_max(out, n_kv_max); |
| 8740 | ggml_prec_set_acc(out, prec); |
| 8741 | ggml_set_name(out, "out"); |
| 8742 | |
| 8743 | return out; |
| 8744 | } |
| 8745 | |
| 8746 | void initialize_tensors(ggml_context * ctx) override { |
| 8747 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 8748 | if (strcmp(t->name, "s") == 0) { |
| 8749 | // make the sink values more noticeable in order to trigger a test failure when the implementation is wrong |
| 8750 | init_tensor_uniform(t, -10.0f, 10.0f); |
| 8751 | } else if (strcmp(t->name, "m") == 0) { |
| 8752 | if (n_kv_max > 0) { |
| 8753 | init_tensor_kq_mask_sparse(t, n_kv_max); |
| 8754 | } else { |
| 8755 | init_tensor_kq_mask(t); |
| 8756 | } |
| 8757 | } else { |
| 8758 | init_tensor_uniform(t); |
| 8759 | } |
| 8760 | } |
| 8761 | } |
| 8762 | |
| 8763 | bool grad_precise() override { |
| 8764 | return true; |
| 8765 | } |
| 8766 | }; |
| 8767 | |
| 8768 | // large Q values, so the online softmax has to rescale the partial results |
| 8769 | struct test_flash_attn_ext_large_logits : public test_flash_attn_ext { |
| 8770 | static constexpr int q_range = 20; |
| 8771 | |
| 8772 | using test_flash_attn_ext::test_flash_attn_ext; |
| 8773 | |
| 8774 | std::string vars() override { |
| 8775 | return test_flash_attn_ext::vars() + ",q_range=" + std::to_string(q_range); |
| 8776 | } |
| 8777 | |
| 8778 | void initialize_tensors(ggml_context * ctx) override { |
| 8779 | test_flash_attn_ext::initialize_tensors(ctx); |
| 8780 | init_tensor_uniform(ggml_get_tensor(ctx, "q"), -(float) q_range, (float) q_range); |
| 8781 | } |
| 8782 | }; |
| 8783 | |
| 8784 | // GGML_OP_FLASH_ATTN_EXT with kv_rows: each q slice attends to its own list of K/V cache rows |
| 8785 | struct test_flash_attn_ext_kv_rows : public test_case { |
| 8786 | const int64_t hs; |
| 8787 | const int64_t nh; // K/V heads |
| 8788 | const int64_t gqa; |
| 8789 | const int64_t n_cells; // rows of the K/V cache |
| 8790 | const int64_t kv; // list length per slice |
| 8791 | const int64_t nb; // queries per slice |
| 8792 | const int64_t n_slices; |
| 8793 | const ggml_type type_KV; |
| 8794 | |
| 8795 | std::vector<int32_t> rows; |
| 8796 | |
| 8797 | std::string vars() override { |
| 8798 | return VARS_TO_STR8(hs, nh, gqa, n_cells, kv, nb, n_slices, type_KV); |
| 8799 | } |
| 8800 | |
| 8801 | double max_nmse_err() override { |
| 8802 | return 5e-4; |
| 8803 | } |
| 8804 | |
| 8805 | test_flash_attn_ext_kv_rows(int64_t hs = 256, int64_t nh = 2, int64_t gqa = 8, int64_t n_cells = 2048, int64_t kv = 512, |
| 8806 | int64_t nb = 8, int64_t n_slices = 4, ggml_type type_KV = GGML_TYPE_F16) |
| 8807 | : hs(hs), nh(nh), gqa(gqa), n_cells(n_cells), kv(kv), nb(nb), n_slices(n_slices), type_KV(type_KV) {} |
| 8808 | |
| 8809 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8810 | ggml_tensor * q = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, hs, nb, nh*gqa, n_slices); |
| 8811 | ggml_set_name(q, "q"); |
| 8812 | |
| 8813 | // cache layout [hs, nh, n_cells], shared by all slices |
| 8814 | ggml_tensor * k0 = ggml_new_tensor_3d(ctx, type_KV, hs, nh, n_cells); |
| 8815 | ggml_tensor * v0 = ggml_new_tensor_3d(ctx, type_KV, hs, nh, n_cells); |
| 8816 | ggml_set_name(k0, "k0"); |
| 8817 | ggml_set_name(v0, "v0"); |
| 8818 | |
| 8819 | ggml_tensor * k = ggml_permute(ctx, k0, 0, 2, 1, 3); |
| 8820 | ggml_tensor * v = ggml_permute(ctx, v0, 0, 2, 1, 3); |
| 8821 | |
| 8822 | ggml_tensor * m = ggml_new_tensor_4d(ctx, GGML_TYPE_F16, kv, nb, 1, n_slices); |
| 8823 | ggml_set_name(m, "m"); |
| 8824 | |
| 8825 | ggml_tensor * r = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, kv, n_slices); |
| 8826 | ggml_set_name(r, "r"); |
| 8827 | |
| 8828 | ggml_tensor * out = ggml_flash_attn_ext_rows(ctx, q, k, v, m, r, 1.0f/sqrtf(hs), 0.0f, 0.0f); |
| 8829 | ggml_prec_set_acc(out, GGML_PREC_F32); |
| 8830 | ggml_set_name(out, "out"); |
| 8831 | |
| 8832 | return out; |
| 8833 | } |
| 8834 | |
| 8835 | void initialize_tensors(ggml_context * ctx) override { |
| 8836 | std::mt19937 gen(0x6B76); |
| 8837 | |
| 8838 | // per slice: distinct random cells, the tail of the list is padding |
| 8839 | rows.assign(kv*n_slices, -1); |
| 8840 | std::vector<int32_t> order(n_cells); |
| 8841 | for (int64_t s = 0; s < n_slices; ++s) { |
| 8842 | for (int64_t i = 0; i < n_cells; ++i) { |
| 8843 | order[i] = i; |
| 8844 | } |
| 8845 | std::shuffle(order.begin(), order.end(), gen); |
| 8846 | const int64_t n_used = std::min<int64_t>(n_cells, kv - (s*37) % std::max<int64_t>(1, kv/2)); |
| 8847 | for (int64_t i = 0; i < n_used; ++i) { |
| 8848 | rows[s*kv + i] = order[i]; |
| 8849 | } |
| 8850 | } |
| 8851 | |
| 8852 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 8853 | if (t->view_src) { |
| 8854 | continue; |
| 8855 | } |
| 8856 | if (strcmp(t->name, "r") == 0) { |
| 8857 | ggml_backend_tensor_set(t, rows.data(), 0, rows.size()*sizeof(int32_t)); |
| 8858 | } else if (strcmp(t->name, "m") == 0) { |
| 8859 | std::vector<float> f(ggml_nelements(t)); |
| 8860 | std::uniform_real_distribution<float> dis(-1.0f, 1.0f); |
| 8861 | for (int64_t s = 0; s < n_slices; ++s) { |
| 8862 | for (int64_t j = 0; j < nb; ++j) { |
| 8863 | for (int64_t i = 0; i < kv; ++i) { |
| 8864 | const bool pad = rows[s*kv + i] < 0; |
| 8865 | const bool drop = (i + 3*j + 5*s) % 11 == 0; |
| 8866 | f[(s*nb + j)*kv + i] = pad || drop ? -INFINITY : dis(gen); |
| 8867 | } |
| 8868 | } |
| 8869 | } |
| 8870 | std::vector<ggml_fp16_t> h(f.size()); |
| 8871 | ggml_fp32_to_fp16_row(f.data(), h.data(), f.size()); |
| 8872 | ggml_backend_tensor_set(t, h.data(), 0, h.size()*sizeof(ggml_fp16_t)); |
| 8873 | } else { |
| 8874 | init_tensor_uniform(t); |
| 8875 | } |
| 8876 | } |
| 8877 | } |
| 8878 | }; |
| 8879 | |
| 8880 | // GGML_OP_CROSS_ENTROPY_LOSS |
| 8881 | struct test_cross_entropy_loss : public test_case { |
| 8882 | const ggml_type type; |
| 8883 | const std::array<int64_t, 4> ne; |
| 8884 | |
| 8885 | std::string vars() override { |
| 8886 | return VARS_TO_STR2(type, ne); |
| 8887 | } |
| 8888 | |
| 8889 | test_cross_entropy_loss(ggml_type type = GGML_TYPE_F32, |
| 8890 | std::array<int64_t, 4> ne = {10, 5, 4, 3}) |
| 8891 | : type(type), ne(ne) {} |
| 8892 | |
| 8893 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8894 | ggml_tensor * logits = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 8895 | ggml_set_param(logits); |
| 8896 | ggml_set_name(logits, "logits"); |
| 8897 | |
| 8898 | ggml_tensor * labels = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 8899 | // The labels are assumed to be constant -> no gradients. |
| 8900 | ggml_set_name(labels, "labels"); |
| 8901 | |
| 8902 | // Ensure labels add up to 1: |
| 8903 | labels = ggml_soft_max(ctx, labels); |
| 8904 | ggml_set_name(labels, "labels_normalized"); |
| 8905 | |
| 8906 | ggml_tensor * out = ggml_cross_entropy_loss(ctx, logits, labels); |
| 8907 | ggml_set_name(out, "out"); |
| 8908 | |
| 8909 | return out; |
| 8910 | } |
| 8911 | |
| 8912 | void initialize_tensors(ggml_context * ctx) override { |
| 8913 | // For larger abs. diffs between logits softmax is more linear, therefore more precise num. gradients. |
| 8914 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 8915 | init_tensor_uniform(t, -100.0f, 100.0f); |
| 8916 | } |
| 8917 | } |
| 8918 | |
| 8919 | float grad_eps() override { |
| 8920 | return 1.0f; |
| 8921 | } |
| 8922 | |
| 8923 | bool grad_precise() override { |
| 8924 | return true; |
| 8925 | } |
| 8926 | }; |
| 8927 | |
| 8928 | // GGML_OP_CROSS_ENTROPY_LOSS_BACK |
| 8929 | struct test_cross_entropy_loss_back : public test_case { |
| 8930 | const ggml_type type; |
| 8931 | const std::array<int64_t, 4> ne; |
| 8932 | |
| 8933 | std::string vars() override { |
| 8934 | return VARS_TO_STR2(type, ne); |
| 8935 | } |
| 8936 | |
| 8937 | test_cross_entropy_loss_back(ggml_type type = GGML_TYPE_F32, |
| 8938 | std::array<int64_t, 4> ne = {10, 5, 4, 3}) |
| 8939 | : type(type), ne(ne) {} |
| 8940 | |
| 8941 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8942 | ggml_tensor * grad = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1); |
| 8943 | ggml_set_name(grad, "grad"); |
| 8944 | |
| 8945 | ggml_tensor * logits = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 8946 | ggml_set_name(logits, "logits"); |
| 8947 | |
| 8948 | ggml_tensor * labels = ggml_new_tensor(ctx, type, 4, ne.data()); |
| 8949 | ggml_set_name(labels, "labels"); |
| 8950 | |
| 8951 | // Ensure labels add up to 1: |
| 8952 | labels = ggml_soft_max(ctx, labels); |
| 8953 | ggml_set_name(labels, "labels_normalized"); |
| 8954 | |
| 8955 | ggml_tensor * out = ggml_cross_entropy_loss_back(ctx, grad, logits, labels); |
| 8956 | ggml_set_name(out, "out"); |
| 8957 | |
| 8958 | return out; |
| 8959 | } |
| 8960 | }; |
| 8961 | |
| 8962 | // GGML_OP_OPT_STEP_ADAMW |
| 8963 | struct test_opt_step_adamw : public test_case { |
| 8964 | const ggml_type type; |
| 8965 | const std::array<int64_t, 4> ne; |
| 8966 | |
| 8967 | std::string vars() override { |
| 8968 | return VARS_TO_STR2(type, ne); |
| 8969 | } |
| 8970 | |
| 8971 | test_opt_step_adamw(ggml_type type = GGML_TYPE_F32, |
| 8972 | std::array<int64_t, 4> ne = {10, 5, 4, 3}) |
| 8973 | : type(type), ne(ne) {} |
| 8974 | |
| 8975 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 8976 | ggml_tensor * a = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]); |
| 8977 | ggml_set_param(a); // Despite tensor a having gradients the output tensor will not. |
| 8978 | ggml_set_name(a, "a"); |
| 8979 | |
| 8980 | ggml_tensor * grad = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]); |
| 8981 | ggml_set_name(grad, "grad"); |
| 8982 | |
| 8983 | ggml_tensor * grad_m = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]); |
| 8984 | ggml_set_name(grad_m, "grad_m"); |
| 8985 | |
| 8986 | ggml_tensor * grad_v = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]); |
| 8987 | ggml_set_name(grad_v, "grad_v"); |
| 8988 | |
| 8989 | ggml_tensor * adamw_params = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 7); |
| 8990 | ggml_set_name(adamw_params, "adamw_params"); |
| 8991 | |
| 8992 | ggml_tensor * out = ggml_opt_step_adamw(ctx, a, grad, grad_m, grad_v, adamw_params); |
| 8993 | ggml_set_name(out, "out"); |
| 8994 | |
| 8995 | return out; |
| 8996 | } |
| 8997 | |
| 8998 | void initialize_tensors(ggml_context * ctx) override { |
| 8999 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 9000 | init_tensor_uniform(t, 0.0f, 1.0f); // grad_v and adamw_params need non-negative values. |
| 9001 | } |
| 9002 | } |
| 9003 | |
| 9004 | bool grad_precise() override { |
| 9005 | return true; |
| 9006 | } |
| 9007 | }; |
| 9008 | |
| 9009 | // GGML_OP_OPT_STEP_SGD |
| 9010 | struct test_opt_step_sgd : public test_case { |
| 9011 | const ggml_type type; |
| 9012 | const std::array<int64_t, 4> ne; |
| 9013 | |
| 9014 | std::string vars() override { return VARS_TO_STR2(type, ne); } |
| 9015 | |
| 9016 | test_opt_step_sgd(ggml_type type = GGML_TYPE_F32, |
| 9017 | std::array<int64_t, 4> ne = { 10, 5, 4, 3 }) |
| 9018 | : type(type), ne(ne) {} |
| 9019 | |
| 9020 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 9021 | ggml_tensor * a = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]); |
| 9022 | ggml_set_param(a); // Despite tensor a having gradients the output tensor will not. |
| 9023 | ggml_set_name(a, "a"); |
| 9024 | |
| 9025 | ggml_tensor * grad = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]); |
| 9026 | ggml_set_name(grad, "grad"); |
| 9027 | |
| 9028 | ggml_tensor * sgd_params = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 2); |
| 9029 | ggml_set_name(sgd_params, "sgd_params"); |
| 9030 | |
| 9031 | ggml_tensor * out = ggml_opt_step_sgd(ctx, a, grad, sgd_params); |
| 9032 | |
| 9033 | ggml_set_name(out, "out"); |
| 9034 | |
| 9035 | return out; |
| 9036 | } |
| 9037 | |
| 9038 | void initialize_tensors(ggml_context * ctx) override { |
| 9039 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 9040 | init_tensor_uniform(t, 0.0f, 1.0f); // sgd_params need non-negative values. |
| 9041 | } |
| 9042 | } |
| 9043 | |
| 9044 | bool grad_precise() override { |
| 9045 | return true; |
| 9046 | } |
| 9047 | }; |
| 9048 | |
| 9049 | // GGML_OP_CUMSUM |
| 9050 | struct test_cumsum : public test_case { |
| 9051 | const ggml_type type; |
| 9052 | const std::array<int64_t, 4> ne; |
| 9053 | |
| 9054 | std::string vars() override { return VARS_TO_STR2(type, ne); } |
| 9055 | |
| 9056 | test_cumsum(ggml_type type = GGML_TYPE_F32, |
| 9057 | std::array<int64_t, 4> ne = { 10, 5, 4, 3 }) |
| 9058 | : type(type), ne(ne) {} |
| 9059 | |
| 9060 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 9061 | ggml_tensor * a = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]); |
| 9062 | ggml_set_param(a); |
| 9063 | ggml_set_name(a, "a"); |
| 9064 | |
| 9065 | ggml_tensor * out = ggml_cumsum(ctx, a); |
| 9066 | |
| 9067 | ggml_set_name(out, "out"); |
| 9068 | |
| 9069 | return out; |
| 9070 | } |
| 9071 | |
| 9072 | void initialize_tensors(ggml_context * ctx) override { |
| 9073 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 9074 | init_tensor_uniform(t, -1.0f, 1.0f); |
| 9075 | } |
| 9076 | } |
| 9077 | }; |
| 9078 | |
| 9079 | // GGML_OP_XIELU |
| 9080 | struct test_xielu : public test_case { |
| 9081 | const ggml_type type; |
| 9082 | const std::array<int64_t, 4> ne; |
| 9083 | |
| 9084 | std::string vars() override { return VARS_TO_STR2(type, ne); } |
| 9085 | |
| 9086 | test_xielu(ggml_type type = GGML_TYPE_F32, |
| 9087 | std::array<int64_t, 4> ne = { 10, 5, 4, 3 }) |
| 9088 | : type(type), ne(ne) {} |
| 9089 | |
| 9090 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 9091 | ggml_tensor * a = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]); |
| 9092 | ggml_set_param(a); |
| 9093 | ggml_set_name(a, "a"); |
| 9094 | |
| 9095 | float alpha_n = 4.0f; |
| 9096 | float alpha_p = 20.0f; |
| 9097 | float beta = 0.5f; |
| 9098 | float eps = 0.0000001f; |
| 9099 | |
| 9100 | ggml_tensor * out = ggml_xielu(ctx, a, alpha_n, alpha_p, beta, eps); |
| 9101 | |
| 9102 | ggml_set_name(out, "out"); |
| 9103 | |
| 9104 | return out; |
| 9105 | } |
| 9106 | |
| 9107 | void initialize_tensors(ggml_context * ctx) override { |
| 9108 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 9109 | init_tensor_uniform(t, -1.0f, 1.0f); |
| 9110 | } |
| 9111 | } |
| 9112 | }; |
| 9113 | |
| 9114 | // GGML_OP_TRI |
| 9115 | struct test_tri : public test_case { |
| 9116 | const ggml_type type; |
| 9117 | const std::array<int64_t, 4> ne; |
| 9118 | const ggml_tri_type tri_type; |
| 9119 | |
| 9120 | std::string vars() override { return VARS_TO_STR3(type, ne, tri_type); } |
| 9121 | |
| 9122 | test_tri(ggml_tri_type tri_type, ggml_type type = GGML_TYPE_F32, |
| 9123 | std::array<int64_t, 4> ne = { 10, 10, 4, 3 }) |
| 9124 | : type(type), ne(ne), tri_type(tri_type) { |
| 9125 | GGML_ASSERT(ne[0] == ne[1]); |
| 9126 | } |
| 9127 | |
| 9128 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 9129 | ggml_tensor * a = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]); |
| 9130 | ggml_set_param(a); |
| 9131 | ggml_set_name(a, "a"); |
| 9132 | |
| 9133 | ggml_tensor * out = ggml_tri(ctx, a, tri_type); |
| 9134 | |
| 9135 | ggml_set_name(out, "out"); |
| 9136 | |
| 9137 | return out; |
| 9138 | } |
| 9139 | |
| 9140 | void initialize_tensors(ggml_context * ctx) override { |
| 9141 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 9142 | init_tensor_uniform(t, -1.0f, 1.0f); |
| 9143 | } |
| 9144 | } |
| 9145 | }; |
| 9146 | |
| 9147 | // GGML_OP_FILL |
| 9148 | struct test_fill : public test_case { |
| 9149 | const ggml_type type; |
| 9150 | const std::array<int64_t, 4> ne; |
| 9151 | float c; |
| 9152 | |
| 9153 | std::string vars() override { return VARS_TO_STR3(type, ne, c); } |
| 9154 | |
| 9155 | test_fill(float c, ggml_type type = GGML_TYPE_F32, |
| 9156 | std::array<int64_t, 4> ne = { 10, 10, 4, 3 }) |
| 9157 | : type(type), ne(ne), c(c) {} |
| 9158 | |
| 9159 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 9160 | ggml_tensor * a = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]); |
| 9161 | ggml_set_param(a); |
| 9162 | ggml_set_name(a, "a"); |
| 9163 | |
| 9164 | ggml_tensor * out = ggml_fill(ctx, a, c); |
| 9165 | |
| 9166 | ggml_set_name(out, "out"); |
| 9167 | |
| 9168 | return out; |
| 9169 | } |
| 9170 | }; |
| 9171 | |
| 9172 | // GGML_OP_SOLVE_TRI |
| 9173 | struct test_solve_tri : public test_case { |
| 9174 | const ggml_type type; |
| 9175 | const std::array<int64_t, 4> ne_lhs; |
| 9176 | const std::array<int64_t, 4> ne_rhs; |
| 9177 | |
| 9178 | std::string vars() override { return VARS_TO_STR3(type, ne_lhs, ne_rhs); } |
| 9179 | |
| 9180 | uint64_t op_flops(ggml_tensor * t) override { |
| 9181 | GGML_UNUSED(t); |
| 9182 | int64_t n = ne_lhs[0]; |
| 9183 | int64_t k = ne_rhs[0]; |
| 9184 | int64_t batch = ne_lhs[2] * ne_lhs[3]; |
| 9185 | // n * (n + 1) / 2 non-zero elements of lhs, 2 flops each, for each col of rhs |
| 9186 | return n * (n + 1) * k * batch; |
| 9187 | } |
| 9188 | |
| 9189 | test_solve_tri(ggml_type type = GGML_TYPE_F32, |
| 9190 | std::array<int64_t, 4> ne_lhs = { 10, 10, 4, 3 }, |
| 9191 | std::array<int64_t, 4> ne_rhs = { 3, 10, 4, 3 } |
| 9192 | ) |
| 9193 | : type(type), ne_lhs(ne_lhs), ne_rhs(ne_rhs) {} |
| 9194 | |
| 9195 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 9196 | ggml_tensor * a = ggml_new_tensor_4d(ctx, type, ne_lhs[0], ne_lhs[1], ne_lhs[2], ne_lhs[3]); |
| 9197 | ggml_set_param(a); |
| 9198 | ggml_set_name(a, "a"); |
| 9199 | |
| 9200 | ggml_tensor * b = ggml_new_tensor_4d(ctx, type, ne_rhs[0], ne_rhs[1], ne_rhs[2], ne_rhs[3]); |
| 9201 | ggml_set_param(b); |
| 9202 | ggml_set_name(b, "b"); |
| 9203 | |
| 9204 | ggml_tensor * out = ggml_solve_tri(ctx, a, b, true, true, false); |
| 9205 | ggml_set_name(out, "out"); |
| 9206 | |
| 9207 | return out; |
| 9208 | } |
| 9209 | |
| 9210 | void initialize_tensors(ggml_context * ctx) override { |
| 9211 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 9212 | if (strcmp(t->name, "a") == 0) { |
| 9213 | // note: avoid zeros in the diagonal |
| 9214 | init_tensor_tril(t, 0.1, 1.0f); |
| 9215 | } else { |
| 9216 | init_tensor_uniform(t, -1.0f, 1.0f); |
| 9217 | } |
| 9218 | } |
| 9219 | } |
| 9220 | }; |
| 9221 | |
| 9222 | // GGML_OP_DIAG |
| 9223 | struct test_diag : public test_case { |
| 9224 | const ggml_type type; |
| 9225 | const std::array<int64_t, 4> ne; |
| 9226 | |
| 9227 | std::string vars() override { return VARS_TO_STR2(type, ne); } |
| 9228 | |
| 9229 | test_diag(ggml_type type = GGML_TYPE_F32, |
| 9230 | std::array<int64_t, 4> ne = { 10, 1, 4, 3 }) |
| 9231 | : type(type), ne(ne) {} |
| 9232 | |
| 9233 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 9234 | GGML_ASSERT(ne[1] == 1); |
| 9235 | ggml_tensor * a = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]); |
| 9236 | ggml_set_param(a); |
| 9237 | ggml_set_name(a, "a"); |
| 9238 | |
| 9239 | ggml_tensor * out = ggml_diag(ctx, a); |
| 9240 | ggml_set_name(out, "out"); |
| 9241 | |
| 9242 | return out; |
| 9243 | } |
| 9244 | }; |
| 9245 | |
| 9246 | // GGML_OP_LIGHTNING_INDEXER |
| 9247 | struct test_lightning_indexer : public test_case { |
| 9248 | const int64_t hsk; // indexer K head size |
| 9249 | const int64_t nh; // num indexer heads |
| 9250 | const int64_t kv; // kv size |
| 9251 | const int64_t nb; // batch size |
| 9252 | const int64_t ns; // num streams |
| 9253 | const int64_t nm; // ne[3] of mask |
| 9254 | |
| 9255 | const ggml_type type_K; |
| 9256 | |
| 9257 | std::string vars() override { |
| 9258 | return VARS_TO_STR7(hsk, nh, kv, nb, ns, nm, type_K); |
| 9259 | } |
| 9260 | |
| 9261 | double max_nmse_err() override { |
| 9262 | return 1e-6; |
| 9263 | } |
| 9264 | |
| 9265 | uint64_t op_flops(ggml_tensor * t) override { |
| 9266 | GGML_UNUSED(t); |
| 9267 | return ((2 * hsk + 2) * nh + 1) * kv * nb * ns; |
| 9268 | } |
| 9269 | |
| 9270 | test_lightning_indexer(int64_t hsk = 128, int64_t nh = 64, int64_t kv = 256, int64_t nb = 128, int64_t ns = 1, int64_t nm = 1, ggml_type type_K = GGML_TYPE_F16) |
| 9271 | : hsk(hsk), nh(nh), kv(kv), nb(nb), ns(ns), nm(nm), type_K(type_K) {} |
| 9272 | |
| 9273 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 9274 | ggml_tensor * q = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, hsk, nh, nb, ns); |
| 9275 | ggml_set_param(q); |
| 9276 | ggml_set_name(q, "q"); |
| 9277 | |
| 9278 | ggml_tensor * k = ggml_new_tensor_4d(ctx, type_K, hsk, 1, kv, ns); |
| 9279 | ggml_set_param(k); |
| 9280 | ggml_set_name(k, "k"); |
| 9281 | |
| 9282 | ggml_tensor * w = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, nh, nb, 1, ns); |
| 9283 | ggml_set_param(w); |
| 9284 | ggml_set_name(w, "w"); |
| 9285 | |
| 9286 | ggml_tensor * m = ggml_new_tensor_4d(ctx, GGML_TYPE_F16, kv, nb, 1, nm); |
| 9287 | ggml_set_param(m); |
| 9288 | ggml_set_name(m, "m"); |
| 9289 | |
| 9290 | ggml_tensor * out = ggml_lightning_indexer(ctx, q, k, w, m); |
| 9291 | ggml_set_name(out, "out"); |
| 9292 | |
| 9293 | return out; |
| 9294 | } |
| 9295 | |
| 9296 | void initialize_tensors(ggml_context * ctx) override { |
| 9297 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 9298 | if (strcmp(t->name, "m") == 0) { |
| 9299 | init_tensor_kq_mask(t); |
| 9300 | } else { |
| 9301 | init_tensor_uniform(t); |
| 9302 | } |
| 9303 | } |
| 9304 | } |
| 9305 | }; |
| 9306 | |
| 9307 | // GGML_OP_TOP_K_MASK |
| 9308 | struct test_top_k_mask : public test_case { |
| 9309 | const ggml_type type; |
| 9310 | const int64_t n_kv; |
| 9311 | const int64_t n_blk; // 0: no blocks, a pick is a cell |
| 9312 | const int64_t n_pick; // number of blocks, ignored without blocks |
| 9313 | const int64_t n_top; |
| 9314 | const int64_t n_extra; // 0: no extra cells |
| 9315 | const bool score; // false: every pick is live |
| 9316 | const int64_t nb; // rows per stream |
| 9317 | const int64_t nh; // rows of top_k per mask row, broadcast over ne2 |
| 9318 | const int64_t ns; // streams |
| 9319 | const bool real; // blocks of consecutive cells, live picks, as in a real cache |
| 9320 | const bool bcast; // one mask row for all rows and streams |
| 9321 | |
| 9322 | std::string vars() override { |
| 9323 | return VARS_TO_STR12(type, n_kv, n_blk, n_pick, n_top, n_extra, score, nb, nh, ns, real, bcast); |
| 9324 | } |
| 9325 | |
| 9326 | // the result is a copy of the mask or -inf, the harness checks the -inf pattern, the finite values must match exactly |
| 9327 | double max_err() override { |
| 9328 | return 0.0; |
| 9329 | } |
| 9330 | |
| 9331 | double err(const float * a, const float * b, size_t n) override { |
| 9332 | double result = 0.0; |
| 9333 | for (size_t i = 0; i < n; ++i) { |
| 9334 | if (std::isfinite(a[i]) && std::isfinite(b[i])) { |
| 9335 | result = std::max(result, (double) fabsf(a[i] - b[i])); |
| 9336 | } |
| 9337 | } |
| 9338 | return result; |
| 9339 | } |
| 9340 | |
| 9341 | size_t op_size(ggml_tensor * t) override { |
| 9342 | const size_t ts = ggml_type_size(type); |
| 9343 | const size_t nc = std::max<int64_t>(n_blk, 1); |
| 9344 | return ggml_nbytes(t) + nb*nh*ns*(n_top*(sizeof(int32_t) + (score ? sizeof(float) : 0) + nc*(sizeof(int32_t) + 2*ts)) + n_extra*(sizeof(int32_t) + 2*ts)); |
| 9345 | } |
| 9346 | |
| 9347 | test_top_k_mask(ggml_type type = GGML_TYPE_F16, int64_t n_kv = 256, int64_t n_blk = 4, int64_t n_pick = 64, |
| 9348 | int64_t n_top = 16, int64_t n_extra = 3, bool score = true, int64_t nb = 32, int64_t nh = 1, int64_t ns = 1, bool real = false, |
| 9349 | bool bcast = false) |
| 9350 | : type(type), n_kv(n_kv), n_blk(n_blk), n_pick(n_blk ? n_pick : n_kv), n_top(n_top), n_extra(n_extra), score(score), |
| 9351 | nb(nb), nh(nh), ns(ns), real(real), bcast(bcast) {} |
| 9352 | |
| 9353 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 9354 | ggml_tensor * m = bcast ? ggml_new_tensor_1d(ctx, type, n_kv) : ggml_new_tensor_4d(ctx, type, n_kv, nb, 1, ns); |
| 9355 | ggml_set_name(m, "m"); |
| 9356 | |
| 9357 | ggml_tensor * top_k = ggml_new_tensor_4d(ctx, GGML_TYPE_I32, n_top, nb, nh, ns); |
| 9358 | ggml_set_name(top_k, "top_k"); |
| 9359 | |
| 9360 | ggml_tensor * s = nullptr; |
| 9361 | if (score) { |
| 9362 | s = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, n_pick, nb, nh, ns); |
| 9363 | ggml_set_name(s, "score"); |
| 9364 | } |
| 9365 | |
| 9366 | ggml_tensor * blocks = nullptr; |
| 9367 | if (n_blk > 0) { |
| 9368 | blocks = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_blk, n_pick); |
| 9369 | ggml_set_name(blocks, "blocks"); |
| 9370 | } |
| 9371 | |
| 9372 | ggml_tensor * extra = nullptr; |
| 9373 | if (n_extra > 0) { |
| 9374 | extra = ggml_new_tensor_4d(ctx, GGML_TYPE_I32, n_extra, nb, nh, ns); |
| 9375 | ggml_set_name(extra, "extra"); |
| 9376 | } |
| 9377 | |
| 9378 | ggml_tensor * out = ggml_top_k_mask(ctx, m, top_k, s, blocks, extra); |
| 9379 | ggml_set_name(out, "out"); |
| 9380 | |
| 9381 | return out; |
| 9382 | } |
| 9383 | |
| 9384 | void initialize_tensors(ggml_context * ctx) override { |
| 9385 | std::mt19937 gen(0x4B50); |
| 9386 | |
| 9387 | // cells and picks sometimes fall out of range, the op skips them |
| 9388 | auto init_cells = [&](ggml_tensor * t) { |
| 9389 | std::vector<int32_t> data(ggml_nelements(t)); |
| 9390 | if (real) { |
| 9391 | // block p holds cells p*n_blk..p*n_blk + n_blk - 1, the extra cells are the last cells |
| 9392 | const bool is_blocks = strcmp(t->name, "blocks") == 0; |
| 9393 | for (int64_t i = 0; i < (int64_t) data.size(); ++i) { |
| 9394 | const int64_t c = is_blocks ? i : n_kv - 1 - i % t->ne[0]; |
| 9395 | data[i] = c < n_kv ? (int32_t) c : (int32_t) n_kv; |
| 9396 | } |
| 9397 | } else { |
| 9398 | std::uniform_int_distribution<int32_t> cell(0, n_kv - 1); |
| 9399 | for (auto & c : data) { |
| 9400 | const uint32_t r = gen() % 16; |
| 9401 | c = r == 0 ? (int32_t) n_kv : r == 1 ? -1 : cell(gen); |
| 9402 | } |
| 9403 | } |
| 9404 | ggml_backend_tensor_set(t, data.data(), 0, data.size()*sizeof(int32_t)); |
| 9405 | }; |
| 9406 | |
| 9407 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 9408 | if (strcmp(t->name, "m") == 0) { |
| 9409 | if (type == GGML_TYPE_F16) { |
| 9410 | init_tensor_kq_mask(t); |
| 9411 | } else { |
| 9412 | std::vector<float> data(ggml_nelements(t)); |
| 9413 | std::uniform_real_distribution<float> dis(-1.0f, 1.0f); |
| 9414 | for (auto & v : data) { |
| 9415 | v = gen() % 4 == 0 ? -INFINITY : dis(gen); |
| 9416 | } |
| 9417 | ggml_backend_tensor_set(t, data.data(), 0, data.size()*sizeof(float)); |
| 9418 | } |
| 9419 | } else if (strcmp(t->name, "score") == 0) { |
| 9420 | // -inf marks the picks a row cannot see |
| 9421 | std::vector<float> data(ggml_nelements(t)); |
| 9422 | std::uniform_real_distribution<float> dis(-1.0f, 1.0f); |
| 9423 | for (auto & v : data) { |
| 9424 | v = !real && gen() % 4 == 0 ? -INFINITY : dis(gen); |
| 9425 | } |
| 9426 | ggml_backend_tensor_set(t, data.data(), 0, data.size()*sizeof(float)); |
| 9427 | } else if (strcmp(t->name, "top_k") == 0) { |
| 9428 | std::vector<int32_t> data(ggml_nelements(t)); |
| 9429 | std::vector<int32_t> order(n_pick); |
| 9430 | for (int64_t r = 0; r < ggml_nrows(t); ++r) { |
| 9431 | for (int64_t i = 0; i < n_pick; ++i) { |
| 9432 | order[i] = i; |
| 9433 | } |
| 9434 | std::shuffle(order.begin(), order.end(), gen); |
| 9435 | for (int64_t j = 0; j < n_top; ++j) { |
| 9436 | const bool out = !real && j == 0 && r % 5 == 0; |
| 9437 | data[r*n_top + j] = out ? (r % 2 ? -1 : (int32_t) n_pick) : order[j % n_pick]; |
| 9438 | } |
| 9439 | } |
| 9440 | ggml_backend_tensor_set(t, data.data(), 0, data.size()*sizeof(int32_t)); |
| 9441 | } else if (strcmp(t->name, "blocks") == 0 || strcmp(t->name, "extra") == 0) { |
| 9442 | init_cells(t); |
| 9443 | } else { |
| 9444 | // the harness sentinels and the output, the callback checks the sentinels for NaN |
| 9445 | init_tensor_uniform(t); |
| 9446 | } |
| 9447 | } |
| 9448 | } |
| 9449 | }; |
| 9450 | |
| 9451 | // Deserializable generic test case |
| 9452 | struct input_tensor { |
| 9453 | ggml_type type; |
| 9454 | std::array<int64_t, 4> ne; |
| 9455 | std::array<size_t, 4> nb; // strides (0 = use default contiguous strides) |
| 9456 | }; |
| 9457 | |
| 9458 | static bool is_non_contiguous(const input_tensor & src) { |
| 9459 | if (src.nb[0] == 0) { |
| 9460 | return false; |
| 9461 | } |
| 9462 | const size_t default_nb0 = ggml_type_size(src.type); |
| 9463 | const size_t default_nb1 = default_nb0 * (src.ne[0] / ggml_blck_size(src.type)); |
| 9464 | const size_t default_nb2 = default_nb1 * src.ne[1]; |
| 9465 | const size_t default_nb3 = default_nb2 * src.ne[2]; |
| 9466 | return src.nb[0] != default_nb0 || |
| 9467 | src.nb[1] != default_nb1 || |
| 9468 | src.nb[2] != default_nb2 || |
| 9469 | src.nb[3] != default_nb3; |
| 9470 | } |
| 9471 | |
| 9472 | static std::string var_to_str(const std::vector<input_tensor>& sources) { |
| 9473 | std::ostringstream oss; |
| 9474 | bool first = true; |
| 9475 | for (const auto& src : sources) { |
| 9476 | if (!first) oss << ","; |
| 9477 | oss << ggml_type_name(src.type) << "[" << src.ne[0] << "," << src.ne[1] << "," << src.ne[2] << "," << src.ne[3] << "]"; |
| 9478 | if (is_non_contiguous(src)) { |
| 9479 | oss << "nb[" << src.nb[0] << "," << src.nb[1] << "," << src.nb[2] << "," << src.nb[3] << "]"; |
| 9480 | } |
| 9481 | first = false; |
| 9482 | } |
| 9483 | return oss.str(); |
| 9484 | } |
| 9485 | |
| 9486 | static std::string var_to_str(const std::array<int32_t, GGML_MAX_OP_PARAMS / sizeof(int32_t)>& params) { |
| 9487 | std::ostringstream oss; |
| 9488 | oss << "["; |
| 9489 | bool first = true; |
| 9490 | for (size_t i = 0; i < params.size(); ++i) { |
| 9491 | if (params[i] != 0) { |
| 9492 | if (!first) oss << ","; |
| 9493 | oss << i << ":" << params[i]; |
| 9494 | first = false; |
| 9495 | } |
| 9496 | } |
| 9497 | oss << "]"; |
| 9498 | return oss.str(); |
| 9499 | } |
| 9500 | |
| 9501 | |
| 9502 | struct test_generic_op : public test_case { |
| 9503 | const ggml_op op; |
| 9504 | const ggml_type type; |
| 9505 | const std::array<int64_t, 4> ne; |
| 9506 | const std::array<int32_t, GGML_MAX_OP_PARAMS / sizeof(int32_t)> op_params; |
| 9507 | |
| 9508 | const std::vector<input_tensor> sources; |
| 9509 | const std::string name; |
| 9510 | |
| 9511 | std::string vars() override { |
| 9512 | if (name.empty()) { |
| 9513 | return VARS_TO_STR4(type, ne, op_params, sources); |
| 9514 | } |
| 9515 | |
| 9516 | return VARS_TO_STR5(name, type, ne, op_params, sources); |
| 9517 | } |
| 9518 | |
| 9519 | test_generic_op(ggml_op op, ggml_type type, std::array<int64_t, 4> ne, |
| 9520 | std::array<int32_t, GGML_MAX_OP_PARAMS / sizeof(int32_t)> op_params, |
| 9521 | std::vector<input_tensor> sources, std::string name = "") |
| 9522 | : op(op), type(type), ne(ne), op_params(op_params), sources(sources), name(std::move(name)) {} |
| 9523 | |
| 9524 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 9525 | const size_t source_count = std::min(sources.size(), (size_t)GGML_MAX_SRC); |
| 9526 | |
| 9527 | std::array<ggml_tensor *, GGML_MAX_SRC> source_tensors; |
| 9528 | for (size_t i = 0; i < source_count; ++i) { |
| 9529 | const input_tensor& src = sources[i]; |
| 9530 | |
| 9531 | if (is_non_contiguous(src)) { |
| 9532 | size_t total_size; |
| 9533 | const size_t blck_size = ggml_blck_size(src.type); |
| 9534 | if (blck_size == 1) { |
| 9535 | total_size = ggml_type_size(src.type); |
| 9536 | for (int d = 0; d < 4; d++) { |
| 9537 | total_size += (src.ne[d] - 1) * src.nb[d]; |
| 9538 | } |
| 9539 | } else { |
| 9540 | total_size = src.ne[0] * src.nb[0] / blck_size; |
| 9541 | for (int d = 1; d < 4; d++) { |
| 9542 | total_size += (src.ne[d] - 1) * src.nb[d]; |
| 9543 | } |
| 9544 | } |
| 9545 | |
| 9546 | // Convert bytes to elements, padded to block size for quantized types |
| 9547 | const size_t type_size = ggml_type_size(src.type); |
| 9548 | size_t backing_elements = (total_size * blck_size + type_size - 1) / type_size; |
| 9549 | backing_elements = ((backing_elements + blck_size - 1) / blck_size) * blck_size; |
| 9550 | ggml_tensor * backing = ggml_new_tensor_1d(ctx, src.type, backing_elements); |
| 9551 | source_tensors[i] = ggml_view_4d(ctx, backing, |
| 9552 | src.ne[0], src.ne[1], src.ne[2], src.ne[3], |
| 9553 | src.nb[1], src.nb[2], src.nb[3], 0); |
| 9554 | // nb[0] does not get set by view_4d, so set it manually |
| 9555 | source_tensors[i]->nb[0] = src.nb[0]; |
| 9556 | } else { |
| 9557 | source_tensors[i] = ggml_new_tensor_4d(ctx, src.type, src.ne[0], src.ne[1], src.ne[2], src.ne[3]); |
| 9558 | } |
| 9559 | } |
| 9560 | |
| 9561 | // Ops with an inplace flag create a view of src[0] as their output. |
| 9562 | bool inplace = false; |
| 9563 | if (op == GGML_OP_SET || op == GGML_OP_ACC) { |
| 9564 | inplace = op_params[4] != 0; |
| 9565 | } else if (op == GGML_OP_ADD_REL_POS) { |
| 9566 | inplace = op_params[0] != 0; |
| 9567 | } |
| 9568 | |
| 9569 | ggml_tensor * out; |
| 9570 | if (inplace && source_count > 0) { |
| 9571 | out = ggml_view_tensor(ctx, source_tensors[0]); |
| 9572 | } else { |
| 9573 | out = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]); |
| 9574 | } |
| 9575 | out->op = op; |
| 9576 | for (size_t i = 0; i < source_count; ++i) { |
| 9577 | out->src[i] = source_tensors[i]; |
| 9578 | } |
| 9579 | |
| 9580 | memcpy(out->op_params, op_params.data(), GGML_MAX_OP_PARAMS); |
| 9581 | ggml_set_name(out, "out"); |
| 9582 | |
| 9583 | return out; |
| 9584 | } |
| 9585 | |
| 9586 | double max_nmse_err() override { |
| 9587 | switch (op) { |
| 9588 | case GGML_OP_MUL_MAT: |
| 9589 | case GGML_OP_MUL_MAT_ID: |
| 9590 | case GGML_OP_OUT_PROD: |
| 9591 | case GGML_OP_CONV_TRANSPOSE_2D: |
| 9592 | case GGML_OP_IM2COL: |
| 9593 | case GGML_OP_CONV_2D: |
| 9594 | case GGML_OP_CONV_3D: |
| 9595 | case GGML_OP_SET_ROWS: |
| 9596 | case GGML_OP_CPY: |
| 9597 | return 5e-4; |
| 9598 | case GGML_OP_SOFT_MAX: |
| 9599 | return 1e-6; |
| 9600 | case GGML_OP_RWKV_WKV7: |
| 9601 | return 5e-3; |
| 9602 | case GGML_OP_FLASH_ATTN_EXT: |
| 9603 | { |
| 9604 | // Scale error with kv length to account for accumulating floating point error |
| 9605 | const int64_t kv = sources[1].ne[1]; |
| 9606 | return 5e-4 * std::max(1.0, kv / 20000.0); |
| 9607 | } |
| 9608 | default: |
| 9609 | return 1e-7; |
| 9610 | } |
| 9611 | } |
| 9612 | |
| 9613 | void initialize_tensors(ggml_context * ctx) override { |
| 9614 | ggml_tensor * out = ggml_get_tensor(ctx, "out"); |
| 9615 | |
| 9616 | std::random_device rd; |
| 9617 | std::default_random_engine rng(rd()); |
| 9618 | |
| 9619 | for (size_t i = 0; i < sources.size() && i < GGML_MAX_SRC; i++) { |
| 9620 | ggml_tensor * t = out->src[i]; |
| 9621 | if (!t) { |
| 9622 | break; |
| 9623 | } |
| 9624 | |
| 9625 | // FLASH_ATTN_EXT: src[3] is the KQ mask |
| 9626 | if (op == GGML_OP_FLASH_ATTN_EXT && i == 3) { |
| 9627 | init_tensor_kq_mask(t); |
| 9628 | continue; |
| 9629 | } |
| 9630 | |
| 9631 | if (t->type == GGML_TYPE_I32 || t->type == GGML_TYPE_I64) { |
| 9632 | if (op == GGML_OP_GET_ROWS || op == GGML_OP_GET_ROWS_BACK) { |
| 9633 | const int64_t num_rows = sources[0].ne[1]; |
| 9634 | const int64_t nels = ggml_nelements(t); |
| 9635 | std::vector<int32_t> data(nels); |
| 9636 | std::uniform_int_distribution<int32_t> dist(0, num_rows - 1); |
| 9637 | for (int64_t i = 0; i < nels; i++) { |
| 9638 | data[i] = dist(rng); |
| 9639 | } |
| 9640 | ggml_backend_tensor_set(t, data.data(), 0, nels * sizeof(int32_t)); |
| 9641 | } else if (op == GGML_OP_SET_ROWS) { |
| 9642 | init_set_rows_row_ids(t, ne[1]); |
| 9643 | } else if (op == GGML_OP_ROPE) { |
| 9644 | const int mode = op_params[2]; |
| 9645 | const int64_t nels = (mode & GGML_ROPE_TYPE_MROPE) ? ne[2] * 4 : ne[2]; |
| 9646 | std::vector<int32_t> data(nels); |
| 9647 | std::uniform_int_distribution<int32_t> dist(0, ne[2] - 1); |
| 9648 | for (int64_t i = 0; i < nels; i++) { |
| 9649 | data[i] = dist(rng); |
| 9650 | } |
| 9651 | ggml_backend_tensor_set(t, data.data(), 0, nels * sizeof(int32_t)); |
| 9652 | } else if (op == GGML_OP_MUL_MAT_ID || op == GGML_OP_ADD_ID) { |
| 9653 | const int64_t n_expert = (op == GGML_OP_MUL_MAT_ID) ? sources[0].ne[2] : sources[1].ne[1]; |
| 9654 | for (int64_t r = 0; r < ggml_nrows(t); r++) { |
| 9655 | std::vector<int32_t> data(t->ne[0]); |
| 9656 | for (int32_t i = 0; i < t->ne[0]; i++) { |
| 9657 | data[i] = i % n_expert; |
| 9658 | } |
| 9659 | std::shuffle(data.begin(), data.end(), rng); |
| 9660 | ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(int32_t)); |
| 9661 | } |
| 9662 | } else if (op == GGML_OP_SSM_SCAN) { |
| 9663 | for (int64_t r = 0; r < ggml_nrows(t); r++) { |
| 9664 | std::vector<int32_t> data(t->ne[0]); |
| 9665 | for (int32_t i = 0; i < t->ne[0]; i++) { |
| 9666 | data[i] = i; |
| 9667 | } |
| 9668 | std::shuffle(data.begin(), data.end(), rng); |
| 9669 | ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(int32_t)); |
| 9670 | } |
| 9671 | } else { |
| 9672 | init_tensor_uniform(t); |
| 9673 | } |
| 9674 | } else { |
| 9675 | init_tensor_uniform(t); |
| 9676 | } |
| 9677 | } |
| 9678 | } |
| 9679 | }; |
| 9680 | |
| 9681 | |
| 9682 | enum llm_norm_type { |
| 9683 | LLM_NORM, |
| 9684 | LLM_NORM_RMS, |
| 9685 | }; |
| 9686 | |
| 9687 | struct llama_hparams { |
| 9688 | uint32_t n_vocab; |
| 9689 | uint32_t n_embd; |
| 9690 | uint32_t n_head; |
| 9691 | uint32_t n_head_kv; |
| 9692 | static constexpr uint32_t n_layer = 1; |
| 9693 | uint32_t n_rot; |
| 9694 | uint32_t n_embd_head; // dimension of values (d_v) |
| 9695 | uint32_t n_ff; |
| 9696 | |
| 9697 | float f_norm_eps; |
| 9698 | float f_norm_rms_eps; |
| 9699 | |
| 9700 | // cparams |
| 9701 | static constexpr uint32_t n_ctx = 512; // user-specified context size |
| 9702 | static constexpr uint32_t n_ctx_orig = n_ctx; |
| 9703 | |
| 9704 | // batch |
| 9705 | int32_t n_tokens; |
| 9706 | |
| 9707 | // llm_build_context |
| 9708 | static constexpr int32_t n_kv = 32; // size of KV cache to consider (n_kv <= n_ctx |
| 9709 | static constexpr int32_t kv_head = 1; // index of where we store new KV data in the cache |
| 9710 | |
| 9711 | uint32_t n_embd_gqa() const { // dimension of key embeddings across all k-v heads |
| 9712 | return n_embd_head * n_head_kv; |
| 9713 | } |
| 9714 | }; |
| 9715 | |
| 9716 | // LLM base class |
| 9717 | struct test_llm : public test_case { |
| 9718 | llama_hparams hp; |
| 9719 | |
| 9720 | protected: |
| 9721 | test_llm(llama_hparams hp) |
| 9722 | : hp(std::move(hp)) { |
| 9723 | } |
| 9724 | |
| 9725 | public: |
| 9726 | struct ggml_tensor * llm_build_norm( |
| 9727 | struct ggml_context * ctx, |
| 9728 | struct ggml_tensor * cur, |
| 9729 | struct ggml_tensor * mw, |
| 9730 | struct ggml_tensor * mb, |
| 9731 | llm_norm_type type) { |
| 9732 | switch (type) { |
| 9733 | case LLM_NORM: cur = ggml_norm (ctx, cur, hp.f_norm_eps); break; |
| 9734 | case LLM_NORM_RMS: cur = ggml_rms_norm(ctx, cur, hp.f_norm_rms_eps); break; |
| 9735 | } |
| 9736 | cur = ggml_mul(ctx, cur, mw); |
| 9737 | if (mb) { |
| 9738 | cur = ggml_add(ctx, cur, mb); |
| 9739 | } |
| 9740 | return cur; |
| 9741 | } |
| 9742 | |
| 9743 | void llm_build_kv_store( |
| 9744 | struct ggml_context * ctx, |
| 9745 | struct ggml_tensor * k_l, |
| 9746 | struct ggml_tensor * v_l, |
| 9747 | struct ggml_tensor * k_cur, |
| 9748 | struct ggml_tensor * v_cur) { |
| 9749 | // compute the transposed [n_tokens, n_embd] V matrix |
| 9750 | struct ggml_tensor * v_cur_t = ggml_transpose(ctx, ggml_reshape_2d(ctx, v_cur, hp.n_embd_gqa(), hp.n_tokens)); |
| 9751 | |
| 9752 | struct ggml_tensor * k_cache_view = ggml_view_1d(ctx, k_l, hp.n_tokens*hp.n_embd_gqa(), |
| 9753 | (ggml_row_size(k_l->type, hp.n_embd_gqa()))*hp.kv_head); |
| 9754 | |
| 9755 | struct ggml_tensor * v_cache_view = ggml_view_2d(ctx, v_l, hp.n_tokens, hp.n_embd_gqa(), |
| 9756 | ( hp.n_ctx)*ggml_element_size(v_l), |
| 9757 | (hp.kv_head)*ggml_element_size(v_l)); |
| 9758 | |
| 9759 | // important: storing RoPE-ed version of K in the KV cache! |
| 9760 | ggml_cpy(ctx, k_cur, k_cache_view); |
| 9761 | ggml_cpy(ctx, v_cur_t, v_cache_view); |
| 9762 | } |
| 9763 | |
| 9764 | struct ggml_tensor * llm_build_kqv( |
| 9765 | struct ggml_context * ctx, |
| 9766 | struct ggml_tensor * k_l, |
| 9767 | struct ggml_tensor * v_l, |
| 9768 | struct ggml_tensor * q_cur, |
| 9769 | struct ggml_tensor * kq_mask, |
| 9770 | float kq_scale) { |
| 9771 | struct ggml_tensor * q = ggml_permute(ctx, q_cur, 0, 2, 1, 3); |
| 9772 | |
| 9773 | struct ggml_tensor * k = |
| 9774 | ggml_view_3d(ctx, k_l, |
| 9775 | hp.n_embd_head, hp.n_kv, hp.n_head_kv, |
| 9776 | ggml_row_size(k_l->type, hp.n_embd_gqa()), |
| 9777 | ggml_row_size(k_l->type, hp.n_embd_head), |
| 9778 | 0); |
| 9779 | |
| 9780 | struct ggml_tensor * kq = ggml_mul_mat(ctx, k, q); |
| 9781 | |
| 9782 | kq = ggml_soft_max_ext(ctx, kq, kq_mask, kq_scale, 0.0f); |
| 9783 | |
| 9784 | // split cached v into n_head heads |
| 9785 | struct ggml_tensor * v = |
| 9786 | ggml_view_3d(ctx, v_l, |
| 9787 | hp.n_kv, hp.n_embd_head, hp.n_head_kv, |
| 9788 | ggml_element_size(v_l)*hp.n_ctx, |
| 9789 | ggml_element_size(v_l)*hp.n_ctx*hp.n_embd_head, |
| 9790 | 0); |
| 9791 | |
| 9792 | struct ggml_tensor * kqv = ggml_mul_mat(ctx, v, kq); |
| 9793 | |
| 9794 | struct ggml_tensor * kqv_merged = ggml_permute(ctx, kqv, 0, 2, 1, 3); |
| 9795 | |
| 9796 | struct ggml_tensor * cur = ggml_cont_2d(ctx, kqv_merged, hp.n_embd_head*hp.n_head, hp.n_tokens); |
| 9797 | |
| 9798 | struct ggml_tensor * wo = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd); |
| 9799 | cur = ggml_mul_mat(ctx, wo, cur); |
| 9800 | |
| 9801 | return cur; |
| 9802 | } |
| 9803 | |
| 9804 | void initialize_tensors(ggml_context * ctx) override { |
| 9805 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { |
| 9806 | if (t->type == GGML_TYPE_I32) { |
| 9807 | // pos |
| 9808 | std::vector<int> data(hp.n_tokens); |
| 9809 | for (int i = 0; i < hp.n_tokens; i++) { |
| 9810 | data[i] = rand() % hp.n_ctx; |
| 9811 | } |
| 9812 | ggml_backend_tensor_set(t, data.data(), 0, hp.n_tokens * sizeof(int)); |
| 9813 | } else { |
| 9814 | init_tensor_uniform(t); |
| 9815 | } |
| 9816 | } |
| 9817 | } |
| 9818 | }; |
| 9819 | |
| 9820 | // Llama |
| 9821 | struct test_llama : public test_llm { |
| 9822 | static constexpr float freq_base = 10000.0f; |
| 9823 | static constexpr float freq_scale = 1.0f; |
| 9824 | static constexpr float ext_factor = 0.0f; |
| 9825 | static constexpr float attn_factor = 1.0f; |
| 9826 | static constexpr float beta_fast = 32.0f; |
| 9827 | static constexpr float beta_slow = 1.0f; |
| 9828 | bool fused; |
| 9829 | |
| 9830 | std::string op_desc(ggml_tensor * t) override { |
| 9831 | GGML_UNUSED(t); |
| 9832 | return "LLAMA"; |
| 9833 | } |
| 9834 | |
| 9835 | std::string vars() override { |
| 9836 | auto n_tokens = hp.n_tokens; |
| 9837 | return VARS_TO_STR1(n_tokens); |
| 9838 | } |
| 9839 | |
| 9840 | double max_nmse_err() override { |
| 9841 | return 2e-3; |
| 9842 | } |
| 9843 | |
| 9844 | bool run_whole_graph() override { return fused; } |
| 9845 | |
| 9846 | test_llama(int n_tokens = 1, bool fused = false) |
| 9847 | : test_llm({ |
| 9848 | /*n_vocab =*/ 32000, |
| 9849 | /*n_embd =*/ 3200, |
| 9850 | /*n_head =*/ 32, |
| 9851 | /*n_head_kv =*/ 32, |
| 9852 | /*n_rot =*/ 100, |
| 9853 | /*n_embd_head =*/ 100, |
| 9854 | /*n_ff =*/ 8640, |
| 9855 | /*f_norm_eps =*/ 0.f, |
| 9856 | /*f_norm_rms_eps =*/ 1e-5f, |
| 9857 | /*n_tokens =*/ n_tokens, |
| 9858 | }) |
| 9859 | , fused(fused) |
| 9860 | { |
| 9861 | } |
| 9862 | |
| 9863 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 9864 | struct ggml_tensor * cur; |
| 9865 | struct ggml_tensor * inpL; |
| 9866 | |
| 9867 | inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens); |
| 9868 | |
| 9869 | // inp_pos - contains the positions |
| 9870 | struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens); |
| 9871 | |
| 9872 | // KQ_mask (mask for 1 head, it will be broadcasted to all heads) |
| 9873 | struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, hp.n_kv, hp.n_tokens, 1); |
| 9874 | |
| 9875 | ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400); |
| 9876 | ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400); |
| 9877 | |
| 9878 | for (uint32_t il = 0; il < hp.n_layer; ++il) { |
| 9879 | struct ggml_tensor * inpSA = inpL; |
| 9880 | |
| 9881 | // norm |
| 9882 | ggml_tensor * attn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd); |
| 9883 | cur = llm_build_norm(ctx, inpL, attn_norm, nullptr, LLM_NORM_RMS); |
| 9884 | |
| 9885 | // self-attention |
| 9886 | { |
| 9887 | ggml_tensor * wq = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd); |
| 9888 | ggml_tensor * wk = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa()); |
| 9889 | ggml_tensor * wv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa()); |
| 9890 | |
| 9891 | // compute Q and K and RoPE them |
| 9892 | struct ggml_tensor * Qcur = ggml_mul_mat(ctx, wq, cur); |
| 9893 | struct ggml_tensor * Kcur = ggml_mul_mat(ctx, wk, cur); |
| 9894 | struct ggml_tensor * Vcur = ggml_mul_mat(ctx, wv, cur); |
| 9895 | |
| 9896 | Qcur = ggml_rope_ext( |
| 9897 | ctx, ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens), inp_pos, nullptr, |
| 9898 | hp.n_rot, 0, hp.n_ctx_orig, freq_base, freq_scale, |
| 9899 | ext_factor, attn_factor, beta_fast, beta_slow |
| 9900 | ); |
| 9901 | |
| 9902 | Kcur = ggml_rope_ext( |
| 9903 | ctx, ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens), inp_pos, nullptr, |
| 9904 | hp.n_rot, 0, hp.n_ctx_orig, freq_base, freq_scale, |
| 9905 | ext_factor, attn_factor, beta_fast, beta_slow |
| 9906 | ); |
| 9907 | |
| 9908 | llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur); |
| 9909 | |
| 9910 | cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head))); |
| 9911 | } |
| 9912 | |
| 9913 | struct ggml_tensor * ffn_inp = ggml_add(ctx, cur, inpSA); |
| 9914 | |
| 9915 | // feed-forward network |
| 9916 | ggml_tensor * ffn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd); |
| 9917 | cur = llm_build_norm(ctx, ffn_inp, ffn_norm, nullptr, LLM_NORM_RMS); |
| 9918 | |
| 9919 | ggml_tensor * ffn_gate = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff); |
| 9920 | ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd); |
| 9921 | ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff); |
| 9922 | struct ggml_tensor * tmp = ggml_mul_mat(ctx, ffn_up, cur); |
| 9923 | cur = ggml_mul_mat(ctx, ffn_gate, cur); |
| 9924 | cur = ggml_silu(ctx, cur); |
| 9925 | cur = ggml_mul(ctx, cur, tmp); |
| 9926 | cur = ggml_mul_mat(ctx, ffn_down, cur); |
| 9927 | |
| 9928 | cur = ggml_add(ctx, cur, ffn_inp); |
| 9929 | |
| 9930 | // input for next layer |
| 9931 | inpL = cur; |
| 9932 | } |
| 9933 | |
| 9934 | cur = inpL; |
| 9935 | |
| 9936 | ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd); |
| 9937 | cur = llm_build_norm(ctx, cur, output_norm, nullptr, LLM_NORM_RMS); |
| 9938 | |
| 9939 | // lm_head |
| 9940 | ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_vocab); |
| 9941 | cur = ggml_mul_mat(ctx, output, cur); |
| 9942 | |
| 9943 | return cur; |
| 9944 | } |
| 9945 | }; |
| 9946 | |
| 9947 | // Falcon |
| 9948 | struct test_falcon : public test_llm { |
| 9949 | static constexpr float freq_base = 10000.0f; |
| 9950 | static constexpr float freq_scale = 1.0f; |
| 9951 | static constexpr float ext_factor = 0.0f; |
| 9952 | static constexpr float attn_factor = 1.0f; |
| 9953 | static constexpr float beta_fast = 32.0f; |
| 9954 | static constexpr float beta_slow = 1.0f; |
| 9955 | |
| 9956 | std::string op_desc(ggml_tensor * t) override { |
| 9957 | GGML_UNUSED(t); |
| 9958 | return "FALCON"; |
| 9959 | } |
| 9960 | |
| 9961 | std::string vars() override { |
| 9962 | auto n_tokens = hp.n_tokens; |
| 9963 | return VARS_TO_STR1(n_tokens); |
| 9964 | } |
| 9965 | |
| 9966 | double max_nmse_err() override { |
| 9967 | return 2e-3; |
| 9968 | } |
| 9969 | |
| 9970 | test_falcon(int n_tokens = 1) |
| 9971 | : test_llm({ |
| 9972 | /*n_vocab =*/ 32000, |
| 9973 | /*n_embd =*/ 3200, |
| 9974 | /*n_head =*/ 50, |
| 9975 | /*n_head_kv =*/ 1, |
| 9976 | /*n_rot =*/ 64, |
| 9977 | /*n_embd_head =*/ 64, |
| 9978 | /*n_ff =*/ 8640, |
| 9979 | /*f_norm_eps =*/ 1e-5f, |
| 9980 | /*f_norm_rms_eps =*/ 0.f, |
| 9981 | /*n_tokens =*/ n_tokens, |
| 9982 | }) { |
| 9983 | } |
| 9984 | |
| 9985 | ggml_tensor * build_graph(ggml_context * ctx) override { |
| 9986 | struct ggml_tensor * cur; |
| 9987 | struct ggml_tensor * inpL; |
| 9988 | |
| 9989 | inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens); |
| 9990 | |
| 9991 | // inp_pos - contains the positions |
| 9992 | struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens); |
| 9993 | |
| 9994 | // KQ_mask (mask for 1 head, it will be broadcasted to all heads) |
| 9995 | struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, hp.n_kv, hp.n_tokens, 1); |
| 9996 | |
| 9997 | ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400); |
| 9998 | ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400); |
| 9999 | |
| 10000 | for (uint32_t il = 0; il < hp.n_layer; ++il) { |
| 10001 | // norm |
| 10002 | ggml_tensor * attn_norm_w = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd); |
| 10003 | ggml_tensor * attn_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd); |
| 10004 | ggml_tensor * attn_norm = llm_build_norm(ctx, inpL, attn_norm_w, attn_norm_b, LLM_NORM); |
| 10005 | |
| 10006 | // self-attention |
| 10007 | { |
| 10008 | cur = attn_norm; |
| 10009 | |
| 10010 | ggml_tensor * wqkv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd + 2*hp.n_embd_gqa()); |
| 10011 | |
| 10012 | cur = ggml_mul_mat(ctx, wqkv, cur); |
| 10013 | |
| 10014 | struct ggml_tensor * Qcur = ggml_cont(ctx, ggml_view_2d(ctx, cur, hp.n_embd, hp.n_tokens, cur->nb[1], 0*sizeof(float)*(hp.n_embd))); |
| 10015 | struct ggml_tensor * Kcur = ggml_cont(ctx, ggml_view_2d(ctx, cur, hp.n_embd_gqa(), hp.n_tokens, cur->nb[1], 1*sizeof(float)*(hp.n_embd))); |
| 10016 | struct ggml_tensor * Vcur = ggml_cont(ctx, ggml_view_2d(ctx, cur, hp.n_embd_gqa(), hp.n_tokens, cur->nb[1], 1*sizeof(float)*(hp.n_embd + hp.n_embd_gqa()))); |
| 10017 | |
| 10018 | Qcur = ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens); |
| 10019 | Kcur = ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens); |
| 10020 | |
| 10021 | // using mode = 2 for neox mode |
| 10022 | Qcur = ggml_rope_ext( |
| 10023 | ctx, Qcur, inp_pos, nullptr, hp.n_rot, 2, hp.n_ctx_orig, |
| 10024 | freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow |
| 10025 | ); |
| 10026 | |
| 10027 | Kcur = ggml_rope_ext( |
| 10028 | ctx, Kcur, inp_pos, nullptr, hp.n_rot, 2, hp.n_ctx_orig, |
| 10029 | freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow |
| 10030 | ); |
| 10031 | |
| 10032 | llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur); |
| 10033 | |
| 10034 | cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head))); |
| 10035 | } |
| 10036 | |
| 10037 | struct ggml_tensor * ffn_inp = cur; |
| 10038 | |
| 10039 | // feed forward |
| 10040 | { |
| 10041 | ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff); |
| 10042 | ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd); |
| 10043 | cur = attn_norm; |
| 10044 | cur = ggml_mul_mat(ctx, ffn_up, cur); |
| 10045 | cur = ggml_gelu(ctx, cur); |
| 10046 | cur = ggml_mul_mat(ctx, ffn_down, cur); |
| 10047 | } |
| 10048 | |
| 10049 | cur = ggml_add(ctx, cur, ffn_inp); |
| 10050 | |
| 10051 | cur = ggml_add(ctx, cur, inpL); |
| 10052 | |
| 10053 | // input for next layer |
| 10054 | inpL = cur; |
| 10055 | } |
| 10056 | |
| 10057 | cur = inpL; |
| 10058 | |
| 10059 | ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd); |
| 10060 | ggml_tensor * output_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd); |
| 10061 | cur = llm_build_norm(ctx, cur, output_norm, output_norm_b, LLM_NORM); |
| 10062 | |
| 10063 | // lm_head |
| 10064 | ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q8_0, hp.n_embd, hp.n_vocab); |
| 10065 | cur = ggml_mul_mat(ctx, output, cur); |
| 10066 | |
| 10067 | return cur; |
| 10068 | } |
| 10069 | }; |
| 10070 | |
| 10071 | |
| 10072 | // ########################################### |
| 10073 | // ## Section 3: GGML Op Test Instantiation ## |
| 10074 | // ########################################### |
| 10075 | static const ggml_type all_types[] = { |
| 10076 | GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16, |
| 10077 | GGML_TYPE_Q4_0, GGML_TYPE_Q4_1, |
| 10078 | GGML_TYPE_Q5_0, GGML_TYPE_Q5_1, |
| 10079 | GGML_TYPE_Q8_0, |
| 10080 | GGML_TYPE_Q1_0, |
| 10081 | GGML_TYPE_Q2_0, |
| 10082 | GGML_TYPE_MXFP4, GGML_TYPE_NVFP4, |
| 10083 | GGML_TYPE_F8_E4M3, |
| 10084 | GGML_TYPE_Q2_K, GGML_TYPE_Q3_K, |
| 10085 | GGML_TYPE_Q4_K, GGML_TYPE_Q5_K, |
| 10086 | GGML_TYPE_Q6_K, |
| 10087 | GGML_TYPE_TQ2_0, |
| 10088 | GGML_TYPE_TQ1_0, |
| 10089 | GGML_TYPE_IQ2_XXS, GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S, |
| 10090 | GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M, |
| 10091 | GGML_TYPE_IQ4_NL, GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_XS, |
| 10092 | }; |
| 10093 | |
| 10094 | static const ggml_type base_types[] = { |
| 10095 | GGML_TYPE_F32, GGML_TYPE_F16, |
| 10096 | GGML_TYPE_Q8_0, // for I8MM tests |
| 10097 | GGML_TYPE_Q1_0, |
| 10098 | GGML_TYPE_Q2_0, |
| 10099 | GGML_TYPE_Q4_0, |
| 10100 | GGML_TYPE_Q4_1, // for I8MM tests |
| 10101 | GGML_TYPE_Q4_K, |
| 10102 | GGML_TYPE_MXFP4, GGML_TYPE_NVFP4, // TODO: or "other" |
| 10103 | GGML_TYPE_F8_E4M3, |
| 10104 | GGML_TYPE_IQ2_XXS |
| 10105 | }; |
| 10106 | |
| 10107 | static const ggml_type other_types[] = { |
| 10108 | GGML_TYPE_Q4_1, |
| 10109 | GGML_TYPE_Q5_0, GGML_TYPE_Q5_1, |
| 10110 | GGML_TYPE_Q8_0, |
| 10111 | GGML_TYPE_Q1_0, |
| 10112 | GGML_TYPE_Q2_0, |
| 10113 | GGML_TYPE_Q2_K, GGML_TYPE_Q3_K, |
| 10114 | GGML_TYPE_Q5_K, |
| 10115 | GGML_TYPE_Q6_K, |
| 10116 | GGML_TYPE_TQ2_0, |
| 10117 | GGML_TYPE_TQ1_0, |
| 10118 | GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S, |
| 10119 | GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M, |
| 10120 | GGML_TYPE_IQ4_NL, GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_XS, |
| 10121 | GGML_TYPE_BF16, |
| 10122 | }; |
| 10123 | |
| 10124 | #ifdef _MSC_VER |
| 10125 | // Workaround long compile time with msvc |
| 10126 | #pragma optimize("", off) |
| 10127 | #endif |
| 10128 | |
| 10129 | // Test cases for evaluation: should try to cover edge cases while using small input sizes to keep the runtime low |
| 10130 | static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() { |
| 10131 | std::vector<std::unique_ptr<test_case>> test_cases; |
| 10132 | std::default_random_engine rng(0); |
| 10133 | |
| 10134 | // unary ops |
| 10135 | // TODO: add GGML_UNARY_OP_SOFTPLUS test that is sensitive to using `log1p(exp(x))` vs `log(exp(x) + 1)` |
| 10136 | for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_F32, GGML_TYPE_BF16}) { |
| 10137 | for (int v : {0, 1}) { |
| 10138 | for (int op = 0; op < GGML_UNARY_OP_COUNT; op++) { |
| 10139 | if (op == GGML_UNARY_OP_XIELU) { |
| 10140 | continue; // need extra params, separate test |
| 10141 | } |
| 10142 | test_cases.emplace_back(new test_unary((ggml_unary_op) op, type, { 128, 2, 2, 2 }, v)); |
| 10143 | test_cases.emplace_back(new test_unary((ggml_unary_op) op, type, { 5, 7, 11, 13 }, v)); |
| 10144 | } |
| 10145 | } |
| 10146 | } |
| 10147 | |
| 10148 | // fused relu + sqr (squared ReLU) |
| 10149 | for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_F32}) { |
| 10150 | test_cases.emplace_back(new test_relu_sqr(type, { 128, 2, 2, 2 })); |
| 10151 | test_cases.emplace_back(new test_relu_sqr(type, { 5, 7, 11, 13 })); |
| 10152 | } |
| 10153 | |
| 10154 | // fused unary + mul (gated activations that are not expressed as GGML_OP_GLU) |
| 10155 | for (ggml_unary_op op : { GGML_UNARY_OP_GELU, GGML_UNARY_OP_SILU, GGML_UNARY_OP_RELU, GGML_UNARY_OP_SIGMOID, GGML_UNARY_OP_SOFTPLUS }) { |
| 10156 | for (ggml_type type : { GGML_TYPE_F16, GGML_TYPE_F32, GGML_TYPE_BF16 }) { |
| 10157 | for (bool swap : { false, true }) { |
| 10158 | test_cases.emplace_back(new test_unary_mul(op, type, { 128, 2, 2, 2 }, swap)); |
| 10159 | } |
| 10160 | test_cases.emplace_back(new test_unary_mul(op, type, { 5, 7, 11, 13 })); |
| 10161 | test_cases.emplace_back(new test_unary_mul(op, type, { 128, 2, 2, 2 }, false, "pad_unary")); |
| 10162 | // a view only stays out from between the two ops when the unary result is second |
| 10163 | test_cases.emplace_back(new test_unary_mul(op, type, { 128, 2, 2, 2 }, true, "pad_other")); |
| 10164 | test_cases.emplace_back(new test_unary_mul(op, type, { 128, 2, 2, 2 }, true, "halves")); |
| 10165 | test_cases.emplace_back(new test_unary_mul(op, type, { 128, 2, 2, 2 }, false, "packed", "consumer")); |
| 10166 | test_cases.emplace_back(new test_unary_mul(op, type, { 128, 2, 2, 2 }, false, "bcast")); |
| 10167 | test_cases.emplace_back(new test_unary_mul(op, type, { 128, 2, 2, 2 }, false, "rep_ne0")); |
| 10168 | test_cases.emplace_back(new test_unary_mul(op, type, { 128, 2, 2, 2 }, false, "view_mid")); |
| 10169 | test_cases.emplace_back(new test_unary_mul(op, type, { 128, 2, 2, 2 }, false, "gate")); |
| 10170 | // must not fuse |
| 10171 | test_cases.emplace_back(new test_unary_mul(op, type, { 128, 2, 2, 2 }, false, "strided_dim1")); |
| 10172 | test_cases.emplace_back(new test_unary_mul(op, type, { 128, 2, 2, 2 }, false, "packed", "reuse")); |
| 10173 | } |
| 10174 | } |
| 10175 | |
| 10176 | // fused add + unary + mul: the delta-net alpha gate, bias and scale broadcast over dim 0 |
| 10177 | for (ggml_unary_op op : { GGML_UNARY_OP_SILU, GGML_UNARY_OP_SIGMOID, GGML_UNARY_OP_SOFTPLUS }) { |
| 10178 | test_cases.emplace_back(new test_add_unary_mul(op, GGML_TYPE_F32, { 32, 7, 1, 1 })); |
| 10179 | test_cases.emplace_back(new test_add_unary_mul(op, GGML_TYPE_F32, { 32, 512, 1, 1 })); |
| 10180 | test_cases.emplace_back(new test_add_unary_mul(op, GGML_TYPE_F32, { 5, 7, 11, 13 })); |
| 10181 | // one token: no broadcast left, and the unary result may be either MUL operand |
| 10182 | test_cases.emplace_back(new test_add_unary_mul(op, GGML_TYPE_F32, { 32, 1, 1, 1 }, false, "same_shape")); |
| 10183 | test_cases.emplace_back(new test_add_unary_mul(op, GGML_TYPE_F32, { 32, 1, 1, 1 }, true, "same_shape")); |
| 10184 | test_cases.emplace_back(new test_add_unary_mul(op, GGML_TYPE_F32, { 32, 7, 1, 1 }, false, "bcast", "consumer")); |
| 10185 | // must not fuse |
| 10186 | test_cases.emplace_back(new test_add_unary_mul(op, GGML_TYPE_F32, { 32, 7, 1, 1 }, false, "same_shape")); |
| 10187 | test_cases.emplace_back(new test_add_unary_mul(op, GGML_TYPE_F32, { 32, 7, 1, 1 }, false, "rep_ne0")); |
| 10188 | test_cases.emplace_back(new test_add_unary_mul(op, GGML_TYPE_F32, { 32, 7, 1, 1 }, false, "bcast", "reuse")); |
| 10189 | test_cases.emplace_back(new test_add_unary_mul(op, GGML_TYPE_F16, { 32, 7, 1, 1 })); |
| 10190 | } |
| 10191 | // a unary op with no fused kernel must fall back to the three-op chain |
| 10192 | test_cases.emplace_back(new test_add_unary_mul(GGML_UNARY_OP_GELU, GGML_TYPE_F32, { 32, 7, 1, 1 })); |
| 10193 | |
| 10194 | // SNAKE activation fusion: x + sin(a*x)^2 * inv_b |
| 10195 | for (ggml_type type : { GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16 }) { |
| 10196 | test_cases.emplace_back(new test_snake_fuse(type, { 5, 7, 1, 1})); // primes sub-block |
| 10197 | test_cases.emplace_back(new test_snake_fuse(type, { 33, 32, 1, 1})); // boundary |
| 10198 | test_cases.emplace_back(new test_snake_fuse(type, {1025, 13, 1, 1})); // large prime, grid-stride |
| 10199 | test_cases.emplace_back(new test_snake_fuse(type, { 128, 16, 1, 1})); // power-of-two |
| 10200 | test_cases.emplace_back(new test_snake_fuse(type, { 256, 192, 1, 1})); // BigVGAN-ish |
| 10201 | // higher-rank shapes: matcher must reject fusion, fallback to naive chain |
| 10202 | test_cases.emplace_back(new test_snake_fuse(type, { 64, 32, 2, 1})); // ne[2] > 1 |
| 10203 | test_cases.emplace_back(new test_snake_fuse(type, { 64, 32, 1, 2})); // ne[3] > 1 |
| 10204 | test_cases.emplace_back(new test_snake_fuse(type, { 64, 32, 2, 3})); // ne[2] > 1 and ne[3] > 1 |
| 10205 | } |
| 10206 | |
| 10207 | test_cases.emplace_back(new test_dsv4_hc_comb(1, 1)); |
| 10208 | test_cases.emplace_back(new test_dsv4_hc_comb(17, 4)); |
| 10209 | test_cases.emplace_back(new test_dsv4_hc_comb(257, 8)); |
| 10210 | test_cases.emplace_back(new test_dsv4_hc_comb(17, 20)); |
| 10211 | // production n_iter (DeepSeek-V4 uses 20) across batch sizes that cross |
| 10212 | // subgroup and workgroup boundaries; 1 = single-token decode |
| 10213 | for (int64_t n_tokens : {1, 256, 336, 512, 513, 1024, 2048}) { |
| 10214 | test_cases.emplace_back(new test_dsv4_hc_comb(n_tokens, 20)); |
| 10215 | } |
| 10216 | |
| 10217 | test_cases.emplace_back(new test_dsv4_hc_pre(1, 4, 1)); |
| 10218 | test_cases.emplace_back(new test_dsv4_hc_pre(31, 4, 17)); |
| 10219 | test_cases.emplace_back(new test_dsv4_hc_pre(128, 4, 257)); |
| 10220 | test_cases.emplace_back(new test_dsv4_hc_pre(4096, 4, 21)); |
| 10221 | test_cases.emplace_back(new test_dsv4_hc_pre(31, 4, 17, true)); |
| 10222 | test_cases.emplace_back(new test_dsv4_hc_pre(4096, 4, 21, true)); |
| 10223 | for (int64_t n_hc : {1, 2, 3, 5, 8, 65}) { |
| 10224 | test_cases.emplace_back(new test_dsv4_hc_pre(128, n_hc, 17)); |
| 10225 | test_cases.emplace_back(new test_dsv4_hc_pre(128, n_hc, 17, true)); |
| 10226 | } |
| 10227 | |
| 10228 | test_cases.emplace_back(new test_dsv4_hc_post(1, 1)); |
| 10229 | test_cases.emplace_back(new test_dsv4_hc_post(31, 17)); |
| 10230 | test_cases.emplace_back(new test_dsv4_hc_post(128, 257)); |
| 10231 | test_cases.emplace_back(new test_dsv4_hc_post(4096, 21)); |
| 10232 | test_cases.emplace_back(new test_dsv4_hc_post(31, 17, true)); |
| 10233 | test_cases.emplace_back(new test_dsv4_hc_post(4096, 21, true)); |
| 10234 | test_cases.emplace_back(new test_dsv4_hc_post(31, 17, true, true)); |
| 10235 | test_cases.emplace_back(new test_dsv4_hc_post(2560, 21, true, true)); |
| 10236 | test_cases.emplace_back(new test_dsv4_hc_post(31, 17, false, true)); |
| 10237 | test_cases.emplace_back(new test_dsv4_hc_post(4096, 1)); // DeepSeek-V4-Flash decode |
| 10238 | test_cases.emplace_back(new test_dsv4_hc_post(2560, 1, true, true)); // Qwen4-Exp decode |
| 10239 | |
| 10240 | for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_F32}) { |
| 10241 | // blocks with extra cells and scores (k-pool) |
| 10242 | test_cases.emplace_back(new test_top_k_mask(type)); |
| 10243 | test_cases.emplace_back(new test_top_k_mask(type, 1001, 4, 70, 16, 3, true, 7)); |
| 10244 | test_cases.emplace_back(new test_top_k_mask(type, 256, 4, 64, 64, 0, true, 33)); |
| 10245 | test_cases.emplace_back(new test_top_k_mask(type, 512, 8, 16, 32, 7, true, 5, 1, 4)); |
| 10246 | test_cases.emplace_back(new test_top_k_mask(type, 4096, 4, 1024, 512, 3, true, 1)); |
| 10247 | test_cases.emplace_back(new test_top_k_mask(type, 4096, 4, 1024, 512, 3, true, 64, 1, 2)); |
| 10248 | // cells without scores (DSA) |
| 10249 | test_cases.emplace_back(new test_top_k_mask(type, 256, 0, 0, 16, 0, false, 32)); |
| 10250 | test_cases.emplace_back(new test_top_k_mask(type, 1001, 0, 0, 64, 0, false, 7, 1, 3)); |
| 10251 | test_cases.emplace_back(new test_top_k_mask(type, 4096, 0, 0, 2048, 0, false, 5)); |
| 10252 | // one row, no blocks, fewer than 128 picks: no read past the pick list |
| 10253 | test_cases.emplace_back(new test_top_k_mask(type, 1001, 0, 0, 16, 3, false, 1)); |
| 10254 | test_cases.emplace_back(new test_top_k_mask(type, 256, 0, 0, 40, 0, true, 1)); |
| 10255 | // one row (single-row kernel) with each optional input combination: no score and no extra, blocks only, blocks and extra without score |
| 10256 | test_cases.emplace_back(new test_top_k_mask(type, 512, 0, 0, 16, 0, false, 1)); |
| 10257 | test_cases.emplace_back(new test_top_k_mask(type, 1024, 4, 200, 32, 0, false, 1)); |
| 10258 | test_cases.emplace_back(new test_top_k_mask(type, 1024, 4, 200, 32, 5, false, 1)); |
| 10259 | // cells with scores and extra cells |
| 10260 | test_cases.emplace_back(new test_top_k_mask(type, 512, 0, 0, 32, 5, true, 9)); |
| 10261 | // blocks picked per head, the mask broadcast over the heads |
| 10262 | test_cases.emplace_back(new test_top_k_mask(type, 512, 16, 32, 4, 0, true, 6, 4, 1)); |
| 10263 | test_cases.emplace_back(new test_top_k_mask(type, 1024, 8, 128, 16, 8, false, 3, 2, 2)); |
| 10264 | // one mask row for all rows and streams (QSA) |
| 10265 | test_cases.emplace_back(new test_top_k_mask(type, 1001, 4, 70, 16, 3, true, 7, 1, 1, false, true)); |
| 10266 | test_cases.emplace_back(new test_top_k_mask(type, 512, 8, 16, 32, 7, true, 5, 1, 4, false, true)); |
| 10267 | } |
| 10268 | |
| 10269 | // glu ops |
| 10270 | for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_F32, GGML_TYPE_BF16}) { |
| 10271 | for (int v : {0, 1}) { |
| 10272 | for (int op = 0; op < GGML_GLU_OP_COUNT; op++) { |
| 10273 | if (op == GGML_GLU_OP_SWIGLU_OAI || op == GGML_GLU_OP_SWIGLU_CLAMP) { |
| 10274 | continue; |
| 10275 | } |
| 10276 | |
| 10277 | for (bool swapped : {false, true}) { |
| 10278 | test_cases.emplace_back(new test_glu((ggml_glu_op) op, type, { 128, 2, 2, 2 }, v, swapped)); |
| 10279 | test_cases.emplace_back(new test_glu((ggml_glu_op) op, type, { 5, 7, 11, 13 }, v, swapped)); |
| 10280 | } |
| 10281 | |
| 10282 | test_cases.emplace_back(new test_glu_split((ggml_glu_op) op, type, { 128, 2, 2, 2 }, v)); |
| 10283 | test_cases.emplace_back(new test_glu_split((ggml_glu_op) op, type, { 5, 7, 11, 13 }, v)); |
| 10284 | } |
| 10285 | } |
| 10286 | } |
| 10287 | |
| 10288 | for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 10289 | for (int v : {0, 1}) { |
| 10290 | for (float alpha : {.5f, 1.702f}) { |
| 10291 | for (float limit : {2.0f, 7.0f}) { |
| 10292 | test_cases.emplace_back(new test_swiglu_oai(type, { 128, 2, 2, 2 }, v, alpha, limit)); |
| 10293 | } |
| 10294 | } |
| 10295 | } |
| 10296 | } |
| 10297 | |
| 10298 | for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_F32}) { |
| 10299 | for (int v : {0, 1}) { |
| 10300 | for (float limit : {2.0f, 10.0f}) { |
| 10301 | test_cases.emplace_back(new test_swiglu_clamp(type, { 128, 2, 2, 2 }, v, limit)); |
| 10302 | } |
| 10303 | } |
| 10304 | } |
| 10305 | |
| 10306 | for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_Q4_0}) { |
| 10307 | test_cases.emplace_back(new test_get_rows(type, 300*256, 5, 4, 1, 2, false)); |
| 10308 | test_cases.emplace_back(new test_get_rows(type, 256, 80000, 70000, 2, 1, false)); |
| 10309 | test_cases.emplace_back(new test_get_rows(type, 256, 5, 4, 700, 100, false)); |
| 10310 | } |
| 10311 | |
| 10312 | test_cases.emplace_back(new test_get_rows(GGML_TYPE_F32, 1, 8, 2, 1, 1, false)); |
| 10313 | for (ggml_type type : {GGML_TYPE_Q4_0, GGML_TYPE_Q8_0, GGML_TYPE_Q2_K, GGML_TYPE_Q3_K, GGML_TYPE_Q4_K, GGML_TYPE_Q5_K, GGML_TYPE_Q6_K, |
| 10314 | GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ3_S}) { |
| 10315 | test_cases.emplace_back(new test_get_rows_after_mul_mat(type)); |
| 10316 | } |
| 10317 | for (ggml_type type : {GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ3_S}) { |
| 10318 | test_cases.emplace_back(new test_mul_mat_reorder_then_big_batch(type)); |
| 10319 | } |
| 10320 | for (ggml_type type : all_types) { |
| 10321 | for (int b : {1, 7}) { |
| 10322 | for (bool v : {false, true}) { |
| 10323 | for (bool vs0 : {false, true}) { |
| 10324 | test_cases.emplace_back(new test_get_rows(type, 256, 5, 4, b, 1, v, vs0)); |
| 10325 | } |
| 10326 | } |
| 10327 | } |
| 10328 | } |
| 10329 | for (int b : {1, 7}) { |
| 10330 | for (bool v : {false, true}) { |
| 10331 | for (bool vs0 : {false, true}) { |
| 10332 | test_cases.emplace_back(new test_get_rows(GGML_TYPE_I32, 256, 5, 4, b, 1, v, vs0)); |
| 10333 | } |
| 10334 | } |
| 10335 | } |
| 10336 | test_cases.emplace_back(new test_get_rows(GGML_TYPE_F32, 256, 8, 2, 1, 1, false, true, 3)); |
| 10337 | |
| 10338 | test_cases.emplace_back(new test_get_rows_back(GGML_TYPE_F32, 1, 8, 2, 1, false)); |
| 10339 | test_cases.emplace_back(new test_get_rows_back(GGML_TYPE_F32, 1, 70000, 4, 1, false)); // row count > CUDA grid-y limit (65535) |
| 10340 | for (ggml_type type : all_types) { |
| 10341 | for (bool v : {false, true}) { |
| 10342 | test_cases.emplace_back(new test_get_rows_back(type, 256, 5, 4, 1, v)); |
| 10343 | } |
| 10344 | } |
| 10345 | for (bool v : {false, true}) { |
| 10346 | test_cases.emplace_back(new test_get_rows_back(GGML_TYPE_I32, 256, 5, 4, 1, v)); |
| 10347 | } |
| 10348 | |
| 10349 | test_cases.emplace_back(new test_set_rows(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_I64, { 1, 8, 1, 3 }, { 1, 1 }, 2, false)); |
| 10350 | test_cases.emplace_back(new test_set_rows(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_I32, { 1, 8, 1, 3 }, { 1, 1 }, 2, false)); |
| 10351 | test_cases.emplace_back(new test_set_rows(GGML_TYPE_F32, GGML_TYPE_Q8_0, GGML_TYPE_I32, { 256, 5, 1, 3 }, { 1, 1, }, 1, false)); |
| 10352 | for (ggml_type src_type : {GGML_TYPE_F16, GGML_TYPE_F32}) { |
| 10353 | for (ggml_type type : all_types) { |
| 10354 | for (int b : {1, 7}) { |
| 10355 | for (bool v : {false, true}) { |
| 10356 | test_cases.emplace_back(new test_set_rows(src_type, type, GGML_TYPE_I64, { 256, 5, b, 3 }, { 1, 1, }, 1, v)); |
| 10357 | test_cases.emplace_back(new test_set_rows(src_type, type, GGML_TYPE_I64, { 256, 11, 1, b }, { 2, 3, }, 7, v)); |
| 10358 | |
| 10359 | test_cases.emplace_back(new test_set_rows(src_type, type, GGML_TYPE_I64, { 3*ggml_blck_size(type), 3, b, 1 }, { 2, 3, }, 2, v)); |
| 10360 | |
| 10361 | if (ggml_blck_size(type) == 1) { |
| 10362 | test_cases.emplace_back(new test_set_rows(src_type, type, GGML_TYPE_I64, { 31, 3, b, 1 }, { 2, 3, }, 2, v)); |
| 10363 | test_cases.emplace_back(new test_set_rows(src_type, type, GGML_TYPE_I64, { 33, 5, 1, b }, { 2, 3, }, 1, v)); |
| 10364 | } |
| 10365 | } |
| 10366 | } |
| 10367 | } |
| 10368 | } |
| 10369 | test_cases.emplace_back(new test_set_rows(GGML_TYPE_F16, GGML_TYPE_F16, GGML_TYPE_I64, { 1, 8, 1, 3 }, { 1, 1 }, 2, false)); |
| 10370 | test_cases.emplace_back(new test_set_rows(GGML_TYPE_F16, GGML_TYPE_F16, GGML_TYPE_I32, { 1, 8, 1, 3 }, { 1, 1 }, 2, false)); |
| 10371 | test_cases.emplace_back(new test_set_rows(GGML_TYPE_F16, GGML_TYPE_F16, GGML_TYPE_I64, { 1, 8, 1, 3 }, { 1, 1 }, 2, true)); |
| 10372 | test_cases.emplace_back(new test_set_rows(GGML_TYPE_F16, GGML_TYPE_F16, GGML_TYPE_I32, { 1, 8, 1, 3 }, { 1, 1 }, 2, true)); |
| 10373 | |
| 10374 | for (int mode : { GGML_ROPE_TYPE_NORMAL, GGML_ROPE_TYPE_NEOX, GGML_ROPE_TYPE_MROPE, GGML_ROPE_TYPE_VISION, GGML_ROPE_TYPE_IMROPE }) { |
| 10375 | for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_F32}) { |
| 10376 | for (int ne2 : {1, 8, 512}) { |
| 10377 | test_cases.emplace_back(new test_rope_set_rows(type, GGML_TYPE_I64, { 128, 32, ne2, 1 }, mode)); |
| 10378 | test_cases.emplace_back(new test_rope_set_rows(type, GGML_TYPE_I64, { 128, 32, ne2, 3 }, mode)); |
| 10379 | } |
| 10380 | } |
| 10381 | } |
| 10382 | test_cases.emplace_back(new test_rope_set_rows(GGML_TYPE_F32, GGML_TYPE_I32, { 128, 32, 8, 1 }, GGML_ROPE_TYPE_IMROPE)); |
| 10383 | // partial rotation |
| 10384 | for (int mode : { GGML_ROPE_TYPE_NORMAL, GGML_ROPE_TYPE_NEOX, GGML_ROPE_TYPE_MROPE, GGML_ROPE_TYPE_IMROPE }) { |
| 10385 | for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_F32}) { |
| 10386 | test_cases.emplace_back(new test_rope_set_rows(type, GGML_TYPE_I64, { 256, 4, 8, 1 }, mode, 64)); |
| 10387 | } |
| 10388 | } |
| 10389 | |
| 10390 | for (ggml_type type_input : {GGML_TYPE_F32}) { |
| 10391 | for (ggml_op_pool pool_type : {GGML_OP_POOL_AVG, GGML_OP_POOL_MAX}) { |
| 10392 | for (int k0 : {1, 3}) { |
| 10393 | for (int k1 : {1, 3}) { |
| 10394 | for (int s0 : {1, 2}) { |
| 10395 | for (int s1 : {1, 2}) { |
| 10396 | for (int p0 : {0, 1}) { |
| 10397 | for (int p1 : {0, 1}) { |
| 10398 | test_cases.emplace_back(new test_pool2d(pool_type, type_input, {10, 10, 3, 1}, k0, k1, s0, s1, p0, p1)); |
| 10399 | } |
| 10400 | } |
| 10401 | } |
| 10402 | } |
| 10403 | } |
| 10404 | } |
| 10405 | // MSA indexer block-max shape: wide 1-D window along ne0, no padding, non-divisible width |
| 10406 | for (int64_t iw : {4096, 4160}) { |
| 10407 | for (int blk : {32, 64, 128}) { |
| 10408 | test_cases.emplace_back(new test_pool2d(pool_type, type_input, {iw, 2, 1, 1}, blk, 1, blk, 1, 0, 0)); |
| 10409 | } |
| 10410 | } |
| 10411 | test_cases.emplace_back(new test_pool2d(pool_type, type_input, {259, 5, 3, 2}, 128, 3, 64, 2, 64, 1)); |
| 10412 | test_cases.emplace_back(new test_pool2d(pool_type, type_input, {193, 2, 1, 1}, 128, 1, 128, 1, 128, 2)); |
| 10413 | } |
| 10414 | } |
| 10415 | |
| 10416 | for (ggml_type type_input : {GGML_TYPE_F32}) { |
| 10417 | for (ggml_op_pool pool_type : {GGML_OP_POOL_AVG, GGML_OP_POOL_MAX}) { |
| 10418 | for (int k0 : {1, 2, 3}) { |
| 10419 | for (int s0 : {1, 2, 3}) { |
| 10420 | for (int p0 : {0, 1, 2, 3}) { |
| 10421 | test_cases.emplace_back(new test_pool1d(pool_type, type_input, { 10, 3, 2, 1 }, k0, s0, p0)); |
| 10422 | test_cases.emplace_back(new test_pool1d(pool_type, type_input, { 11, 1, 3, 2 }, k0, s0, p0)); |
| 10423 | test_cases.emplace_back(new test_pool1d(pool_type, type_input, { 128, 2, 1, 3 }, k0, s0, p0)); |
| 10424 | } |
| 10425 | } |
| 10426 | } |
| 10427 | } |
| 10428 | } |
| 10429 | |
| 10430 | #if 0 |
| 10431 | // >4GB im2col destination. Too slow to run by default. |
| 10432 | // Test cases taken from Wan2.1 T2V 1.3B. |
| 10433 | test_cases.emplace_back(new test_im2col (GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32, {832, 480, 192, 4}, {3, 3, 192, 96}, 1, 1, 1, 1, 1, 1, true)); |
| 10434 | test_cases.emplace_back(new test_im2col_3d(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32, {834, 482, 6, 96}, {3, 3,3, 9216}, 96, 1, 1, 1, 0, 0, 0, 1, 1, 1, false)); |
| 10435 | #endif |
| 10436 | |
| 10437 | // im2col 1D |
| 10438 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32, {3000, 128, 1, 1}, {3, 128, 1280, 1}, 1, 0, 1, 0, 1, 0, false)); |
| 10439 | // deepseek4v / deepseek41v aligner: 3x3 stride-3 unfold (F.unfold == im2col) |
| 10440 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32, {48, 33, 1024, 1}, {3, 3, 1024, 1}, 3, 3, 0, 0, 1, 1, true)); |
| 10441 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32, {3000, 128, 1, 1}, {3, 128, 1280, 1}, 1, 0, 1, 0, 1, 0, false)); |
| 10442 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {3000, 128, 1, 1}, {3, 128, 1280, 1}, 1, 0, 1, 0, 1, 0, false)); |
| 10443 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {3000, 384, 1, 1}, {3, 384, 384, 1}, 1, 0, 1, 0, 1, 0, false)); |
| 10444 | // 1D convs of an audio decoder: long sequences, dilated taps, pointwise kernels |
| 10445 | for (int d0 : {1, 3, 9}) { |
| 10446 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {2000 + 6 * d0, 96, 1, 1}, {7, 96, 96, 1}, 1, 0, 0, 0, d0, 0, false)); |
| 10447 | } |
| 10448 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {1500, 192, 1, 1}, {1, 192, 192, 1}, 1, 0, 0, 0, 1, 0, false)); |
| 10449 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32, {1001, 64, 1, 1}, {5, 64, 64, 1}, 2, 0, 2, 0, 1, 0, false)); |
| 10450 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {777, 48, 2, 1}, {3, 48, 48, 1}, 1, 0, 4, 0, 3, 0, false)); |
| 10451 | // several rows of a few output-col blocks each: a thread's range of blocks can start mid-row |
| 10452 | for (int64_t n : {5, 7}) { |
| 10453 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {600, 8, n, 1}, {3, 8, 8, 1}, 1, 0, 1, 0, 1, 0, false)); |
| 10454 | } |
| 10455 | // 2D with a kernel one row tall: with p1 > 0 some output rows fall outside the input height |
| 10456 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {40, 6, 8, 1}, {3, 1, 8, 4}, 1, 1, 1, 1, 1, 1, true)); |
| 10457 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {40, 6, 8, 1}, {3, 1, 8, 4}, 1, 1, 1, 0, 1, 1, true)); |
| 10458 | for (int s0 : {1, 3}) { |
| 10459 | for (int p0 : {0, 3}) { |
| 10460 | for (int d0 : {1, 3}) { |
| 10461 | test_cases.emplace_back(new test_im2col( |
| 10462 | GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32, {20, 2, 2, 1}, {3, 2, 2, 1}, |
| 10463 | s0, 0, p0, 0, d0, 0, false)); |
| 10464 | } |
| 10465 | } |
| 10466 | } |
| 10467 | |
| 10468 | // im2col 2D |
| 10469 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32)); |
| 10470 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F16)); |
| 10471 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32)); |
| 10472 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 10473 | for (int s0 : {1, 3}) { |
| 10474 | for (int s1 : {1, 3}) { |
| 10475 | for (int p0 : {0, 3}) { |
| 10476 | for (int p1 : {0, 3}) { |
| 10477 | for (int d0 : {1, 3}) { |
| 10478 | for (int d1 : {1, 3}) { |
| 10479 | test_cases.emplace_back(new test_im2col( |
| 10480 | GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32, {20, 20, 2, 2}, {3, 3, 2, 2}, |
| 10481 | s0, s1, p0, p1, d0, d1, true)); |
| 10482 | } |
| 10483 | } |
| 10484 | } |
| 10485 | } |
| 10486 | } |
| 10487 | } |
| 10488 | |
| 10489 | // extra tests for im2col 2D |
| 10490 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {12, 12, 1, 32}, {3, 3, 1, 32}, 1, 1, 1, 1, 1, 1, true)); |
| 10491 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {12, 12, 2, 32}, {3, 3, 2, 32}, 1, 1, 1, 1, 1, 1, true)); |
| 10492 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {12, 12, 1, 1024}, {3, 3, 1, 1024}, 1, 1, 1, 1, 1, 1, true)); |
| 10493 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {12, 12, 2, 1024}, {3, 3, 2, 1024}, 1, 1, 1, 1, 1, 1, true)); |
| 10494 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {12, 12, 1, 2048}, {3, 3, 1, 2048}, 1, 1, 1, 1, 1, 1, true)); |
| 10495 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {12, 12, 2, 2048}, {3, 3, 2, 2048}, 1, 1, 1, 1, 1, 1, true)); |
| 10496 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {12, 12, 1, 2560}, {3, 3, 1, 2560}, 1, 1, 1, 1, 1, 1, true)); |
| 10497 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {12, 12, 2, 2560}, {3, 3, 2, 2560}, 1, 1, 1, 1, 1, 1, true)); |
| 10498 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {5, 5, 1, 32}, {3, 4, 1, 32}, 1, 1, 0, 0, 1, 1, true)); |
| 10499 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32, {2, 2, 1536, 729}, {2, 2, 1536, 4096}, 1, 1, 0, 0, 1, 1, true)); |
| 10500 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {128, 128, 1, 2}, {32, 33, 1, 2}, 1, 1, 1, 1, 1, 1, true)); |
| 10501 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {128, 128, 2, 1}, {33, 34, 2, 1}, 1, 1, 1, 1, 1, 1, true)); |
| 10502 | // non-overlapping (stride == kernel, no pad/dilation) with IC*KH well above 256, e.g. SD1.5 1x1 convs |
| 10503 | for (ggml_type dst_type : {GGML_TYPE_F16, GGML_TYPE_F32}) { |
| 10504 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, dst_type, {64, 64, 320, 1}, {1, 1, 320, 1}, 1, 1, 0, 0, 1, 1, true)); |
| 10505 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, dst_type, {16, 16, 1280, 2}, {1, 1, 1280, 1}, 1, 1, 0, 0, 1, 1, true)); |
| 10506 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, dst_type, {8, 8, 2560, 1}, {1, 1, 2560, 1}, 1, 1, 0, 0, 1, 1, true)); |
| 10507 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, dst_type, {32, 32, 192, 1}, {2, 2, 192, 1}, 2, 2, 0, 0, 1, 1, true)); |
| 10508 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, dst_type, {64, 512, 1, 1}, {2, 512, 1, 1}, 2, 0, 0, 0, 1, 0, false)); |
| 10509 | } |
| 10510 | |
| 10511 | // im2col 3D |
| 10512 | test_cases.emplace_back(new test_im2col_3d(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32)); |
| 10513 | test_cases.emplace_back(new test_im2col_3d(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32)); |
| 10514 | test_cases.emplace_back(new test_im2col_3d(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 10515 | for (int s0 : {1, 3}) { |
| 10516 | for (int s1 : {1, 3}) { |
| 10517 | for (int s2 : {1, 3}) { |
| 10518 | for (int p0 : {0, 3}) { |
| 10519 | for (int p1 : {0, 3}) { |
| 10520 | for (int p2 : {0, 3}) { |
| 10521 | for (int d0 : {1, 3}) { |
| 10522 | for (int d1 : {1, 3}) { |
| 10523 | for (int d2 : {1, 3}) { |
| 10524 | for (int IC : {1, 3}) { |
| 10525 | for (bool v : {false, true}) { |
| 10526 | test_cases.emplace_back(new test_im2col_3d( |
| 10527 | GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32, {20, 20, 10, 3}, {3, 3, 3, 3}, |
| 10528 | IC, s0, s1, s2, p0, p1, p2, d0, d1, d2, v)); |
| 10529 | } |
| 10530 | } |
| 10531 | } |
| 10532 | } |
| 10533 | } |
| 10534 | } |
| 10535 | } |
| 10536 | } |
| 10537 | } |
| 10538 | } |
| 10539 | } |
| 10540 | |
| 10541 | // Conv_2D test cases |
| 10542 | #ifdef DETAILED_TESTS |
| 10543 | // Probably we do not have enough time to execute these in the pipeline. |
| 10544 | uint32_t iwh_idx = 0; |
| 10545 | uint32_t kwh_idx = 1; |
| 10546 | uint32_t Cout_idx = 2; |
| 10547 | uint32_t Cin_idx = 3; |
| 10548 | uint32_t B_idx = 4; |
| 10549 | |
| 10550 | std::vector<std::array<int, 5>> cases = { |
| 10551 | //{IWH, KWH, Cout, Cin, B} |
| 10552 | // K=CRS=NPQ=4096 conv_2d matmul performance |
| 10553 | {19, 4, 4096, 256, 16}, |
| 10554 | // K=128, CRS=128, NPQ=4096 |
| 10555 | { 19, 4, 128, 8, 16}, |
| 10556 | // K=130, CRS=128, NPQ=4096 |
| 10557 | { 19, 4, 130, 8, 16}, |
| 10558 | // Edge case: K x CRS is small |
| 10559 | { 19, 2, 4, 4, 16}, |
| 10560 | // A ConvNet's first layer |
| 10561 | { 224, 3, 8, 3, 1 }, |
| 10562 | // A ConvNet's first layer with 2x2 convolution, and 1 channel |
| 10563 | { 224, 2, 8, 1, 1 }, |
| 10564 | // A ConvNet's first layer with 2x2 convolution, and 1 channel, several images in the batch |
| 10565 | { 224, 2, 8, 1, 8 }, |
| 10566 | // A middle layer of a ConvNet |
| 10567 | { 58, 3, 64, 32, 1 }, |
| 10568 | // A middle layer of a ConvNet, several images in the batch |
| 10569 | { 58, 3, 64, 32, 8 }, |
| 10570 | // A deep layer of a ConvNet, several images in the batch |
| 10571 | { 16, 3, 256, 128, 8 } |
| 10572 | }; |
| 10573 | |
| 10574 | for (auto kernel_type : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 10575 | for (auto act_case : cases) { |
| 10576 | test_cases.emplace_back(new test_conv_2d( |
| 10577 | { act_case[iwh_idx], act_case[iwh_idx], act_case[Cin_idx], act_case[B_idx] }, |
| 10578 | { act_case[kwh_idx], act_case[kwh_idx], act_case[Cin_idx], act_case[Cout_idx] }, |
| 10579 | kernel_type, 1, 1, 0, 0, 1, 1, false)); // bool cwhn = false |
| 10580 | test_cases.emplace_back(new test_conv_2d( |
| 10581 | { act_case[iwh_idx], act_case[iwh_idx], act_case[Cin_idx], act_case[B_idx] }, |
| 10582 | { act_case[kwh_idx], act_case[kwh_idx], act_case[Cin_idx], act_case[Cout_idx] }, |
| 10583 | kernel_type, 1, 1, 0, 0, 1, 1, true)); // bool cwhn = true |
| 10584 | } |
| 10585 | } |
| 10586 | #endif |
| 10587 | |
| 10588 | // CONV_2D: |
| 10589 | auto calc_conv_output_size = [](int64_t ins, int64_t ks, int s, int p, int d) -> int64_t { |
| 10590 | return (ins + 2 * p - d * (ks - 1) - 1) / s + 1; |
| 10591 | }; |
| 10592 | |
| 10593 | //uint32_t s0 = 3; |
| 10594 | uint32_t s1 = 5; |
| 10595 | uint32_t p0 = 5; |
| 10596 | //uint32_t p1 = 2; |
| 10597 | uint32_t d0 = 2; |
| 10598 | uint32_t d1 = 4; |
| 10599 | |
| 10600 | for (uint32_t s0 : { 1, 3 }) { |
| 10601 | for (uint32_t p1 : { 2, 5 }) { |
| 10602 | for (uint32_t Cin : { 1, 25 }) { |
| 10603 | for (uint32_t Cout : { 1, 12 }) { |
| 10604 | for (uint32_t KH : { 1, 2, 3, 11 }) { |
| 10605 | for (uint32_t KW : { 1, 2, 3, 11 }) { |
| 10606 | for (uint32_t H : { 1, 133 }) { |
| 10607 | for (uint32_t W : { 1, 141 }) { |
| 10608 | if (calc_conv_output_size(W, KW, s0, p0, d0) > 0 && |
| 10609 | calc_conv_output_size(H, KH, s1, p1, d1) > 0) { |
| 10610 | for (auto kernel_type : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 10611 | test_cases.emplace_back(new test_conv_2d( |
| 10612 | { W, H, Cin, 2 }, { KW, KH, Cin, Cout }, kernel_type, s0, s1, p0, p1, d0, d1, false)); // bool cwhn = false |
| 10613 | test_cases.emplace_back(new test_conv_2d( |
| 10614 | { W, H, Cin, 2 }, { KW, KH, Cin, Cout }, kernel_type, s0, s1, p0, p1, d0, d1, true)); // bool cwhn = true |
| 10615 | } |
| 10616 | } |
| 10617 | } |
| 10618 | } |
| 10619 | } |
| 10620 | } |
| 10621 | } |
| 10622 | } |
| 10623 | } |
| 10624 | } |
| 10625 | for (auto kernel_type : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 10626 | test_cases.emplace_back(new test_conv_2d({ 256, 256, 192, 1 }, { 3, 3, 192, 96 }, kernel_type, 1, 1, 1, 1, 1, 1, false)); // bool cwhn = false |
| 10627 | test_cases.emplace_back(new test_conv_2d({ 256, 256, 192, 1 }, { 3, 3, 192, 96 }, kernel_type, 1, 1, 1, 1, 1, 1, true)); // bool cwhn = true |
| 10628 | } |
| 10629 | test_cases.emplace_back(new test_conv_2d({ 19, 17, 8, 2 }, { 3, 3, 8, 65 }, GGML_TYPE_F16, 1, 1, 1, 1, 1, 1)); |
| 10630 | test_cases.emplace_back(new test_conv_2d({ 19, 17, 16, 3 }, { 3, 3, 16, 33 }, GGML_TYPE_F16, 2, 3, 4, 2, 2, 1)); |
| 10631 | test_cases.emplace_back(new test_conv_2d({ 13, 11, 16, 3 }, { 1, 1, 16, 33 }, GGML_TYPE_F16, 1, 1, 0, 0, 1, 1)); |
| 10632 | test_cases.emplace_back(new test_conv_2d({ 19, 17, 8, 2 }, { 3, 3, 8, 17 }, GGML_TYPE_F16, 1, 1, 1, 1, 1, 1, false, 1)); |
| 10633 | |
| 10634 | // sycl backend will limit task global_range < MAX_INT |
| 10635 | // test cases for 2D im2col with large input W and H (occurs in stable-diffusion) |
| 10636 | // however these cases need to alloc more memory which may fail in some devices (Intel Arc770, etc.) |
| 10637 | // these cases are verified (pass) in Intel(R) Data Center GPU Max 1100 (sycl backend) and NV A30 (cuda backend) |
| 10638 | // test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {1024, 1024, 256, 1}, {3, 3, 256, 1}, 1, 1, 1, 1, 1, 1, true)); |
| 10639 | // test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32, {1024, 1024, 256, 1}, {3, 3, 256, 1}, 1, 1, 1, 1, 1, 1, true)); |
| 10640 | |
| 10641 | for (ggml_type kernel_type : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16}) { |
| 10642 | test_cases.emplace_back(new test_conv_1d_dw({64, 16, 1, 1}, {3, 1, 16, 1}, kernel_type, 1, 0, 1)); |
| 10643 | test_cases.emplace_back(new test_conv_1d_dw({64, 16, 1, 1}, {7, 1, 16, 1}, kernel_type, 1, 3, 1)); |
| 10644 | test_cases.emplace_back(new test_conv_1d_dw({97, 33, 1, 1}, {5, 1, 33, 1}, kernel_type, 2, 2, 2)); |
| 10645 | } |
| 10646 | |
| 10647 | test_cases.emplace_back(new test_conv_2d_dw({17, 34, 9, 1}, {3, 3, 1, 9}, GGML_TYPE_F32, 1, 0, 1, false)); |
| 10648 | test_cases.emplace_back(new test_conv_2d_dw({17, 34, 9, 1}, {3, 3, 1, 9}, GGML_TYPE_F32, 1, 0, 1, true)); |
| 10649 | test_cases.emplace_back(new test_conv_2d_dw({32, 8, 64, 1}, {3, 3, 1, 64}, GGML_TYPE_F32, 2, 1, 1, false)); |
| 10650 | test_cases.emplace_back(new test_conv_2d_dw({32, 8, 64, 1}, {3, 3, 1, 64}, GGML_TYPE_F32, 2, 1, 1, true)); |
| 10651 | |
| 10652 | test_cases.emplace_back(new test_conv_2d_dw({17, 34, 9, 1}, {3, 3, 1, 9}, GGML_TYPE_F16, 1, 0, 1, false)); |
| 10653 | test_cases.emplace_back(new test_conv_2d_dw({17, 34, 9, 1}, {3, 3, 1, 9}, GGML_TYPE_F16, 1, 0, 1, true)); |
| 10654 | test_cases.emplace_back(new test_conv_2d_dw({32, 8, 64, 1}, {3, 3, 1, 64}, GGML_TYPE_F16, 2, 1, 1, false)); |
| 10655 | test_cases.emplace_back(new test_conv_2d_dw({32, 8, 64, 1}, {3, 3, 1, 64}, GGML_TYPE_F16, 2, 1, 1, true)); |
| 10656 | |
| 10657 | // CONV_3D |
| 10658 | auto calc_conv_output_size_3d = [](int64_t ins, int64_t ks, int s, int p, int d) -> int64_t { |
| 10659 | return (ins + 2 * p - d * (ks - 1) - 1) / s + 1; |
| 10660 | }; |
| 10661 | |
| 10662 | for (ggml_type kernel_type : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 10663 | test_cases.emplace_back(new test_conv_3d(1, 4, 6, 78, 54, 512, 3, 3, 3, 1, 1, 1, 0, 0, 0, 1, 1, 1, kernel_type)); |
| 10664 | test_cases.emplace_back(new test_conv_3d(2, 16, 4, 64, 64, 256, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, kernel_type)); |
| 10665 | for (int N : {1, 2}) { |
| 10666 | for (int IC : {1, 3}) { |
| 10667 | for (int OC : {1, 4}) { |
| 10668 | for (int s0 : {1, 2}) { |
| 10669 | for (int p1 : {0, 1}) { |
| 10670 | for (int d2 : {1, 2}) { |
| 10671 | int64_t IW = 20, IH = 22, ID = 18; |
| 10672 | int64_t KW = 3, KH = 3, KD = 3; |
| 10673 | int s1 = s0, s2 = s0; |
| 10674 | int p0 = p1, p2 = p1; |
| 10675 | int d0 = d2, d1 = d2; |
| 10676 | |
| 10677 | if (calc_conv_output_size_3d(IW, KW, s0, p0, d0) <= 0 || |
| 10678 | calc_conv_output_size_3d(IH, KH, s1, p1, d1) <= 0 || |
| 10679 | calc_conv_output_size_3d(ID, KD, s2, p2, d2) <= 0) { |
| 10680 | continue; |
| 10681 | } |
| 10682 | test_cases.emplace_back(new test_conv_3d( |
| 10683 | N, IC, ID, IH, IW, |
| 10684 | OC, KD, KH, KW, |
| 10685 | s0, s1, s2, p0, p1, p2, d0, d1, d2, |
| 10686 | kernel_type)); |
| 10687 | |
| 10688 | // Asymmetric kernel and params |
| 10689 | int64_t asym_KW = 5, asym_KH = 1, asym_KD = 3; |
| 10690 | int asym_s0 = 2, asym_s1 = 1, asym_s2 = 1; |
| 10691 | int asym_p0 = 2, asym_p1 = 0, asym_p2 = 1; |
| 10692 | int asym_d0 = 1, asym_d1 = 1, asym_d2 = 2; |
| 10693 | |
| 10694 | if (calc_conv_output_size_3d(IW, asym_KW, asym_s0, asym_p0, asym_d0) <= 0 || |
| 10695 | calc_conv_output_size_3d(IH, asym_KH, asym_s1, asym_p1, asym_d1) <= 0 || |
| 10696 | calc_conv_output_size_3d(ID, asym_KD, asym_s2, asym_p2, asym_d2) <= 0) { |
| 10697 | continue; |
| 10698 | } |
| 10699 | test_cases.emplace_back(new test_conv_3d( |
| 10700 | N, IC, ID, IH, IW, |
| 10701 | OC, asym_KD, asym_KH, asym_KW, |
| 10702 | asym_s0, asym_s1, asym_s2, asym_p0, asym_p1, asym_p2, asym_d0, asym_d1, asym_d2, |
| 10703 | kernel_type)); |
| 10704 | } |
| 10705 | } |
| 10706 | } |
| 10707 | } |
| 10708 | } |
| 10709 | } |
| 10710 | // Case with kernel size 1 |
| 10711 | test_cases.emplace_back(new test_conv_3d(1, 4, 8, 8, 8, 8, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, kernel_type)); |
| 10712 | test_cases.emplace_back(new test_conv_3d(2, 8, 5, 11, 9, 65, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, kernel_type)); |
| 10713 | test_cases.emplace_back(new test_conv_3d(2, 5, 7, 9, 13, 17, 2, 3, 4, 2, 1, 3, 3, 2, 2, 2, 1, 2, kernel_type)); |
| 10714 | test_cases.emplace_back(new test_conv_3d(3, 16, 3, 7, 9, 33, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, kernel_type)); |
| 10715 | test_cases.emplace_back(new test_conv_3d(2, 8, 7, 5, 9, 33, 3, 1, 1, 1, 1, 2, 0, 0, 2, 1, 1, 2, kernel_type)); |
| 10716 | test_cases.emplace_back(new test_conv_3d(2, 3, 1, 2, 1, 7, 1, 1, 1, 1, 1, 1, 3, 4, 2, 1, 1, 1, kernel_type)); |
| 10717 | test_cases.emplace_back(new test_conv_3d(2, 8, 5, 7, 9, 17, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, kernel_type, 1)); |
| 10718 | test_cases.emplace_back(new test_conv_3d(1, 1, 2, 2, 2, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, kernel_type)); |
| 10719 | test_cases.emplace_back(new test_conv_3d(1, 1, 2, 2, 2, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, kernel_type)); |
| 10720 | test_cases.emplace_back(new test_conv_3d(1, 1, 2, 2, 2, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, kernel_type)); |
| 10721 | } |
| 10722 | |
| 10723 | for(uint32_t Cout : {1, 9}){ |
| 10724 | for(uint32_t Cin : {1, 7}){ |
| 10725 | for(uint32_t K : {1, 3, 1337}){ |
| 10726 | for(uint32_t L : {1, 2, 13}){ |
| 10727 | for(uint32_t s0: {1, 2, 3}){ |
| 10728 | test_cases.emplace_back(new test_conv_transpose_1d({L,Cin,1,1}, {K,Cout,Cin,1}, s0, 0, 1)); |
| 10729 | } |
| 10730 | } |
| 10731 | } |
| 10732 | } |
| 10733 | } |
| 10734 | |
| 10735 | test_cases.emplace_back(new test_conv_transpose_1d()); |
| 10736 | test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 3, 0, 1)); |
| 10737 | test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 2, 0, 1)); |
| 10738 | test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 1, 0, 1)); |
| 10739 | test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,2,2,1}, 2, 0, 1)); |
| 10740 | test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,2,2,1}, 1, 0, 1)); |
| 10741 | test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,1,2,1}, 1, 0, 1)); |
| 10742 | test_cases.emplace_back(new test_conv_transpose_1d({2,1,1,1}, {3,1,1,1}, 1, 0, 1)); |
| 10743 | |
| 10744 | for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16}) { |
| 10745 | // ConvTranspose1d expressed as mul_mat + col2im (DAC decoder upsampling) |
| 10746 | test_cases.emplace_back(new test_col2im_1d(type, 16, 32, 197, 8, 0)); // kernel = 2*stride |
| 10747 | test_cases.emplace_back(new test_col2im_1d(type, 4, 3, 7, 2, 0)); |
| 10748 | test_cases.emplace_back(new test_col2im_1d(type, 1, 5, 13, 1, 0)); // stride 1, no overlap |
| 10749 | test_cases.emplace_back(new test_col2im_1d(type, 6, 4, 11, 3, 1)); // with cropping |
| 10750 | test_cases.emplace_back(new test_col2im_1d(type, 2, 3, 9, 3, 0)); // kernel < stride, gap positions are zeroed |
| 10751 | test_cases.emplace_back(new test_col2im_1d(type, 5, 4, 11, 2, 0)); // kernel not a multiple of stride, alternating overlap |
| 10752 | test_cases.emplace_back(new test_col2im_1d(type, 8, 4, 13, 4, 2)); // padding = stride/2 (DAC causal cropping) |
| 10753 | test_cases.emplace_back(new test_col2im_1d(type, 4, 3, 1, 2, 0)); // single column, pure kernel unfold |
| 10754 | test_cases.emplace_back(new test_col2im_1d(type, 16, 1, 197, 8, 0)); // OC = 1, mono output stage |
| 10755 | test_cases.emplace_back(new test_col2im_1d(type, 1, 5, 13, 3, 0)); // K = 1 with stride > 1, sparse scatter |
| 10756 | test_cases.emplace_back(new test_col2im_1d(type, 8, 2, 3, 2, 5)); // cropping eats most of the signal, T_out = 2 |
| 10757 | } |
| 10758 | |
| 10759 | for (ggml_type kernel_type : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 10760 | test_cases.emplace_back(new test_conv_transpose_2d({3, 2, 3, 1}, {2, 2, 1, 3}, 1, kernel_type)); |
| 10761 | test_cases.emplace_back(new test_conv_transpose_2d({10, 10, 9, 1}, {3, 3, 1, 9}, 2, kernel_type)); |
| 10762 | test_cases.emplace_back(new test_conv_transpose_2d({129, 63, 35, 1}, {3, 3, 48, 35}, 1, kernel_type)); |
| 10763 | test_cases.emplace_back(new test_conv_transpose_2d({10, 10, 9, 2}, {3, 3, 1, 9}, 2, kernel_type)); // for multiple batches |
| 10764 | } |
| 10765 | |
| 10766 | test_cases.emplace_back(new test_count_equal(GGML_TYPE_F32, {4, 500, 1, 1})); |
| 10767 | test_cases.emplace_back(new test_count_equal(GGML_TYPE_F32, {4, 5000, 1, 1})); |
| 10768 | |
| 10769 | test_cases.emplace_back(new test_argmax(GGML_TYPE_F32, {32, 1, 1, 1})); |
| 10770 | test_cases.emplace_back(new test_argmax(GGML_TYPE_F32, {32, 513, 1, 1})); |
| 10771 | test_cases.emplace_back(new test_argmax(GGML_TYPE_F32, {100, 10, 1, 1})); |
| 10772 | test_cases.emplace_back(new test_argmax(GGML_TYPE_F32, {1024, 10, 1, 1})); |
| 10773 | test_cases.emplace_back(new test_argmax(GGML_TYPE_F32, {1024, 12, 1, 1})); |
| 10774 | test_cases.emplace_back(new test_argmax(GGML_TYPE_F32, {2000, 10, 1, 1})); |
| 10775 | test_cases.emplace_back(new test_argmax(GGML_TYPE_F32, {5438, 3, 1, 1})); |
| 10776 | |
| 10777 | for (int ne3 : {1, 3}) { // CUDA backward pass only supports ne3 == 1 |
| 10778 | test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 5, 4, ne3}, {1, 1, 1, 1})); |
| 10779 | test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 5, 4, ne3}, {2, 1, 1, 1})); |
| 10780 | test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 5, 4, ne3}, {1, 2, 1, 1})); |
| 10781 | test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 5, 4, ne3}, {1, 1, 2, 1})); |
| 10782 | test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 5, 4, ne3}, {1, 1, 1, 2})); |
| 10783 | test_cases.emplace_back(new test_repeat(GGML_TYPE_F16, {10, 5, 4, ne3}, {2, 1, 1, 1})); |
| 10784 | test_cases.emplace_back(new test_repeat(GGML_TYPE_I32, {10, 5, 4, ne3}, {2, 1, 1, 1})); |
| 10785 | test_cases.emplace_back(new test_repeat(GGML_TYPE_I16, {10, 5, 4, ne3}, {1, 1, 1, 2})); |
| 10786 | test_cases.emplace_back(new test_repeat(GGML_TYPE_BF16, {10, 5, 4, ne3}, {2, 1, 1, 1})); |
| 10787 | } |
| 10788 | |
| 10789 | for (bool view : {false, true}) { |
| 10790 | test_cases.emplace_back(new test_repeat_back(GGML_TYPE_F32, {8, 6, 4, 2}, {1, 1, 1, 1}, view)); |
| 10791 | test_cases.emplace_back(new test_repeat_back(GGML_TYPE_F32, {8, 6, 4, 2}, {2, 1, 1, 1}, view)); |
| 10792 | test_cases.emplace_back(new test_repeat_back(GGML_TYPE_F32, {8, 6, 4, 2}, {1, 2, 1, 1}, view)); |
| 10793 | test_cases.emplace_back(new test_repeat_back(GGML_TYPE_F32, {8, 6, 4, 2}, {1, 1, 2, 1}, view)); |
| 10794 | test_cases.emplace_back(new test_repeat_back(GGML_TYPE_F32, {8, 6, 4, 2}, {1, 1, 1, 2}, view)); |
| 10795 | } |
| 10796 | |
| 10797 | test_cases.emplace_back(new test_dup(GGML_TYPE_F32)); |
| 10798 | test_cases.emplace_back(new test_dup(GGML_TYPE_F16)); |
| 10799 | test_cases.emplace_back(new test_dup(GGML_TYPE_I32)); |
| 10800 | test_cases.emplace_back(new test_dup(GGML_TYPE_I16)); |
| 10801 | test_cases.emplace_back(new test_dup(GGML_TYPE_F32, {10, 10, 5, 1}, {0, 2, 1, 3})); |
| 10802 | test_cases.emplace_back(new test_dup(GGML_TYPE_F16, {10, 10, 5, 1}, {0, 2, 1, 3})); // dup by rows |
| 10803 | test_cases.emplace_back(new test_dup(GGML_TYPE_F32, {10, 10, 5, 1}, {1, 0, 2, 3})); |
| 10804 | test_cases.emplace_back(new test_dup(GGML_TYPE_F16, {10, 10, 5, 1}, {1, 0, 2, 3})); // dup dst not-contiguous |
| 10805 | test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {0, 2, 1, 3})); |
| 10806 | test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {1, 2, 0, 3})); |
| 10807 | |
| 10808 | for (int dim = 1; dim < GGML_MAX_DIMS; ++dim) { |
| 10809 | test_cases.emplace_back(new test_set(GGML_TYPE_F32, GGML_TYPE_F32, {6, 5, 4, 3}, dim, false)); |
| 10810 | test_cases.emplace_back(new test_set(GGML_TYPE_F32, GGML_TYPE_F32, {6, 5, 4, 3}, dim, true)); |
| 10811 | } |
| 10812 | |
| 10813 | for (int dim = 1; dim < GGML_MAX_DIMS; ++dim) { |
| 10814 | test_cases.emplace_back(new test_set(GGML_TYPE_I32, GGML_TYPE_I32, {6, 5, 4, 3}, dim, false)); |
| 10815 | test_cases.emplace_back(new test_set(GGML_TYPE_I32, GGML_TYPE_I32, {6, 5, 4, 3}, dim, true)); |
| 10816 | } |
| 10817 | |
| 10818 | // same-type copy |
| 10819 | for (ggml_type type : all_types) { |
| 10820 | const auto nk = ggml_blck_size(type); |
| 10821 | |
| 10822 | for (int k = 1; k < 4; ++k) { |
| 10823 | test_cases.emplace_back(new test_cpy(type, type, {k*nk, 2, 3, 4})); |
| 10824 | test_cases.emplace_back(new test_cpy(type, type, {k*nk, 2, 3, 4}, {-1,-1,-1,-1}, {0, 2, 1, 3})); |
| 10825 | test_cases.emplace_back(new test_cpy(type, type, {k*nk, 2, 3, 4}, {-1,-1,-1,-1}, {0, 3, 1, 2}, {0, 2, 1, 3})); |
| 10826 | } |
| 10827 | } |
| 10828 | |
| 10829 | for (ggml_type type_src : {GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_F32}) { |
| 10830 | for (ggml_type type_dst : all_types) { |
| 10831 | test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 4, 4, 4})); |
| 10832 | test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 2, 3, 4}, {-1,-1,-1,-1}, {0, 2, 1, 3})); // cpy by rows |
| 10833 | } |
| 10834 | } |
| 10835 | for (ggml_type type_src : all_types) { |
| 10836 | for (ggml_type type_dst : {GGML_TYPE_F32}) { |
| 10837 | test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 4, 4, 4})); |
| 10838 | test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 2, 3, 4}, {-1,-1,-1,-1}, {0, 2, 1, 3})); // cpy by rows |
| 10839 | } |
| 10840 | } |
| 10841 | for (ggml_type type_src : {GGML_TYPE_F16, GGML_TYPE_F32}) { |
| 10842 | for (ggml_type type_dst : {GGML_TYPE_F16, GGML_TYPE_F32}) { |
| 10843 | test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 2, 3, 4}, {-1,-1,-1,-1}, {1, 0, 2, 3})); // cpy not-contiguous |
| 10844 | } |
| 10845 | } |
| 10846 | for (ggml_type type_src : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16}) { |
| 10847 | test_cases.emplace_back(new test_cpy(type_src, GGML_TYPE_F8_E4M3, {32, 2, 3, 4}, {-1,-1,-1,-1}, {0, 0, 0, 0}, {0, 0, 0, 0}, false, {64, 2, 3, 4})); |
| 10848 | test_cases.emplace_back(new test_cpy(type_src, GGML_TYPE_F8_E4M3, {32, 2, 3, 4}, {-1,-1,-1,-1}, {1, 0, 2, 3}, {1, 0, 2, 3})); |
| 10849 | test_cases.emplace_back(new test_cpy(type_src, GGML_TYPE_F8_E4M3, {32, 2, 3, 4}, {16, 4, 3, 4}, {1, 0, 2, 3}, {0, 2, 1, 3})); |
| 10850 | } |
| 10851 | |
| 10852 | for (ggml_type type_dst : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16}) { |
| 10853 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F8_E4M3, type_dst, {33, 2, 3, 4})); |
| 10854 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F8_E4M3, type_dst, {32, 2, 3, 4}, {-1,-1,-1,-1}, {0, 0, 0, 0}, {0, 0, 0, 0}, false, {64, 2, 3, 4})); |
| 10855 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F8_E4M3, type_dst, {32, 2, 3, 4}, {-1,-1,-1,-1}, {1, 0, 2, 3}, {1, 0, 2, 3})); |
| 10856 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F8_E4M3, type_dst, {32, 2, 3, 4}, {16, 4, 3, 4}, {1, 0, 2, 3}, {0, 2, 1, 3})); |
| 10857 | } |
| 10858 | |
| 10859 | // quant block count not a multiple of the kernel block size |
| 10860 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_Q4_0, {96, 1, 1, 1})); |
| 10861 | test_cases.emplace_back(new test_cpy(GGML_TYPE_Q4_0, GGML_TYPE_F32, {96, 1, 1, 1})); |
| 10862 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_I32, {256, 2, 3, 4})); |
| 10863 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_I32, {256, 2, 3, 4}, {-1,-1,-1,-1}, {1, 0, 2, 3})); |
| 10864 | test_cases.emplace_back(new test_cpy(GGML_TYPE_I32, GGML_TYPE_F32, {256, 2, 3, 4})); |
| 10865 | test_cases.emplace_back(new test_cpy(GGML_TYPE_I32, GGML_TYPE_F32, {256, 2, 3, 4}, {-1,-1,-1,-1}, {1, 0, 2, 3})); |
| 10866 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F16, GGML_TYPE_F16, {256, 4, 3, 1}, {-1,-1,-1,-1}, {0, 0, 0, 0}, {0, 0, 0, 0}, true)); |
| 10867 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_F32, {256, 4, 3, 1}, {-1,-1,-1,-1}, {0, 0, 0, 0}, {0, 0, 0, 0}, true)); |
| 10868 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_F32, {256, 4, 3, 3}, {-1,-1,-1,-1}, {0, 0, 0, 0}, {0, 0, 0, 0}, true)); |
| 10869 | test_cases.emplace_back(new test_cpy(GGML_TYPE_BF16, GGML_TYPE_BF16, {256, 4, 3, 1}, {-1,-1,-1,-1}, {0, 0, 0, 0}, {0, 0, 0, 0}, true)); |
| 10870 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F16, GGML_TYPE_F16, {256, 4, 1, 1}, {-1,-1,-1,-1}, {0, 0, 0, 0}, {0, 0, 0, 0}, true)); |
| 10871 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_F32, {256, 4, 1, 1}, {-1,-1,-1,-1}, {0, 0, 0, 0}, {0, 0, 0, 0}, true)); |
| 10872 | test_cases.emplace_back(new test_cpy(GGML_TYPE_BF16, GGML_TYPE_BF16, {256, 4, 1, 1}, {-1,-1,-1,-1}, {0, 0, 0, 0}, {0, 0, 0, 0}, true)); |
| 10873 | test_cases.emplace_back(new test_cpy(GGML_TYPE_I32, GGML_TYPE_I32, {256, 4, 1, 1}, {-1,-1,-1,-1}, {0, 0, 0, 0}, {0, 0, 0, 0}, true)); |
| 10874 | test_cases.emplace_back(new test_cpy(GGML_TYPE_I32, GGML_TYPE_I32, {256, 1, 4, 1}, {-1,-1,-1,-1}, {1, 2, 0, 3}, {0, 0, 0, 0})); |
| 10875 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_F32, {256, 1, 4, 1}, {-1,-1,-1,-1}, {1, 2, 0, 3}, {0, 0, 0, 0})); |
| 10876 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_F32, {2, 2097121, 1, 1}, {-1,-1,-1,-1}, {1, 0, 2, 3})); |
| 10877 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_F32, {1, 64, 48, 1}, {-1,-1,-1,-1}, {0, 2, 1, 3})); // ne[0] == 1 permute |
| 10878 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F16, GGML_TYPE_F16, {1, 64, 48, 1}, {-1,-1,-1,-1}, {0, 2, 1, 3})); // ne[0] == 1 permute |
| 10879 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_F32, {2, 2, 524281, 1}, {-1,-1,-1,-1}, {1, 0, 2, 3})); |
| 10880 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_F32, {128, 2, 3, 1}, {128, 2, 3, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, false, {128, 4, 3, 1})); // strided dst |
| 10881 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F16, GGML_TYPE_F16, {128, 2, 3, 1}, {128, 2, 3, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, false, {128, 4, 3, 1})); // strided dst |
| 10882 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_F32, {64, 16, 1, 1}, {1, 64, 16, 1}, {1, 2, 0, 3}, {0, 0, 0, 0}, false, {32, 64, 16, 1})); // permuted src, strided dst |
| 10883 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F16, GGML_TYPE_F16, {64, 16, 1, 1}, {1, 64, 16, 1}, {1, 2, 0, 3}, {0, 0, 0, 0}, false, {32, 64, 16, 1})); // permuted src, strided dst |
| 10884 | |
| 10885 | // CPY - different src/dst shapes (reshaping via CPY) |
| 10886 | // Use permutations of {3, 5, 7, 32}. Total elements: 3*5*7*32 = 3360. |
| 10887 | // Each src permutation is tested against canonical sorted and reverse dst (skip self). |
| 10888 | { |
| 10889 | std::array<int64_t, 4> dims = {3, 5, 7, 32}; |
| 10890 | std::sort(dims.begin(), dims.end()); |
| 10891 | std::array<int64_t, 4> canonical = dims; |
| 10892 | std::array<int64_t, 4> reversed = {32, 7, 5, 3}; |
| 10893 | for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 10894 | std::array<int64_t, 4> cur = dims; |
| 10895 | do { |
| 10896 | if (cur != canonical) { |
| 10897 | test_cases.emplace_back(new test_cpy(type, type, cur, canonical)); |
| 10898 | } |
| 10899 | if (cur != reversed) { |
| 10900 | test_cases.emplace_back(new test_cpy(type, type, cur, reversed)); |
| 10901 | } |
| 10902 | if (cur[0] == 32 && type == GGML_TYPE_F32) { |
| 10903 | if (canonical[0] == 32) { |
| 10904 | test_cases.emplace_back(new test_cpy(GGML_TYPE_Q4_0, GGML_TYPE_Q4_0, cur, canonical)); |
| 10905 | } |
| 10906 | if (reversed[0] == 32) { |
| 10907 | test_cases.emplace_back(new test_cpy(GGML_TYPE_Q4_0, GGML_TYPE_Q4_0, cur, reversed)); |
| 10908 | } |
| 10909 | } |
| 10910 | std::next_permutation(cur.begin(), cur.end()); |
| 10911 | } while (cur != canonical); |
| 10912 | } |
| 10913 | } |
| 10914 | |
| 10915 | for (ggml_type type_dst : { GGML_TYPE_F32, GGML_TYPE_I32, GGML_TYPE_F16, GGML_TYPE_BF16 }) { |
| 10916 | for (bool use_view_slice : { true, false }) { |
| 10917 | for (std::array<int64_t, 4> ne : std::initializer_list<std::array<int64_t, 4>>{ {2, 1, 1, 1}, {2, 1, 3, 5}, |
| 10918 | {2, 3, 5, 7}, {1, 4, 4, 1}, {1, 8, 17, 1}, {10, 10, 10, 1} }) { |
| 10919 | if (use_view_slice && (type_dst == GGML_TYPE_F16 || type_dst == GGML_TYPE_BF16)) { |
| 10920 | continue; // TODO: add after WebGPU is fixed |
| 10921 | } |
| 10922 | test_cases.emplace_back(new test_cont(type_dst, ne, use_view_slice)); |
| 10923 | } |
| 10924 | } |
| 10925 | } |
| 10926 | |
| 10927 | for (ggml_type type_dst : { GGML_TYPE_F32, GGML_TYPE_F16 }) { |
| 10928 | for (std::array<int64_t, 4> ne : std::initializer_list<std::array<int64_t, 4>>{ |
| 10929 | {10, 10, 10, 1}, {33, 5, 7, 1}, {64, 3, 65, 1}, {2, 3, 5, 7}, |
| 10930 | // large, tile-aligned and tile-unaligned, matching the perf cases |
| 10931 | {1024, 64, 64, 1}, {2304, 64, 64, 1}, {1000, 33, 65, 1} }) { |
| 10932 | for (std::array<int64_t, 4> perm : std::initializer_list<std::array<int64_t, 4>>{ |
| 10933 | {2, 1, 0, 3}, // 0<->2 swap |
| 10934 | {1, 2, 0, 3}, // 3-cycle |
| 10935 | {0, 2, 1, 3} }) { |
| 10936 | test_cases.emplace_back(new test_cont(type_dst, ne, false, perm)); |
| 10937 | } |
| 10938 | } |
| 10939 | } |
| 10940 | |
| 10941 | // source strides over 16 MB (24-bit DMA descriptor limit) |
| 10942 | test_cases.emplace_back(new test_cont(GGML_TYPE_F32, {512, 8193, 2, 1}, false, {2, 1, 0, 3})); |
| 10943 | test_cases.emplace_back(new test_cont(GGML_TYPE_F32, {256, 16385, 2, 1}, false, {0, 2, 1, 3})); |
| 10944 | |
| 10945 | // channel swap with sizes not multiples of 32/64 tile |
| 10946 | for (ggml_type type_dst : { GGML_TYPE_F32, GGML_TYPE_F16 }) { |
| 10947 | test_cases.emplace_back(new test_cont(type_dst, {33, 17, 5, 19}, false, {1, 2, 3, 0})); |
| 10948 | } |
| 10949 | |
| 10950 | // small channel dims and extent-1 dims |
| 10951 | test_cases.emplace_back(new test_cont(GGML_TYPE_F32, {8, 104, 60, 13}, false, {1, 2, 3, 0})); |
| 10952 | test_cases.emplace_back(new test_cont(GGML_TYPE_F16, {8, 104, 60, 13}, false, {1, 2, 3, 0})); |
| 10953 | test_cases.emplace_back(new test_cont(GGML_TYPE_F32, {16, 1, 50, 7}, false, {1, 2, 3, 0})); |
| 10954 | |
| 10955 | // full reversal: contiguous src dim lands on dst dim 3 |
| 10956 | test_cases.emplace_back(new test_cont(GGML_TYPE_F32, {33, 5, 7, 9}, false, {3, 2, 1, 0})); |
| 10957 | test_cases.emplace_back(new test_cont(GGML_TYPE_F32, {33, 64, 32, 16}, false, {3, 2, 1, 0})); |
| 10958 | |
| 10959 | // permutes that move dense blocks (ne[0] stays dim 0) |
| 10960 | for (ggml_type type_dst : { GGML_TYPE_F32, GGML_TYPE_F16 }) { |
| 10961 | test_cases.emplace_back(new test_cont(type_dst, {4, 65, 9, 2}, false, {0, 2, 1, 3})); |
| 10962 | } |
| 10963 | test_cases.emplace_back(new test_cont(GGML_TYPE_F16, {64, 9, 7, 1}, false, {0, 2, 1, 3})); |
| 10964 | |
| 10965 | // transpose with i32 and CPY with reshape |
| 10966 | test_cases.emplace_back(new test_cont(GGML_TYPE_I32, {33, 17, 5, 19}, false, {1, 2, 3, 0})); |
| 10967 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_F32, {33, 17, 5, 1}, {17, 33, 5, 1}, {0, 0, 0, 0}, {1, 0, 2, 3})); |
| 10968 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F16, GGML_TYPE_F16, {8, 52, 39, 1}, {8, 39, 52, 1}, {0, 0, 0, 0}, {0, 2, 1, 3})); |
| 10969 | |
| 10970 | auto add_test_bin_bcast = [&](ggml_type type, std::array<int64_t, 4> ne, std::array<int, 4> nr, bool perm1 = false, bool src_overlap = false) { |
| 10971 | for (auto op : {ggml_add, ggml_sub, ggml_mul, ggml_div}) { |
| 10972 | test_cases.emplace_back(new test_bin_bcast(op, type, ne, nr, 1, perm1, src_overlap)); |
| 10973 | } |
| 10974 | }; |
| 10975 | for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_F32, GGML_TYPE_BF16}) { |
| 10976 | for (bool perm1 : {false, true}) { |
| 10977 | add_test_bin_bcast(type, {1, 1, 8, 1}, {1, 1, 1, 1}, perm1); |
| 10978 | add_test_bin_bcast(type, {1, 1, 1, 1}, {32, 1, 1, 1}, perm1); |
| 10979 | add_test_bin_bcast(type, {1, 1, 320, 320}, {1, 1, 1, 1}, perm1); |
| 10980 | add_test_bin_bcast(type, {10, 5, 1, 1}, {1, 1, 1, 1}, perm1); |
| 10981 | add_test_bin_bcast(type, {10, 5, 4, 1}, {1, 1, 1, 1}, perm1); |
| 10982 | add_test_bin_bcast(type, {10, 5, 4, 3}, {1, 1, 1, 1}, perm1); |
| 10983 | add_test_bin_bcast(type, {10, 5, 4, 3}, {2, 1, 1, 1}, perm1); |
| 10984 | add_test_bin_bcast(type, {10, 5, 4, 3}, {1, 2, 1, 1}, perm1); |
| 10985 | add_test_bin_bcast(type, {10, 5, 4, 3}, {1, 1, 2, 1}, perm1); |
| 10986 | add_test_bin_bcast(type, {10, 5, 4, 3}, {1, 1, 1, 2}, perm1); |
| 10987 | add_test_bin_bcast(type, {10, 5, 4, 3}, {1, 1, 2, 2}, perm1); |
| 10988 | add_test_bin_bcast(type, {10, 5, 4, 3}, {1, 2, 2, 2}, perm1); |
| 10989 | add_test_bin_bcast(type, {10, 5, 4, 3}, {2, 2, 2, 2}, perm1); |
| 10990 | } |
| 10991 | |
| 10992 | // src_overlap |
| 10993 | add_test_bin_bcast(type, {10, 5, 4, 6}, {1, 1, 1, 1}, false, true); |
| 10994 | add_test_bin_bcast(type, {10, 5, 4, 5}, {1, 1, 1, 1}, false, true); |
| 10995 | add_test_bin_bcast(type, {1, 1, 120, 120}, {1, 1, 1, 1}, false, true); |
| 10996 | add_test_bin_bcast(type, {1, 1, 4, 320}, {1, 1, 1, 1}, false, true); |
| 10997 | |
| 10998 | // test case for k_bin_bcast_unravel in CUDA backend |
| 10999 | add_test_bin_bcast(type, {1, 1, 65536, 1}, {256, 1, 1, 1}); |
| 11000 | |
| 11001 | // stable diffusion |
| 11002 | add_test_bin_bcast(type, {1280, 1, 1, 1}, {1, 1, 1, 1}); |
| 11003 | add_test_bin_bcast(type, {1280, 1, 1, 1}, {1, 16, 16, 1}); |
| 11004 | add_test_bin_bcast(type, {1280, 16, 16, 1}, {1, 1, 1, 1}); |
| 11005 | add_test_bin_bcast(type, {1280, 1, 1, 1}, {1, 256, 1, 1}); |
| 11006 | add_test_bin_bcast(type, {1, 1, 1280, 1}, {16, 16, 1, 1}); |
| 11007 | add_test_bin_bcast(type, {16, 16, 1280, 1}, {1, 1, 1, 1}); |
| 11008 | add_test_bin_bcast(type, {1, 1, 1920, 1}, {16, 16, 1, 1}); |
| 11009 | add_test_bin_bcast(type, {1, 1, 2560, 1}, {16, 16, 1, 1}); |
| 11010 | add_test_bin_bcast(type, {1, 1, 1280, 1}, {32, 32, 1, 1}); |
| 11011 | add_test_bin_bcast(type, {1, 1, 1920, 1}, {32, 32, 1, 1}); |
| 11012 | add_test_bin_bcast(type, {1, 1, 640, 1}, {32, 32, 1, 1}); |
| 11013 | add_test_bin_bcast(type, {5120, 1, 1, 1}, {1, 256, 1, 1}); |
| 11014 | add_test_bin_bcast(type, {640, 1, 1, 1}, {1, 1, 1, 1}); |
| 11015 | add_test_bin_bcast(type, {64, 262144, 1, 1}, {1, 1, 1, 1}); |
| 11016 | add_test_bin_bcast(type, {128, 1, 8, 1}, {1, 4, 1, 1}); |
| 11017 | //add_test_bin_bcast(type, {3, 3, 2560, 1280}, {1, 1, 1, 1}); |
| 11018 | //add_test_bin_bcast(type, {3, 3, 2560, 1280}, {2, 1, 1, 1}); |
| 11019 | } |
| 11020 | |
| 11021 | for (auto op : {ggml_add, ggml_add_inplace}) { |
| 11022 | for (int64_t n : {1, 7, 33}) { |
| 11023 | test_cases.emplace_back(new test_bin_bcast(op, GGML_TYPE_F8_E4M3, {n, 5, 2, 1}, {1, 1, 1, 1}, 1, false, false, GGML_TYPE_F32)); |
| 11024 | } |
| 11025 | } |
| 11026 | |
| 11027 | for (auto op : {ggml_add, ggml_mul}) { |
| 11028 | test_cases.emplace_back(new test_bin_bcast(op, GGML_TYPE_F32, {1, 2, 2, 2}, {17, 1, 1, 1}, 1, false, false, GGML_TYPE_COUNT, true)); |
| 11029 | } |
| 11030 | test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_F32, {2, 2, 2, 2}, {17, 1, 1, 1}, 1, false, false, GGML_TYPE_COUNT, true)); |
| 11031 | |
| 11032 | // single inplace tests, especially important for WebGPU backend since kernels for inplace vs. not are different |
| 11033 | test_cases.emplace_back(new test_bin_bcast(ggml_add_inplace, GGML_TYPE_F32, {16, 5, 4, 3}, {1, 1, 1, 1}, 16)); |
| 11034 | test_cases.emplace_back(new test_bin_bcast(ggml_mul_inplace, GGML_TYPE_F32, {16, 5, 4, 3}, {1, 1, 1, 1}, 16)); |
| 11035 | test_cases.emplace_back(new test_bin_bcast(ggml_sub_inplace, GGML_TYPE_F32, {16, 5, 4, 3}, {1, 1, 1, 1}, 16)); |
| 11036 | test_cases.emplace_back(new test_bin_bcast(ggml_div_inplace, GGML_TYPE_F32, {16, 5, 4, 3}, {1, 1, 1, 1}, 16)); |
| 11037 | |
| 11038 | // BF16 activations with F32 weights |
| 11039 | for (auto op : {ggml_add, ggml_sub, ggml_mul, ggml_div}) { |
| 11040 | test_cases.emplace_back(new test_bin_bcast(op, GGML_TYPE_BF16, {10, 5, 4, 3}, {1, 1, 1, 1}, 1, false, false, GGML_TYPE_F32)); |
| 11041 | test_cases.emplace_back(new test_bin_bcast(op, GGML_TYPE_BF16, {10, 5, 4, 3}, {2, 2, 2, 2}, 1, false, false, GGML_TYPE_F32)); |
| 11042 | test_cases.emplace_back(new test_bin_bcast(op, GGML_TYPE_BF16, {5120, 1, 1, 1}, {1, 32, 1, 1}, 1, false, false, GGML_TYPE_F32)); |
| 11043 | } |
| 11044 | |
| 11045 | // fusion |
| 11046 | test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_F32, {10, 5, 4, 3}, {2, 1, 1, 1}, 2)); |
| 11047 | test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_F16, {10, 5, 4, 3}, {2, 1, 1, 1}, 2)); |
| 11048 | test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_BF16, {10, 5, 4, 3}, {2, 1, 1, 1}, 2)); |
| 11049 | test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_BF16, {16, 5, 4, 3}, {1, 2, 1, 1}, 3)); |
| 11050 | test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_F32, {16, 5, 4, 3}, {1, 1, 1, 1}, 2, true)); |
| 11051 | test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_F32, {16, 5, 4, 3}, {1, 2, 1, 1}, 3)); |
| 11052 | test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_F32, {10, 5, 4, 3}, {1, 1, 2, 1}, 4)); |
| 11053 | test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_F32, {16, 5, 4, 3}, {1, 1, 1, 2}, 5)); |
| 11054 | test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_F32, {10, 5, 4, 3}, {1, 1, 2, 2}, 6)); |
| 11055 | test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_F32, {10, 5, 4, 3}, {1, 2, 2, 2}, 7)); |
| 11056 | test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_F32, {16, 5, 4, 3}, {2, 2, 2, 2}, 8)); |
| 11057 | test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_F32, {16, 5, 4, 3}, {1, 1, 1, 1}, 16)); |
| 11058 | |
| 11059 | test_cases.emplace_back(new test_scale()); |
| 11060 | test_cases.emplace_back(new test_scale(GGML_TYPE_F32, {10, 10, 10, 10}, 2.0f, 1.0f)); |
| 11061 | test_cases.emplace_back(new test_scale(GGML_TYPE_F32, {10, 10, 10, 10}, 2.0f, 1.0f, true)); // inplace test |
| 11062 | test_cases.emplace_back(new test_scale(GGML_TYPE_F32, {100, 10, 10, 10}, 2.0f, 1.0f)); |
| 11063 | test_cases.emplace_back(new test_scale(GGML_TYPE_BF16, {10, 10, 10, 10}, 2.0f, 1.0f)); |
| 11064 | test_cases.emplace_back(new test_scale(GGML_TYPE_BF16, {10, 10, 10, 10}, 2.0f, 1.0f, true)); // inplace test |
| 11065 | test_cases.emplace_back(new test_scale(GGML_TYPE_BF16, {100, 10, 10, 10}, 0.5f, 0.0f)); |
| 11066 | test_cases.emplace_back(new test_scale(GGML_TYPE_F32, {10, 10, 10, 10}, 2.0f, 0.0f, false, 1)); // view test |
| 11067 | test_cases.emplace_back(new test_scale(GGML_TYPE_F32, {10, 10, 10, 10}, 2.0f, 1.0f, false, 1)); // view test |
| 11068 | test_cases.emplace_back(new test_scale(GGML_TYPE_F32, {10, 10, 10, 10}, 2.0f, 1.0f, true, 1)); // view test, inplace |
| 11069 | test_cases.emplace_back(new test_scale(GGML_TYPE_BF16, {10, 10, 10, 10}, 2.0f, 1.0f, true, 1)); // view test, inplace |
| 11070 | test_cases.emplace_back(new test_softcap(GGML_TYPE_F32, {10, 10, 10, 10}, 50.0f)); |
| 11071 | test_cases.emplace_back(new test_silu_back()); |
| 11072 | |
| 11073 | for (float eps : { 0.0f, 1e-6f, 1e-4f, 1e-1f, 10.f }) { |
| 11074 | for (uint32_t n : { 64, 1025 }) { |
| 11075 | for (bool v : { false, true }) { |
| 11076 | test_cases.emplace_back(new test_norm(GGML_TYPE_F32, { n, 5, 4, 3 }, v, eps)); |
| 11077 | test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, { n, 5, 4, 3 }, v, eps)); |
| 11078 | } |
| 11079 | test_cases.emplace_back(new test_norm(GGML_TYPE_F32, { n, 5, 4, 3 }, false, eps, true)); |
| 11080 | test_cases.emplace_back(new test_norm_scale(GGML_TYPE_F32, { n, 5, 4, 3 }, eps, false, 1.5f)); |
| 11081 | test_cases.emplace_back(new test_norm_scale(GGML_TYPE_F32, { n, 5, 4, 3 }, eps, true, 1.5f)); |
| 11082 | test_cases.emplace_back(new test_rms_norm_back(GGML_TYPE_F32, { n, 5, 4, 3 }, eps)); |
| 11083 | test_cases.emplace_back(new test_l2_norm(GGML_TYPE_F32, { n, 5, 4, 3 }, eps, false)); |
| 11084 | test_cases.emplace_back(new test_l2_norm(GGML_TYPE_F32, { n, 5, 4, 3 }, eps, true)); |
| 11085 | test_cases.emplace_back(new test_l2_norm(GGML_TYPE_F32, { n, 5, 4, 3 }, eps, false, true)); |
| 11086 | // sibling batching: strided (production shape) and contiguous, 2 and 4 wide |
| 11087 | test_cases.emplace_back(new test_l2_norm_batch(GGML_TYPE_F32, { n, 5, 4, 3 }, 2, eps, true)); |
| 11088 | test_cases.emplace_back(new test_l2_norm_batch(GGML_TYPE_F32, { n, 5, 4, 3 }, 4, eps, true)); |
| 11089 | test_cases.emplace_back(new test_l2_norm_batch(GGML_TYPE_F32, { n, 5, 4, 3 }, 4, eps, false)); |
| 11090 | } |
| 11091 | // row lengths that are not a multiple of 32, for the scalar (33) and float4 (132, 260) paths |
| 11092 | for (uint32_t n : { 33, 132, 260 }) { |
| 11093 | for (bool v : { false, true }) { |
| 11094 | test_cases.emplace_back(new test_norm(GGML_TYPE_F32, { n, 5, 4, 3 }, v, eps)); |
| 11095 | test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, { n, 5, 4, 3 }, v, eps)); |
| 11096 | } |
| 11097 | } |
| 11098 | } |
| 11099 | |
| 11100 | // in-place tests |
| 11101 | test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, {64, 5, 4, 3}, false, 1e-6f, true)); |
| 11102 | // shapes below exceed the CUDA gridDim.y/gridDim.z limit of 65535 (#27901) |
| 11103 | test_cases.emplace_back(new test_norm (GGML_TYPE_F32, {4, 1, 65536, 1}, false, 1e-6f)); |
| 11104 | test_cases.emplace_back(new test_rms_norm (GGML_TYPE_F32, {4, 1, 65536, 1}, false, 1e-6f, false)); |
| 11105 | test_cases.emplace_back(new test_rms_norm (GGML_TYPE_F32, {4, 1, 1, 65536}, false, 1e-6f, false)); |
| 11106 | test_cases.emplace_back(new test_l2_norm (GGML_TYPE_F32, {4, 1, 65536, 1}, 1e-12f, false, false)); |
| 11107 | test_cases.emplace_back(new test_rms_norm_mul_add(GGML_TYPE_F32, {4, 1, 65536, 1}, 1e-6f, false, false)); |
| 11108 | |
| 11109 | for (uint32_t n : {1, 127, 129, 511, 513, 2816, 4096, 16385}) { |
| 11110 | test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, {n, 3, 2, 1})); |
| 11111 | test_cases.emplace_back(new test_rms_norm_mul_rope({n, 3, 2, 1}, 1e-6f, false, false, true, GGML_ROPE_TYPE_NORMAL, true, false)); |
| 11112 | test_cases.emplace_back(new test_rms_norm_mul_add(GGML_TYPE_F32, {n, 3, 2, 1}, 1e-6f, false, false, false, false, true)); |
| 11113 | } |
| 11114 | |
| 11115 | for (ggml_type set_rows_type : { GGML_TYPE_F32, GGML_TYPE_F16 }) { |
| 11116 | test_cases.emplace_back(new test_rms_norm_mul_rope({ 256, 1, 1, 1 }, 1e-6f, false, true, false, GGML_ROPE_TYPE_NORMAL, false, false, set_rows_type)); |
| 11117 | test_cases.emplace_back(new test_rms_norm_mul_rope({ 128, 4, 3, 1 }, 1e-6f, false, true, false, GGML_ROPE_TYPE_NORMAL, false, false, set_rows_type)); |
| 11118 | for (int mode : {GGML_ROPE_TYPE_NORMAL, GGML_ROPE_TYPE_NEOX}) { |
| 11119 | for (int64_t n : {64, 256, 512, 1024}) { |
| 11120 | test_cases.emplace_back(new test_rms_norm_mul_rope({n, 4, 3, 1}, 1e-6f, false, true, true, mode, true, true, set_rows_type)); |
| 11121 | } |
| 11122 | } |
| 11123 | } |
| 11124 | |
| 11125 | for (float eps : { 0.0f, 1e-6f, 1e-4f, 1e-1f, 1.0f }) { |
| 11126 | for (uint32_t n : { 64, 1025 }) { |
| 11127 | test_cases.emplace_back(new test_rms_norm_mul_add(GGML_TYPE_F32, { n, 5, 4, 3 }, eps, false)); |
| 11128 | test_cases.emplace_back(new test_rms_norm_mul_add(GGML_TYPE_F32, { n, 5, 4, 3 }, eps, true)); |
| 11129 | test_cases.emplace_back(new test_norm_mul_add(GGML_TYPE_F32, { n, 5, 4, 3 }, eps, false)); |
| 11130 | test_cases.emplace_back(new test_norm_mul_add(GGML_TYPE_F32, { n, 5, 4, 3 }, eps, true)); |
| 11131 | test_cases.emplace_back(new test_add_rms_norm(GGML_TYPE_F32, { n, 5, 4, 3 }, eps, false)); |
| 11132 | test_cases.emplace_back(new test_add_rms_norm(GGML_TYPE_F32, { n, 5, 4, 3 }, eps, true)); |
| 11133 | } |
| 11134 | } |
| 11135 | for (uint32_t n : {1, 511, 1025, 8192, 33*512}) { |
| 11136 | for (bool multi_add : {false, true}) { |
| 11137 | test_cases.emplace_back(new test_rms_norm_mul_add(GGML_TYPE_F32, {n, 1, 1, 1}, 1e-6f, false, multi_add)); |
| 11138 | } |
| 11139 | test_cases.emplace_back(new test_add_rms_norm(GGML_TYPE_F32, {n, 1, 1, 1}, 1e-6f, false)); |
| 11140 | } |
| 11141 | for (uint32_t n : {64, 1025}) { |
| 11142 | test_cases.emplace_back(new test_add_add(GGML_TYPE_F32, GGML_TYPE_F32, { n, 5, 4, 3 }, false, false)); |
| 11143 | test_cases.emplace_back(new test_add_add(GGML_TYPE_F32, GGML_TYPE_F32, { n, 5, 4, 3 }, true, false)); |
| 11144 | test_cases.emplace_back(new test_add_add(GGML_TYPE_F32, GGML_TYPE_F32, { n, 5, 4, 3 }, false, true)); |
| 11145 | test_cases.emplace_back(new test_add_add(GGML_TYPE_F16, GGML_TYPE_F16, { n, 5, 4, 3 }, false, false)); |
| 11146 | test_cases.emplace_back(new test_add_add(GGML_TYPE_F16, GGML_TYPE_F32, { n, 5, 4, 3 }, false, false)); |
| 11147 | test_cases.emplace_back(new test_add_add(GGML_TYPE_F16, GGML_TYPE_F32, { n, 5, 4, 3 }, true, false)); |
| 11148 | } |
| 11149 | |
| 11150 | test_cases.emplace_back(new test_rms_norm_mul_add(GGML_TYPE_F32, { 1536, 1, 1, 1 }, 1e-6f, false, false, true)); |
| 11151 | test_cases.emplace_back(new test_rms_norm_mul_add(GGML_TYPE_F32, { 256, 4, 1, 1 }, 1e-6f, false, false, true)); |
| 11152 | test_cases.emplace_back(new test_rms_norm_mul_add(GGML_TYPE_F32, { 256, 4, 3, 2 }, 1e-6f, false, false, true)); |
| 11153 | test_cases.emplace_back(new test_rms_norm_mul_add(GGML_TYPE_F32, { 256, 4, 3, 2 }, 1e-6f, false, false, true, false, true)); |
| 11154 | test_cases.emplace_back(new test_rms_norm_mul_add(GGML_TYPE_F32, { 1536, 1, 1, 1 }, 1e-6f, false, false, false, true)); |
| 11155 | test_cases.emplace_back(new test_rms_norm_mul_add(GGML_TYPE_F32, { 256, 4, 1, 1 }, 1e-6f, false, false, false, true)); |
| 11156 | |
| 11157 | test_cases.emplace_back(new test_rms_norm_mul_rope({128, 4, 7, 2})); |
| 11158 | test_cases.emplace_back(new test_rms_norm_mul_rope({128, 4, 7, 2}, 1e-6f, false, true)); |
| 11159 | |
| 11160 | for (auto multi_add : {false, true}) { |
| 11161 | for (auto set_rows : {false, true}) { |
| 11162 | for (auto broadcast : {false, true}) { |
| 11163 | for (auto rope : {GGML_ROPE_TYPE_NORMAL, GGML_ROPE_TYPE_NEOX, GGML_ROPE_TYPE_IMROPE}) { |
| 11164 | test_cases.emplace_back(new test_rms_norm_mul_rope({768, 1, 1, 1}, 1e-6f, multi_add, set_rows, broadcast, rope)); |
| 11165 | test_cases.emplace_back(new test_rms_norm_mul_rope({768, 3, 1, 1}, 1e-6f, multi_add, set_rows, broadcast, rope)); |
| 11166 | test_cases.emplace_back(new test_rms_norm_mul_rope({768, 3, 5, 1}, 1e-6f, multi_add, set_rows, broadcast, rope)); |
| 11167 | test_cases.emplace_back(new test_rms_norm_mul_rope({128, 32, 2, 1}, 1e-6f, multi_add, set_rows, broadcast, rope)); |
| 11168 | test_cases.emplace_back(new test_rms_norm_mul_rope({128, 4, 2, 1}, 1e-6f, multi_add, set_rows, broadcast, rope)); |
| 11169 | test_cases.emplace_back(new test_rms_norm_mul_rope({128, 32, 50, 1}, 1e-6f, multi_add, set_rows, broadcast, rope)); |
| 11170 | test_cases.emplace_back(new test_rms_norm_mul_rope({128, 4, 50, 1}, 1e-6f, multi_add, set_rows, broadcast, rope)); |
| 11171 | test_cases.emplace_back(new test_rms_norm_mul_rope({8192, 2, 2, 1}, 1e-6f, multi_add, set_rows, broadcast, rope)); |
| 11172 | test_cases.emplace_back(new test_rms_norm_mul_rope({8192, 2, 2, 1}, 1e-6f, multi_add, set_rows, broadcast, rope)); |
| 11173 | } |
| 11174 | } |
| 11175 | } |
| 11176 | } |
| 11177 | // ne[2] > 65535 exceeds the CUDA gridDim.y limit (#27901) |
| 11178 | test_cases.emplace_back(new test_rms_norm_mul_rope({4, 1, 65536, 1}, 1e-6f, false, false, false, GGML_ROPE_TYPE_NORMAL)); |
| 11179 | test_cases.emplace_back(new test_rms_norm_mul_rope({4, 1, 65536, 1}, 1e-6f, false, true, false, GGML_ROPE_TYPE_NEOX)); |
| 11180 | for (int64_t d_conv : {3, 4, 7, 9}) { |
| 11181 | for (int64_t d_inner: {1024, 1536, 2048}) { |
| 11182 | test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {d_conv, d_inner, 1, 1}, {d_conv, d_inner, 1, 1})); |
| 11183 | test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {2 * d_conv, d_inner, 1, 1}, {d_conv, d_inner, 1, 1})); |
| 11184 | test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {d_conv, d_inner, 4, 1}, {d_conv, d_inner, 1, 1})); |
| 11185 | // long token (n_t > 32, exercises the long_token kernel path) |
| 11186 | test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {d_conv - 1 + 64, d_inner, 1, 1}, {d_conv, d_inner, 1, 1})); |
| 11187 | test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {d_conv - 1 + 64, d_inner, 4, 1}, {d_conv, d_inner, 1, 1})); |
| 11188 | } |
| 11189 | } |
| 11190 | |
| 11191 | // fused ssm_conv + (optional) bias_add + silu. The bias-only graph (no silu) is intentionally |
| 11192 | // not tested since there's no fusion for that pattern in ggml_cuda_can_fuse. |
| 11193 | for (int64_t d_conv : {3, 4, 7, 9}) { |
| 11194 | for (int64_t d_inner : {1024, 1536, 2048}) { |
| 11195 | for (bool fuse_bias : {false, true}) { |
| 11196 | // short token path (n_t <= 32) |
| 11197 | test_cases.emplace_back(new test_ssm_conv_bias_silu( |
| 11198 | GGML_TYPE_F32, {d_conv, d_inner, 1, 1}, {d_conv, d_inner, 1, 1}, fuse_bias)); |
| 11199 | test_cases.emplace_back(new test_ssm_conv_bias_silu( |
| 11200 | GGML_TYPE_F32, {2 * d_conv, d_inner, 1, 1}, {d_conv, d_inner, 1, 1}, fuse_bias)); |
| 11201 | test_cases.emplace_back(new test_ssm_conv_bias_silu( |
| 11202 | GGML_TYPE_F32, {d_conv, d_inner, 4, 1}, {d_conv, d_inner, 1, 1}, fuse_bias)); |
| 11203 | // long token path (n_t > 32) |
| 11204 | test_cases.emplace_back(new test_ssm_conv_bias_silu( |
| 11205 | GGML_TYPE_F32, {d_conv - 1 + 64, d_inner, 1, 1}, {d_conv, d_inner, 1, 1}, fuse_bias)); |
| 11206 | test_cases.emplace_back(new test_ssm_conv_bias_silu( |
| 11207 | GGML_TYPE_F32, {d_conv - 1 + 64, d_inner, 4, 1}, {d_conv, d_inner, 1, 1}, fuse_bias)); |
| 11208 | } |
| 11209 | } |
| 11210 | } |
| 11211 | |
| 11212 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 16, 1, 1024, 1, 32, 4)); // Mamba-1 |
| 11213 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 96, 64, 128, 8, 1, 1)); // Nemotron-3-Puzzle decode (scan path, unused warp in last block) |
| 11214 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 96, 64, 16, 8, 300, 2, false, /*K=*/1, /*weak_decay=*/true)); // d_state=96 SSD multi-chunk (partial 2nd chunk, 2 seqs) |
| 11215 | test_cases.emplace_back(new test_ssm_scan_rollback(GGML_TYPE_F32, 96, 64, 16, 8, 8, 2, /*K=*/3)); // d_state=96 rollback snapshots match prefix states |
| 11216 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 64, 16, 2, 32, 4)); // Mamba-2 |
| 11217 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 256, 64, 8, 2, 32, 4)); // Falcon-H1 |
| 11218 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 128, 4, 4, 16, 2, true)); // x/B/C overlap |
| 11219 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 80, 128, 1, 256, 1)); // Nemotron-9B SSD path |
| 11220 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 80, 128, 1, 512, 1)); // Nemotron-9B SSD multi-chunk (2 aligned chunks) |
| 11221 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 64, 80, 8, 300, 2)); // Mamba-2 SSD multi-chunk (partial 2nd chunk, 2 seqs) |
| 11222 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 64, 16, 2, 4, 2, false, /*K=*/4)); // Mamba-2 rollback snapshots |
| 11223 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 64, 16, 2, 8, 2, false, /*K=*/3)); // Mamba-2 rollback overflow |
| 11224 | test_cases.emplace_back(new test_ssm_scan_rollback(GGML_TYPE_F32, 128, 64, 16, 2, 8, 2, /*K=*/3)); // rollback snapshots match prefix states |
| 11225 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 64, 16, 2, 64, 4)); // Metal SSD one chunk MMA only, no seq tail |
| 11226 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 64, 16, 2, 65, 2)); // SSD one chunk + 1-token sequential tail |
| 11227 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 64, 16, 2, 128, 2)); // SSD multi-chunk, no tail (exercises the chunk-to-chunk state handoff) |
| 11228 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 64, 16, 2, 128, 2, false, /*K=*/1, /*weak_decay=*/true)); // SSD multi-chunk, carried state not numerically negligible |
| 11229 | |
| 11230 | // ssm_scan + cache cpy fusion |
| 11231 | test_cases.emplace_back(new test_ssm_scan_cache_fusion(GGML_TYPE_F32, 128, 64, 16, 2, 4, 1, 4)); |
| 11232 | test_cases.emplace_back(new test_ssm_scan_cache_fusion(GGML_TYPE_F32, 128, 64, 16, 2, 1, 1, 4)); // n_seq_tokens < K |
| 11233 | test_cases.emplace_back(new test_ssm_scan_cache_fusion(GGML_TYPE_F32, 128, 64, 16, 2, 8, 1, 3)); // n_seq_tokens > K |
| 11234 | test_cases.emplace_back(new test_ssm_scan_cache_fusion(GGML_TYPE_F32, 96, 64, 16, 2, 4, 1, 4)); |
| 11235 | test_cases.emplace_back(new test_ssm_scan_cache_fusion(GGML_TYPE_F32, 256, 64, 8, 2, 4, 1, 4)); |
| 11236 | test_cases.emplace_back(new test_ssm_scan_cache_fusion(GGML_TYPE_F32, 128, 64, 16, 2, 1, 1, 1)); // K == 1, final state only |
| 11237 | test_cases.emplace_back(new test_ssm_scan_cache_fusion(GGML_TYPE_F32, 128, 64, 16, 2, 4, 1, 1)); |
| 11238 | test_cases.emplace_back(new test_ssm_scan_cache_fusion(GGML_TYPE_F32, 128, 64, 16, 2, 300, 1, 1)); // K == 1, SSD path over two chunks |
| 11239 | |
| 11240 | test_cases.emplace_back(new test_rwkv_wkv6(GGML_TYPE_F32, 32, 64, 1, 1)); |
| 11241 | test_cases.emplace_back(new test_rwkv_wkv6(GGML_TYPE_F32, 32, 64, 32, 1)); |
| 11242 | test_cases.emplace_back(new test_rwkv_wkv6(GGML_TYPE_F32, 32, 64, 32, 4)); |
| 11243 | test_cases.emplace_back(new test_rwkv_wkv6(GGML_TYPE_F32, 32, 64, 128, 4)); |
| 11244 | |
| 11245 | test_cases.emplace_back(new test_rwkv_wkv7(GGML_TYPE_F32, 32, 64, 1, 1)); |
| 11246 | test_cases.emplace_back(new test_rwkv_wkv7(GGML_TYPE_F32, 32, 64, 1, 4)); |
| 11247 | test_cases.emplace_back(new test_rwkv_wkv7(GGML_TYPE_F32, 32, 64, 32, 1)); |
| 11248 | test_cases.emplace_back(new test_rwkv_wkv7(GGML_TYPE_F32, 32, 64, 32, 4)); |
| 11249 | test_cases.emplace_back(new test_rwkv_wkv7(GGML_TYPE_F32, 32, 64, 128, 4)); |
| 11250 | |
| 11251 | test_cases.emplace_back(new test_gla(GGML_TYPE_F32, 32, 64, 1, 1)); |
| 11252 | test_cases.emplace_back(new test_gla(GGML_TYPE_F32, 32, 64, 32, 1)); |
| 11253 | test_cases.emplace_back(new test_gla(GGML_TYPE_F32, 32, 64, 32, 4)); |
| 11254 | test_cases.emplace_back(new test_gla(GGML_TYPE_F32, 32, 64, 128, 4)); |
| 11255 | |
| 11256 | // FWHT tests |
| 11257 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 128, 1, 128)); |
| 11258 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 64, 1, 64)); |
| 11259 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 256, 1, 256)); |
| 11260 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 512, 1, 512)); |
| 11261 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 128, 32, 128)); |
| 11262 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 128, 4, 128, {2, 3})); |
| 11263 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 256, 512, 256)); // many rows |
| 11264 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 64, 1, 64)); |
| 11265 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 128, 1, 128)); |
| 11266 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 256, 1, 256)); |
| 11267 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 512, 1, 512)); |
| 11268 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 128, 32, 128)); |
| 11269 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 256, 512, 256)); // many rows |
| 11270 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 32, 1, 32)); // too small (N<64) |
| 11271 | test_cases.emplace_back( |
| 11272 | new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 16384, 1, 16384)); // too big (N>8192) |
| 11273 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 64, 1, 64)); |
| 11274 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 128, 1, 128)); |
| 11275 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 256, 1, 256)); |
| 11276 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 512, 1, 512)); |
| 11277 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 128, 32, 128)); |
| 11278 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 128, 4, 128, {2, 3})); |
| 11279 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 256, 512, 256)); // many rows |
| 11280 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 1024, 1, 1024)); |
| 11281 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 2048, 1, 2048)); |
| 11282 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 4096, 1, 4096)); |
| 11283 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 8192, 1, 8192)); |
| 11284 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 1024, 7, 1024)); // many rows |
| 11285 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 1024, 1, 1024)); |
| 11286 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 2048, 1, 2048)); |
| 11287 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 4096, 1, 4096)); |
| 11288 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 8192, 1, 8192)); |
| 11289 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 1024, 7, 1024)); // many rows |
| 11290 | |
| 11291 | // FP4 activation precision (default = native W4A4, src1 GGML_PREC_Q8 = W4A8) |
| 11292 | test_cases.emplace_back(new test_mul_mat_w4a8(GGML_TYPE_NVFP4, GGML_TYPE_F32, 32, 1, 256)); |
| 11293 | test_cases.emplace_back(new test_mul_mat_w4a8(GGML_TYPE_NVFP4, GGML_TYPE_F32, 32, 32, 256)); |
| 11294 | test_cases.emplace_back(new test_mul_mat_w4a8(GGML_TYPE_NVFP4, GGML_TYPE_F32, 64, 16, 512)); |
| 11295 | test_cases.emplace_back(new test_mul_mat_w4a4(GGML_TYPE_NVFP4, GGML_TYPE_F32, 32, 1, 256)); |
| 11296 | test_cases.emplace_back(new test_mul_mat_w4a4(GGML_TYPE_NVFP4, GGML_TYPE_F32, 32, 32, 256)); |
| 11297 | test_cases.emplace_back(new test_mul_mat_id_w4a8(GGML_TYPE_NVFP4, GGML_TYPE_F32, 8, 2, false, 32, 32, 256)); |
| 11298 | test_cases.emplace_back(new test_mul_mat_id_w4a8(GGML_TYPE_NVFP4, GGML_TYPE_F32, 4, 2, true, 64, 16, 256)); |
| 11299 | test_cases.emplace_back(new test_mul_mat_id_w4a4(GGML_TYPE_NVFP4, GGML_TYPE_F32, 8, 2, false, 32, 32, 256)); |
| 11300 | test_cases.emplace_back(new test_mul_mat_id_w4a4(GGML_TYPE_NVFP4, GGML_TYPE_F32, 4, 2, true, 64, 16, 256)); |
| 11301 | test_cases.emplace_back(new test_mul_mat_w4a8(GGML_TYPE_MXFP4, GGML_TYPE_F32, 32, 32, 256)); |
| 11302 | test_cases.emplace_back(new test_mul_mat_w4a8(GGML_TYPE_MXFP4, GGML_TYPE_F32, 64, 16, 512)); |
| 11303 | test_cases.emplace_back(new test_mul_mat_w4a4(GGML_TYPE_MXFP4, GGML_TYPE_F32, 32, 32, 256)); |
| 11304 | test_cases.emplace_back(new test_mul_mat_id_w4a8(GGML_TYPE_MXFP4, GGML_TYPE_F32, 8, 2, false, 32, 32, 256)); |
| 11305 | test_cases.emplace_back(new test_mul_mat_id_w4a4(GGML_TYPE_MXFP4, GGML_TYPE_F32, 8, 2, false, 32, 32, 256)); |
| 11306 | |
| 11307 | #if 0 |
| 11308 | // > 2^31-element dst (8.9 GB), about 45 GB peak memory. Too large to be enabled by default. |
| 11309 | // kernel_mul_mm dst offset of the last batch is past INT32_MAX: rows 0-31 use full tiles, rows 32-39 use edge tiles. |
| 11310 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, 1048576, 40, 64, {1, 1}, {53, 1})); |
| 11311 | |
| 11312 | // kernel_mul_mm_id: the index of tokens 32768-33791 does not fit in 16 bits (about 0.3 GB peak memory). |
| 11313 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F32, GGML_TYPE_F32, 4, 2, false, 64, 33792, 64)); |
| 11314 | // kernel_mul_mm_id: > 2^31-element dst (8.7 GB), about 45 GB peak memory. Tokens 32514+ have a dst offset past INT32_MAX, tokens 32768+ also need a 32-bit token index. |
| 11315 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F32, GGML_TYPE_F32, 16, 8, false, 8256, 32832, 64)); |
| 11316 | #endif |
| 11317 | |
| 11318 | #if 0 |
| 11319 | // > 4GB A matrix. Too slow to be enabled by default. |
| 11320 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 900000, 3, 2592, {1, 1}, {1, 1})); |
| 11321 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 1700000, 96, 2592, {1, 1}, {1, 1})); |
| 11322 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 1700000, 3, 2592, {1, 1}, {1, 1})); |
| 11323 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 1700000, 1, 2592, {1, 1}, {1, 1})); |
| 11324 | |
| 11325 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_Q8_0, GGML_TYPE_F32, 128, 128, false, 8192, 2, 5120)); // Llama-4-Maverick-17B-128E-PAB-Q8_0 |
| 11326 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_Q8_0, GGML_TYPE_F32, 128, 128, false, 8192, 1, 5120)); // Llama-4-Maverick-17B-128E-PAB-Q8_0 |
| 11327 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q8_0, GGML_TYPE_F32, 8192, 1, 5120, {128, 1}, {1, 1})); |
| 11328 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q8_0, GGML_TYPE_F32, 8192, 512, 5120, {128, 1}, {1, 1})); |
| 11329 | #endif |
| 11330 | |
| 11331 | for (ggml_type type_a : all_types) { |
| 11332 | for (int i = 1; i < 10; ++i) { |
| 11333 | test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 16, i, 1*256, { 1, 1}, {1, 1})); |
| 11334 | //test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 12, i, 2*256, { 2, 1}, {1, 1})); |
| 11335 | //test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 11, i, 3*256, { 1, 3}, {5, 1})); |
| 11336 | //test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 13, i, 4*256, { 2, 3}, {1, 1})); |
| 11337 | //test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 17, i, 31*256, { 4, 1}, {1, 1})); |
| 11338 | //test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 18, i, 32*256, { 1, 1}, {8, 1})); |
| 11339 | //test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 19, i, 33*256, { 1, 1}, {1, 1})); |
| 11340 | } |
| 11341 | // mat-vec shaders split k across lanes and loop over the blocks in strides. k must be |
| 11342 | // long enough that the loop wraps, else the stride is never exercised |
| 11343 | test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 16, 1, 16*256, { 1, 1}, {1, 1})); |
| 11344 | test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 16, 8, 16*256, { 1, 1}, {1, 1})); |
| 11345 | } |
| 11346 | |
| 11347 | // Multi-column MMVQ coverage for the Q4_K weight-reuse path and a Q5_K control. |
| 11348 | for (ggml_type type_a : { GGML_TYPE_Q4_K, GGML_TYPE_Q5_K }) { |
| 11349 | for (int n = 1; n <= 8; ++n) { |
| 11350 | test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 4096, n, 1024, { 1, 1 }, { 1, 1 })); |
| 11351 | test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 1023, n, 4096, { 1, 1 }, { 1, 1 })); |
| 11352 | } |
| 11353 | } |
| 11354 | |
| 11355 | // The SYCL backend picks between one and two output rows per subgroup by row count when there |
| 11356 | // are two destination columns (Q4_K_MMVQ_ROW_PAIR_MIN_NROWS in ggml-sycl/mmvq.cpp). Cover both |
| 11357 | // sides of that boundary, including an odd row count above it for the row-pair tail. |
| 11358 | for (int64_t m : {6271, 6272, 6273}) { |
| 11359 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_K, GGML_TYPE_F32, m, 2, 1024, { 1, 1 }, { 1, 1 })); |
| 11360 | } |
| 11361 | |
| 11362 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_0, GGML_TYPE_F32, 2880, 32, 2880, {1, 1}, {1, 1})); |
| 11363 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q8_0, GGML_TYPE_F32, 2880, 32, 2880, {1, 1}, {1, 1})); |
| 11364 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_MXFP4, GGML_TYPE_F32, 2880, 32, 2880, {1, 1}, {1, 1})); |
| 11365 | |
| 11366 | // m == 1, with n on both sides of MMVF_MAX_BATCH_SIZE (8): mmvf below, operand swap above |
| 11367 | for (int64_t n : {1, 7, 8, 9, 16, 127, 128, 511, 512}) { |
| 11368 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, 1, n, 2048, {1, 1}, {1, 1})); |
| 11369 | } |
| 11370 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 1, 512, 2048, {1, 1}, {1, 1})); |
| 11371 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 1, 512, 2048, {1, 1}, {1, 1})); |
| 11372 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 1, 509, 2051, {1, 1}, {1, 1})); |
| 11373 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 1, 509, 2051, {1, 1}, {1, 1})); |
| 11374 | |
| 11375 | // testing very large N |
| 11376 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 16, 2105344, 32, {1, 1}, {1, 1})); |
| 11377 | |
| 11378 | #if 0 |
| 11379 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 16, 8400000, 32, {1, 1}, {1, 1})); |
| 11380 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_0, GGML_TYPE_F32, 16, 8400000, 32, {1, 1}, {1, 1})); |
| 11381 | // same shapes with a repeat in dim 3 |
| 11382 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 16, 8400000, 32, {1, 1}, {2, 1})); |
| 11383 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_0, GGML_TYPE_F32, 16, 8400000, 32, {1, 1}, {2, 1})); |
| 11384 | #endif |
| 11385 | |
| 11386 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, 31, 509, 2051, {1, 1}, {1, 1})); |
| 11387 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, 32, 509, 2112, {1, 1}, {1, 1})); |
| 11388 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q8_0, GGML_TYPE_F32, 32, 509, 2112, {1, 1}, {1, 1})); |
| 11389 | |
| 11390 | // few src1 rows (speculative verify): odd m, a single K block, long K, every tile width, broadcast batches |
| 11391 | for (int64_t n : {2, 3, 5, 8, 9, 13, 16}) { |
| 11392 | for (auto [m, k] : std::vector<std::pair<int64_t, int64_t>>{{40, 32}, {100, 96}, {1000, 5120}, {3000, 1024}, {6144, 5120}, {17408, 512}}) { |
| 11393 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_0, GGML_TYPE_F32, m, n, k, {1, 1}, {1, 1})); |
| 11394 | } |
| 11395 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_0, GGML_TYPE_F32, 64, n, 256, {3, 2}, {2, 1})); |
| 11396 | for (ggml_type type_a : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_Q4_1, GGML_TYPE_Q5_0, GGML_TYPE_Q5_1, |
| 11397 | GGML_TYPE_Q8_0, GGML_TYPE_Q1_0, GGML_TYPE_Q2_0, GGML_TYPE_MXFP4, GGML_TYPE_Q2_K, GGML_TYPE_Q3_K, |
| 11398 | GGML_TYPE_Q4_K, GGML_TYPE_Q5_K, GGML_TYPE_Q6_K, GGML_TYPE_TQ2_0, |
| 11399 | GGML_TYPE_IQ2_XXS, GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S, GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ3_S, |
| 11400 | GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M, GGML_TYPE_IQ4_NL, GGML_TYPE_IQ4_XS}) { |
| 11401 | test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 48, n, 2560, {1, 1}, {1, 1})); |
| 11402 | test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 1000, n, 1024, {1, 1}, {1, 1})); |
| 11403 | test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 3000, n, 512, {1, 1}, {1, 1})); |
| 11404 | test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 4100, n, 256, {1, 1}, {1, 1})); |
| 11405 | test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 64, n, 256, {3, 2}, {2, 1})); |
| 11406 | // a K that is not a multiple of the 64-weight chunk takes the other kernels |
| 11407 | if (96 % ggml_blck_size(type_a) == 0) { |
| 11408 | test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 100, n, 96, {1, 1}, {1, 1})); |
| 11409 | } |
| 11410 | } |
| 11411 | for (ggml_type type_a : {GGML_TYPE_Q4_0, GGML_TYPE_Q8_0, GGML_TYPE_Q5_K, GGML_TYPE_F16}) { |
| 11412 | for (mul_mat_add_mode mode : {MUL_MAT_ADD_MM_RES, MUL_MAT_ADD_RES_MM, MUL_MAT_ADD_ROW, MUL_MAT_ADD_RES_INPLACE, MUL_MAT_ADD_MM_MM}) { |
| 11413 | test_cases.emplace_back(new test_mul_mat_add(type_a, 1000, n, 1024, mode)); |
| 11414 | } |
| 11415 | test_cases.emplace_back(new test_mul_mat_add(type_a, 2048, n, 2048, MUL_MAT_ADD_B_INPLACE)); |
| 11416 | } |
| 11417 | // the widest tiles with two src1 tiles, and src1 rows padded in memory |
| 11418 | for (ggml_type type_a : {GGML_TYPE_F16, GGML_TYPE_Q4_0, GGML_TYPE_Q8_0, GGML_TYPE_Q4_K, GGML_TYPE_Q5_K, GGML_TYPE_Q6_K}) { |
| 11419 | test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 8192, n, 512, {1, 1}, {1, 1})); |
| 11420 | test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 1000, n, 1024, {1, 1}, {1, 1}, {0, 1, 2, 3}, 1280)); |
| 11421 | } |
| 11422 | // a src1 with a nonzero mean, for the zero points and mins of quantized src0 types |
| 11423 | for (ggml_type type_a : {GGML_TYPE_Q4_0, GGML_TYPE_Q4_1, GGML_TYPE_Q5_0, GGML_TYPE_Q8_0, GGML_TYPE_Q2_K, GGML_TYPE_Q4_K, GGML_TYPE_Q5_K}) { |
| 11424 | test_cases.emplace_back(new test_mul_mat_pos(type_a, GGML_TYPE_F32, 1000, n, 1024, {1, 1}, {1, 1})); |
| 11425 | } |
| 11426 | } |
| 11427 | |
| 11428 | #if 0 |
| 11429 | { |
| 11430 | // Test paths in OpenCL |
| 11431 | std::vector<int> ns = {32, 64, 128, 256, 512, 1024, 4096}; |
| 11432 | std::vector<int> ks = {896, 1536, 4096}; |
| 11433 | for (auto n : ns) { |
| 11434 | for (auto k : ks) { |
| 11435 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q8_0, GGML_TYPE_F32, 1024, n, k, {1, 1}, {1, 1})); |
| 11436 | } |
| 11437 | } |
| 11438 | } |
| 11439 | #endif |
| 11440 | |
| 11441 | #if 1 |
| 11442 | for (ggml_type type_a : base_types) { |
| 11443 | for (ggml_type type_b : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 11444 | std::vector<int> ks = { 256 }; |
| 11445 | if (ggml_blck_size(type_a) == 1) { |
| 11446 | ks.push_back(4); |
| 11447 | } |
| 11448 | for (auto k : ks) { |
| 11449 | // test cases without permutation |
| 11450 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, k, {1, 1}, {1, 1})); |
| 11451 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, k, {1, 1}, {2, 1})); |
| 11452 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, k, {1, 1}, {1, 2})); |
| 11453 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, k, {3, 1}, {1, 1})); |
| 11454 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, k, {3, 1}, {2, 1})); |
| 11455 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, k, {3, 2}, {1, 1})); |
| 11456 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, k, {3, 2}, {2, 1})); |
| 11457 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, k, {3, 2}, {1, 2})); |
| 11458 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, k, {3, 2}, {2, 2})); |
| 11459 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 4, k, {3, 2}, {2, 2})); |
| 11460 | |
| 11461 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, k, {1, 1}, {1, 1})); |
| 11462 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, k, {1, 1}, {2, 1})); |
| 11463 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, k, {1, 1}, {1, 2})); |
| 11464 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, k, {3, 1}, {1, 1})); |
| 11465 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, k, {3, 1}, {2, 1})); |
| 11466 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, k, {3, 2}, {1, 1})); |
| 11467 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, k, {3, 2}, {2, 1})); |
| 11468 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, k, {3, 2}, {1, 2})); |
| 11469 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, k, {3, 2}, {2, 2})); |
| 11470 | |
| 11471 | // test cases with permutation |
| 11472 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, k, {2, 3}, {1, 1}, {0, 2, 1, 3})); |
| 11473 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, k, {2, 3}, {1, 1}, {0, 1, 3, 2})); |
| 11474 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, k, {2, 3}, {1, 1}, {0, 3, 2, 1})); |
| 11475 | |
| 11476 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 4, k, {2, 3}, {1, 1}, {0, 3, 2, 1})); |
| 11477 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 8, k, {2, 3}, {1, 1}, {0, 2, 1, 3})); |
| 11478 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 8, k, {2, 3}, {1, 1}, {0, 1, 3, 2})); |
| 11479 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 8, k, {2, 3}, {1, 1}, {0, 3, 2, 1})); |
| 11480 | |
| 11481 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, k, {2, 3}, {1, 1}, {0, 2, 1, 3})); |
| 11482 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, k, {2, 3}, {1, 1}, {0, 1, 3, 2})); |
| 11483 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, k, {2, 3}, {1, 1}, {0, 3, 2, 1})); |
| 11484 | |
| 11485 | // test cases with permutation and a broadcast of a in dim 3 (e.g. a weight against a per-stream attention output) |
| 11486 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, k, {1, 1}, {1, 2}, {0, 2, 1, 3})); |
| 11487 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 8, k, {1, 1}, {1, 2}, {0, 2, 1, 3})); |
| 11488 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 8, k, {1, 1}, {4, 2}, {0, 2, 1, 3})); |
| 11489 | } |
| 11490 | |
| 11491 | // test cases with large ne00/ne10 to cover stream-k fixup |
| 11492 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 1024, {3, 2}, {1, 1})); |
| 11493 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 8, 1024, {3, 2}, {1, 1})); |
| 11494 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 1024, {3, 2}, {1, 1})); |
| 11495 | |
| 11496 | // test cases with large batch size |
| 11497 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 8, 256, {1536, 1}, {1, 1})); |
| 11498 | } |
| 11499 | } |
| 11500 | |
| 11501 | // BF16 is absent from base_types: add the 3 standard non-contig permutations explicitly |
| 11502 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_BF16, GGML_TYPE_F32, 16, 1, 256, {2, 3}, {1, 1}, {0, 2, 1, 3})); |
| 11503 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_BF16, GGML_TYPE_F32, 16, 1, 256, {2, 3}, {1, 1}, {0, 1, 3, 2})); |
| 11504 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_BF16, GGML_TYPE_F32, 16, 1, 256, {2, 3}, {1, 1}, {0, 3, 2, 1})); |
| 11505 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_BF16, GGML_TYPE_F32, 16, 8, 256, {2, 3}, {1, 1}, {0, 2, 1, 3})); |
| 11506 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_BF16, GGML_TYPE_F32, 16, 8, 256, {2, 3}, {1, 1}, {0, 1, 3, 2})); |
| 11507 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_BF16, GGML_TYPE_F32, 16, 8, 256, {2, 3}, {1, 1}, {0, 3, 2, 1})); |
| 11508 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_BF16, GGML_TYPE_F32, 16, 16, 256, {2, 3}, {1, 1}, {0, 2, 1, 3})); |
| 11509 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_BF16, GGML_TYPE_F32, 16, 16, 256, {2, 3}, {1, 1}, {0, 1, 3, 2})); |
| 11510 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_BF16, GGML_TYPE_F32, 16, 16, 256, {2, 3}, {1, 1}, {0, 3, 2, 1})); |
| 11511 | |
| 11512 | // BF16 in src1, as ggml_conv_1d_dw emits it for a BF16 kernel |
| 11513 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_BF16, 16, 1, 256, {1, 1}, {1, 1})); |
| 11514 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_BF16, 16, 1, 256, {3, 2}, {2, 2})); |
| 11515 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_BF16, 16, 8, 256, {1, 1}, {1, 1})); |
| 11516 | |
| 11517 | // token-tile boundary coverage. With n_used == n_mats every token routes to every expert, so |
| 11518 | // each expert receives exactly n rows, with no dependence on the random draw. mul_mm_id is used |
| 11519 | // from 32 tokens up: n = 32, 33, 47, 48, 49 reach it, leaving a last tile of 32, 1, 15, 16 and |
| 11520 | // 17 rows - 16 and 17 straddle the point where the upper half stops being skipped. The smaller |
| 11521 | // n cover the same row counts on the mat-vec path. |
| 11522 | for (ggml_type type_a : {GGML_TYPE_Q4_K, GGML_TYPE_IQ2_XS, GGML_TYPE_F16}) { |
| 11523 | for (int n : {1, 15, 16, 17, 31, 32, 33, 47, 48, 49}) { |
| 11524 | test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 4, 4, false, 512, n, 256)); |
| 11525 | } |
| 11526 | // experts that receive no rows at all |
| 11527 | test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 8, 1, false, 512, 1, 256)); |
| 11528 | } |
| 11529 | |
| 11530 | for (ggml_type type_a : other_types) { |
| 11531 | for (ggml_type type_b : {GGML_TYPE_F32}) { |
| 11532 | if (ggml_blck_size(type_a) != 256) { |
| 11533 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, ggml_blck_size(type_a), {1, 1}, {1, 1})); |
| 11534 | } |
| 11535 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {1, 1}, {1, 1})); |
| 11536 | } |
| 11537 | } |
| 11538 | |
| 11539 | // Test IQP panel path for all grid IQ types |
| 11540 | for (ggml_type type_a : {GGML_TYPE_IQ2_XXS, GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S, GGML_TYPE_IQ3_XXS, |
| 11541 | GGML_TYPE_IQ3_S, GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M, GGML_TYPE_IQ4_XS}) { |
| 11542 | test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 16, 10, 256, {1, 1}, {1, 1})); |
| 11543 | } |
| 11544 | #else |
| 11545 | // m = a rows |
| 11546 | // n = b rows |
| 11547 | // k = cols |
| 11548 | std::uniform_int_distribution<> dist_m(1, 128); |
| 11549 | std::uniform_int_distribution<> dist_n(16, 128); |
| 11550 | std::uniform_int_distribution<> dist_k(1, 16); |
| 11551 | for (int i = 0; i < 1000; i++) { |
| 11552 | for (ggml_type type_a : all_types) { |
| 11553 | for (ggml_type type_b : {GGML_TYPE_F32}) { |
| 11554 | int m = dist_m(rng); |
| 11555 | int n = dist_n(rng); |
| 11556 | int k = dist_k(rng) * ggml_blck_size(type_a); |
| 11557 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, m, n, k, { 1, 1}, {1, 1})); |
| 11558 | } |
| 11559 | } |
| 11560 | } |
| 11561 | #endif |
| 11562 | |
| 11563 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 2, 128, { 8, 1}, {1, 1})); |
| 11564 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 83, 2, 128, { 8, 1}, {4, 1})); |
| 11565 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 2, 64, { 8, 1}, {4, 1})); |
| 11566 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 83, 2, 64, { 8, 1}, {4, 1})); |
| 11567 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 45, 128, { 8, 1}, {4, 1})); |
| 11568 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 128, 45, 64, { 8, 1}, {4, 1})); |
| 11569 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 1056, 1, 193, {1, 1}, {4, 1}, {0, 2, 1, 3})); |
| 11570 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 1056, 1, 67, {1, 1}, {4, 1}, {0, 2, 1, 3})); |
| 11571 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, 16, 32, 32, { 1, 1}, {1, 1}, {0, 1, 2, 3}, 64, 3)); |
| 11572 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, 64, 77, 77, {12,1}, {1,1})); |
| 11573 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, 32, 4, 96, {3, 2}, {1, 1}, {0, 1, 2, 3}, 0, 1, true)); |
| 11574 | // the first rows of a KV cache: a is a view whose batches are strided by the cache length |
| 11575 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, 8, 1, 64, {8, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 5120)); |
| 11576 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 8, 16, 64, {8, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 5120)); |
| 11577 | // the batches of a are padded, nb[2] is not a multiple of nb[1] |
| 11578 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, 8, 1, 64, {8, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 16, 16)); |
| 11579 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 8, 16, 64, {8, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 16, 16)); |
| 11580 | // as is a view whose experts are strided by more rows than it uses |
| 11581 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F32, GGML_TYPE_F32, 4, 2, false, 8, 1, 64, 64)); |
| 11582 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F16, GGML_TYPE_F32, 4, 2, false, 8, 16, 64, 64)); |
| 11583 | |
| 11584 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_0, GGML_TYPE_F32, 576, 512, 576, {1,1}, {1,1})); |
| 11585 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_0, GGML_TYPE_F32, 1, 2048, 8192, {1, 1}, {1, 1})); |
| 11586 | for (ggml_type type_a : all_types) { |
| 11587 | test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 1, 64, 256, {1, 1}, {1, 1})); |
| 11588 | } |
| 11589 | |
| 11590 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q8_0, GGML_TYPE_F32, 6, 4096, 5120, {1, 1}, {1, 1})); |
| 11591 | |
| 11592 | // SIMD K boundaries, including tails shorter than one vector. |
| 11593 | for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16}) { |
| 11594 | for (int k : {1, 7, 8, 9, 15, 16, 17, 31, 32, 33, 48}) { |
| 11595 | test_cases.emplace_back(new test_mul_mat(type, type, 64, 17, k, {1, 1}, {1, 1})); |
| 11596 | if (type != GGML_TYPE_F32) { |
| 11597 | test_cases.emplace_back(new test_mul_mat(type, GGML_TYPE_F32, 64, 17, k, {1, 1}, {1, 1})); |
| 11598 | } |
| 11599 | } |
| 11600 | } |
| 11601 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 64, 32, 65, {1, 1}, {1, 1})); |
| 11602 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 64, 32, 80, {1, 1}, {1, 1})); |
| 11603 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 32, 80, {1, 1}, {1, 1})); |
| 11604 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, 64, 32, 80, {1, 1}, {1, 1})); |
| 11605 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 64, 32, 588, {1, 1}, {1, 1})); // 14*14*3, e.g. conv_2d im2col |
| 11606 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 64, 32, 80, {4, 1}, {1, 1})); |
| 11607 | |
| 11608 | #if 0 |
| 11609 | // test the mat-mat path for Metal |
| 11610 | for (int k = 1; k < 512; ++k) { |
| 11611 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 127, k, {12,1}, {1,1})); |
| 11612 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, 64, 127, k, {12,1}, {1,1})); |
| 11613 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 77, k, {12,1}, {1,1})); |
| 11614 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, 64, 77, k, {12,1}, {1,1})); |
| 11615 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 128, k, {12,1}, {1,1})); |
| 11616 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, 64, 128, k, {12,1}, {1,1})); |
| 11617 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F16, GGML_TYPE_F32, 16, 16, false, 50, 200, k)); |
| 11618 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F16, GGML_TYPE_F32, 16, 16, true, 50, 200, k)); |
| 11619 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_BF16, GGML_TYPE_F32, 16, 16, false, 50, 200, k)); |
| 11620 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_BF16, GGML_TYPE_F32, 16, 16, true, 50, 200, k)); |
| 11621 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F32, GGML_TYPE_F32, 16, 16, false, 50, 200, k)); |
| 11622 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F32, GGML_TYPE_F32, 16, 16, true, 50, 200, k)); |
| 11623 | } |
| 11624 | #endif |
| 11625 | |
| 11626 | for (auto bs2 : {1,3}) { |
| 11627 | for (auto bs : {1,2,4,8}) { |
| 11628 | for (auto nr : {1,4}) { |
| 11629 | for (uint32_t m = 0; m < 2; ++m) { |
| 11630 | for (uint32_t k = 0; k < 2; ++k) { |
| 11631 | for (ggml_type type: {GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_F32}) { |
| 11632 | test_cases.emplace_back(new test_mul_mat(type, GGML_TYPE_F32, 1056 + m, 1, 128 + k, {bs, bs2}, {nr, 1}, {0, 2, 1, 3})); |
| 11633 | test_cases.emplace_back(new test_mul_mat(type, GGML_TYPE_F32, 128 + m, 1, 1056 + k, {bs, bs2}, {nr, 1}, {0, 1, 2, 3}, 2*1056 + k)); |
| 11634 | } |
| 11635 | } |
| 11636 | } |
| 11637 | } |
| 11638 | } |
| 11639 | } |
| 11640 | |
| 11641 | // sycl backend will limit task global_range < MAX_INT |
| 11642 | // test case for f16-type-convert-to-fp32 kernel with large k under fp32 compute dtype (occurs in stable-diffusion) |
| 11643 | // however this case needs to alloc more memory which may fail in some devices (Intel Arc770, etc.) |
| 11644 | // this case is verified (pass) in Intel(R) Data Center GPU Max 1100 (sycl backend) and NV A30 (cuda backend) |
| 11645 | // test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 512, 262144, 9216, {1, 1}, {1, 1})); |
| 11646 | |
| 11647 | // test large experts*tokens |
| 11648 | for (bool b : {false, true}) { |
| 11649 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F16, GGML_TYPE_F32, 16, 16, b, 32, 1024, 16)); |
| 11650 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F16, GGML_TYPE_F32, 2, 2, b, 32, 8192, 64)); |
| 11651 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F16, GGML_TYPE_F32, 16, 16, b, 50, 200, 64)); |
| 11652 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_BF16, GGML_TYPE_F32, 16, 16, b, 32, 1024, 16)); |
| 11653 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_BF16, GGML_TYPE_F32, 16, 16, b, 50, 200, 64)); |
| 11654 | } |
| 11655 | |
| 11656 | // For issue 27873 |
| 11657 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_IQ2_XXS, GGML_TYPE_F32, 1, 1, false, 1, 8192, 4096)); |
| 11658 | |
| 11659 | for (int k : {1, 63, 65}) { |
| 11660 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F16, GGML_TYPE_F32, 1, 1, false, 8, 16, k)); |
| 11661 | } |
| 11662 | test_cases.emplace_back(new test_mul_mat_id_fusion(GGML_TYPE_F16, GGML_TYPE_F32, 16, 16, false, 32, 32, 32, 3)); |
| 11663 | for (ggml_type type : { GGML_TYPE_Q4_K, GGML_TYPE_Q8_0 }) { |
| 11664 | for (int64_t n : { 1, 2, 3, 4, 8, 9 }) { |
| 11665 | test_cases.emplace_back(new test_mul_mat_id_shared(type, n)); |
| 11666 | } |
| 11667 | } |
| 11668 | |
| 11669 | // gpt-oss issue with Vulkan mmq_id |
| 11670 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_MXFP4, GGML_TYPE_F32, 32, 2, false, 2880, 32, 2880)); |
| 11671 | // more than 256 experts (hoisted row-id path): 512 as in Qwen3.8-Flash-Next, |
| 11672 | // and 1024 at the LLAMA_MAX_EXPERTS limit |
| 11673 | for (int n : {1, 5, 64, 300}) { |
| 11674 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_IQ3_S, GGML_TYPE_F32, 512, 10, false, 128, n, 512)); |
| 11675 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_Q4_0, GGML_TYPE_F32, 512, 10, false, 256, n, 128)); |
| 11676 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_IQ3_S, GGML_TYPE_F32, 1024, 10, false, 128, n, 512)); |
| 11677 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_Q4_0, GGML_TYPE_F32, 1024, 10, false, 256, n, 128)); |
| 11678 | } |
| 11679 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_Q4_0, GGML_TYPE_F32, 32, 2, false, 2880, 32, 2880)); |
| 11680 | |
| 11681 | // multiple blocks per row: exercises the block-stride loop and the |
| 11682 | // per-expert base offset, which k == 256 alone leaves untested |
| 11683 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_TQ1_0, GGML_TYPE_F32, 28, 10, false, 1024, 1, 4096)); |
| 11684 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_TQ1_0, GGML_TYPE_F32, 128, 8, false, 1024, 1, 2048)); |
| 11685 | |
| 11686 | // TODO: disabled until CUDA backend support is added [TAG_MUL_MAT_ID_DUPLICATE_ID] |
| 11687 | #if 0 |
| 11688 | // repeated expert ids within a row |
| 11689 | for (ggml_type ta : {GGML_TYPE_F16, GGML_TYPE_Q8_0, GGML_TYPE_Q4_0}) { |
| 11690 | for (int n : {9, 16, 33, 64}) { |
| 11691 | test_cases.emplace_back(new test_mul_mat_id_dup(ta, GGML_TYPE_F32, 28, 10, false, 1024, n, 256)); |
| 11692 | } |
| 11693 | } |
| 11694 | |
| 11695 | for (ggml_type ta : {GGML_TYPE_F16, GGML_TYPE_Q4_0}) { |
| 11696 | test_cases.emplace_back(new test_mul_mat_id_dup(ta, GGML_TYPE_F32, 1025, 10, false, 64, 33, 256, 0, true)); |
| 11697 | test_cases.emplace_back(new test_mul_mat_id_dup(ta, GGML_TYPE_F32, 2050, 10, true, 64, 33, 256)); |
| 11698 | } |
| 11699 | #endif |
| 11700 | |
| 11701 | for (ggml_type type_a : all_types) { |
| 11702 | test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 4, 2, false, 64, 16, 3*ggml_blck_size(type_a))); |
| 11703 | } |
| 11704 | |
| 11705 | // Test IQP panel path for all grid IQ types |
| 11706 | for (ggml_type type_a : {GGML_TYPE_IQ2_XXS, GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S, GGML_TYPE_IQ3_XXS, |
| 11707 | GGML_TYPE_IQ3_S, GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M, GGML_TYPE_IQ4_XS}) { |
| 11708 | test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 4, 4, false, 16, 10, 256)); |
| 11709 | } |
| 11710 | |
| 11711 | for (ggml_type type_a : base_types) { |
| 11712 | for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) { |
| 11713 | for (int n_mats : {4, 8}) { |
| 11714 | for (int n_used : {1, 2, 4}) { |
| 11715 | for (bool b : {false, true}) { |
| 11716 | for (int n : {1, 4, 5, 17, 32, 129}) { |
| 11717 | int m = 512; |
| 11718 | int k = 256; |
| 11719 | test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, n_used, b, m, n, k)); |
| 11720 | } |
| 11721 | } |
| 11722 | } |
| 11723 | } |
| 11724 | } |
| 11725 | } |
| 11726 | |
| 11727 | for (ggml_type type_a : other_types) { |
| 11728 | for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) { |
| 11729 | for (int n_mats : {4}) { |
| 11730 | for (int n_used : {2}) { |
| 11731 | for (bool b : {false}) { |
| 11732 | for (int n : {1, 32}) { |
| 11733 | int m = 512; |
| 11734 | int k = 256; |
| 11735 | test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, n_used, b, m, n, k)); |
| 11736 | } |
| 11737 | } |
| 11738 | } |
| 11739 | } |
| 11740 | } |
| 11741 | } |
| 11742 | |
| 11743 | // skipped slots (negative ids) |
| 11744 | for (ggml_type type_a : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_Q4_0, GGML_TYPE_Q4_K, GGML_TYPE_Q8_0, GGML_TYPE_MXFP4}) { |
| 11745 | for (int n_used : {1, 2, 4}) { |
| 11746 | for (bool b : {false, true}) { |
| 11747 | for (int n : {1, 4, 33, 129}) { |
| 11748 | test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 8, n_used, b, 512, n, 256, 0, false, true)); |
| 11749 | } |
| 11750 | } |
| 11751 | } |
| 11752 | } |
| 11753 | // n_used == n_mats, so every expert can be skipped |
| 11754 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F16, GGML_TYPE_F32, 4, 4, false, 512, 64, 256, 0, false, true)); |
| 11755 | test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F16, GGML_TYPE_F32, 1, 1, false, 8, 16, 1, 0, false, true)); |
| 11756 | test_cases.emplace_back(new test_mul_mat_id_fusion(GGML_TYPE_F16, GGML_TYPE_F32, 16, 16, false, 32, 32, 32, 3, false, true)); |
| 11757 | |
| 11758 | for (int bs : {1, 4, 512}) { |
| 11759 | for (ggml_type type_a : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_Q4_0, GGML_TYPE_Q4_K}) { |
| 11760 | for (ggml_type type_b : {GGML_TYPE_F32}) { |
| 11761 | // test with mul after (ffn_moe_weighted) |
| 11762 | test_cases.emplace_back(new test_mul_mat_id_fusion(type_a, type_b, 128, 8, false, 768, bs, 2048, 1, true)); |
| 11763 | } |
| 11764 | } |
| 11765 | } |
| 11766 | |
| 11767 | for (ggml_type type_a : base_types) { |
| 11768 | for (ggml_type type_b : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 11769 | for (int n : {1, 16}) { |
| 11770 | for (int k : {1, 16}) { |
| 11771 | for (int bs2 : {1, 3}) { |
| 11772 | for (int bs3 : {1, 3}) { |
| 11773 | for (int nr2 : {1, 2}) { |
| 11774 | for (int nr3 : {1, 2}) { |
| 11775 | test_cases.emplace_back(new test_out_prod(type_a, type_b, 256, n, k, {bs2, bs3}, {nr2, nr3})); |
| 11776 | } |
| 11777 | } |
| 11778 | } |
| 11779 | } |
| 11780 | } |
| 11781 | } |
| 11782 | } |
| 11783 | } |
| 11784 | |
| 11785 | // ne2 sweep to cover the cublasSgemmStridedBatched path (dps2 == 1, ne2 > 1) |
| 11786 | for (int64_t ne2 : {1, 8, 16, 32}) { |
| 11787 | test_cases.emplace_back(new test_out_prod(GGML_TYPE_F32, GGML_TYPE_F32, |
| 11788 | 256, 16, 16, {ne2, 1}, {1, 1})); |
| 11789 | } |
| 11790 | |
| 11791 | // nr2 sweep to cover the cublasSgemmBatched pointer-array path (dps2 > 1) |
| 11792 | for (int64_t nr2 : {8, 16, 32}) { |
| 11793 | test_cases.emplace_back(new test_out_prod(GGML_TYPE_F32, GGML_TYPE_F32, |
| 11794 | 256, 16, 16, {1, 1}, {nr2, 1})); |
| 11795 | } |
| 11796 | |
| 11797 | // add_id |
| 11798 | for (ggml_type type_a : {GGML_TYPE_F32}) { |
| 11799 | for (ggml_type type_b : {GGML_TYPE_F32}) { |
| 11800 | for (int n_mats : {4, 8}) { |
| 11801 | for (int n_used : {1, 2, 4}) { |
| 11802 | for (int n_embd : {32, 129}) { |
| 11803 | for (int n_token : {1, 32, 129}) { |
| 11804 | test_cases.emplace_back(new test_add_id(type_a, type_b, n_embd, n_mats, n_used, n_token)); |
| 11805 | test_cases.emplace_back(new test_add_id(type_a, type_b, n_embd, n_mats, n_used, n_token, true)); |
| 11806 | } |
| 11807 | } |
| 11808 | } |
| 11809 | } |
| 11810 | } |
| 11811 | } |
| 11812 | |
| 11813 | for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_F32}) { |
| 11814 | test_cases.emplace_back(new test_sqr (type)); |
| 11815 | test_cases.emplace_back(new test_sqrt (type)); |
| 11816 | test_cases.emplace_back(new test_log (type)); |
| 11817 | test_cases.emplace_back(new test_sin (type)); |
| 11818 | test_cases.emplace_back(new test_cos (type)); |
| 11819 | test_cases.emplace_back(new test_clamp (type)); |
| 11820 | test_cases.emplace_back(new test_clamp (type, {10, 5, 4, 3}, -0.5f, 0.5f, 1)); |
| 11821 | test_cases.emplace_back(new test_leaky_relu(type)); |
| 11822 | test_cases.emplace_back(new test_floor (type)); |
| 11823 | test_cases.emplace_back(new test_ceil (type)); |
| 11824 | test_cases.emplace_back(new test_round (type)); |
| 11825 | test_cases.emplace_back(new test_trunc (type)); |
| 11826 | test_cases.emplace_back(new test_sqr (type, {7, 1, 5, 3})); |
| 11827 | test_cases.emplace_back(new test_sqr (type, {1024, 1024, 1, 1})); |
| 11828 | test_cases.emplace_back(new test_sqrt (type, {7, 1, 5, 3})); |
| 11829 | test_cases.emplace_back(new test_sqrt (type, {1024, 1024, 1, 1})); |
| 11830 | test_cases.emplace_back(new test_log (type, {7, 1, 5, 3})); |
| 11831 | test_cases.emplace_back(new test_log (type, {1024, 1024, 1, 1})); |
| 11832 | test_cases.emplace_back(new test_sin (type, {7, 1, 5, 3})); |
| 11833 | test_cases.emplace_back(new test_sin (type, {1024, 1024, 1, 1})); |
| 11834 | test_cases.emplace_back(new test_cos (type, {7, 1, 5, 3})); |
| 11835 | test_cases.emplace_back(new test_cos (type, {1024, 1024, 1, 1})); |
| 11836 | test_cases.emplace_back(new test_clamp (type, {7, 1, 5, 3})); |
| 11837 | test_cases.emplace_back(new test_clamp (type, {1024, 1024, 1, 1})); |
| 11838 | test_cases.emplace_back(new test_leaky_relu(type, {7, 1, 5, 3})); |
| 11839 | test_cases.emplace_back(new test_leaky_relu(type, {1024, 1024, 1, 1})); |
| 11840 | test_cases.emplace_back(new test_floor (type, {7, 1, 5, 3})); |
| 11841 | test_cases.emplace_back(new test_floor (type, {1024, 1024, 1, 1})); |
| 11842 | test_cases.emplace_back(new test_ceil (type, {7, 1, 5, 3})); |
| 11843 | test_cases.emplace_back(new test_ceil (type, {1024, 1024, 1, 1})); |
| 11844 | test_cases.emplace_back(new test_round (type, {7, 1, 5, 3})); |
| 11845 | test_cases.emplace_back(new test_round (type, {1024, 1024, 1, 1})); |
| 11846 | test_cases.emplace_back(new test_trunc (type, {7, 1, 5, 3})); |
| 11847 | test_cases.emplace_back(new test_trunc (type, {1024, 1024, 1, 1})); |
| 11848 | } |
| 11849 | |
| 11850 | test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 1, 1}, 5)); |
| 11851 | test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 3, 1}, 5)); |
| 11852 | test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 3, 2}, 5)); |
| 11853 | |
| 11854 | #if 0 |
| 11855 | std::uniform_int_distribution<> dist_ne1(1, 50); |
| 11856 | int exponent = 1; |
| 11857 | while (exponent < (1 << 17)) { |
| 11858 | std::uniform_int_distribution<> dist_ne0(exponent, 2*exponent); |
| 11859 | |
| 11860 | for (int n = 0; n < 10; ++n) { |
| 11861 | int64_t ne0 = dist_ne0(rng); |
| 11862 | int64_t ne1 = dist_ne1(rng); |
| 11863 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, GGML_TYPE_F32, {ne0, ne1, 1, 1}, n/2 == 0, 0.1f, ne0 < 1000 ? 4.0f : 0.0f)); |
| 11864 | } |
| 11865 | |
| 11866 | exponent <<= 1; |
| 11867 | } |
| 11868 | #endif |
| 11869 | for (bool mask : {false, true}) { |
| 11870 | for (bool sinks : {false, true}) { |
| 11871 | for (float max_bias : {0.0f, 8.0f}) { |
| 11872 | if (!mask && max_bias > 0.0f) continue; |
| 11873 | for (float scale : {1.0f, 0.1f}) { |
| 11874 | for (int64_t ne0 : {16, 1024}) { |
| 11875 | for (int64_t ne1 : {16, 1024}) { |
| 11876 | if (mask) { |
| 11877 | for (ggml_type m_prec : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 11878 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0, ne1, 1, 1}, mask, sinks, m_prec, {1, 1}, scale, max_bias)); |
| 11879 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0-1, ne1-1, 1, 1}, mask, sinks, m_prec, {1, 1}, scale, max_bias)); |
| 11880 | |
| 11881 | if (ne0 <= 32 && ne1 <= 32) { |
| 11882 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0, ne1, 1, 3}, mask, sinks, m_prec, {3, 1}, scale, max_bias)); |
| 11883 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0-1, ne1-1, 1, 1}, mask, sinks, m_prec, {2, 3}, scale, max_bias)); |
| 11884 | } |
| 11885 | } |
| 11886 | } else { |
| 11887 | /* The precision of mask here doesn't matter as boolean mask is false */ |
| 11888 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0, ne1, 1, 1}, mask, sinks, GGML_TYPE_F32, {1, 1}, scale, max_bias)); |
| 11889 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0-1, ne1-1, 1, 1}, mask, sinks, GGML_TYPE_F32, {1, 1}, scale, max_bias)); |
| 11890 | } |
| 11891 | } |
| 11892 | } |
| 11893 | } |
| 11894 | } |
| 11895 | // inplace tests |
| 11896 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, mask, sinks, GGML_TYPE_F32, {1, 1}, 0.1f, 0.0f, true)); |
| 11897 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, mask, sinks, GGML_TYPE_F16, {1, 1}, 0.1f, 0.0f, true)); |
| 11898 | } |
| 11899 | } |
| 11900 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, true, true, GGML_TYPE_F32, {1, 1}, 0.1f, 0.0f)); |
| 11901 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, true, false, GGML_TYPE_F16, {1, 1}, 0.1f, 0.0f)); |
| 11902 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, false, true, GGML_TYPE_F32, {1, 1}, 0.1f, 0.0f)); |
| 11903 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, true, GGML_TYPE_F32, {1, 1}, 0.1f, 0.0f)); |
| 11904 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, false, GGML_TYPE_F16, {1, 1}, 0.1f, 0.0f)); |
| 11905 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, true, GGML_TYPE_F32, {1, 1}, 0.1f, 8.0f)); |
| 11906 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, true, GGML_TYPE_F16, {1, 1}, 0.1f, 8.0f)); |
| 11907 | |
| 11908 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {200001, 2, 3, 1}, true, true, GGML_TYPE_F32, {1, 1}, 0.1f, 8.0f)); |
| 11909 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {200001, 2, 3, 1}, true, true, GGML_TYPE_F16, {1, 1}, 0.1f, 8.0f)); |
| 11910 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {200000, 1, 1, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f)); |
| 11911 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {200000, 4, 1, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f)); |
| 11912 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {4, 1, 1, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f)); |
| 11913 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {4, 1023, 1, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f)); |
| 11914 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {643251, 3, 1, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f)); |
| 11915 | |
| 11916 | for (float max_bias : {0.0f, 8.0f}) { |
| 11917 | for (float scale : {1.0f, 0.1f}) { |
| 11918 | for (int64_t ne0 : {16, 1024}) { |
| 11919 | for (int64_t ne1 : {16, 1024}) { |
| 11920 | test_cases.emplace_back(new test_soft_max_back(GGML_TYPE_F32, {ne0, ne1, 1, 1}, scale, max_bias)); |
| 11921 | test_cases.emplace_back(new test_soft_max_back(GGML_TYPE_F32, {ne0-1, ne1-1, 1, 1}, scale, max_bias)); |
| 11922 | test_cases.emplace_back(new test_soft_max_back(GGML_TYPE_F32, {ne0, ne1, 2, 3}, scale, max_bias)); |
| 11923 | } |
| 11924 | } |
| 11925 | } |
| 11926 | } |
| 11927 | |
| 11928 | for (bool fw : {true, false}) { // fw == forward |
| 11929 | bool all = true; |
| 11930 | |
| 11931 | for (float fs : { 1.0f, 1.4245f }) { |
| 11932 | for (float ef : { 0.0f, 0.7465f }) { |
| 11933 | for (float af : { 1.0f, 1.4245f }) { |
| 11934 | for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 11935 | for (bool ff : {false, true}) { // freq_factors |
| 11936 | for (float v : { 0, 1 }) { |
| 11937 | test_cases.emplace_back(new test_rope(type, {128, 32, 2, 1}, 128, GGML_ROPE_TYPE_NORMAL, 512, fs, ef, af, ff, v, fw)); // llama 7B |
| 11938 | |
| 11939 | if (all) { |
| 11940 | test_cases.emplace_back(new test_rope(type, {128, 40, 2, 1}, 128, GGML_ROPE_TYPE_NORMAL, 512, fs, ef, af, ff, v, fw)); // llama 13B |
| 11941 | test_cases.emplace_back(new test_rope(type, {128, 52, 2, 1}, 128, GGML_ROPE_TYPE_NORMAL, 512, fs, ef, af, ff, v, fw)); // llama 30B |
| 11942 | test_cases.emplace_back(new test_rope(type, {128, 64, 2, 1}, 128, GGML_ROPE_TYPE_NORMAL, 512, fs, ef, af, ff, v, fw)); // llama 65B |
| 11943 | test_cases.emplace_back(new test_rope(type, {16, 16, 8192, 1}, 16, GGML_ROPE_TYPE_NORMAL, 512, fs, ef, af, ff, v, fw)); |
| 11944 | } |
| 11945 | |
| 11946 | if (all) { |
| 11947 | test_cases.emplace_back(new test_rope(type, { 64, 1, 2, 1}, 64, GGML_ROPE_TYPE_NEOX, 512, fs, ef, af, ff, v, fw)); // neox (falcon 7B) |
| 11948 | test_cases.emplace_back(new test_rope(type, { 64, 71, 2, 1}, 64, GGML_ROPE_TYPE_NEOX, 512, fs, ef, af, ff, v, fw)); // neox (falcon 7B) |
| 11949 | test_cases.emplace_back(new test_rope(type, { 64, 8, 2, 1}, 64, GGML_ROPE_TYPE_NEOX, 512, fs, ef, af, ff, v, fw)); // neox (falcon 40B) |
| 11950 | |
| 11951 | test_cases.emplace_back(new test_rope(type, { 80, 32, 2, 1}, 20, GGML_ROPE_TYPE_NORMAL, 512, fs, ef, af, ff, v, fw)); |
| 11952 | test_cases.emplace_back(new test_rope(type, { 80, 32, 2, 1}, 32, GGML_ROPE_TYPE_NORMAL, 512, fs, ef, af, ff, v, fw)); |
| 11953 | test_cases.emplace_back(new test_rope(type, { 80, 32, 4, 1}, 32, GGML_ROPE_TYPE_NORMAL, 512, fs, ef, af, ff, v, fw)); |
| 11954 | |
| 11955 | test_cases.emplace_back(new test_rope(type, { 80, 32, 2, 1}, 20, GGML_ROPE_TYPE_NEOX, 512, fs, ef, af, ff, v, fw)); // neox (stablelm) |
| 11956 | test_cases.emplace_back(new test_rope(type, { 80, 32, 2, 1}, 32, GGML_ROPE_TYPE_NEOX, 512, fs, ef, af, ff, v, fw)); // neox (phi-2) |
| 11957 | test_cases.emplace_back(new test_rope(type, { 80, 32, 4, 1}, 32, GGML_ROPE_TYPE_NEOX, 512, fs, ef, af, ff, v, fw)); // neox (phi-2) |
| 11958 | test_cases.emplace_back(new test_rope(type, { 16, 16, 8192, 1}, 16, GGML_ROPE_TYPE_NEOX, 512, fs, ef, af, ff, v, fw)); |
| 11959 | } |
| 11960 | |
| 11961 | if (all) { |
| 11962 | test_cases.emplace_back(new test_rope(type, {128, 12, 2, 1}, 128, GGML_ROPE_TYPE_MROPE, 512, fs, ef, af, ff, v, fw)); // rope_multi,m-rope (qwen2vl 2B) |
| 11963 | test_cases.emplace_back(new test_rope(type, {128, 28, 2, 1}, 128, GGML_ROPE_TYPE_MROPE, 512, fs, ef, af, ff, v, fw)); // rope_multi,m-rope (qwen2vl 7B) |
| 11964 | test_cases.emplace_back(new test_rope(type, {128, 12, 2, 1}, 20, GGML_ROPE_TYPE_MROPE, 512, fs, ef, af, ff, v, fw)); |
| 11965 | test_cases.emplace_back(new test_rope(type, {128, 28, 2, 1}, 32, GGML_ROPE_TYPE_MROPE, 512, fs, ef, af, ff, v, fw)); |
| 11966 | test_cases.emplace_back(new test_rope(type, {128, 12, 2, 1}, 128, GGML_ROPE_TYPE_IMROPE, 512, fs, ef, af, ff, v, fw)); // rope_multi,imrope (qwen3vl 2B) |
| 11967 | test_cases.emplace_back(new test_rope(type, {128, 28, 2, 1}, 128, GGML_ROPE_TYPE_IMROPE, 512, fs, ef, af, ff, v, fw)); // rope_multi,imrope (qwen3vl 7B) |
| 11968 | test_cases.emplace_back(new test_rope(type, {128, 12, 2, 1}, 20, GGML_ROPE_TYPE_IMROPE, 512, fs, ef, af, ff, v, fw)); |
| 11969 | test_cases.emplace_back(new test_rope(type, {128, 28, 2, 1}, 32, GGML_ROPE_TYPE_IMROPE, 512, fs, ef, af, ff, v, fw)); |
| 11970 | test_cases.emplace_back(new test_rope(type, { 80, 16, 2, 1}, 80, GGML_ROPE_TYPE_VISION, 512, fs, ef, af, ff, v, fw)); // rope_multi,m-rope (qwen2vl ViT) |
| 11971 | test_cases.emplace_back(new test_rope(type, {128, 16, 2, 1}, 128, GGML_ROPE_TYPE_IMROPE, 512, fs, ef, af, ff, v, fw)); // rope_multi,m-rope (qwen3vl) |
| 11972 | test_cases.emplace_back(new test_rope(type, {16, 16, 8192, 1}, 16, GGML_ROPE_TYPE_IMROPE, 512, fs, ef, af, ff, v, fw)); |
| 11973 | } |
| 11974 | |
| 11975 | test_cases.emplace_back(new test_rope(type, { 64, 128, 2, 1}, 64, GGML_ROPE_TYPE_NEOX, 512, fs, ef, af, ff, v, fw)); // neox (falcon 40B) |
| 11976 | } |
| 11977 | |
| 11978 | // build_rope_2d-style: ROPE on a non-contiguous view |
| 11979 | // that starts at a non-zero offset along dim 0 |
| 11980 | // (e.g. gemma4v vision second-half view). |
| 11981 | for (int rmode : { GGML_ROPE_TYPE_NORMAL, GGML_ROPE_TYPE_NEOX, GGML_ROPE_TYPE_MROPE, GGML_ROPE_TYPE_IMROPE, GGML_ROPE_TYPE_VISION }) { |
| 11982 | test_cases.emplace_back(new test_rope(type, { 36, 16, 2457, 1}, 36, rmode, 512, fs, ef, af, ff, 2, fw)); |
| 11983 | } |
| 11984 | } |
| 11985 | |
| 11986 | all = false; |
| 11987 | } |
| 11988 | } |
| 11989 | } |
| 11990 | } |
| 11991 | } |
| 11992 | |
| 11993 | // single inplace test per type/mode/ff |
| 11994 | for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 11995 | for (int mode : {GGML_ROPE_TYPE_NORMAL, GGML_ROPE_TYPE_NEOX, GGML_ROPE_TYPE_MROPE, GGML_ROPE_TYPE_IMROPE, GGML_ROPE_TYPE_VISION}) { |
| 11996 | for (bool ff : {false, true}) { |
| 11997 | test_cases.emplace_back(new test_rope(type, {128, 32, 2, 1}, 128, mode, 512, 1.4245f, 0.7465f, 1.4245f, ff, 0, true, true)); |
| 11998 | test_cases.emplace_back(new test_rope(type, {128, 32, 2, 1}, 128, mode, 512, 1.4245f, 0.7465f, 1.4245f, ff, 1, true, true)); |
| 11999 | test_cases.emplace_back(new test_rope(type, {128, 32, 2, 3}, 128, mode, 512, 1.4245f, 0.7465f, 1.4245f, ff, 1, true, true)); |
| 12000 | } |
| 12001 | } |
| 12002 | } |
| 12003 | |
| 12004 | // rotated dims window at an offset (ggml_rope_set_offset), not supported for vision mode |
| 12005 | for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 12006 | for (bool fw : {true, false}) { // fw == forward |
| 12007 | for (bool ff : {false, true}) { |
| 12008 | test_cases.emplace_back(new test_rope(type, {128, 32, 2, 1}, 32, GGML_ROPE_TYPE_NORMAL, 512, 1.4245f, 0.7465f, 1.4245f, ff, 0, fw, false, 32)); |
| 12009 | test_cases.emplace_back(new test_rope(type, {128, 32, 2, 1}, 32, GGML_ROPE_TYPE_NEOX, 512, 1.4245f, 0.7465f, 1.4245f, ff, 0, fw, false, 32)); |
| 12010 | test_cases.emplace_back(new test_rope(type, {128, 12, 2, 1}, 24, GGML_ROPE_TYPE_MROPE, 512, 1.4245f, 0.7465f, 1.4245f, ff, 0, fw, false, 32)); |
| 12011 | test_cases.emplace_back(new test_rope(type, {128, 12, 2, 1}, 24, GGML_ROPE_TYPE_IMROPE, 512, 1.4245f, 0.7465f, 1.4245f, ff, 0, fw, false, 32)); |
| 12012 | } |
| 12013 | } |
| 12014 | // inplace with an offset |
| 12015 | test_cases.emplace_back(new test_rope(type, {128, 32, 2, 1}, 32, GGML_ROPE_TYPE_NEOX, 512, 1.4245f, 0.7465f, 1.4245f, false, 0, true, true, 32)); |
| 12016 | } |
| 12017 | |
| 12018 | // Real-model RoPE: F32 forward, packed Q, 512-token prefill. |
| 12019 | test_cases.emplace_back(new test_rope(GGML_TYPE_F32, {256, 8, 512, 1}, 64, GGML_ROPE_TYPE_IMROPE, 512, 1.0f, 0.0f, 1.0f, false, 0, true)); // qwen3.5 0.8B |
| 12020 | test_cases.emplace_back(new test_rope(GGML_TYPE_F32, {256, 16, 512, 1}, 64, GGML_ROPE_TYPE_IMROPE, 512, 1.0f, 0.0f, 1.0f, false, 0, true)); // qwen3.5 4B |
| 12021 | test_cases.emplace_back(new test_rope(GGML_TYPE_F32, {256, 8, 512, 1}, 256, GGML_ROPE_TYPE_NEOX, 512, 1.0f, 0.0f, 1.0f, false, 0, true)); // gemma4 E2B sliding |
| 12022 | test_cases.emplace_back(new test_rope(GGML_TYPE_F32, {512, 8, 512, 1}, 128, GGML_ROPE_TYPE_NEOX, 512, 1.0f, 0.0f, 1.0f, true, 0, true)); // gemma4 E4B global |
| 12023 | |
| 12024 | for (int v : { 0, 1, 2, 3, 16 }) { |
| 12025 | for (int dim : { 0, 1, 2, 3, }) { |
| 12026 | test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {11, 12, 13, 14}, 7, dim, v)); |
| 12027 | test_cases.emplace_back(new test_concat(GGML_TYPE_F16, {11, 12, 13, 14}, 7, dim, v)); |
| 12028 | test_cases.emplace_back(new test_concat(GGML_TYPE_BF16, {11, 12, 13, 14}, 7, dim, v)); |
| 12029 | test_cases.emplace_back(new test_concat(GGML_TYPE_I8, {11, 12, 13, 14}, 7, dim, v)); |
| 12030 | test_cases.emplace_back(new test_concat(GGML_TYPE_I16, {11, 12, 13, 14}, 7, dim, v)); |
| 12031 | test_cases.emplace_back(new test_concat(GGML_TYPE_I32, {11, 12, 13, 14}, 7, dim, v)); |
| 12032 | test_cases.emplace_back(new test_concat(GGML_TYPE_I64, {11, 12, 13, 14}, 7, dim, v)); |
| 12033 | } |
| 12034 | } |
| 12035 | |
| 12036 | for (ggml_type type_a : { GGML_TYPE_Q4_0, GGML_TYPE_Q4_1, GGML_TYPE_Q5_0, GGML_TYPE_Q5_1, GGML_TYPE_Q8_0 }) { |
| 12037 | for (int v : { 0, 4, 8, 12 }) { |
| 12038 | for (int dim : { 0, 1, 2, 3, }) { |
| 12039 | test_cases.emplace_back(new test_concat(type_a, {128, 12, 13, 14}, dim == 0 ? 256 : 7, dim, v)); |
| 12040 | } |
| 12041 | } |
| 12042 | } |
| 12043 | |
| 12044 | // few long rows, which backends may split across workgroups |
| 12045 | for (int dim : { 0, 1 }) { |
| 12046 | test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {98304, 8, 1, 1}, dim == 0 ? 288 : 2, dim, 0)); |
| 12047 | test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {20000, 3, 2, 1}, dim == 0 ? 333 : 1, dim, 1)); |
| 12048 | test_cases.emplace_back(new test_concat(GGML_TYPE_Q8_0, {8192, 2, 1, 1}, dim == 0 ? 4096 : 1, dim, 0)); |
| 12049 | } |
| 12050 | |
| 12051 | for (ggml_sort_order order : {GGML_SORT_ORDER_ASC, GGML_SORT_ORDER_DESC}) { |
| 12052 | for (uint32_t i = 4; i <= 1024*1024; i *= 2) { |
| 12053 | test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {i-1, 1, 1, 1})); |
| 12054 | test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {i, 1, 1, 1})); |
| 12055 | } |
| 12056 | test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {16, 10, 10, 10}, order)); |
| 12057 | test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {60, 10, 10, 10}, order)); // qwen |
| 12058 | test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {1023, 2, 1, 3}, order)); |
| 12059 | test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {1024, 2, 1, 3}, order)); |
| 12060 | test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {1025, 2, 1, 3}, order)); |
| 12061 | test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {1025, 256, 1, 1}, order)); // test ceildiv in CUDA's CUB's DeviceSegmentedSort |
| 12062 | test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {2047, 2, 1, 3}, order)); |
| 12063 | test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {2048, 2, 1, 3}, order)); |
| 12064 | test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {2049, 2, 1, 3}, order)); |
| 12065 | test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {2, 8, 8192, 1}, order)); // bailingmoe2 (group selection) |
| 12066 | test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {2048, 512, 1, 1}, order)); // test CUDA dispatching to radix sort for nrows > = 1 in graph mode |
| 12067 | } |
| 12068 | |
| 12069 | for (int n = 1; n < 5; ++n) { |
| 12070 | for (int k = 1; k <= n; ++k) { |
| 12071 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {n, 2, 1, 3}, k, true)); |
| 12072 | } |
| 12073 | } |
| 12074 | for (int k : {1, 10, 40}) { |
| 12075 | test_cases.emplace_back(new test_top_k_inf({4096, 2, 1, 1}, k)); |
| 12076 | test_cases.emplace_back(new test_top_k_inf({248320, 1, 1, 1}, k)); |
| 12077 | } |
| 12078 | |
| 12079 | for (int i = 0; i < 20; ++i) { |
| 12080 | for (int k : {1, 2, 3, 7, 15, 100, 500, 1023, 9999}) { |
| 12081 | if (k <= 1<<i) { |
| 12082 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {(1<<i), 1, 1, 1}, k)); |
| 12083 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {(1<<i) + 11, 1, 2, 1}, k)); |
| 12084 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {(1<<i) + 11, 1, 2, 1}, k, true)); |
| 12085 | } |
| 12086 | } |
| 12087 | } |
| 12088 | for (int k : {4, 8, 16, 32}) { |
| 12089 | for (int nrows : {1, 8, 16}) { |
| 12090 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {202048, nrows, 1, 1}, k)); |
| 12091 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {151936, nrows, 1, 1}, k)); |
| 12092 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {8192, nrows, 1, 1}, k)); |
| 12093 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {8193, nrows, 1, 1}, k)); |
| 12094 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {8192, nrows, 1, 1}, k, true)); |
| 12095 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {202048, nrows, 1, 1}, k, true)); |
| 12096 | } |
| 12097 | } |
| 12098 | |
| 12099 | for (int k : {1, 2, 3, 7, 15}) { |
| 12100 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {16, 10, 10, 10}, k)); |
| 12101 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {60, 10, 10, 10}, k)); |
| 12102 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {1023, 2, 1, 3}, k)); |
| 12103 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {1024, 2, 1, 3}, k)); |
| 12104 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {1025, 2, 1, 3}, k)); |
| 12105 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {16384, 1, 1, 1}, k)); |
| 12106 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {2047, 2, 1, 3}, k)); |
| 12107 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {2048, 2, 1, 3}, k)); |
| 12108 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {2049, 2, 1, 3}, k)); |
| 12109 | } |
| 12110 | |
| 12111 | // Large-k, including multi-row and ties (qwen4exp) |
| 12112 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, { 1024, 1, 1, 1 }, 1024)); |
| 12113 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, { 2048, 2, 1, 1 }, 1024)); |
| 12114 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, { 4096, 1, 1, 1 }, 2048)); |
| 12115 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, { 8192, 2, 1, 1 }, 2051)); |
| 12116 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, { 33024, 1, 1, 1 }, 2051)); |
| 12117 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, { 33024, 4, 1, 1 }, 2051)); |
| 12118 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, { 8192, 2, 1, 1 }, 2051, true)); |
| 12119 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, { 33024, 4, 1, 1 }, 2051, true)); |
| 12120 | |
| 12121 | // rows that CUDA processes in several chunks (bitonic, radix) |
| 12122 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, { 500, 40000, 1, 1 }, 16)); |
| 12123 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, { 1100, 33000, 1, 1 }, 16)); |
| 12124 | |
| 12125 | // qwen4exp QSA indexer top-k fusion (get_rows + f16 mask + top_k) |
| 12126 | test_cases.emplace_back(new test_topk_qsa(512, 2048, 1, 1, 1500)); |
| 12127 | test_cases.emplace_back(new test_topk_qsa(512, 2048, 2, 1, 1500)); |
| 12128 | test_cases.emplace_back(new test_topk_qsa(256, 2048, 4, 2, 2000)); |
| 12129 | test_cases.emplace_back(new test_topk_qsa(64, 256, 2, 1, 200)); // small k: unfused fallback |
| 12130 | |
| 12131 | // exhaustive top_k tests |
| 12132 | //for (int i = 1; i < 9999; ++i) { |
| 12133 | // test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {i, 2, 1, 3}, rand() % i + 1)); |
| 12134 | //} |
| 12135 | |
| 12136 | for (ggml_scale_mode mode : {GGML_SCALE_MODE_NEAREST, GGML_SCALE_MODE_BILINEAR, GGML_SCALE_MODE_BICUBIC, ggml_scale_mode(GGML_SCALE_MODE_BILINEAR | GGML_SCALE_FLAG_ANTIALIAS)}) { |
| 12137 | test_cases.emplace_back(new test_upscale(GGML_TYPE_F32, {512, 512, 3, 2}, 2, mode)); |
| 12138 | test_cases.emplace_back(new test_upscale(GGML_TYPE_F32, {512, 512, 3, 2}, 2, mode, true)); |
| 12139 | test_cases.emplace_back(new test_interpolate(GGML_TYPE_F32, {2, 5, 7, 11}, {5, 7, 11, 13}, mode)); |
| 12140 | test_cases.emplace_back(new test_interpolate(GGML_TYPE_F32, {5, 7, 11, 13}, {2, 5, 7, 11}, mode)); |
| 12141 | } |
| 12142 | for (ggml_scale_mode mode : {GGML_SCALE_MODE_BILINEAR, GGML_SCALE_MODE_BICUBIC}) { |
| 12143 | test_cases.emplace_back(new test_interpolate(GGML_TYPE_F32, {2, 5, 7, 11}, {5, 7, 11, 13}, (ggml_scale_mode)(mode | GGML_SCALE_FLAG_ALIGN_CORNERS))); |
| 12144 | test_cases.emplace_back(new test_interpolate(GGML_TYPE_F32, {1, 4, 3, 2}, {2, 8, 3, 2}, (ggml_scale_mode)(mode | GGML_SCALE_FLAG_ALIGN_CORNERS))); |
| 12145 | test_cases.emplace_back(new test_interpolate(GGML_TYPE_F32, {4, 1, 3, 2}, {1, 1, 3, 2}, (ggml_scale_mode)(mode | GGML_SCALE_FLAG_ALIGN_CORNERS))); |
| 12146 | test_cases.emplace_back(new test_interpolate(GGML_TYPE_F32, {64, 48, 48, 1}, {156, 104, 64, 1}, (ggml_scale_mode)(mode | GGML_SCALE_FLAG_ALIGN_CORNERS), {2, 0, 1, 3})); |
| 12147 | } |
| 12148 | |
| 12149 | test_cases.emplace_back(new test_sum()); |
| 12150 | test_cases.emplace_back(new test_sum(GGML_TYPE_F32, {11, 5, 6, 3}, {0, 2, 1, 3})); // row-contiguous but non-contiguous |
| 12151 | test_cases.emplace_back(new test_sum(GGML_TYPE_F32, {11, 5, 6, 3}, {0, 3, 2, 1})); |
| 12152 | test_cases.emplace_back(new test_sum(GGML_TYPE_F32, {11, 5, 6, 3}, {0, 1, 3, 2})); |
| 12153 | test_cases.emplace_back(new test_mean()); |
| 12154 | test_cases.emplace_back(new test_mean(GGML_TYPE_F32, { 33, 1, 1, 1 })); |
| 12155 | test_cases.emplace_back(new test_mean(GGML_TYPE_F32, { 33, 256, 1, 1 })); |
| 12156 | test_cases.emplace_back(new test_mean(GGML_TYPE_F32, { 32769, 1, 1, 1 })); |
| 12157 | test_cases.emplace_back(new test_mean(GGML_TYPE_F32, { 32, 1, 1, 1 })); |
| 12158 | test_cases.emplace_back(new test_mean(GGML_TYPE_F32, { 32, 256, 1, 1 })); |
| 12159 | test_cases.emplace_back(new test_mean(GGML_TYPE_F32, { 32768, 1, 1, 1 })); |
| 12160 | test_cases.emplace_back(new test_mean(GGML_TYPE_F32, { 11, 5, 6, 3 }, true, false)); |
| 12161 | test_cases.emplace_back(new test_mean(GGML_TYPE_F32, { 11, 5, 6, 3 }, false, true)); |
| 12162 | test_cases.emplace_back(new test_mean(GGML_TYPE_F32, { 11, 5, 6, 3 }, true, true)); |
| 12163 | test_cases.emplace_back(new test_sum(GGML_TYPE_F32, { 33, 1, 1, 1 })); |
| 12164 | test_cases.emplace_back(new test_sum(GGML_TYPE_F32, { 33, 1024, 1, 1 })); |
| 12165 | test_cases.emplace_back(new test_sum(GGML_TYPE_F32, { 33, 256, 1, 1 })); |
| 12166 | test_cases.emplace_back(new test_sum(GGML_TYPE_F32, { 33, 256, 1, 1 }, { 1, 0, 2, 3 })); // sum dst not-contiguous |
| 12167 | test_cases.emplace_back(new test_sum(GGML_TYPE_F32, { 10, 5, 4, 3 }, { 1, 0, 2, 3 })); // non-contiguous rows, 4D |
| 12168 | test_cases.emplace_back(new test_sum(GGML_TYPE_F32, { 11, 5, 6, 3 }, { 2, 0, 1, 3 })); // rotated dims, nb[0] != sizeof(float) |
| 12169 | test_cases.emplace_back(new test_sum(GGML_TYPE_F32, {11, 5, 6, 3}, {0, 0, 0, 0}, true)); // sliced view (gaps) |
| 12170 | test_cases.emplace_back(new test_sum(GGML_TYPE_F32, {11, 5, 6, 3}, {1, 0, 2, 3}, true)); // sliced + non-contiguous rows |
| 12171 | test_cases.emplace_back(new test_sum_rows()); |
| 12172 | test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 11, 5, 6, 3 }, true, false)); |
| 12173 | test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 11, 5, 6, 3 }, false, true)); |
| 12174 | test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 11, 5, 6, 3 }, true, true)); |
| 12175 | test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 16, 5, 6, 3 }, true, false)); |
| 12176 | test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 16, 5, 6, 3 }, false, true)); |
| 12177 | test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 16, 5, 6, 3 }, true, true)); |
| 12178 | test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 33, 1, 1, 1 })); |
| 12179 | test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 33, 1024, 1, 1 })); |
| 12180 | test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 33, 256, 1, 1 })); |
| 12181 | test_cases.emplace_back(new test_group_norm(GGML_TYPE_F32, {64, 64, 320, 1})); |
| 12182 | test_cases.emplace_back(new test_group_norm(GGML_TYPE_F32, {9, 9, 1280, 1})); |
| 12183 | test_cases.emplace_back(new test_group_norm_mul_add(GGML_TYPE_F32, {64, 64, 320, 1})); |
| 12184 | test_cases.emplace_back(new test_group_norm_mul_add(GGML_TYPE_F32, {9, 9, 1280, 1})); |
| 12185 | test_cases.emplace_back(new test_acc(GGML_TYPE_F32, {256, 17, 1, 1}, {256, 16, 1, 1}, -1)); |
| 12186 | test_cases.emplace_back(new test_acc(GGML_TYPE_F32, {256, 17, 2, 3}, {256, 16, 2, 3}, -1)); |
| 12187 | test_cases.emplace_back(new test_acc(GGML_TYPE_F32, {256, 17, 2, 3}, {128, 16, 2, 3}, -1)); |
| 12188 | test_cases.emplace_back(new test_acc(GGML_TYPE_F32, {256, 17, 2, 3}, {256, 16, 2, 3}, 1)); |
| 12189 | test_cases.emplace_back(new test_acc(GGML_TYPE_F32, {256, 17, 2, 3}, {128, 16, 2, 3}, 2)); |
| 12190 | test_cases.emplace_back(new test_acc(GGML_TYPE_F32, {256, 17, 2, 3}, {64, 16, 2, 3}, 3)); |
| 12191 | |
| 12192 | test_cases.emplace_back(new test_pad()); |
| 12193 | test_cases.emplace_back(new test_pad(GGML_TYPE_F32, {33, 17, 2, 1}, 4, 3, true)); // circular |
| 12194 | test_cases.emplace_back(new test_pad_ext()); |
| 12195 | test_cases.emplace_back(new test_pad(GGML_TYPE_F32, {1024, 1, 1, 1}, 1, 0, false)); |
| 12196 | test_cases.emplace_back(new test_pad(GGML_TYPE_F32, {1024, 2, 1, 1}, 1, 0, false)); |
| 12197 | test_cases.emplace_back(new test_pad(GGML_TYPE_F32, {1024, 16, 1, 1}, 0, 1, false)); |
| 12198 | test_cases.emplace_back(new test_pad(GGML_TYPE_F32, {1023, 1, 1, 1}, 1, 0, false)); |
| 12199 | test_cases.emplace_back(new test_pad(GGML_TYPE_F32, {1023, 8, 1, 1}, 1, 0, false)); |
| 12200 | test_cases.emplace_back(new test_pad(GGML_TYPE_F32, {1025, 1, 1, 1}, 1, 0, false)); |
| 12201 | test_cases.emplace_back(new test_pad(GGML_TYPE_F32, {1025, 8, 1, 1}, 1, 0, false)); |
| 12202 | test_cases.emplace_back(new test_pad(GGML_TYPE_F32, {2048, 1, 1, 1}, 1, 0, false)); |
| 12203 | test_cases.emplace_back(new test_pad(GGML_TYPE_F32, {2048, 4, 1, 1}, 1, 0, false)); |
| 12204 | test_cases.emplace_back(new test_pad(GGML_TYPE_F32, {2049, 1, 1, 1}, 1, 0, false)); |
| 12205 | test_cases.emplace_back(new test_pad(GGML_TYPE_F32, {100, 1, 1, 1}, 100, 0, false)); |
| 12206 | test_cases.emplace_back(new test_pad(GGML_TYPE_F32, {100, 1, 1, 1}, 0, 100, false)); |
| 12207 | test_cases.emplace_back(new test_pad(GGML_TYPE_F32, {100, 100, 1, 1}, 50, 50, false)); |
| 12208 | // more than 65535 rows or slices, beyond the CUDA grid.y/grid.z limit |
| 12209 | test_cases.emplace_back(new test_pad(GGML_TYPE_F32, {4, 70000, 1, 1}, 1, 1, false)); |
| 12210 | test_cases.emplace_back(new test_pad(GGML_TYPE_F32, {4, 70000, 1, 1}, 1, 1, true)); |
| 12211 | test_cases.emplace_back(new test_pad_ext(GGML_TYPE_F32, {4, 2, 300, 300}, 1, 1, 0, 0, 0, 0, 0, 0, 0, false)); |
| 12212 | |
| 12213 | test_cases.emplace_back(new test_pad_reflect_1d()); |
| 12214 | test_cases.emplace_back(new test_pad_reflect_1d(GGML_TYPE_F32, {3000, 384, 4, 1})); |
| 12215 | test_cases.emplace_back(new test_roll()); |
| 12216 | test_cases.emplace_back(new test_roll(3, -2, 1, -1, true)); |
| 12217 | test_cases.emplace_back(new test_arange()); |
| 12218 | test_cases.emplace_back(new test_arange(GGML_TYPE_F32, 3.0f, 3.5f, 1.0f)); |
| 12219 | test_cases.emplace_back(new test_arange(GGML_TYPE_F32, -10.0f, 10.0f, 0.3f)); |
| 12220 | for (float n : {255.0f, 256.0f, 257.0f, 1023.0f, 1024.0f, 1025.0f}) { |
| 12221 | test_cases.emplace_back(new test_arange(GGML_TYPE_F32, -1.0f, -1.0f + n * 0.25f, 0.25f)); |
| 12222 | } |
| 12223 | test_cases.emplace_back(new test_arange(GGML_TYPE_F32, 0.0f, 1048576.0f, 1.0f)); |
| 12224 | test_cases.emplace_back(new test_timestep_embedding()); |
| 12225 | test_cases.emplace_back(new test_leaky_relu()); |
| 12226 | |
| 12227 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 10, 5, 4, 3 })); |
| 12228 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 127, 5, 4, 3 })); |
| 12229 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 128, 5, 4, 3 })); |
| 12230 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 128, 128, 4, 4 })); |
| 12231 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 255, 5, 4, 3 })); |
| 12232 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 256, 5, 4, 3 })); |
| 12233 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 511, 5, 4, 3 })); |
| 12234 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 512, 5, 4, 3 })); |
| 12235 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 1023, 5, 4, 3 })); |
| 12236 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 1024, 5, 4, 3 })); |
| 12237 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 2047, 5, 4, 3 })); |
| 12238 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 2048, 5, 4, 3 })); |
| 12239 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 201*1204, 1, 1, 1 })); |
| 12240 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 312*1205, 1, 1, 1 })); |
| 12241 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 20481, 4, 1, 1 })); |
| 12242 | |
| 12243 | test_cases.emplace_back(new test_xielu()); |
| 12244 | test_cases.emplace_back(new test_xielu(GGML_TYPE_F16)); |
| 12245 | test_cases.emplace_back(new test_xielu(GGML_TYPE_BF16)); |
| 12246 | test_cases.emplace_back(new test_xielu(GGML_TYPE_F32, { 512, 16, 1, 1 })); |
| 12247 | test_cases.emplace_back(new test_xielu(GGML_TYPE_F16, { 512, 16, 1, 1 })); |
| 12248 | test_cases.emplace_back(new test_xielu(GGML_TYPE_BF16, { 512, 16, 1, 1 })); |
| 12249 | |
| 12250 | test_cases.emplace_back(new test_tri(GGML_TRI_TYPE_LOWER)); |
| 12251 | test_cases.emplace_back(new test_tri(GGML_TRI_TYPE_LOWER_DIAG)); |
| 12252 | test_cases.emplace_back(new test_tri(GGML_TRI_TYPE_UPPER)); |
| 12253 | test_cases.emplace_back(new test_tri(GGML_TRI_TYPE_UPPER_DIAG)); |
| 12254 | |
| 12255 | test_cases.emplace_back(new test_fill(0.0f)); |
| 12256 | test_cases.emplace_back(new test_fill(2.0f, GGML_TYPE_F32, { 303, 207, 11, 3 })); |
| 12257 | test_cases.emplace_back(new test_fill(-152.0f, GGML_TYPE_F32, { 800, 600, 4, 4 })); |
| 12258 | test_cases.emplace_back(new test_fill(3.5f, GGML_TYPE_F32, { 2048, 512, 2, 2 })); |
| 12259 | test_cases.emplace_back(new test_fill(2.0f, GGML_TYPE_F16, { 303, 207, 11, 3 })); |
| 12260 | test_cases.emplace_back(new test_fill(-152.0f, GGML_TYPE_F16, { 800, 600, 4, 4 })); |
| 12261 | test_cases.emplace_back(new test_fill(3.5f, GGML_TYPE_F16, { 2048, 512, 2, 2 })); |
| 12262 | |
| 12263 | test_cases.emplace_back(new test_diag()); |
| 12264 | test_cases.emplace_back(new test_diag(GGML_TYPE_F32, { 79, 1, 19, 13 })); |
| 12265 | test_cases.emplace_back(new test_diag(GGML_TYPE_F32, { 256, 1, 8, 16 })); |
| 12266 | |
| 12267 | test_cases.emplace_back(new test_solve_tri()); |
| 12268 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 11, 11, 1, 1 }, { 5, 11, 1, 1 })); |
| 12269 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 17, 17, 2, 4 }, { 9, 17, 2, 4 })); |
| 12270 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 30, 30, 7, 1 }, { 8, 30, 7, 1 })); |
| 12271 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 42, 42, 5, 2 }, { 10, 42, 5, 2 })); |
| 12272 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 64, 64, 2, 2 }, { 10, 64, 2, 2 })); |
| 12273 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 64, 64, 2, 2 }, { 64, 64, 2, 2 })); |
| 12274 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 79, 79, 5, 3 }, { 417, 79, 5, 3 })); |
| 12275 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 128, 128, 4, 2 }, { 32, 128, 4, 2 })); |
| 12276 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 80, 80, 2, 8 }, { 80, 80, 2, 8 })); |
| 12277 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 80, 80, 2, 8 }, { 79, 80, 2, 8 })); |
| 12278 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 80, 80, 2, 8 }, { 81, 80, 2, 8 })); |
| 12279 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 80, 80, 8, 8 }, { 80, 80, 8, 8 })); |
| 12280 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 80, 80, 8, 8 }, { 79, 80, 8, 8 })); |
| 12281 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 80, 80, 8, 8 }, { 81, 80, 8, 8 })); |
| 12282 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 84, 84, 4, 4 }, { 32, 84, 4, 4 })); |
| 12283 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 95, 95, 8, 8 }, { 40, 95, 8, 8 })); |
| 12284 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 100, 100, 4, 4 }, { 41, 100, 4, 4 })); |
| 12285 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 128, 128, 4, 4 }, { 31, 128, 4, 4 })); |
| 12286 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 128, 128, 4, 4 }, { 32, 128, 4, 4 })); |
| 12287 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 128, 128, 3, 4 }, { 32, 128, 3, 4 })); |
| 12288 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 128, 128, 4, 1 }, { 32, 128, 4, 1 })); |
| 12289 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 64, 64, 4, 4 }, { 200, 64, 4, 4 })); |
| 12290 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 64, 64, 4, 4 }, { 384, 64, 4, 4 })); |
| 12291 | |
| 12292 | for (int tfrm : {0, 1, 2}) { |
| 12293 | for (bool circular : {false, true}) { |
| 12294 | test_cases.emplace_back(new test_pad_ext(GGML_TYPE_F32, {512, 512, 1, 1}, 0, 1, 0, 1, 0, 0, 0, 0, tfrm, circular)); |
| 12295 | test_cases.emplace_back(new test_pad_ext(GGML_TYPE_F32, {11, 22, 33, 44}, 1, 2, 3, 4, 5, 6, 7, 8, tfrm, circular)); |
| 12296 | test_cases.emplace_back(new test_pad_ext(GGML_TYPE_F32, {11, 22, 33, 44}, 0, 2, 0, 4, 0, 6, 0, 8, tfrm, circular)); |
| 12297 | } |
| 12298 | } |
| 12299 | |
| 12300 | // prefill-shaped cases with long KV (nb >= 32, kv >= 1024): covers the |
| 12301 | // XMX/GEMM-accelerated SYCL FA path which only activates for these shapes. |
| 12302 | for (int kv : { 1024, 2048, }) { |
| 12303 | for (int hs : { 64, 128, 256, }) { |
| 12304 | for (int nb : { 32, 64, }) { |
| 12305 | for (ggml_type type_KV : { GGML_TYPE_F16, GGML_TYPE_Q8_0, GGML_TYPE_Q4_0, }) { |
| 12306 | test_cases.emplace_back(new test_flash_attn_ext(hs, hs, 8, {4, 1}, kv, nb, true, false, 0, 0, GGML_PREC_F32, type_KV, type_KV)); |
| 12307 | } |
| 12308 | } |
| 12309 | } |
| 12310 | } |
| 12311 | |
| 12312 | // KV-head counts that do not divide the device count |
| 12313 | for (int hs : { 128, 256, 512, }) { |
| 12314 | for (int nh : { 1, 2, 6, }) { |
| 12315 | for (int nr2 : { 4, 8, }) { |
| 12316 | for (int nb : { 75, 1024, }) { |
| 12317 | test_cases.emplace_back(new test_flash_attn_ext(hs, hs, nh, {nr2, 1}, 1024, nb, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12318 | } |
| 12319 | } |
| 12320 | } |
| 12321 | } |
| 12322 | |
| 12323 | for (int hsk : { 40, 64, 72, 80, 96, 128, 192, 256, 320, 512, 576 }) { |
| 12324 | for (int hsv : { 40, 64, 72, 80, 96, 128, 192, 256, 512 }) { |
| 12325 | if (hsk != 96 && hsk != 192 && hsk != 320 && hsk != 576 && hsk != hsv) continue; |
| 12326 | if (hsk == 96 && (hsv != 64 && hsv != 96)) continue; // MiniCPM3 |
| 12327 | if (hsk == 192 && (hsv != 128 && hsv != 192)) continue; |
| 12328 | if (hsk == 576 && hsv != 512) continue; // DeepSeek MLA |
| 12329 | if (hsk == 320 && hsv != 256) continue; // Mistral4 MLA |
| 12330 | |
| 12331 | for (bool mask : { true, false } ) { |
| 12332 | for (bool sinks : { true, false } ) { |
| 12333 | for (float max_bias : { 0.0f, 8.0f }) { |
| 12334 | if (!mask && max_bias > 0.0f) continue; |
| 12335 | for (float logit_softcap : {0.0f, 10.0f}) { |
| 12336 | if (hsk != 128 && logit_softcap != 0.0f) continue; |
| 12337 | for (int nh : { 1, 4 }) { |
| 12338 | if (nh == 1 && hsk != 320 && hsk != 576) continue; |
| 12339 | for (int nr3 : { 1, 3, }) { |
| 12340 | if (hsk > 64 && nr3 > 1) continue; // skip broadcast for large head sizes |
| 12341 | for (int nr2 : { 1, 4, 8, 12, 16, 20, 32 }) { |
| 12342 | if (nr2 == 8 && hsk != 192) continue; |
| 12343 | if (nr2 == 12 && hsk != 128) continue; |
| 12344 | if (nr2 == 16 && hsk != 192) continue; |
| 12345 | if (nr2 == 20 && (nh != 1 || hsk != 576)) continue; |
| 12346 | if (nr2 == 32 && (nh != 1 || hsk != 320)) continue; |
| 12347 | //for (int kv : { 1, 17, 31, 33, 61, 113, 65, 127, 129, 130, 255, 260, 371, 380, 407, 512, 1024, }) { |
| 12348 | for (int kv : { 113, 512, 1024, }) { |
| 12349 | if (nr2 != 1 && kv != 512) continue; |
| 12350 | for (int nb : { 1, 3, 32, 75, }) { |
| 12351 | for (ggml_prec prec : {GGML_PREC_F32, GGML_PREC_DEFAULT}) { |
| 12352 | if (hsk != 128 && prec == GGML_PREC_DEFAULT) continue; |
| 12353 | for (ggml_type type_KV : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_Q8_0, GGML_TYPE_Q5_1, GGML_TYPE_Q5_0, GGML_TYPE_Q4_1, GGML_TYPE_Q4_0, GGML_TYPE_IQ4_NL}) { |
| 12354 | if (type_KV != GGML_TYPE_F16 && hsk != 64 && hsk != 72) continue; |
| 12355 | // DeepSeek MLA: the V cache is a sub-view of the K cache |
| 12356 | const bool v_is_view_of_k = hsk == 576; |
| 12357 | test_cases.emplace_back(new test_flash_attn_ext( |
| 12358 | hsk, hsv, nh, {nr2, nr3}, kv, nb, mask, sinks, max_bias, logit_softcap, prec, type_KV, type_KV, {0, 1, 2, 3}, true, v_is_view_of_k)); |
| 12359 | // run fewer test cases permuted |
| 12360 | if (mask == true && max_bias == 0.0f && logit_softcap == 0 && kv == 512) { |
| 12361 | test_cases.emplace_back(new test_flash_attn_ext( |
| 12362 | hsk, hsv, nh, {nr2, nr3}, kv, nb, mask, sinks, max_bias, logit_softcap, prec, type_KV, type_KV, {0, 2, 1, 3}, true, v_is_view_of_k)); |
| 12363 | } |
| 12364 | } |
| 12365 | } |
| 12366 | } |
| 12367 | } |
| 12368 | } |
| 12369 | } |
| 12370 | } |
| 12371 | } |
| 12372 | } |
| 12373 | } |
| 12374 | } |
| 12375 | } |
| 12376 | } |
| 12377 | |
| 12378 | // asymmetric head_dim (hsk != hsv) with one or both sides not 64-aligned |
| 12379 | test_cases.emplace_back(new test_flash_attn_ext(72, 64, 4, {1, 1}, 256, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12380 | test_cases.emplace_back(new test_flash_attn_ext(64, 72, 4, {1, 1}, 256, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12381 | test_cases.emplace_back(new test_flash_attn_ext(65, 67, 4, {1, 1}, 113, 75, true, true, 8.0f, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12382 | test_cases.emplace_back(new test_flash_attn_ext(65, 67, 4, {1, 1}, 17, 75, false, false, 0, 1.0f, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12383 | test_cases.emplace_back(new test_flash_attn_ext(65, 67, 4, {1, 1}, 113, 75, false, false, 0, 1.0f, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12384 | |
| 12385 | // mixed quant and Q1_0 test cases |
| 12386 | test_cases.emplace_back(new test_flash_attn_ext(64, 64, 4, {1, 1}, 128, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q4_0)); |
| 12387 | test_cases.emplace_back(new test_flash_attn_ext(64, 64, 4, {1, 1}, 128, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q4_0, GGML_TYPE_F16)); |
| 12388 | test_cases.emplace_back(new test_flash_attn_ext(72, 72, 4, {1, 1}, 96, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q4_0, GGML_TYPE_Q8_0)); |
| 12389 | test_cases.emplace_back(new test_flash_attn_ext(64, 64, 4, {1, 1}, 96, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F32)); |
| 12390 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 4, {1, 1}, 256, 1, false, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_Q4_0)); |
| 12391 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 4, {1, 1}, 96, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q1_0, GGML_TYPE_Q1_0)); |
| 12392 | test_cases.emplace_back(new test_flash_attn_ext(128, 64, 4, {1, 1}, 128, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q1_0, GGML_TYPE_Q4_0)); |
| 12393 | test_cases.emplace_back(new test_flash_attn_ext(64, 128, 4, {1, 1}, 128, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q4_0, GGML_TYPE_Q1_0)); |
| 12394 | test_cases.emplace_back(new test_flash_attn_ext(128, 64, 4, {1, 1}, 64, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q1_0, GGML_TYPE_F16)); |
| 12395 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 4, {1, 1}, 96, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q2_0, GGML_TYPE_Q2_0)); |
| 12396 | test_cases.emplace_back(new test_flash_attn_ext(128, 64, 4, {1, 1}, 128, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q2_0, GGML_TYPE_Q4_0)); |
| 12397 | test_cases.emplace_back(new test_flash_attn_ext(64, 128, 4, {1, 1}, 128, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q4_0, GGML_TYPE_Q2_0)); |
| 12398 | test_cases.emplace_back(new test_flash_attn_ext(128, 64, 4, {1, 1}, 64, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q2_0, GGML_TYPE_F16)); |
| 12399 | |
| 12400 | // q8_0 KV cases: decode and prompt batches, KV pad, permuted KV, feature flags, and long context |
| 12401 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 113, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0)); |
| 12402 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 1024, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0)); |
| 12403 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 1024, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 2, 1, 3})); |
| 12404 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 2}, 1025, 1, true, true, 8, 30, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0)); |
| 12405 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 1025, 64, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 2, 1, 3})); |
| 12406 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 16384, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0)); |
| 12407 | |
| 12408 | // MLA shape: the V cache is a sub-view of the K cache, with quantized KV |
| 12409 | test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {8, 1}, 113, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 1, 2, 3}, true, true)); |
| 12410 | test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {8, 1}, 1024, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 1, 2, 3}, true, true)); |
| 12411 | test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {8, 1}, 1024, 64, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 1, 2, 3}, true, true)); |
| 12412 | test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {8, 1}, 4096, 24, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 1, 2, 3}, true, true)); |
| 12413 | test_cases.emplace_back(new test_flash_attn_ext(512, 512, 1, {8, 1}, 4096, 24, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 1, 2, 3}, true, false)); |
| 12414 | |
| 12415 | // Sparse mask hint: supported decode/prefill layouts and dense fallbacks. |
| 12416 | test_cases.emplace_back(new test_flash_attn_ext(512, 512, 1, { 8, 1}, 4096, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512)); |
| 12417 | test_cases.emplace_back(new test_flash_attn_ext(512, 512, 1, { 8, 2}, 4096, 3, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 768)); |
| 12418 | test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {16, 1}, 4096, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true, 512)); |
| 12419 | test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {16, 2}, 4096, 2, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true, 768)); |
| 12420 | test_cases.emplace_back(new test_flash_attn_ext(512, 512, 1, { 8, 1}, 4096, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2304)); |
| 12421 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 1, { 8, 1}, 4096, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512)); |
| 12422 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 1, { 8, 1}, 4096, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512)); |
| 12423 | |
| 12424 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 1, { 8, 1}, 4096, 4, true, false, 8.0f, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512)); |
| 12425 | |
| 12426 | // flash attention over a list of K/V rows per slice (unified KV cache) |
| 12427 | for (int64_t nb : {1, 3, 8, 64, 512}) { |
| 12428 | for (int64_t n_slices : {1, 4}) { |
| 12429 | test_cases.emplace_back(new test_flash_attn_ext_kv_rows(256, 2, 8, 4096, 1024, nb, n_slices)); |
| 12430 | test_cases.emplace_back(new test_flash_attn_ext_kv_rows(128, 8, 4, 4096, 512, nb, n_slices)); |
| 12431 | test_cases.emplace_back(new test_flash_attn_ext_kv_rows(128, 4, 1, 1000, 256, nb, n_slices)); |
| 12432 | test_cases.emplace_back(new test_flash_attn_ext_kv_rows( 64, 4, 2, 3000, 768, nb, n_slices, GGML_TYPE_Q8_0)); |
| 12433 | test_cases.emplace_back(new test_flash_attn_ext_kv_rows(512, 2, 8, 4096, 1024, nb, n_slices)); |
| 12434 | test_cases.emplace_back(new test_flash_attn_ext_kv_rows(256, 8, 2, 4096, 1280, nb, n_slices)); |
| 12435 | } |
| 12436 | } |
| 12437 | |
| 12438 | // sparse mask with large batch size |
| 12439 | test_cases.emplace_back(new test_flash_attn_ext(512, 512, 1, { 8, 1}, 4096, 64, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512)); |
| 12440 | test_cases.emplace_back(new test_flash_attn_ext(512, 512, 1, { 8, 1}, 4096, 64, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048)); |
| 12441 | test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {16, 1}, 4096, 64, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true, 512)); |
| 12442 | test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {16, 1}, 4096, 64, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true, 2048)); |
| 12443 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 1, { 8, 1}, 4096, 64, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512)); |
| 12444 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 1, { 8, 1}, 4096, 64, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512)); |
| 12445 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 1, { 8, 1}, 4096, 64, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048)); |
| 12446 | |
| 12447 | // sparse attn (qwen4 shape - gqa 12) |
| 12448 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, 4096, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512)); |
| 12449 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, 8192, 64, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512)); |
| 12450 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 1, {12, 2}, 8192, 67, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512)); |
| 12451 | |
| 12452 | // sparse mask + quantized cache |
| 12453 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 1, { 8, 1}, 4096, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 1, 2, 3}, true, false, 512)); |
| 12454 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 1, { 8, 1}, 4096, 64, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 1, 2, 3}, true, false, 512)); |
| 12455 | |
| 12456 | // Qwen QSA: 256/256, gqa 12, budget 2048. |
| 12457 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, 8192, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048)); |
| 12458 | // quantized cache, deep enough to take the sparse path |
| 12459 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, 32768, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 1, 2, 3}, true, false, 2048)); |
| 12460 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, 32768, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q4_0, GGML_TYPE_Q4_0, {0, 1, 2, 3}, true, false, 2048)); |
| 12461 | // single head with quantized K (MMQ on Vulkan) |
| 12462 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 1, { 1, 1}, 8192, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 1, 2, 3}, true, false, 512)); |
| 12463 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 1, { 1, 1}, 8192, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q4_1, GGML_TYPE_Q4_1, {0, 1, 2, 3}, true, false, 512)); |
| 12464 | // KV not a multiple of the compaction workgroup size. |
| 12465 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 1, { 8, 1}, 5003, 4, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512)); |
| 12466 | |
| 12467 | // Sparse gather: native block sizes, padded slots, and head/batch strides. |
| 12468 | for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_Q4_0, GGML_TYPE_Q4_1, GGML_TYPE_Q5_0, GGML_TYPE_Q5_1, GGML_TYPE_Q8_0, GGML_TYPE_IQ4_NL}) { |
| 12469 | test_cases.emplace_back(new test_flash_attn_ext(128, 96, 2, {8, 2}, 5003, 3, true, false, 0, 0, GGML_PREC_F32, type, type, {0, 1, 2, 3}, true, false, 257)); |
| 12470 | } |
| 12471 | test_cases.emplace_back(new test_flash_attn_ext(128, 96, 2, {8, 2}, 5003, 3, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_F16, {0, 2, 1, 3}, true, false, 257)); |
| 12472 | test_cases.emplace_back(new test_flash_attn_ext(128, 96, 2, {8, 2}, 5003, 3, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_Q4_0, {0, 2, 1, 3}, true, false, 257)); |
| 12473 | test_cases.emplace_back(new test_flash_attn_ext(128, 96, 2, {8, 2}, 5003, 3, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_F32, {0, 1, 2, 3}, true, false, 257)); |
| 12474 | test_cases.emplace_back(new test_flash_attn_ext(128, 96, 2, {8, 2}, 5003, 3, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F32, GGML_TYPE_Q8_0, {0, 1, 2, 3}, true, false, 257)); |
| 12475 | |
| 12476 | // more V-is-sub-view-of-K cases: other head shapes, and full views with equal head sizes |
| 12477 | test_cases.emplace_back(new test_flash_attn_ext(320, 256, 1, {32, 1}, 512, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true)); |
| 12478 | test_cases.emplace_back(new test_flash_attn_ext(192, 128, 4, {8, 1}, 512, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true)); |
| 12479 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 8, {4, 1}, 512, 8, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true)); |
| 12480 | test_cases.emplace_back(new test_flash_attn_ext(64, 64, 4, {1, 1}, 512, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 1, 2, 3}, true, true)); |
| 12481 | |
| 12482 | // large-KV F16 cases (Qwen3.6-27B geometry and a llama-class control): the upstream matrix |
| 12483 | // stops at kv=1024, blind to long-context FA bugs (e.g. the oneDNN SDPA ordering race on BMG). |
| 12484 | for (int64_t kv : { 4096, 16384 }) { |
| 12485 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, kv, 512, true, false, 0, 0, |
| 12486 | GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12487 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 8, {4, 1}, kv, 512, true, false, 0, 0, |
| 12488 | GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12489 | } |
| 12490 | |
| 12491 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 512, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12492 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 4096, 64, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12493 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 4096, 16, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12494 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {2, 1}, 4096, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12495 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {4, 1}, 4096, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12496 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, 4096, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12497 | |
| 12498 | // dense-allocated (non-view) quant K/V at batch >= 64, in cache and native layouts |
| 12499 | test_cases.emplace_back(new test_flash_attn_ext(64, 64, 4, {1, 1}, 512, 75, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 2, 1, 3}, false)); |
| 12500 | test_cases.emplace_back(new test_flash_attn_ext(64, 64, 4, {4, 1}, 512, 75, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 2, 1, 3}, false)); |
| 12501 | test_cases.emplace_back(new test_flash_attn_ext(64, 64, 4, {1, 1}, 1024, 75, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 2, 1, 3}, false)); |
| 12502 | test_cases.emplace_back(new test_flash_attn_ext(64, 64, 4, {1, 1}, 512, 75, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 1, 2, 3}, false)); |
| 12503 | |
| 12504 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 4, {8, 1}, 1024, 320, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 2, 1, 3}, false)); |
| 12505 | test_cases.emplace_back(new test_flash_attn_ext(96, 96, 8, {4, 1}, 512, 256, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 2, 1, 3}, false)); |
| 12506 | test_cases.emplace_back(new test_flash_attn_ext(64, 64, 4, {8, 1}, 512, 259, true, false, 8.0f, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 2, 1, 3}, false)); |
| 12507 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 4, {8, 1}, 512, 288, true, false, 0, 30.0f, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 2, 1, 3}, false)); |
| 12508 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 4, {8, 1}, 1024, 320, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_BF16, GGML_TYPE_BF16, {0, 2, 1, 3}, false)); |
| 12509 | test_cases.emplace_back(new test_flash_attn_ext(64, 64, 4, {8, 1}, 512, 259, true, false, 8.0f, 0, GGML_PREC_F32, GGML_TYPE_BF16, GGML_TYPE_BF16, {0, 2, 1, 3}, false)); |
| 12510 | |
| 12511 | // FLASH_ATTN_EXT MMA: non-pow2 head size and MLA K/V view. |
| 12512 | test_cases.emplace_back(new test_flash_attn_ext(192, 128, 8, {8, 1}, 4096, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12513 | test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {20, 1}, 512, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true)); |
| 12514 | |
| 12515 | // FLASH_ATTN_EXT MMA, swizzled K/V tiles, power-of-two stride: nbatch_K2 = 32, 64, 128, 256. |
| 12516 | test_cases.emplace_back(new test_flash_attn_ext( 64, 64, 8, {8, 1}, 4096, 4, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12517 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 8, {4, 1}, 4096, 8, true, true, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12518 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {2, 1}, 1024, 32, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12519 | test_cases.emplace_back(new test_flash_attn_ext(512, 512, 4, {2, 1}, 1024, 4, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12520 | |
| 12521 | // BF16 K/V for the other head sizes of the Metal tensor API kernel |
| 12522 | test_cases.emplace_back(new test_flash_attn_ext(128, 128, 4, {12, 1}, 512, 75, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_BF16, GGML_TYPE_BF16)); |
| 12523 | test_cases.emplace_back(new test_flash_attn_ext(192, 128, 4, { 8, 1}, 512, 75, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_BF16, GGML_TYPE_BF16)); |
| 12524 | test_cases.emplace_back(new test_flash_attn_ext(512, 512, 4, { 4, 1}, 512, 75, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_BF16, GGML_TYPE_BF16)); |
| 12525 | test_cases.emplace_back(new test_flash_attn_ext(576, 512, 4, { 4, 1}, 512, 75, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_BF16, GGML_TYPE_BF16, {0, 1, 2, 3}, true, true)); |
| 12526 | |
| 12527 | // FLASH_ATTN_EXT: large logits |
| 12528 | test_cases.emplace_back(new test_flash_attn_ext_large_logits( 64, 64, 16, {4, 1}, 1024, 75, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12529 | test_cases.emplace_back(new test_flash_attn_ext_large_logits(128, 128, 8, {4, 1}, 1024, 75, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12530 | test_cases.emplace_back(new test_flash_attn_ext_large_logits(256, 256, 4, {4, 1}, 1024, 75, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12531 | test_cases.emplace_back(new test_flash_attn_ext_large_logits(256, 256, 4, {4, 1}, 1024, 75, true, false, 0, 10.0f, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 12532 | test_cases.emplace_back(new test_flash_attn_ext_large_logits(256, 256, 4, {4, 1}, 1024, 75, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_BF16, GGML_TYPE_BF16)); |
| 12533 | |
| 12534 | test_cases.emplace_back(new test_cross_entropy_loss (GGML_TYPE_F32, { 10, 5, 4, 3})); |
| 12535 | test_cases.emplace_back(new test_cross_entropy_loss (GGML_TYPE_F32, {30000, 1, 1, 1})); |
| 12536 | test_cases.emplace_back(new test_cross_entropy_loss_back(GGML_TYPE_F32, { 10, 5, 4, 3})); |
| 12537 | test_cases.emplace_back(new test_cross_entropy_loss_back(GGML_TYPE_F32, {30000, 1, 1, 1})); |
| 12538 | |
| 12539 | test_cases.emplace_back(new test_opt_step_adamw(GGML_TYPE_F32, {10, 5, 4, 3})); |
| 12540 | test_cases.emplace_back(new test_opt_step_sgd(GGML_TYPE_F32, {10, 5, 4, 3})); |
| 12541 | |
| 12542 | for (ggml_type type : base_types) { |
| 12543 | for (bool with_gate : {false, true}) { |
| 12544 | for (bool use_id : {false, true}) { |
| 12545 | for (bool b : {false, true}) { |
| 12546 | if (!use_id && b) { |
| 12547 | continue; |
| 12548 | } |
| 12549 | for (bool with_bias : {false, true}) { |
| 12550 | if (!with_gate && !with_bias) { |
| 12551 | continue; |
| 12552 | } |
| 12553 | for (ggml_glu_op glu_op : {GGML_GLU_OP_SWIGLU, GGML_GLU_OP_GEGLU, GGML_GLU_OP_SWIGLU_CLAMP}) { |
| 12554 | if (!with_bias && glu_op == GGML_GLU_OP_SWIGLU_OAI) { |
| 12555 | continue; |
| 12556 | } |
| 12557 | if (!with_gate && glu_op != GGML_GLU_OP_SWIGLU) { |
| 12558 | continue; |
| 12559 | } |
| 12560 | for (bool with_lane_scale : {false, true}) { |
| 12561 | if (with_lane_scale && type != GGML_TYPE_NVFP4 && type != GGML_TYPE_F8_E4M3) { |
| 12562 | continue; |
| 12563 | } |
| 12564 | test_cases.emplace_back(new test_mul_mat_vec_fusion(type, glu_op, 1, 32, 256, |
| 12565 | use_id, 16, 8, b, with_bias, with_gate, with_lane_scale)); |
| 12566 | test_cases.emplace_back(new test_mul_mat_vec_fusion(type, glu_op, 1, 32, 256, |
| 12567 | use_id, 16, 8, b, with_bias, with_gate, with_lane_scale, {1, 1})); |
| 12568 | // multi-token batches (spec decoding) |
| 12569 | for (int64_t m_batch : { 2, 4, 8 }) { |
| 12570 | test_cases.emplace_back(new test_mul_mat_vec_fusion(type, glu_op, m_batch, 32, 256, |
| 12571 | use_id, 16, 8, b, with_bias, with_gate, with_lane_scale, {1, 1})); |
| 12572 | } |
| 12573 | // the lane scale does a GET_ROWS on the ids, which does not accept -1 |
| 12574 | if (use_id && !with_lane_scale) { |
| 12575 | test_cases.emplace_back(new test_mul_mat_vec_fusion(type, glu_op, 8, 32, 256, |
| 12576 | use_id, 16, 8, b, with_bias, with_gate, with_lane_scale, {1, 1}, true)); |
| 12577 | } |
| 12578 | } |
| 12579 | } |
| 12580 | } |
| 12581 | } |
| 12582 | } |
| 12583 | } |
| 12584 | } |
| 12585 | |
| 12586 | for (bool b : {false, true}) { |
| 12587 | test_cases.emplace_back(new test_mul_mat_vec_fusion(GGML_TYPE_IQ2_S, GGML_GLU_OP_SWIGLU_CLAMP, 1, 32, 256, |
| 12588 | true, 16, 8, b, false, true, false)); |
| 12589 | } |
| 12590 | |
| 12591 | // Gate and up on the SYCL XMX kernels: rows long enough for 2D block loads, a last tile of 8 rows, and |
| 12592 | // columns on both sides of the 8 and 16 column tiles |
| 12593 | for (ggml_type type : {GGML_TYPE_Q2_K, GGML_TYPE_Q3_K, GGML_TYPE_Q4_K, GGML_TYPE_Q5_K, GGML_TYPE_Q6_K, |
| 12594 | GGML_TYPE_Q8_0}) { |
| 12595 | for (ggml_glu_op glu_op : {GGML_GLU_OP_SWIGLU, GGML_GLU_OP_GEGLU}) { |
| 12596 | for (int64_t m_batch : {1, 2, 3, 5, 8, 9, 16, 17}) { |
| 12597 | test_cases.emplace_back(new test_mul_mat_vec_fusion(type, glu_op, m_batch, 1000, 1024, |
| 12598 | false, 16, 8, false, false, true, false, { 1, 1 })); |
| 12599 | } |
| 12600 | } |
| 12601 | } |
| 12602 | |
| 12603 | for (int64_t k : {96, 896, 1024, 2880}) { |
| 12604 | for (ggml_glu_op glu_op : {GGML_GLU_OP_SWIGLU, GGML_GLU_OP_GEGLU}) { |
| 12605 | test_cases.emplace_back(new test_mul_mat_vec_fusion(GGML_TYPE_Q8_0, glu_op, 1, 32, k, |
| 12606 | false, 16, 8, false, false, true, false, {1, 1})); |
| 12607 | } |
| 12608 | } |
| 12609 | |
| 12610 | // Fused row-pair coverage: minimum rows, an even pair, and an odd tail. |
| 12611 | // TODO: the max_nmse_err() for these cases is not estimated correctly causing sporadic false failures. |
| 12612 | //for (ggml_glu_op glu_op : { GGML_GLU_OP_SWIGLU, GGML_GLU_OP_GEGLU }) { |
| 12613 | // for (int64_t m_batch : { 2, 3, 4 }) { |
| 12614 | // for (int64_t rows : { 1, 2, 3 }) { |
| 12615 | // test_cases.emplace_back(new test_mul_mat_vec_fusion(GGML_TYPE_Q4_K, glu_op, m_batch, rows, 256, |
| 12616 | // false, 16, 8, false, false, true, false, { 1, 1 })); |
| 12617 | // } |
| 12618 | // } |
| 12619 | //} |
| 12620 | |
| 12621 | // Both sides of the same row-count boundary as above, on the fused path. |
| 12622 | for (int64_t rows : {6271, 6272, 6273}) { |
| 12623 | test_cases.emplace_back(new test_mul_mat_vec_fusion(GGML_TYPE_Q4_K, GGML_GLU_OP_SWIGLU, 2, rows, 256, |
| 12624 | false, 16, 8, false, false, true, false, { 1, 1 })); |
| 12625 | } |
| 12626 | |
| 12627 | // Fused gate/up + GLU on the MMQ path, only used if its tiling is efficient: ~1280 tiles per case, one case per type and GLU op. |
| 12628 | test_cases.emplace_back(new test_mul_mat_vec_fusion(GGML_TYPE_Q4_K, GGML_GLU_OP_SWIGLU, 17, 81920, 256, false, 1, 1, false, false, true, false, { 1, 1 })); |
| 12629 | test_cases.emplace_back(new test_mul_mat_vec_fusion(GGML_TYPE_Q5_K, GGML_GLU_OP_GEGLU, 150, 40960, 256, false, 1, 1, false, false, true, false, { 1, 1 })); |
| 12630 | test_cases.emplace_back(new test_mul_mat_vec_fusion(GGML_TYPE_Q6_K, GGML_GLU_OP_SWIGLU_OAI, 150, 40960, 256, false, 1, 1, false, false, true, false, { 1, 1 })); |
| 12631 | test_cases.emplace_back(new test_mul_mat_vec_fusion(GGML_TYPE_Q8_0, GGML_GLU_OP_SWIGLU_CLAMP, 150, 6912, 256, false, 1, 1, false, false, true, false, { 2, 3 })); |
| 12632 | test_cases.emplace_back(new test_mul_mat_vec_fusion(GGML_TYPE_IQ4_XS, GGML_GLU_OP_SWIGLU, 150, 40960, 512, false, 1, 1, false, false, true, false, { 1, 1 })); |
| 12633 | // The same with experts (~1280 tiles each): 17 tokens with broadcast input (one MMA tile per warp), 2048 tokens without (two, several column tiles per expert). |
| 12634 | test_cases.emplace_back(new test_mul_mat_vec_fusion(GGML_TYPE_Q4_K, GGML_GLU_OP_SWIGLU, 17, 5120, 256, true, 16, 8, true)); |
| 12635 | test_cases.emplace_back(new test_mul_mat_vec_fusion(GGML_TYPE_Q8_0, GGML_GLU_OP_SWIGLU, 2048, 128, 256, true, 40, 8, false)); |
| 12636 | |
| 12637 | for (auto gate : {GATING_FUNC_SOFTMAX, GATING_FUNC_SIGMOID, GATING_FUNC_SOFTMAX_WEIGHT, GATING_FUNC_SQRT_SOFTPLUS}) { |
| 12638 | for (bool with_norm : {false, true}) { |
| 12639 | for (bool bias_probs : {false, true}) { |
| 12640 | for (float scale_w : {0.0f, 2.0f}) { |
| 12641 | test_cases.emplace_back(new test_topk_moe({8, 22, 1, 1}, 4, with_norm, bias_probs, gate, scale_w)); |
| 12642 | test_cases.emplace_back(new test_topk_moe({31, 22, 1, 1}, 8, with_norm, bias_probs, gate, scale_w)); |
| 12643 | test_cases.emplace_back(new test_topk_moe({32, 22, 1, 1}, 8, with_norm, bias_probs, gate, scale_w)); |
| 12644 | test_cases.emplace_back(new test_topk_moe({40, 22, 1, 1}, 8, with_norm, bias_probs, gate, scale_w)); |
| 12645 | test_cases.emplace_back(new test_topk_moe({71, 22, 1, 1}, 8, with_norm, bias_probs, gate, scale_w)); |
| 12646 | test_cases.emplace_back(new test_topk_moe({128, 1, 1, 1}, 128, with_norm, bias_probs, gate, scale_w)); |
| 12647 | test_cases.emplace_back(new test_topk_moe({129, 1, 1, 1}, 128, with_norm, bias_probs, gate, scale_w)); |
| 12648 | test_cases.emplace_back(new test_topk_moe({160, 4, 1, 1}, 160, with_norm, bias_probs, gate, scale_w)); |
| 12649 | test_cases.emplace_back(new test_topk_moe({256, 22, 1, 1}, 6, with_norm, bias_probs, gate, scale_w)); // Used by DeepSeek-V4 |
| 12650 | test_cases.emplace_back(new test_topk_moe({288, 22, 1, 1}, 8, with_norm, bias_probs, gate, scale_w)); // Used by StepFun 3.7 |
| 12651 | test_cases.emplace_back(new test_topk_moe({384, 1, 1, 1}, 6, with_norm, bias_probs, gate, scale_w)); // Used by DeepSeek-V4.1 |
| 12652 | test_cases.emplace_back(new test_topk_moe({384, 8, 1, 1}, 6, with_norm, bias_probs, gate, scale_w)); |
| 12653 | test_cases.emplace_back(new test_topk_moe({384, 22, 1, 1}, 6, with_norm, bias_probs, gate, scale_w)); |
| 12654 | // rows at and just past the limit where one block still covers all rows |
| 12655 | test_cases.emplace_back(new test_topk_moe({32, 8, 1, 1}, 4, with_norm, bias_probs, gate, scale_w)); |
| 12656 | test_cases.emplace_back(new test_topk_moe({32, 8, 1, 1}, 8, with_norm, bias_probs, gate, scale_w)); |
| 12657 | test_cases.emplace_back(new test_topk_moe({32, 9, 1, 1}, 8, with_norm, bias_probs, gate, scale_w)); |
| 12658 | |
| 12659 | // grouped experts |
| 12660 | test_cases.emplace_back(new test_topk_moe({128, 1, 1, 1}, 8, with_norm, bias_probs, gate, scale_w, 16)); |
| 12661 | test_cases.emplace_back(new test_topk_moe({256, 22, 1, 1}, 6, with_norm, bias_probs, gate, scale_w, 32)); |
| 12662 | test_cases.emplace_back(new test_topk_moe({256, 7, 1, 1}, 8, with_norm, bias_probs, gate, scale_w, 8)); // DeepSeek V3 |
| 12663 | test_cases.emplace_back(new test_topk_moe({128, 5, 1, 1}, 6, with_norm, bias_probs, gate, scale_w, 8)); |
| 12664 | test_cases.emplace_back(new test_topk_moe({512, 3, 1, 1}, 8, with_norm, bias_probs, gate, scale_w, 4)); |
| 12665 | } |
| 12666 | } |
| 12667 | } |
| 12668 | } |
| 12669 | |
| 12670 | // Cover the supported boundaries, common k = 8 shapes, interleaved views and adds, and k = 16 fallback. |
| 12671 | test_cases.emplace_back(new test_moe_reduce(63, 2, 17)); |
| 12672 | test_cases.emplace_back(new test_moe_reduce(2048, 8, 128)); |
| 12673 | test_cases.emplace_back(new test_moe_reduce(2048, 8, 128, false, true)); |
| 12674 | test_cases.emplace_back(new test_moe_reduce(63, 12, 33, true, true, true)); |
| 12675 | test_cases.emplace_back(new test_moe_reduce(2048, 15, 40, false, true)); |
| 12676 | test_cases.emplace_back(new test_moe_reduce(2048, 16, 32, false, true)); |
| 12677 | |
| 12678 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 32, 128, 1, 1)); |
| 12679 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 32, 16, 1, 1)); |
| 12680 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 32, 16, 1, 1, 1, true, true)); |
| 12681 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 32, 16, 1, 1, 1, false, true)); |
| 12682 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 16, 64, 1, 2)); |
| 12683 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 4, 1)); |
| 12684 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 4, 2)); |
| 12685 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 8, 32, 4, 2, 2)); |
| 12686 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 4, 2, 1, true)); |
| 12687 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 4, 1, 1, true)); |
| 12688 | // KDA (vector gate) |
| 12689 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 1, 1, 1, false, true)); |
| 12690 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 1, 2, 1, false, true)); |
| 12691 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 16, 1, 2, 1, false, true)); |
| 12692 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 32, 4, 1, 1, false, true)); |
| 12693 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 4, 2, 1, false, true)); |
| 12694 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 8, 32, 4, 2, 2, false, true)); |
| 12695 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 4, 2, 1, true, true)); |
| 12696 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 16, 4, 2, 1, true, true)); |
| 12697 | // chunked path: multi-chunk and non-multiple-of-chunk-size (chunk_size=64 GDN, 16 KDA) |
| 12698 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 64, 1)); |
| 12699 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 127, 1)); |
| 12700 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 256, 1)); |
| 12701 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 65, 1)); |
| 12702 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 100, 1)); |
| 12703 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 200, 1)); |
| 12704 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 127, 2)); |
| 12705 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 64, 1, 1, false, true)); |
| 12706 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 33, 1, 1, false, true)); |
| 12707 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 100, 1, 1, false, true)); |
| 12708 | |
| 12709 | // MMA-eligible GDN shapes: whole heads, value slices, tails, and the HIP prefill threshold. |
| 12710 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 16, 128, 65, 1, 1)); |
| 12711 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 16, 128, 65, 3, 4)); |
| 12712 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 16, 128, 64, 1, 4)); |
| 12713 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 16, 128, 257, 1, 3)); |
| 12714 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 16, 128, 512, 1, 2)); |
| 12715 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 16, 128, 513, 1, 3)); |
| 12716 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 16, 128, 513, 2, 2, true)); |
| 12717 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 16, 128, 2048, 1, 2)); |
| 12718 | // Keep the WebGPU dispatch below 65535 workgroups (32 per token for this shape). |
| 12719 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 16, 128, 2047, 2, 3)); |
| 12720 | |
| 12721 | // K > 1: output keeps the last min(n_tokens, K) per-token snapshots, ordered most-recent-first |
| 12722 | // (slot 0 = final state, slot s = state s tokens back). |
| 12723 | // exact-match cases (K == n_seq_tokens): |
| 12724 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 16, 2, 1, 1, false, false, /*K=*/2)); |
| 12725 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 32, 4, 1, 1, false, false, /*K=*/4)); |
| 12726 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 4, 2, 1, false, false, /*K=*/4)); |
| 12727 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 8, 128, 4, 1, 1, false, false, /*K=*/4)); |
| 12728 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 64, 4, 2, 1, false, true, /*K=*/4)); |
| 12729 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 8, 32, 4, 2, 2, false, true, /*K=*/4)); |
| 12730 | |
| 12731 | // gdn + cache cpy fusion (K > 1) |
| 12732 | test_cases.emplace_back(new test_gated_delta_net_cache_fusion(GGML_TYPE_F32, 4, 32, 2, 1, 2)); |
| 12733 | test_cases.emplace_back(new test_gated_delta_net_cache_fusion(GGML_TYPE_F32, 4, 64, 4, 1, 2)); |
| 12734 | test_cases.emplace_back(new test_gated_delta_net_cache_fusion(GGML_TYPE_F32, 4, 32, 4, 1, 4)); |
| 12735 | test_cases.emplace_back(new test_gated_delta_net_cache_fusion(GGML_TYPE_F32, 8, 32, 4, 2, 4)); |
| 12736 | test_cases.emplace_back(new test_gated_delta_net_cache_fusion(GGML_TYPE_F32, 4, 32, 8, 1, 4)); |
| 12737 | |
| 12738 | #if 0 |
| 12739 | // these tests are disabled to save execution time, sbut they can be handy for debugging |
| 12740 | test_cases.emplace_back(new test_llama(2, true)); |
| 12741 | test_cases.emplace_back(new test_llama(1)); |
| 12742 | test_cases.emplace_back(new test_llama(2)); |
| 12743 | test_cases.emplace_back(new test_falcon(1)); |
| 12744 | test_cases.emplace_back(new test_falcon(2)); |
| 12745 | #endif |
| 12746 | |
| 12747 | // lightning_indexer |
| 12748 | for (int kv : { 256 }) { |
| 12749 | for (int bs : { 1, 512 }) { |
| 12750 | for (int nh : { 4, 32, 64 }) { |
| 12751 | for (auto [ns, nm] : { std::pair{1, 1}, std::pair{4, 4}, std::pair{4, 1} }) { |
| 12752 | for (ggml_type type_K : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_Q8_0, GGML_TYPE_Q5_1, GGML_TYPE_Q5_0, GGML_TYPE_Q4_1, GGML_TYPE_Q4_0, GGML_TYPE_IQ4_NL}) { |
| 12753 | test_cases.emplace_back(new test_lightning_indexer(128, nh, kv, bs, ns, nm, type_K)); |
| 12754 | } |
| 12755 | } |
| 12756 | } |
| 12757 | } |
| 12758 | } |
| 12759 | |
| 12760 | for (int kv : { 1, 7, 8, 63, 64, 65 }) { |
| 12761 | for (ggml_type type_K : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_Q8_0, GGML_TYPE_Q5_1, GGML_TYPE_Q5_0, GGML_TYPE_Q4_1, GGML_TYPE_Q4_0}) { |
| 12762 | test_cases.emplace_back(new test_lightning_indexer(128, 64, kv, 32, 4, 1, type_K)); |
| 12763 | } |
| 12764 | } |
| 12765 | |
| 12766 | return test_cases; |
| 12767 | } |
| 12768 | #ifdef _MSC_VER |
| 12769 | #pragma optimize("", on) |
| 12770 | #endif |
| 12771 | |
| 12772 | // Test cases for performance evaluation: should be representative of real-world use cases |
| 12773 | static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() { |
| 12774 | std::vector<std::unique_ptr<test_case>> test_cases; |
| 12775 | |
| 12776 | for (float n : {128.0f, 4096.0f, 32768.0f, 1048576.0f}) { |
| 12777 | test_cases.emplace_back(new test_arange(GGML_TYPE_F32, 0.0f, n, 1.0f)); |
| 12778 | } |
| 12779 | |
| 12780 | // delta-net conv input: conv state followed by the transposed qkv of the ubatch |
| 12781 | test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {3, 8192, 1, 1}, 512, 0, 16)); |
| 12782 | |
| 12783 | // DSV4_HC_PRE: n_tokens 1 is decode, 128 and 512 are prompt batches |
| 12784 | for (int64_t n_tokens : {1, 128, 512}) { |
| 12785 | test_cases.emplace_back(new test_dsv4_hc_pre(4096, 4, n_tokens)); // DeepSeek-V4-Flash |
| 12786 | test_cases.emplace_back(new test_dsv4_hc_pre(4096, 4, n_tokens, true)); // synthetic, no gated model uses this width |
| 12787 | test_cases.emplace_back(new test_dsv4_hc_pre(2560, 4, n_tokens, true)); // Qwen4-Exp (Qwen3.8-Flash-Next) |
| 12788 | } |
| 12789 | |
| 12790 | // DSV4_HC_POST: n_tokens 1 is decode, 128 and 512 are prompt batches |
| 12791 | for (int64_t n_tokens : {1, 128, 512}) { |
| 12792 | test_cases.emplace_back(new test_dsv4_hc_post(4096, n_tokens)); // DeepSeek-V4-Flash |
| 12793 | test_cases.emplace_back(new test_dsv4_hc_post(7168, n_tokens)); // DeepSeek-V4-Pro |
| 12794 | test_cases.emplace_back(new test_dsv4_hc_post(2560, n_tokens, true, true)); // Qwen4-Exp, with the post gate chain |
| 12795 | test_cases.emplace_back(new test_dsv4_hc_post(2560, n_tokens, true)); // synthetic, Qwen4-Exp kernel without the gate chain |
| 12796 | } |
| 12797 | |
| 12798 | // Qwen3.8-Flash-Next pooled indexer prefill. |
| 12799 | test_cases.emplace_back(new test_lightning_indexer(128, 4, 1088, 1024, 1, 1, GGML_TYPE_F32)); |
| 12800 | test_cases.emplace_back(new test_lightning_indexer(128, 4, 1088, 1024, 1, 1, GGML_TYPE_Q4_0)); |
| 12801 | // SWIGLU at a 27B-class FFN width, fused [gate|up] vs split operands |
| 12802 | // note: same bytes either way, so a backend that indexes them differently shows it here |
| 12803 | for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_F32, GGML_TYPE_BF16}) { |
| 12804 | for (int64_t n_tokens : {512, 2048}) { |
| 12805 | test_cases.emplace_back(new test_glu(GGML_GLU_OP_SWIGLU, type, { 2*17408, n_tokens, 1, 1 }, 0, false)); |
| 12806 | test_cases.emplace_back(new test_glu_split(GGML_GLU_OP_SWIGLU, type, { 17408, n_tokens, 1, 1 }, 0)); |
| 12807 | } |
| 12808 | } |
| 12809 | |
| 12810 | // CONT of a 0<->2 permute at DeepSeek-V4 lightning-indexer shapes: |
| 12811 | // indexer_kq is [n_kv, n_tokens, n_head=64] and gets ggml_cont(ggml_permute(.., 2,1,0,3)). |
| 12812 | for (int64_t n_kv : { 1024, 1280, 2048, 2304 }) { |
| 12813 | test_cases.emplace_back(new test_cont( |
| 12814 | GGML_TYPE_F32, {n_kv, 64, 64, 1}, false, {2, 1, 0, 3})); |
| 12815 | } |
| 12816 | for (int64_t n_kv : { 2048, 2304 }) { |
| 12817 | test_cases.emplace_back(new test_cont( |
| 12818 | GGML_TYPE_F32, {n_kv, 512, 64, 1}, false, {2, 1, 0, 3})); |
| 12819 | } |
| 12820 | |
| 12821 | // POOL_2D max over MiniMax-M3 indexer block scores: |
| 12822 | // sc is [n_ps, n_head=4, n_tokens] and gets ggml_pool_2d(.., MAX, blk=128, 1, blk=128, 1, 0, 0). |
| 12823 | for (int64_t n_ps : { 8192, 32768 }) { |
| 12824 | for (int64_t n_tokens : { 1, 512 }) { |
| 12825 | test_cases.emplace_back(new test_pool2d( |
| 12826 | GGML_OP_POOL_MAX, GGML_TYPE_F32, {n_ps, 4, n_tokens, 1}, 128, 1, 128, 1, 0, 0)); |
| 12827 | } |
| 12828 | } |
| 12829 | |
| 12830 | // RMS_NORM and fused variants at decode and prefill shapes |
| 12831 | for (int64_t n_tokens : {1, 512}) { |
| 12832 | test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, {4096, n_tokens, 1, 1})); |
| 12833 | test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, {128, 32*n_tokens, 1, 1})); |
| 12834 | test_cases.emplace_back(new test_rms_norm_mul_rope({4096, n_tokens, 1, 1}, 1e-6f, false, false, true, GGML_ROPE_TYPE_NORMAL, true, false)); |
| 12835 | test_cases.emplace_back(new test_rms_norm_mul_rope({128, 32, n_tokens, 1}, 1e-6f, false, false, true, GGML_ROPE_TYPE_NEOX)); |
| 12836 | test_cases.emplace_back(new test_rms_norm_mul_rope({128, 8, n_tokens, 1}, 1e-6f, false, true, true, GGML_ROPE_TYPE_NEOX)); |
| 12837 | } |
| 12838 | |
| 12839 | // POOL_1D max over MoE down-projection amax shapes: |
| 12840 | // amax is [1, n_expert, n_tokens] after reducing the whole intermediate dim. |
| 12841 | for (int64_t n_ff : { 2048, 4096, 8192 }) { |
| 12842 | for (int64_t n_expert : { 8, 16 }) { |
| 12843 | for (int64_t n_tokens : { 1, 512 }) { |
| 12844 | test_cases.emplace_back(new test_pool1d( |
| 12845 | GGML_OP_POOL_MAX, GGML_TYPE_F32, {n_ff, n_expert, n_tokens, 1}, n_ff, n_ff, 0)); |
| 12846 | } |
| 12847 | } |
| 12848 | } |
| 12849 | |
| 12850 | // LEAKY_RELU at FFN activation width, for direct comparison with RELU |
| 12851 | for (int64_t n_tokens : {512, 2048}) { |
| 12852 | test_cases.emplace_back(new test_leaky_relu(GGML_TYPE_F32, { 17408, n_tokens, 1, 1 }, 0.1f)); |
| 12853 | } |
| 12854 | |
| 12855 | // Conv2d: K=CRS=NPQ=4096 matmul performance |
| 12856 | uint32_t iwh_idx = 0; |
| 12857 | uint32_t kwh_idx = 1; |
| 12858 | uint32_t Cout_idx = 2; |
| 12859 | uint32_t Cin_idx = 3; |
| 12860 | uint32_t B_idx = 4; |
| 12861 | std::vector<std::array<int, 5>> cases = { |
| 12862 | //{IWH, KWH, Cout, Cin, B} |
| 12863 | // K=CRS=NPQ=4096 conv2d matmul performance |
| 12864 | {19, 4, 4096, 256, 16}, |
| 12865 | // K=128, CRS=128, NPQ=4096 |
| 12866 | { 19, 4, 128, 8, 16}, |
| 12867 | // K=130, CRS=128, NPQ=4096 |
| 12868 | { 19, 4, 130, 8, 16}, |
| 12869 | // Edge case: K x CRS is small |
| 12870 | { 19, 2, 4, 4, 16}, |
| 12871 | // A ConvNet's first layer |
| 12872 | { 224, 3, 8, 3, 1 }, |
| 12873 | // A ConvNet's first layer with 2x2 convolution, and 1 channel |
| 12874 | { 224, 2, 8, 1, 1 }, |
| 12875 | // A ConvNet's first layer with 2x2 convolution, and 1 channel, several images in the batch |
| 12876 | { 224, 2, 8, 1, 8 }, |
| 12877 | // A middle layer of a ConvNet |
| 12878 | { 58, 3, 64, 32, 1 }, |
| 12879 | // A middle layer of a ConvNet, several images in the batch |
| 12880 | { 58, 3, 64, 32, 8 }, |
| 12881 | // A deep layer of a ConvNet, several images in the batch |
| 12882 | { 16, 3, 512, 128, 8 }, |
| 12883 | // High resolution output (large NPQ) |
| 12884 | {1536, 3, 64, 32, 1 }, |
| 12885 | }; |
| 12886 | |
| 12887 | for (auto kernel_type : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 12888 | for (auto act_case : cases) { |
| 12889 | // Direct CONV_2D |
| 12890 | test_cases.emplace_back(new test_conv_2d( |
| 12891 | { act_case[iwh_idx], act_case[iwh_idx], act_case[Cin_idx], act_case[B_idx] }, |
| 12892 | { act_case[kwh_idx], act_case[kwh_idx], act_case[Cin_idx], act_case[Cout_idx] }, |
| 12893 | kernel_type, 1, 1, 0, 0, 1, 1, false)); // bool cwhn = false |
| 12894 | test_cases.emplace_back(new test_conv_2d( |
| 12895 | { act_case[iwh_idx], act_case[iwh_idx], act_case[Cin_idx], act_case[B_idx] }, |
| 12896 | { act_case[kwh_idx], act_case[kwh_idx], act_case[Cin_idx], act_case[Cout_idx] }, |
| 12897 | kernel_type, 1, 1, 0, 0, 1, 1, true)); // bool cwhn = true |
| 12898 | } |
| 12899 | } |
| 12900 | |
| 12901 | struct conv3d_perf_case { |
| 12902 | int N, IC, ID, IH, IW, OC, KD, KH, KW, s0, s1, s2, p0, p1, p2, d0, d1, d2; |
| 12903 | }; |
| 12904 | |
| 12905 | const std::vector<conv3d_perf_case> conv3d_cases = { |
| 12906 | {1, 320, 8, 38, 26, 1280, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1}, |
| 12907 | {1, 1280, 8, 38, 26, 1280, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1}, |
| 12908 | {1, 320, 8, 76, 52, 1280, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1}, |
| 12909 | {1, 1280, 8, 76, 52, 1280, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1}, |
| 12910 | {1, 320, 8, 152, 104, 1280, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1}, |
| 12911 | // Wan VAE pads spatial borders and temporal history before conv3d; operator padding is zero. |
| 12912 | {1, 384, 3, 40, 28, 384, 3, 3, 3, 1, 1, 1, 0, 0, 0, 1, 1, 1}, |
| 12913 | {1, 192, 4, 78, 54, 192, 3, 3, 3, 1, 1, 1, 0, 0, 0, 1, 1, 1}, |
| 12914 | {1, 96, 6, 154, 106, 96, 3, 3, 3, 1, 1, 1, 0, 0, 0, 1, 1, 1}, |
| 12915 | {1, 96, 6, 306, 210, 3, 3, 3, 3, 1, 1, 1, 0, 0, 0, 1, 1, 1}, |
| 12916 | {1, 1024, 3, 40, 28, 1024, 3, 3, 3, 1, 1, 1, 0, 0, 0, 1, 1, 1}, |
| 12917 | {1, 256, 6, 154, 106, 256, 3, 3, 3, 1, 1, 1, 0, 0, 0, 1, 1, 1}, |
| 12918 | {1, 384, 3, 38, 26, 768, 3, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1}, |
| 12919 | {1, 192, 2, 76, 52, 96, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1}, |
| 12920 | #if 0 |
| 12921 | // too slow on some devices |
| 12922 | {1, 1280, 8, 152, 104, 1280, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1}, |
| 12923 | {1, 320, 4, 304, 208, 1280, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1}, |
| 12924 | {1, 640, 4, 304, 208, 1280, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1}, |
| 12925 | #endif |
| 12926 | }; |
| 12927 | |
| 12928 | for (ggml_type kernel_type : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 12929 | for (const conv3d_perf_case & c : conv3d_cases) { |
| 12930 | test_cases.emplace_back(new test_conv_3d( |
| 12931 | c.N, c.IC, c.ID, c.IH, c.IW, |
| 12932 | c.OC, c.KD, c.KH, c.KW, |
| 12933 | c.s0, c.s1, c.s2, c.p0, c.p1, c.p2, c.d0, c.d1, c.d2, |
| 12934 | kernel_type)); |
| 12935 | } |
| 12936 | } |
| 12937 | |
| 12938 | test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_F32, {4096, 1, 1, 1}, {1, 1, 1, 1})); |
| 12939 | test_cases.emplace_back(new test_bin_bcast(ggml_add, GGML_TYPE_F32, {4096, 1, 1, 1}, {1, 512, 1, 1})); |
| 12940 | |
| 12941 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_F16, {512, 3072, 1, 1})); |
| 12942 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_F32, {8192, 512, 2, 1}, {-1,-1,-1,-1}, {0, 2, 1, 3})); |
| 12943 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_F32, {3072, 512, 2, 1}, {-1,-1,-1,-1}, {0, 2, 1, 3})); |
| 12944 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_Q4_0, {8192, 512, 2, 1})); |
| 12945 | test_cases.emplace_back(new test_cpy(GGML_TYPE_Q4_0, GGML_TYPE_F32, {8192, 512, 2, 1})); |
| 12946 | |
| 12947 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_F32, {768*1024, 256, 1, 1}, {-1,-1,-1,-1}, {1, 0, 2, 3}, {0, 0, 0, 0})); |
| 12948 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F16, GGML_TYPE_F16, {768*1024, 256, 1, 1}, {-1,-1,-1,-1}, {1, 0, 2, 3}, {0, 0, 0, 0})); |
| 12949 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F16, GGML_TYPE_F16, {768, 1024, 256, 1}, {-1,-1,-1,-1}, {1, 0, 2, 3}, {0, 0, 0, 0})); |
| 12950 | test_cases.emplace_back(new test_cpy(GGML_TYPE_BF16, GGML_TYPE_BF16, {768, 1024, 256, 1}, {-1,-1,-1,-1}, {1, 0, 2, 3}, {0, 0, 0, 0})); |
| 12951 | |
| 12952 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_F32, {768*1024, 256, 1, 1}, {-1,-1,-1,-1}, {0, 0, 0, 0}, {0, 0, 0, 0}, true)); |
| 12953 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, GGML_TYPE_F32, {768, 1024, 256, 1}, {-1,-1,-1,-1}, {0, 0, 0, 0}, {0, 0, 0, 0}, true)); |
| 12954 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F16, GGML_TYPE_F16, {768*1024, 256, 1, 1}, {-1,-1,-1,-1}, {0, 0, 0, 0}, {0, 0, 0, 0}, true)); |
| 12955 | test_cases.emplace_back(new test_cpy(GGML_TYPE_F16, GGML_TYPE_F16, {768, 1024, 256, 1}, {-1,-1,-1,-1}, {0, 0, 0, 0}, {0, 0, 0, 0}, true)); |
| 12956 | test_cases.emplace_back(new test_cpy(GGML_TYPE_BF16, GGML_TYPE_BF16, {768, 1024, 256, 1}, {-1,-1,-1,-1}, {0, 0, 0, 0}, {0, 0, 0, 0}, true)); |
| 12957 | |
| 12958 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {4096, 4096, 5, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f)); |
| 12959 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {12888, 256, 5, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f)); |
| 12960 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {77, 4096, 5, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f)); |
| 12961 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {1024, 1024, 10, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f)); |
| 12962 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {77, 1024, 10, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f)); |
| 12963 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {256, 256, 20, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f)); |
| 12964 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {64, 64, 20, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f)); |
| 12965 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {77, 64, 20, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f)); |
| 12966 | |
| 12967 | test_cases.emplace_back(new test_argmax(GGML_TYPE_F32, {32, 10, 1, 1})); |
| 12968 | test_cases.emplace_back(new test_argmax(GGML_TYPE_F32, {1024, 10, 1, 1})); |
| 12969 | test_cases.emplace_back(new test_argmax(GGML_TYPE_F32, {32000, 512, 1, 1})); |
| 12970 | |
| 12971 | test_cases.emplace_back(new test_pad_reflect_1d(GGML_TYPE_F32, {512, 34, 2, 1})); |
| 12972 | test_cases.emplace_back(new test_pad_reflect_1d(GGML_TYPE_F32, {3000, 80, 1, 1})); |
| 12973 | test_cases.emplace_back(new test_pad_reflect_1d(GGML_TYPE_F32, {3000, 80, 4, 1})); |
| 12974 | test_cases.emplace_back(new test_pad_reflect_1d(GGML_TYPE_F32, {3000, 384, 1, 1})); |
| 12975 | test_cases.emplace_back(new test_pad_reflect_1d(GGML_TYPE_F32, {3000, 384, 4, 1})); |
| 12976 | |
| 12977 | // SNAKE activation fusion at BigVGAN scale (T=7680 = 24 kHz x 320 ms, C=192) |
| 12978 | test_cases.emplace_back(new test_snake_fuse(GGML_TYPE_F32, {7680, 192, 1, 1})); |
| 12979 | test_cases.emplace_back(new test_snake_fuse(GGML_TYPE_F16, {7680, 192, 1, 1})); |
| 12980 | test_cases.emplace_back(new test_snake_fuse(GGML_TYPE_BF16, {7680, 192, 1, 1})); |
| 12981 | |
| 12982 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 16416, 1, 128, {8, 1}, {4, 1}, {0, 2, 1, 3})); |
| 12983 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 128, 1, 16416, {8, 1}, {4, 1}, {0, 1, 2, 3}, 2*16416)); |
| 12984 | |
| 12985 | // FWHT tests |
| 12986 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 128, 1, 128)); |
| 12987 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 64, 1, 64)); |
| 12988 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 256, 1, 256)); |
| 12989 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 128, 32, 128)); |
| 12990 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 64, 2048, 64)); |
| 12991 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 128, 2048, 128)); |
| 12992 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 256, 2048, 256)); |
| 12993 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F32, 512, 2048, 512)); |
| 12994 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 128, 1, 128)); |
| 12995 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 64, 1, 64)); |
| 12996 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 256, 1, 256)); |
| 12997 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 128, 32, 128)); |
| 12998 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 64, 2048, 64)); |
| 12999 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 128, 2048, 128)); |
| 13000 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 256, 2048, 256)); |
| 13001 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 512, 2048, 512)); |
| 13002 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 1024, 2048, 1024)); |
| 13003 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 4096, 2048, 4096)); |
| 13004 | test_cases.emplace_back(new test_mul_mat_hadamard(GGML_TYPE_F32, GGML_TYPE_F16, 8192, 2048, 8192)); |
| 13005 | |
| 13006 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 64, 64, 4, 4 }, { 32, 64, 4, 4 })); |
| 13007 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 128, 128, 4, 2 }, { 32, 128, 4, 2 })); |
| 13008 | // qwen3next with CHUNK_SIZE 64 |
| 13009 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 64, 64, 8, 32 }, { 64, 64, 8, 32 })); |
| 13010 | // qwen3next with CHUNK_SIZE 128 |
| 13011 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 128, 128, 4, 32 }, { 128, 128, 4, 32 })); |
| 13012 | test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 256, 256, 4, 2 }, { 128, 256, 4, 2 })); |
| 13013 | |
| 13014 | test_cases.emplace_back(new test_tri(GGML_TRI_TYPE_LOWER, GGML_TYPE_F32, { 256, 256, 4, 4 })); |
| 13015 | test_cases.emplace_back(new test_tri(GGML_TRI_TYPE_UPPER_DIAG, GGML_TYPE_F32, { 1024, 1024, 8, 4 })); |
| 13016 | |
| 13017 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 128, 128, 4, 4 })); |
| 13018 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 2048, 16, 5, 4 })); |
| 13019 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, { 20000, 10, 4, 1 })); |
| 13020 | |
| 13021 | for (int bs : {1, 2, 3, 4, 5, 8, 512}) { |
| 13022 | for (ggml_type type_a : all_types) { |
| 13023 | for (ggml_type type_b : {GGML_TYPE_F32}) { |
| 13024 | test_cases.emplace_back(new test_mul_mat(type_a, type_b, 4096, bs, 14336, {1, 1}, {1, 1})); |
| 13025 | } |
| 13026 | } |
| 13027 | } |
| 13028 | |
| 13029 | // Q4_K multi-column mat-vec |
| 13030 | for (int64_t m : {4096, 6144, 6272, 14336}) { |
| 13031 | for (int bs : {1, 2, 3, 4, 8}) { |
| 13032 | test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_K, GGML_TYPE_F32, m, bs, 4096, {1, 1}, {1, 1})); |
| 13033 | } |
| 13034 | } |
| 13035 | |
| 13036 | // qwen3-30b-a3b |
| 13037 | for (int bs : {1, 4, 8, 32, 64, 128, 256, 512}) { |
| 13038 | for (ggml_type type_a : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_Q4_0, GGML_TYPE_Q8_0, GGML_TYPE_Q4_K, GGML_TYPE_Q6_K, GGML_TYPE_IQ2_XS, GGML_TYPE_IQ4_XS}) { |
| 13039 | for (ggml_type type_b : {GGML_TYPE_F32}) { |
| 13040 | test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, 128, 8, false, 768, bs, 2048)); |
| 13041 | test_cases.emplace_back(new test_mul_mat_id_fusion(type_a, type_b, 128, 8, false, 768, bs, 2048, 1)); |
| 13042 | } |
| 13043 | } |
| 13044 | } |
| 13045 | |
| 13046 | for (int bs : {1, 4, 8, 32, 64, 128, 256, 512}) { |
| 13047 | for (ggml_type type_a : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_Q4_0, GGML_TYPE_Q8_0, GGML_TYPE_Q4_K, GGML_TYPE_Q6_K, GGML_TYPE_IQ2_XS, GGML_TYPE_IQ4_XS}) { |
| 13048 | for (ggml_type type_b : {GGML_TYPE_F32}) { |
| 13049 | test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, 32, 4, false, 1792, bs, 2048)); |
| 13050 | test_cases.emplace_back(new test_mul_mat_id_fusion(type_a, type_b, 32, 4, false, 1792, bs, 2048, 1)); |
| 13051 | } |
| 13052 | } |
| 13053 | } |
| 13054 | |
| 13055 | |
| 13056 | // gpt-oss-20b |
| 13057 | for (int bs : {1, 4, 8, 512}) { |
| 13058 | for (ggml_type type_a : {GGML_TYPE_MXFP4}) { |
| 13059 | for (ggml_type type_b : {GGML_TYPE_F32}) { |
| 13060 | test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, 32, 4, false, 2880, bs, 2880)); |
| 13061 | test_cases.emplace_back(new test_mul_mat_id_fusion(type_a, type_b, 32, 4, false, 2880, bs, 2880, 1)); |
| 13062 | } |
| 13063 | } |
| 13064 | } |
| 13065 | |
| 13066 | for (int K : {3, 5}) { |
| 13067 | for (int IC : {256, 2560}) { |
| 13068 | for (int IW_IH : {32, 64, 256}) { |
| 13069 | if (IC == 2560 && IW_IH == 256) { |
| 13070 | // too big |
| 13071 | continue; |
| 13072 | } |
| 13073 | test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32, {IW_IH, IW_IH, IC, 1}, {K, K, IC, 1}, 1, 1, 1, 1, 1, 1, true)); |
| 13074 | } |
| 13075 | } |
| 13076 | } |
| 13077 | |
| 13078 | // Qwen3-VL-8B https://github.com/ggml-org/llama.cpp/issues/17012 |
| 13079 | test_cases.emplace_back(new test_flash_attn_ext(72, 72, 16, {1, 1}, 5776, 5776, false, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 13080 | |
| 13081 | // Sparse flash attention (n_kv_max hint) decode across KV depths. |
| 13082 | // Shapes: 576/512 DeepSeek MLA, 512/512 DeepSeek-V4/GLM-5.2, 256/256 gqa12 Qwen QSA. |
| 13083 | for (int64_t kv : {4096, 16384, 32768}) { |
| 13084 | test_cases.emplace_back(new test_flash_attn_ext(512, 512, 1, { 8, 1}, kv, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512)); |
| 13085 | test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {16, 1}, kv, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true, 512)); |
| 13086 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, kv, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048)); |
| 13087 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, kv, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 1, 2, 3}, true, false, 2048)); |
| 13088 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, kv, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q4_0, GGML_TYPE_Q4_0, {0, 1, 2, 3}, true, false, 2048)); |
| 13089 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, kv, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_BF16, GGML_TYPE_BF16, {0, 1, 2, 3}, true, false, 2048)); |
| 13090 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, kv, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 1, 2, 3}, true, false, 0)); |
| 13091 | } |
| 13092 | |
| 13093 | test_cases.emplace_back(new test_flash_attn_ext(64, 64, 8, {8, 1}, 7680, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 13094 | test_cases.emplace_back(new test_flash_attn_ext(64, 64, 8, {8, 1}, 7680, 4, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 13095 | test_cases.emplace_back(new test_flash_attn_ext(64, 64, 8, {8, 1}, 7680, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q4_0, GGML_TYPE_Q4_0)); |
| 13096 | test_cases.emplace_back(new test_flash_attn_ext(64, 64, 8, {8, 1}, 7680, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q4_0, GGML_TYPE_Q4_0)); |
| 13097 | test_cases.emplace_back(new test_flash_attn_ext(64, 64, 8, {8, 1}, 7680, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0)); |
| 13098 | test_cases.emplace_back(new test_flash_attn_ext(64, 64, 8, {8, 1}, 7680, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0)); |
| 13099 | |
| 13100 | // sparse decode at long context |
| 13101 | test_cases.emplace_back(new test_flash_attn_ext(512, 512, 1, { 8, 1}, 49152, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 0)); |
| 13102 | test_cases.emplace_back(new test_flash_attn_ext(512, 512, 1, { 8, 1}, 49152, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048)); |
| 13103 | // gemma-4-26b-a4b global-attn layers: head_count_kv=2, 16 query heads (gqa_ratio=8) |
| 13104 | test_cases.emplace_back(new test_flash_attn_ext(512, 512, 2, { 8, 1}, 49152, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 0)); |
| 13105 | test_cases.emplace_back(new test_flash_attn_ext(512, 512, 2, { 8, 1}, 49152, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048)); |
| 13106 | test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {16, 1}, 49152, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true, 0)); |
| 13107 | test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {16, 1}, 49152, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true, 2048)); |
| 13108 | |
| 13109 | // GLM 4.7 Flash gqa20 |
| 13110 | test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {20, 1}, 8192, 32, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true)); |
| 13111 | test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {20, 1}, 8192, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true)); |
| 13112 | |
| 13113 | // q8_0 KV cases with long context (decode and prompt) |
| 13114 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 128, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0)); |
| 13115 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 512, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0)); |
| 13116 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 1024, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0)); |
| 13117 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 2048, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0)); |
| 13118 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 4096, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0)); |
| 13119 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 10000, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0)); |
| 13120 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 20000, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0)); |
| 13121 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 10000, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 13122 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 20000, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 13123 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 10000, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0)); |
| 13124 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 20000, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0)); |
| 13125 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 10000, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 13126 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {16, 1}, 20000, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 13127 | |
| 13128 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 4096, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 13129 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 4096, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 13130 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 16384, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 13131 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 16384, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 13132 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 65536, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 13133 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 65536, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 13134 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 131072, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 13135 | test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 131072, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); |
| 13136 | |
| 13137 | for (int kv : { 4096, 8192, 16384,32768, 65536, }) { |
| 13138 | for (int hs : { 64, 128, 256, 512, 576, }) { |
| 13139 | const int hsv = hs == 576 ? 512 : hs; |
| 13140 | const bool v_view = hs == 576; |
| 13141 | for (int nr : { 1, 4, 8, }) { |
| 13142 | for (int nb : { 1, 4096, }) { |
| 13143 | test_cases.emplace_back(new test_flash_attn_ext(hs, hsv, 8, {nr, 1}, kv, nb, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, v_view)); |
| 13144 | } |
| 13145 | } |
| 13146 | } |
| 13147 | } |
| 13148 | |
| 13149 | for (int col : {8192, 16384, 32768, 65536, 131072, 262144, 524288}) { |
| 13150 | for (int rows : {1, 4, 16}){ |
| 13151 | test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {col, rows, 1, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f)); |
| 13152 | } |
| 13153 | } |
| 13154 | |
| 13155 | test_cases.emplace_back(new test_conv_2d_dw({512, 512, 256, 1}, {3, 3, 1, 256}, GGML_TYPE_F32, 1, 1, 1, false)); |
| 13156 | test_cases.emplace_back(new test_conv_2d_dw({512, 512, 256, 1}, {3, 3, 1, 256}, GGML_TYPE_F32, 1, 1, 1, true)); |
| 13157 | test_cases.emplace_back(new test_conv_2d_dw({112, 112, 32, 1}, {3, 3, 1, 32}, GGML_TYPE_F32, 1, 1, 1, false)); |
| 13158 | test_cases.emplace_back(new test_conv_2d_dw({112, 112, 32, 1}, {3, 3, 1, 32}, GGML_TYPE_F32, 1, 1, 1, true)); |
| 13159 | test_cases.emplace_back(new test_conv_2d_dw({56, 56, 128, 1}, {5, 5, 1, 128}, GGML_TYPE_F32, 2, 2, 1, false)); |
| 13160 | test_cases.emplace_back(new test_conv_2d_dw({56, 56, 128, 1}, {5, 5, 1, 128}, GGML_TYPE_F32, 2, 2, 1, true)); |
| 13161 | |
| 13162 | for (ggml_type kernel_type : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 13163 | test_cases.emplace_back(new test_conv_transpose_2d({256, 256, 256, 1}, {3, 3, 16, 256}, 1, kernel_type)); |
| 13164 | test_cases.emplace_back(new test_conv_transpose_2d({16, 16, 16, 1}, {3, 3, 8, 16}, 1, kernel_type)); |
| 13165 | test_cases.emplace_back(new test_conv_transpose_2d({10, 10, 9, 1}, {3, 3, 1, 9}, 2, kernel_type)); |
| 13166 | } |
| 13167 | |
| 13168 | // Memory bound overlap-add of the GEMM + col2im_1d transposed conv path, real vocoder stage shapes |
| 13169 | test_cases.emplace_back(new test_col2im_1d(GGML_TYPE_F32, 16, 512, 2048, 8, 0)); |
| 13170 | test_cases.emplace_back(new test_col2im_1d(GGML_TYPE_F32, 4, 128, 65536, 2, 0)); |
| 13171 | test_cases.emplace_back(new test_col2im_1d(GGML_TYPE_F16, 16, 512, 2048, 8, 0)); |
| 13172 | |
| 13173 | test_cases.emplace_back(new test_mean(GGML_TYPE_F32, {256, 256, 3, 1})); |
| 13174 | |
| 13175 | |
| 13176 | for (int n_token : {1, 512}) { |
| 13177 | test_cases.emplace_back(new test_add_id(GGML_TYPE_F32, GGML_TYPE_F32, 2880, 128, 4, n_token)); |
| 13178 | test_cases.emplace_back(new test_add_id(GGML_TYPE_F32, GGML_TYPE_F32, 2880, 32, 4, n_token)); |
| 13179 | } |
| 13180 | |
| 13181 | for (bool fw : {true, false}) { // fw == forward |
| 13182 | for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) { |
| 13183 | for (bool ff : {false, true}) { // freq_factors |
| 13184 | for (float v : { 0, 1 }) { |
| 13185 | test_cases.emplace_back(new test_rope(type, {128, 32, 512, 1}, 128, GGML_ROPE_TYPE_NORMAL, 512, 1.0f, 0.0f, 1.0f, ff, v, fw)); // llama 7B |
| 13186 | test_cases.emplace_back(new test_rope(type, {128, 64, 512, 1}, 128, GGML_ROPE_TYPE_NORMAL, 512, 1.0f, 0.0f, 1.0f, ff, v, fw)); // llama 65B |
| 13187 | test_cases.emplace_back(new test_rope(type, { 80, 32, 512, 1}, 20, GGML_ROPE_TYPE_NEOX, 512, 1.0f, 0.0f, 1.0f, ff, v, fw)); // neox (stablelm) |
| 13188 | test_cases.emplace_back(new test_rope(type, { 64, 8, 512, 1}, 64, GGML_ROPE_TYPE_NEOX, 512, 1.0f, 0.0f, 1.0f, ff, v, fw)); // neox (falcon 40B) |
| 13189 | test_cases.emplace_back(new test_rope(type, {128, 12, 512, 1}, 128, GGML_ROPE_TYPE_MROPE, 512, 1.0f, 0.0f, 1.0f, ff, v, fw)); // rope_multi,m-rope (qwen2vl 2B) |
| 13190 | test_cases.emplace_back(new test_rope(type, {128, 12, 512, 1}, 128, GGML_ROPE_TYPE_IMROPE, 512, 1.0f, 0.0f, 1.0f, ff, v, fw)); // rope_multi,imrope (qwen3vl 2B) |
| 13191 | test_cases.emplace_back(new test_rope(type, { 80, 16, 2, 1}, 80, GGML_ROPE_TYPE_VISION, 512, 1.0f, 0.0f, 1.0f, ff, v, fw)); // rope_multi,m-rope (qwen2vl ViT) |
| 13192 | } |
| 13193 | } |
| 13194 | } |
| 13195 | } |
| 13196 | |
| 13197 | // Real-model RoPE: F32 forward, packed Q, 512-token prefill. |
| 13198 | test_cases.emplace_back(new test_rope(GGML_TYPE_F32, {256, 8, 512, 1}, 64, GGML_ROPE_TYPE_IMROPE, 512, 1.0f, 0.0f, 1.0f, false, 0, true)); // qwen3.5 0.8B |
| 13199 | test_cases.emplace_back(new test_rope(GGML_TYPE_F32, {256, 16, 512, 1}, 64, GGML_ROPE_TYPE_IMROPE, 512, 1.0f, 0.0f, 1.0f, false, 0, true)); // qwen3.5 4B |
| 13200 | test_cases.emplace_back(new test_rope(GGML_TYPE_F32, {256, 8, 512, 1}, 256, GGML_ROPE_TYPE_NEOX, 512, 1.0f, 0.0f, 1.0f, false, 0, true)); // gemma4 E2B sliding |
| 13201 | test_cases.emplace_back(new test_rope(GGML_TYPE_F32, {512, 8, 512, 1}, 128, GGML_ROPE_TYPE_NEOX, 512, 1.0f, 0.0f, 1.0f, true, 0, true)); // gemma4 E4B global |
| 13202 | |
| 13203 | std::vector<std::array<int64_t, 4>> reduce_rows_cases = { |
| 13204 | { 8192, 1, 1, 1 }, |
| 13205 | { 8192, 8192, 1, 1 }, |
| 13206 | { 128, 8192, 1, 1 }, |
| 13207 | }; |
| 13208 | |
| 13209 | for (auto it: reduce_rows_cases){ |
| 13210 | test_cases.emplace_back(new test_mean(GGML_TYPE_F32, it)); |
| 13211 | test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, it)); |
| 13212 | test_cases.emplace_back(new test_sum(GGML_TYPE_F32, it)); |
| 13213 | } |
| 13214 | |
| 13215 | test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {65000, 16, 1, 1})); |
| 13216 | test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {200000, 1, 1, 1})); |
| 13217 | test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {200000, 16, 1, 1})); |
| 13218 | |
| 13219 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {2, 1, 1, 1}, 1)); |
| 13220 | // widths around the tiling threshold |
| 13221 | for (auto cols : {4096, 8192, 12288, 16384, 24576, 32768, 65536, 131072}) { |
| 13222 | for (auto nrows : {1, 16}) { |
| 13223 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {cols, nrows, 1, 1}, 16)); |
| 13224 | } |
| 13225 | } |
| 13226 | for (auto k : {1, 4, 8, 10, 16, 32, 40, 400}) { |
| 13227 | for (auto nrows : {1, 16}) { |
| 13228 | for (auto cols : {k, 1000, 65000, 200000}) { |
| 13229 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {cols, nrows, 1, 1}, k)); |
| 13230 | } |
| 13231 | } |
| 13232 | } |
| 13233 | |
| 13234 | // qwen4exp sparse-attention indexer: nrows = n_tokens/n_stream, so tg gives nrows==1. |
| 13235 | // Sweep nrows to expose how much of the device a single row leaves idle. |
| 13236 | for (auto cols : {8192, 32768, 131072}) { |
| 13237 | for (auto nrows : {1, 2, 4, 8, 16, 32}) { |
| 13238 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {cols, nrows, 1, 1}, 2048)); |
| 13239 | } |
| 13240 | } |
| 13241 | // bitonic vs radix crossover |
| 13242 | for (auto cols : {520, 1000, 2048, 3000, 4096}) { |
| 13243 | for (auto nrows : {2, 16, 32, 48, 64, 128, 256, 512}) { |
| 13244 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {cols, nrows, 1, 1}, 16)); |
| 13245 | } |
| 13246 | } |
| 13247 | // backend sampler: one row of the vocab (llama-sampler.cpp top_k) |
| 13248 | for (auto k : {20, 40}) { |
| 13249 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {151936, 1, 1, 1}, k)); |
| 13250 | } |
| 13251 | |
| 13252 | // short rows, many of them: MoE routing and group selection. The opposite corner from |
| 13253 | // the indexer, and the one where a work-group per row is the wasteful choice. |
| 13254 | for (auto cols : {2, 16, 128, 1024}) { |
| 13255 | for (auto nrows : {1024, 8192}) { |
| 13256 | for (auto k : {1, 2, 8, 16, 32}) { |
| 13257 | if (k <= cols) { |
| 13258 | test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {cols, nrows, 1, 1}, k)); |
| 13259 | } |
| 13260 | } |
| 13261 | } |
| 13262 | } |
| 13263 | |
| 13264 | for (auto nrows : {1, 4, 8, 16}) { |
| 13265 | for (auto cols : {128, 1024, 4096, 8192, 16384, 32768, 65536, 131072, 200000, 2000000}) { |
| 13266 | test_cases.emplace_back(new test_cumsum(GGML_TYPE_F32, {cols, nrows, 1, 1})); |
| 13267 | } |
| 13268 | } |
| 13269 | |
| 13270 | // Examples from granite-4.0-h-1b/ggml-model-Q8_0.gguf |
| 13271 | test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {515, 3328, 1, 1}, {4, 3328, 1, 1})); // prefill |
| 13272 | test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {937, 8192, 1, 1}, {4, 8192, 1, 1})); // prefill |
| 13273 | test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {4, 3328, 1, 1}, {4, 3328, 1, 1})); // generate |
| 13274 | test_cases.emplace_back(new test_ssm_conv_bias_silu(GGML_TYPE_F32, {515, 3328, 1, 1}, {4, 3328, 1, 1}, true)); // prefill |
| 13275 | test_cases.emplace_back(new test_ssm_conv_bias_silu(GGML_TYPE_F32, {4, 3328, 1, 1}, {4, 3328, 1, 1}, true)); // generate |
| 13276 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 64, 48, 1, 512, 1)); // prefill |
| 13277 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 64, 48, 1, 1, 1)); // generate |
| 13278 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 80, 128, 1, 512, 1)); // Nemotron-9B prefill |
| 13279 | test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 80, 128, 1, 1, 1)); // Nemotron-9B generate |
| 13280 | |
| 13281 | // acc |
| 13282 | test_cases.emplace_back(new test_acc(GGML_TYPE_F32, {256, 17, 1, 1}, {256, 16, 1, 1}, -1)); |
| 13283 | test_cases.emplace_back(new test_acc(GGML_TYPE_F32, {256, 17, 2, 3}, {256, 16, 2, 3}, -1)); |
| 13284 | test_cases.emplace_back(new test_acc(GGML_TYPE_F32, {256, 17, 2, 3}, {128, 16, 2, 3}, -1)); |
| 13285 | test_cases.emplace_back(new test_acc(GGML_TYPE_F32, {256, 17, 2, 3}, {256, 16, 2, 3}, 1)); |
| 13286 | test_cases.emplace_back(new test_acc(GGML_TYPE_F32, {256, 17, 2, 3}, {128, 16, 2, 3}, 2)); |
| 13287 | test_cases.emplace_back(new test_acc(GGML_TYPE_F32, {256, 17, 2, 3}, {64, 16, 2, 3}, 3)); |
| 13288 | |
| 13289 | // GATED_DELTA_NET: realistic model configurations |
| 13290 | // TG: n_seq_tokens=1 (autoregressive) |
| 13291 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 32, 128, 1, 1)); // Qwen3.5-like: 32 heads, d=128 |
| 13292 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 16, 64, 1, 1)); // smaller model |
| 13293 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 32, 128, 1, 1, 1, false, true)); // KDA |
| 13294 | // PP: n_seq_tokens=64,256 (prompt processing) |
| 13295 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 32, 128, 64, 1)); // PP-64 |
| 13296 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 32, 128, 256, 1)); // PP-256 |
| 13297 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 32, 128, 512, 1)); // PP-512 |
| 13298 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 32, 128, 1024, 1)); // PP-1024 |
| 13299 | // Small model configs (fewer heads = less GPU occupancy for autoregressive) |
| 13300 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 128, 64, 1)); // 4h PP-64 |
| 13301 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 128, 256, 1)); // 4h PP-256 |
| 13302 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 128, 512, 1)); // 4h PP-512 |
| 13303 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 4, 128, 1024, 1)); // 4h PP-1024 |
| 13304 | test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 32, 128, 64, 1, 1, false, true)); // KDA PP-64 |
| 13305 | |
| 13306 | // lightning_indexer |
| 13307 | for (int kv : { 256, 4096, 65536 }) { |
| 13308 | for (int bs : { 1, 512, 2048 }) { |
| 13309 | for (int nh : { 4, 32, 64 }) { |
| 13310 | for (int ns : { 1, 4 }) { |
| 13311 | for (ggml_type type_K : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_Q8_0, GGML_TYPE_Q5_1, GGML_TYPE_Q5_0, GGML_TYPE_Q4_1, GGML_TYPE_Q4_0, GGML_TYPE_IQ4_NL}) { |
| 13312 | test_cases.emplace_back(new test_lightning_indexer(128, nh, kv, bs, ns, ns, type_K)); |
| 13313 | } |
| 13314 | } |
| 13315 | } |
| 13316 | } |
| 13317 | } |
| 13318 | |
| 13319 | // top_k_mask, Flash-Next QSA shapes: 512 pools of 4 + a 3-cell tail, and DSA shapes: 2048 cells |
| 13320 | for (int64_t n_kv : { 18432, 131072 }) { |
| 13321 | for (int64_t nb : { 1, 2048 }) { |
| 13322 | test_cases.emplace_back(new test_top_k_mask(GGML_TYPE_F16, n_kv, 4, n_kv/4, 512, 3, true, nb, 1, 1, true)); |
| 13323 | test_cases.emplace_back(new test_top_k_mask(GGML_TYPE_F16, n_kv, 0, 0, 2048, 0, false, nb, 1, 1, true)); |
| 13324 | } |
| 13325 | } |
| 13326 | |
| 13327 | // launch-overhead isolation: single L2_NORM launch vs batched siblings at the GDN |
| 13328 | // production shape (strided qkv views) -- perf-mode only, the eval list has its own |
| 13329 | // 2/4-wide coverage |
| 13330 | for (int n : { 128, 256 }) { |
| 13331 | test_cases.emplace_back(new test_l2_norm(GGML_TYPE_F32, { n, 16, 16, 1 }, 1e-12f, false, false)); |
| 13332 | test_cases.emplace_back(new test_l2_norm_batch(GGML_TYPE_F32, { n, 16, 16, 1 }, 2, 1e-12f, true)); |
| 13333 | test_cases.emplace_back(new test_l2_norm_batch(GGML_TYPE_F32, { n, 16, 16, 1 }, 4, 1e-12f, true)); |
| 13334 | } |
| 13335 | |
| 13336 | |
| 13337 | return test_cases; |
| 13338 | } |
| 13339 | |
| 13340 | static std::vector<std::unique_ptr<test_case>> make_test_cases_from_file(const char * path) { |
| 13341 | std::ifstream f(path); |
| 13342 | |
| 13343 | if (!f.is_open()) { |
| 13344 | throw std::runtime_error("Unable to read test file"); |
| 13345 | } |
| 13346 | |
| 13347 | std::vector<std::unique_ptr<test_case>> test_cases; |
| 13348 | |
| 13349 | std::string line; |
| 13350 | |
| 13351 | while (std::getline(f, line)) { |
| 13352 | std::istringstream iss(line); |
| 13353 | |
| 13354 | ggml_op op; |
| 13355 | ggml_type type; |
| 13356 | std::array<int64_t, 4> ne; |
| 13357 | std::array<int32_t, GGML_MAX_OP_PARAMS / sizeof(int32_t)> op_params = {}; |
| 13358 | std::string name; |
| 13359 | uint64_t tmp; |
| 13360 | |
| 13361 | iss >> tmp; |
| 13362 | op = (ggml_op)tmp; |
| 13363 | iss >> tmp; |
| 13364 | type = (ggml_type)tmp; |
| 13365 | |
| 13366 | for (size_t i = 0; i < 4; i++) { |
| 13367 | iss >> ne[i]; |
| 13368 | } |
| 13369 | |
| 13370 | iss >> tmp; |
| 13371 | for (size_t i = 0; i < tmp && i < op_params.size(); i++) { |
| 13372 | iss >> op_params[i]; |
| 13373 | } |
| 13374 | |
| 13375 | iss >> tmp; |
| 13376 | |
| 13377 | size_t num_src = std::min((uint64_t)GGML_MAX_SRC, tmp); |
| 13378 | std::vector<input_tensor> sources(num_src); |
| 13379 | for (size_t i = 0; i < num_src; i++) { |
| 13380 | input_tensor& src = sources[i]; |
| 13381 | iss >> tmp; |
| 13382 | src.type = (ggml_type)tmp; |
| 13383 | |
| 13384 | for (size_t i = 0; i < 4; i++) { |
| 13385 | iss >> src.ne[i]; |
| 13386 | } |
| 13387 | for (size_t i = 0; i < 4; i++) { |
| 13388 | iss >> src.nb[i]; |
| 13389 | } |
| 13390 | } |
| 13391 | |
| 13392 | iss >> name; |
| 13393 | |
| 13394 | if (name.length() == 1 && name[0] == '-') { |
| 13395 | name = ""; |
| 13396 | } |
| 13397 | |
| 13398 | test_cases.emplace_back(new test_generic_op(op, type, ne, op_params, sources, std::move(name))); |
| 13399 | } |
| 13400 | |
| 13401 | return test_cases; |
| 13402 | } |
| 13403 | |
| 13404 | // ---- FA vec (Q,NE): forced-config numerical slice (Metal only) ---- |
| 13405 | using set_fa_vec_override_t = void (*)(int, int); |
| 13406 | using clear_fa_vec_override_t = void (*)(void); |
| 13407 | |
| 13408 | // NL = 32/NE must divide both dk/4 and dv/4. |
| 13409 | static std::vector<int> fa_vec_legal_ne(int dk, int dv) { |
| 13410 | std::vector<int> r; |
| 13411 | for (int ne : {1, 2, 4}) { |
| 13412 | const int nl = 32 / ne; |
| 13413 | if ((dk/4) % nl == 0 && (dv/4) % nl == 0) { |
| 13414 | r.push_back(ne); |
| 13415 | } |
| 13416 | } |
| 13417 | return r; |
| 13418 | } |
| 13419 | |
| 13420 | static bool op_names_filter_selects(const char * op_names_filter, const char * op_name) { |
| 13421 | if (op_names_filter == nullptr) { |
| 13422 | return true; |
| 13423 | } |
| 13424 | for (const auto & entry : op_filter_entries(op_names_filter)) { |
| 13425 | // a full test case string is matched by its op name prefix |
| 13426 | const auto lparen_pos = entry.find_first_of('('); |
| 13427 | const auto op_entry = lparen_pos != std::string_view::npos ? entry.substr(0, lparen_pos) : entry; |
| 13428 | if (op_filter_entry_matches(op_entry, op_name)) { |
| 13429 | return true; |
| 13430 | } |
| 13431 | } |
| 13432 | return false; |
| 13433 | } |
| 13434 | |
| 13435 | // Covers padded rows, sinks, kvpad, multi-SIMDgroup reduction, quantized K/V, and MLA views. |
| 13436 | // The override is backend-global, so this runs after all parallel workers have joined. |
| 13437 | static bool run_fa_vec_slice(ggml_backend_t backend, ggml_backend_t backend_cpu, const char * op_names_filter) { |
| 13438 | const char * LLAMA_TEST_FA_VEC_DISABLE = getenv("LLAMA_TEST_FA_VEC_DISABLE"); |
| 13439 | if (LLAMA_TEST_FA_VEC_DISABLE) { |
| 13440 | return true; |
| 13441 | } |
| 13442 | |
| 13443 | if (!op_names_filter_selects(op_names_filter, "FLASH_ATTN_EXT")) { |
| 13444 | return true; |
| 13445 | } |
| 13446 | |
| 13447 | auto * reg = ggml_backend_dev_backend_reg(ggml_backend_get_device(backend)); |
| 13448 | |
| 13449 | auto set_ov = (set_fa_vec_override_t) ggml_backend_reg_get_proc_address(reg, "ggml_backend_metal_tuning_set_fa_vec_override"); |
| 13450 | auto clear_ov = (clear_fa_vec_override_t) ggml_backend_reg_get_proc_address(reg, "ggml_backend_metal_tuning_clear_fa_vec_override"); |
| 13451 | if (!set_ov || !clear_ov) { |
| 13452 | return true; // not the Metal backend: nothing to force |
| 13453 | } |
| 13454 | |
| 13455 | printf("Running FA vec slice tests (env LLAMA_TEST_FA_VEC_DISABLE=1 to skip)\n"); |
| 13456 | |
| 13457 | struct shape_t { int dk, dv; }; |
| 13458 | const shape_t shapes[] = { { 128, 128 }, { 576, 512 } }; // mainstream head size + MLA shared K/V view |
| 13459 | const int ne01_pts[] = { 1, 3 }; // decode, and padded rows for Q=2 and Q=4 |
| 13460 | const int ne11_pts[] = { 512, 4097 }; // nsg=1, and nsg>=2 together with kvpad |
| 13461 | const ggml_type types[] = { GGML_TYPE_F16, GGML_TYPE_Q4_0 }; |
| 13462 | |
| 13463 | int n_run = 0; |
| 13464 | int n_fail = 0; |
| 13465 | for (auto s : shapes) { |
| 13466 | for (int ne : fa_vec_legal_ne(s.dk, s.dv)) { |
| 13467 | for (int Q : { 1, 2, 4 }) { |
| 13468 | for (ggml_type type_kv : types) { |
| 13469 | for (bool sinks : { false, true }) { |
| 13470 | for (int ne01 : ne01_pts) { |
| 13471 | for (int ne11 : ne11_pts) { |
| 13472 | set_ov(Q, ne); |
| 13473 | test_flash_attn_ext tc(s.dk, s.dv, /*nh=*/4, { 1, 1 }, /*kv=*/ne11, /*nb=*/ne01, |
| 13474 | /*mask=*/true, sinks, 0.0f, 0.0f, GGML_PREC_F32, |
| 13475 | type_kv, type_kv); |
| 13476 | auto st = tc.eval(backend, backend_cpu, "FLASH_ATTN_EXT", nullptr); |
| 13477 | clear_ov(); |
| 13478 | |
| 13479 | if (st == test_status_t::FAIL) { |
| 13480 | printf(" FAIL fa_vec slice: dk=%d dv=%d Q=%d ne=%d type=%s ne01=%d ne11=%d sinks=%d\n", |
| 13481 | s.dk, s.dv, Q, ne, ggml_type_name(type_kv), ne01, ne11, (int) sinks); |
| 13482 | n_fail++; |
| 13483 | } |
| 13484 | n_run++; |
| 13485 | } |
| 13486 | } |
| 13487 | } |
| 13488 | } |
| 13489 | } |
| 13490 | } |
| 13491 | } |
| 13492 | |
| 13493 | printf(" fa_vec (Q,NE) slice: %d cases run, %d failed\n", n_run, n_fail); |
| 13494 | |
| 13495 | return n_fail == 0; |
| 13496 | } |
| 13497 | |
| 13498 | static bool test_backend(ggml_backend_t backend, ggml_backend_dev_t dev, test_mode mode, const char * op_names_filter, const char * params_filter, |
| 13499 | printer * output_printer, const char * test_file_path, int parallel_workers) { |
| 13500 | auto filter_test_cases = [](std::vector<std::unique_ptr<test_case>> & test_cases, const char * params_filter) { |
| 13501 | if (params_filter == nullptr) { |
| 13502 | return; |
| 13503 | } |
| 13504 | |
| 13505 | std::regex params_filter_regex(params_filter); |
| 13506 | |
| 13507 | for (auto it = test_cases.begin(); it != test_cases.end();) { |
| 13508 | if (!std::regex_search((*it)->vars(), params_filter_regex)) { |
| 13509 | it = test_cases.erase(it); |
| 13510 | continue; |
| 13511 | } |
| 13512 | |
| 13513 | it++; |
| 13514 | } |
| 13515 | }; |
| 13516 | |
| 13517 | std::vector<std::unique_ptr<test_case>> test_cases; |
| 13518 | |
| 13519 | if (test_file_path == nullptr) { |
| 13520 | switch (mode) { |
| 13521 | case MODE_TEST: |
| 13522 | case MODE_GRAD: |
| 13523 | case MODE_SUPPORT: |
| 13524 | test_cases = make_test_cases_eval(); |
| 13525 | break; |
| 13526 | case MODE_PERF: |
| 13527 | test_cases = make_test_cases_perf(); |
| 13528 | break; |
| 13529 | } |
| 13530 | } else { |
| 13531 | test_cases = make_test_cases_from_file(test_file_path); |
| 13532 | } |
| 13533 | |
| 13534 | filter_test_cases(test_cases, params_filter); |
| 13535 | |
| 13536 | if (mode == MODE_TEST) { |
| 13537 | ggml_backend_ptr backend_cpu(ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, NULL)); |
| 13538 | if (backend_cpu == NULL) { |
| 13539 | test_operation_info info("", "", "CPU"); |
| 13540 | info.set_error("backend", "Failed to initialize CPU backend"); |
| 13541 | output_printer->print_operation(info); |
| 13542 | return false; |
| 13543 | } |
| 13544 | // Use reference implementation on the CPU backend for comparison |
| 13545 | using ggml_backend_cpu_set_use_ref_t = void (*)(ggml_backend_t, bool); |
| 13546 | auto * reg = ggml_backend_dev_backend_reg(ggml_backend_get_device(backend_cpu.get())); |
| 13547 | auto * set_use_ref = (ggml_backend_cpu_set_use_ref_t) ggml_backend_reg_get_proc_address(reg, "ggml_backend_cpu_set_use_ref"); |
| 13548 | if (set_use_ref) { |
| 13549 | set_use_ref(backend_cpu.get(), true); |
| 13550 | } |
| 13551 | |
| 13552 | std::atomic<size_t> n_ok = 0; |
| 13553 | std::atomic<size_t> tests_run = 0; |
| 13554 | std::vector<std::string> failed_tests; |
| 13555 | std::mutex failed_tests_mutex; |
| 13556 | |
| 13557 | // Each worker grabs a chunk of cases at a time. The chunk shrinks as we |
| 13558 | // run out of work so that a few slow tests at the tail get spread across |
| 13559 | // workers instead of landing on one unlucky thread. |
| 13560 | constexpr size_t MAX_TESTS_PER_ITER = 100; |
| 13561 | std::atomic<size_t> test_idx = 0; |
| 13562 | |
| 13563 | const auto & next_chunk = [&](size_t & my_begin, size_t & my_end) { |
| 13564 | const size_t cur = test_idx.load(std::memory_order_relaxed); |
| 13565 | const size_t remaining = cur < test_cases.size() ? test_cases.size() - cur : 0; |
| 13566 | const size_t chunk = std::max<size_t>(1, std::min<size_t>(MAX_TESTS_PER_ITER, remaining / parallel_workers)); |
| 13567 | my_begin = test_idx.fetch_add(chunk); |
| 13568 | my_end = std::min(my_begin + chunk, test_cases.size()); |
| 13569 | }; |
| 13570 | |
| 13571 | const auto & run_tests = [&](ggml_backend_t b, ggml_backend_t b_cpu) { |
| 13572 | size_t my_begin, my_end; |
| 13573 | next_chunk(my_begin, my_end); |
| 13574 | while (my_begin < test_cases.size()) { |
| 13575 | for (size_t i = my_begin; i < my_end; ++i) { |
| 13576 | auto & test = test_cases[i]; |
| 13577 | test_status_t status = test->eval(b, b_cpu, op_names_filter, output_printer); |
| 13578 | if (status == test_status_t::SKIPPED || status == test_status_t::NOT_SUPPORTED) { |
| 13579 | continue; |
| 13580 | } |
| 13581 | tests_run++; |
| 13582 | if (status == test_status_t::OK) { |
| 13583 | n_ok++; |
| 13584 | } else if (status == test_status_t::FAIL) { |
| 13585 | std::lock_guard<std::mutex> guard(failed_tests_mutex); |
| 13586 | failed_tests.push_back(test->current_op_name + "(" + test->vars() + ")"); |
| 13587 | } |
| 13588 | } |
| 13589 | next_chunk(my_begin, my_end); |
| 13590 | } |
| 13591 | }; |
| 13592 | |
| 13593 | if (parallel_workers <= 1) { |
| 13594 | // Reuse the outer backend / backend_cpu so we don't pay an |
| 13595 | // extra CPU backend init. |
| 13596 | run_tests(backend, backend_cpu.get()); |
| 13597 | } else { |
| 13598 | std::atomic<size_t> workers_started = 0; |
| 13599 | |
| 13600 | const auto & eval_worker = [&]() { |
| 13601 | ggml_backend_ptr b(ggml_backend_dev_init(dev, NULL)); |
| 13602 | if (b == NULL) { |
| 13603 | return; |
| 13604 | } |
| 13605 | |
| 13606 | ggml_backend_ptr b_cpu(ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, NULL)); |
| 13607 | if (b_cpu == NULL) { |
| 13608 | return; |
| 13609 | } |
| 13610 | |
| 13611 | if (set_use_ref) { |
| 13612 | set_use_ref(b_cpu.get(), true); |
| 13613 | } |
| 13614 | workers_started++; |
| 13615 | run_tests(b.get(), b_cpu.get()); |
| 13616 | }; |
| 13617 | |
| 13618 | std::vector<std::thread> threads; |
| 13619 | threads.reserve(parallel_workers); |
| 13620 | for (int i = 0; i < parallel_workers; ++i) { |
| 13621 | threads.emplace_back(eval_worker); |
| 13622 | } |
| 13623 | for (auto & t : threads) { |
| 13624 | t.join(); |
| 13625 | } |
| 13626 | |
| 13627 | if (workers_started == 0 && !test_cases.empty()) { |
| 13628 | return false; |
| 13629 | } |
| 13630 | } |
| 13631 | |
| 13632 | output_printer->print_summary(test_summary_info(n_ok, tests_run, false)); |
| 13633 | output_printer->print_failed_tests(failed_tests); |
| 13634 | |
| 13635 | const bool slice_ok = run_fa_vec_slice(backend, backend_cpu.get(), op_names_filter); |
| 13636 | |
| 13637 | return n_ok == tests_run && slice_ok; |
| 13638 | } |
| 13639 | |
| 13640 | if (mode == MODE_GRAD) { |
| 13641 | test_cases.erase( |
| 13642 | std::remove_if(test_cases.begin(), test_cases.end(), [](const std::unique_ptr<test_case> & tc) { |
| 13643 | return tc->run_whole_graph(); |
| 13644 | }), |
| 13645 | test_cases.end() |
| 13646 | ); |
| 13647 | |
| 13648 | size_t n_ok = 0; |
| 13649 | for (auto & test : test_cases) { |
| 13650 | if (test->eval_grad(backend, op_names_filter, output_printer)) { |
| 13651 | n_ok++; |
| 13652 | } |
| 13653 | } |
| 13654 | output_printer->print_summary(test_summary_info(n_ok, test_cases.size(), false)); |
| 13655 | |
| 13656 | return n_ok == test_cases.size(); |
| 13657 | } |
| 13658 | |
| 13659 | if (mode == MODE_PERF) { |
| 13660 | for (auto & test : test_cases) { |
| 13661 | test->eval_perf(backend, op_names_filter, output_printer); |
| 13662 | } |
| 13663 | return true; |
| 13664 | } |
| 13665 | |
| 13666 | if (mode == MODE_SUPPORT) { |
| 13667 | // Filter out fusion cases |
| 13668 | test_cases.erase( |
| 13669 | std::remove_if(test_cases.begin(), test_cases.end(), [](const std::unique_ptr<test_case> & tc) { |
| 13670 | return tc->run_whole_graph(); |
| 13671 | }), |
| 13672 | test_cases.end() |
| 13673 | ); |
| 13674 | |
| 13675 | for (auto & test : test_cases) { |
| 13676 | test->eval_support(backend, op_names_filter, output_printer); |
| 13677 | } |
| 13678 | return true; |
| 13679 | } |
| 13680 | |
| 13681 | GGML_ABORT("fatal error"); |
| 13682 | } |
| 13683 | |
| 13684 | static void list_all_ops() { |
| 13685 | printf("GGML operations:\n"); |
| 13686 | std::set<std::string> all_ops; |
| 13687 | |
| 13688 | for (int i = 1; i < GGML_OP_COUNT; i++) { |
| 13689 | all_ops.insert(ggml_op_name((enum ggml_op)i)); |
| 13690 | } |
| 13691 | for (int i = 0; i < GGML_UNARY_OP_COUNT; i++) { |
| 13692 | all_ops.insert(ggml_unary_op_name((enum ggml_unary_op)i)); |
| 13693 | } |
| 13694 | for (int i = 0; i < GGML_GLU_OP_COUNT; i++) { |
| 13695 | all_ops.insert(ggml_glu_op_name((enum ggml_glu_op)i)); |
| 13696 | } |
| 13697 | for (const auto & op : all_ops) { |
| 13698 | printf(" %s\n", op.c_str()); |
| 13699 | } |
| 13700 | printf("\nTotal: %zu operations\n", all_ops.size()); |
| 13701 | } |
| 13702 | |
| 13703 | static void show_test_coverage() { |
| 13704 | std::set<std::string> all_ops; |
| 13705 | for (int i = 1; i < GGML_OP_COUNT; i++) { |
| 13706 | auto op = (enum ggml_op)i; |
| 13707 | if (op == GGML_OP_VIEW || |
| 13708 | op == GGML_OP_RESHAPE || |
| 13709 | op == GGML_OP_PERMUTE || |
| 13710 | op == GGML_OP_TRANSPOSE || |
| 13711 | op == GGML_OP_CONT || |
| 13712 | op == GGML_OP_GLU || |
| 13713 | op == GGML_OP_UNARY) { |
| 13714 | continue; |
| 13715 | } |
| 13716 | all_ops.insert(ggml_op_name(op)); |
| 13717 | } |
| 13718 | for (int i = 0; i < GGML_UNARY_OP_COUNT; i++) { |
| 13719 | all_ops.insert(ggml_unary_op_name((enum ggml_unary_op)i)); |
| 13720 | } |
| 13721 | for (int i = 0; i < GGML_GLU_OP_COUNT; i++) { |
| 13722 | all_ops.insert(ggml_glu_op_name((enum ggml_glu_op)i)); |
| 13723 | } |
| 13724 | auto test_cases = make_test_cases_eval(); |
| 13725 | // Filter out fusion cases |
| 13726 | test_cases.erase( |
| 13727 | std::remove_if(test_cases.begin(), test_cases.end(), [](const std::unique_ptr<test_case> & tc) { |
| 13728 | return tc->run_whole_graph(); |
| 13729 | }), |
| 13730 | test_cases.end() |
| 13731 | ); |
| 13732 | |
| 13733 | std::set<std::string> tested_ops; |
| 13734 | |
| 13735 | ggml_init_params params = { |
| 13736 | /* .mem_size = */ ggml_tensor_overhead()*128 + ggml_graph_overhead(), |
| 13737 | /* .mem_base = */ NULL, |
| 13738 | /* .no_alloc = */ true, |
| 13739 | }; |
| 13740 | |
| 13741 | for (auto & test_case : test_cases) { |
| 13742 | ggml_context_ptr ctx(ggml_init(params)); |
| 13743 | if (ctx) { |
| 13744 | test_case->mode = MODE_TEST; |
| 13745 | ggml_tensor * out = test_case->build_graph(ctx.get()); |
| 13746 | if (out && out->op != GGML_OP_NONE) { |
| 13747 | if (out->op == GGML_OP_UNARY) { |
| 13748 | tested_ops.insert(ggml_unary_op_name(ggml_get_unary_op(out))); |
| 13749 | } else if (out->op == GGML_OP_GLU) { |
| 13750 | tested_ops.insert(ggml_glu_op_name(ggml_get_glu_op(out))); |
| 13751 | } else { |
| 13752 | tested_ops.insert(ggml_op_name(out->op)); |
| 13753 | } |
| 13754 | } |
| 13755 | } |
| 13756 | } |
| 13757 | std::set<std::string> covered_ops; |
| 13758 | std::set<std::string> uncovered_ops; |
| 13759 | for (const auto & op : all_ops) { |
| 13760 | if (tested_ops.count(op) > 0) { |
| 13761 | covered_ops.insert(op); |
| 13762 | } else { |
| 13763 | uncovered_ops.insert(op); |
| 13764 | } |
| 13765 | } |
| 13766 | |
| 13767 | printf("Operations covered by tests (%zu):\n", covered_ops.size()); |
| 13768 | for (const auto & op : covered_ops) { |
| 13769 | printf(" ✓ %s\n", op.c_str()); |
| 13770 | } |
| 13771 | printf("\nOperations without tests (%zu):\n", uncovered_ops.size()); |
| 13772 | for (const auto & op : uncovered_ops) { |
| 13773 | printf(" ✗ %s\n", op.c_str()); |
| 13774 | } |
| 13775 | |
| 13776 | printf("\nCoverage Summary:\n"); |
| 13777 | printf(" Total operations: %zu\n", all_ops.size()); |
| 13778 | printf(" Tested operations: %zu\n", covered_ops.size()); |
| 13779 | printf(" Untested operations: %zu\n", uncovered_ops.size()); |
| 13780 | printf(" Coverage: %.1f%%\n", (double)covered_ops.size() / all_ops.size() * 100.0); |
| 13781 | } |
| 13782 | |
| 13783 | static void usage(char ** argv) { |
| 13784 | printf("Usage: %s [mode] [options]\n\n", argv[0]); |
| 13785 | printf("Valid modes:\n"); |
| 13786 | printf(" test (default) compare with CPU backend for correctness\n"); |
| 13787 | printf(" grad compare gradients from backpropagation with method of finite differences\n"); |
| 13788 | printf(" perf performance evaluation\n"); |
| 13789 | printf(" support probe backend operation support\n\n"); |
| 13790 | printf("Options:\n"); |
| 13791 | printf(" -o <op|regex,..> comma separated list of exact op names (as given by ggml_op_desc()),\n"); |
| 13792 | printf(" full test case strings, and/or regexes matched against the op name\n"); |
| 13793 | printf(" -b <backend> run tests on the given backend (e.g. CPU, MTL0, CUDA0)\n"); |
| 13794 | printf(" -p <params regex> filter test cases by a regex matched against their params\n"); |
| 13795 | printf(" --output <console|sql|csv> output format (default: console)\n"); |
| 13796 | printf(" --list-ops list all available GGML operations\n"); |
| 13797 | printf(" --show-coverage show test coverage\n"); |
| 13798 | printf(" --test-file <path> read test operators from a test file generated by test-export-graph-ops\n"); |
| 13799 | printf(" -j <n> run tests using <n> parallel worker threads (default: 1, test mode only)\n\n"); |
| 13800 | printf("Examples:\n"); |
| 13801 | printf(" %s -j 8\n", argv[0]); |
| 13802 | printf(" %s -o ADD,MUL_MAT\n", argv[0]); |
| 13803 | printf(" %s -o ADD -p 'type=f16.*perm1=0'\n", argv[0]); |
| 13804 | printf(" %s -b MTL0 -o 'DSV4.*'\n", argv[0]); |
| 13805 | printf(" %s -b CUDA0 -o 'ADD(type=f16,ne=[1,1,1,1],nr=[32,1,1,1],nf=1,perm1=0,src_overlap=0)'\n", argv[0]); |
| 13806 | printf(" %s perf -o 'MUL_MAT.*'\n", argv[0]); |
| 13807 | } |
| 13808 | |
| 13809 | int main(int argc, char ** argv) { |
| 13810 | test_mode mode = MODE_TEST; |
| 13811 | output_formats output_format = CONSOLE; |
| 13812 | const char * op_names_filter = nullptr; |
| 13813 | const char * backend_filter = nullptr; |
| 13814 | const char * params_filter = nullptr; |
| 13815 | const char * test_file_path = nullptr; |
| 13816 | int parallel_workers = 1; |
| 13817 | |
| 13818 | for (int i = 1; i < argc; i++) { |
| 13819 | if (strcmp(argv[i], "test") == 0) { |
| 13820 | mode = MODE_TEST; |
| 13821 | } else if (strcmp(argv[i], "perf") == 0) { |
| 13822 | mode = MODE_PERF; |
| 13823 | } else if (strcmp(argv[i], "grad") == 0) { |
| 13824 | mode = MODE_GRAD; |
| 13825 | } else if (strcmp(argv[i], "support") == 0) { |
| 13826 | mode = MODE_SUPPORT; |
| 13827 | } else if (strcmp(argv[i], "-o") == 0) { |
| 13828 | if (i + 1 < argc) { |
| 13829 | op_names_filter = argv[++i]; |
| 13830 | } else { |
| 13831 | usage(argv); |
| 13832 | return 1; |
| 13833 | } |
| 13834 | } else if (strcmp(argv[i], "-b") == 0) { |
| 13835 | if (i + 1 < argc) { |
| 13836 | backend_filter = argv[++i]; |
| 13837 | } else { |
| 13838 | usage(argv); |
| 13839 | return 1; |
| 13840 | } |
| 13841 | } else if (strcmp(argv[i], "-p") == 0) { |
| 13842 | if (i + 1 < argc) { |
| 13843 | params_filter = argv[++i]; |
| 13844 | } else { |
| 13845 | usage(argv); |
| 13846 | return 1; |
| 13847 | } |
| 13848 | } else if (strcmp(argv[i], "--output") == 0) { |
| 13849 | if (i + 1 < argc) { |
| 13850 | if (!output_format_from_str(argv[++i], output_format)) { |
| 13851 | usage(argv); |
| 13852 | return 1; |
| 13853 | } |
| 13854 | } else { |
| 13855 | usage(argv); |
| 13856 | return 1; |
| 13857 | } |
| 13858 | } else if (strcmp(argv[i], "--list-ops") == 0) { |
| 13859 | list_all_ops(); |
| 13860 | return 0; |
| 13861 | } else if (strcmp(argv[i], "--show-coverage") == 0) { |
| 13862 | show_test_coverage(); |
| 13863 | return 0; |
| 13864 | } else if (strcmp(argv[i], "--test-file") == 0) { |
| 13865 | if (i + 1 < argc) { |
| 13866 | test_file_path = argv[++i]; |
| 13867 | } else { |
| 13868 | usage(argv); |
| 13869 | return 1; |
| 13870 | } |
| 13871 | } else if (strcmp(argv[i], "-j") == 0) { |
| 13872 | if (i + 1 < argc) { |
| 13873 | parallel_workers = atoi(argv[++i]); |
| 13874 | if (parallel_workers < 1) { |
| 13875 | usage(argv); |
| 13876 | return 1; |
| 13877 | } |
| 13878 | } else { |
| 13879 | usage(argv); |
| 13880 | return 1; |
| 13881 | } |
| 13882 | } else { |
| 13883 | usage(argv); |
| 13884 | return 1; |
| 13885 | } |
| 13886 | } |
| 13887 | |
| 13888 | // load and enumerate backends |
| 13889 | ggml_backend_load_all(); |
| 13890 | |
| 13891 | // Create printer for output format |
| 13892 | std::unique_ptr<printer> output_printer = create_printer(output_format); |
| 13893 | if (output_printer) { |
| 13894 | output_printer->print_header(); |
| 13895 | } |
| 13896 | |
| 13897 | output_printer->print_testing_start(testing_start_info(ggml_backend_dev_count())); |
| 13898 | |
| 13899 | size_t n_ok = 0; |
| 13900 | |
| 13901 | for (size_t i = 0; i < ggml_backend_dev_count(); i++) { |
| 13902 | ggml_backend_dev_t dev = ggml_backend_dev_get(i); |
| 13903 | |
| 13904 | if (backend_filter != NULL && strcmp(backend_filter, ggml_backend_dev_name(dev)) != 0) { |
| 13905 | output_printer->print_backend_init( |
| 13906 | backend_init_info(i, ggml_backend_dev_count(), ggml_backend_dev_name(dev), true, "Skipping")); |
| 13907 | n_ok++; |
| 13908 | continue; |
| 13909 | } |
| 13910 | |
| 13911 | if (backend_filter == NULL && ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU && mode != MODE_GRAD) { |
| 13912 | output_printer->print_backend_init(backend_init_info( |
| 13913 | i, ggml_backend_dev_count(), ggml_backend_dev_name(dev), true, "Skipping CPU backend")); |
| 13914 | n_ok++; |
| 13915 | continue; |
| 13916 | } |
| 13917 | |
| 13918 | ggml_backend_ptr backend(ggml_backend_dev_init(dev, NULL)); |
| 13919 | GGML_ASSERT(backend != NULL); |
| 13920 | |
| 13921 | ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(dev); |
| 13922 | auto ggml_backend_set_n_threads_fn = (ggml_backend_set_n_threads_t) ggml_backend_reg_get_proc_address(reg, "ggml_backend_set_n_threads"); |
| 13923 | if (ggml_backend_set_n_threads_fn) { |
| 13924 | ggml_backend_set_n_threads_fn(backend.get(), std::max<int>(1, N_THREADS/2)); |
| 13925 | } |
| 13926 | |
| 13927 | size_t free, total; // NOLINT |
| 13928 | ggml_backend_dev_memory(dev, &free, &total); |
| 13929 | output_printer->print_backend_init(backend_init_info(i, ggml_backend_dev_count(), ggml_backend_dev_name(dev), |
| 13930 | false, "", ggml_backend_dev_description(dev), |
| 13931 | total / 1024 / 1024, free / 1024 / 1024, true)); |
| 13932 | |
| 13933 | bool ok = test_backend(backend.get(), dev, mode, op_names_filter, params_filter, output_printer.get(), test_file_path, parallel_workers); |
| 13934 | |
| 13935 | if (ok) { |
| 13936 | n_ok++; |
| 13937 | } |
| 13938 | output_printer->print_backend_status( |
| 13939 | backend_status_info(ggml_backend_name(backend.get()), ok ? test_status_t::OK : test_status_t::FAIL)); |
| 13940 | } |
| 13941 | |
| 13942 | ggml_quantize_free(); |
| 13943 | |
| 13944 | if (output_printer) { |
| 13945 | output_printer->print_footer(); |
| 13946 | } |
| 13947 | |
| 13948 | output_printer->print_overall_summary( |
| 13949 | overall_summary_info(n_ok, ggml_backend_dev_count(), n_ok == ggml_backend_dev_count())); |
| 13950 | |
| 13951 | if (n_ok != ggml_backend_dev_count()) { |
| 13952 | return 1; |
| 13953 | } |
| 13954 | |
| 13955 | return 0; |
| 13956 | } |