Skip to content

Commit cb6d11d

Browse files
committed
add checks to examples
ggml-ci
1 parent 09f9994 commit cb6d11d

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

tools/main/main.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,12 @@ int main(int argc, char ** argv) {
152152

153153
LOG_INF("%s: llama threadpool init, n_threads = %d\n", __func__, (int) params.cpuparams.n_threads);
154154

155-
auto * reg = ggml_backend_dev_backend_reg(ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU));
155+
auto * cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
156+
if (!cpu_dev) {
157+
LOG_ERR("%s: no CPU backend found\n", __func__);
158+
return 1;
159+
}
160+
auto * reg = ggml_backend_dev_backend_reg(cpu_dev);
156161
auto * ggml_threadpool_new_fn = (decltype(ggml_threadpool_new) *) ggml_backend_reg_get_proc_address(reg, "ggml_threadpool_new");
157162
auto * ggml_threadpool_free_fn = (decltype(ggml_threadpool_free) *) ggml_backend_reg_get_proc_address(reg, "ggml_threadpool_free");
158163

tools/mtmd/clip.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,12 @@ struct clip_ctx {
352352

353353
clip_ctx(clip_context_params & ctx_params) {
354354
backend_cpu = ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, nullptr);
355-
backend = ctx_params.use_gpu
356-
? ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_GPU, nullptr)
357-
: nullptr;
355+
if (!backend_cpu) {
356+
throw std::runtime_error("failed to initialize CPU backend");
357+
}
358+
backend = ctx_params.use_gpu
359+
? ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_GPU, nullptr)
360+
: nullptr;
358361

359362
if (backend) {
360363
LOG_INF("%s: CLIP using %s backend\n", __func__, ggml_backend_name(backend));
@@ -2185,9 +2188,10 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity) {
21852188

21862189
struct clip_ctx * clip_init(const char * fname, struct clip_context_params ctx_params) {
21872190
g_logger_state.verbosity_thold = ctx_params.verbosity;
2188-
clip_ctx * ctx_clip = new clip_ctx(ctx_params);
2191+
clip_ctx * ctx_clip = nullptr;
21892192

21902193
try {
2194+
ctx_clip = new clip_ctx(ctx_params);
21912195
clip_model_loader loader(fname, *ctx_clip);
21922196
loader.load_hparams();
21932197
loader.load_tensors();

tools/mtmd/llava.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ static bool clip_llava_handle_patches(clip_ctx * ctx_clip, std::vector<float *>
212212
ggml_build_forward_expand(gf, flatten);
213213

214214
ggml_backend_ptr backend { ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, nullptr) };
215+
GGML_ASSERT(backend != nullptr && "failed to initialize CPU backend");
215216
ggml_backend_graph_compute(backend.get(), gf);
216217

217218
struct ggml_tensor* result = ggml_graph_node(gf, -1);

tools/rpc/rpc-server.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,17 @@ static ggml_backend_t create_backend(const rpc_server_params & params) {
237237
backend = ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, nullptr);
238238
}
239239

240-
fprintf(stderr, "%s: using %s backend\n", __func__, ggml_backend_name(backend));
241-
242-
// set the number of threads
243-
ggml_backend_dev_t dev = ggml_backend_get_device(backend);
244-
ggml_backend_reg_t reg = dev ? ggml_backend_dev_backend_reg(dev) : nullptr;
245-
if (reg) {
246-
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");
247-
if (ggml_backend_set_n_threads_fn) {
248-
ggml_backend_set_n_threads_fn(backend, params.n_threads);
240+
if (backend) {
241+
fprintf(stderr, "%s: using %s backend\n", __func__, ggml_backend_name(backend));
242+
243+
// set the number of threads
244+
ggml_backend_dev_t dev = ggml_backend_get_device(backend);
245+
ggml_backend_reg_t reg = dev ? ggml_backend_dev_backend_reg(dev) : nullptr;
246+
if (reg) {
247+
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");
248+
if (ggml_backend_set_n_threads_fn) {
249+
ggml_backend_set_n_threads_fn(backend, params.n_threads);
250+
}
249251
}
250252
}
251253

0 commit comments

Comments
 (0)