llama

Форк
0
/
llava.cpp 
497 строк · 22.0 Кб
1
#include "clip.h"
2
#include "llava.h"
3

4
#include "llama.h"
5

6
#include <algorithm>
7
#include <cerrno>
8
#include <cstdio>
9
#include <cstdlib>
10
#include <cstring>
11
#include <limits>
12
#include <vector>
13

14
#define die(msg)          do { fputs("error: " msg "\n", stderr);                exit(1); } while (0)
15
#define die_fmt(fmt, ...) do { fprintf(stderr, "error: " fmt "\n", __VA_ARGS__); exit(1); } while (0)
16

17
#define LOG_INF(...) do { fprintf(stdout, __VA_ARGS__); } while (0)
18
#define LOG_WRN(...) do { fprintf(stderr, __VA_ARGS__); } while (0)
19
#define LOG_ERR(...) do { fprintf(stderr, __VA_ARGS__); } while (0)
20
#define LOG_DBG(...) do { fprintf(stdout, __VA_ARGS__); } while (0)
21

22
// RGB uint8 image
23
struct clip_image_u8 {
24
    int nx;
25
    int ny;
26

27
    std::vector<uint8_t> buf;
28
};
29

30
// RGB float32 image (NHWC)
31
// Memory layout: RGBRGBRGB...
32
struct clip_image_f32 {
33
    int nx;
34
    int ny;
35

36
    std::vector<float> buf;
37
};
38

39
struct clip_image_grid_shape {
40
    int first;
41
    int second;
42
};
43

44
/**
45
 * Selects the best resolution from a list of possible resolutions based on the original size.
46
 *
47
 * @param original_size The original size of the image in the format (width, height).
48
 * @param possible_resolutions A list of possible resolutions in the format [(width1, height1), (width2, height2), ...].
49
 * @return The best fit resolution in the format (width, height).
50
 */
51
static std::pair<int, int> select_best_resolution(const std::pair<int, int>& original_size, const std::vector<std::pair<int, int>>& possible_resolutions) {
52
    int original_width  = original_size.first;
53
    int original_height = original_size.second;
54

55
    std::pair<int, int> best_fit;
56
    int max_effective_resolution = 0;
57
    int min_wasted_resolution = std::numeric_limits<int>::max();
58

59
    for (const auto& resolution : possible_resolutions) {
60
        int width = resolution.first;
61
        int height = resolution.second;
62
        float scale = std::min(static_cast<float>(width) / original_width, static_cast<float>(height) / original_height);
63
        int downscaled_width  = static_cast<int>(original_width * scale);
64
        int downscaled_height = static_cast<int>(original_height * scale);
65
        int effective_resolution = std::min(downscaled_width * downscaled_height, original_width * original_height);
66
        int wasted_resolution = (width * height) - effective_resolution;
67
        // LOG_DBG("resolution: %d %d, scale: %f, downscaled: %d %d, effective: %d, wasted: %d\n", width, height, scale, downscaled_width, downscaled_height, effective_resolution, wasted_resolution);
68
        if (effective_resolution > max_effective_resolution || (effective_resolution == max_effective_resolution && wasted_resolution < min_wasted_resolution)) {
69
            max_effective_resolution = effective_resolution;
70
            min_wasted_resolution = wasted_resolution;
71
            best_fit = resolution;
72
        }
73
    }
74

75
    return best_fit;
76
}
77

78
/**
79
 * @brief Get the anyres image grid shape object
80
 *
81
 * @param image_size
82
 * @param grid_pinpoints
83
 * @param image_patch_size
84
 * @return <int, int>
85
 */
86
static struct clip_image_grid_shape get_anyres_image_grid_shape(const std::pair<int, int> & image_size, const std::vector<std::pair<int, int>> & grid_pinpoints, int image_patch_size) {
87
    /**
88
        Conversion from gguf flat array to vector:
89
        std::vector<std::pair<int, int>> possible_resolutions;
90
        for (int i = 0; i < 32 && params.image_grid_pinpoints[i] != 0; i+=2) {
91
            possible_resolutions.push_back({params.image_grid_pinpoints[i], params.image_grid_pinpoints[i+1]});
92
        }
93
     */
94
    auto best_resolution = select_best_resolution(image_size, grid_pinpoints);
95
    return {best_resolution.first / image_patch_size, best_resolution.second / image_patch_size};
96
}
97

