-
Notifications
You must be signed in to change notification settings - Fork 285
feat(misc): Profiler support #1121
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @WuSiYu, 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 significantly enhances LightLLM's observability by adding comprehensive profiling capabilities. It allows developers to easily enable and control performance profiling across the entire distributed system, supporting both PyTorch's native profiler and NVIDIA NVTX. This feature is crucial for identifying performance bottlenecks and optimizing the execution flow of the LLM inference server. Highlights
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces profiler support, which is a great feature for debugging and performance analysis. The changes span across the API layer, server managers, and model backends to enable torch_profiler and nvtx profiling modes. The implementation is comprehensive, adding new CLI arguments, API endpoints, and internal command propagation.
My review focuses on ensuring correctness and maintainability. I've identified a potential race condition in the model backend, a few typos in the CLI arguments and help text that could affect functionality and clarity, and some minor inconsistencies in API responses and type hints. I've also pointed out a busy-wait loop that could be optimized. Addressing these points will make the new profiler feature more robust and easier to maintain.
| mode=self.args.enable_profiling, | ||
| name=f"lightllm-model_backend-node{self.node_rank}_dev{get_current_device_id()}", | ||
| ) | ||
| self.profiling_active = False |
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 self.profiling_active attribute is accessed by multiple threads (infer_loop_thread and infer_loop_thread1) without a lock, which can lead to a race condition. One thread might read an outdated value while another is writing to it. You should introduce a threading.Lock to protect access to this attribute.
Initialize it in init_model:
self.profiling_active = False
self.profiling_lock = threading.Lock()Then use it when accessing self.profiling_active in _try_read_new_reqs and _read_reqs_buffer_and_init_reqs:
In _try_read_new_reqs:
with self.profiling_lock:
if self.profiler.is_active != self.profiling_active:
if self.profiling_active:
self.profiler.start()
else:
self.profiler.stop()In _read_reqs_buffer_and_init_reqs:
with self.profiling_lock:
if obj.cmd == "start":
self.profiling_active = True
elif obj.cmd == "stop":
self.profiling_active = False
lightllm/utils/profiler.py
Outdated
| if self.mode == "torch_profiler": | ||
| self._torch_profiler_start() | ||
| elif self.mode == "nvtx": | ||
| self._nxtx_start() |
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.
lightllm/utils/profiler.py
Outdated
| self._torch_profiler.start() | ||
| torch.cuda.synchronize() | ||
|
|
||
| def _nxtx_start(self) -> None: |
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.
lightllm/server/api_http.py
Outdated
| async def profiler_stop() -> Response: | ||
| if g_objs.args.enable_profiling: | ||
| await g_objs.httpserver_manager.profiler_cmd("stop") | ||
| return {"status": "ok"} |
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.
lightllm/server/api_http.py
Outdated
| await g_objs.httpserver_manager.profiler_cmd("stop") | ||
| return {"status": "ok"} | ||
| else: | ||
| return JSONResponse({"message": "Profiling support not enabled"}, status_code=500) |
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.
Returning a 500 Internal Server Error for a disabled feature can be misleading. A 400 Bad Request or 404 Not Found would be more appropriate as the feature is not available, which is not a server failure.
| return JSONResponse({"message": "Profiling support not enabled"}, status_code=500) | |
| return JSONResponse({"message": "Profiling support not enabled"}, status_code=400) |
lightllm/utils/profiler.py
Outdated
|
|
||
| @dataclass | ||
| class ProfilerCmd: | ||
| cmd: str # "start" or "stop" for now |
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.
lightllm/utils/profiler.py
Outdated
| ) | ||
| elif self.mode == "nvtx": | ||
| self._nvtx_toplevel_mark = "LIGHTLLM_PROFILE" | ||
| self._nvtx_toplevel_id = None |
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.
lightllm/utils/profiler.py
Outdated
| self._nvtx_toplevel_mark, | ||
| ) | ||
| elif self.mode is not None: | ||
| assert False, "invalid profiler mode" |
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.
assert False statements are removed when Python is run with the -O (optimize) flag, which can happen in production environments. It's better to raise an exception like ValueError or NotImplementedError for invalid profiler modes to ensure the error is always caught.
| assert False, "invalid profiler mode" | |
| raise ValueError("invalid profiler mode") |
lightllm/server/api_http.py
Outdated
| await g_objs.httpserver_manager.profiler_cmd("start") | ||
| return {"status": "ok"} | ||
| else: | ||
| return JSONResponse({"message": "Profiling support not enabled"}, status_code=500) |
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.
Returning a 500 Internal Server Error for a disabled feature can be misleading. A 400 Bad Request or 404 Not Found would be more appropriate as the feature is not available, which is not a server failure.
| return JSONResponse({"message": "Profiling support not enabled"}, status_code=500) | |
| return JSONResponse({"message": "Profiling support not enabled"}, status_code=400) |
lightllm/server/api_http.py
Outdated
| async def profiler_start() -> Response: | ||
| if g_objs.args.enable_profiling: | ||
| await g_objs.httpserver_manager.profiler_cmd("start") | ||
| return {"status": "ok"} |
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.
c6d977e to
84185b5
Compare
use --enable_profiling=MODE to enable, currently support torch_profile and nvtx (use with NVIDIA Nsight system) mode
84185b5 to
2cde8e6
Compare
use --enable_profiling=MODE to enable, currently support torch_profile and nvtx (use with NVIDIA Nsight system) mode