12-paligemma.diff 4.0 KB

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