98
// Take the image segments in a grid configuration and return the embeddings and the number of embeddings into preallocated memory (image_embd_out)
99
static bool clip_llava_handle_patches(clip_ctx * ctx_clip, std::vector<float *> & image_embd_v, struct clip_image_grid_shape grid_shape, float * image_embd_out, int * n_img_pos_out) {
100
    struct {
101
        struct ggml_context * ctx;
102
    } model;
103

104
    const int32_t image_size = clip_image_size(ctx_clip);
105
    const int32_t patch_size = clip_patch_size(ctx_clip);
106

107
    int32_t num_patches_per_side = image_size / patch_size; // 336 / 14 = 24 - used for embedding-patching boxes (24*24 = 576 patches)
108

109
    int num_patches_width  = grid_shape.first;  // grid 1-4
110
    int num_patches_height = grid_shape.second; // grid 1-4
111

112
    const size_t num_images = num_patches_width * num_patches_height + 1;
113

114
    // TODO: size calculation is not calculated - it's only tens of MB
115
    size_t ctx_size = 0;
116

117
    {
118
        ctx_size += clip_embd_nbytes(ctx_clip) * num_images * 8; // image_features
119
        ctx_size += 1024*1024 * ggml_type_size(GGML_TYPE_F32);
120
    }
121

122
    struct ggml_init_params params {
123
        /*.mem_size   =*/ ctx_size,
124
        /*.mem_buffer =*/ NULL,
125
        /*.no_alloc   =*/ false, // NOTE: this should be false when using the legacy API
126
    };
127

128
    // Python reference code for full unpad:
129
    /*
130
        base_image_feature = image_feature[0]
131
        image_feature = image_feature[1:]
132
        image_feature = image_feature.permute(4, 0, 2, 1, 3).contiguous()
133
        image_feature = image_feature.flatten(1, 2).flatten(2, 3)
134
        image_feature = unpad_image(image_feature, image_sizes[image_idx])
135
        image_feature = torch.cat((
136
            image_feature,
137
            self.model.image_newline[:, None, None].expand(*image_feature.shape[:-1], 1)
138
        ), dim=-1)
139
        image_feature = image_feature.flatten(1, 2).transpose(0, 1)
140
        image_feature = torch.cat((base_image_feature, image_feature), dim=0)
141
    */
142
    // We now have two options: unpad or no unpad. Unpad removes tokens for faster llm eval.
143
    // In terms of result quality it appears to make no difference, so we'll start with the easier approach given 5D tensors are not supported in ggml yet.
144
    // Without unpad we have to split the sub-image embeddings into patches of 24 features each and permute them.
145
    // Once all images are processed to prepended the base_image_features without any changes.
146

147
    // Pytorch reference simplified, modified for ggml compatibility - confirmed identical output in python (for a 2x2 grid image (676x676 scaling))
148
    /*
149
        image_feature = image_feature.view(2, 2, 24, 24, 4096)
150
        image_feature = image_feature.permute(0, 2, 1, 3, 4).contiguous()
151
        image_feature = image_feature.view(2, 24, 2, 24, 4096)
152
        image_feature = image_feature.flatten(0, 3)
153

154
        // Reshape to 4D tensor by merging the last two dimensions
155
        image_feature = image_feature.view(2, 2, 24, 24*4096)
156
        image_feature = image_feature.permute(0, 2, 1, 3).contiguous()
157
        image_feature = image_feature.view(-1, 4096)
158
    */
159

160
    model.ctx = ggml_init(params);
161

162
    struct ggml_tensor * image_features = ggml_new_tensor_3d(model.ctx, GGML_TYPE_F32, clip_n_mmproj_embd(ctx_clip), clip_n_patches(ctx_clip), num_images - 1); // example: 4096 x 576 x 4
163
    // ggml_tensor_printf(image_features,"image_features",__LINE__,false,false);
164
    // fill it with the image embeddings, ignoring the base
165
    for (size_t i = 1; i < num_images; i++) {
166
        size_t offset = (i-1) * clip_embd_nbytes(ctx_clip);
167
        memcpy((uint8_t *)(image_features->data) + offset, image_embd_v[i], clip_embd_nbytes(ctx_clip));
168
    }
169

170
    struct ggml_cgraph  * gf = ggml_new_graph(model.ctx);
171
    size_t size_ele = ggml_type_size(GGML_TYPE_F32);
172

173
    struct ggml_tensor *image_features_patchview = ggml_view_4d(model.ctx, image_features,
174
                                                                num_patches_per_side * clip_n_mmproj_embd(ctx_clip),
175
                                                                num_patches_per_side,
176
                                                                num_patches_width,
177
                                                                num_patches_height,
178
                                                                size_ele * num_patches_per_side * clip_n_mmproj_embd(ctx_clip),
179
                                                                size_ele * num_patches_per_side * clip_n_mmproj_embd(ctx_clip) * num_patches_per_side,
180
                                                                size_ele * num_patches_per_side * clip_n_mmproj_embd(ctx_clip) * num_patches_per_side * num_patches_width, 0);
181
    // ggml_tensor_printf(image_features_patchview,"image_features_patchview",__LINE__,false,false);
182
    struct ggml_tensor *permuted_cont = ggml_cont(model.ctx, ggml_permute(model.ctx, image_features_patchview, 0, 2, 1, 3));
183
    /**
184
     At the end of each row we have to add the row_end embeddings, which are the same as the newline embeddings
185
         image_feature = torch.cat((
186
        image_feature,
187
        self.model.image_newline[:, None, None].expand(*image_feature.shape[:-1], 1).to(image_feature.device)
188
    ), dim=-1)
189
     *
190
     */
191

192
    // ggml_tensor_printf(permuted_cont,"permuted_cont",__LINE__,false,false);
193
    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);
