From 24faefe3c5b57cb96113ed83dc38da56eb4353be Mon Sep 17 00:00:00 2001 From: Ilya Lavrenov Date: Fri, 10 May 2024 17:42:26 +0400 Subject: [PATCH] Integrate JinjaCpp --- CMakeLists.txt | 4 +- src/CMakeLists.txt | 4 +- src/cpp/CMakeLists.txt | 67 ++++++++++++++----- src/cpp/src/llm_pipeline.cpp | 40 +++++------ .../CMakeLists.txt | 4 ++ .../py_generate_pipeline.cpp | 0 thirdparty/Jinja2Cpp | 1 - thirdparty/nlohmann_json | 1 - thirdparty/openvino_tokenizers | 2 +- 9 files changed, 76 insertions(+), 47 deletions(-) rename src/{python-bindings => python}/CMakeLists.txt (84%) rename src/{python-bindings => python}/py_generate_pipeline.cpp (100%) delete mode 160000 thirdparty/Jinja2Cpp delete mode 160000 thirdparty/nlohmann_json diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c55fba075..1627347ade 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,10 @@ -# Copyright (C) 2018-2023 Intel Corporation +# Copyright (C) 2018-2024 Intel Corporation # SPDX-License-Identifier: Apache-2.0 # cmake_minimum_required(VERSION 3.15) -project(openvino_genai) +project(openvino_genai) add_subdirectory(src) add_subdirectory(text_generation/causal_lm/cpp) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dad9bd54a1..99d80180a2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,6 @@ -# Copyright (C) 2018-2023 Intel Corporation +# Copyright (C) 2018-2024 Intel Corporation # SPDX-License-Identifier: Apache-2.0 # -add_subdirectory(python-bindings) add_subdirectory(cpp) +add_subdirectory(python) diff --git a/src/cpp/CMakeLists.txt b/src/cpp/CMakeLists.txt index bbdea5b1ab..247db07bdc 100644 --- a/src/cpp/CMakeLists.txt +++ b/src/cpp/CMakeLists.txt @@ -1,25 +1,60 @@ -# Generate Pipeline library +# Copyright (C) 2018-2024 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 +# -set(JINJA2CPP_DEPS_MODE internal) +# Dependencies + +include(FetchContent) + +FetchContent_Declare(nlohmann_json + URL https://github.com/nlohmann/json/archive/refs/tags/v3.11.3.tar.gz + URL_HASH SHA256=0d8ef5af7f9794e3263480193c491549b2ba6cc74bb018906202ada498a79406) +FetchContent_MakeAvailable(nlohmann_json) + +function(ov_genai_build_jinja2cpp) + FetchContent_Declare(jinja2cpp + URL https://github.com/ilya-lavrenov/Jinja2Cpp/archive/a5d002cbf44469775556daea14ba3ccdba1e365a.tar.gz + URL_HASH SHA256=5aa5378d9acf3c44dfb607fd7f16f48b17ffa6495c219957901e9191ffe28900) + + FetchContent_GetProperties(jinja2cpp) + if(NOT jinja2cpp_POPULATED) + FetchContent_Populate(jinja2cpp) + + set(BUILD_SHARED_LIBS OFF) + set(JINJA2CPP_INSTALL OFF CACHE BOOL "") + set(JINJA2CPP_CXX_STANDARD 17 CACHE STRING "") + set(JINJA2CPP_BUILD_SHARED OFF CACHE BOOL "") + set(JINJA2CPP_USE_REGEX "std" CACHE STRING "") + set(JINJA2CPP_WITH_JSON_BINDINGS "none" CACHE STRING "") + set(JINJA2CPP_STRICT_WARNINGS OFF CACHE BOOL "") + set(JINJA2CPP_PIC ON CACHE BOOL "") + + add_subdirectory("${jinja2cpp_SOURCE_DIR}" "${jinja2cpp_BINARY_DIR}" EXCLUDE_FROM_ALL) + endif() +endfunction() + +ov_genai_build_jinja2cpp() add_subdirectory(../../thirdparty/openvino_tokenizers/ "${CMAKE_CURRENT_BINARY_DIR}/openvino_tokenizers/") -add_subdirectory(../../thirdparty/nlohmann_json/ "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_json/") -# todo: remove hardcodes and make submodule work -# include_directories($ENV{HOME}/opt/jinja2cpp/include) -# add_subdirectory(../../../thirdparty/Jinja2Cpp/ "${CMAKE_CURRENT_BINARY_DIR}/Jinja2Cpp/") -# include_directories(../../../thirdparty/inja/include/Jinja2Cpp) +find_package(OpenVINO REQUIRED COMPONENTS Runtime) + +# Library +file(GLOB SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp") set(TARGET_NAME generate_pipeline_lib) -file(GLOB SOURCE_FILES "src/*.cpp") add_library(${TARGET_NAME} SHARED ${SOURCE_FILES}) -target_include_directories(${TARGET_NAME} PRIVATE ../../text_generation/causal_lm/cpp/) -target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) -find_package(OpenVINO REQUIRED COMPONENTS Runtime) -target_link_libraries(${TARGET_NAME} PUBLIC openvino::runtime) -target_link_libraries(${TARGET_NAME} PUBLIC nlohmann_json::nlohmann_json) + +target_include_directories(${TARGET_NAME} + # TODO: remove it, because beam_search algo should not be exposed to end users + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../text_generation/causal_lm/cpp/ + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) + +target_link_libraries(${TARGET_NAME} PUBLIC openvino::runtime PRIVATE nlohmann_json::nlohmann_json jinja2cpp) + target_compile_definitions(${TARGET_NAME} PRIVATE OPENVINO_TOKENIZERS_PATH=\"$\") -# target_link_libraries(${TARGET_NAME} PRIVATE $ENV{HOME}/opt/jinja2cpp/lib/static/libjinja2cpp.a) # todo: remove hardcode -set_target_properties(${TARGET_NAME} PROPERTIES CXX_STANDARD 17) -set_target_properties(${TARGET_NAME} PROPERTIES CXX_STANDARD_REQUIRED ON) + +set_target_properties(${TARGET_NAME} PROPERTIES + CXX_STANDARD_REQUIRED ON + CXX_STANDARD 17) diff --git a/src/cpp/src/llm_pipeline.cpp b/src/cpp/src/llm_pipeline.cpp index de984efb44..e9eb7ea0d8 100644 --- a/src/cpp/src/llm_pipeline.cpp +++ b/src/cpp/src/llm_pipeline.cpp @@ -11,9 +11,9 @@ #include "utils.hpp" #include -// #include -// #include -// #include "generation_config.hpp" +#include +#include +#include "openvino/genai/generation_config.hpp" namespace ov { @@ -313,29 +313,21 @@ std::string ov::LLMPipeline::apply_chat_template(std::string prompt, std::string } std::string ov::LLMPipeline::LLMPipelineImpl::apply_chat_template(std::string prompt, std::string role) const { - // todo: temporary disable for easier and faster build - // jinja2::TemplateEnv env; - // env.GetSettings().lstripBlocks = true; - // env.GetSettings().trimBlocks = true; - // jinja2::Template tpl(&env); - // tpl.Load(m_chat_template); + jinja2::TemplateEnv env; + env.GetSettings().lstripBlocks = true; + env.GetSettings().trimBlocks = true; + jinja2::Template tpl(&env); + tpl.Load(m_chat_template); - // jinja2::ValuesMap message {{"role", role}, {"content", prompt}}; - // jinja2::ValuesMap params = { - // {"messages", jinja2::ValuesList({message})}, - // {"bos_token", ""}, - // {"eos_token", ""}, // todo: load from config - // {"add_generation_prompt", true}, - // }; + jinja2::ValuesMap message {{"role", role}, {"content", prompt}}; + jinja2::ValuesMap params = { + {"messages", jinja2::ValuesList({message})}, + {"bos_token", ""}, + {"eos_token", ""}, // todo: load from config + {"add_generation_prompt", true}, + }; - // return tpl.RenderAsString(params).value(); - - std::stringstream result_prompt; - result_prompt << "<|user|>\n" << prompt << "\n<|assistant|>\n"; // hardcode template for TinyLlama - // result_prompt << "user\n" << prompt << "\nmodel"; // Gemma-7b-it - // result_prompt << "[INST] " << input << " [/INST]"; // LLama-2-7b - - return result_prompt.str(); + return tpl.RenderAsString(params).value(); } void ov::LLMPipeline::start_chat() { diff --git a/src/python-bindings/CMakeLists.txt b/src/python/CMakeLists.txt similarity index 84% rename from src/python-bindings/CMakeLists.txt rename to src/python/CMakeLists.txt index a030fed156..d951d25d25 100644 --- a/src/python-bindings/CMakeLists.txt +++ b/src/python/CMakeLists.txt @@ -1,3 +1,7 @@ +# Copyright (C) 2018-2024 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 +# + include(FetchContent) FetchContent_Declare( pybind11 diff --git a/src/python-bindings/py_generate_pipeline.cpp b/src/python/py_generate_pipeline.cpp similarity index 100% rename from src/python-bindings/py_generate_pipeline.cpp rename to src/python/py_generate_pipeline.cpp diff --git a/thirdparty/Jinja2Cpp b/thirdparty/Jinja2Cpp deleted file mode 160000 index a853f8e9f7..0000000000 --- a/thirdparty/Jinja2Cpp +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a853f8e9f784de53b11973a47af0b20b0167f6f3 diff --git a/thirdparty/nlohmann_json b/thirdparty/nlohmann_json deleted file mode 160000 index 199dea11b1..0000000000 --- a/thirdparty/nlohmann_json +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 199dea11b17c533721b26249e2dcaee6ca1d51d3 diff --git a/thirdparty/openvino_tokenizers b/thirdparty/openvino_tokenizers index 0e4bb32ca3..c754503462 160000 --- a/thirdparty/openvino_tokenizers +++ b/thirdparty/openvino_tokenizers @@ -1 +1 @@ -Subproject commit 0e4bb32ca3412f589e1d094faa8b0aad19ee47ca +Subproject commit c754503462f569b648b598d57ff91ea57bb8deb1