Fix profiling tools#10977
Conversation
Summary of ChangesHello @Qiaolin-Yu, 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 addresses a regression in the profiling tools by adjusting how profiling stages are represented in log messages. Specifically, it reverts a change that incorrectly used 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.
Code Review
This pull request fixes a crash in the profiling tools by changing how the stage parameter is converted to a string. The change from stage.name to stage.__str__() prevents an AttributeError when an unexpected type (like a boolean) is passed. My review focuses on making this fix more robust and idiomatic. I've suggested using isinstance(stage, ForwardMode) to explicitly check the type of the stage parameter. This prevents confusing logs and filenames when an incorrect type is passed, and makes the code more resilient to such errors. I've also recommended using the built-in str() function instead of the dunder __str__ method, which is standard Python practice.
| self, stage: Optional[ForwardMode] = None | ||
| ) -> ProfileReqOutput | None: | ||
| stage_str = f" for {stage.name}" if stage else "" | ||
| stage_str = f" for {stage.__str__()}" if stage else "" |
There was a problem hiding this comment.
While using __str__ fixes the crash when a boolean is passed, it's more of a workaround that leads to confusing log messages like "Profiling starts for True...". A more robust solution would be to explicitly check if stage is an instance of ForwardMode. This handles the incorrect type gracefully by producing an empty string, leading to cleaner logs, while still using the desired string representation for valid ForwardMode objects.
Also, it's more idiomatic to use str(stage) instead of the dunder method stage.__str__().
| stage_str = f" for {stage.__str__()}" if stage else "" | |
| stage_str = f" for {str(stage)}" if isinstance(stage, ForwardMode) else "" |
| Path(self.torch_profiler_output_dir).mkdir(parents=True, exist_ok=True) | ||
|
|
||
| stage_suffix = f"-{stage.name}" if stage else "" | ||
| stage_suffix = f"-{stage.__str__()}" if stage else "" |
There was a problem hiding this comment.
For consistency with the start_profile method, it's good to apply the same robust type checking here. This ensures that if this function is ever called with an unexpected type for stage, it will handle it gracefully. Using isinstance makes the code more resilient.
It's also more idiomatic to use str(stage) over stage.__str__().
| stage_suffix = f"-{stage.__str__()}" if stage else "" | |
| stage_suffix = f"-{str(stage)}" if isinstance(stage, ForwardMode) else "" |
|
@hnyls2002 We shall merge this PR directly |
Motivation
Before this pr,
This issue is introduced in 777eb53.
Modifications
Accuracy Tests
Benchmarking and Profiling
Checklist