12-paligemma.diff 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. diff --git a/examples/llava/clip.cpp b/examples/llava/clip.cpp
  2. index 7cda5f10..671806fd 100644
  3. --- a/examples/llava/clip.cpp
  4. +++ b/examples/llava/clip.cpp
  5. @@ -708,11 +708,12 @@ static ggml_cgraph * clip_image_build_graph(clip_ctx * ctx, const clip_image_f32
  6. if (ctx->proj_type == PROJECTOR_TYPE_MLP) {
  7. embeddings = ggml_mul_mat(ctx0, model.mm_0_w, embeddings);
  8. embeddings = ggml_add(ctx0, embeddings, model.mm_0_b);
  9. -
  10. - embeddings = ggml_gelu(ctx0, embeddings);
  11. - embeddings = ggml_mul_mat(ctx0, model.mm_2_w, embeddings);
  12. - embeddings = ggml_add(ctx0, embeddings, model.mm_2_b);
  13. -
  14. + if (model.mm_2_w)
  15. + {
  16. + embeddings = ggml_gelu(ctx0, embeddings);
  17. + embeddings = ggml_mul_mat(ctx0, model.mm_2_w, embeddings);
  18. + embeddings = ggml_add(ctx0, embeddings, model.mm_2_b);
  19. + }
  20. } else if (ctx->proj_type == PROJECTOR_TYPE_MLP_NORM) {
  21. embeddings = ggml_mul_mat(ctx0, model.mm_0_w, embeddings);
  22. embeddings = ggml_add(ctx0, embeddings, model.mm_0_b);
  23. @@ -2076,6 +2077,10 @@ int clip_n_mmproj_embd(const struct clip_ctx * ctx) {
  24. return ctx->vision_model.mm_model_peg_0_b->ne[0];
  25. }
  26. if (ctx->proj_type == PROJECTOR_TYPE_MLP) {
  27. + if (ctx->vision_model.mm_2_b == nullptr)
  28. + {
  29. + return ctx->vision_model.mm_0_b->ne[0];
  30. + }
  31. return ctx->vision_model.mm_2_b->ne[0];
  32. }
  33. if (ctx->proj_type == PROJECTOR_TYPE_MLP_NORM) {
  34. diff --git a/include/llama.h b/include/llama.h
  35. index f23355a6..e48da401 100644
  36. --- a/include/llama.h
  37. +++ b/include/llama.h
  38. @@ -444,6 +444,12 @@ extern "C" {
  39. // Frees all allocated memory
  40. LLAMA_API void llama_free(struct llama_context * ctx);
  41. + // Sets image embeddings
  42. + LLAMA_API void set_image_embeds(struct llama_context *ctx, float *data);
  43. +
  44. + // Gets architecture
  45. + LLAMA_API int llama_get_architecture(struct llama_model *model);
  46. +
  47. LLAMA_API int64_t llama_time_us(void);
  48. LLAMA_API size_t llama_max_devices(void);
  49. diff --git a/src/llama.cpp b/src/llama.cpp
  50. index a7b1c9eb..ee067919 100644
  51. --- a/src/llama.cpp
  52. +++ b/src/llama.cpp
  53. @@ -2710,6 +2710,8 @@ struct llama_context {
  54. bool logits_all = false;
  55. + float *image_embeds = nullptr;
  56. +
  57. // embeddings output (2-dimensional array: [n_outputs][n_embd])
  58. // populated only when pooling_type == LLAMA_POOLING_TYPE_NONE
  59. size_t embd_size = 0; // capacity (of floats) for embeddings
  60. @@ -11599,6 +11601,15 @@ struct llm_build_context {
  61. inpL = llm_build_inp_embd(ctx0, lctx, hparams, batch, model.tok_embd, cb);
  62. + if (lctx.image_embeds)
  63. + {
  64. + struct ggml_tensor *image_embeds = ggml_dup_tensor(ctx0, inpL);
  65. + image_embeds->data = lctx.image_embeds;
  66. + image_embeds->ne[1] = 256;
  67. + inpL = ggml_set_2d_inplace(ctx0, inpL, image_embeds, inpL->nb[1], 0);
  68. + lctx.image_embeds = NULL;
  69. + }
  70. +
  71. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  72. cb(inpL, "inp_scaled", -1);
  73. @@ -14589,7 +14600,8 @@ static int llama_decode_internal(
  74. }
  75. // non-causal masks do not use the KV cache
  76. - if (hparams.causal_attn) {
  77. + if (hparams.causal_attn || lctx.image_embeds)
  78. + {
  79. llama_kv_cache_update(&lctx);
  80. // if we have enough unused cells before the current head ->
  81. @@ -16448,6 +16460,16 @@ void llama_free_model(struct llama_model * model) {
  82. delete model;
  83. }
  84. +void set_image_embeds(llama_context *ctx, float *data)
  85. +{
  86. + ctx->image_embeds = data;
  87. +}
  88. +
  89. +int llama_get_architecture(llama_model *model)
  90. +{
  91. + return model->arch;
  92. +}
  93. +
  94. struct llama_context * llama_new_context_with_model(
  95. struct llama_model * model,
  96. struct llama_context_params params) {