-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support INFERENCE_NUM_THREADS options in LLAMA_CPP (#898)
* Support INFERENCE_NUM_THREADS options in LLAMA_CPP * Run func tests during build * Update modules/llama_cpp_plugin/tests/functional/src/threading.cpp Co-authored-by: Ilya Lavrenov <[email protected]> --------- Co-authored-by: Ilya Lavrenov <[email protected]>
- Loading branch information
1 parent
8f23817
commit cb1c767
Showing
19 changed files
with
382 additions
and
27 deletions.
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
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) | ||
|
||
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.