Skip to content

Commit 7328f0f

Browse files
committed
Update GenieAPIService, support phi4mm & qwen2.5vl multimodal model.
1 parent ceed1d9 commit 7328f0f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3986
-1898
lines changed

samples/genie/c++/Service/CMakeLists.txt

Lines changed: 15 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
CMAKE_MINIMUM_REQUIRED(VERSION 3.4...3.18)
1010
set(CMAKE_CXX_STANDARD 17)
1111

12-
################### Build Service ###################
1312
project(GenieAPIService)
1413

1514
option(USE_MNN "Enable the use mnn platform" OFF)
@@ -18,90 +17,34 @@ option(USE_GGUF "Enable the use gguf platform" OFF)
1817

1918
set(SCRIPT_PATH ${CMAKE_SOURCE_DIR}/scripts)
2019
include(${SCRIPT_PATH}/version.cmake)
21-
include(${SCRIPT_PATH}/platform.cmake)
2220

23-
set(BUILD_PATH ${CMAKE_SOURCE_DIR}/GenieSerivce_v${QAI_APP_BUILDER_VERSION})
21+
set(G_EXTERNAL_DIR ${CMAKE_SOURCE_DIR}/../External)
22+
set(G_EXTERNAL_INCLUDE_PATH ${CMAKE_BINARY_DIR}/include)
23+
24+
message(STATUS ${G_EXTERNAL_INCLUDE_PATH})
25+
set(G_EXTERNAL_BIN_PATH ${CMAKE_BINARY_DIR}/bin)
26+
set(G_EXTERNAL_LIB_PATH ${CMAKE_BINARY_DIR}/lib)
27+
28+
set(BUILD_PATH ${CMAKE_SOURCE_DIR}/GenieService_v${QAI_APP_BUILDER_VERSION})
2429
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${BUILD_PATH})
2530
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${BUILD_PATH})
26-
set(APP "GenieAPIService")
27-
2831
file(REMOVE_RECURSE ${BUILD_PATH})
2932

30-
list(APPEND SOURCE_HEADER
31-
${EXTERNAL_HEADER_PATH}
32-
${CMAKE_CURRENT_SOURCE_DIR}/src/core/include
33-
)
34-
35-
list(APPEND SERVICE_SOURCES
36-
src/core/context_base.cpp
37-
src/core/chat_history.cpp
38-
src/core/utils.cpp
39-
src/core/model_manager.cpp
40-
src/context/genie.cpp
41-
src/GenieAPIService.cpp
42-
src/processor/harmony.cpp
43-
src/processor/general.cpp
44-
src/core/response/response_tools.cpp
45-
src/core/response/response_dispatcher.cpp
46-
)
47-
4833
add_compile_options(
49-
-DQAI_APP_BUILDER_MAJOR_VERSION=${QAI_APP_BUILDER_MAJOR_VERSION}
50-
-DQAI_APP_BUILDER_MINOR_VERSION=${QAI_APP_BUILDER_MINOR_VERSION}
51-
-DQAI_APP_BUILDER_PATCH_VERSION=${QAI_APP_BUILDER_PATCH_VERSION}
5234
/MD
5335
/O2
5436
/Ob2
5537
/utf-8
5638
)
5739

