-
Notifications
You must be signed in to change notification settings - Fork 300
refactor: clean up logging and add system status reporter #1204
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
Open
sufubao
wants to merge
3
commits into
main
Choose a base branch
from
clean_log
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+176
−101
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,47 +1,110 @@ | ||
| import time | ||
| from lightllm.utils.log_utils import init_logger | ||
| from .batch import Batch | ||
| import logging | ||
| from lightllm.utils.log_utils import init_system_status_logger | ||
|
|
||
| logger = init_logger(__name__) | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class Stats: | ||
| def __init__(self, log_status, log_stats_interval) -> None: | ||
| self.log_stats = log_status | ||
| self.log_stats_interval = log_stats_interval | ||
| self.last_log_time = time.time() | ||
| self.all_tokens = 0 | ||
| self.output_tokens = 0 | ||
| class SystemStatusReporter: | ||
| def __init__(self, args, max_total_token_num, dp_size_in_node): | ||
| self.enabled = not args.disable_log_stats | ||
| self.interval = max(5, args.log_stats_interval) | ||
| if args.log_stats_interval < 5: | ||
| logger.warning(f"log_stats_interval={args.log_stats_interval}s is below minimum, using 5s") | ||
| self.max_total_token_num = max_total_token_num | ||
| self.dp_size_in_node = dp_size_in_node | ||
| self.status_logger = init_system_status_logger("router") | ||
|
|
||
| # Accumulation counters (reset each interval) | ||
| self.last_print_time = time.time() | ||
| self.prompt_tokens = 0 | ||
| return | ||
|
|
||
| def count_prompt_tokens(self, run_batch: Batch): | ||
| if self.log_stats and run_batch is not None: | ||
| tokens = run_batch.input_tokens() | ||
| self.prompt_tokens += tokens | ||
| self.all_tokens += tokens | ||
| return | ||
|
|
||
| def count_output_tokens(self, run_batch: Batch): | ||
| if self.log_stats and run_batch is not None: | ||
| tokens = len(run_batch.reqs) | ||
| self.output_tokens += tokens | ||
| self.all_tokens += tokens | ||
| return | ||
|
|
||
| def print_stats(self): | ||
| if not self.log_stats: | ||
| return | ||
| self.output_tokens = 0 | ||
|
|
||
| # Global counters (never reset, for lifetime stats) | ||
| self.global_input_total = 0 | ||
| self.global_cache_total = 0 | ||
| self.global_mtp_output_total = 0 | ||
| self.global_mtp_accepted_total = 0 | ||
|
|
||
| def count_prompt_tokens(self, num_tokens: int): | ||
| if self.enabled: | ||
| self.prompt_tokens += num_tokens | ||
|
|
||
| def count_output_tokens(self, num_tokens: int): | ||
| if self.enabled: | ||
| self.output_tokens += num_tokens | ||
|
|
||
| def on_request_completed(self, input_len: int, output_len: int, cache_len: int, mtp_accepted: int): | ||
| if self.enabled: | ||
| self.global_input_total += input_len | ||
| self.global_cache_total += cache_len | ||
| self.global_mtp_output_total += output_len | ||
| self.global_mtp_accepted_total += mtp_accepted | ||
|
|
||
| def maybe_print( | ||
| self, | ||
| running_batch, | ||
| req_queue, | ||
| read_only_statics_mem_manager, | ||
| paused_req_num=0, | ||
| radix_cache_client=None, | ||
| disable_dynamic_prompt_cache=False, | ||
| ): | ||
| if not self.enabled: | ||
| return | ||
| now = time.time() | ||
| if now - self.last_log_time > self.log_stats_interval: | ||
| logger.debug( | ||
| f"Avg tokens(prompt+generate) throughput: {self.all_tokens/(now-self.last_log_time):8.3f} tokens/s\n" | ||
| f"Avg prompt tokens throughput: {self.prompt_tokens/(now-self.last_log_time):8.3f} tokens/s\n" | ||
| f"Avg generate tokens throughput: {self.output_tokens/(now-self.last_log_time):8.3f} tokens/s" | ||
| ) | ||
| self.all_tokens = 0 | ||
| self.output_tokens = 0 | ||
| self.prompt_tokens = 0 | ||
| self.last_log_time = now | ||
| return | ||
| elapsed = now - self.last_print_time | ||
| if elapsed < self.interval: | ||
| return | ||
|
|
||
| total_tps = (self.prompt_tokens + self.output_tokens) / elapsed | ||
| input_tps = self.prompt_tokens / elapsed | ||
| output_tps = self.output_tokens / elapsed | ||
|
|
||
| running = len(running_batch.reqs) if running_batch else 0 | ||
| queued = req_queue.get_wait_req_num() | ||
|
|
||
| # Memory utilization (average across dp) | ||
| # kv_used: physical KV memory usage (includes prefix cache tree occupancy) | ||
| # kv_used_no_cache: effective usage excluding unrefed prefix cache tokens | ||
| kv_used_list = [] | ||
| kv_used_no_cache_list = [] | ||
| for dp_i in range(self.dp_size_in_node): | ||
| unrefed = read_only_statics_mem_manager.get_unrefed_token_num(dp_i) | ||
| used = self.max_total_token_num - unrefed | ||
| kv_used_list.append(used / self.max_total_token_num) | ||
| if not disable_dynamic_prompt_cache and radix_cache_client is not None: | ||
| cache_unrefed = radix_cache_client.get_unrefed_tokens_num(dp_i) | ||
| kv_used_no_cache_list.append((used - cache_unrefed) / self.max_total_token_num) | ||
| else: | ||
| kv_used_no_cache_list.append(used / self.max_total_token_num) | ||
| avg_kv_used = sum(kv_used_list) / len(kv_used_list) | ||
| avg_kv_used_no_cache = sum(kv_used_no_cache_list) / len(kv_used_no_cache_list) | ||
|
|
||
| # Global prefix cache hit rate | ||
| cache_hit_rate = ( | ||
| (self.global_cache_total / self.global_input_total * 100) if self.global_input_total > 0 else 0.0 | ||
| ) | ||
|
|
||
| kv_pct = avg_kv_used * 100 | ||
| kv_pct_no_cache = avg_kv_used_no_cache * 100 | ||
|
|
||
| # Avg MTP accepted length (only shown when MTP is active) | ||
| mtp_suffix = "" | ||
| if self.global_mtp_accepted_total > 0: | ||
| decode_steps = self.global_mtp_output_total - self.global_mtp_accepted_total | ||
| avg_mtp_len = self.global_mtp_output_total / max(decode_steps, 1) | ||
| mtp_suffix = f" | MTP {avg_mtp_len:.2f}" | ||
|
|
||
| self.status_logger.info( | ||
| f"Throughput {total_tps:>7.1f} tok/s (in {input_tps:.1f}, out {output_tps:.1f}) | " | ||
| f"Reqs {running} run, {queued} wait, {paused_req_num} pause | " | ||
| f"KV Cache {kv_pct:.1f}% (active {kv_pct_no_cache:.1f}%) | " | ||
| f"Prefix Hit {cache_hit_rate:.1f}%" | ||
| f"{mtp_suffix}" | ||
| ) | ||
|
|
||
| # Reset windowed counters | ||
| self.prompt_tokens = 0 | ||
| self.output_tokens = 0 | ||
| self.last_print_time = now | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
maybe_printmethod has a large number of parameters. Many of these, such asreq_queue,read_only_statics_mem_manager,radix_cache_client, anddisable_dynamic_prompt_cache, are available whenSystemStatusReporteris initialized and seem to be constant throughout its lifetime.To improve code clarity and maintainability, consider moving these stable dependencies to the
__init__method. This simplifies themaybe_printsignature and makes the dependencies ofSystemStatusReportermore explicit.For example, you could modify
__init__to accept these objects and store them as instance attributes. Thenmaybe_printwould only need the parameters that change on each call, likerunning_batchandpaused_req_num.