Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion src/common/AutoModel/automodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,11 @@ bool AutoModel::_shared_insert(chat_meta_info_t& meta_info, std::vector<int>& to

auto prefill_end_time = this->profiler_list[PREFILL_TIME].stop(tokens.size());
meta_info.prefill_duration = (uint64_t)time_utils::duration_ns(prefill_start_time, prefill_end_time).first;
meta_info.prompt_tokens = tokens.size();
// `tokens` was trimmed to the uncached suffix above, so add the prefix served
// from the KV cache back on: usage.prompt_tokens is the whole prompt, and the
// cached part is reported separately rather than subtracted.
meta_info.cached_prompt_tokens = static_cast<int>(skip_count);
meta_info.prompt_tokens = static_cast<int>(skip_count + tokens.size());

if (meta_info.stop_reason == CANCEL_DETECTED) {
return false;
Expand Down
7 changes: 6 additions & 1 deletion src/common/AutoModel/modeling_qwen3_5_omni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,12 @@ bool Qwen3_5_Omni::insert(chat_meta_info_t& meta_info, lm_uniform_input_t& input

auto prefill_end = this->profiler_list[PREFILL_TIME].stop(tokens.size());
meta_info.prefill_duration = (uint64_t)time_utils::duration_ns(prefill_start, prefill_end).first;
meta_info.prompt_tokens = tokens.size();
// As in AutoModel::_shared_insert, `tokens` holds only the uncached suffix, so
// the cached prefix is added back to report the whole prompt. The relevant count
// is `skip_count`, which is what was erased from `tokens` above -- not the earlier
// `prefix_skip_count`, which only trims the multi-modal payload.
meta_info.cached_prompt_tokens = static_cast<int>(skip_count);
meta_info.prompt_tokens = static_cast<int>(skip_count + tokens.size());

this->total_tokens += tokens.size();

Expand Down
3 changes: 3 additions & 0 deletions src/common/AutoModel/modeling_qwen3vl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,9 @@ bool Qwen3VL_Flash::insert(chat_meta_info_t& meta_info, lm_uniform_input_t& inpu

meta_info.prefill_duration = (uint64_t)time_utils::duration_ns(prefill_start_time, prefill_end_time).first;
meta_info.prompt_tokens = static_cast<int>(tokens.size()); // report full prompt length to caller
// The pinned system prefix is already in the kv cache, so it is part of the
// prompt but was not prefilled on this turn.
meta_info.cached_prompt_tokens = skip;

if (meta_info.stop_reason == CANCEL_DETECTED) {
return false;
Expand Down
5 changes: 3 additions & 2 deletions src/include/AutoModel/automodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ typedef enum {

struct chat_meta_info_t {
int max_prefill_len;
int prompt_tokens;
int prompt_tokens; // whole prompt, cached prefix included
int cached_prompt_tokens; // subset of prompt_tokens served from the KV cache
int generated_tokens;
uint64_t total_duration; // in nanoseconds
uint64_t load_duration; // in nanoseconds
Expand All @@ -118,7 +119,7 @@ struct chat_meta_info_t {
bool restore_allowed;
tool_choice_t tool_choice;

chat_meta_info_t() : max_prefill_len(0), prompt_tokens(0), generated_tokens(0), total_duration(0), load_duration(0), prefill_duration(0), decoding_duration(0), stop_reason(EOT_DETECTED), restore_allowed(false), tool_choice(TOOL_CHOICE_AUTO) {}
chat_meta_info_t() : max_prefill_len(0), prompt_tokens(0), cached_prompt_tokens(0), generated_tokens(0), total_duration(0), load_duration(0), prefill_duration(0), decoding_duration(0), stop_reason(EOT_DETECTED), restore_allowed(false), tool_choice(TOOL_CHOICE_AUTO) {}
};

typedef enum {
Expand Down
4 changes: 3 additions & 1 deletion src/server/rest_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1303,13 +1303,14 @@ void RestHandler::handle_openai_chat_completion(const json& request,
{"choices", choices},
{"usage", {
{"prompt_tokens", meta_info.prompt_tokens},
{"prompt_tokens_details", {{"cached_tokens", meta_info.cached_prompt_tokens}}},
{"completion_tokens", meta_info.generated_tokens},
{"total_tokens", meta_info.prompt_tokens + meta_info.generated_tokens},
{"kv_token_occupancy_rate_percentage", (float)this->auto_chat_engine->get_current_context_length() / (float)this->auto_chat_engine->get_max_length() * 100},
{"load_duration", static_cast<double>(meta_info.load_duration) / 1'000'000'000},
{"prefill_duration_ttft", static_cast<double>(meta_info.prefill_duration) / 1'000'000'000},
{"decoding_duration", static_cast<double>(meta_info.decoding_duration) / 1'000'000'000},
{"prefill_speed_tps", static_cast<double>(meta_info.prompt_tokens) / static_cast<double>(meta_info.prefill_duration) * 1'000'000'000},
{"prefill_speed_tps", static_cast<double>(meta_info.prompt_tokens - meta_info.cached_prompt_tokens) / static_cast<double>(meta_info.prefill_duration) * 1'000'000'000},
{"decoding_speed_tps", static_cast<double>(meta_info.generated_tokens) / static_cast<double>(meta_info.decoding_duration) * 1'000'000'000},
}},
{"service_tier", "default"}
Expand Down Expand Up @@ -1511,6 +1512,7 @@ void RestHandler::handle_openai_completion(const json& request,
})},
{"usage", {
{"prompt_tokens", meta_info.prompt_tokens},
{"prompt_tokens_details", {{"cached_tokens", meta_info.cached_prompt_tokens}}},
{"completion_tokens", meta_info.generated_tokens},
{"total_tokens", meta_info.prompt_tokens + meta_info.generated_tokens}
}}
Expand Down
4 changes: 3 additions & 1 deletion src/server/streaming_ostream_openai.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class streaming_buf_openai : public std::streambuf {
})},
{"usage", {
{"prompt_tokens", meta_info.prompt_tokens},
{"prompt_tokens_details", {{"cached_tokens", meta_info.cached_prompt_tokens}}},
{"completion_tokens", meta_info.generated_tokens},
{"total_tokens", meta_info.prompt_tokens + meta_info.generated_tokens}
}}
Expand Down Expand Up @@ -497,6 +498,7 @@ class streaming_buf_openai_chat : public std::streambuf {
})},
{"usage", {
{"prompt_tokens", meta_info.prompt_tokens},
{"prompt_tokens_details", {{"cached_tokens", meta_info.cached_prompt_tokens}}},
{"completion_tokens", meta_info.generated_tokens},
{"total_tokens", meta_info.prompt_tokens + meta_info.generated_tokens},
{"active_kv_tokens", this->auto_chat_engine->get_current_context_length()},
Expand All @@ -505,7 +507,7 @@ class streaming_buf_openai_chat : public std::streambuf {
{"load_duration", static_cast<double>(meta_info.load_duration) / 1'000'000'000},
{"prefill_duration_ttft", static_cast<double>(meta_info.prefill_duration) / 1'000'000'000},
{"decoding_duration", static_cast<double>(meta_info.decoding_duration) / 1'000'000'000},
{"prefill_speed_tps", static_cast<double>(meta_info.prompt_tokens) / static_cast<double>(meta_info.prefill_duration) * 1'000'000'000},
{"prefill_speed_tps", static_cast<double>(meta_info.prompt_tokens - meta_info.cached_prompt_tokens) / static_cast<double>(meta_info.prefill_duration) * 1'000'000'000},
{"decoding_speed_tps", static_cast<double>(meta_info.generated_tokens) / static_cast<double>(meta_info.decoding_duration) * 1'000'000'000},
}}
};
Expand Down
Loading