Skip to content

Commit

Permalink
feat: log generated tokens per second (#11)
Browse files Browse the repository at this point in the history
Co-authored-by: vansangpfiev <[email protected]>
  • Loading branch information
vansangpfiev and sangjanai authored Jun 20, 2024
1 parent 79f1299 commit c5f73d0
Showing 1 changed file with 16 additions and 0 deletions.
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

0 comments on commit c5f73d0

Please sign in to comment.