ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-vulkan / ggml-vulkan-debug.cpp
| 1 | #include "ggml-vulkan-common.h" |
| 2 | |
| 3 | bool vk_memory_logger_enabled = false; |
| 4 | |
| 5 | bool vk_perf_logger_enabled = false; |
| 6 | |
| 7 | bool vk_perf_logger_concurrent = false; |
| 8 | |
| 9 | bool vk_enable_sync_logger = false; |
| 10 | |
| 11 | uint32_t vk_perf_logger_frequency = 1; |
| 12 | |
| 13 | std::string vk_pipeline_stats_filter; |
| 14 | |
| 15 | void vk_memory_logger::log_allocation(vk_buffer_ref buf_ref, size_t size) { |
| 16 | if (!vk_memory_logger_enabled) { |
| 17 | return; |
| 18 | } |
| 19 | std::lock_guard<std::mutex> guard(log_mutex); |
| 20 | vk_buffer buf = buf_ref.lock(); |
| 21 | const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal); |
| 22 | const std::string type = device ? "device" : "host"; |
| 23 | allocations[buf->buffer] = size; |
| 24 | total_device += device ? size : 0; |
| 25 | total_host += device ? 0 : size; |
| 26 | VK_LOG_MEMORY(buf->device->name << ": +" << format_size(size) << " " << type << " at " << buf->buffer << ". Total device: " << format_size(total_device) << ", total host: " << format_size(total_host)); |
| 27 | } |
| 28 | |
| 29 | void vk_memory_logger::log_deallocation(vk_buffer_ref buf_ref) { |
| 30 | if (buf_ref.expired() || buf_ref.lock()->size == 0 || !vk_memory_logger_enabled) { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | std::lock_guard<std::mutex> guard(log_mutex); |
| 35 | vk_buffer buf = buf_ref.lock(); |
| 36 | const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal); |
| 37 | std::string type = device ? "device" : "host"; |
| 38 | auto it = allocations.find(buf->buffer); |
| 39 | if (it != allocations.end()) { |
| 40 | total_device -= device ? it->second : 0; |
| 41 | total_host -= device ? 0 : it->second; |
| 42 | VK_LOG_MEMORY(buf->device->name << ": -" << format_size(it->second) << " " << type << " at " << buf->buffer << ". Total device: " << format_size(total_device) << ", total host: " << format_size(total_host)); |
| 43 | allocations.erase(it); |
| 44 | } else { |
| 45 | VK_LOG_MEMORY("ERROR " << buf->device->name << ": Attempted to deallocate unknown " << type << " memory at " << buf->buffer); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | #ifdef GGML_VULKAN_CHECK_RESULTS |
| 50 | size_t vk_skip_checks; |
| 51 | size_t vk_output_tensor; |
| 52 | |
| 53 | static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name); |
| 54 | #endif |
| 55 | |
| 56 | #ifdef GGML_VULKAN_RUN_TESTS |
| 57 | static void ggml_vk_print_matrix_area(const void * data, ggml_type type, int ne0, int ne1, int i0, int i1, int i2) { |
| 58 | if (type != GGML_TYPE_F32 && type != GGML_TYPE_F16) { |
| 59 | return; |
| 60 | } |
| 61 | i0 = std::max(i0, 5); |
| 62 | i1 = std::max(i1, 5); |
| 63 | i2 = std::max(i2, 0); |
| 64 | fprintf(stderr, " "); |
| 65 | for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) { |
| 66 | fprintf(stderr, "%7d ", idx1); |
| 67 | } |
| 68 | fprintf(stderr, "\n"); |
| 69 | for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) { |
| 70 | fprintf(stderr, "%7d: ", idx0); |
| 71 | for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) { |
| 72 | if (idx0 >= 0 && idx0 < ne0 && idx1 >= 0 && idx1 < ne1) { |
| 73 | float val; |
| 74 | if (type == GGML_TYPE_F32) { |
| 75 | val = *((const float *) data + i2*ne1*ne0 + idx1*ne0 + idx0); |
| 76 | } else if (type == GGML_TYPE_F16) { |
| 77 | val = ggml_fp16_to_fp32(*((const ggml_fp16_t *) data + i2*ne1*ne0 + idx1*ne0 + idx0)); |
| 78 | } else { |
| 79 | GGML_ABORT("fatal error"); |
| 80 | } |
| 81 | fprintf(stderr, "% 7.2f ", val); |
| 82 | } else { |
| 83 | fprintf(stderr, " "); |
| 84 | } |
| 85 | } |
| 86 | fprintf(stderr, "\n"); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | template <typename X_TYPE, typename Y_TYPE> |
| 91 | void ggml_vk_test_matmul(ggml_backend_vk_context * ctx, size_t m, size_t n, size_t k, size_t batch, size_t num_it, int split_k, int shader_size) { |
| 92 | VK_LOG_DEBUG("ggml_vk_test_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << shader_size << ")"); |
| 93 | const size_t x_ne = m * k * batch; |
| 94 | const size_t y_ne = k * n * batch; |
| 95 | const size_t d_ne = m * n * batch; |
| 96 | |
| 97 | ggml_type x_type = std::is_same<float, X_TYPE>() ? GGML_TYPE_F32 : GGML_TYPE_F16; |
| 98 | ggml_type y_type = std::is_same<float, Y_TYPE>() ? GGML_TYPE_F32 : GGML_TYPE_F16; |
| 99 | vk_matmul_pipeline_key mm_test_key{x_type, y_type, false, false}; |
| 100 | auto mm_test_it = ctx->device->pipeline_matmul.find(mm_test_key); |
| 101 | GGML_ASSERT(mm_test_it != ctx->device->pipeline_matmul.end() && !mm_test_it->second.empty()); |
| 102 | auto& mm_test_configs = mm_test_it->second; |
| 103 | GGML_ASSERT(shader_size >= 0 && shader_size < (int)mm_test_configs.size()); |
| 104 | |
| 105 | std::string shname = std::string(ggml_type_name(x_type)) + "_" + std::string(ggml_type_name(y_type)) + "_ALIGNED_" + std::to_string(shader_size); |
| 106 | vk_pipeline p = mm_test_configs[shader_size].aligned ? mm_test_configs[shader_size].aligned : mm_test_configs[shader_size].unaligned; |
| 107 | |
| 108 | const size_t kpad = ggml_vk_align_size(k, mm_test_configs[shader_size].align); |
| 109 | |
| 110 | if (k != kpad) { |
| 111 | p = mm_test_configs[shader_size].unaligned; |
| 112 | shname = std::string(ggml_type_name(x_type)) + "_" + std::string(ggml_type_name(y_type)) + "_" + std::to_string(shader_size); |
| 113 | } |
| 114 | |
| 115 | if (split_k > 1) { |
| 116 | ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it); |
| 117 | |
| 118 | if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) { |
| 119 | // Resize buffer |
| 120 | if (ctx->prealloc_split_k != nullptr) { |
| 121 | ggml_vk_destroy_buffer(ctx->prealloc_split_k); |
| 122 | } |
| 123 | ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, {vk::MemoryPropertyFlagBits::eDeviceLocal}); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | ggml_pipeline_allocate_descriptor_sets(ctx); |
| 128 | |
| 129 | vk_buffer d_X = ggml_vk_create_buffer_check(ctx->device, sizeof(X_TYPE) * x_ne, {vk::MemoryPropertyFlagBits::eDeviceLocal}); |
| 130 | vk_buffer d_Y = ggml_vk_create_buffer_check(ctx->device, sizeof(Y_TYPE) * y_ne, {vk::MemoryPropertyFlagBits::eDeviceLocal}); |
| 131 | vk_buffer d_D = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne, {vk::MemoryPropertyFlagBits::eDeviceLocal}); |
| 132 | |
| 133 | X_TYPE* x = (X_TYPE *) malloc(sizeof(X_TYPE) * x_ne); |
| 134 | Y_TYPE* y = (Y_TYPE *) malloc(sizeof(Y_TYPE) * y_ne); |
| 135 | float* d = (float *) malloc(sizeof(float) * d_ne); |
| 136 | |
| 137 | for (size_t i = 0; i < x_ne; i++) { |
| 138 | if (std::is_same<float, X_TYPE>()) { |
| 139 | x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f; |
| 140 | // x[i] = 1.0f; |
| 141 | // x[i] = i + 1; |
| 142 | // x[i] = (i % k == i / k) ? 1.0f : 0.0f; |
| 143 | } else if (std::is_same<ggml_fp16_t, X_TYPE>()) { |
| 144 | x[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f); |
| 145 | // x[i] = ggml_fp32_to_fp16(1.0f); |
| 146 | // x[i] = ggml_fp32_to_fp16(i + 1); |
| 147 | // x[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f); |
| 148 | } else { |
| 149 | GGML_ABORT("fatal error"); |
| 150 | } |
| 151 | } |
| 152 | for (size_t i = 0; i < y_ne; i++) { |
| 153 | if (std::is_same<float, Y_TYPE>()) { |
| 154 | y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f; |
| 155 | // y[i] = (i % k == i / k) ? 1.0f : 0.0f; |
| 156 | // y[i] = i + 1; |
| 157 | } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) { |
| 158 | y[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f); |
| 159 | // y[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f); |
| 160 | // y[i] = ggml_fp32_to_fp16(i + 1); |
| 161 | } else { |
| 162 | GGML_ABORT("fatal error"); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | ggml_vk_buffer_write(d_X, 0, x, sizeof(X_TYPE) * k * m * batch); |
| 167 | ggml_vk_buffer_write(d_Y, 0, y, sizeof(Y_TYPE) * k * n * batch); |
| 168 | |
| 169 | vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool); |
| 170 | ggml_vk_ctx_begin(ctx->device, subctx); |
| 171 | for (size_t i = 0; i < num_it; i++) { |
| 172 | ggml_vk_matmul( |
| 173 | ctx, subctx, p, ggml_vk_subbuffer(ctx, d_X), ggml_vk_subbuffer(ctx, d_Y), ggml_vk_subbuffer(ctx, d_D), ggml_vk_subbuffer(ctx, ctx->prealloc_split_k), |
| 174 | m, n, k, |
| 175 | k, k, m, k*m, k*n, m*n, |
| 176 | split_k, batch, batch, batch, 1, 1, n, |
| 177 | k * sizeof(Y_TYPE), m * sizeof(float) |
| 178 | ); |
| 179 | } |
| 180 | ggml_vk_ctx_end(subctx); |
| 181 | |
| 182 | auto begin = std::chrono::high_resolution_clock::now(); |
| 183 | ggml_vk_submit(subctx, ctx->fence); |
| 184 | VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_matmul waitForFences", ctx->device); |
| 185 | ctx->device->device.resetFences({ ctx->fence }); |
| 186 | ggml_vk_queue_command_pools_cleanup(ctx->device); |
| 187 | |
| 188 | auto end = std::chrono::high_resolution_clock::now(); |
| 189 | double time = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0; |
| 190 | |
| 191 | // copy dst to host |
| 192 | ggml_vk_buffer_read(d_D, 0, d, sizeof(float) * d_ne); |
| 193 | |
| 194 | float * d_chk = (float *) malloc(sizeof(float) * d_ne); |
| 195 | |
| 196 | ggml_init_params iparams = { |
| 197 | /*.mem_size =*/ 1024*1024*1024, |
| 198 | /*.mem_buffer =*/ NULL, |
| 199 | /*.no_alloc =*/ true, |
| 200 | }; |
| 201 | |
| 202 | ggml_context * ggml_ctx = ggml_init(iparams); |
| 203 | |
| 204 | ggml_type src0_type; |
| 205 | ggml_type src1_type; |
| 206 | |
| 207 | if (std::is_same<float, X_TYPE>()) { |
| 208 | src0_type = GGML_TYPE_F32; |
| 209 | } else if (std::is_same<ggml_fp16_t, X_TYPE>()) { |
| 210 | src0_type = GGML_TYPE_F16; |
| 211 | } else { |
| 212 | GGML_ABORT("fatal error"); |
| 213 | } |
| 214 | if (std::is_same<float, Y_TYPE>()) { |
| 215 | src1_type = GGML_TYPE_F32; |
| 216 | } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) { |
| 217 | src1_type = GGML_TYPE_F16; |
| 218 | } else { |
| 219 | GGML_ABORT("fatal error"); |
| 220 | } |
| 221 | |
| 222 | ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, src0_type, k, m, batch); |
| 223 | ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, src1_type, k, n, batch); |
| 224 | ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml); |
| 225 | |
| 226 | src0_ggml->data = x; |
| 227 | src1_ggml->data = y; |
| 228 | tensor_ggml->data = d_chk; |
| 229 | |
| 230 | ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx); |
| 231 | ggml_build_forward_expand(cgraph, tensor_ggml); |
| 232 | |
| 233 | ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1); |
| 234 | |
| 235 | ggml_free(ggml_ctx); |
| 236 | |
| 237 | double avg_err = 0.0; |
| 238 | int first_err_n = -1; |
| 239 | int first_err_m = -1; |
| 240 | int first_err_b = -1; |
| 241 | |
| 242 | for (size_t i = 0; i < m*n*batch; i++) { |
| 243 | double err = std::fabs(d[i] - d_chk[i]); |
| 244 | avg_err += err; |
| 245 | |
| 246 | if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) { |
| 247 | first_err_b = i / (m * n); |
| 248 | first_err_n = (i % (m * n)) / m; |
| 249 | first_err_m = (i % (m * n)) % m; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | avg_err /= m * n; |
| 254 | |
| 255 | double tflops = 2.0*m*n*k*batch*num_it / (time / 1000.0) / (1000.0*1000.0*1000.0*1000.0); |
| 256 | |
| 257 | std::cerr << "TEST " << shname << " m=" << m << " n=" << n << " k=" << k << " batch=" << batch << " split_k=" << split_k << " matmul " << time / num_it << "ms " << tflops << " TFLOPS avg_err=" << avg_err << std::endl; |
| 258 | |
| 259 | if (avg_err > 0.1 || std::isnan(avg_err)) { |
| 260 | std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl; |
| 261 | std::cerr << "Actual result: " << std::endl << std::endl; |
| 262 | ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b); |
| 263 | std::cerr << "Expected result: " << std::endl << std::endl; |
| 264 | ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b); |
| 265 | |
| 266 | if (split_k > 1) { |
| 267 | float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k); |
| 268 | ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k); |
| 269 | |
| 270 | std::cerr << "d_buf0: " << std::endl << std::endl; |
| 271 | ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b); |
| 272 | |
| 273 | std::cerr << "d_buf1: " << std::endl << std::endl; |
| 274 | ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b); |
| 275 | |
| 276 | std::cerr << "d_buf2: " << std::endl << std::endl; |
| 277 | ggml_vk_print_matrix_area(split_k_buf + 2 * d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b); |
| 278 | |
| 279 | std::cerr << "d_buf3: " << std::endl << std::endl; |
| 280 | ggml_vk_print_matrix_area(split_k_buf + 3 * d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b); |
| 281 | |
| 282 | free(split_k_buf); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | free(d_chk); |
| 287 | |
| 288 | ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool); |
| 289 | |
| 290 | ggml_vk_destroy_buffer(d_X); |
| 291 | ggml_vk_destroy_buffer(d_Y); |
| 292 | ggml_vk_destroy_buffer(d_D); |
| 293 | |
| 294 | free(x); |
| 295 | free(y); |
| 296 | free(d); |
| 297 | } |
| 298 | |
| 299 | template void ggml_vk_test_matmul<ggml_fp16_t, float>(ggml_backend_vk_context * ctx, size_t m, size_t n, size_t k, size_t batch, size_t num_it, int split_k, int shader_size); |
| 300 | |
| 301 | static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, int i0, int i1, int i2, int i3) { |
| 302 | if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16) { |
| 303 | return; |
| 304 | } |
| 305 | i0 = std::max(i0, 5); |
| 306 | i1 = std::max(i1, 5); |
| 307 | i2 = std::max(i2, 0); |
| 308 | i3 = std::max(i3, 0); |
| 309 | fprintf(stderr, " "); |
| 310 | for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) { |
| 311 | fprintf(stderr, "%7d ", idx1); |
| 312 | } |
| 313 | fprintf(stderr, "\n"); |
| 314 | for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) { |
| 315 | fprintf(stderr, "%7d: ", idx0); |
| 316 | for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) { |
| 317 | if (idx0 >= 0 && idx0 < tensor->ne[0] && idx1 >= 0 && idx1 < tensor->ne[1] && i2 >= 0 && i2 < tensor->ne[2] && i3 >= 0 && i3 < tensor->ne[3]) { |
| 318 | float val; |
| 319 | if (tensor->type == GGML_TYPE_F32) { |
| 320 | val = *(float *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]); |
| 321 | } else if (tensor->type == GGML_TYPE_F16) { |
| 322 | val = ggml_fp16_to_fp32(*(ggml_fp16_t *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0])); |
| 323 | } else { |
| 324 | GGML_ABORT("fatal error"); |
| 325 | } |
| 326 | fprintf(stderr, "% 7.2f ", val); |
| 327 | } else { |
| 328 | fprintf(stderr, " "); |
| 329 | } |
| 330 | } |
| 331 | fprintf(stderr, "\n"); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | static void ggml_vk_quantize_data(const float * from, void * to, size_t ne, ggml_type quant) { |
| 336 | ggml_quantize_chunk(quant, from, to, 0, 1, ne, nullptr); |
| 337 | } |
| 338 | |
| 339 | static void ggml_vk_dequantize_data(const void * from, float * to, size_t ne, ggml_type quant) { |
| 340 | if (quant == GGML_TYPE_F32) { |
| 341 | memcpy(to, from, sizeof(float) * ne); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | const auto * tt = ggml_get_type_traits(quant); |
| 346 | |
| 347 | ggml_to_float_t dequant_fn = tt->to_float; |
| 348 | |
| 349 | dequant_fn(from, to, ne); |
| 350 | } |
| 351 | |
| 352 | void ggml_vk_test_dequant(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) { |
| 353 | VK_LOG_DEBUG("ggml_vk_test_dequant(" << ne << ")"); |
| 354 | const size_t x_sz = sizeof(float) * ne; |
| 355 | const size_t x_sz_f16 = sizeof(ggml_fp16_t) * ne; |
| 356 | const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant); |
| 357 | float * x = (float *) malloc(x_sz); |
| 358 | void * qx = malloc(qx_sz); |
| 359 | vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal}); |
| 360 | vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz_f16, {vk::MemoryPropertyFlagBits::eDeviceLocal}); |
| 361 | float * x_ref = (float *) malloc(x_sz); |
| 362 | ggml_fp16_t * x_chk = (ggml_fp16_t *) malloc(x_sz_f16); |
| 363 | |
| 364 | for (size_t i = 0; i < ne; i++) { |
| 365 | x[i] = rand() / (float)RAND_MAX; |
| 366 | } |
| 367 | |
| 368 | vk_pipeline p = ggml_vk_get_to_fp16(ctx, quant); |
| 369 | |
| 370 | ggml_vk_quantize_data(x, qx, ne, quant); |
| 371 | ggml_vk_dequantize_data(qx, x_ref, ne, quant); |
| 372 | |
| 373 | ggml_pipeline_request_descriptor_sets(ctx, p, 1); |
| 374 | |
| 375 | ggml_pipeline_allocate_descriptor_sets(ctx); |
| 376 | |
| 377 | ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz); |
| 378 | |
| 379 | vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool); |
| 380 | ggml_vk_ctx_begin(ctx->device, subctx); |
| 381 | const std::vector<uint32_t> pc = { 1, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne }; |
| 382 | ggml_vk_dispatch_pipeline(ctx, subctx, p, { vk_subbuffer{ qx_buf, 0, qx_sz }, vk_subbuffer{ x_buf, 0, x_sz_f16 } }, pc, { (uint32_t)ne, 1, 1}); |
| 383 | ggml_vk_ctx_end(subctx); |
| 384 | |
| 385 | auto begin = std::chrono::high_resolution_clock::now(); |
| 386 | |
| 387 | ggml_vk_submit(subctx, ctx->fence); |
| 388 | VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences", ctx->device); |
| 389 | ctx->device->device.resetFences({ ctx->fence }); |
| 390 | ggml_vk_queue_command_pools_cleanup(ctx->device); |
| 391 | |
| 392 | auto end = std::chrono::high_resolution_clock::now(); |
| 393 | |
| 394 | double ms_dequant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0; |
| 395 | ggml_vk_buffer_read(x_buf, 0, x_chk, x_sz_f16); |
| 396 | |
| 397 | int first_err = -1; |
| 398 | |
| 399 | double avg_err = 0.0; |
| 400 | for (size_t i = 0; i < ne; i++) { |
| 401 | double error = std::fabs(x_ref[i] - ggml_fp16_to_fp32(x_chk[i])); |
| 402 | avg_err += error; |
| 403 | |
| 404 | if (first_err < 0 && error > 0.05) { |
| 405 | first_err = i; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | avg_err /= ne; |
| 410 | |
| 411 | std::cerr << "TEST DEQUANT " << ggml_type_name(quant) << " time=" << ms_dequant << "ms avg_err=" << avg_err << std::endl; |
| 412 | |
| 413 | if (avg_err > 0.1) { |
| 414 | std::cerr << "first_error = " << first_err << std::endl; |
| 415 | std::cerr << "Actual result: " << std::endl << std::endl; |
| 416 | for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) { |
| 417 | std::cerr << ggml_fp16_to_fp32(x_chk[i]) << ", "; |
| 418 | } |
| 419 | std::cerr << std::endl << "Expected result: " << std::endl << std::endl; |
| 420 | for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) { |
| 421 | std::cerr << x_ref[i] << ", "; |
| 422 | } |
| 423 | std::cerr << std::endl; |
| 424 | } |
| 425 | |
| 426 | ggml_vk_destroy_buffer(x_buf); |
| 427 | ggml_vk_destroy_buffer(qx_buf); |
| 428 | |
| 429 | free(x); |
| 430 | free(qx); |
| 431 | free(x_ref); |
| 432 | free(x_chk); |
| 433 | } |
| 434 | |
| 435 | // This does not work without ggml q8_1 quantization support |
| 436 | // |
| 437 | // typedef uint16_t ggml_half; |
| 438 | // typedef uint32_t ggml_half2; |
| 439 | // |
| 440 | // #define QK8_1 32 |
| 441 | // typedef struct { |
| 442 | // union { |
| 443 | // struct { |
| 444 | // ggml_half d; // delta |
| 445 | // ggml_half s; // d * sum(qs[i]) |
| 446 | // } GGML_COMMON_AGGR_S; |
| 447 | // ggml_half2 ds; |
| 448 | // } GGML_COMMON_AGGR_U; |
| 449 | // int8_t qs[QK8_1]; // quants |
| 450 | // } block_q8_1; |
| 451 | // |
| 452 | // static void ggml_vk_test_quantize(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) { |
| 453 | // VK_LOG_DEBUG("ggml_vk_test_quantize(" << ne << ")"); |
| 454 | // GGML_ASSERT(quant == GGML_TYPE_Q8_1); |
| 455 | // |
| 456 | // const size_t x_sz = sizeof(float) * ne; |
| 457 | // const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant); |
| 458 | // float * x = (float *) malloc(x_sz); |
| 459 | // block_q8_1 * qx = (block_q8_1 *)malloc(qx_sz); |
| 460 | // block_q8_1 * qx_res = (block_q8_1 *)malloc(qx_sz); |
| 461 | // vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal}); |
| 462 | // vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal}); |
| 463 | // |
| 464 | // for (size_t i = 0; i < ne; i++) { |
| 465 | // x[i] = rand() / (float)RAND_MAX; |
| 466 | // } |
| 467 | // |
| 468 | // vk_pipeline p = ggml_vk_get_quantize_pipeline(ctx, quant); |
| 469 | // |
| 470 | // ggml_pipeline_request_descriptor_sets(ctx, p, 1); |
| 471 | // |
| 472 | // ggml_pipeline_allocate_descriptor_sets(ctx); |
| 473 | // |
| 474 | // ggml_vk_buffer_write(x_buf, 0, x, x_sz); |
| 475 | // |
| 476 | // vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool); |
| 477 | // ggml_vk_ctx_begin(ctx->device, subctx); |
| 478 | // ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(ctx, x_buf), ggml_vk_subbuffer(ctx, qx_buf), ne); |
| 479 | // ggml_vk_ctx_end(subctx); |
| 480 | // |
| 481 | // auto begin = std::chrono::high_resolution_clock::now(); |
| 482 | // |
| 483 | // ggml_vk_submit(subctx, ctx->fence); |
| 484 | // VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_quantize waitForFences"); |
| 485 | // ctx->device->device.resetFences({ ctx->fence }); |
| 486 | // ggml_vk_queue_command_pools_cleanup(ctx->device); |
| 487 | // |
| 488 | // auto end = std::chrono::high_resolution_clock::now(); |
| 489 | // |
| 490 | // double ms_quant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0; |
| 491 | // ggml_vk_buffer_read(qx_buf, 0, qx, qx_sz); |
| 492 | // |
| 493 | // ggml_vk_quantize_data(x, qx_res, ne, quant); |
| 494 | // |
| 495 | // int first_err = -1; |
| 496 | // |
| 497 | // for (size_t i = 0; i < ne / 32; i++) { |
| 498 | // double error = std::fabs(ggml_fp16_to_fp32(qx_res[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d) - ggml_fp16_to_fp32(qx[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d)); |
| 499 | // |
| 500 | // if (first_err < 0 && error > 0.1) { |
| 501 | // first_err = i; |
| 502 | // } |
| 503 | // |
| 504 | // error = std::fabs(ggml_fp16_to_fp32(qx_res[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s) - ggml_fp16_to_fp32(qx[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s)); |
| 505 | // |
| 506 | // if (first_err < 0 && error > 0.1) { |
| 507 | // first_err = i; |
| 508 | // } |
| 509 | // |
| 510 | // for (size_t j = 0; j < 32; j++) { |
| 511 | // uint64_t error = std::abs(qx_res[i].qs[j] - qx[i].qs[j]); |
| 512 | // |
| 513 | // if (first_err < 0 && error > 1) { |
| 514 | // first_err = i; |
| 515 | // } |
| 516 | // } |
| 517 | // } |
| 518 | // |
| 519 | // std::cerr << "TEST QUANTIZE " << ggml_type_name(quant) << " time=" << ms_quant << "ms " << (first_err == -1 ? "CORRECT" : "INCORRECT") << std::endl; |
| 520 | // |
| 521 | // if (first_err != -1) { |
| 522 | // std::cerr << "first_error = " << first_err << std::endl; |
| 523 | // std::cerr << "Actual result: " << std::endl << std::endl; |
| 524 | // std::cout << "d=" << ggml_fp16_to_fp32(qx[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d) << " s=" << ggml_fp16_to_fp32(qx[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s) << " "; |
| 525 | // for (size_t j = 0; j < 32; j++) { |
| 526 | // std::cout << " qs" << j << "=" << (uint32_t)qx[first_err].qs[j] << " "; |
| 527 | // } |
| 528 | // std::cerr << std::endl << std::endl << "Expected result: " << std::endl << std::endl; |
| 529 | // std::cout << "d=" << ggml_fp16_to_fp32(qx_res[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d) << " s=" << ggml_fp16_to_fp32(qx_res[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s) << " "; |
| 530 | // for (size_t j = 0; j < 32; j++) { |
| 531 | // std::cout << " qs" << j << "=" << (uint32_t)qx_res[first_err].qs[j] << " "; |
| 532 | // } |
| 533 | // std::cerr << std::endl; |
| 534 | // } |
| 535 | // |
| 536 | // ggml_vk_destroy_buffer(x_buf); |
| 537 | // ggml_vk_destroy_buffer(qx_buf); |
| 538 | // |
| 539 | // free(x); |
| 540 | // free(qx); |
| 541 | // free(qx_res); |
| 542 | // } |
| 543 | |
| 544 | void ggml_vk_test_dequant_matmul(ggml_backend_vk_context * ctx, size_t m, size_t n, size_t k, size_t batch, size_t num_it, size_t split_k, size_t shader_size, ggml_type quant, bool mmq) { |
| 545 | VK_LOG_DEBUG("ggml_vk_test_dequant_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << ggml_type_name(quant) << ")"); |
| 546 | const size_t x_ne = m * k * batch; |
| 547 | const size_t y_ne = k * n * batch; |
| 548 | const size_t d_ne = m * n * batch; |
| 549 | |
| 550 | ggml_type b_type = mmq ? GGML_TYPE_Q8_1 : GGML_TYPE_F32; |
| 551 | bool f16acc = ctx->device->fp16 && !mmq; |
| 552 | vk_matmul_pipeline_key dq_key{quant, b_type, false, f16acc}; |
| 553 | auto dq_it = ctx->device->pipeline_matmul.find(dq_key); |
| 554 | if (dq_it == ctx->device->pipeline_matmul.end() || dq_it->second.empty()) { |
| 555 | if (f16acc) { |
| 556 | dq_key.f16acc = false; |
| 557 | dq_it = ctx->device->pipeline_matmul.find(dq_key); |
| 558 | } |
| 559 | } |
| 560 | if (dq_it == ctx->device->pipeline_matmul.end() || dq_it->second.empty()) { |
| 561 | std::cerr << "error: no pipeline for ggml_vk_test_dequant_matmul " << ggml_type_name(quant) << std::endl; |
| 562 | return; |
| 563 | } |
| 564 | auto& dq_configs = dq_it->second; |
| 565 | if (shader_size >= (int)dq_configs.size()) { |
| 566 | std::cerr << "error: shader_size " << shader_size << " >= configs.size() " << dq_configs.size() << " for " << ggml_type_name(quant) << std::endl; |
| 567 | return; |
| 568 | } |
| 569 | |
| 570 | std::string shname = std::string(ggml_type_name(quant)) + "_ALIGNED_" + std::to_string(shader_size); |
| 571 | vk_pipeline p = dq_configs[shader_size].aligned ? dq_configs[shader_size].aligned : dq_configs[shader_size].unaligned; |
| 572 | |
| 573 | const size_t kpad = mmq ? 0 : ggml_vk_align_size(k, dq_configs[shader_size].align); |
| 574 | |
| 575 | if (mmq || k != kpad) { |
| 576 | p = dq_configs[shader_size].unaligned; |
| 577 | shname = std::string(ggml_type_name(quant)) + "_" + std::to_string(shader_size); |
| 578 | } |
| 579 | |
| 580 | if (p == nullptr) { |
| 581 | std::cerr << "error: no pipeline for ggml_vk_test_dequant_matmul " << ggml_type_name(quant) << std::endl; |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | const size_t x_sz = sizeof(float) * x_ne; |
| 586 | const size_t y_sz = sizeof(float) * y_ne; |
| 587 | const size_t qx_sz = x_ne * ggml_type_size(quant)/ggml_blck_size(quant); |
| 588 | const size_t qy_sz = mmq ? y_ne * ggml_type_size(GGML_TYPE_Q8_1)/ggml_blck_size(GGML_TYPE_Q8_1) : y_sz; |
| 589 | const size_t d_sz = sizeof(float) * d_ne; |
| 590 | float * x = (float *) malloc(x_sz); |
| 591 | float * y = (float *) malloc(y_sz); |
| 592 | void * qx = malloc(qx_sz); |
| 593 | vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal}); |
| 594 | vk_buffer y_buf = ggml_vk_create_buffer_check(ctx->device, y_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal}); |
| 595 | vk_buffer qy_buf = ggml_vk_create_buffer_check(ctx->device, qy_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal}); |
| 596 | vk_buffer d_buf = ggml_vk_create_buffer_check(ctx->device, d_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal}); |
| 597 | float * d = (float *) malloc(d_sz); |
| 598 | float * d_chk = (float *) malloc(d_sz); |
| 599 | |
| 600 | for (size_t i = 0; i < x_ne; i++) { |
| 601 | x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f; |
| 602 | // x[i] = (i % k == i / k) ? 1.0f : 0.0f; |
| 603 | // x[i] = i % k; |
| 604 | } |
| 605 | |
| 606 | ggml_vk_quantize_data(x, qx, x_ne, quant); |
| 607 | |
| 608 | for (size_t i = 0; i < y_ne; i++) { |
| 609 | y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f; |
| 610 | // y[i] = (i % k == i / k) ? 1.0f : 0.0f; |
| 611 | // y[i] = i % k; |
| 612 | } |
| 613 | |
| 614 | if (split_k > 1) { |
| 615 | ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it); |
| 616 | |
| 617 | if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) { |
| 618 | // Resize buffer |
| 619 | if (ctx->prealloc_split_k != nullptr) { |
| 620 | ggml_vk_destroy_buffer(ctx->prealloc_split_k); |
| 621 | } |
| 622 | ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, {vk::MemoryPropertyFlagBits::eDeviceLocal}); |
| 623 | } |
| 624 | } |
| 625 | if (mmq) { |
| 626 | vk_pipeline pipeline_quantize_q8_1 = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1); |
| 627 | ggml_pipeline_request_descriptor_sets(ctx, pipeline_quantize_q8_1, num_it); |
| 628 | } |
| 629 | |
| 630 | ggml_pipeline_allocate_descriptor_sets(ctx); |
| 631 | |
| 632 | ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz); |
| 633 | ggml_vk_buffer_write(y_buf, 0, y, y_sz); |
| 634 | |
| 635 | vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool); |
| 636 | ggml_vk_ctx_begin(ctx->device, subctx); |
| 637 | if (mmq) { |
| 638 | for (size_t i = 0; i < num_it; i++) { |
| 639 | ggml_vk_quantize_q8_1(ctx, subctx, { y_buf, 0, y_sz }, { qy_buf, 0, qy_sz }, y_ne); |
| 640 | ggml_vk_matmul( |
| 641 | ctx, subctx, p, { qx_buf, 0, qx_sz }, { qy_buf, 0, qy_sz }, { d_buf, 0, d_sz }, { ctx->prealloc_split_k, 0, ctx->prealloc_size_split_k }, |
| 642 | m, n, k, |
| 643 | k, k, m, k*m, k*n, m*n, |
| 644 | split_k, batch, batch, batch, 1, 1, n, |
| 645 | (uint64_t)(k / ggml_blck_size(GGML_TYPE_Q8_1)) * ggml_type_size(GGML_TYPE_Q8_1), m * sizeof(float) |
| 646 | ); |
| 647 | } |
| 648 | } else { |
| 649 | for (size_t i = 0; i < num_it; i++) { |
| 650 | ggml_vk_matmul( |
| 651 | ctx, subctx, p, { qx_buf, 0, qx_sz }, { y_buf, 0, y_sz }, { d_buf, 0, d_sz }, { ctx->prealloc_split_k, 0, ctx->prealloc_size_split_k }, |
| 652 | m, n, k, |
| 653 | k, k, m, k*m, k*n, m*n, |
| 654 | split_k, batch, batch, batch, 1, 1, n, |
| 655 | k * sizeof(float), m * sizeof(float) |
| 656 | ); |
| 657 | } |
| 658 | } |
| 659 | ggml_vk_ctx_end(subctx); |
| 660 | |
| 661 | auto begin = std::chrono::high_resolution_clock::now(); |
| 662 | |
| 663 | ggml_vk_submit(subctx, ctx->fence); |
| 664 | VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences", ctx->device); |
| 665 | ctx->device->device.resetFences({ ctx->fence }); |
| 666 | ggml_vk_queue_command_pools_cleanup(ctx->device); |
| 667 | |
| 668 | auto end = std::chrono::high_resolution_clock::now(); |
| 669 | |
| 670 | double time_ms = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0; |
| 671 | ggml_vk_buffer_read(d_buf, 0, d, d_sz); |
| 672 | |
| 673 | ggml_init_params iparams = { |
| 674 | /*.mem_size =*/ 1024*1024*1024, |
| 675 | /*.mem_buffer =*/ NULL, |
| 676 | /*.no_alloc =*/ true, |
| 677 | }; |
| 678 | |
| 679 | ggml_context * ggml_ctx = ggml_init(iparams); |
| 680 | |
| 681 | ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, quant, k, m, batch); |
| 682 | ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, GGML_TYPE_F32, k, n, batch); |
| 683 | ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml); |
| 684 | |
| 685 | src0_ggml->data = qx; |
| 686 | src1_ggml->data = y; |
| 687 | tensor_ggml->data = d_chk; |
| 688 | |
| 689 | ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx); |
| 690 | ggml_build_forward_expand(cgraph, tensor_ggml); |
| 691 | |
| 692 | ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1); |
| 693 | |
| 694 | ggml_free(ggml_ctx); |
| 695 | |
| 696 | double avg_err = 0.0; |
| 697 | int first_err_n = -1; |
| 698 | int first_err_m = -1; |
| 699 | int first_err_b = -1; |
| 700 | |
| 701 | for (size_t i = 0; i < m*n*batch; i++) { |
| 702 | double err = std::fabs(d[i] - d_chk[i]); |
| 703 | avg_err += err; |
| 704 | |
| 705 | if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) { |
| 706 | first_err_b = i / (m * n); |
| 707 | first_err_n = (i % (m * n)) / m; |
| 708 | first_err_m = (i % (m * n)) % m; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | avg_err /= m * n; |
| 713 | |
| 714 | double tflops = 2.0*m*n*k*batch*num_it / (time_ms / 1000.0) / (1000.0*1000.0*1000.0*1000.0); |
| 715 | |
| 716 | std::cerr << "TEST dequant matmul " << shname; |
| 717 | if (mmq) { |
| 718 | std::cerr << " mmq"; |
| 719 | } |
| 720 | std::cerr << " m=" << m << " n=" << n << " k=" << k << " batch=" << batch << " split_k=" << split_k << " matmul " << time_ms / num_it << "ms " << tflops << " TFLOPS avg_err=" << avg_err << std::endl; |
| 721 | |
| 722 | if (avg_err > 0.01 || std::isnan(avg_err)) { |
| 723 | std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl; |
| 724 | std::cerr << "Actual result: " << std::endl << std::endl; |
| 725 | ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b); |
| 726 | std::cerr << std::endl; |
| 727 | std::cerr << "Expected result: " << std::endl << std::endl; |
| 728 | ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b); |
| 729 | |
| 730 | std::cerr << "src0: " << std::endl << std::endl; |
| 731 | ggml_vk_print_matrix_area(x, GGML_TYPE_F32, k, m, first_err_m, first_err_n, first_err_b); |
| 732 | std::cerr << std::endl; |
| 733 | std::cerr << "src1: " << std::endl << std::endl; |
| 734 | ggml_vk_print_matrix_area(y, GGML_TYPE_F32, k, n, first_err_m, first_err_n, first_err_b); |
| 735 | |
| 736 | if (split_k > 1) { |
| 737 | float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k); |
| 738 | ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k); |
| 739 | |
| 740 | std::cerr << "d_buf0: " << std::endl << std::endl; |
| 741 | ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b); |
| 742 | |
| 743 | std::cerr << "d_buf1: " << std::endl << std::endl; |
| 744 | ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b); |
| 745 | |
| 746 | std::cerr << "d_buf2: " << std::endl << std::endl; |
| 747 | ggml_vk_print_matrix_area(split_k_buf + 2 * d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b); |
| 748 | |
| 749 | std::cerr << "d_buf3: " << std::endl << std::endl; |
| 750 | ggml_vk_print_matrix_area(split_k_buf + 3 * d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b); |
| 751 | |
| 752 | free(split_k_buf); |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | ggml_vk_destroy_buffer(qx_buf); |
| 757 | ggml_vk_destroy_buffer(y_buf); |
| 758 | ggml_vk_destroy_buffer(qy_buf); |
| 759 | ggml_vk_destroy_buffer(d_buf); |
| 760 | |
| 761 | free(x); |
| 762 | free(qx); |
| 763 | free(y); |
| 764 | free(d); |
| 765 | free(d_chk); |
| 766 | } |
| 767 | #endif |
| 768 | |
| 769 | int64_t ggml_vk_get_op_batch_size(const ggml_tensor * op) { |
| 770 | switch (op->op) { |
| 771 | case GGML_OP_GET_ROWS: |
| 772 | return 0; |
| 773 | case GGML_OP_MUL_MAT: |
| 774 | return op->ne[1]; |
| 775 | case GGML_OP_MUL_MAT_ID: |
| 776 | case GGML_OP_ROPE: |
| 777 | case GGML_OP_ROPE_BACK: |
| 778 | return op->ne[2]; |
| 779 | default: |
| 780 | return ggml_nrows(op); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | #ifdef GGML_VULKAN_CHECK_RESULTS |
| 785 | static void ggml_vk_print_graph_origin(const ggml_tensor * tensor, std::vector<const ggml_tensor *>& done, int level = 0) { |
| 786 | if (std::find(done.begin(), done.end(), tensor) != done.end() || level > 10) { |
| 787 | return; |
| 788 | } |
| 789 | for (int j = 0; j < level; j++) { |
| 790 | std::cerr << " "; |
| 791 | } |
| 792 | std::cerr << ggml_op_name(tensor->op) << " gpu=" << (tensor->extra != nullptr) << std::endl; |
| 793 | |
| 794 | done.push_back(tensor); |
| 795 | |
| 796 | for (int i = 0; i < GGML_MAX_SRC; i++) { |
| 797 | if (tensor->src[i] != nullptr) { |
| 798 | ggml_vk_print_graph_origin(tensor->src[i], done, level + 1); |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, const void * data, int i0, int i1, int i2, int i3) { |
| 804 | if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16 && tensor->type != GGML_TYPE_I32) { |
| 805 | return; |
| 806 | } |
| 807 | i0 = std::max(i0, 5); |
| 808 | i1 = std::max(i1, 5); |
| 809 | i2 = std::max(i2, 0); |
| 810 | i3 = std::max(i3, 0); |
| 811 | fprintf(stderr, " "); |
| 812 | for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) { |
| 813 | fprintf(stderr, "%7d ", idx1); |
| 814 | } |
| 815 | fprintf(stderr, "\n"); |
| 816 | for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) { |
| 817 | fprintf(stderr, "%7d: ", idx0); |
| 818 | for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) { |
| 819 | if (idx0 >= 0 && idx0 < tensor->ne[0] && idx1 >= 0 && idx1 < tensor->ne[1] && i2 >= 0 && i2 < tensor->ne[2] && i3 >= 0 && i3 < tensor->ne[3]) { |
| 820 | float val; |
| 821 | if (tensor->type == GGML_TYPE_F32) { |
| 822 | val = *(const float *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]); |
| 823 | } else if (tensor->type == GGML_TYPE_F16) { |
| 824 | val = ggml_fp16_to_fp32(*(const ggml_fp16_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0])); |
| 825 | } else if (tensor->type == GGML_TYPE_I32) { |
| 826 | val = *(const int32_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]); |
| 827 | } else { |
| 828 | GGML_ABORT("fatal error"); |
| 829 | } |
| 830 | fprintf(stderr, "% 7.2f ", val); |
| 831 | } else { |
| 832 | fprintf(stderr, " "); |
| 833 | } |
| 834 | } |
| 835 | fprintf(stderr, "\n"); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name) { |
| 840 | void * tensor_data = tensor->data; |
| 841 | |
| 842 | const bool is_gpu = tensor->buffer != nullptr && ggml_backend_buffer_is_vk(tensor->buffer); |
| 843 | |
| 844 | if (is_gpu) { |
| 845 | const size_t tensor_size = ggml_nbytes(tensor); |
| 846 | tensor_data = malloc(tensor_size); |
| 847 | |
| 848 | ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context; |
| 849 | |
| 850 | vk_buffer buffer_gpu = buf_ctx->dev_buffer; |
| 851 | ggml_vk_buffer_read(buffer_gpu, vk_tensor_offset(tensor) + tensor->view_offs, tensor_data, tensor_size); |
| 852 | } |
| 853 | |
| 854 | std::cerr << "TENSOR CHECK " << name << " (" << tensor->name << "): " << ggml_op_name(tensor->op) << std::endl; |
| 855 | std::cerr << "tensor=" << tensor << " tensor->type: " << ggml_type_name(tensor->type) << " ne0=" << tensor->ne[0] << " nb0=" << tensor->nb[0] << " ne1=" << tensor->ne[1] << " nb1=" << tensor->nb[1] << " ne2=" << tensor->ne[2] << " nb2=" << tensor->nb[2] << " ne3=" << tensor->ne[3] << " nb3=" << tensor->nb[3] << std::endl; |
| 856 | if (tensor->src[0] != nullptr) { |
| 857 | std::cerr << "tensor->src[0]=" << tensor->src[0] << " name=" << tensor->src[0]->name << " op=" << ggml_op_name(tensor->src[0]->op) << " type=" << ggml_type_name(tensor->src[0]->type) << " ne0=" << tensor->src[0]->ne[0] << " nb0=" << tensor->src[0]->nb[0] << " ne1=" << tensor->src[0]->ne[1] << " nb1=" << tensor->src[0]->nb[1] << " ne2=" << tensor->src[0]->ne[2] << " nb2=" << tensor->src[0]->nb[2] << " ne3=" << tensor->src[0]->ne[3] << " nb3=" << tensor->src[0]->nb[3] << std::endl; |
| 858 | } |
| 859 | if (tensor->src[1] != nullptr) { |
| 860 | std::cerr << "tensor->src[1]=" << tensor->src[1] << " name=" << tensor->src[1]->name << " op=" << ggml_op_name(tensor->src[1]->op) << " type=" << ggml_type_name(tensor->src[1]->type) << " ne0=" << tensor->src[1]->ne[0] << " nb0=" << tensor->src[1]->nb[0] << " ne1=" << tensor->src[1]->ne[1] << " nb1=" << tensor->src[1]->nb[1] << " ne2=" << tensor->src[1]->ne[2] << " nb2=" << tensor->src[1]->nb[2] << " ne3=" << tensor->src[1]->ne[3] << " nb3=" << tensor->src[1]->nb[3] << std::endl; |
| 861 | } |
| 862 | std::cerr << std::endl << "Result:" << std::endl; |
| 863 | ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0); |
| 864 | std::cerr << std::endl; |
| 865 | std::vector<const ggml_tensor *> done; |
| 866 | ggml_vk_print_graph_origin(tensor, done); |
| 867 | |
| 868 | if (is_gpu) { |
| 869 | free(tensor_data); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | void * comp_result; |
| 874 | size_t comp_size; |
| 875 | size_t comp_nb[GGML_MAX_DIMS]; |
| 876 | size_t check_counter = 0; |
| 877 | void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) { |
| 878 | ggml_tensor * tensor = cgraph->nodes[tensor_idx + ctx->num_additional_fused_ops]; |
| 879 | if (tensor->op == GGML_OP_TRANSPOSE || tensor->op == GGML_OP_SET_ROWS) { |
| 880 | return; |
| 881 | } |
| 882 | |
| 883 | check_counter++; |
| 884 | if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) { |
| 885 | return; |
| 886 | } |
| 887 | |
| 888 | VK_LOG_DEBUG("ggml_vk_check_results_0(" << tensor->name << ")"); |
| 889 | |
| 890 | struct ggml_init_params iparams = { |
| 891 | /*.mem_size =*/ 2ul*1024ul*1024ul*1024ul, |
| 892 | /*.mem_buffer =*/ NULL, |
| 893 | /*.no_alloc =*/ false, |
| 894 | }; |
| 895 | |
| 896 | struct ggml_context * ggml_ctx = ggml_init(iparams); |
| 897 | |
| 898 | std::array<struct ggml_tensor *, GGML_MAX_SRC> src_clone = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr}; |
| 899 | const char * srci_name[GGML_MAX_SRC] = {"src0", "src1", "src2", "src3", "src4", "src5", "src6", "src7", "src8", "src9"}; |
| 900 | |
| 901 | std::map<ggml_tensor *, ggml_tensor *> cloned_tensors; |
| 902 | std::vector<void *> cloned_mallocs; |
| 903 | |
| 904 | struct ggml_tensor * tensor_clone = nullptr; |
| 905 | |
| 906 | for (int f = 0; f < ctx->num_additional_fused_ops + 1; ++f) { |
| 907 | tensor = cgraph->nodes[tensor_idx + f]; |
| 908 | for (int i = 0; i < GGML_MAX_SRC; i++) { |
| 909 | ggml_tensor * srci = tensor->src[i]; |
| 910 | if (srci == nullptr) { |
| 911 | continue; |
| 912 | } |
| 913 | // If a src tensor has been cloned, use that one |
| 914 | auto it = cloned_tensors.find(srci); |
| 915 | if (it != cloned_tensors.end()) { |
| 916 | src_clone[i] = it->second; |
| 917 | continue; |
| 918 | } |
| 919 | ggml_tensor * srci_clone = ggml_dup_tensor(ggml_ctx, srci); |
| 920 | size_t srci_size = ggml_nbytes(srci); |
| 921 | |
| 922 | src_clone[i] = srci_clone; |
| 923 | void *src_buffer = malloc(srci_size); |
| 924 | cloned_mallocs.push_back(src_buffer); |
| 925 | |
| 926 | srci_clone->data = src_buffer; |
| 927 | if (ggml_backend_buffer_is_host(srci->buffer)) { |
| 928 | memcpy(srci_clone->data, srci->data, srci_size); |
| 929 | memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS); |
| 930 | } else if (ggml_backend_buffer_is_vk(srci->buffer)) { |
| 931 | ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)srci->buffer->context; |
| 932 | vk_buffer& buffer_gpu = buf_ctx->dev_buffer; |
| 933 | uint64_t offset = vk_tensor_offset(srci) + srci->view_offs; |
| 934 | if (!ggml_is_contiguous(srci) && ggml_vk_dim01_contiguous(srci)) { |
| 935 | for (int i3 = 0; i3 < srci->ne[3]; i3++) { |
| 936 | for (int i2 = 0; i2 < srci->ne[2]; i2++) { |
| 937 | const int idx = i3*srci->ne[2] + i2; |
| 938 | ggml_vk_buffer_read(buffer_gpu, offset + idx * srci->nb[2], ((char *)srci_clone->data + idx * srci_clone->nb[2]), srci->ne[1] * srci->nb[1]); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | srci_clone->nb[0] = srci->nb[0]; |
| 943 | srci_clone->nb[1] = srci->nb[1]; |
| 944 | for (int i = 2; i < GGML_MAX_DIMS; i++) { |
| 945 | srci_clone->nb[i] = srci_clone->nb[i - 1]*srci_clone->ne[i - 1]; |
| 946 | } |
| 947 | } else { |
| 948 | if (offset + srci_size >= buffer_gpu->size) { |
| 949 | srci_size = buffer_gpu->size - offset; |
| 950 | } |
| 951 | ggml_vk_buffer_read(buffer_gpu, offset, srci_clone->data, srci_size); |
| 952 | memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS); |
| 953 | } |
| 954 | } else { |
| 955 | GGML_ABORT("fatal error"); |
| 956 | } |
| 957 | |
| 958 | if (vk_output_tensor > 0 && vk_output_tensor == check_counter) { |
| 959 | ggml_vk_print_tensor(srci, srci_name[i]); |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | if (tensor->op == GGML_OP_FLASH_ATTN_EXT) { |
| 964 | const float * params = (const float *)tensor->op_params; |
| 965 | tensor_clone = ggml_flash_attn_ext(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3], params[0], params[1], params[2]); |
| 966 | if (src_clone[4]) { |
| 967 | ggml_flash_attn_ext_add_sinks(tensor_clone, src_clone[4]); |
| 968 | } |
| 969 | } else if (tensor->op == GGML_OP_MUL_MAT) { |
| 970 | tensor_clone = ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]); |
| 971 | } else if (tensor->op == GGML_OP_MUL_MAT_ID) { |
| 972 | tensor_clone = ggml_mul_mat_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]); |
| 973 | } else if (tensor->op == GGML_OP_SUB) { |
| 974 | tensor_clone = ggml_sub(ggml_ctx, src_clone[0], src_clone[1]); |
| 975 | } else if (tensor->op == GGML_OP_MUL) { |
| 976 | tensor_clone = ggml_mul(ggml_ctx, src_clone[0], src_clone[1]); |
| 977 | } else if (tensor->op == GGML_OP_DIV) { |
| 978 | tensor_clone = ggml_div(ggml_ctx, src_clone[0], src_clone[1]); |
| 979 | } else if (tensor->op == GGML_OP_CONCAT) { |
| 980 | tensor_clone = ggml_concat(ggml_ctx, src_clone[0], src_clone[1], *(int *)tensor->op_params); |
| 981 | } else if (tensor->op == GGML_OP_UPSCALE) { |
| 982 | tensor_clone = ggml_interpolate(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3], (ggml_scale_mode) tensor->op_params[0]); |
| 983 | } else if (tensor->op == GGML_OP_SCALE) { |
| 984 | const float * params = (const float *)tensor->op_params; |
| 985 | tensor_clone = ggml_scale_bias(ggml_ctx, src_clone[0], params[0], params[1]); |
| 986 | } else if (tensor->op == GGML_OP_ADD1) { |
| 987 | tensor_clone = ggml_add1(ggml_ctx, src_clone[0], src_clone[1]); |
| 988 | } else if (tensor->op == GGML_OP_ARANGE) { |
| 989 | const float start = ggml_get_op_params_f32(tensor, 0); |
| 990 | const float stop = ggml_get_op_params_f32(tensor, 1); |
| 991 | const float step = ggml_get_op_params_f32(tensor, 2); |
| 992 | tensor_clone = ggml_arange(ggml_ctx, start, stop, step); |
| 993 | } else if (tensor->op == GGML_OP_FILL) { |
| 994 | const float value = ggml_get_op_params_f32(tensor, 0); |
| 995 | tensor_clone = ggml_fill(ggml_ctx, src_clone[0], value); |
| 996 | } else if (tensor->op == GGML_OP_SQR) { |
| 997 | tensor_clone = ggml_sqr(ggml_ctx, src_clone[0]); |
| 998 | } else if (tensor->op == GGML_OP_SQRT) { |
| 999 | tensor_clone = ggml_sqrt(ggml_ctx, src_clone[0]); |
| 1000 | } else if (tensor->op == GGML_OP_SIN) { |
| 1001 | tensor_clone = ggml_sin(ggml_ctx, src_clone[0]); |
| 1002 | } else if (tensor->op == GGML_OP_COS) { |
| 1003 | tensor_clone = ggml_cos(ggml_ctx, src_clone[0]); |
| 1004 | } else if (tensor->op == GGML_OP_LOG) { |
| 1005 | tensor_clone = ggml_log(ggml_ctx, src_clone[0]); |
| 1006 | } else if (tensor->op == GGML_OP_TRI) { |
| 1007 | tensor_clone = ggml_tri(ggml_ctx, src_clone[0], (ggml_tri_type)ggml_get_op_params_i32(tensor, 0)); |
| 1008 | } else if (tensor->op == GGML_OP_DIAG) { |
| 1009 | tensor_clone = ggml_diag(ggml_ctx, src_clone[0]); |
| 1010 | } else if (tensor->op == GGML_OP_CLAMP) { |
| 1011 | const float * params = (const float *)tensor->op_params; |
| 1012 | tensor_clone = ggml_clamp(ggml_ctx, src_clone[0], params[0], params[1]); |
| 1013 | } else if (tensor->op == GGML_OP_PAD) { |
| 1014 | tensor_clone = ggml_pad_ext(ggml_ctx, src_clone[0], tensor->op_params[0], tensor->op_params[1], tensor->op_params[2], tensor->op_params[3], |
| 1015 | tensor->op_params[4], tensor->op_params[5], tensor->op_params[6], tensor->op_params[7]); |
| 1016 | } else if (tensor->op == GGML_OP_PAD_REFLECT_1D) { |
| 1017 | tensor_clone = ggml_pad_reflect_1d(ggml_ctx, src_clone[0], tensor->op_params[0], tensor->op_params[1]); |
| 1018 | } else if (tensor->op == GGML_OP_REPEAT) { |
| 1019 | tensor_clone = ggml_repeat(ggml_ctx, src_clone[0], tensor); |
| 1020 | } else if (tensor->op == GGML_OP_REPEAT_BACK) { |
| 1021 | tensor_clone = ggml_repeat_back(ggml_ctx, src_clone[0], tensor); |
| 1022 | } else if (tensor->op == GGML_OP_ADD) { |
| 1023 | tensor_clone = ggml_add(ggml_ctx, src_clone[0], src_clone[1]); |
| 1024 | } else if (tensor->op == GGML_OP_ACC) { |
| 1025 | tensor_clone = ggml_acc(ggml_ctx, src_clone[0], src_clone[1], tensor->op_params[0], tensor->op_params[1], tensor->op_params[2], tensor->op_params[3]); |
| 1026 | } else if (tensor->op == GGML_OP_SET) { |
| 1027 | tensor_clone = ggml_set(ggml_ctx, src_clone[0], src_clone[1], tensor->op_params[0], tensor->op_params[1], tensor->op_params[2], tensor->op_params[3]); |
| 1028 | } else if (tensor->op == GGML_OP_NORM) { |
| 1029 | tensor_clone = ggml_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params); |
| 1030 | } else if (tensor->op == GGML_OP_GROUP_NORM) { |
| 1031 | const float * float_params = (const float *)tensor->op_params; |
| 1032 | tensor_clone = ggml_group_norm(ggml_ctx, src_clone[0], tensor->op_params[0], float_params[1]); |
| 1033 | } else if (tensor->op == GGML_OP_RMS_NORM) { |
| 1034 | tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params); |
| 1035 | } else if (tensor->op == GGML_OP_RMS_NORM_BACK) { |
| 1036 | const float eps = ((float *) tensor->op_params)[0]; |
| 1037 | tensor_clone = ggml_rms_norm_back(ggml_ctx, src_clone[0], src_clone[1], eps); |
| 1038 | } else if (tensor->op == GGML_OP_SILU_BACK) { |
| 1039 | tensor_clone = ggml_silu_back(ggml_ctx, src_clone[0], src_clone[1]); |
| 1040 | } else if (tensor->op == GGML_OP_L2_NORM) { |
| 1041 | const float eps = ((float *) tensor->op_params)[0]; |
| 1042 | tensor_clone = ggml_l2_norm(ggml_ctx, src_clone[0], eps); |
| 1043 | } else if (tensor->op == GGML_OP_SOFT_MAX) { |
| 1044 | if (tensor->src[1] != nullptr) { |
| 1045 | const float * params = (const float *)tensor->op_params; |
| 1046 | tensor_clone = ggml_soft_max_ext(ggml_ctx, src_clone[0], src_clone[1], params[0], params[1]); |
| 1047 | } else { |
| 1048 | tensor_clone = ggml_soft_max(ggml_ctx, src_clone[0]); |
| 1049 | } |
| 1050 | } else if (tensor->op == GGML_OP_SOFT_MAX_BACK) { |
| 1051 | tensor_clone = ggml_soft_max_ext_back(ggml_ctx, src_clone[0], src_clone[1], ((float *)tensor->op_params)[0], ((float *)tensor->op_params)[1]); |
| 1052 | } else if (tensor->op == GGML_OP_DIAG_MASK_INF) { |
| 1053 | tensor_clone = ggml_diag_mask_inf(ggml_ctx, src_clone[0], tensor->op_params[0]); |
| 1054 | } else if (tensor->op == GGML_OP_ROPE || tensor->op == GGML_OP_ROPE_BACK) { |
| 1055 | const int n_dims = ((int32_t *) tensor->op_params)[1]; |
| 1056 | const int mode = ((int32_t *) tensor->op_params)[2]; |
| 1057 | //const int n_ctx_ggml = ((int32_t *) tensor->op_params)[3]; |
| 1058 | const int n_ctx_orig_ggml = ((int32_t *) tensor->op_params)[4]; |
| 1059 | const float freq_base = ((float *) tensor->op_params)[5]; |
| 1060 | const float freq_scale = ((float *) tensor->op_params)[6]; |
| 1061 | const float ext_factor = ((float *) tensor->op_params)[7]; |
| 1062 | const float attn_factor = ((float *) tensor->op_params)[8]; |
| 1063 | const float beta_fast = ((float *) tensor->op_params)[9]; |
| 1064 | const float beta_slow = ((float *) tensor->op_params)[10]; |
| 1065 | if (mode & GGML_ROPE_TYPE_MROPE) { |
| 1066 | int32_t *sections = ((int32_t *) tensor->op_params) + 11; |
| 1067 | if (tensor->op == GGML_OP_ROPE) { |
| 1068 | tensor_clone = ggml_rope_multi(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, sections, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow); |
| 1069 | } else { |
| 1070 | tensor_clone = ggml_rope_multi_back(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, sections, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow); |
| 1071 | } |
| 1072 | } else { |
| 1073 | if (tensor->op == GGML_OP_ROPE) { |
| 1074 | tensor_clone = ggml_rope_ext(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow); |
| 1075 | } else { |
| 1076 | tensor_clone = ggml_rope_ext_back(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow); |
| 1077 | } |
| 1078 | } |
| 1079 | const int n_offs = ((int32_t *) tensor->op_params)[15]; |
| 1080 | if (n_offs != 0) { |
| 1081 | tensor_clone = ggml_rope_set_offset(tensor_clone, n_offs); |
| 1082 | } |
| 1083 | } else if (tensor->op == GGML_OP_UNARY) { |
| 1084 | switch (ggml_get_unary_op(tensor)) { |
| 1085 | case GGML_UNARY_OP_EXP: |
| 1086 | tensor_clone = ggml_exp(ggml_ctx, src_clone[0]); |
| 1087 | break; |
| 1088 | case GGML_UNARY_OP_EXPM1: |
| 1089 | tensor_clone = ggml_expm1(ggml_ctx, src_clone[0]); |
| 1090 | break; |
| 1091 | case GGML_UNARY_OP_ELU: |
| 1092 | tensor_clone = ggml_elu(ggml_ctx, src_clone[0]); |
| 1093 | break; |
| 1094 | case GGML_UNARY_OP_SILU: |
| 1095 | tensor_clone = ggml_silu(ggml_ctx, src_clone[0]); |
| 1096 | break; |
| 1097 | case GGML_UNARY_OP_GELU: |
| 1098 | tensor_clone = ggml_gelu(ggml_ctx, src_clone[0]); |
| 1099 | break; |
| 1100 | case GGML_UNARY_OP_GELU_ERF: |
| 1101 | tensor_clone = ggml_gelu_erf(ggml_ctx, src_clone[0]); |
| 1102 | break; |
| 1103 | case GGML_UNARY_OP_GELU_QUICK: |
| 1104 | tensor_clone = ggml_gelu_quick(ggml_ctx, src_clone[0]); |
| 1105 | break; |
| 1106 | case GGML_UNARY_OP_RELU: |
| 1107 | tensor_clone = ggml_relu(ggml_ctx, src_clone[0]); |
| 1108 | break; |
| 1109 | case GGML_UNARY_OP_XIELU: |
| 1110 | tensor_clone = ggml_xielu(ggml_ctx, src_clone[0], 0, 0, 0, 0); |
| 1111 | ggml_set_op_params_f32(tensor_clone, 1, ggml_get_op_params_f32(tensor, 1)); |
| 1112 | ggml_set_op_params_f32(tensor_clone, 2, ggml_get_op_params_f32(tensor, 2)); |
| 1113 | ggml_set_op_params_f32(tensor_clone, 3, ggml_get_op_params_f32(tensor, 3)); |
| 1114 | ggml_set_op_params_f32(tensor_clone, 4, ggml_get_op_params_f32(tensor, 4)); |
| 1115 | break; |
| 1116 | case GGML_UNARY_OP_NEG: |
| 1117 | tensor_clone = ggml_neg(ggml_ctx, src_clone[0]); |
| 1118 | break; |
| 1119 | case GGML_UNARY_OP_TANH: |
| 1120 | tensor_clone = ggml_tanh(ggml_ctx, src_clone[0]); |
| 1121 | break; |
| 1122 | case GGML_UNARY_OP_SIGMOID: |
| 1123 | tensor_clone = ggml_sigmoid(ggml_ctx, src_clone[0]); |
| 1124 | break; |
| 1125 | case GGML_UNARY_OP_HARDSIGMOID: |
| 1126 | tensor_clone = ggml_hardsigmoid(ggml_ctx, src_clone[0]); |
| 1127 | break; |
| 1128 | case GGML_UNARY_OP_HARDSWISH: |
| 1129 | tensor_clone = ggml_hardswish(ggml_ctx, src_clone[0]); |
| 1130 | break; |
| 1131 | case GGML_UNARY_OP_ABS: |
| 1132 | tensor_clone = ggml_abs(ggml_ctx, src_clone[0]); |
| 1133 | break; |
| 1134 | case GGML_UNARY_OP_SOFTPLUS: |
| 1135 | tensor_clone = ggml_softplus(ggml_ctx, src_clone[0]); |
| 1136 | break; |
| 1137 | case GGML_UNARY_OP_STEP: |
| 1138 | tensor_clone = ggml_step(ggml_ctx, src_clone[0]); |
| 1139 | break; |
| 1140 | case GGML_UNARY_OP_ROUND: |
| 1141 | tensor_clone = ggml_round(ggml_ctx, src_clone[0]); |
| 1142 | break; |
| 1143 | case GGML_UNARY_OP_CEIL: |
| 1144 | tensor_clone = ggml_ceil(ggml_ctx, src_clone[0]); |
| 1145 | break; |
| 1146 | case GGML_UNARY_OP_FLOOR: |
| 1147 | tensor_clone = ggml_floor(ggml_ctx, src_clone[0]); |
| 1148 | break; |
| 1149 | case GGML_UNARY_OP_TRUNC: |
| 1150 | tensor_clone = ggml_trunc(ggml_ctx, src_clone[0]); |
| 1151 | break; |
| 1152 | case GGML_UNARY_OP_SGN: |
| 1153 | tensor_clone = ggml_sgn(ggml_ctx, src_clone[0]); |
| 1154 | break; |
| 1155 | default: |
| 1156 | std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl; |
| 1157 | GGML_ABORT("fatal error"); |
| 1158 | } |
| 1159 | } else if (tensor->op == GGML_OP_GLU) { |
| 1160 | if (src_clone[1] == nullptr) { |
| 1161 | tensor_clone = ggml_glu(ggml_ctx, src_clone[0], (ggml_glu_op) tensor->op_params[0], tensor->op_params[1]); |
| 1162 | } else { |
| 1163 | tensor_clone = ggml_glu_split(ggml_ctx, src_clone[0], src_clone[1], (ggml_glu_op) tensor->op_params[0]); |
| 1164 | } |
| 1165 | ggml_set_op_params_i32(tensor_clone, 2, ggml_get_op_params_i32(tensor, 2)); |
| 1166 | ggml_set_op_params_i32(tensor_clone, 3, ggml_get_op_params_i32(tensor, 3)); |
| 1167 | } else if (tensor->op == GGML_OP_CPY || tensor->op == GGML_OP_DUP) { |
| 1168 | if (tensor->src[1] == nullptr) { |
| 1169 | tensor_clone = ggml_dup(ggml_ctx, src_clone[0]); |
| 1170 | tensor_clone->type = tensor->type; |
| 1171 | } else { |
| 1172 | tensor_clone = ggml_cpy(ggml_ctx, src_clone[0], src_clone[1]); |
| 1173 | } |
| 1174 | } else if (tensor->op == GGML_OP_CONT) { |
| 1175 | tensor_clone = ggml_cont_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]); |
| 1176 | } else if (tensor->op == GGML_OP_RESHAPE) { |
| 1177 | tensor_clone = ggml_reshape_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]); |
| 1178 | } else if (tensor->op == GGML_OP_VIEW) { |
| 1179 | tensor_clone = ggml_view_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3], tensor->nb[1], tensor->nb[2], tensor->nb[3], ((int32_t *) tensor->op_params)[0]); |
| 1180 | } else if (tensor->op == GGML_OP_PERMUTE) { |
| 1181 | int32_t * params = (int32_t *)tensor->op_params; |
| 1182 | tensor_clone = ggml_permute(ggml_ctx, src_clone[0], params[0], params[1], params[2], params[3]); |
| 1183 | } else if (tensor->op == GGML_OP_TRANSPOSE) { |
| 1184 | tensor_clone = ggml_transpose(ggml_ctx, src_clone[0]); |
| 1185 | } else if (tensor->op == GGML_OP_GET_ROWS) { |
| 1186 | tensor_clone = ggml_get_rows(ggml_ctx, src_clone[0], src_clone[1]); |
| 1187 | } else if (tensor->op == GGML_OP_ARGSORT) { |
| 1188 | tensor_clone = ggml_argsort(ggml_ctx, src_clone[0], (ggml_sort_order) *(int *)tensor->op_params); |
| 1189 | } else if (tensor->op == GGML_OP_TOP_K) { |
| 1190 | tensor_clone = ggml_top_k(ggml_ctx, src_clone[0], tensor->ne[0]); |
| 1191 | } else if (tensor->op == GGML_OP_SUM) { |
| 1192 | tensor_clone = ggml_sum(ggml_ctx, src_clone[0]); |
| 1193 | } else if (tensor->op == GGML_OP_SUM_ROWS) { |
| 1194 | tensor_clone = ggml_sum_rows(ggml_ctx, src_clone[0]); |
| 1195 | } else if (tensor->op == GGML_OP_CUMSUM) { |
| 1196 | tensor_clone = ggml_cumsum(ggml_ctx, src_clone[0]); |
| 1197 | } else if (tensor->op == GGML_OP_DSV4_HC_COMB) { |
| 1198 | tensor_clone = ggml_dsv4_hc_comb(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], |
| 1199 | ggml_get_op_params_f32(tensor, 0), ggml_get_op_params_i32(tensor, 1)); |
| 1200 | } else if (tensor->op == GGML_OP_DSV4_HC_PRE) { |
| 1201 | if (ggml_get_op_params_i32(tensor, 1) != 0) { |
| 1202 | tensor_clone = ggml_dsv4_hc_pre_gated(ggml_ctx, src_clone[0], src_clone[1], ggml_get_op_params_f32(tensor, 0)); |
| 1203 | } else { |
| 1204 | tensor_clone = ggml_dsv4_hc_pre(ggml_ctx, src_clone[0], src_clone[1]); |
| 1205 | } |
| 1206 | } else if (tensor->op == GGML_OP_DSV4_HC_POST) { |
| 1207 | tensor_clone = ggml_dsv4_hc_post(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3]); |
| 1208 | } else if (tensor->op == GGML_OP_MEAN) { |
| 1209 | tensor_clone = ggml_mean(ggml_ctx, src_clone[0]); |
| 1210 | } else if (tensor->op == GGML_OP_ARGMAX) { |
| 1211 | tensor_clone = ggml_argmax(ggml_ctx, src_clone[0]); |
| 1212 | } else if (tensor->op == GGML_OP_CROSS_ENTROPY_LOSS) { |
| 1213 | tensor_clone = ggml_cross_entropy_loss(ggml_ctx, src_clone[0], src_clone[1]); |
| 1214 | } else if (tensor->op == GGML_OP_CROSS_ENTROPY_LOSS_BACK) { |
| 1215 | tensor_clone = ggml_cross_entropy_loss_back(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]); |
| 1216 | } else if (tensor->op == GGML_OP_COUNT_EQUAL) { |
| 1217 | tensor_clone = ggml_count_equal(ggml_ctx, src_clone[0], src_clone[1]); |
| 1218 | } else if (tensor->op == GGML_OP_SOLVE_TRI) { |
| 1219 | tensor_clone = ggml_solve_tri(ggml_ctx, src_clone[0], src_clone[1], true, true, false); |
| 1220 | } else if (tensor->op == GGML_OP_IM2COL) { |
| 1221 | const int32_t s0 = tensor->op_params[0]; |
| 1222 | const int32_t s1 = tensor->op_params[1]; |
| 1223 | const int32_t p0 = tensor->op_params[2]; |
| 1224 | const int32_t p1 = tensor->op_params[3]; |
| 1225 | const int32_t d0 = tensor->op_params[4]; |
| 1226 | const int32_t d1 = tensor->op_params[5]; |
| 1227 | |
| 1228 | const bool is_2D = tensor->op_params[6] == 1; |
| 1229 | tensor_clone = ggml_im2col(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1, is_2D, tensor->type); |
| 1230 | } else if (tensor->op == GGML_OP_IM2COL_3D) { |
| 1231 | const int32_t s0 = tensor->op_params[0]; |
| 1232 | const int32_t s1 = tensor->op_params[1]; |
| 1233 | const int32_t s2 = tensor->op_params[2]; |
| 1234 | const int32_t p0 = tensor->op_params[3]; |
| 1235 | const int32_t p1 = tensor->op_params[4]; |
| 1236 | const int32_t p2 = tensor->op_params[5]; |
| 1237 | const int32_t d0 = tensor->op_params[6]; |
| 1238 | const int32_t d1 = tensor->op_params[7]; |
| 1239 | const int32_t d2 = tensor->op_params[8]; |
| 1240 | const int32_t IC = tensor->op_params[9]; |
| 1241 | |
| 1242 | tensor_clone = ggml_im2col_3d(ggml_ctx, src_clone[0], src_clone[1], IC, s0, s1, s2, p0, p1, p2, d0, d1, d2, tensor->type); |
| 1243 | } else if (tensor->op == GGML_OP_TIMESTEP_EMBEDDING) { |
| 1244 | const int32_t dim = tensor->op_params[0]; |
| 1245 | const int32_t max_period = tensor->op_params[1]; |
| 1246 | tensor_clone = ggml_timestep_embedding(ggml_ctx, src_clone[0], dim, max_period); |
| 1247 | } else if (tensor->op == GGML_OP_CONV_TRANSPOSE_1D){ |
| 1248 | const int32_t s0 = tensor->op_params[0]; |
| 1249 | const int32_t p0 = tensor->op_params[1]; |
| 1250 | const int32_t d0 = tensor->op_params[2]; |
| 1251 | tensor_clone = ggml_conv_transpose_1d(ggml_ctx, src_clone[0], src_clone[1], s0, p0, d0); |
| 1252 | } else if (tensor->op == GGML_OP_COL2IM_1D) { |
| 1253 | const int32_t stride = tensor->op_params[0]; |
| 1254 | const int32_t oc = tensor->op_params[1]; |
| 1255 | const int32_t p0 = tensor->op_params[2]; |
| 1256 | tensor_clone = ggml_col2im_1d(ggml_ctx, src_clone[0], stride, oc, p0); |
| 1257 | } else if (tensor->op == GGML_OP_POOL_1D) { |
| 1258 | enum ggml_op_pool op = static_cast<ggml_op_pool>(tensor->op_params[0]); |
| 1259 | const int32_t k0 = tensor->op_params[1]; |
| 1260 | const int32_t s0 = tensor->op_params[2]; |
| 1261 | const int32_t p0 = tensor->op_params[3]; |
| 1262 | |
| 1263 | tensor_clone = ggml_pool_1d(ggml_ctx, src_clone[0], op, k0, s0, p0); |
| 1264 | } else if (tensor->op == GGML_OP_POOL_2D) { |
| 1265 | enum ggml_op_pool op = static_cast<ggml_op_pool>(tensor->op_params[0]); |
| 1266 | const int32_t k0 = tensor->op_params[1]; |
| 1267 | const int32_t k1 = tensor->op_params[2]; |
| 1268 | const int32_t s0 = tensor->op_params[3]; |
| 1269 | const int32_t s1 = tensor->op_params[4]; |
| 1270 | const int32_t p0 = tensor->op_params[5]; |
| 1271 | const int32_t p1 = tensor->op_params[6]; |
| 1272 | |
| 1273 | tensor_clone = ggml_pool_2d(ggml_ctx, src_clone[0], op, k0, k1, s0, s1, p0, p1); |
| 1274 | } else if (tensor->op == GGML_OP_CONV_2D) { |
| 1275 | const int32_t s0 = tensor->op_params[0]; |
| 1276 | const int32_t s1 = tensor->op_params[1]; |
| 1277 | const int32_t p0 = tensor->op_params[2]; |
| 1278 | const int32_t p1 = tensor->op_params[3]; |
| 1279 | const int32_t d0 = tensor->op_params[4]; |
| 1280 | const int32_t d1 = tensor->op_params[5]; |
| 1281 | tensor_clone = ggml_conv_2d(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1); |
| 1282 | } else if (tensor->op == GGML_OP_CONV_3D) { |
| 1283 | const int32_t s0 = tensor->op_params[0]; |
| 1284 | const int32_t s1 = tensor->op_params[1]; |
| 1285 | const int32_t s2 = tensor->op_params[2]; |
| 1286 | const int32_t p0 = tensor->op_params[3]; |
| 1287 | const int32_t p1 = tensor->op_params[4]; |
| 1288 | const int32_t p2 = tensor->op_params[5]; |
| 1289 | const int32_t d0 = tensor->op_params[6]; |
| 1290 | const int32_t d1 = tensor->op_params[7]; |
| 1291 | const int32_t d2 = tensor->op_params[8]; |
| 1292 | const int32_t IC = tensor->op_params[9]; |
| 1293 | const int32_t N = tensor->op_params[10]; |
| 1294 | const int32_t OC = tensor->op_params[11]; |
| 1295 | tensor_clone = ggml_conv_3d_direct(ggml_ctx, src_clone[0], src_clone[1], s0, s1, s2, p0, p1, p2, d0, d1, d2, IC, N, OC); |
| 1296 | } else if (tensor->op == GGML_OP_CONV_2D_DW) { |
| 1297 | const int32_t s0 = tensor->op_params[0]; |
| 1298 | const int32_t s1 = tensor->op_params[1]; |
| 1299 | const int32_t p0 = tensor->op_params[2]; |
| 1300 | const int32_t p1 = tensor->op_params[3]; |
| 1301 | const int32_t d0 = tensor->op_params[4]; |
| 1302 | const int32_t d1 = tensor->op_params[5]; |
| 1303 | tensor_clone = ggml_conv_2d_dw_direct(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1); |
| 1304 | } else if (tensor->op == GGML_OP_CONV_TRANSPOSE_2D) { |
| 1305 | const int32_t s = tensor->op_params[0]; |
| 1306 | tensor_clone = ggml_conv_transpose_2d_p0(ggml_ctx, src_clone[0], src_clone[1], s); |
| 1307 | } else if (tensor->op == GGML_OP_LEAKY_RELU) { |
| 1308 | const float * op_params = (const float *)tensor->op_params; |
| 1309 | tensor_clone = ggml_leaky_relu(ggml_ctx, src_clone[0], op_params[0], false); |
| 1310 | } else if (tensor->op == GGML_OP_RWKV_WKV6) { |
| 1311 | tensor_clone = ggml_rwkv_wkv6(ggml_ctx, src_clone[0], src_clone[1], |
| 1312 | src_clone[2], src_clone[3], src_clone[4], src_clone[5]); |
| 1313 | } else if (tensor->op == GGML_OP_RWKV_WKV7) { |
| 1314 | tensor_clone = ggml_rwkv_wkv7(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3], |
| 1315 | src_clone[4], src_clone[5], src_clone[6]); |
| 1316 | } else if (tensor->op == GGML_OP_GATED_LINEAR_ATTN) { |
| 1317 | const float * op_params = (const float *)tensor->op_params; |
| 1318 | tensor_clone = ggml_gated_linear_attn(ggml_ctx, src_clone[0], src_clone[1], |
| 1319 | src_clone[2], src_clone[3], src_clone[4], op_params[0]); |
| 1320 | } else if (tensor->op == GGML_OP_LIGHTNING_INDEXER) { |
| 1321 | tensor_clone = ggml_lightning_indexer(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3]); |
| 1322 | } else if (tensor->op == GGML_OP_GATED_DELTA_NET) { |
| 1323 | tensor_clone = ggml_gated_delta_net(ggml_ctx, src_clone[0], src_clone[1], |
| 1324 | src_clone[2], src_clone[3], src_clone[4], src_clone[5], |
| 1325 | ggml_get_op_params_i32(tensor, 0)); |
| 1326 | } else if (tensor->op == GGML_OP_OPT_STEP_ADAMW) { |
| 1327 | src_clone[0]->flags = tensor->src[0]->flags; |
| 1328 | tensor_clone = ggml_opt_step_adamw(ggml_ctx, src_clone[0], src_clone[1], |
| 1329 | src_clone[2], src_clone[3], src_clone[4]); |
| 1330 | } else if (tensor->op == GGML_OP_OPT_STEP_SGD) { |
| 1331 | src_clone[0]->flags = tensor->src[0]->flags; |
| 1332 | tensor_clone = ggml_opt_step_sgd(ggml_ctx, src_clone[0], src_clone[1], |
| 1333 | src_clone[2]); |
| 1334 | } else if (tensor->op == GGML_OP_ADD_ID) { |
| 1335 | tensor_clone = ggml_add_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]); |
| 1336 | } else if (tensor->op == GGML_OP_SSM_SCAN) { |
| 1337 | const int32_t K = ggml_get_op_params_i32(tensor, 0); |
| 1338 | tensor_clone = ggml_ssm_scan(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], |
| 1339 | src_clone[3], src_clone[4], src_clone[5], src_clone[6], K); |
| 1340 | } else if (tensor->op == GGML_OP_SSM_CONV) { |
| 1341 | tensor_clone = ggml_ssm_conv(ggml_ctx, src_clone[0], src_clone[1]); |
| 1342 | } else if (tensor->op == GGML_OP_ROLL) { |
| 1343 | const int32_t s0 = tensor->op_params[0]; |
| 1344 | const int32_t s1 = tensor->op_params[1]; |
| 1345 | const int32_t s2 = tensor->op_params[2]; |
| 1346 | const int32_t s3 = tensor->op_params[3]; |
| 1347 | tensor_clone = ggml_roll(ggml_ctx, src_clone[0], s0, s1, s2, s3); |
| 1348 | } |
| 1349 | else { |
| 1350 | std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl; |
| 1351 | GGML_ABORT("fatal error"); |
| 1352 | } |
| 1353 | cloned_tensors[tensor] = tensor_clone; |
| 1354 | } |
| 1355 | |
| 1356 | ggml_cgraph * cgraph_cpu = ggml_new_graph(ggml_ctx); |
| 1357 | ggml_build_forward_expand(cgraph_cpu, tensor_clone); |
| 1358 | |
| 1359 | ggml_graph_compute_with_ctx(ggml_ctx, cgraph_cpu, 8); |
| 1360 | |
| 1361 | if (vk_output_tensor > 0 && vk_output_tensor == check_counter) { |
| 1362 | ggml_vk_print_tensor(tensor_clone, "tensor_clone"); |
| 1363 | } |
| 1364 | |
| 1365 | comp_size = ggml_nbytes(tensor_clone); |
| 1366 | |
| 1367 | comp_result = malloc(comp_size); |
| 1368 | memcpy(comp_result, tensor_clone->data, comp_size); |
| 1369 | memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS); |
| 1370 | |
| 1371 | for (auto m : cloned_mallocs) { |
| 1372 | free(m); |
| 1373 | } |
| 1374 | |
| 1375 | ggml_free(ggml_ctx); |
| 1376 | |
| 1377 | VK_LOG_DEBUG("END ggml_vk_check_results_0(" << tensor->name << ")"); |
| 1378 | } |
| 1379 | |
| 1380 | void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) { |
| 1381 | ggml_tensor * tensor = cgraph->nodes[tensor_idx + ctx->num_additional_fused_ops]; |
| 1382 | if (tensor->op == GGML_OP_TRANSPOSE || tensor->op == GGML_OP_SET_ROWS) { |
| 1383 | return; |
| 1384 | } |
| 1385 | |
| 1386 | if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) { |
| 1387 | return; |
| 1388 | } |
| 1389 | |
| 1390 | VK_LOG_DEBUG("ggml_vk_check_results_1(" << tensor->name << ")"); |
| 1391 | |
| 1392 | ggml_tensor * src0 = tensor->src[0]; |
| 1393 | ggml_tensor * src1 = tensor->src[1]; |
| 1394 | ggml_tensor * src2 = tensor->src[2]; |
| 1395 | ggml_tensor * src3 = tensor->src[3]; |
| 1396 | |
| 1397 | void * tensor_data = tensor->data; |
| 1398 | |
| 1399 | if (ggml_backend_buffer_is_vk(tensor->buffer)) { |
| 1400 | size_t tensor_size = ggml_nbytes(tensor); |
| 1401 | tensor_data = malloc(tensor_size); |
| 1402 | |
| 1403 | ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context; |
| 1404 | |
| 1405 | vk_buffer& buffer_gpu = buf_ctx->dev_buffer; |
| 1406 | uint64_t offset = vk_tensor_offset(tensor) + tensor->view_offs; |
| 1407 | if (offset + tensor_size >= buffer_gpu->size) { |
| 1408 | tensor_size = buffer_gpu->size - offset; |
| 1409 | } |
| 1410 | |
| 1411 | ggml_vk_buffer_read(buffer_gpu, offset, tensor_data, tensor_size); |
| 1412 | } |
| 1413 | |
| 1414 | float first_error_result = -1.0f; |
| 1415 | float first_error_correct = -1.0f; |
| 1416 | std::array<int, 4> first_error = { -1, -1, -1, -1 }; |
| 1417 | double avg_err = 0.0; |
| 1418 | size_t counter = 0; |
| 1419 | |
| 1420 | for (int i3 = 0; i3 < tensor->ne[3]; i3++) { |
| 1421 | for (int i2 = 0; i2 < tensor->ne[2]; i2++) { |
| 1422 | for (int i1 = 0; i1 < tensor->ne[1]; i1++) { |
| 1423 | for (int i0 = 0; i0 < tensor->ne[0]; i0++) { |
| 1424 | const bool buffer_size_fit = i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0] < comp_size; |
| 1425 | float correct = 0.0f; |
| 1426 | float result = 0.0f; |
| 1427 | |
| 1428 | if (buffer_size_fit) { |
| 1429 | if (tensor->type == GGML_TYPE_F32) { |
| 1430 | correct = *(float *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]); |
| 1431 | result = *(float *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]); |
| 1432 | } else if (tensor->type == GGML_TYPE_F16) { |
| 1433 | correct = ggml_fp16_to_fp32(*(ggml_fp16_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0])); |
| 1434 | result = ggml_fp16_to_fp32(*(ggml_fp16_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0])); |
| 1435 | } else if (tensor->type == GGML_TYPE_BF16) { |
| 1436 | correct = ggml_bf16_to_fp32(*(ggml_bf16_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0])); |
| 1437 | result = ggml_bf16_to_fp32(*(ggml_bf16_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0])); |
| 1438 | } else if (tensor->type == GGML_TYPE_I32) { |
| 1439 | correct = *(int32_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]); |
| 1440 | result = *(int32_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]); |
| 1441 | } else if (tensor->type == GGML_TYPE_I64) { |
| 1442 | correct = *(int64_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]); |
| 1443 | result = *(int64_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]); |
| 1444 | } else { |
| 1445 | std::cerr << "Results check not implemented for type " << ggml_type_name(tensor->type) << std::endl; |
| 1446 | } |
| 1447 | } else { |
| 1448 | std::cerr << "Missing debug code for type " << ggml_type_name(tensor->type) << std::endl; |
| 1449 | GGML_ABORT("fatal error"); |
| 1450 | } |
| 1451 | |
| 1452 | if ((std::isnan(correct) != std::isnan(result)) || (std::isinf(correct) != std::isinf(result)) || !buffer_size_fit) { |
| 1453 | std::cerr << "ERROR: Invalid value in " << ggml_op_name(tensor->op) << " i3=" << i3 << " i2=" << i2 << " i1=" << i1 << " i0=" << i0 << " result=" << result << " correct=" << correct << " avg_err=" << (avg_err / counter) << std::endl; |
| 1454 | std::cerr << "tensor=" << tensor << " tensor->name=" << tensor->name << " tensor->type: " << ggml_type_name(tensor->type) << " ne0=" << tensor->ne[0] << " nb0=" << tensor->nb[0] << " ne1=" << tensor->ne[1] << " nb1=" << tensor->nb[1] << " ne2=" << tensor->ne[2] << " nb2=" << tensor->nb[2] << " ne3=" << tensor->ne[3] << " nb3=" << tensor->nb[3] << " offset=" << tensor->view_offs << std::endl; |
| 1455 | if (src0 != nullptr) { |
| 1456 | std::cerr << "src0=" << src0 << " src0->name=" << src0->name << " op=" << ggml_op_name(src0->op) << " type=" << ggml_type_name(src0->type) << " ne0=" << src0->ne[0] << " nb0=" << src0->nb[0] << " ne1=" << src0->ne[1] << " nb1=" << src0->nb[1] << " ne2=" << src0->ne[2] << " nb2=" << src0->nb[2] << " ne3=" << src0->ne[3] << " nb3=" << src0->nb[3] << " offset=" << src0->view_offs << std::endl; |
| 1457 | } |
| 1458 | if (src1 != nullptr) { |
| 1459 | std::cerr << "src1=" << src1 << " src1->name=" << src1->name << " op=" << ggml_op_name(src1->op) << " type=" << ggml_type_name(src1->type) << " ne0=" << src1->ne[0] << " nb0=" << src1->nb[0] << " ne1=" << src1->ne[1] << " nb1=" << src1->nb[1] << " ne2=" << src1->ne[2] << " nb2=" << src1->nb[2] << " ne3=" << src1->ne[3] << " nb3=" << src1->nb[3] << " offset=" << src1->view_offs << std::endl; |
| 1460 | } |
| 1461 | if (src2 != nullptr) { |
| 1462 | std::cerr << "src2=" << src2 << " src2->name=" << src2->name << " op=" << ggml_op_name(src2->op) << " type=" << ggml_type_name(src2->type) << " ne0=" << src2->ne[0] << " nb0=" << src2->nb[0] << " ne1=" << src2->ne[1] << " nb1=" << src2->nb[1] << " ne2=" << src2->ne[2] << " nb2=" << src2->nb[2] << " ne3=" << src2->ne[3] << " nb3=" << src2->nb[3] << " offset=" << src2->view_offs << std::endl; |
| 1463 | } |
| 1464 | if (src3 != nullptr) { |
| 1465 | std::cerr << "src3=" << src3 << " src3->name=" << src3->name << " op=" << ggml_op_name(src3->op) << " type=" << ggml_type_name(src3->type) << " ne0=" << src3->ne[0] << " nb0=" << src3->nb[0] << " ne1=" << src3->ne[1] << " nb1=" << src3->nb[1] << " ne2=" << src3->ne[2] << " nb2=" << src3->nb[2] << " ne3=" << src3->ne[3] << " nb3=" << src3->nb[3] << " offset=" << src3->view_offs << std::endl; |
| 1466 | } |
| 1467 | std::cerr << "First error: result=" << first_error_result << " correct=" << first_error_correct << " i3=" << first_error[3] << " i2=" << first_error[2] << " i1=" << first_error[1] << " i0=" << first_error[0] << std::endl; |
| 1468 | std::cerr << std::endl << "Result:" << std::endl; |
| 1469 | ggml_vk_print_tensor_area(tensor, tensor_data, i0, i1, i2, i3); |
| 1470 | std::cerr << std::endl << "Correct:" << std::endl; |
| 1471 | ggml_vk_print_tensor_area(tensor, comp_result, i0, i1, i2, i3); |
| 1472 | std::cerr << std::endl; |
| 1473 | std::vector<const ggml_tensor *> done; |
| 1474 | ggml_vk_print_graph_origin(tensor, done); |
| 1475 | GGML_ABORT("fatal error"); |
| 1476 | } |
| 1477 | const double denom = std::fabs(correct) > 1.0f ? (std::fabs(correct) > 1e-8 ? std::fabs(correct) : 1e-8) : 1.0f; |
| 1478 | if (first_error[0] == -1 && std::fabs(correct - result) / denom > 0.5) { |
| 1479 | first_error[0] = i0; |
| 1480 | first_error[1] = i1; |
| 1481 | first_error[2] = i2; |
| 1482 | first_error[3] = i3; |
| 1483 | first_error_result = result; |
| 1484 | first_error_correct = correct; |
| 1485 | } |
| 1486 | |
| 1487 | // Special case, value is infinite, avoid NaN result in avg_err |
| 1488 | // NaN also appears in results, if both are nan error is 0 |
| 1489 | if (!std::isinf(correct) && !std::isinf(result) && !std::isnan(correct) && !std::isnan(result)) { |
| 1490 | avg_err += std::fabs(correct - result) / denom; |
| 1491 | } |
| 1492 | counter++; |
| 1493 | } |
| 1494 | } |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | avg_err /= counter; |
| 1499 | |
| 1500 | if (vk_output_tensor > 0 && vk_output_tensor == check_counter) { |
| 1501 | std::cerr << "TENSOR CHECK: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl; |
| 1502 | std::cerr << "tensor=" << tensor << " tensor->name=" << tensor->name << " tensor->type: " << ggml_type_name(tensor->type) << " ne0=" << tensor->ne[0] << " nb0=" << tensor->nb[0] << " ne1=" << tensor->ne[1] << " nb1=" << tensor->nb[1] << " ne2=" << tensor->ne[2] << " nb2=" << tensor->nb[2] << " ne3=" << tensor->ne[3] << " nb3=" << tensor->nb[3] << " offset=" << tensor->view_offs << std::endl; |
| 1503 | if (src0 != nullptr) { |
| 1504 | std::cerr << "src0=" << src0 << " op=" << ggml_op_name(src0->op) << " type=" << ggml_type_name(src0->type) << " ne0=" << src0->ne[0] << " nb0=" << src0->nb[0] << " ne1=" << src0->ne[1] << " nb1=" << src0->nb[1] << " ne2=" << src0->ne[2] << " nb2=" << src0->nb[2] << " ne3=" << src0->ne[3] << " nb3=" << src0->nb[3] << " offset=" << src0->view_offs << std::endl; |
| 1505 | } |
| 1506 | if (src1 != nullptr) { |
| 1507 | std::cerr << "src1=" << src1 << " op=" << ggml_op_name(src1->op) << " type=" << ggml_type_name(src1->type) << " ne0=" << src1->ne[0] << " nb0=" << src1->nb[0] << " ne1=" << src1->ne[1] << " nb1=" << src1->nb[1] << " ne2=" << src1->ne[2] << " nb2=" << src1->nb[2] << " ne3=" << src1->ne[3] << " nb3=" << src1->nb[3] << " offset=" << src1->view_offs << std::endl; |
| 1508 | } |
| 1509 | if (src2 != nullptr) { |
| 1510 | std::cerr << "src2=" << src2 << " op=" << ggml_op_name(src2->op) << " type=" << ggml_type_name(src2->type) << " ne0=" << src2->ne[0] << " nb0=" << src2->nb[0] << " ne1=" << src2->ne[1] << " nb1=" << src2->nb[1] << " ne2=" << src2->ne[2] << " nb2=" << src2->nb[2] << " ne3=" << src2->ne[3] << " nb3=" << src2->nb[3] << " offset=" << src2->view_offs << std::endl; |
| 1511 | } |
| 1512 | if (src3 != nullptr) { |
| 1513 | std::cerr << "src3=" << src3 << " op=" << ggml_op_name(src3->op) << " type=" << ggml_type_name(src3->type) << " ne0=" << src3->ne[0] << " nb0=" << src3->nb[0] << " ne1=" << src3->ne[1] << " nb1=" << src3->nb[1] << " ne2=" << src3->ne[2] << " nb2=" << src3->nb[2] << " ne3=" << src3->ne[3] << " nb3=" << src3->nb[3] << " offset=" << src3->view_offs << std::endl; |
| 1514 | } |
| 1515 | std::cerr << "First error: result=" << first_error_result << " correct=" << first_error_correct << " i3=" << first_error[3] << " i2=" << first_error[2] << " i1=" << first_error[1] << " i0=" << first_error[0] << std::endl; |
| 1516 | std::cerr << std::endl << "Result:" << std::endl; |
| 1517 | ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0); |
| 1518 | std::cerr << std::endl << "Correct:" << std::endl; |
| 1519 | ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 0, 0); |
| 1520 | std::cerr << std::endl; |
| 1521 | std::vector<const ggml_tensor *> done; |
| 1522 | ggml_vk_print_graph_origin(tensor, done); |
| 1523 | } |
| 1524 | |
| 1525 | if (avg_err > 0.01 || std::isnan(avg_err)) { |
| 1526 | std::cerr << "ERROR: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl; |
| 1527 | std::cerr << "tensor=" << tensor << " tensor->name=" << tensor->name << " tensor->type: " << ggml_type_name(tensor->type) << " ne0=" << tensor->ne[0] << " nb0=" << tensor->nb[0] << " ne1=" << tensor->ne[1] << " nb1=" << tensor->nb[1] << " ne2=" << tensor->ne[2] << " nb2=" << tensor->nb[2] << " ne3=" << tensor->ne[3] << " nb3=" << tensor->nb[3] << " offset=" << tensor->view_offs << std::endl; |
| 1528 | if (src0 != nullptr) { |
| 1529 | std::cerr << "src0=" << src0 << " op=" << ggml_op_name(src0->op) << " type=" << ggml_type_name(src0->type) << " ne0=" << src0->ne[0] << " nb0=" << src0->nb[0] << " ne1=" << src0->ne[1] << " nb1=" << src0->nb[1] << " ne2=" << src0->ne[2] << " nb2=" << src0->nb[2] << " ne3=" << src0->ne[3] << " nb3=" << src0->nb[3] << " offset=" << src0->view_offs << std::endl; |
| 1530 | } |
| 1531 | if (src1 != nullptr) { |
| 1532 | std::cerr << "src1=" << src1 << " op=" << ggml_op_name(src1->op) << " type=" << ggml_type_name(src1->type) << " ne0=" << src1->ne[0] << " nb0=" << src1->nb[0] << " ne1=" << src1->ne[1] << " nb1=" << src1->nb[1] << " ne2=" << src1->ne[2] << " nb2=" << src1->nb[2] << " ne3=" << src1->ne[3] << " nb3=" << src1->nb[3] << " offset=" << src1->view_offs << std::endl; |
| 1533 | } |
| 1534 | if (src2 != nullptr) { |
| 1535 | std::cerr << "src2=" << src2 << " op=" << ggml_op_name(src2->op) << " type=" << ggml_type_name(src2->type) << " ne0=" << src2->ne[0] << " nb0=" << src2->nb[0] << " ne1=" << src2->ne[1] << " nb1=" << src2->nb[1] << " ne2=" << src2->ne[2] << " nb2=" << src2->nb[2] << " ne3=" << src2->ne[3] << " nb3=" << src2->nb[3] << " offset=" << src2->view_offs << std::endl; |
| 1536 | } |
| 1537 | if (src3 != nullptr) { |
| 1538 | std::cerr << "src3=" << src3 << " op=" << ggml_op_name(src3->op) << " type=" << ggml_type_name(src3->type) << " ne0=" << src3->ne[0] << " nb0=" << src3->nb[0] << " ne1=" << src3->ne[1] << " nb1=" << src3->nb[1] << " ne2=" << src3->ne[2] << " nb2=" << src3->nb[2] << " ne3=" << src3->ne[3] << " nb3=" << src3->nb[3] << " offset=" << src3->view_offs << std::endl; |
| 1539 | } |
| 1540 | std::cerr << "First error: result=" << first_error_result << " correct=" << first_error_correct << " i3=" << first_error[3] << " i2=" << first_error[2] << " i1=" << first_error[1] << " i0=" << first_error[0] << std::endl; |
| 1541 | std::cerr << std::endl << "Result:" << std::endl; |
| 1542 | ggml_vk_print_tensor_area(tensor, tensor_data, first_error[0], first_error[1], first_error[2], first_error[3]); |
| 1543 | std::cerr << std::endl << "Correct:" << std::endl; |
| 1544 | ggml_vk_print_tensor_area(tensor, comp_result, first_error[0], first_error[1], first_error[2], first_error[3]); |
| 1545 | std::cerr << std::endl; |
| 1546 | std::vector<const ggml_tensor *> done; |
| 1547 | ggml_vk_print_graph_origin(tensor, done); |
| 1548 | GGML_ABORT("fatal error"); |
| 1549 | } else { |
| 1550 | std::cerr << check_counter << " " << tensor->name << " op=" << ggml_op_name(tensor->op) << " avg_err=" << avg_err << std::endl; |
| 1551 | } |
| 1552 | |
| 1553 | free(comp_result); |
| 1554 | comp_result = nullptr; |
| 1555 | comp_size = 0; |
| 1556 | |
| 1557 | if (ggml_backend_buffer_is_vk(tensor->buffer)) { |
| 1558 | free(tensor_data); |
| 1559 | } |
| 1560 | |
| 1561 | VK_LOG_DEBUG("END ggml_vk_check_results_1(" << tensor->name << ")"); |
| 1562 | } |
| 1563 | #endif |
| 1564 |