llama-cpp-python-win 0.3.16__cp314-cp314-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. bin/convert_hf_to_gguf.py +8751 -0
  2. bin/ggml-base.dll +0 -0
  3. bin/ggml-cpu.dll +0 -0
  4. bin/ggml.dll +0 -0
  5. bin/llama-mtmd-cli.exe +0 -0
  6. bin/llama.dll +0 -0
  7. bin/mtmd.dll +0 -0
  8. include/ggml-alloc.h +76 -0
  9. include/ggml-backend.h +354 -0
  10. include/ggml-blas.h +25 -0
  11. include/ggml-cann.h +123 -0
  12. include/ggml-cpp.h +39 -0
  13. include/ggml-cpu.h +145 -0
  14. include/ggml-cuda.h +47 -0
  15. include/ggml-metal.h +66 -0
  16. include/ggml-opt.h +256 -0
  17. include/ggml-rpc.h +33 -0
  18. include/ggml-sycl.h +49 -0
  19. include/ggml-vulkan.h +29 -0
  20. include/ggml-webgpu.h +19 -0
  21. include/ggml.h +2467 -0
  22. include/gguf.h +202 -0
  23. include/llama-cpp.h +30 -0
  24. include/llama.h +1482 -0
  25. include/mtmd-helper.h +91 -0
  26. include/mtmd.h +298 -0
  27. lib/cmake/ggml/ggml-config.cmake +328 -0
  28. lib/cmake/ggml/ggml-version.cmake +65 -0
  29. lib/cmake/llama/llama-config.cmake +54 -0
  30. lib/cmake/llama/llama-version.cmake +65 -0
  31. lib/ggml-base.lib +0 -0
  32. lib/ggml-cpu.lib +0 -0
  33. lib/ggml.lib +0 -0
  34. lib/llama.lib +0 -0
  35. lib/mtmd.lib +0 -0
  36. lib/pkgconfig/llama.pc +10 -0
  37. llama_cpp/__init__.py +4 -0
  38. llama_cpp/_ctypes_extensions.py +131 -0
  39. llama_cpp/_ggml.py +12 -0
  40. llama_cpp/_internals.py +856 -0
  41. llama_cpp/_logger.py +47 -0
  42. llama_cpp/_utils.py +78 -0
  43. llama_cpp/lib/ggml-base.dll +0 -0
  44. llama_cpp/lib/ggml-base.lib +0 -0
  45. llama_cpp/lib/ggml-cpu.dll +0 -0
  46. llama_cpp/lib/ggml-cpu.lib +0 -0
  47. llama_cpp/lib/ggml.dll +0 -0
  48. llama_cpp/lib/ggml.lib +0 -0
  49. llama_cpp/lib/llama.dll +0 -0
  50. llama_cpp/lib/llama.lib +0 -0
  51. llama_cpp/lib/mtmd.dll +0 -0
  52. llama_cpp/lib/mtmd.lib +0 -0
  53. llama_cpp/llama.py +2422 -0
  54. llama_cpp/llama_cache.py +155 -0
  55. llama_cpp/llama_chat_format.py +3962 -0
  56. llama_cpp/llama_cpp.py +4374 -0
  57. llama_cpp/llama_grammar.py +953 -0
  58. llama_cpp/llama_speculative.py +64 -0
  59. llama_cpp/llama_tokenizer.py +120 -0
  60. llama_cpp/llama_types.py +316 -0
  61. llama_cpp/llava_cpp.py +158 -0
  62. llama_cpp/mtmd_cpp.py +280 -0
  63. llama_cpp/py.typed +0 -0
  64. llama_cpp/server/__init__.py +0 -0
  65. llama_cpp/server/__main__.py +100 -0
  66. llama_cpp/server/app.py +597 -0
  67. llama_cpp/server/cli.py +97 -0
  68. llama_cpp/server/errors.py +212 -0
  69. llama_cpp/server/model.py +312 -0
  70. llama_cpp/server/settings.py +240 -0
  71. llama_cpp/server/types.py +316 -0
  72. llama_cpp_python_win-0.3.16.dist-info/METADATA +856 -0
  73. llama_cpp_python_win-0.3.16.dist-info/RECORD +75 -0
  74. llama_cpp_python_win-0.3.16.dist-info/WHEEL +5 -0
  75. llama_cpp_python_win-0.3.16.dist-info/licenses/LICENSE.md +9 -0