58-
if (USE_MNN)
59-
add_definitions(-DUSE_MNN)
60-
list(APPEND SERVICE_SOURCES
61-
src/context/mnn.cpp
62-
)
63-
endif ()
64-
65-
if (USE_GGUF)
66-
add_definitions(-DUSE_GGUF)
67-
list(APPEND SERVICE_SOURCES
68-
src/context/llama_cpp.cpp
69-
)
70-
endif ()
71-
72-
configure_file(
73-
${SCRIPT_PATH}/version.rc.in
74-
${CMAKE_BINARY_DIR}/version.rc
75-
@ONLY
76-
)
77-
78-
include_directories(${SOURCE_HEADER})
79-
add_executable(${APP} ${SERVICE_SOURCES})
80-
target_compile_definitions(${APP} PUBLIC -DNOMINMAX -DBUILDING_LIBCURL -D_HAS_STD_BYTE=0)
81-
target_link_libraries(${APP} PRIVATE
82-
Shlwapi
83-
Shell32
84-
Wldap32.lib
85-
Iphlpapi.lib
86-
${EXTERNAL_LIBS}
40+
include_directories(${G_EXTERNAL_INCLUDE_PATH}
41+
${G_EXTERNAL_DIR}/json/single_include
42+
${CMAKE_CURRENT_SOURCE_DIR}/src/common
8743
)
44+
link_directories(${G_EXTERNAL_LIB_PATH})
45+
add_subdirectory(src/GenieAPIService)
46+
add_subdirectory(examples/GenieAPIClient)
8847

89-
################### Build Client ###################
90-
set(CLIENT_APP "GenieAPIClient")
91-
add_executable(${CLIENT_APP} src/GenieAPIClient.cpp)
92-
target_compile_definitions(${CLIENT_APP} PUBLIC -DNOMINMAX -DCURL_STATICLIB)
93-
target_link_libraries(${CLIENT_APP} PRIVATE advapi32 Wldap32 Iphlpapi ws2_32 Shlwapi Shell32 libcurl)
94-
95-
################### Generate Release ###################
96-
add_custom_command(
97-
TARGET ${APP} PRE_BUILD
48+
add_custom_target(GenieSerivesPacking
9849
COMMAND ${CMAKE_COMMAND} -E copy ${BUILD_VERSION_FILE} -t ${BUILD_PATH}
9950
)
100-
101-
foreach (FILE IN LISTS EXTERNAL_BIN)
102-
add_custom_command(
103-
TARGET ${APP} POST_BUILD
104-
COMMAND ${CMAKE_COMMAND} -E echo copy ${FILE} to ${BUILD_PATH}
105-
COMMAND ${CMAKE_COMMAND} -E copy ${FILE} -t ${BUILD_PATH}
106-
)
107-
endforeach ()

samples/genie/c++/Service/Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#=============================================================================
2+
#
3+
# Copyright (c) 2023, Qualcomm Innovation Center, Inc. All rights reserved.
4+
#
5+
# SPDX-License-Identifier: BSD-3-Clause
6+
#
7+
#=============================================================================
8+
9+
# specify compiler
10+
export CXX := clang++-14 -D QUALLA_MAJOR_VERSION=2 -D QUALLA_MINOR_VERSION=0 -D QUALLA_PATCH_VERSION=0
11+
export PATH := $(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64/bin:$(PATH)
12+
13+
.PHONY: all android
14+
.DEFAULT: android
15+
16+
all: android
17+
18+
android:
19+
@echo "-------------------- Building GenieAPIServer for android -------------------- "
20+
@$(ANDROID_NDK_ROOT)/ndk-build.cmd APP_ALLOW_MISSING_DEPS=true APP_ABI="arm64-v8a" NDK_PROJECT_PATH=./ NDK_APPLICATION_MK=make/Application.mk APP_BUILD_SCRIPT=make/Android.mk || (echo "-------------------- server android build failed --------------------"; exit 1; )
21+
@echo "-------------------- GenieAPIServer for android build succeeded -------------------- "
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#=============================================================================
2+
#
3+
# Copyright (c) 2025, Qualcomm Innovation Center, Inc. All rights reserved.
4+
#
5+
# SPDX-License-Identifier: BSD-3-Clause
6+
#
7+
#=============================================================================
8+
9+
project(GenieAPIClient)
10+
11+
ExternalProject_Add(Libcurl
12+
SOURCE_DIR ${G_EXTERNAL_DIR}/curl
13+
CMAKE_ARGS
14+
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}
15+
-DBUILD_SHARED_LIBS=OFF
16+
-DBUILD_CURL_EXE=OFF
17+
-DBUILD_TESTING=OFF
18+
-DCURL_USE_LIBPSL=OFF
19+
-DENABLE_UNICODE=ON
20+
BUILD_IN_SOURCE ON
21+
)
22+
23+
add_executable(${PROJECT_NAME}
24+
GenieAPIClient.cpp
25+
${CMAKE_SOURCE_DIR}/src/common/utils.cpp
26+
)
27+
target_include_directories(${PROJECT_NAME} PRIVATE ${EXTERNAL_HEADER_PATH})
28+
target_compile_definitions(${PROJECT_NAME} PRIVATE -DCURL_STATICLIB -DNOMINMAX)
29+
target_link_libraries(${PROJECT_NAME} PRIVATE libcurl Iphlpapi ws2_32 Wldap32)

samples/genie/c++/Service/src/GenieAPIClient.cpp renamed to samples/genie/c++/Service/examples/GenieAPIClient/GenieAPIClient.cpp

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
//==============================================================================
22
//
33
// Copyright (c) 2025, Qualcomm Innovation Center, Inc. All rights reserved.
4-
//
4+
//
55
// SPDX-License-Identifier: BSD-3-Clause
66
//
77
//==============================================================================
88

99
#include <string>
1010
#include <sstream>
11-
#include <curl/curl.h>
12-
#include <core/log.h>
13-
#include <nlohmann/json.hpp>
11+
#include <log.h>
1412
#include <thread>
1513
#include <random>
14+
#include <fstream>
15+
#include <base64.h>
16+
#include <curl/curl.h>
17+
#include <nlohmann/json.hpp>
1618

1719
using json = nlohmann::ordered_json;
20+
1821
std::string SVR_URL = "http://10.92.143.176:8910/v1/chat/completions";
1922

2023
static class CLI
2124
{
2225
public:
2326
std::string prompt{"hello"};
27+
std::string picture_path_;
2428
std::string system{"You are a helpful assistant."};
2529
std::string model{"IBM-Granite-v3.1-8B"};
2630
bool loop{false};
@@ -76,6 +80,8 @@ void CLI::Init(int argc, CharT **argv)
7680
cli_info.system = ArgToString(argv[++i]);
7781
else if (ArgToString(arg) == "--model" && i + 1 < argc)
7882
cli_info.model = ArgToString(argv[++i]);
83+
else if (ArgToString(arg) == "--img" && i + 1 < argc)
84+
cli_info.picture_path_ = ArgToString(argv[++i]);
7985
else if (ArgToString(arg) == "--loop")
8086
cli_info.loop = true;
8187
else if (ArgToString(arg) == "--ip")
@@ -132,7 +138,7 @@ size_t write_callback_stream(char *ptr, size_t size, size_t nmemb, void *userdat
132138
j["choices"][0].contains("delta") &&
133139
j["choices"][0]["delta"].contains("content"))
134140
{
135-
My_Log{}.original(true) << j["choices"][0]["delta"]["content"].get<std::string>() ;
141+
My_Log{}.original(true) << j["choices"][0]["delta"]["content"].get<std::string>();
136142
}
137143

138144
}
@@ -153,8 +159,8 @@ size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp)
153159
return size * nmemb;
154160
}
155161