194
    // ggml_tensor_printf(flatten,"flatten",__LINE__,false,false);
195
    ggml_build_forward_expand(gf, flatten);
196
    ggml_graph_compute_with_ctx(model.ctx, gf, 1);
197
    struct ggml_tensor* result = ggml_graph_node(gf, -1);
198

199
    memcpy(image_embd_out, image_embd_v[0], clip_embd_nbytes(ctx_clip)); // main image as global context
200
    // append without newline tokens (default behavior in llava_arch when not using unpad ):
201
    memcpy(image_embd_out + clip_n_patches(ctx_clip) * clip_n_mmproj_embd(ctx_clip), (float*)result->data, clip_embd_nbytes(ctx_clip) * (num_images-1)); // grid patches
202
    *n_img_pos_out = static_cast<int>(result->ne[1]+clip_n_patches(ctx_clip));
203

204
    // Debug: Test single segments
205
    // Current findings: sending base image, sending a segment embedding all works similar to python
206
    // However, permuted embeddings do not work yet (stride issue?)
207
    // memcpy(image_embd_out, image_embd_v[0], clip_embd_nbytes(ctx_clip)); // main image as context
208
    // memcpy(image_embd_out, (float*)prepared_cont->data, clip_embd_nbytes(ctx_clip)); // main image as context
209
    // *n_img_pos_out=576;
210

211
    ggml_free(model.ctx);
212
    return true;
213
}
214

215
static clip_image_f32 * only_v2_5_reshape_by_patch(clip_image_f32 * image, int patch_size) {
216
    int width = image->nx;
217
    int height = image->ny;
218
    int num_patches = (height / patch_size) * (width / patch_size);
219
    clip_image_f32 * patch = clip_image_f32_init();
220
    patch->nx = patch_size * num_patches;
221
    patch->ny = patch_size;
222
    patch->buf.resize(3 * patch->nx * patch->ny);
223

224
    int patch_index = 0;
225

226
    for (int i = 0; i < height; i += patch_size) {
227
        for (int j = 0; j < width; j += patch_size) {
228
            for (int pi = 0; pi < patch_size; ++pi) {
229
                for (int pj = 0; pj < patch_size; ++pj) {
230
                    int input_index = ((i + pi) * width + (j + pj)) * 3;
231
                    int output_index = (pi * patch_size * num_patches + patch_index * patch_size + pj) * 3;
232
                    patch->buf[output_index] = image->buf[input_index];
233
                    patch->buf[output_index+1] = image->buf[input_index+1];
234
                    patch->buf[output_index+2] = image->buf[input_index+2];
235
                }
236
            }
237
            patch_index++;
238
        }
239
    }
240
    return patch;
241
}
242

