Skip to content

Commit 24faefe

Browse files
committed
Integrate JinjaCpp
1 parent c6620d9 commit 24faefe

9 files changed

+76
-47
lines changed

CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Copyright (C) 2018-2023 Intel Corporation
1+
# Copyright (C) 2018-2024 Intel Corporation
22
# SPDX-License-Identifier: Apache-2.0
33
#
44

55
cmake_minimum_required(VERSION 3.15)
6-
project(openvino_genai)
76

7+
project(openvino_genai)
88

99
add_subdirectory(src)
1010
add_subdirectory(text_generation/causal_lm/cpp)

src/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Copyright (C) 2018-2023 Intel Corporation
1+
# Copyright (C) 2018-2024 Intel Corporation
22
# SPDX-License-Identifier: Apache-2.0
33
#
44

5-
add_subdirectory(python-bindings)
65
add_subdirectory(cpp)
6+
add_subdirectory(python)

src/cpp/CMakeLists.txt

+51-16
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,60 @@
1-
# Generate Pipeline library
1+
# Copyright (C) 2018-2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
24

3-
set(JINJA2CPP_DEPS_MODE internal)
5+
# Dependencies
6+
7+
include(FetchContent)
8+
9+
FetchContent_Declare(nlohmann_json
10+
URL https://github.com/nlohmann/json/archive/refs/tags/v3.11.3.tar.gz
11+
URL_HASH SHA256=0d8ef5af7f9794e3263480193c491549b2ba6cc74bb018906202ada498a79406)
12+
FetchContent_MakeAvailable(nlohmann_json)
13+
14+
function(ov_genai_build_jinja2cpp)
15+
FetchContent_Declare(jinja2cpp
16+
URL https://github.com/ilya-lavrenov/Jinja2Cpp/archive/a5d002cbf44469775556daea14ba3ccdba1e365a.tar.gz
17+
URL_HASH SHA256=5aa5378d9acf3c44dfb607fd7f16f48b17ffa6495c219957901e9191ffe28900)
18+
19+
FetchContent_GetProperties(jinja2cpp)
20+
if(NOT jinja2cpp_POPULATED)
21+
FetchContent_Populate(jinja2cpp)
22+
23+
set(BUILD_SHARED_LIBS OFF)
24+
set(JINJA2CPP_INSTALL OFF CACHE BOOL "")
25+
set(JINJA2CPP_CXX_STANDARD 17 CACHE STRING "")
26+
set(JINJA2CPP_BUILD_SHARED OFF CACHE BOOL "")
27+
set(JINJA2CPP_USE_REGEX "std" CACHE STRING "")
28+
set(JINJA2CPP_WITH_JSON_BINDINGS "none" CACHE STRING "")
29+
set(JINJA2CPP_STRICT_WARNINGS OFF CACHE BOOL "")
30+
set(JINJA2CPP_PIC ON CACHE BOOL "")
31+
32+
add_subdirectory("${jinja2cpp_SOURCE_DIR}" "${jinja2cpp_BINARY_DIR}" EXCLUDE_FROM_ALL)
33+
endif()
34+
endfunction()
35+
36+
ov_genai_build_jinja2cpp()
437

538
add_subdirectory(../../thirdparty/openvino_tokenizers/ "${CMAKE_CURRENT_BINARY_DIR}/openvino_tokenizers/")
6-
add_subdirectory(../../thirdparty/nlohmann_json/ "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_json/")
739

8-
# todo: remove hardcodes and make submodule work
9-
# include_directories($ENV{HOME}/opt/jinja2cpp/include)
10-
# add_subdirectory(../../../thirdparty/Jinja2Cpp/ "${CMAKE_CURRENT_BINARY_DIR}/Jinja2Cpp/")
11-
# include_directories(../../../thirdparty/inja/include/Jinja2Cpp)
40+
find_package(OpenVINO REQUIRED COMPONENTS Runtime)
41+
42+
# Library
1243