include/mtmd-helper.h ADDED
@@ -0,0 +1,91 @@
1
+ #ifndef MTMD_HELPER_H
2
+ #define MTMD_HELPER_H
3
+
4
+ #include "ggml.h"
5
+ #include "llama.h"
6
+ #include "mtmd.h"
7
+
8
+ #include <stddef.h>
9
+ #include <stdint.h>
10
+ #include <stdbool.h>
11
+
12
+ #ifdef __cplusplus
13
+ extern "C" {
14
+ #endif
15
+
16
+ //
17
+ // libmtmd helper functions
18
+ //
19
+ // Please note that these helpers are not guaranteed to be stable.
20
+ // BREAKING CHANGES are expected.
21
+ //
22
+
23
+ // helper function to construct a mtmd_bitmap from a file
24
+ // it calls mtmd_helper_bitmap_init_from_buf() internally
25
+ // returns nullptr on failure
26
+ // this function is thread-safe
27
+ MTMD_API mtmd_bitmap * mtmd_helper_bitmap_init_from_file(mtmd_context * ctx, const char * fname);
28
+
29
+ // helper function to construct a mtmd_bitmap from a buffer containing a file
30
+ // supported formats:
31
+ // image: formats supported by stb_image: jpg, png, bmp, gif, etc.
32
+ // audio: formats supported by miniaudio: wav, mp3, flac
33
+ // note: audio files will be auto-detected based on magic bytes
34
+ // returns nullptr on failure
35
+ // this function is thread-safe
36
+ MTMD_API mtmd_bitmap * mtmd_helper_bitmap_init_from_buf(mtmd_context * ctx, const unsigned char * buf, size_t len);
37
+
38
+ // helper to count the total number of tokens from a list of chunks, useful to keep track of KV cache
39
+ MTMD_API size_t mtmd_helper_get_n_tokens(const mtmd_input_chunks * chunks);
40
+
41
+ // helper to count the total position of tokens from a list of chunks, useful to keep track of n_past
42
+ // normally, n_pos is equal to n_tokens, but for M-RoPE it is different
43
+ MTMD_API llama_pos mtmd_helper_get_n_pos(const mtmd_input_chunks * chunks);
44
+
45
+ // helper function that automatically:
46
+ // 1. run llama_decode() on text chunks
47
+ // 2. run mtmd_encode() on image chunks, then mtmd_get_output_embd() and then llama_decode()
48
+ // if any of the mtmd_encode() or llama_decode() calls return non-zero, stop and forward the error
49
+ // otherwise, returns 0 on success
50
+ // this function is NOT thread-safe
51
+ MTMD_API int32_t mtmd_helper_eval_chunks(mtmd_context * ctx,
52
+ struct llama_context * lctx,
53
+ const mtmd_input_chunks * chunks,
54
+ llama_pos n_past,
55
+ llama_seq_id seq_id,
56
+ int32_t n_batch,
57
+ bool logits_last,
58
+ llama_pos * new_n_past);
59
+
60
+ // works like mtmd_helper_eval_chunks(), but only for a single chunk
61
+ // this function is NOT thread-safe
62
+ MTMD_API int32_t mtmd_helper_eval_chunk_single(mtmd_context * ctx,
63
+ struct llama_context * lctx,
64
+ const mtmd_input_chunk * chunk,
65
+ llama_pos n_past,
66
+ llama_seq_id seq_id,
67
+ int32_t n_batch,
68
+ bool logits_last,
69
+ llama_pos * new_n_past);
70
+
71
+ // helper function to decode an image whose embeddings have already been calculated
72
+ // this helper will handle batching and pre/post decoding setup (for ex. gemma 3 requires non-causal attention)
73
+ // ret 0 on success, -1 on chunk not being a valid image chunk, 1 on decode failure
74
+ MTMD_API int32_t mtmd_helper_decode_image_chunk(mtmd_context * ctx,
75
+ struct llama_context * lctx,
76
+ const mtmd_input_chunk * chunk,
77
+ float * encoded_embd,
78
+ llama_pos n_past,
79
+ llama_seq_id seq_id,
80
+ int32_t n_batch,
81
+ llama_pos * new_n_past);
82
+
83
+ #ifdef __cplusplus
84
+ } // extern "C"
85
+ #endif
86
+
87
+ //
88
+ // C++ wrappers
89
+ //
90
+
91
+ #endif
include/mtmd.h ADDED
@@ -0,0 +1,298 @@
1
+ #ifndef MTMD_H
2
+ #define MTMD_H
3
+
4
+ #include "ggml.h"
5
+ #include "llama.h"
6
+
7
+ #include <stddef.h>
8
+ #include <stdint.h>
9
+ #include <stdbool.h>
10
+
11
+ #ifdef __cplusplus
12
+ #include <string>
13
+ #include <vector>
14
+ #include <cinttypes>
15
+ #include <memory>
16
+ #endif
17
+
18
+ /**
19
+ * libmtmd: A library for multimodal support in llama.cpp.
20
+ *
21
+ * WARNING: This API is experimental and subject to many BREAKING CHANGES.
22
+ * Issues related to API usage may receive lower priority support.
23
+ *
24
+ * For the usage, see an example in mtmd-cli.cpp
25
+ */
26
+
27
+ #ifdef LLAMA_SHARED
28
+ # if defined(_WIN32) && !defined(__MINGW32__)
29
+ # ifdef LLAMA_BUILD
30
+ # define MTMD_API __declspec(dllexport)
31
+ # else
32
+ # define MTMD_API __declspec(dllimport)
33
+ # endif
34
+ # else
35
+ # define MTMD_API __attribute__ ((visibility ("default")))
36
+ # endif
37
+ #else
38
+ # define MTMD_API
39
+ #endif
40
+
41
+ // deprecated marker, use mtmd_default_marker() instead
42
+ #define MTMD_DEFAULT_IMAGE_MARKER "<__image__>"
43
+
44
+ #ifdef __cplusplus
45
+ extern "C" {
46
+ #endif
47
+
48
+ enum mtmd_input_chunk_type {
49
+ MTMD_INPUT_CHUNK_TYPE_TEXT,
50
+ MTMD_INPUT_CHUNK_TYPE_IMAGE,
51
+ MTMD_INPUT_CHUNK_TYPE_AUDIO,
52
+ };
53
+
54
+ // opaque types
55
+ struct mtmd_context;
56
+ struct mtmd_bitmap;
57
+ struct mtmd_image_tokens;
58
+ struct mtmd_input_chunk;
59
+ struct mtmd_input_chunks;
60
+
61
+ struct mtmd_input_text {
62
+ const char * text;
63
+ bool add_special;
64
+ bool parse_special;
65
+ };
66
+
67
+ //
68
+ // C API
69
+ //
70
+
71
+ typedef struct mtmd_context mtmd_context;
72
+ typedef struct mtmd_bitmap mtmd_bitmap;
73
+ typedef struct mtmd_image_tokens mtmd_image_tokens;
74
+ typedef struct mtmd_input_chunk mtmd_input_chunk;
75
+ typedef struct mtmd_input_chunks mtmd_input_chunks;
76
+ typedef struct mtmd_input_text mtmd_input_text;
77
+
78
+ struct mtmd_context_params {
79
+ bool use_gpu;
80
+ bool print_timings;
81
+ int n_threads;
82
+ enum ggml_log_level verbosity;
83
+ const char * image_marker; // deprecated, use media_marker instead
84
+ const char * media_marker;
85
+ };
86
+
87
+ MTMD_API const char * mtmd_default_marker(void);
88
+
89
+ MTMD_API struct mtmd_context_params mtmd_context_params_default(void);
90
+
91
+ // initialize the mtmd context
92
+ // return nullptr on failure
93
+ MTMD_API mtmd_context * mtmd_init_from_file(const char * mmproj_fname,
94
+ const struct llama_model * text_model,
95
+ const struct mtmd_context_params ctx_params);
96
+
97
+ MTMD_API void mtmd_free(mtmd_context * ctx);
98
+
99
+ // whether we need to set non-causal mask before llama_decode
100
+ MTMD_API bool mtmd_decode_use_non_causal(mtmd_context * ctx);
101
+
102
+ // whether the current model use M-RoPE for llama_decode
103
+ MTMD_API bool mtmd_decode_use_mrope(mtmd_context * ctx);
104
+
105
+ // whether the current model supports vision input
106
+ MTMD_API bool mtmd_support_vision(mtmd_context * ctx);
107
+
108
+ // whether the current model supports audio input
109
+ MTMD_API bool mtmd_support_audio(mtmd_context * ctx);
110
+
111
+ // get audio bitrate in Hz, for example 16000 for Whisper
112
+ // return -1 if audio is not supported
113
+ MTMD_API int mtmd_get_audio_bitrate(mtmd_context * ctx);
114
+
115
+ // mtmd_bitmap
116
+ //
117
+ // if bitmap is image:
118
+ // length of data must be nx * ny * 3
119
+ // the data is in RGBRGBRGB... format
120
+ // if bitmap is audio:
121
+ // length of data must be n_samples * sizeof(float)
122
+ // the data is in float format (PCM F32)
123
+ MTMD_API mtmd_bitmap * mtmd_bitmap_init (uint32_t nx, uint32_t ny, const unsigned char * data);
124
+ MTMD_API mtmd_bitmap * mtmd_bitmap_init_from_audio(size_t n_samples, const float * data);
125
+ MTMD_API uint32_t mtmd_bitmap_get_nx (const mtmd_bitmap * bitmap);
126
+ MTMD_API uint32_t mtmd_bitmap_get_ny (const mtmd_bitmap * bitmap);
127
+ MTMD_API const unsigned char * mtmd_bitmap_get_data (const mtmd_bitmap * bitmap);
128
+ MTMD_API size_t mtmd_bitmap_get_n_bytes(const mtmd_bitmap * bitmap);
129
+ MTMD_API bool mtmd_bitmap_is_audio (const mtmd_bitmap * bitmap);
130
+ MTMD_API void mtmd_bitmap_free (mtmd_bitmap * bitmap);
131
+ // bitmap ID is optional, but useful for KV cache tracking
132
+ // these getters/setters are dedicated functions, so you can for example calculate the hash of the image based on mtmd_bitmap_get_data()
133
+ MTMD_API const char * mtmd_bitmap_get_id(const mtmd_bitmap * bitmap);
134
+ MTMD_API void mtmd_bitmap_set_id(mtmd_bitmap * bitmap, const char * id);
135
+
136
+
137
+ // mtmd_input_chunks
138
+ //
139
+ // this is simply a list of mtmd_input_chunk
140
+ // the elements can only be populated via mtmd_tokenize()
141
+ MTMD_API mtmd_input_chunks * mtmd_input_chunks_init(void);
142
+ MTMD_API size_t mtmd_input_chunks_size(const mtmd_input_chunks * chunks);
143
+ MTMD_API const mtmd_input_chunk * mtmd_input_chunks_get (const mtmd_input_chunks * chunks, size_t idx);
144
+ MTMD_API void mtmd_input_chunks_free(mtmd_input_chunks * chunks);
145
+
146
+ // mtmd_input_chunk
147
+ //
148
+ // the instance will be constructed via mtmd_tokenize()
149
+ // it will be freed along with mtmd_input_chunks
150
+ MTMD_API enum mtmd_input_chunk_type mtmd_input_chunk_get_type (const mtmd_input_chunk * chunk);
151
+ MTMD_API const llama_token * mtmd_input_chunk_get_tokens_text (const mtmd_input_chunk * chunk, size_t * n_tokens_output);
152
+ MTMD_API const mtmd_image_tokens * mtmd_input_chunk_get_tokens_image(const mtmd_input_chunk * chunk);
153
+ MTMD_API size_t mtmd_input_chunk_get_n_tokens (const mtmd_input_chunk * chunk);
154
+ // returns nullptr for ID on text chunk
155
+ MTMD_API const char * mtmd_input_chunk_get_id (const mtmd_input_chunk * chunk);
156
+ // number of temporal positions (always 1 for M-RoPE, n_tokens otherwise)
157
+ MTMD_API llama_pos mtmd_input_chunk_get_n_pos (const mtmd_input_chunk * chunk);
158
+
159
+ // in case you want to use custom logic to handle the chunk (i.e. KV cache management)
160
+ // you can move the chunk ownership to your own code by copying it
161
+ // remember to free the chunk when you are done with it
162
+ MTMD_API mtmd_input_chunk * mtmd_input_chunk_copy(const mtmd_input_chunk * chunk);
163
+ MTMD_API void mtmd_input_chunk_free(mtmd_input_chunk * chunk);
164
+
165
+
166
+ // mtmd_image_tokens
167
+ //
168
+ // the instance will be constructed via mtmd_tokenize()
169
+ // it will be freed along with mtmd_input_chunk
170
+ MTMD_API size_t mtmd_image_tokens_get_n_tokens(const mtmd_image_tokens * image_tokens); // TODO: deprecate
171
+ MTMD_API size_t mtmd_image_tokens_get_nx (const mtmd_image_tokens * image_tokens);
172
+ MTMD_API size_t mtmd_image_tokens_get_ny (const mtmd_image_tokens * image_tokens);
173
+ MTMD_API const char * mtmd_image_tokens_get_id (const mtmd_image_tokens * image_tokens); // TODO: deprecate
174
+ // number of temporal positions (always 1 for M-RoPE, n_tokens otherwise)
175
+ MTMD_API llama_pos mtmd_image_tokens_get_n_pos (const mtmd_image_tokens * image_tokens); // TODO: deprecate
176
+
177
+ // tokenize an input text prompt and a list of bitmaps (images/audio)
178
+ // the prompt must have the input image marker (default: "<__media__>") in it
179
+ // the default marker is defined by mtmd_default_marker()
180
+ // the marker will be replaced with the image/audio chunk
181
+ // for example:
182
+ // "here is an image: <__media__>\ndescribe it in detail."
183
+ // this will gives 3 chunks:
184
+ // 1. "here is an image: <start_of_image>"
185
+ // 2. (image/audio tokens)
186
+ // 3. "<end_of_image>\ndescribe it in detail."
187
+ // number of bitmaps must be equal to the number of markers in the prompt
188
+ // this function is thread-safe (shared ctx)
189
+ // return values:
190
+ // 0 on success
191
+ // 1 on number of bitmaps not matching the number of markers
192
+ // 2 on image preprocessing error
193
+ MTMD_API int32_t mtmd_tokenize(mtmd_context * ctx,
194
+ mtmd_input_chunks * output,
195
+ const mtmd_input_text * text,
196
+ const mtmd_bitmap ** bitmaps,
197
+ size_t n_bitmaps);
198
+
199
+ // returns 0 on success
200
+ // TODO: deprecate
201
+ MTMD_API int32_t mtmd_encode(mtmd_context * ctx,
202
+ const mtmd_image_tokens * image_tokens);
203
+
204
+ // returns 0 on success
205
+ MTMD_API int32_t mtmd_encode_chunk(mtmd_context * ctx,
206
+ const mtmd_input_chunk * chunk);
207
+
208
+ // get output embeddings from the last encode pass
209
+ // the reading size (in bytes) is equal to:
210
+ // llama_model_n_embd(model) * mtmd_input_chunk_get_n_tokens(chunk) * sizeof(float)
211
+ MTMD_API float * mtmd_get_output_embd(mtmd_context * ctx);
212
+
213
+ /////////////////////////////////////////
214
+
215
+ // test function, to be used in test-mtmd-c-api.c
216
+ MTMD_API mtmd_input_chunks * mtmd_test_create_input_chunks(void);
217
+
218
+ #ifdef __cplusplus
219
+ } // extern "C"
220
+ #endif
221
+
222
+ //
223
+ // C++ wrappers
224
+ //
225
+
226
+ #ifdef __cplusplus
227
+
228
+ namespace mtmd {
229
+
230
+ struct mtmd_context_deleter {
231
+ void operator()(mtmd_context * val) { mtmd_free(val); }
232
+ };
233
+ using context_ptr = std::unique_ptr<mtmd_context, mtmd_context_deleter>;
234
+
235
+ struct mtmd_bitmap_deleter {
236
+ void operator()(mtmd_bitmap * val) { mtmd_bitmap_free(val); }
237
+ };
238
+ using bitmap_ptr = std::unique_ptr<mtmd_bitmap, mtmd_bitmap_deleter>;
239
+
240
+ struct mtmd_input_chunks_deleter {
241
+ void operator()(mtmd_input_chunks * val) { mtmd_input_chunks_free(val); }
242
+ };
243
+ using input_chunks_ptr = std::unique_ptr<mtmd_input_chunks, mtmd_input_chunks_deleter>;
244
+
245
+ struct mtmd_input_chunk_deleter {
246
+ void operator()(mtmd_input_chunk * val) { mtmd_input_chunk_free(val); }
247
+ };
248
+ using input_chunk_ptr = std::unique_ptr<mtmd_input_chunk, mtmd_input_chunk_deleter>;
249
+
250
+ struct bitmap {
251
+ bitmap_ptr ptr;
252
+ bitmap() : ptr(nullptr) {}
253
+ bitmap(mtmd_bitmap * bitmap) : ptr(bitmap) {}
254
+ bitmap(bitmap && other) noexcept : ptr(std::move(other.ptr)) {}
255
+ bitmap(uint32_t nx, uint32_t ny, const unsigned char * data) {
256
+ ptr.reset(mtmd_bitmap_init(nx, ny, data));
257
+ }
258
+ ~bitmap() = default;
259
+ uint32_t nx() { return mtmd_bitmap_get_nx(ptr.get()); }
260
+ uint32_t ny() { return mtmd_bitmap_get_ny(ptr.get()); }
261
+ const unsigned char * data() { return mtmd_bitmap_get_data(ptr.get()); }
262
+ size_t n_bytes() { return mtmd_bitmap_get_n_bytes(ptr.get()); }
263
+ std::string id() { return mtmd_bitmap_get_id(ptr.get()); }
264
+ void set_id(const char * id) { mtmd_bitmap_set_id(ptr.get(), id); }
265
+ };
266
+
267
+ struct bitmaps {
268
+ std::vector<bitmap> entries;
269
+ ~bitmaps() = default;
270
+ // return list of pointers to mtmd_bitmap
271
+ // example:
272
+ // auto bitmaps_c_ptr = bitmaps.c_ptr();
273
+ // int32_t res = mtmd_tokenize(... bitmaps_c_ptr.data(), bitmaps_c_ptr.size());
274
+ std::vector<const mtmd_bitmap *> c_ptr() {
275
+ std::vector<const mtmd_bitmap *> res(entries.size());
276
+ for (size_t i = 0; i < entries.size(); i++) {
277
+ res[i] = entries[i].ptr.get();
278
+ }
279
+ return res;
280
+ }
281
+ };
282
+
283
+ struct input_chunks {
284
+ input_chunks_ptr ptr;
285
+ input_chunks() = default;
286
+ input_chunks(mtmd_input_chunks * chunks) : ptr(chunks) {}
287
+ ~input_chunks() = default;
288
+ size_t size() { return mtmd_input_chunks_size(ptr.get()); }
289
+ const mtmd_input_chunk * operator[](size_t idx) {
290
+ return mtmd_input_chunks_get(ptr.get(), idx);
291
+ }
292
+ };
293
+
294
+ } // namespace mtmd
295
+
296
+ #endif
297
+
298
+ #endif