Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement multimodal models (LLaVA) #3436

Merged
merged 36 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
59aa1ac
WIP: start implementing LLaVA
monatis Oct 2, 2023
0f0e7c6
rm scratch buf for now, will revert after cleanup
monatis Oct 2, 2023
7e9120f
LLaVA image encoder is working. will combine with llama
monatis Oct 2, 2023
d37ed47
Add llava inference code, but it's buggy. debugging
monatis Oct 3, 2023
8690f42
LLaVA is working e2e, needs to optimize memory allocation + cleanup
monatis Oct 7, 2023
94eeac3
Use ggml_allocr + rm unnecessary code
monatis Oct 8, 2023
0c2bd79
fix: crlf -> lf
monatis Oct 8, 2023
204d08b
fix: new line at EoF
monatis Oct 8, 2023
95da79e
fix: trailing whitespace
monatis Oct 8, 2023
2a04d0b
Merge branch 'master' into llava
monatis Oct 8, 2023
444dbce
Add readme
monatis Oct 9, 2023
8af7e21
Update readme
monatis Oct 9, 2023
54495c9
Some cleanup
monatis Oct 9, 2023
9b0ec4d
Are you happy editorconfig?
monatis Oct 9, 2023
8278a73
rm unused batch image preprocessing
monatis Oct 9, 2023
d78e816
rm unused import
monatis Oct 9, 2023
4759bfd
fix: rm designated initializers
monatis Oct 9, 2023
325d240
introduce pad-to-square mode for non-square images
monatis Oct 9, 2023
d75a031
are you happy editorconfig?
monatis Oct 9, 2023
ae01c85
gitignore /llava
monatis Oct 9, 2023
5009ae9
Handle cases where image file does not exist
monatis Oct 9, 2023
96171de
add llava target to Makefile
monatis Oct 9, 2023
d640aae
add support for 13b model variant
monatis Oct 10, 2023
587bde8
Maybe seed is unlucky?
monatis Oct 11, 2023
f1564bb
Merge branch 'master' into llava
monatis Oct 11, 2023
ab21587
Check if apples are compared to apples
monatis Oct 11, 2023
0409ae0
are you happy editorconfig?
monatis Oct 11, 2023
f0f7834
Use temperature = 0.1 by default
monatis Oct 11, 2023
2bc1710
command line: use gpt_params_parse()
monatis Oct 11, 2023
1403d87
Merge master and fix conflicts
monatis Oct 11, 2023
dc913ea
minor
monatis Oct 12, 2023
56ccf97
handle default n_predict
monatis Oct 12, 2023
e9534ea
fix typo
monatis Oct 12, 2023
346e3c1
Merge branch 'master' into llava
ggerganov Oct 12, 2023
4bc5c9c
llava : code formatting, rename files, fix compile warnings
ggerganov Oct 12, 2023
0bd7e69
do not use Wno-cast-qual for MSVC
monatis Oct 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rm scratch buf for now, will revert after cleanup
  • Loading branch information