156-
std::string
157-
build_request_body(const std::string &model, const std::string &prompt, const std::string &system, bool stream)
162+
template<typename T>
163+
std::string build_request_body(const std::string &model, const T &prompt, const std::string &system, bool stream)
158164
{
159165
json body;
160166
body["model"] = model;
@@ -235,6 +241,38 @@ void DoLoop()
235241
}
236242
}
237243

244+
std::string EncodeImage(std::string &path)
245+
{
246+
std::ifstream in(path, std::ios::binary);
247+
if (!in.good())
248+
{
249+
My_Log{My_Log::Level::kError} << "open file path failed\n";
250+
return "";
251+
}
252+
253+
in.seekg(0, std::ios::end);
254+
std::vector<char> buf(in.tellg());
255+
in.seekg(0, std::ios::beg);
256+
if (!in.read(reinterpret_cast<char *>(buf.data()), buf.size()))
257+
{
258+
My_Log{My_Log::Level::kError} << "read form file failed\n";
259+
return "";
260+
}
261+
262+
char *out_buf = new char[BASE64_ENCODE_OUT_SIZE(buf.size())];
263+
int size = Base64Encode(buf.data(), buf.size(), out_buf);
264+
if (size == 0)
265+
{
266+
My_Log{My_Log::Level::kError} << "encode to binrary failed\n";
267+
delete[] out_buf;
268+
return "";
269+
}
270+
271+
std::string out(const_cast<const char *>(out_buf), size);
272+
delete[] out_buf;
273+
return out;
274+
}
275+
238276
#ifdef WIN32
239277
#define MAIN int wmain(int argc, wchar_t *argv[])
240278
#else
@@ -251,16 +289,32 @@ MAIN
251289
#endif
252290

