Skip to content

Commit 86bd60d

Browse files
authored
llava/mtmd : fixes to fully support dl backends (#13303)
1 parent 9f2da58 commit 86bd60d

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

tools/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ else()
2727
add_subdirectory(run)
2828
add_subdirectory(tokenize)
2929
add_subdirectory(tts)
30+
add_subdirectory(llava)
3031
if (NOT GGML_BACKEND_DL)
3132
# these examples use the backends directly and cannot be built with dynamic loading
3233
add_subdirectory(cvector-generator)
3334
add_subdirectory(export-lora)
34-
add_subdirectory(llava)
3535
if (GGML_RPC)
3636
add_subdirectory(rpc)
3737
endif()

tools/llava/clip.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3382,7 +3382,15 @@ bool clip_image_batch_encode(clip_ctx * ctx, const int n_threads, const clip_ima
33823382
GGML_ABORT("Unknown projector type");
33833383
}
33843384

3385-
ggml_backend_cpu_set_n_threads(ctx->backend_cpu, n_threads);
3385+
// ggml_backend_cpu_set_n_threads(ctx->backend_cpu, n_threads);
3386+
ggml_backend_dev_t dev = ggml_backend_get_device(ctx->backend_cpu);
3387+
ggml_backend_reg_t reg = dev ? ggml_backend_dev_backend_reg(dev) : nullptr;
3388+
if (reg) {
3389+
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");
3390+
if (ggml_backend_set_n_threads_fn) {
3391+
ggml_backend_set_n_threads_fn(ctx->backend_cpu, n_threads);
3392+
}
3393+
}
33863394

33873395
auto status = ggml_backend_sched_graph_compute(ctx->sched.get(), gf);
33883396
if (status != GGML_STATUS_SUCCESS) {

tools/llava/llava.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "llava.h"
33

44
#include "llama.h"
5+
#include "ggml-cpp.h"
56

67
#include <algorithm>
78
#include <cerrno>
@@ -209,7 +210,10 @@ static bool clip_llava_handle_patches(clip_ctx * ctx_clip, std::vector<float *>
209210
struct ggml_tensor *flatten = ggml_view_2d(model.ctx, permuted_cont, clip_n_mmproj_embd(ctx_clip), num_patches_height * num_patches_width * num_patches_per_side * num_patches_per_side, size_ele * clip_n_mmproj_embd(ctx_clip), 0);
210211
// ggml_tensor_printf(flatten,"flatten",__LINE__,false,false);
211212
ggml_build_forward_expand(gf, flatten);
212-
ggml_graph_compute_with_ctx(model.ctx, gf, 1);
213+
214+
ggml_backend_ptr backend { ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, nullptr) };
215+
ggml_backend_graph_compute(backend.get(), gf);
216+
213217
struct ggml_tensor* result = ggml_graph_node(gf, -1);
214218

215219
memcpy(image_embd_out, image_embd_v[0], clip_embd_nbytes(ctx_clip)); // main image as global context

0 commit comments

Comments
 (0)