Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refines the request scheduling and resource management within the system. It introduces an automatic mechanism for setting the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors the scheduling logic by making it more dynamic. It removes the fixed router_max_new_token_len parameter and introduces a new RouterStatics class to calculate an exponential moving average of request output lengths (ema_req_out_len). This allows the scheduler to adapt to the actual workload. Additionally, it introduces request chunking in the pd_master to handle long generation requests more efficiently by splitting them into smaller blocks.
My review focuses on a critical bug in the exception handling logic for the new request chunking feature, which could lead to runtime errors. I've also suggested a minor refactoring for code clarity. The rest of the changes for dynamic scheduling appear to be well-implemented and consistent across the codebase.
| try: | ||
| sampling_params.group_request_id = group_request_id | ||
| # 记录请求到达的相关信息 | ||
| await self._log_req_header(request, group_request_id) | ||
| await self._log_req_header(request, origin_group_request_id) | ||
| # 监控 | ||
| self.metric_client.counter_inc("lightllm_request_count") | ||
| self.metric_client.histogram_observe("lightllm_request_max_new_tokens", sampling_params.max_new_tokens) | ||
| self.metric_client.histogram_observe( | ||
| "lightllm_request_max_new_tokens", origin_sampling_params.max_new_tokens | ||
| ) | ||
|
|
||
| p_node, d_node = await self.select_p_d_node(prompt, origin_sampling_params, multimodal_params) | ||
|
|
||
| p_node, d_node = await self.select_p_d_node(prompt, sampling_params, multimodal_params) | ||
| history_gen_token_strs = [] | ||
|
|
||
| if not p_node or not d_node: | ||
| logger.error(f"{group_request_id}: No p_node or d_node found") | ||
| raise Exception(f"{group_request_id}: No p_node or d_node found") | ||
|
|
||
| results_generator = self._wait_to_token_package( | ||
| p_node, | ||
| d_node, | ||
| start_time, | ||
| prompt, | ||
| sampling_params, | ||
| multimodal_params, | ||
| request, | ||
| ) | ||
| async for sub_req_id, request_output, metadata, finish_status in results_generator: | ||
| yield sub_req_id, request_output, metadata, finish_status | ||
| logger.error(f"{origin_group_request_id}: No p_node or d_node found") | ||
| raise Exception(f"{origin_group_request_id}: No p_node or d_node found") | ||
|
|
||
| for iter_index, block_max_new_tokens in enumerate(max_new_tokens_list): | ||
| sampling_params = SamplingParams.from_buffer_copy(origin_sampling_params) | ||
| block_group_request_id = self.id_gen.generate_id() | ||
| sampling_params.group_request_id = block_group_request_id | ||
| logger.info( | ||
| f"pd log gen sub req id {block_group_request_id}" f" for main req id {origin_group_request_id}" | ||
| ) | ||
| sampling_params.max_new_tokens = block_max_new_tokens | ||
|
|
||
| results_generator = self._wait_to_token_package( | ||
| p_node, | ||
| d_node, | ||
| start_time, | ||
| prompt + "".join(history_gen_token_strs), | ||
| sampling_params, | ||
| multimodal_params, | ||
| request, | ||
| ) | ||
| is_last_block = iter_index == len(max_new_tokens_list) - 1 | ||
| prompt_tokens = sys.maxsize # 因为分段的原因 | ||
| async for sub_req_id, request_output, metadata, finish_status in results_generator: | ||
| # pd 分离模式下,返回的 metadata 可能序号信息可能存在不准确性。 | ||
| assert sub_req_id == block_group_request_id | ||
| if finish_status.get_finish_reason() == "length" and (not is_last_block): | ||
| finish_status = FinishStatus() # 转换为NoFinished | ||
| history_gen_token_strs.append(request_output) | ||
| prompt_tokens = min(prompt_tokens, metadata["prompt_tokens"]) | ||
| metadata["prompt_tokens"] = prompt_tokens | ||
| yield origin_group_request_id, request_output, metadata, finish_status | ||
|
|
||
| await self.remove_req(group_request_id=block_group_request_id) | ||
|
|
||
| except BaseException as e: | ||
| logger.error(f"has exception {str(e)}") | ||
| try: | ||
| await self.abort(group_request_id, p_node=p_node, d_node=d_node) | ||
| await self.abort(block_group_request_id, p_node=p_node, d_node=d_node) | ||
| except: | ||
| await self.abort(group_request_id) | ||
| await self.abort(block_group_request_id) | ||
| raise e | ||
|
|
||
| finally: | ||
| await self.remove_req(group_request_id) | ||
| await self.remove_req(block_group_request_id) | ||
| return |
There was a problem hiding this comment.
The exception and finalization logic in the generate method has a couple of critical issues that could lead to runtime errors:
- Potential
NameError:block_group_request_idis defined inside theforloop. If an exception occurs before the loop starts (e.g., inselect_p_d_node), theexceptandfinallyblocks will raise aNameErrorwhen trying to accessblock_group_request_id. - Double
remove_reqcall: On successful completion of all chunks,remove_reqis called for the last chunk at the end of theforloop (line 154), and then called again in thefinallyblock. This will likely cause an error (e.g.,KeyError) asremove_reqis probably not idempotent.
To fix this, I suggest initializing block_group_request_id = None before the try block and ensuring cleanup logic handles all edge cases correctly without causing errors. For instance, the finally block could be removed and cleanup handled within the except block for failures, while the success path is already handled inside the loop.
| ans_list = [block_max_new_tokens for _ in range(max_new_tokens // block_max_new_tokens)] | ||
| left_token = max_new_tokens - (max_new_tokens // block_max_new_tokens) * block_max_new_tokens | ||
| if left_token > 0: | ||
| ans_list.append(left_token) |
There was a problem hiding this comment.
This implementation can be made more concise and Pythonic by using the modulo operator (%) for the remainder and list multiplication for creating the list of blocks.
| ans_list = [block_max_new_tokens for _ in range(max_new_tokens // block_max_new_tokens)] | |
| left_token = max_new_tokens - (max_new_tokens // block_max_new_tokens) * block_max_new_tokens | |
| if left_token > 0: | |
| ans_list.append(left_token) | |
| ans_list = [block_max_new_tokens] * (max_new_tokens // block_max_new_tokens) | |
| remainder = max_new_tokens % block_max_new_tokens | |
| if remainder > 0: | |
| ans_list.append(remainder) |
pd 分离分段 + 自动启动高级调度 + 自动休整 max_new_tokens 为正常值。