| 75 | }; |
| 76 | |
| 77 | static bool load_model(const std::string & fname, yolo_model & model) { |
| 78 | struct ggml_context * tmp_ctx = nullptr; |
| 79 | struct gguf_init_params gguf_params = { |
| 80 | /*.no_alloc =*/ false, |
| 81 | /*.ctx =*/ &tmp_ctx, |
| 82 | }; |
| 83 | gguf_context * gguf_ctx = gguf_init_from_file(fname.c_str(), gguf_params); |
| 84 | if (!gguf_ctx) { |
| 85 | fprintf(stderr, "%s: gguf_init_from_file() failed\n", __func__); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | int num_tensors = gguf_get_n_tensors(gguf_ctx); |
| 90 | struct ggml_init_params params { |
| 91 | /*.mem_size =*/ ggml_tensor_overhead() * num_tensors, |
| 92 | /*.mem_buffer =*/ NULL, |
| 93 | /*.no_alloc =*/ true, |
| 94 | }; |
| 95 | model.ctx = ggml_init(params); |
| 96 | for (int i = 0; i < num_tensors; i++) { |
| 97 | const char * name = gguf_get_tensor_name(gguf_ctx, i); |
| 98 | struct ggml_tensor * src = ggml_get_tensor(tmp_ctx, name); |
| 99 | struct ggml_tensor * dst = ggml_dup_tensor(model.ctx, src); |
| 100 | ggml_set_name(dst, name); |
| 101 | } |
| 102 | model.buffer = ggml_backend_alloc_ctx_tensors(model.ctx, model.backend); |
| 103 | // copy tensors from main memory to backend |
| 104 | for (struct ggml_tensor * cur = ggml_get_first_tensor(model.ctx); cur != NULL; cur = ggml_get_next_tensor(model.ctx, cur)) { |
| 105 | struct ggml_tensor * src = ggml_get_tensor(tmp_ctx, ggml_get_name(cur)); |
| 106 | size_t n_size = ggml_nbytes(src); |
| 107 | ggml_backend_tensor_set(cur, ggml_get_data(src), 0, n_size); |
| 108 | } |
| 109 | gguf_free(gguf_ctx); |
| 110 | ggml_free(tmp_ctx); |
| 111 | |
| 112 | model.width = 416; |
| 113 | model.height = 416; |
| 114 | model.conv2d_layers.resize(13); |
| 115 | model.conv2d_layers[7].padding = 0; |
| 116 | model.conv2d_layers[9].padding = 0; |
| 117 | model.conv2d_layers[9].batch_normalize = false; |
| 118 | model.conv2d_layers[9].activate = false; |
| 119 | model.conv2d_layers[10].padding = 0; |
| 120 | model.conv2d_layers[12].padding = 0; |
| 121 | model.conv2d_layers[12].batch_normalize = false; |
| 122 | model.conv2d_layers[12].activate = false; |
| 123 | for (int i = 0; i < (int)model.conv2d_layers.size(); i++) { |
| 124 | char name[256]; |
| 125 | snprintf(name, sizeof(name), "l%d_weights", i); |
| 126 | model.conv2d_layers[i].weights = ggml_get_tensor(model.ctx, name); |
| 127 | snprintf(name, sizeof(name), "l%d_biases", i); |
| 128 | model.conv2d_layers[i].biases = ggml_get_tensor(model.ctx, name); |
| 129 | if (model.conv2d_layers[i].batch_normalize) { |
| 130 | snprintf(name, sizeof(name), "l%d_scales", i); |
| 131 | model.conv2d_layers[i].scales = ggml_get_tensor(model.ctx, name); |
| 132 | snprintf(name, sizeof(name), "l%d_rolling_mean", i); |
| 133 | model.conv2d_layers[i].rolling_mean = ggml_get_tensor(model.ctx, name); |
| 134 | snprintf(name, sizeof(name), "l%d_rolling_variance", i); |
no test coverage detected