253291
curl_global_init(CURL_GLOBAL_ALL);
292+
std::string req_body;
293+
if (cli_info.picture_path_.empty())
294+
{
295+
req_body = build_request_body(cli_info.model, cli_info.prompt, cli_info.system, cli_info.stream);
296+
}
297+
else
298+
{
299+
auto image = EncodeImage(cli_info.picture_path_);
300+
if (image.empty())
301+
{
302+
return -1;
303+
}
304+
json j;
305+
j["question"] = cli_info.prompt;
306+
j["image"] = image;
307+
req_body = build_request_body(cli_info.model, j, cli_info.system, cli_info.stream);
308+
}
254309
try
255310
{
256311
cli_info.loop ?
257-
DoLoop() : Do(build_request_body(cli_info.model, cli_info.prompt, cli_info.system, cli_info.stream));
312+
DoLoop() : Do(req_body);
258313
}
259314
catch (const std::exception &e)
260315
{
261316
My_Log{My_Log::Level::kError} << e.what();
262317
}
263318
curl_global_cleanup();
264319
return 0;
265-
}
266-
320+
}

samples/genie/c++/Service/make/Android.mk renamed to samples/genie/c++/Service/scripts/Android.mk

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ endef
1515
#============================ Define Common Variables ===============================================================
1616
PACKAGE_C_INCLUDES += -I $(LOCAL_PATH)/../../External/cpp-httplib/
1717
PACKAGE_C_INCLUDES += -I $(LOCAL_PATH)/../../External/json/include/
18-
PACKAGE_C_INCLUDES += -I $(LOCAL_PATH)/../../External/fmt/include/
1918
PACKAGE_C_INCLUDES += -I $(LOCAL_PATH)/../../External/curl/include/curl/
2019
PACKAGE_C_INCLUDES += -I $(LOCAL_PATH)/../../External/curl/include/
2120
PACKAGE_C_INCLUDES += -I $(LOCAL_PATH)/../../External/curl/lib/
22-
PACKAGE_C_INCLUDES += -I $(LOCAL_PATH)/../../External/CmdParser/
21+
PACKAGE_C_INCLUDES += -I $(LOCAL_PATH)/../../External/CLI11/include
2322
PACKAGE_C_INCLUDES += -I $(LOCAL_PATH)/../
2423
PACKAGE_C_INCLUDES += -I ${QNN_SDK_ROOT}include/Genie/
25-
PACKAGE_C_INCLUDES += -I $(LOCAL_PATH)/../src/core/include
24+
PACKAGE_C_INCLUDES += -I $(LOCAL_PATH)/../src/utils
25+
PACKAGE_C_INCLUDES += -I $(LOCAL_PATH)/../src/common
2626

