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

feat: log generated tokens per second #11

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Changes from all 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
16 changes: 16 additions & 0 deletions src/onnx_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ void OnnxEngine::HandleChatCompletion(
params->SetInputSequences(*sequences);

auto generator = OgaGenerator::Create(*oga_model_, *params);
auto start = std::chrono::system_clock::now();
double generated_tokens = 0;
while (!generator->IsDone() && model_loaded_) {
generator->ComputeLogits();
generator->GenerateNextToken();
Expand All @@ -241,6 +243,7 @@ void OnnxEngine::HandleChatCompletion(
status["is_stream"] = true;
status["status_code"] = k200OK;
cb(std::move(status), std::move(resp_data));
generated_tokens++;
}

if (!model_loaded_) {
Expand All @@ -255,6 +258,12 @@ void OnnxEngine::HandleChatCompletion(
cb(std::move(status), std::move(respData));
return;
}
auto end = std::chrono::system_clock::now();
auto duration_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
.count();
LOG_DEBUG << "Generated tokens per second: "
<< generated_tokens / duration_ms * 1000;

LOG_INFO << "End of result";
Json::Value resp_data;
Expand All @@ -281,6 +290,7 @@ void OnnxEngine::HandleChatCompletion(
// params->SetSearchOption("repetition_penalty", req.frequency_penalty);
params->SetInputSequences(*sequences);

auto start = std::chrono::system_clock::now();
auto output_sequences = oga_model_->Generate(*params);
const auto output_sequence_length =
output_sequences->SequenceCount(0) - sequences->SequenceCount(0);
Expand All @@ -290,6 +300,12 @@ void OnnxEngine::HandleChatCompletion(
tokenizer_->Decode(output_sequence_data, output_sequence_length);

// std::cout << "Output: " << std::endl << out_string << std::endl;
auto end = std::chrono::system_clock::now();
auto duration_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
.count();
LOG_DEBUG << "Generated tokens per second: "
<< static_cast<double>(output_sequence_length) / duration_ms * 1000;

std::string to_send = out_string.p_;
auto resp_data = CreateFullReturnJson(GenerateRandomString(20), "_",
Expand Down
Loading