12-paligemma.diff 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. diff --git a/examples/llava/clip.cpp b/examples/llava/clip.cpp
  2. index 7cda5f10..50fbcf08 100644
  3. --- a/examples/llava/clip.cpp
  4. +++ b/examples/llava/clip.cpp
  5. @@ -709,9 +709,12 @@ static ggml_cgraph * clip_image_build_graph(clip_ctx * ctx, const clip_image_f32
  6. embeddings = ggml_mul_mat(ctx0, model.mm_0_w, embeddings);
  7. embeddings = ggml_add(ctx0, embeddings, model.mm_0_b);
  8. - embeddings = ggml_gelu(ctx0, embeddings);
  9. - embeddings = ggml_mul_mat(ctx0, model.mm_2_w, embeddings);
  10. - embeddings = ggml_add(ctx0, embeddings, model.mm_2_b);
  11. + // paligemma missing second linear layer
  12. + if (model.mm_2_w) {
  13. + embeddings = ggml_gelu(ctx0, embeddings);
  14. + embeddings = ggml_mul_mat(ctx0, model.mm_2_w, embeddings);
  15. + embeddings = ggml_add(ctx0, embeddings, model.mm_2_b);
  16. + }
  17. } else if (ctx->proj_type == PROJECTOR_TYPE_MLP_NORM) {
  18. embeddings = ggml_mul_mat(ctx0, model.mm_0_w, embeddings);
  19. @@ -2076,7 +2079,10 @@ int clip_n_mmproj_embd(const struct clip_ctx * ctx) {
  20. return ctx->vision_model.mm_model_peg_0_b->ne[0];
  21. }
  22. if (ctx->proj_type == PROJECTOR_TYPE_MLP) {
  23. - return ctx->vision_model.mm_2_b->ne[0];
  24. + // paligemma missing second linear layer
  25. + if (ctx->vision_model.mm_2_b == nullptr) {
  26. + return ctx->vision_model.mm_0_b->ne[0];
  27. + }
  28. }
  29. if (ctx->proj_type == PROJECTOR_TYPE_MLP_NORM) {
  30. return ctx->vision_model.mm_3_b->ne[0];
  31. diff --git a/include/llama.h b/include/llama.h
  32. index f23355a6..7c6301bf 100644
  33. --- a/include/llama.h
  34. +++ b/include/llama.h
  35. @@ -444,6 +444,9 @@ extern "C" {
  36. // Frees all allocated memory
  37. LLAMA_API void llama_free(struct llama_context * ctx);
  38. + // save image embeddings
  39. + LLAMA_API void set_image_embeds(struct llama_context *ctx, float *data);
  40. +
  41. LLAMA_API int64_t llama_time_us(void);
  42. LLAMA_API size_t llama_max_devices(void);
  43. diff --git a/src/llama.cpp b/src/llama.cpp
  44. index a7b1c9eb..b0a6bc27 100644
  45. --- a/src/llama.cpp
  46. +++ b/src/llama.cpp
  47. @@ -2668,6 +2668,7 @@ struct llama_context {
  48. const struct llama_model & model;
  49. + float *image_embeds;
  50. struct llama_cparams cparams;
  51. struct llama_sampling sampling;
  52. struct llama_kv_cache kv_self;
  53. @@ -2751,6 +2752,10 @@ struct llama_context {
  54. struct ggml_tensor * inp_KQ_mask_cross; // F32 [n_outputs_enc, n_batch]
  55. };
  56. +void set_image_embeds(llama_context *ctx, float *data) {
  57. + ctx->image_embeds = data;
  58. +}
  59. +
  60. struct llama_lora_weight {
  61. struct ggml_tensor * a = nullptr;
  62. struct ggml_tensor * b = nullptr;
  63. @@ -11599,6 +11604,15 @@ struct llm_build_context {
  64. inpL = llm_build_inp_embd(ctx0, lctx, hparams, batch, model.tok_embd, cb);
  65. + // set the image embeddings in the input tensor
  66. + if (lctx.image_embeds) {
  67. + struct ggml_tensor *image_embeds = ggml_dup_tensor(ctx0, inpL);
  68. + image_embeds->data = lctx.image_embeds;
  69. + image_embeds->ne[1] = 256;
  70. + inpL = ggml_set_2d_inplace(ctx0, inpL, image_embeds, inpL->nb[1], 0);
  71. + lctx.image_embeds = NULL;
  72. + }
  73. +
  74. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  75. cb(inpL, "inp_scaled", -1);
  76. @@ -14589,7 +14603,7 @@ static int llama_decode_internal(
  77. }
  78. // non-causal masks do not use the KV cache
  79. - if (hparams.causal_attn) {
  80. + if (hparams.causal_attn || lctx.image_embeds) {
  81. llama_kv_cache_update(&lctx);
  82. // if we have enough unused cells before the current head ->