monatis committed Oct 2, 2023
commit 0f0e7c64803b40964eee1fe44e7eb6907c2cf5c3
4 changes: 4 additions & 0 deletions examples/llava/clip-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ int main(int argc, char ** argv) {

auto ctx_clip = clip_model_load(model_path, 1);
clip_image_u8 img;
//clip_tokens tokens;
//clip_tokenize(ctx_clip, text, &tokens);
//float vec[512];
//clip_text_encode(ctx_clip, 4, &tokens, vec, false);
clip_image_load_from_file(img_path, &img);
float score;
clip_compare_text_and_image(ctx_clip, 4, text, &img, &score);
Expand Down
42 changes: 25 additions & 17 deletions examples/llava/clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

// #define CLIP_DEBUG
#define CLIP_DEBUG

static std::string format(const char * fmt, ...) {
va_list ap;
Expand Down Expand Up @@ -267,19 +267,19 @@ size_t get_mem_req_by_size(struct clip_ctx * ctx) {
switch (n_tensors) {
case 397: // base, two-tower
case 200: // base, vision-only
if (n_positions == 50) { // patch size = 32
return 12 * mb;
if (vision_hparams->patch_size == 32) { // patch size = 32
return 96 * mb;
} else { // patch size = 16
return 24 * mb;
return 256 * mb;
}
case 197: // base or large, text-only
return 12 * mb;
return 16 * mb;
case 589: // large, two-tower
case 392: // large, vision-only
if (n_positions == 257) { // input image size = 224
return 24 * mb;
} else { // input image size = 336
return 60 * mb;
} else { // input image size = 336
return 96 * mb;
}
case 909: // huge, two-tower
case 520: // huge, vision-only
Expand Down Expand Up @@ -873,8 +873,8 @@ bool clip_text_encode(const clip_ctx * ctx, const int n_threads, const clip_toke
struct ggml_context * ctx0 = ggml_init(params);
struct ggml_cgraph gf = {};

static size_t scr0_size = get_scr_buf_req_by_size((struct clip_ctx *)ctx);
static void * scr0 = malloc(scr0_size);
//static size_t scr0_size = get_scr_buf_req_by_size((struct clip_ctx *)ctx);
//static void * scr0 = malloc(scr0_size);

struct ggml_tensor * input_ids = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, N);
memcpy(input_ids->data, tokens->data, N * ggml_element_size(input_ids));
Expand All @@ -892,7 +892,7 @@ bool clip_text_encode(const clip_ctx * ctx, const int n_threads, const clip_toke
for (int il = 0; il < n_layer; il++) {
struct ggml_tensor * cur = embeddings; // embeddings = residual, cur = hidden_states

ggml_set_scratch(ctx0, {0, scr0_size, scr0});
//ggml_set_scratch(ctx0, {0, scr0_size, scr0});

// layernorm1
{
Expand Down Expand Up @@ -982,7 +982,7 @@ bool clip_text_encode(const clip_ctx * ctx, const int n_threads, const clip_toke
struct ggml_tensor * eot = ggml_new_i32(ctx0, N - 1);
embeddings = ggml_get_rows(ctx0, embeddings, eot);

ggml_set_scratch(ctx0, {0, 0, nullptr});
//ggml_set_scratch(ctx0, {0, 0, nullptr});

// text projection
embeddings = ggml_mul_mat(ctx0, model.projection, embeddings);
Expand All @@ -998,11 +998,14 @@ bool clip_text_encode(const clip_ctx * ctx, const int n_threads, const clip_toke
// run the computation

ggml_build_forward_expand(&gf, embeddings);
/*
ggml_cplan cplan = ggml_graph_plan(&gf, n_threads);
if (cplan.work_size != 0) {
cplan.work_data = (uint8_t *)malloc(cplan.work_size);
}
ggml_graph_compute(&gf, &cplan);
*/
ggml_graph_compute_with_ctx(ctx0, &gf, n_threads);

// print
#ifdef CLIP_DEBUG
Expand Down Expand Up @@ -1050,10 +1053,11 @@ bool clip_text_encode(const clip_ctx * ctx, const int n_threads, const clip_toke
printf("used_mem = %zu\n", ggml_used_mem(ctx0));
#endif
memcpy(vec, ggml_get_data_f32(embeddings), sizeof(float) * projection_dim);

/*
if (cplan.work_size != 0) {
free(cplan.work_data);
}
*/

ggml_free(ctx0);

Expand Down Expand Up @@ -1107,8 +1111,8 @@ bool clip_image_batch_encode(const clip_ctx * ctx, const int n_threads, const cl
struct ggml_context * ctx0 = ggml_init(params);
struct ggml_cgraph gf = {};

static size_t scr0_size = get_scr_buf_req_by_size((struct clip_ctx *)ctx);
static void * scr0 = malloc(scr0_size);
//static size_t scr0_size = get_scr_buf_req_by_size((struct clip_ctx *)ctx);
//static void * scr0 = malloc(scr0_size);

struct ggml_tensor * inp_raw = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, image_size, image_size, 3, batch_size);

Expand Down Expand Up @@ -1172,7 +1176,7 @@ bool clip_image_batch_encode(const clip_ctx * ctx, const int n_threads, const cl

const size_t nb_q_w = model.layers[il].q_w->nb[0];

ggml_set_scratch(ctx0, {0, scr0_size, scr0});
//ggml_set_scratch(ctx0, {0, scr0_size, scr0});

// layernorm1
{
Expand Down Expand Up @@ -1265,7 +1269,7 @@ bool clip_image_batch_encode(const clip_ctx * ctx, const int n_threads, const cl
ggml_repeat(ctx0, model.post_ln_b, embeddings));
}

ggml_set_scratch(ctx0, {0, 0, nullptr});
//ggml_set_scratch(ctx0, {0, 0, nullptr});

// final visual projection
embeddings = ggml_mul_mat(ctx0, model.projection, embeddings);
Expand All @@ -1285,12 +1289,15 @@ bool clip_image_batch_encode(const clip_ctx * ctx, const int n_threads, const cl

// run the computation
ggml_build_forward_expand(&gf, output);
/*
ggml_cplan cplan = ggml_graph_plan(&gf, n_threads);
cplan.work_size *= batch_size;
if (cplan.work_size != 0) {
cplan.work_data = (uint8_t *)malloc(cplan.work_size);
}
ggml_graph_compute(&gf, &cplan);
*/
ggml_graph_compute_with_ctx(ctx0, &gf, n_threads);

// print
#ifdef CLIP_DEBUG
Expand Down Expand Up @@ -1340,10 +1347,11 @@ bool clip_image_batch_encode(const clip_ctx * ctx, const int n_threads, const cl
#endif

memcpy(vec, ggml_get_data_f32(output), sizeof(float) * projection_dim * batch_size);

/*
if (cplan.work_size != 0) {
free(cplan.work_data);
}
*/

ggml_free(ctx0);

Expand Down
Loading