44+
file(GLOB SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
1345

1446
set(TARGET_NAME generate_pipeline_lib)
15-
file(GLOB SOURCE_FILES "src/*.cpp")
1647
add_library(${TARGET_NAME} SHARED ${SOURCE_FILES})
17-
target_include_directories(${TARGET_NAME} PRIVATE ../../text_generation/causal_lm/cpp/)
18-
target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
19-
find_package(OpenVINO REQUIRED COMPONENTS Runtime)
20-
target_link_libraries(${TARGET_NAME} PUBLIC openvino::runtime)
21-
target_link_libraries(${TARGET_NAME} PUBLIC nlohmann_json::nlohmann_json)
48+
49+
target_include_directories(${TARGET_NAME}
50+
# TODO: remove it, because beam_search algo should not be exposed to end users
51+
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../text_generation/causal_lm/cpp/
52+
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
53+
54+
target_link_libraries(${TARGET_NAME} PUBLIC openvino::runtime PRIVATE nlohmann_json::nlohmann_json jinja2cpp)
55+
2256
target_compile_definitions(${TARGET_NAME} PRIVATE OPENVINO_TOKENIZERS_PATH=\"$<TARGET_FILE:openvino_tokenizers>\")
23-
# target_link_libraries(${TARGET_NAME} PRIVATE $ENV{HOME}/opt/jinja2cpp/lib/static/libjinja2cpp.a) # todo: remove hardcode
24-
set_target_properties(${TARGET_NAME} PROPERTIES CXX_STANDARD 17)
25-
set_target_properties(${TARGET_NAME} PROPERTIES CXX_STANDARD_REQUIRED ON)
57+
58+
set_target_properties(${TARGET_NAME} PROPERTIES
59+
CXX_STANDARD_REQUIRED ON
60+
CXX_STANDARD 17)

src/cpp/src/llm_pipeline.cpp

+16-24
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
#include "utils.hpp"
1212
#include <nlohmann/json.hpp>
1313

14-
// #include <jinja2cpp/template.h>
15-
// #include <jinja2cpp/template_env.h>
16-
// #include "generation_config.hpp"
14+
#include <jinja2cpp/template.h>
15+
#include <jinja2cpp/template_env.h>
16+
#include "openvino/genai/generation_config.hpp"
1717

1818

1919
namespace ov {
@@ -313,29 +313,21 @@ std::string ov::LLMPipeline::apply_chat_template(std::string prompt, std::string
313313
}
314314

315315
std::string ov::LLMPipeline::LLMPipelineImpl::apply_chat_template(std::string prompt, std::string role) const {
316-
// todo: temporary disable for easier and faster build
317-
// jinja2::TemplateEnv env;
318-
// env.GetSettings().lstripBlocks = true;
319-
// env.GetSettings().trimBlocks = true;
320-
// jinja2::Template tpl(&env);
321-
// tpl.Load(m_chat_template);
316+
jinja2::TemplateEnv env;
317+
env.GetSettings().lstripBlocks = true;
318+
env.GetSettings().trimBlocks = true;
319+
jinja2::Template tpl(&env);
320+
tpl.Load(m_chat_template);
322321

323-
// jinja2::ValuesMap message {{"role", role}, {"content", prompt}};
324-
// jinja2::ValuesMap params = {
325-
// {"messages", jinja2::ValuesList({message})},
326-
// {"bos_token", "<s>"},
327-
// {"eos_token", "</s>"}, // todo: load from config
328-
// {"add_generation_prompt", true},
329-
// };
322+
jinja2::ValuesMap message {{"role", role}, {"content", prompt}};
323+
jinja2::ValuesMap params = {
324+
{"messages", jinja2::ValuesList({message})},
325+
{"bos_token", "<s>"},
326+
{"eos_token", "</s>"}, // todo: load from config
327+
{"add_generation_prompt", true},
328+
};
330329

331-
// return tpl.RenderAsString(params).value();
332-
333-
std::stringstream result_prompt;
334-
result_prompt << "<|user|>\n" << prompt << "</s>\n<|assistant|>\n"; // hardcode template for TinyLlama
335-
// result_prompt << "<bos><start_of_turn>user\n" << prompt << "<end_of_turn>\n<start_of_turn>model"; // Gemma-7b-it
336-
// result_prompt << "<s>[INST] " << input << " [/INST]"; // LLama-2-7b
337-
338-
return result_prompt.str();
330+
return tpl.RenderAsString(params).value();
339331
}
340332

341333
void ov::LLMPipeline::start_chat() {

src/python-bindings/CMakeLists.txt src/python/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Copyright (C) 2018-2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
15
include(FetchContent)
26
FetchContent_Declare(
37
pybind11

thirdparty/Jinja2Cpp

Submodule Jinja2Cpp deleted from a853f8e

thirdparty/nlohmann_json

Submodule nlohmann_json deleted from 199dea1

thirdparty/openvino_tokenizers

Submodule openvino_tokenizers updated 56 files

0 commit comments

Comments
 (0)