243
static bool encode_image_with_clip(clip_ctx * ctx_clip, int n_threads, const clip_image_u8 * img, float * image_embd, int * n_img_pos) {
244
    // std::vector<clip_image_f32*> img_res_v; // format VectN x H x W x RGB (N x 336 x 336 x 3), so interleaved RGB - different to the python implementation which is N x 3 x 336 x 336
245
    clip_image_f32_batch img_res_v;
246
    img_res_v.size = 0;
247
    img_res_v.data = nullptr;
248
    if (!clip_image_preprocess(ctx_clip, img, &img_res_v)) {
249
        LOG_ERR("%s: unable to preprocess image\n", __func__);
250
        delete[] img_res_v.data;
251
        return false;
252
    }
253

254
    const int64_t t_img_enc_start_us = ggml_time_us();
255

256
    const char * mm_patch_merge_type = clip_patch_merge_type(ctx_clip);
257

258
    if (clip_is_minicpmv(ctx_clip)) {
259
        std::vector<float *> image_embd_v;
260
        image_embd_v.resize(img_res_v.size);
261
        struct clip_image_size * load_image_size = clip_image_size_init();
262
        for (size_t i = 0; i < img_res_v.size; i++) {
263
            const int64_t t_img_enc_step_start_us = ggml_time_us();
264
            image_embd_v[i] = (float *)malloc(clip_embd_nbytes(ctx_clip));
265
            int patch_size=14;
266
            load_image_size->width = img_res_v.data[i].nx;
267
            load_image_size->height = img_res_v.data[i].ny;
268
            clip_add_load_image_size(ctx_clip, load_image_size);
269
            bool encoded = false;
270
            int has_minicpmv_projector = clip_is_minicpmv(ctx_clip);
271
            if (has_minicpmv_projector == 2) {
272
                encoded = clip_image_encode(ctx_clip, n_threads, only_v2_5_reshape_by_patch(&img_res_v.data[i], patch_size), image_embd_v[i]);
273
            }
274
            else if (has_minicpmv_projector == 3) {
275
                encoded = clip_image_encode(ctx_clip, n_threads, &img_res_v.data[i], image_embd_v[i]);
276
            }
277
            if (!encoded) {
278
                LOG_ERR("Unable to encode image - spatial_unpad - subimage %d of %d\n", (int) i+1, (int) img_res_v.size);
279
                return false;
280
            }
281
            const int64_t t_img_enc_steop_batch_us = ggml_time_us();
282
            LOG_INF("%s: step %d of %d encoded in %8.2f ms\n", __func__, (int)i+1, (int)img_res_v.size, (t_img_enc_steop_batch_us - t_img_enc_step_start_us) / 1000.0);
283
        }
284
        const int64_t t_img_enc_batch_us = ggml_time_us();
285
        LOG_INF("%s: all %d segments encoded in %8.2f ms\n", __func__, (int)img_res_v.size, (t_img_enc_batch_us - t_img_enc_start_us) / 1000.0);
286

287
        int n_img_pos_out = 0;
288
        for (size_t i = 0; i < image_embd_v.size(); i++) {
289
            std::memcpy(image_embd + n_img_pos_out * clip_n_mmproj_embd(ctx_clip), image_embd_v[i], clip_embd_nbytes(ctx_clip));
290
            n_img_pos_out += clip_n_patches(ctx_clip);
291
        }
292
        *n_img_pos = n_img_pos_out;
293
        for (size_t i = 0; i < image_embd_v.size(); i++) {
294
            free(image_embd_v[i]);
295
        }
296
        image_embd_v.clear();
297
        load_image_size->width = img->nx;
298
        load_image_size->height = img->ny;
299
        clip_add_load_image_size(ctx_clip, load_image_size);
300
        LOG_INF("%s: load_image_size %d %d\n", __func__, load_image_size->width, load_image_size->height);
301
    }
302
    else if (strcmp(mm_patch_merge_type, "spatial_unpad") != 0) {
303
        // flat / default llava-1.5 type embedding
304
        *n_img_pos = clip_n_patches(ctx_clip);
305
        bool encoded = clip_image_encode(ctx_clip, n_threads, &img_res_v.data[0], image_embd); // image_embd shape is 576 x 4096
306
        delete[] img_res_v.data;
307
        if (!encoded) {
308
            LOG_ERR("Unable to encode image\n");
309

310
            return false;
311
        }
312
    }
313
    else {
314
        // spatial_unpad llava-1.6 type embedding
315
        // TODO: CLIP needs batching support - in HF the llm projection is separate after encoding, which might be a solution to quickly get batching working
316
        std::vector<float *> image_embd_v;
317
        image_embd_v.resize(img_res_v.size);
318
        for (size_t i = 0; i < img_res_v.size; i++) {
319
            image_embd_v[i] = (float *)malloc(clip_embd_nbytes(ctx_clip)); // 576 patches * 4096 embeddings * 4 bytes = 9437184
320
            const bool encoded = clip_image_encode(ctx_clip, n_threads, &img_res_v.data[i], image_embd_v[i]); // image data is in 3x336x336 format and will be converted to 336x336x3 inside
321
            if (!encoded) {
322
                LOG_ERR("Unable to encode image - spatial_unpad - subimage %d of %d\n", (int) i+1, (int) img_res_v.size);
323
                return false;
324
            }
325
        }
326
        const int64_t t_img_enc_batch_us = ggml_time_us();
327
        LOG_INF("%s: %d segments encoded in %8.2f ms\n", __func__, (int)img_res_v.size, (t_img_enc_batch_us - t_img_enc_start_us) / 1000.0);
328

329
        const int32_t * image_grid = clip_image_grid(ctx_clip);
330

331
        std::vector<std::pair<int, int>> grid_pinpoints;
332
        for (int i = 0; i < 32 && image_grid[i] != 0; i += 2) {
333
            grid_pinpoints.push_back({image_grid[i], image_grid[i+1]});
334
        }
335

336
        // free all img_res_v - not needed anymore
337
        delete[] img_res_v.data;
338
        img_res_v.size = 0;
339
        img_res_v.data = nullptr;
340

341
        const int32_t image_size = clip_image_size(ctx_clip);
342

343
        struct clip_image_grid_shape grid_shape = get_anyres_image_grid_shape({img->nx,img->ny}, grid_pinpoints, image_size);
344

345
        int n_img_pos_out;
346
        clip_llava_handle_patches(ctx_clip, image_embd_v, grid_shape, image_embd, &n_img_pos_out);
347
        *n_img_pos = n_img_pos_out;
348

349
        for (size_t i = 0; i < image_embd_v.size(); i++) {
350
            free(image_embd_v[i]);
351
        }
352
        image_embd_v.clear();
353

354
        // debug image/segment/normalization content:
355
        // clip_image_u8 * tmp = clip_image_u8_init();
356
        // clip_image_convert_f32_to_u8(*image_feature, *tmp);
357
        // clip_image_save_to_bmp(*tmp, "image_feature.bmp");
358
    }
359

360
    LOG_INF("%s: image embedding created: %d tokens\n", __func__, *n_img_pos);
361

362
    const int64_t t_img_enc_end_us = ggml_time_us();
363
    float t_img_enc_ms = (t_img_enc_end_us - t_img_enc_start_us) / 1000.0;
364

365
    LOG_INF("\n%s: image encoded in %8.2f ms by CLIP (%8.2f ms per image patch)\n", __func__, t_img_enc_ms, t_img_enc_ms / *n_img_pos);
366

367
    return true;
368
}
369

