-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support INFERENCE_NUM_THREADS options in LLAMA_CPP #898
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,19 +35,36 @@ std::shared_ptr<ov::ICompiledModel> LlamaCppPlugin::compile_model(const std::sha | |
} | ||
std::shared_ptr<ov::ICompiledModel> LlamaCppPlugin::compile_model(const std::string& fname, | ||
const ov::AnyMap& properties) const { | ||
return std::make_shared<LlamaCppModel>(fname, shared_from_this()); | ||
size_t num_threads = 0; | ||
auto it = properties.find(ov::inference_num_threads.name()); | ||
if (it != properties.end()) { | ||
num_threads = it->second.as<int>(); | ||
if (num_threads < 0) { | ||
OPENVINO_THROW("INFERENCE_NUM_THREADS cannot be negative"); | ||
} | ||
} else { | ||
num_threads = m_num_threads; | ||
} | ||
return std::make_shared<LlamaCppModel>(fname, shared_from_this(), num_threads); | ||
} | ||
|
||
void LlamaCppPlugin::set_property(const ov::AnyMap& properties) { | ||
for (const auto& map_entry : properties) { | ||
if (ov::inference_num_threads == map_entry.first) { | ||
int num_threads = map_entry.second.as<int>(); | ||
if (num_threads < 0) { | ||
OPENVINO_THROW("INFERENCE_NUM_THREADS cannot be negative"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's use |
||
} | ||
m_num_threads = num_threads; | ||
} | ||
OPENVINO_THROW_NOT_IMPLEMENTED("llama_cpp_plugin: setting property ", map_entry.first, "not implemented"); | ||
} | ||
} | ||
|
||
ov::Any LlamaCppPlugin::get_property(const std::string& name, const ov::AnyMap& arguments) const { | ||
if (ov::supported_properties == name) { | ||
return decltype(ov::supported_properties)::value_type( | ||
std::vector<PropertyName>({ov::device::capabilities, ov::device::full_name})); | ||
std::vector<PropertyName>({ov::device::capabilities, ov::device::full_name, ov::inference_num_threads})); | ||
} | ||
if (ov::device::capabilities == name) { | ||
return decltype(ov::device::capabilities)::value_type( | ||
|
@@ -66,6 +83,10 @@ ov::Any LlamaCppPlugin::get_property(const std::string& name, const ov::AnyMap& | |
return std::string("LLAMA_CPP"); | ||
} | ||
|
||
if (ov::inference_num_threads == name) { | ||
return m_num_threads; | ||
} | ||
|
||
OPENVINO_THROW_NOT_IMPLEMENTED("llama_cpp_plugin: getting property ", name, "not implemented"); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
project(llama_cpp_test_common) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please, add copyright |
||
|
||
add_library(llama_cpp_test_common STATIC | ||
${CMAKE_CURRENT_SOURCE_DIR}/src/llm_inference.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/src/benchmarking.cpp | ||
) | ||
target_include_directories(llama_cpp_test_common PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) | ||
target_link_libraries(llama_cpp_test_common gtest common_test_utils) |
12 changes: 12 additions & 0 deletions
12
modules/llama_cpp_plugin/tests/common/include/benchmarking.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (C) 2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#ifndef BENCHMARKING_HPP | ||
#define BENCHMARKING_HPP | ||
|
||
#include <functional> | ||
#include <vector> | ||
|
||
double measure_iterations_per_second(std::function<void(void)> iteration_fn, size_t iterations); | ||
|
||
#endif /* BENCHMARKING_HPP */ |
22 changes: 22 additions & 0 deletions
22
modules/llama_cpp_plugin/tests/common/include/llm_inference.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (C) 2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
#ifndef LLM_INFERENCE_HPP | ||
#define LLM_INFERENCE_HPP | ||
|
||
#include "model_fixture.hpp" | ||
#include "openvino/openvino.hpp" | ||
|
||
std::vector<float> infer_logits_for_tokens_with_positions(ov::InferRequest& lm, | ||
const std::vector<int64_t>& tokens, | ||
int64_t position_ids_start_value); | ||
|
||
std::vector<int64_t> generate_n_tokens_with_positions(ov::InferRequest& lm, | ||
int64_t last_token, | ||
size_t n_tokens, | ||
int64_t position_ids_start_value); | ||
|
||
inline int64_t get_token_from_logits(const std::vector<float>& logits) { | ||
return std::max_element(logits.cbegin(), logits.cend()) - logits.cbegin(); | ||
} | ||
#endif /* LLM_INFERENCE_HPP */ |
38 changes: 38 additions & 0 deletions
38
modules/llama_cpp_plugin/tests/common/include/model_fixture.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (C) 2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#ifndef MODEL_FIXTURE_HPP | ||
#define MODEL_FIXTURE_HPP | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "common_test_utils/file_utils.hpp" | ||
#include "openvino/openvino.hpp" | ||
#include "openvino/runtime/infer_request.hpp" | ||
|
||
const std::string TEST_FILES_DIR = "test_data"; | ||
const auto SEP = ov::util::FileTraits<char>::file_separator; | ||
|
||
class CompiledModelTest : public ::testing::Test { | ||
public: | ||
static void fill_unused_inputs(ov::InferRequest& infer_request, const ov::Shape& input_ids_reference_shape) { | ||
infer_request.set_tensor("attention_mask", ov::Tensor(ov::element::Type_t::i64, input_ids_reference_shape)); | ||
|
||
size_t batch_size = input_ids_reference_shape[0]; | ||
infer_request.set_tensor("beam_idx", ov::Tensor(ov::element::Type_t::i32, ov::Shape{batch_size})); | ||
} | ||
|
||
protected: | ||
void SetUp() override { | ||
const std::string plugin_name = "LLAMA_CPP"; | ||
ov::Core core; | ||
|
||
const std::string model_file_name = "gpt2.gguf"; | ||
const std::string model_file = | ||
ov::test::utils::getCurrentWorkingDir() + SEP + TEST_FILES_DIR + SEP + model_file_name; | ||
model = core.compile_model(model_file, plugin_name); | ||
} | ||
ov::CompiledModel model; | ||
}; | ||
|
||
#endif /* MODEL_FIXTURE_HPP */ |
26 changes: 26 additions & 0 deletions
26
modules/llama_cpp_plugin/tests/common/src/benchmarking.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (C) 2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#ifndef BENCHMARKING_CPP | ||
#define BENCHMARKING_CPP | ||
|
||
#include "benchmarking.hpp" | ||
|
||
#include <algorithm> | ||
#include <chrono> | ||
|
||
double measure_iterations_per_second(std::function<void(void)> iteration_fn, size_t iterations) { | ||
std::vector<float> iteration_times_s(iterations); | ||
|
||
for (size_t i = 0; i < iterations; i++) { | ||
auto start = std::chrono::steady_clock::now(); | ||
iteration_fn(); | ||
auto end = std::chrono::steady_clock::now(); | ||
iteration_times_s[i] = std::chrono::duration<double>(end - start).count(); | ||
} | ||
|
||
std::sort(iteration_times_s.begin(), iteration_times_s.end()); | ||
return 1.0 / iteration_times_s[iteration_times_s.size() / 2]; | ||
} | ||
|
||
#endif /* BENCHMARKING_CPP */ |
42 changes: 42 additions & 0 deletions
42
modules/llama_cpp_plugin/tests/common/src/llm_inference.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include "llm_inference.hpp" | ||
|
||
std::vector<float> infer_logits_for_tokens_with_positions(ov::InferRequest& lm, | ||
const std::vector<int64_t>& tokens, | ||
int64_t position_ids_start_value) { | ||
auto input_ids_tensor = ov::Tensor(ov::element::Type_t::i64, {1, tokens.size()}); | ||
std::copy(tokens.begin(), tokens.end(), input_ids_tensor.data<int64_t>()); | ||
lm.set_tensor("input_ids", input_ids_tensor); | ||
|
||
ov::Tensor position_ids = lm.get_tensor("position_ids"); | ||
position_ids.set_shape(input_ids_tensor.get_shape()); | ||
std::iota(position_ids.data<int64_t>(), | ||
position_ids.data<int64_t>() + position_ids.get_size(), | ||
position_ids_start_value); | ||
|
||
CompiledModelTest::fill_unused_inputs(lm, input_ids_tensor.get_shape()); | ||
lm.infer(); | ||
|
||
size_t vocab_size = lm.get_tensor("logits").get_shape().back(); | ||
float* logits = lm.get_tensor("logits").data<float>() + (input_ids_tensor.get_size() - 1) * vocab_size; | ||
std::vector<float> logits_vector(vocab_size); | ||
std::copy(logits, logits + vocab_size, logits_vector.begin()); | ||
return logits_vector; | ||
} | ||
|
||
std::vector<int64_t> generate_n_tokens_with_positions(ov::InferRequest& lm, | ||
int64_t last_token, | ||
size_t n_tokens, | ||
int64_t position_ids_start_value) { | ||
size_t cnt = 0; | ||
std::vector<int64_t> out_token_ids; | ||
out_token_ids.push_back(last_token); | ||
|
||
while (cnt < n_tokens) { | ||
std::vector<float> logits_curr = | ||
infer_logits_for_tokens_with_positions(lm, {out_token_ids.back()}, cnt + position_ids_start_value); | ||
int64_t out_token = std::max_element(logits_curr.begin(), logits_curr.end()) - logits_curr.begin(); | ||
out_token_ids.push_back(out_token); | ||
cnt++; | ||
} | ||
return out_token_ids; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright (C) 2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
set(TARGET_NAME llama_cpp_func_tests) | ||
|
||
ov_add_test_target( | ||
NAME ${TARGET_NAME} | ||
ROOT ${CMAKE_CURRENT_SOURCE_DIR} | ||
DEPENDENCIES | ||
llama_cpp_plugin | ||
LINK_LIBRARIES | ||
openvino::runtime::dev | ||
common_test_utils | ||
gtest | ||
llama_cpp_test_common | ||
INCLUDES | ||
"${LlamaCppPlugin_SOURCE_DIR}/include" | ||
"${LlamaCppPlugin_SOURCE_DIR}/tests/common/include" | ||
ADD_CLANG_FORMAT | ||
LABELS | ||
OV UNIT LLAMA_CPP | ||
) | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OPENVINO_ASSERT(num_threads > 0, "INFERENCE_NUM_THREADS should be positive")