forked from JamePeng/llama-cpp-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathllava_cpp.py
More file actions
526 lines (443 loc) · 18.1 KB
/
Copy pathllava_cpp.py
File metadata and controls
526 lines (443 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
from __future__ import annotations
import os
from ctypes import (
c_bool,
c_char_p,
c_int,
c_uint,
c_int32,
c_uint32,
c_uint8,
c_float,
c_void_p,
POINTER,
_Pointer, # type: ignore
Structure,
)
import pathlib
from typing import (
Union,
NewType,
Optional,
TYPE_CHECKING,
)
import llama_cpp.llama_cpp as llama_cpp
from llama_cpp._ctypes_extensions import (
load_shared_library,
ctypes_function_for_shared_library,
)
if TYPE_CHECKING:
from llama_cpp._ctypes_extensions import (
CtypesArray,
)
# Specify the base name of the shared library to load
_libllava_base_name = "llava"
_libllava_override_path = os.environ.get("LLAVA_CPP_LIB")
_libllava_base_path = pathlib.Path(os.path.abspath(os.path.dirname(__file__))) / "lib" if _libllava_override_path is None else pathlib.Path()
# Load the library
_libllava = load_shared_library(_libllava_base_name, _libllava_base_path)
ctypes_function = ctypes_function_for_shared_library(_libllava)
################################################
# llava.h
################################################
# struct clip_ctx;
clip_ctx_p = NewType("clip_ctx_p", int)
clip_ctx_p_ctypes = c_void_p
# struct clip_image_u8;
clip_image_u8_p = NewType("clip_image_u8_p", int)
clip_image_u8_p_ctypes = c_void_p
# struct llava_image_embed {
# float * embed;
# int n_image_pos;
# };
class llava_image_embed(Structure):
_fields_ = [
("embed", POINTER(c_float)),
("n_image_pos", c_int),
]
# /** sanity check for clip <-> llava embed size match */
# LLAVA_API bool llava_validate_embed_size(const llama_context * ctx_llama, const clip_ctx * ctx_clip);
@ctypes_function(
"llava_validate_embed_size",
[llama_cpp.llama_context_p_ctypes, clip_ctx_p_ctypes],
c_bool,
)
def llava_validate_embed_size(
ctx_llama: llama_cpp.llama_context_p, ctx_clip: clip_ctx_p, /
) -> bool:
...
# LLAVA_API bool llava_image_embed_make_with_clip_img(struct clip_ctx * ctx_clip, int n_threads, const struct clip_image_u8 * img, float ** image_embd_out, int * n_img_pos_out);
@ctypes_function(
"llava_image_embed_make_with_clip_img",
[clip_ctx_p_ctypes, c_int, clip_image_u8_p_ctypes, POINTER(POINTER(c_float)), POINTER(c_int)],
c_bool
)
def llava_image_embed_make_with_clip_img(
ctx_clip: clip_ctx_p,
n_threads: int,
img: clip_image_u8_p,
image_embd_out: _Pointer[_Pointer[c_float]],
n_img_pos_out: _Pointer[c_int],
/,
) -> bool:
...
# /** build an image embed from image file bytes */
# LLAVA_API 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);
@ctypes_function(
"llava_image_embed_make_with_bytes",
[clip_ctx_p_ctypes, c_int, c_char_p, c_int],
POINTER(llava_image_embed),
)
def llava_image_embed_make_with_bytes(
ctx_clip: clip_ctx_p,
n_threads: c_int,
image_bytes: c_char_p,
image_bytes_length: c_int,
/,
) -> "_Pointer[llava_image_embed]":
...
# /** build an image embed from a path to an image filename */
# LLAVA_API struct llava_image_embed * llava_image_embed_make_with_filename(struct clip_ctx * ctx_clip, int n_threads, const char * image_path);
@ctypes_function(
"llava_image_embed_make_with_filename",
[clip_ctx_p_ctypes, c_int, c_char_p],
POINTER(llava_image_embed),
)
def llava_image_embed_make_with_filename(
ctx_clip: clip_ctx_p, n_threads: c_int, image_path: c_char_p, /
) -> "_Pointer[llava_image_embed]":
...
# LLAVA_API void llava_image_embed_free(struct llava_image_embed * embed);
# /** free an embedding made with llava_image_embed_make_* */
@ctypes_function("llava_image_embed_free", [POINTER(llava_image_embed)], None)
def llava_image_embed_free(embed: "_Pointer[llava_image_embed]", /):
...
# /** write the image represented by embed into the llama context with batch size n_batch, starting at context pos n_past. on completion, n_past points to the next position in the context after the image embed. */
# LLAVA_API bool llava_eval_image_embed(struct llama_context * ctx_llama, const struct llava_image_embed * embed, int n_batch, int * n_past);
@ctypes_function(
"llava_eval_image_embed",
[
llama_cpp.llama_context_p_ctypes,
POINTER(llava_image_embed),
c_int,
POINTER(c_int),
],
c_bool,
)
def llava_eval_image_embed(
ctx_llama: llama_cpp.llama_context_p,
embed: "_Pointer[llava_image_embed]",
n_batch: Union[c_int, int],
n_past: "_Pointer[c_int]",
/,
) -> bool:
...
################################################
# clip.h
################################################
# struct clip_image_u8_batch;
clip_image_u8_batch_p = NewType("clip_image_u8_batch_p", int)
clip_image_u8_batch_p_ctypes = c_void_p
# struct clip_image_f32;
clip_image_f32_p = NewType("clip_image_f32_p", int)
clip_image_f32_p_ctypes = c_void_p
# struct clip_image_f32_batch;
clip_image_f32_batch_p = NewType("clip_image_f32_batch_p", int)
clip_image_f32_batch_p_ctypes = c_void_p
# struct ggml_tensor;
ggml_tensor_p = NewType("ggml_tensor_p", int)
ggml_tensor_p_ctypes = c_void_p
# struct clip_image_size {
# int width;
# int height;
# };
class clip_image_size(Structure):
if TYPE_CHECKING:
width: int
height: int
_fields_ = [
("width", c_int),
("height", c_int),
]
clip_image_size_p = NewType("clip_image_size_p", int)
clip_image_size_p_ctypes = POINTER(clip_image_size)
# struct clip_context_params {
# bool use_gpu;
# enum ggml_log_level verbosity;
# };
class clip_context_params(Structure):
if TYPE_CHECKING:
use_gpu: bool
ggml_log_level: int
_fields_ = [
("use_gpu", c_bool),
("ggml_log_level", c_int),
]
# /** load mmproj model */
# // deprecated, use clip_init
# CLIP_API struct clip_ctx * clip_model_load (const char * fname, int verbosity);
@ctypes_function("clip_model_load", [c_char_p, c_int], clip_ctx_p_ctypes)
def clip_model_load(
fname: bytes, verbosity: Union[c_int, int], /
) -> Optional[clip_ctx_p]:
...
# CLIP_API struct clip_ctx * clip_init(const char * fname, struct clip_context_params ctx_params);
@ctypes_function("clip_init", [c_char_p, clip_context_params], clip_ctx_p_ctypes)
def clip_init(
fname: bytes, ctx_params: clip_context_params, /
) -> Optional[clip_ctx_p]:
...
# /** free mmproj model */
# CLIP_API void clip_free(struct clip_ctx * ctx);
@ctypes_function("clip_free", [clip_ctx_p_ctypes], None)
def clip_free(ctx: clip_ctx_p, /):
...
# CLIP_API size_t clip_embd_nbytes(const struct clip_ctx * ctx);
@ctypes_function("clip_embd_nbytes", [clip_ctx_p_ctypes], c_int32)
def clip_embd_nbytes(ctx: clip_ctx_p) -> int:
...
# CLIP_API size_t clip_embd_nbytes_by_img(const struct clip_ctx * ctx, int img_w, int img_h);
@ctypes_function("clip_embd_nbytes_by_img", [clip_ctx_p_ctypes, c_int, c_int], c_int)
def clip_embd_nbytes_by_img(
ctx: clip_ctx_p,
img_w: int,
img_h: int, /
) -> int:
...
# CLIP_API int32_t clip_get_image_size (const struct clip_ctx * ctx);
@ctypes_function("clip_get_image_size", [clip_ctx_p_ctypes], c_int32)
def clip_get_image_size(ctx: clip_ctx_p) -> int:
...
# CLIP_API int32_t clip_get_patch_size (const struct clip_ctx * ctx);
@ctypes_function("clip_get_patch_size", [clip_ctx_p_ctypes], c_int32)
def clip_get_patch_size(ctx: clip_ctx_p) -> int:
...
# CLIP_API int32_t clip_get_hidden_size(const struct clip_ctx * ctx);
@ctypes_function("clip_get_hidden_size", [clip_ctx_p_ctypes], c_int32)
def clip_get_hidden_size(ctx: clip_ctx_p) -> int:
...
# // TODO: should be enum, not string
# CLIP_API const char * clip_patch_merge_type(const struct clip_ctx * ctx);
@ctypes_function("clip_patch_merge_type", [clip_ctx_p_ctypes], c_char_p)
def clip_patch_merge_type(ctx: clip_ctx_p) -> bytes:
...
# CLIP_API const int32_t * clip_image_grid(const struct clip_ctx * ctx);
@ctypes_function("clip_image_grid", [clip_ctx_p_ctypes], c_int32)
def clip_image_grid(ctx: clip_ctx_p) -> int:
...
# CLIP_API size_t get_clip_image_grid_size(const struct clip_ctx * ctx);
@ctypes_function("get_clip_image_grid_size", [clip_ctx_p_ctypes], c_uint)
def get_clip_image_grid_size(ctx: clip_ctx_p) -> c_uint:
...
# CLIP_API int clip_n_output_tokens(const struct clip_ctx * ctx, struct clip_image_f32 * img);
@ctypes_function("clip_n_output_tokens", [clip_ctx_p_ctypes, clip_image_f32_p_ctypes], c_int)
def clip_n_output_tokens(
ctx: clip_ctx_p,
img: clip_image_f32_p, /
) -> int:
...
# // for M-RoPE, this will be the number of token positions in X and Y directions
# // for other models, X will be the total number of tokens and Y will be 1
# CLIP_API int clip_n_output_tokens_x(const struct clip_ctx * ctx, struct clip_image_f32 * img);
@ctypes_function("clip_n_output_tokens_x", [clip_ctx_p_ctypes, clip_image_f32_p_ctypes], c_int)
def clip_n_output_tokens_x(
ctx: clip_ctx_p,
img: clip_image_f32_p, /
) -> int:
...
# CLIP_API int clip_n_output_tokens_y(const struct clip_ctx * ctx, struct clip_image_f32 * img);
@ctypes_function("clip_n_output_tokens_y", [clip_ctx_p_ctypes, clip_image_f32_p_ctypes], c_int)
def clip_n_output_tokens_y(
ctx: clip_ctx_p,
img: clip_image_f32_p, /
) -> int:
...
# // this should be equal to the embedding dimension of the text model
# CLIP_API int clip_n_mmproj_embd(const struct clip_ctx * ctx);
@ctypes_function("clip_n_mmproj_embd", [clip_ctx_p_ctypes], c_int32)
def clip_n_mmproj_embd(ctx: clip_ctx_p) -> int:
...
# CLIP_API int clip_uhd_num_image_embeds_col(struct clip_ctx * ctx_clip);
@ctypes_function("clip_uhd_num_image_embeds_col", [clip_ctx_p_ctypes], c_int32)
def clip_uhd_num_image_embeds_col(ctx_clip: clip_ctx_p) -> int:
...
# CLIP_API void clip_add_load_image_size(struct clip_ctx * ctx_clip, struct clip_image_size * load_image_size);
@ctypes_function("clip_add_load_image_size", [clip_ctx_p_ctypes, clip_image_size_p_ctypes], None)
def clip_add_load_image_size(ctx_clip: clip_ctx_p, load_image_size: clip_image_size_p, /):
...
# CLIP_API struct clip_image_size * clip_get_load_image_size(struct clip_ctx * ctx_clip);
@ctypes_function("clip_get_load_image_size", [clip_ctx_p_ctypes], clip_image_size_p_ctypes)
def clip_get_load_image_size(ctx_clip: clip_ctx_p) -> Optional[clip_image_size_p]:
...
# CLIP_API struct clip_image_size * clip_image_size_init();
@ctypes_function("clip_image_size_init", [], clip_image_size_p_ctypes)
def clip_image_size_init() -> Optional[clip_image_size_p]:
...
# CLIP_API struct clip_image_u8 * clip_image_u8_init ();
@ctypes_function("clip_image_u8_init", [], clip_image_u8_p_ctypes)
def clip_image_u8_init() -> Optional[clip_image_u8_p]:
...
# CLIP_API struct clip_image_f32 * clip_image_f32_init();
@ctypes_function("clip_image_f32_init", [], clip_image_f32_p_ctypes)
def clip_image_f32_init() -> Optional[clip_image_f32_p]:
...
# CLIP_API struct clip_image_f32_batch * clip_image_f32_batch_init(); // only used by libllava
@ctypes_function("clip_image_f32_batch_init", [], clip_image_f32_batch_p_ctypes)
def clip_image_f32_batch_init() -> Optional[clip_image_f32_batch_p]:
...
# // nx, ny are the output image dimensions
# CLIP_API unsigned char * clip_image_u8_get_data(struct clip_image_u8 * img, uint32_t * nx, uint32_t * ny);
@ctypes_function("clip_image_u8_get_data", [clip_image_u8_p_ctypes, c_uint32, c_uint32], c_char_p)
def clip_image_u8_get_data(
img: clip_image_u8_p,
nx: c_uint32,
ny: c_uint32, /
) -> bytes:
...
# CLIP_API void clip_image_size_free (struct clip_image_size * img_size);
@ctypes_function("clip_image_size_free", [clip_image_size_p_ctypes], None)
def clip_image_size_free(img_size: clip_image_size_p):
...
# CLIP_API void clip_image_u8_free (struct clip_image_u8 * img);
@ctypes_function("clip_image_u8_free", [clip_image_u8_p_ctypes], None)
def clip_image_u8_free(img: clip_image_u8_p):
...
# CLIP_API void clip_image_f32_free(struct clip_image_f32 * img);
@ctypes_function("clip_image_f32_free", [clip_image_f32_p_ctypes], None)
def clip_image_f32_free(img: clip_image_f32_p):
...
# CLIP_API void clip_image_u8_batch_free (struct clip_image_u8_batch * batch);
@ctypes_function("clip_image_u8_batch_free", [clip_image_u8_batch_p_ctypes], None)
def clip_image_u8_batch_free(batch: clip_image_u8_batch_p):
...
# CLIP_API void clip_image_f32_batch_free(struct clip_image_f32_batch * batch);
@ctypes_function("clip_image_f32_batch_free", [clip_image_f32_batch_p_ctypes], None)
def clip_image_f32_batch_free(batch: clip_image_f32_batch_p):
...
# // use for accessing underlay data of clip_image_f32_batch
# CLIP_API size_t clip_image_f32_batch_n_images(const struct clip_image_f32_batch * batch); // equivalent to batch->size()
@ctypes_function("clip_image_f32_batch_n_images", [clip_image_f32_batch_p_ctypes], c_uint)
def clip_image_f32_batch_n_images(batch: clip_image_f32_batch_p) -> int:
...
# CLIP_API size_t clip_image_f32_batch_nx(const struct clip_image_f32_batch * batch, int idx); // equivalent to batch[idx]->nx
@ctypes_function("clip_image_f32_batch_nx", [clip_image_f32_batch_p_ctypes, c_int], c_uint)
def clip_image_f32_batch_nx(
batch: clip_image_f32_batch_p,
idx: int, /
) -> int:
...
# CLIP_API size_t clip_image_f32_batch_ny(const struct clip_image_f32_batch * batch, int idx); // equivalent to batch[idx]->ny
@ctypes_function("clip_image_f32_batch_nx", [clip_image_f32_batch_p_ctypes, c_int], c_uint)
def clip_image_f32_batch_nx(
batch: clip_image_f32_batch_p,
idx: int, /
) -> int:
...
# CLIP_API struct clip_image_f32 * clip_image_f32_get_img(const struct clip_image_f32_batch * batch, int idx); // equivalent to batch[idx]->data
@ctypes_function("clip_image_f32_get_img", [clip_image_f32_batch_p_ctypes, c_int], clip_image_f32_p_ctypes)
def clip_image_f32_get_img(
batch: clip_image_f32_batch_p,
idx: int, /
) -> Optional[clip_image_f32_p]:
...
# /**
# * Build image from pixels decoded by other libraries instead of stb_image.h for better performance.
# * The memory layout is RGBRGBRGB..., input buffer length must be 3*nx*ny bytes
# */
# CLIP_API void clip_build_img_from_pixels(const unsigned char * rgb_pixels, int nx, int ny, struct clip_image_u8 * img);
@ctypes_function("clip_build_img_from_pixels", [c_char_p, c_int, c_int, clip_image_u8_p_ctypes], c_bool)
def clip_build_img_from_pixels(
rgb_pixels: bytes,
nx: int,
ny: int,
img: clip_image_u8_p, /
) -> bool:
...
# CLIP_API bool clip_image_load_from_file(const char * fname, struct clip_image_u8 * img);
@ctypes_function("clip_image_load_from_file", [c_char_p, clip_image_u8_p_ctypes], c_bool)
def clip_image_load_from_file(
fname: bytes,
img: clip_image_u8_p, /
) -> bool:
...
# /** interpret bytes as an image file with length bytes_length, and use the result to populate img */
# CLIP_API bool clip_image_load_from_bytes(const unsigned char * bytes, size_t bytes_length, struct clip_image_u8 * img);
@ctypes_function("clip_image_load_from_bytes", [c_char_p, c_uint, clip_image_u8_p_ctypes], c_bool)
def clip_image_load_from_bytes(
bytes: bytes,
bytes_length: c_uint,
img: clip_image_u8_p, /
) -> bool:
...
# /** preprocess img and store the result in res_imgs, pad_to_square may be overridden to false depending on model configuration */
# CLIP_API bool clip_image_preprocess(struct clip_ctx * ctx, const struct clip_image_u8 * img, struct clip_image_f32_batch * res_imgs );
@ctypes_function("clip_image_preprocess", [clip_ctx_p_ctypes, clip_image_u8_p_ctypes, clip_image_f32_batch_p_ctypes], c_bool)
def clip_image_preprocess(
ctx: clip_ctx_p,
img: clip_image_u8_p,
res_imgs: clip_image_f32_batch_p, /
) -> bool:
...
# CLIP_API struct ggml_tensor * clip_get_newline_tensor(const struct clip_ctx * ctx);
@ctypes_function("clip_get_newline_tensor", [clip_ctx_p_ctypes], ggml_tensor_p_ctypes)
def clip_get_newline_tensor(
ctx: clip_ctx_p, /
) -> Optional[ggml_tensor_p]:
...
# CLIP_API bool clip_image_encode (struct clip_ctx * ctx, int n_threads, struct clip_image_f32 * img, float * vec);
@ctypes_function("clip_image_encode", [clip_ctx_p_ctypes, c_int, clip_image_f32_p_ctypes, CtypesArray[c_float]], c_bool)
def clip_image_encode(
ctx: clip_ctx_p,
n_threads: int,
img: clip_image_f32_p,
vec: _Pointer[c_float], /
) -> bool:
...
# CLIP_API bool clip_image_batch_encode(struct clip_ctx * ctx, int n_threads, const struct clip_image_f32_batch * imgs, float * vec);
@ctypes_function("clip_image_batch_encode", [clip_ctx_p_ctypes, c_int, clip_image_f32_batch_p_ctypes, CtypesArray[c_float]], c_bool)
def clip_image_batch_encode(
ctx: clip_ctx_p,
n_threads: int,
imgs: clip_image_f32_batch_p,
vec: _Pointer[c_float], /
) -> bool:
...
# CLIP_API bool clip_model_quantize(const char * fname_inp, const char * fname_out, int itype);
@ctypes_function("clip_model_quantize", [c_char_p, c_char_p, c_int], c_bool)
def clip_model_quantize(
fname_inp: bytes,
fname_out: bytes,
itype: int, /
) -> bool:
...
# CLIP_API int clip_is_minicpmv(const struct clip_ctx * ctx);
@ctypes_function("clip_is_minicpmv", [clip_ctx_p_ctypes], c_int32)
def clip_is_minicpmv(ctx: clip_ctx_p) -> int:
...
# CLIP_API bool clip_is_glm(const struct clip_ctx * ctx);
@ctypes_function("clip_is_glm", [clip_ctx_p_ctypes], c_bool)
def clip_is_glm(ctx: clip_ctx_p) -> bool:
...
# CLIP_API bool clip_is_qwen2vl(const struct clip_ctx * ctx);
@ctypes_function("clip_is_qwen2vl", [clip_ctx_p_ctypes], c_bool)
def clip_is_qwen2vl(ctx: clip_ctx_p) -> bool:
...
# CLIP_API bool clip_is_llava(const struct clip_ctx * ctx);
@ctypes_function("clip_is_llava", [clip_ctx_p_ctypes], c_bool)
def clip_is_llava(ctx: clip_ctx_p) -> bool:
...
# CLIP_API bool clip_is_gemma3(const struct clip_ctx * ctx);
@ctypes_function("clip_is_gemma3", [clip_ctx_p_ctypes], c_bool)
def clip_is_gemma3(ctx: clip_ctx_p) -> bool:
...
# CLIP_API bool clip_encode_float_image (struct clip_ctx * ctx, int n_threads, float * img, int h, int w, float * vec);
@ctypes_function("clip_n_output_tokens", [clip_ctx_p_ctypes, c_int, CtypesArray[c_float], c_int, c_int, CtypesArray[c_float]], c_bool)
def clip_n_output_tokens(
ctx: clip_ctx_p,
n_threads: int,
img: _Pointer[float],
h: int,
w: int,
vec: _Pointer[float],/
) -> bool:
...