Skip to content
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

Update greedy_causal_lm.cpp to read EOS Token #315

Merged
merged 20 commits into from
Apr 9, 2024
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions text_generation/causal_lm/cpp/greedy_causal_lm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ int main(int argc, char* argv[]) try {
}
// Compile models
ov::Core core;

auto tokenizer_model = core.read_model(std::string{argv[1]} + "/openvino_tokenizer.xml");

core.add_extension(OPENVINO_TOKENIZERS_PATH); // OPENVINO_TOKENIZERS_PATH is defined in CMakeLists.txt
// tokenizer and detokenizer work on CPU only
ov::InferRequest tokenizer = core.compile_model(
anzr299 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -71,7 +74,6 @@ int main(int argc, char* argv[]) try {
ov::InferRequest lm = core.compile_model(
std::string{argv[1]} + "/openvino_model.xml", "CPU").create_infer_request();
auto seq_len = input_ids.get_size();

// Initialize inputs
lm.set_tensor("input_ids", input_ids);
lm.set_tensor("attention_mask", attention_mask);
Expand All @@ -91,8 +93,15 @@ int main(int argc, char* argv[]) try {
lm.get_tensor("input_ids").set_shape({BATCH_SIZE, 1});
position_ids.set_shape({BATCH_SIZE, 1});
TextStreamer text_streamer{std::move(detokenizer)};
// There's no way to extract special token values from the detokenizer for now
constexpr int64_t SPECIAL_EOS_TOKEN = 2;
// After compiling the model, before the inference loop
anzr299 marked this conversation as resolved.
Show resolved Hide resolved

auto rt_info = tokenizer_model.get_rt_info();
anzr299 marked this conversation as resolved.
Show resolved Hide resolved
if (rt_info.count("eos_token_id") > 0) {
SPECIAL_EOS_TOKEN = rt_info["eos_token_id"];
} else {
throw std::runtime_error("EOS token ID not found in model's runtime information.");
pavel-esir marked this conversation as resolved.
Show resolved Hide resolved
}


int max_sequence_length = 100;
while (out_token != SPECIAL_EOS_TOKEN && seq_len < max_sequence_length) {
Expand Down
Loading