Skip to content

Commit 9234bd6

Browse files
Ding, KevinDing, Kevin
authored andcommitted
SWDEV-1 - Updated public llamacpp with amdchat changes
Updated public llamacpp with amdchat changes patches
1 parent 13aeb7a commit 9234bd6

File tree

7 files changed

+575
-2
lines changed

7 files changed

+575
-2
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ else()
4646
endif()
4747

4848
option(BUILD_SHARED_LIBS "build shared libraries" ${BUILD_SHARED_LIBS_DEFAULT})
49+
option(AMD_CHAT_SERVER_DLL "amd: amd chat server dll" OFF)
50+
option(AMD_CHAT_SERVER_HTTP "amd: amd chat server http server" OFF)
4951

5052
if (WIN32)
5153
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)

common/log.cpp

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
#include <cstdio>
77
#include <mutex>
88
#include <sstream>
9+
#include <fstream>
910
#include <thread>
1011
#include <vector>
1112

13+
bool use_amd_log = false;
14+
1215
int common_log_verbosity_thold = LOG_DEFAULT_LLAMA;
1316

1417
void common_log_set_verbosity_thold(int verbosity) {
@@ -372,7 +375,14 @@ void common_log_free(struct common_log * log) {
372375
void common_log_add(struct common_log * log, enum ggml_log_level level, const char * fmt, ...) {
373376
va_list args;
374377
va_start(args, fmt);
375-
log->add(level, fmt, args);
378+
if (use_amd_log)
379+
{
380+
amd_log_add(level, fmt, args);
381+
}
382+
else
383+
{
384+
log->add(level, fmt, args);
385+
}
376386
va_end(args);
377387
}
378388

@@ -391,3 +401,65 @@ void common_log_set_prefix(struct common_log * log, bool prefix) {
391401
void common_log_set_timestamps(struct common_log * log, bool timestamps) {
392402
log->set_timestamps(timestamps);
393403
}
404+
405+
std::stringstream amd_server_log;
406+
std::mutex amd_server_log_mutex;
407+
408+
void amd_log_set()
409+
{
410+
use_amd_log = true;
411+
}
412+
413+
void amd_log_add(enum ggml_log_level level, const char* text) {
414+
// if (level >= GGML_LOG_LEVEL_DEBUG)
415+
// return;
416+
if (level == GGML_LOG_LEVEL_DEBUG && common_log_verbosity_thold < LOG_DEFAULT_DEBUG) {
417+
return;
418+
}
419+
420+
std::unique_lock<std::mutex> lock(amd_server_log_mutex);
421+
422+
amd_server_log << text;
423+
}
424+
425+
426+
void amd_log_add(enum ggml_log_level level, const char* fmt, va_list args) {
427+
// if (level >= GGML_LOG_LEVEL_DEBUG)
428+
// return;
429+
if (level == GGML_LOG_LEVEL_DEBUG && common_log_verbosity_thold < LOG_DEFAULT_DEBUG) {
430+
return;
431+
}
432+
433+
std::unique_lock<std::mutex> lock(amd_server_log_mutex);
434+
435+
std::vector<char> msg;
436+
437+
va_list args_copy;
438+
va_copy(args_copy, args);
439+
const size_t n = vsnprintf(msg.data(), msg.size(), fmt, args);
440+
if (n >= msg.size()) {
441+
msg.resize(n + 1);
442+
vsnprintf(msg.data(), msg.size(), fmt, args_copy);
443+
}
444+
445+
amd_server_log << msg.data();
446+
}
447+
448+
void amd_log_flush_to_file(char* log_file)
449+
{
450+
std::unique_lock<std::mutex> lock(amd_server_log_mutex);
451+
452+
if (NULL != log_file)
453+
{
454+
std::ofstream log(log_file, std::ios::app);
455+
log << amd_server_log.str();
456+
}
457+
458+
amd_server_log.str("");
459+
}
460+
461+
void amd_llama_log_callback(ggml_log_level level, const char* text, void* user_data) {
462+
(void)user_data;
463+
amd_log_add(level, text);
464+
}
465+

common/log.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#define LOG_DEFAULT_DEBUG 1
2525
#define LOG_DEFAULT_LLAMA 0
2626

27+
extern bool use_amd_log;
28+
2729
// needed by the LOG_TMPL macro to avoid computing log arguments if the verbosity lower
2830
// set via common_log_set_verbosity()
2931
extern int common_log_verbosity_thold;
@@ -80,6 +82,12 @@ void common_log_set_timestamps(struct common_log * log, bool timestamps)
8082
// this will avoid calling expensive_function() if LOG_DEFAULT_DEBUG > common_log_verbosity_thold
8183
//
8284

85+
void amd_log_set();
86+
void amd_log_add(enum ggml_log_level level, const char* text);
87+
void amd_log_add(enum ggml_log_level level, const char* fmt, va_list args);
88+
void amd_log_flush_to_file(char* log_file);
89+
void amd_llama_log_callback(ggml_log_level level, const char* text, void* user_data);
90+
8391
#define LOG_TMPL(level, verbosity, ...) \
8492
do { \
8593
if ((verbosity) <= common_log_verbosity_thold) { \

tools/server/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
if (AMD_CHAT_SERVER_DLL)
2+
set(TARGET server)
3+
else()
14
set(TARGET llama-server)
5+
endif()
26

37
option(LLAMA_SERVER_SSL "Build SSL support for the server" OFF)
48

@@ -9,9 +13,14 @@ if (MINGW)
913
add_compile_definitions(_WIN32_WINNT=${GGML_WIN_VER})
1014
endif()
1115

16+
if (AMD_CHAT_SERVER_DLL)
17+
message(STATUS "***AMD*** building server.dll")
18+
endif()
19+
1220
set(TARGET_SRCS
1321
server.cpp
1422
utils.hpp
23+
dllmain.cpp
1524
)
1625
set(PUBLIC_ASSETS
1726
index.html.gz
@@ -30,7 +39,11 @@ foreach(asset ${PUBLIC_ASSETS})
3039
set_source_files_properties(${output} PROPERTIES GENERATED TRUE)
3140
endforeach()
3241

42+
if (AMD_CHAT_SERVER_DLL)
43+
add_library(${TARGET} SHARED ${TARGET_SRCS})
44+
else()
3345
add_executable(${TARGET} ${TARGET_SRCS})
46+
endif()
3447
install(TARGETS ${TARGET} RUNTIME)
3548

3649
target_include_directories(${TARGET} PRIVATE ../mtmd)

tools/server/dllmain.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "Windows.h"
2+
3+
BOOL APIENTRY DllMain( HMODULE hModule,
4+
DWORD ul_reason_for_call,
5+
LPVOID lpReserved
6+
)
7+
{
8+
switch (ul_reason_for_call)
9+
{
10+
case DLL_PROCESS_ATTACH:
11+
case DLL_THREAD_ATTACH:
12+
case DLL_THREAD_DETACH:
13+
case DLL_PROCESS_DETACH:
14+
break;
15+
}
16+
return TRUE;
17+
}
18+

0 commit comments

Comments
 (0)