Skip to content

Commit

Permalink
Merge pull request #2 from ilya-lavrenov/jinja-integration-pavel
Browse files Browse the repository at this point in the history
Integrate JinjaCpp
  • Loading branch information
pavel-esir authored May 10, 2024
2 parents c6620d9 + 24faefe commit f274b93
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 47 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
67 changes: 51 additions & 16 deletions src/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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_FILE:openvino_tokenizers>\")
# 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)
40 changes: 16 additions & 24 deletions src/cpp/src/llm_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include "utils.hpp"
#include <nlohmann/json.hpp>

// #include <jinja2cpp/template.h>
// #include <jinja2cpp/template_env.h>
// #include "generation_config.hpp"
#include <jinja2cpp/template.h>
#include <jinja2cpp/template_env.h>
#include "openvino/genai/generation_config.hpp"


namespace ov {
Expand Down Expand Up @@ -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", "<s>"},
// {"eos_token", "</s>"}, // todo: load from config
// {"add_generation_prompt", true},
// };
jinja2::ValuesMap message {{"role", role}, {"content", prompt}};
jinja2::ValuesMap params = {
{"messages", jinja2::ValuesList({message})},
{"bos_token", "<s>"},
{"eos_token", "</s>"}, // todo: load from config
{"add_generation_prompt", true},
};

// return tpl.RenderAsString(params).value();

std::stringstream result_prompt;
result_prompt << "<|user|>\n" << prompt << "</s>\n<|assistant|>\n"; // hardcode template for TinyLlama
// result_prompt << "<bos><start_of_turn>user\n" << prompt << "<end_of_turn>\n<start_of_turn>model"; // Gemma-7b-it
// result_prompt << "<s>[INST] " << input << " [/INST]"; // LLama-2-7b

return result_prompt.str();
return tpl.RenderAsString(params).value();
}

void ov::LLMPipeline::start_chat() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Copyright (C) 2018-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#

include(FetchContent)
FetchContent_Declare(
pybind11
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion thirdparty/Jinja2Cpp
Submodule Jinja2Cpp deleted from a853f8
1 change: 0 additions & 1 deletion thirdparty/nlohmann_json
Submodule nlohmann_json deleted from 199dea
2 changes: 1 addition & 1 deletion thirdparty/openvino_tokenizers
Submodule openvino_tokenizers updated 56 files
+33 −0 .github/dependabot.yml
+18 −0 .github/dependency_review.yml
+25 −16 .github/labeler.yml
+11 −3 .github/workflows/labeler.yml
+5 −2 .github/workflows/linux.yml
+5 −2 .github/workflows/mac.yml
+8 −1 .github/workflows/sdl.yml
+5 −2 .github/workflows/windows.yml
+1 −1 CMakeLists.txt
+3 −0 Jenkinsfile
+187 −110 README.md
+6 −4 pyproject.toml
+7 −5 python/openvino_tokenizers/__init__.py
+76 −0 python/openvino_tokenizers/build_tokenizer.py
+14 −1 python/openvino_tokenizers/cli.py
+3 −0 python/openvino_tokenizers/convert_tokenizer.py
+59 −29 python/openvino_tokenizers/hf_parser.py
+1 −1 python/openvino_tokenizers/str_pack.py
+129 −38 python/openvino_tokenizers/tokenizer_pipeline.py
+4 −2 python/openvino_tokenizers/utils.py
+1 −1 requirements-build.txt
+29 −8 src/CMakeLists.txt
+24 −28 src/bpe_tokenizer.cpp
+1 −1 src/bpe_tokenizer.hpp
+22 −6 src/case_fold.cpp
+9 −2 src/case_fold.hpp
+73 −0 src/equal_str.cpp
+40 −0 src/equal_str.hpp
+40 −0 src/fuze.cpp
+35 −0 src/fuze.hpp
+28 −11 src/ov_extension.cpp
+82 −0 src/ragged_to_ragged.cpp
+41 −0 src/ragged_to_ragged.hpp
+47 −0 src/ragged_to_sparse.cpp
+36 −0 src/ragged_to_sparse.hpp
+31 −7 src/regex_normalization.cpp
+13 −7 src/regex_normalization.hpp
+20 −5 src/regex_split.cpp
+7 −4 src/regex_split.hpp
+56 −81 src/sentence_piece.cpp
+3 −3 src/sentence_piece.hpp
+371 −104 src/tensorflow_translators.cpp
+9 −4 src/tensorflow_translators.hpp
+6 −0 src/tokenizer.hpp
+111 −0 src/trie_tokenizer.cpp
+54 −0 src/trie_tokenizer.hpp
+14 −18 src/utils.cpp
+3 −1 src/utils.hpp
+11 −16 src/vocab_decoder.cpp
+1 −2 src/vocab_decoder.hpp
+74 −0 src/vocab_encoder.cpp
+45 −0 src/vocab_encoder.hpp
+22 −25 src/wordpiece_tokenizer.cpp
+1 −1 src/wordpiece_tokenizer.hpp
+1 −1 tests/pass_rates.json
+34 −17 tests/tokenizers_test.py

0 comments on commit f274b93

Please sign in to comment.