370
bool llava_validate_embed_size(const llama_context * ctx_llama, const clip_ctx * ctx_clip) {
371
        // make sure that the correct mmproj was used, i.e., compare apples to apples
372
    int n_llama_embd = llama_n_embd(llama_get_model(ctx_llama));
373
    auto n_image_embd = clip_n_mmproj_embd(ctx_clip);
374
    if (n_image_embd != n_llama_embd) {
375
        LOG_ERR("%s: embedding dim of the multimodal projector (%d) is not equal to that of LLaMA (%d). Make sure that you use the correct mmproj file.\n", __func__, n_image_embd, n_llama_embd);
376
        return false;
377
    }
378
    return true;
379
}
380

381
bool llava_image_embed_make_with_clip_img(clip_ctx * ctx_clip, int n_threads, const clip_image_u8 * img, float ** image_embd_out, int * n_img_pos_out) {
382
    int num_max_patches = 6;
383
    if (clip_is_minicpmv(ctx_clip)) {
384
        num_max_patches = 10;
385
    }
386
    float * image_embd = (float *)malloc(clip_embd_nbytes(ctx_clip)*num_max_patches); // TODO: base on gridsize/llava model
387
    if (!image_embd) {
388
        LOG_ERR("Unable to allocate memory for image embeddings\n");
389
        return false;
390
    }
391

392
    int n_img_pos;
393
    if (!encode_image_with_clip(ctx_clip, n_threads, img, image_embd, &n_img_pos)) {
394
        LOG_ERR("%s: cannot encode image, aborting\n", __func__);
395
        free(image_embd);
396
        return false;
397
    }
398
    *image_embd_out = image_embd;
399
    *n_img_pos_out = n_img_pos;
400

401
    return true;
402
}
403

