-
Notifications
You must be signed in to change notification settings - Fork 347
[AWQ] Fix _accumulate_mean bug, move AWQ activation averaging off CPU, and improve logging #2161
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
|
👋 Hi! Thank you for contributing to llm-compressor. Please add the ready label when the PR is ready for review. Note: This is required to complete the testing suite, please only add the label once the PR is code complete and local testing has been performed. |
Summary of ChangesHello @ZewenShen-Cohere, 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 focuses on enhancing the robustness, performance, and observability of the AWQ (Activation-aware Weight Quantization) calibration process. It addresses a critical bug in the mean accumulation logic, optimizes activation averaging to leverage GPU resources more effectively, and introduces comprehensive logging capabilities to provide better insights into the calibration grid search and its outcomes. 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. 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 several valuable improvements to the AWQ modifier. It correctly fixes a bug in _accumulate_mean that caused incorrect output on its first run. Moving the activation averaging computation off the CPU is a significant performance enhancement, especially for large models. The addition of more informative logging, including a progress bar and JSON output for error metrics, greatly improves the usability and observability of the AWQ calibration process. The code is well-structured, but I have a couple of suggestions to enhance robustness and simplify the implementation.
| # Use the provided log_output_path or generate a default filename | ||
| if self.log_output_path: | ||
| output_path = Path(self.log_output_path) | ||
| # If it's a directory, create a filename with timestamp | ||
| if output_path.is_dir(): | ||
| timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | ||
| filename = output_path / f"awq_error_metrics_{timestamp}.json" | ||
| else: | ||
| filename = output_path | ||
| else: | ||
| # Default filename with timestamp | ||
| timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | ||
| filename = Path(f"awq_error_metrics_{timestamp}.json") | ||
|
|
||
| # Ensure parent directory exists | ||
| filename.parent.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| # Prepare data for saving | ||
| metrics_data = { | ||
| 'timestamp': datetime.now().isoformat(), | ||
| 'quantization_config': { | ||
| 'duo_scaling': self.duo_scaling, | ||
| 'n_grid': self.n_grid, | ||
| }, | ||
| 'total_layers': len(self._error_metrics), | ||
| 'metrics': self._error_metrics | ||
| } |
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.
This section can be simplified and made more robust.
datetime.now()is called multiple times (lines 724, 730, 738), which could lead to slight timestamp inconsistencies between the filename and the file content. It's better to call it once at the beginning of the function and reuse the value.- The
elseblock (lines 728-731) is unreachable. The calling functionon_finalizealready ensures thatself.log_output_pathis notNonebefore invoking_save_error_metrics.
The suggested change addresses both points by capturing datetime.now() once and removing the redundant code path.
now = datetime.now()
# Use the provided log_output_path.
# The calling context ensures `self.log_output_path` is not None,
# so the `else` branch is unreachable and has been removed.
output_path = Path(self.log_output_path)
# If it's a directory, create a filename with timestamp
if output_path.is_dir():
timestamp = now.strftime("%Y%m%d_%H%M%S")
filename = output_path / f"awq_error_metrics_{timestamp}.json"
else:
filename = output_path
# Ensure parent directory exists
filename.parent.mkdir(parents=True, exist_ok=True)
# Prepare data for saving.
# Use the `now` object captured at the start for a consistent timestamp.
metrics_data = {
'timestamp': now.isoformat(),
'quantization_config': {
'duo_scaling': self.duo_scaling,
'n_grid': self.n_grid,
},
'total_layers': len(self._error_metrics),
'metrics': self._error_metrics
}
brian-dellabetta
left a comment
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.
Thanks for creating this! The fixes look good, I just have a few questions on the logging part, hopefully we can streamline some more of it
| offload_device: torch.device | None = None | ||
| duo_scaling: bool | Literal["both"] = True | ||
| n_grid: int = 20 | ||
| log_output_path: str | None = 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.
users are able to configure either a log file or log dir at a higher scope, see code here. So anything logged with loguru.logger should also be automatically added if those fields are set as inputs to oneshot. Have you tried that?
| if self.log_output_path is not None and len(self._error_metrics) > 0: | ||
| self._save_error_metrics() |
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.
rather than relying on log dir to determine if something should be logged, i think it is preferred to use the log level. You can log these metrics as debug so the user will only see it if log level is debug. WDYT?
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.
That's a good idea. Will make the change!
| num_added = inp.size(0) | ||
| if prev_mean_and_count is None: | ||
| return sum_added, num_added | ||
| return sum_added / num_added, num_added |
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.
ah! this was the error you were talking about. yeah this makes sense, nice catch.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Signed-off-by: ZewenShen-Cohere <[email protected]>
|
Thank you for the revision @brian-dellabetta. I've switched to use the logger. |
|
Can you address the quality issues? You can do this by running these steps in the root dir: pip install -e .[dev] |
Done |
This PR addresses the following issues:
_accumulate_mean produces incorrect output on its first run.
cache_smooth_activations_hook previously performed the averaging computation on the CPU. When both the hidden dimension and sequence length are large, this makes AWQ calibration CPU-bound. The slowdown is especially severe when multiple AWQ quantization jobs run concurrently.
Added more informative logging to the AWQ calibration grid search, including per-mapping JSON logs.
This PR is a subset of #2158