|
15 | 15 | #include "utils.hpp"
|
16 | 16 | #include "text_callback_streamer.hpp"
|
17 | 17 |
|
| 18 | +#ifdef _WIN32 |
| 19 | +# include <windows.h> |
| 20 | +# define MAX_ABS_PATH _MAX_PATH |
| 21 | +# define get_absolute_path(result, path) _fullpath(result, path.c_str(), MAX_ABS_PATH) |
| 22 | +#else |
| 23 | +# include <dlfcn.h> |
| 24 | +# include <limits.h> |
| 25 | +# define MAX_ABS_PATH PATH_MAX |
| 26 | +# define get_absolute_path(result, path) realpath(path.c_str(), result) |
| 27 | +namespace { |
| 28 | +std::string get_absolute_file_path(const std::string& path) { |
| 29 | + std::string absolutePath; |
| 30 | + absolutePath.resize(MAX_ABS_PATH); |
| 31 | + std::ignore = get_absolute_path(&absolutePath[0], path); |
| 32 | + if (!absolutePath.empty()) { |
| 33 | + // on Linux if file does not exist or no access, function will return NULL, but |
| 34 | + // `absolutePath` will contain resolved path |
| 35 | + absolutePath.resize(absolutePath.find('\0')); |
| 36 | + return std::string(absolutePath); |
| 37 | + } |
| 38 | + std::stringstream ss; |
| 39 | + ss << "Can't get absolute file path for [" << path << "], err = " << strerror(errno); |
| 40 | + throw std::runtime_error(ss.str()); |
| 41 | +} |
| 42 | +} |
| 43 | +#endif |
| 44 | + |
18 | 45 | namespace {
|
19 | 46 |
|
20 | 47 | ov::genai::GenerationConfig from_config_json_if_exists(const std::string& path) {
|
@@ -56,6 +83,39 @@ std::string from_tokenizer_json_if_exists(const std::string& path) {
|
56 | 83 | return res;
|
57 | 84 | }
|
58 | 85 |
|
| 86 | +std::filesystem::path with_openvino_tokenizers(const std::filesystem::path& path) { |
| 87 | +#ifdef _WIN32 |
| 88 | + constexpr char tokenizers[] = "openvino_tokenizers.dll"; |
| 89 | +#elif __linux__ |
| 90 | + constexpr char tokenizers[] = "libopenvino_tokenizers.so"; |
| 91 | +#elif __APPLE__ |
| 92 | + constexpr char tokenizers[] = "libopenvino_tokenizers.dylib"; |
| 93 | +#endif |
| 94 | + return path.parent_path() / tokenizers; |
| 95 | +} |
| 96 | + |
| 97 | +std::string get_ov_genai_library_path() { |
| 98 | +#ifdef _WIN32 |
| 99 | + CHAR genai_library_path[MAX_PATH]; |
| 100 | + HMODULE hm = NULL; |
| 101 | + if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
| 102 | + reinterpret_cast<LPSTR>(get_ov_genai_library_path), |
| 103 | + &hm)) { |
| 104 | + std::stringstream ss; |
| 105 | + ss << "GetModuleHandle returned " << GetLastError(); |
| 106 | + throw std::runtime_error(ss.str()); |
| 107 | + } |
| 108 | + GetModuleFileNameA(hm, (LPSTR)genai_library_path, sizeof(genai_library_path)); |
| 109 | + return std::string(genai_library_path); |
| 110 | +#elif defined(__APPLE__) || defined(__linux__) || defined(__EMSCRIPTEN__) |
| 111 | + Dl_info info; |
| 112 | + dladdr(reinterpret_cast<void*>(get_ov_genai_library_path), &info); |
| 113 | + return get_absolute_file_path(info.dli_fname).c_str(); |
| 114 | +#else |
| 115 | +# error "Unsupported OS" |
| 116 | +#endif // _WIN32 |
| 117 | +} |
| 118 | + |
59 | 119 | }
|
60 | 120 |
|
61 | 121 | namespace ov {
|
@@ -161,7 +221,11 @@ ov::genai::LLMPipeline::LLMPipelineImpl::LLMPipelineImpl(
|
161 | 221 | const std::string& ov_tokenizers_path
|
162 | 222 | ):
|
163 | 223 | m_model_runner{ov::Core{}.compile_model(path + "/openvino_model.xml", device, config).create_infer_request()},
|
164 |
| - m_tokenizer{Tokenizer(path, device, ov_tokenizers_path)}, |
| 224 | + m_tokenizer{ |
| 225 | + ov_tokenizers_path.empty() |
| 226 | + ? Tokenizer(path, device, with_openvino_tokenizers(get_ov_genai_library_path()).string()) |
| 227 | + : Tokenizer(path, device, ov_tokenizers_path) |
| 228 | + }, |
165 | 229 | m_generation_config{from_config_json_if_exists(path)},
|
166 | 230 | m_chat_template{from_tokenizer_json_if_exists(path)}
|
167 | 231 | {}
|
|
0 commit comments