404
bool llava_eval_image_embed(llama_context * ctx_llama, const struct llava_image_embed * image_embed, int n_batch, int * n_past) {
405
    int n_embd  = llama_n_embd(llama_get_model(ctx_llama));
406

407
    for (int i = 0; i < image_embed->n_image_pos; i += n_batch) {
408
        int n_eval = image_embed->n_image_pos - i;
409
        if (n_eval > n_batch) {
410
            n_eval = n_batch;
411
        }
412
        llama_batch batch = {int32_t(n_eval), nullptr, (image_embed->embed+i*n_embd), nullptr, nullptr, nullptr, nullptr, *n_past, 1, 0, };
413
        if (llama_decode(ctx_llama, batch)) {
414
            LOG_ERR("%s : failed to eval\n", __func__);
415
            return false;
416
        }
417
        *n_past += n_eval;
418
    }
419
    return true;
420
}
421

422
struct llava_image_embed * llava_image_embed_make_with_bytes(struct clip_ctx * ctx_clip, int n_threads, const unsigned char * image_bytes, int image_bytes_length) {
423
    clip_image_u8 * img = clip_image_u8_init();
424
    if (!clip_image_load_from_bytes(image_bytes, image_bytes_length, img)) {
425
        clip_image_u8_free(img);
426
        LOG_ERR("%s: can't load image from bytes, is it a valid image?", __func__);
427
        return NULL;
428
    }
429

430
    float* image_embed = NULL;
431
    int n_image_pos = 0;
432
    bool image_embed_result = llava_image_embed_make_with_clip_img(ctx_clip, n_threads, img, &image_embed, &n_image_pos);
433
    if (!image_embed_result) {
434
        clip_image_u8_free(img);
435
        LOG_ERR("%s: coulnd't embed the image\n", __func__);
436
        return NULL;
437
    }
438

439
    clip_image_u8_free(img);
440
    auto result = (llava_image_embed*)malloc(sizeof(llava_image_embed));
441
    result->embed = image_embed;
442
    result->n_image_pos = n_image_pos;
443
    return result;
444
}
445

446
static bool load_file_to_bytes(const char* path, unsigned char** bytesOut, long *sizeOut) {
447
    auto file = fopen(path, "rb");
448
    if (file == NULL) {
449
        LOG_ERR("%s: can't read file %s\n", __func__, path);
450
        return false;
451
    }
452

453
    fseek(file, 0, SEEK_END);
454
    auto fileSize = ftell(file);
455
    fseek(file, 0, SEEK_SET);
456

457
    auto buffer = (unsigned char *)malloc(fileSize); // Allocate memory to hold the file data
458
    if (buffer == NULL) {
459
        LOG_ERR("%s: failed to alloc %ld bytes for file %s\n", __func__, fileSize, path);
460
        perror("Memory allocation error");
461
        fclose(file);
462
        return false;
463
    }
464
    errno = 0;
465
    size_t ret = fread(buffer, 1, fileSize, file); // Read the file into the buffer
466
    if (ferror(file)) {
467
        die_fmt("read error: %s", strerror(errno));
468
    }
469
    if (ret != (size_t) fileSize) {
470
        die("unexpectedly reached end of file");
471
    }
472
    fclose(file); // Close the file
473

474
    *bytesOut = buffer;
475
    *sizeOut = fileSize;
476
    return true;
477
}
478

479
struct llava_image_embed * llava_image_embed_make_with_filename(struct clip_ctx * ctx_clip, int n_threads, const char * image_path) {
480
    unsigned char* image_bytes;
481
    long image_bytes_length;
482
    auto loaded = load_file_to_bytes(image_path, &image_bytes, &image_bytes_length);
483
    if (!loaded) {
484
        LOG_ERR("%s: failed to load %s\n", __func__, image_path);
485
        return NULL;
486
    }
487

488
    llava_image_embed *embed = llava_image_embed_make_with_bytes(ctx_clip, n_threads, image_bytes, image_bytes_length);
489
    free(image_bytes);
490

491
    return embed;
492
}
493

494
void llava_image_embed_free(struct llava_image_embed * embed) {
495
    free(embed->embed);
496
    free(embed);
497
}
498

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.