2727
#========================== Define libGenie.so variables =============================================
2828
include $(CLEAR_VARS)
@@ -42,16 +42,16 @@ LOCAL_C_INCLUDES := $(PACKAGE_C_INCLUDES)
4242
LOCAL_MODULE := GenieAPIService
4343
LOCAL_SHARED_LIBRARIES := libGenie
4444
LOCAL_LDLIBS := -llog
45-
LOCAL_SRC_FILES := ../src/core/context_base.cpp \
46-
../src/core/chat_history.cpp \
47-
../src/core/utils.cpp \
48-
../src/core/model_manager.cpp \
49-
../src/context/genie.cpp \
50-
../src/GenieAPIService.cpp \
51-
../src/processor/harmony.cpp \
52-
../src/processor/general.cpp \
53-
../src/core/response/response_tools.cpp \
54-
../src/core/response/response_dispatcher.cpp
45+
LOCAL_SRC_FILES := ../src/GenieAPIService/context/context_base.cpp \
46+
../src/GenieAPIService/context/genie.cpp \
47+
../src/GenieAPIService/chat_request_handler/chat_request_handler.cpp \
48+
../src/GenieAPIService/GenieAPIService.cpp \
49+
../src/GenieAPIService/model/model_manager.cpp \
50+
../src/GenieAPIService/utils/utils.cpp \
51+
../src/GenieAPIService/processor/harmony.cpp \
52+
../src/GenieAPIService/processor/general.cpp \
53+
../src/GenieAPIService/response/response_tools.cpp \
54+
../src/GenieAPIService/response/response_dispatcher.cpp
5555
include $(BUILD_SHARED_LIBRARY)
5656
#========================== Define Service Lib variables =============================================
5757
include $(CLEAR_VARS)
@@ -60,16 +60,16 @@ LOCAL_C_INCLUDES := $(PACKAGE_C_INCLUDES)
6060
LOCAL_MODULE := JNIGenieAPIService
6161
LOCAL_SHARED_LIBRARIES := libGenie
6262
LOCAL_LDLIBS := -llog
63-
LOCAL_SRC_FILES := ../src/core/context_base.cpp \
64-
../src/core/chat_history.cpp \
65-
../src/core/utils.cpp \
66-
../src/core/model_manager.cpp \
67-
../src/context/genie.cpp \
68-
../src/GenieAPIService.cpp \
69-
../src/processor/harmony.cpp \
70-
../src/processor/general.cpp \
71-
../src/core/response/response_tools.cpp \
72-
../src/core/response/response_dispatcher.cpp
63+
LOCAL_SRC_FILES := ../src/GenieAPIService/context/context_base.cpp \
64+
../src/GenieAPIService/context/genie.cpp \
65+
../src/GenieAPIService/chat_request_handler/chat_request_handler.cpp \
66+
../src/GenieAPIService/GenieAPIService.cpp \
67+
../src/GenieAPIService/model/model_manager.cpp \
68+
../src/GenieAPIService/utils/utils.cpp \
69+
../src/GenieAPIService/processor/harmony.cpp \
70+
../src/GenieAPIService/processor/general.cpp \
71+
../src/GenieAPIService/response/response_tools.cpp \
72+
../src/GenieAPIService/response/response_dispatcher.cpp
7373
include $(BUILD_SHARED_LIBRARY)
7474
#========================== Define client app variables =============================================
7575
include $(CLEAR_VARS)

samples/genie/c++/Service/make/Application.mk renamed to samples/genie/c++/Service/scripts/Application.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ APP_ABI := arm64-v8a
1010
APP_STL := c++_static
1111
APP_PLATFORM := android-21
1212
APP_MODULES := JNIGenieAPIService GenieAPIService GenieAPIClient
13-
APP_CPPFLAGS += -std=c++17 -O3 -Wall -frtti -fexceptions -fvisibility=hidden -DSPILLFILL -DQUALLA_ENGINE_QNN_HTP=TRUE -DQUALLA_ENGINE_QNN_CPU=TRUE -DQUALLA_ENGINE_QNN_GPU=TRUE -DFMT_HEADER_ONLY -D QUALLA_MAJOR_VERSION=2 -D QUALLA_MINOR_VERSION=0 -D QUALLA_PATCH_VERSION=0 -Wno-unused-function
14-
APP_LDFLAGS += -lc -lm -ldl -Wl,--strip-all
13+
APP_CPPFLAGS += -std=c++17 -g -O2 -Wall -frtti -fexceptions -fvisibility=hidden -DSPILLFILL -DQUALLA_ENGINE_QNN_HTP=TRUE -DQUALLA_ENGINE_QNN_CPU=TRUE -DQUALLA_ENGINE_QNN_GPU=TRUE -DFMT_HEADER_ONLY -D QAI_APP_BUILDER_MAJOR_VERSION=2 -D QAI_APP_BUILDER_MINOR_VERSION=1 -D QAI_APP_BUILDER_PATCH_VERSION=2 -Wno-unused-functio
14+
APP_LDFLAGS += -lc -lm -ldl -Wl,--strip-all

0 commit comments

Comments
 (0)