-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathllama.cpp.patch
More file actions
376 lines (342 loc) · 14 KB
/
llama.cpp.patch
File metadata and controls
376 lines (342 loc) · 14 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
diff --git a/common/common.h b/common/common.h
index c5a803757..255c108ca 100644
--- a/common/common.h
+++ b/common/common.h
@@ -539,7 +539,9 @@ struct common_params {
std::vector<std::string> api_keys;
std::string ssl_file_key = ""; // NOLINT
- std::string ssl_file_cert = ""; // NOLINT
+ std::string ssl_file_cert = "";
+ std::string ssl_key = ""; // NOLINT
+ std::string ssl_cert = ""; // NOLINT
std::map<std::string, std::string> default_template_kwargs;
diff --git a/common/log.cpp b/common/log.cpp
index b17d2b62c..f33876340 100644
--- a/common/log.cpp
+++ b/common/log.cpp
@@ -21,7 +21,8 @@
# include <unistd.h>
#endif // defined(_WIN32)
-int common_log_verbosity_thold = LOG_DEFAULT_LLAMA;
+void (*log_callback)(const char*) = nullptr;
+int common_log_verbosity_thold = -2;
void common_log_set_verbosity_thold(int verbosity) {
common_log_verbosity_thold = verbosity;
@@ -108,6 +109,7 @@ struct common_log_entry {
}
fprintf(fcur, "%s", msg.data());
+ if (log_callback) log_callback(msg.data());
if (level == GGML_LOG_LEVEL_WARN || level == GGML_LOG_LEVEL_ERROR || level == GGML_LOG_LEVEL_DEBUG) {
fprintf(fcur, "%s", g_col[COMMON_LOG_COL_DEFAULT]);
diff --git a/common/log.h b/common/log.h
index f0f8471b5..c1eb92d7e 100644
--- a/common/log.h
+++ b/common/log.h
@@ -36,6 +36,7 @@ enum log_colors {
LOG_COLORS_ENABLED = 1,
};
+extern void (*log_callback)(const char*);
// needed by the LOG_TMPL macro to avoid computing log arguments if the verbosity lower
// set via common_log_set_verbosity()
extern int common_log_verbosity_thold;
diff --git a/ggml/src/CMakeLists.txt b/ggml/src/CMakeLists.txt
index 265023733..9fc397404 100644
--- a/ggml/src/CMakeLists.txt
+++ b/ggml/src/CMakeLists.txt
@@ -136,18 +136,6 @@ endif()
# posix_memalign came in POSIX.1-2001 / SUSv3
# M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
-# Somehow in OpenBSD whenever POSIX conformance is specified
-# some string functions rely on locale_t availability,
-# which was introduced in POSIX.1-2008, forcing us to go higher
-if (CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
- add_compile_definitions(_XOPEN_SOURCE=700)
-elseif (CMAKE_SYSTEM_NAME MATCHES "AIX")
- # Don't define _XOPEN_SOURCE. We need _ALL_SOURCE, which is the default,
- # in order to define _SC_PHYS_PAGES.
-else()
- add_compile_definitions(_XOPEN_SOURCE=600)
-endif()
-
# Data types, macros and functions related to controlling CPU affinity and
# some memory allocation are available on Linux through GNU extensions in libc
if (CMAKE_SYSTEM_NAME MATCHES "Linux" OR CMAKE_SYSTEM_NAME MATCHES "Android")
@@ -186,7 +174,7 @@ endif()
# ggml
if (GGML_BACKEND_DL AND NOT BUILD_SHARED_LIBS)
- message(FATAL_ERROR "GGML_BACKEND_DL requires BUILD_SHARED_LIBS")
+ message(WARNING "GGML_BACKEND_DL requires BUILD_SHARED_LIBS")
endif()
add_library(ggml-base
diff --git a/ggml/src/ggml-hip/CMakeLists.txt b/ggml/src/ggml-hip/CMakeLists.txt
index 80037d243..19ea71b7c 100644
--- a/ggml/src/ggml-hip/CMakeLists.txt
+++ b/ggml/src/ggml-hip/CMakeLists.txt
@@ -134,7 +134,7 @@ else()
endif()
if (GGML_STATIC)
- message(FATAL_ERROR "Static linking not supported for HIP/ROCm")
+ message(WARNING "Static linking not supported for HIP/ROCm")
endif()
target_link_libraries(ggml-hip PRIVATE ggml-base hip::host roc::rocblas roc::hipblas)
diff --git a/ggml/src/ggml-metal/ggml-metal-context.h b/ggml/src/ggml-metal/ggml-metal-context.h
index abf4b06ed..a7f73fdda 100644
--- a/ggml/src/ggml-metal/ggml-metal-context.h
+++ b/ggml/src/ggml-metal/ggml-metal-context.h
@@ -21,6 +21,7 @@ void ggml_metal_synchronize(ggml_metal_t ctx);
void ggml_metal_set_tensor_async(ggml_metal_t ctx, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size);
void ggml_metal_get_tensor_async(ggml_metal_t ctx, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size);
+void ggml_metal_get_tensor_async_staged_copy(ggml_metal_t ctx, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size);
bool ggml_metal_cpy_tensor_async(ggml_metal_t ctx_src, ggml_metal_t ctx_dst, const struct ggml_tensor * src, struct ggml_tensor * dst);
enum ggml_status ggml_metal_graph_compute (ggml_metal_t ctx, struct ggml_cgraph * gf);
diff --git a/ggml/src/ggml-metal/ggml-metal-context.m b/ggml/src/ggml-metal/ggml-metal-context.m
index 5d3a8ce41..18ca6016e 100644
--- a/ggml/src/ggml-metal/ggml-metal-context.m
+++ b/ggml/src/ggml-metal/ggml-metal-context.m
@@ -326,6 +326,66 @@ void ggml_metal_set_tensor_async(ggml_metal_t ctx, struct ggml_tensor * tensor,
}
}
+void ggml_metal_get_tensor_async_staged_copy(
+ ggml_metal_t ctx,
+ const struct ggml_tensor * tensor,
+ void * data,
+ size_t offset,
+ size_t size
+) {
+ if (!data || size == 0) {
+ return;
+ }
+
+ @autoreleasepool {
+ id<MTLDevice> device = ggml_metal_device_get_obj(ctx->dev);
+
+ // Source GPU buffer
+ struct ggml_metal_buffer_id bid_src = ggml_metal_get_buffer_id(tensor);
+ if (bid_src.metal == nil) {
+ GGML_ABORT("%s: failed to find buffer for tensor '%s'\n",
+ __func__, tensor->name);
+ }
+ bid_src.offs += offset;
+
+ // CPU-visible staging buffer
+ id<MTLBuffer> staging = [device newBufferWithLength:size
+ options:MTLResourceStorageModeShared];
+ GGML_ASSERT(staging);
+
+ // Queue GPU → staging copy
+ id<MTLCommandQueue> queue = ggml_metal_device_get_queue(ctx->dev);
+ id<MTLCommandBuffer> cmd_buf = [queue commandBuffer];
+ id<MTLBlitCommandEncoder> encoder = [cmd_buf blitCommandEncoder];
+
+ [encoder copyFromBuffer:bid_src.metal
+ sourceOffset:bid_src.offs
+ toBuffer:staging
+ destinationOffset:0
+ size:size];
+ [encoder endEncoding];
+
+ // Copy staging → user buffer when complete
+ __block id<MTLBuffer> staging_ref = [staging retain]; // Ensure cleanup
+ [cmd_buf addCompletedHandler:^(id<MTLCommandBuffer> cb) {
+ if (cb.status == MTLCommandBufferStatusCompleted) {
+ memcpy(data, staging_ref.contents, size);
+ }
+ // Always release regardless of status
+ [staging_ref release];
+ }];
+
+ [cmd_buf commit];
+
+ // Track command buffer
+ [ctx->cmd_bufs_ext addObject:cmd_buf];
+ ctx->cmd_buf_last = cmd_buf;
+ [cmd_buf retain];
+
+ [staging release]; // Balance the newBuffer call
+ }
+}
+
void ggml_metal_get_tensor_async(ggml_metal_t ctx, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size) {
@autoreleasepool {
id<MTLDevice> device = ggml_metal_device_get_obj(ctx->dev);
@@ -334,7 +394,11 @@ void ggml_metal_get_tensor_async(ggml_metal_t ctx, const struct ggml_tensor * te
options:MTLResourceStorageModeShared
deallocator:nil];
- GGML_ASSERT(buf_dst);
+ if (!buf_dst)
+ {
+ ggml_metal_get_tensor_async_staged_copy(ctx, tensor, data, offset, size);
+ return;
+ }
struct ggml_metal_buffer_id bid_src = ggml_metal_get_buffer_id(tensor);
if (bid_src.metal == nil) {
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan.cpp b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
index 23d6d39e0..052f93ad2 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan.cpp
+++ b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
@@ -104,7 +104,7 @@ static bool is_pow2(uint32_t x) { return x > 1 && (x & (x-1)) == 0; }
if (err_ != vk::Result::eSuccess) { \
fprintf(stderr, "ggml_vulkan: %s error %s at %s:%d\n", \
#err, to_string(err_).c_str(), __FILE__, __LINE__); \
- exit(1); \
+ std::terminate(); \
} \
} while (0)
diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c
index d644cca8a..789ca0174 100644
--- a/ggml/src/ggml.c
+++ b/ggml/src/ggml.c
@@ -253,7 +253,12 @@ void ggml_abort(const char * file, int line, const char * fmt, ...) {
ggml_print_backtrace();
}
- abort();
+ raise(SIGSEGV);
+#ifdef _MSC_VER
+ __assume(0);
+#else
+ __builtin_unreachable();
+#endif
}
// ggml_print_backtrace is registered with std::set_terminate by ggml.cpp
diff --git a/tools/mtmd/CMakeLists.txt b/tools/mtmd/CMakeLists.txt
index 3be3c27e8..54e2e4132 100644
--- a/tools/mtmd/CMakeLists.txt
+++ b/tools/mtmd/CMakeLists.txt
@@ -82,17 +82,3 @@ if (TARGET mtmd)
"It must not link against common")
endif()
endif()
-
-add_executable(llama-llava-cli deprecation-warning.cpp)
-add_executable(llama-gemma3-cli deprecation-warning.cpp)
-add_executable(llama-minicpmv-cli deprecation-warning.cpp)
-add_executable(llama-qwen2vl-cli deprecation-warning.cpp)
-
-set(TARGET llama-mtmd-cli)
-add_executable (${TARGET} mtmd-cli.cpp)
-set_target_properties (${TARGET} PROPERTIES OUTPUT_NAME llama-mtmd-cli)
-if(LLAMA_TOOLS_INSTALL)
- install(TARGETS ${TARGET} RUNTIME)
-endif()
-target_link_libraries (${TARGET} PRIVATE common mtmd Threads::Threads)
-target_compile_features(${TARGET} PRIVATE cxx_std_17)
diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp
index aafed4950..f80fed466 100644
--- a/tools/server/server-context.cpp
+++ b/tools/server/server-context.cpp
@@ -16,6 +16,7 @@
#include <cinttypes>
#include <memory>
#include <filesystem>
+#include <iostream>
// fix problem with std::min and std::max
#if defined(_WIN32)
@@ -58,8 +59,8 @@ struct server_slot {
// TODO: move members that belong to the task (such as `generated_text`, `has_new_line`) to task_results_state
// see https://github.com/ggml-org/llama.cpp/pull/18283#issuecomment-3710175837
- std::unique_ptr<const server_task> task;
- std::unique_ptr<const server_task> task_prev; // used for debugging
+ std::unique_ptr<server_task> task;
+ std::unique_ptr<server_task> task_prev; // used for debugging
// used to determine the slot that has been used the longest
int64_t t_last_used = -1;
@@ -546,7 +547,6 @@ public:
}
}
-private:
// note: accessing these fields outside of this class is not thread-safe
// use server_context methods instead
@@ -615,7 +615,6 @@ private:
}
sleeping = new_state;
}
-
// load the model and initialize llama_context
// this may also be called to resume from sleeping state
bool load_model(const common_params & params) {
@@ -635,6 +634,11 @@ private:
return false;
}
+ if (ctx == nullptr) {
+ SRV_ERR("ctx is null, '%s'\n", "");
+ return false;
+ }
+
vocab = llama_model_get_vocab(model);
n_ctx = llama_n_ctx(ctx);
@@ -1171,7 +1175,7 @@ private:
slot.smpl.reset();
}
- slot.task = std::make_unique<const server_task>(std::move(task));
+ slot.task = std::make_unique<server_task>(std::move(task));
slot.state = slot.task->is_child()
? SLOT_STATE_WAIT_OTHER // wait for the parent to process prompt
diff --git a/tools/server/server-http.cpp b/tools/server/server-http.cpp
index 129022a71..e2d39e2bd 100644
--- a/tools/server/server-http.cpp
+++ b/tools/server/server-http.cpp
@@ -16,6 +16,30 @@
// HTTP implementation using cpp-httplib
//
+#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
+EVP_PKEY *load_key(const std::string &key_str)
+{
+ BIO *bio = BIO_new_mem_buf(key_str.data(), (int)key_str.size());
+ if (!bio)
+ return NULL;
+ EVP_PKEY *key = PEM_read_bio_PrivateKey(bio, NULL, 0, NULL);
+ BIO_free(bio);
+ return key;
+}
+
+X509 *load_cert(const std::string &cert_str)
+{
+ BIO *bio = BIO_new_mem_buf(cert_str.data(), (int)cert_str.size());
+ if (!bio)
+ return NULL;
+ X509 *cert = (cert_str[0] == '-')
+ ? PEM_read_bio_X509(bio, NULL, NULL, NULL)
+ : d2i_X509_bio(bio, NULL);
+ BIO_free(bio);
+ return cert;
+}
+#endif
+
class server_http_context::Impl {
public:
std::unique_ptr<httplib::Server> srv;
@@ -60,6 +84,10 @@ bool server_http_context::init(const common_params & params) {
srv.reset(
new httplib::SSLServer(params.ssl_file_cert.c_str(), params.ssl_file_key.c_str())
);
+ } else if (params.ssl_key != "" && params.ssl_cert != "") {
+ LOG_INF("Running with SSL\n");
+ srv.reset(
+ new httplib::SSLServer(load_cert(params.ssl_cert), load_key(params.ssl_key)));
} else {
LOG_INF("Running without SSL\n");
srv.reset(new httplib::Server());
diff --git a/tools/server/server-queue.h b/tools/server/server-queue.h
index 164f09b19..1fc9ba027 100644
--- a/tools/server/server-queue.h
+++ b/tools/server/server-queue.h
@@ -102,6 +102,9 @@ public:
callback_sleeping_state = std::move(callback);
}
+ bool is_empty() { return queue_tasks.empty(); }
+ bool is_running() { return running; }
+
private:
void cleanup_pending_task(int id_target);
};
diff --git a/tools/server/server.cpp b/tools/server/server.cpp
index fab0bb587..0e6eaa31f 100644
--- a/tools/server/server.cpp
+++ b/tools/server/server.cpp
@@ -67,7 +67,7 @@ static server_http_context::handler_t ex_wrapper(server_http_context::handler_t
};
}
-int main(int argc, char ** argv) {
+int main_server(int argc, char ** argv) {
std::setlocale(LC_NUMERIC, "C");
// own arguments required by this example