Conversation
Summary of ChangesHello @gushiqiao, 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 expands the quantization capabilities of the system by integrating FP8 (Float8) precision for matrix multiplications. It leverages 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 introduces support for FP8 quantization using torch._scaled_mm, which is a great addition for performance. The changes are well-structured, adding new quantization schemes and linear layer implementations. I've identified a few areas for improvement:
- An unreliable
__del__method for resource cleanup. - Some minor code redundancies and duplications.
- A redundant assignment with an in-place operation.
Overall, the changes are good, and addressing these points will improve the code's robustness and maintainability.
| def __del__(self): | ||
| if hasattr(self, "model"): | ||
| del self.model | ||
| if hasattr(self, "text_encoders"): | ||
| del self.text_encoders | ||
| if hasattr(self, "image_encoder"): | ||
| del self.image_encoder | ||
| if hasattr(self, "vae_encoder"): | ||
| del self.vae_encoder | ||
| if hasattr(self, "vae_decoder"): | ||
| del self.vae_decoder | ||
| torch.cuda.empty_cache() | ||
| gc.collect() |
There was a problem hiding this comment.
Using __del__ for resource cleanup, especially for GPU memory, is unreliable. The __del__ method is not guaranteed to be called when you expect it, due to Python's garbage collection behavior (e.g., circular references). This can lead to resource leaks. It's better to implement an explicit cleanup method, like cleanup() or close(), and ensure it's called deterministically when the runner is no longer needed.
| output_tensor = torchao_int8_gemm(input_tensor_quant, input_tensor_scale, self.weight.t(), self.weight_scale.t().float(), output_dtype=torch.bfloat16) | ||
| if self.bias is not None: | ||
| output_tensor = output_tensor + self.bias | ||
| output_tensor = output_tensor.add_(self.bias) |
There was a problem hiding this comment.
The add_ method performs an in-place addition and returns the modified tensor. The assignment back to output_tensor is redundant. You can simplify this to just output_tensor.add_(self.bias) for better clarity.
| output_tensor = output_tensor.add_(self.bias) | |
| output_tensor.add_(self.bias) |
| def act_quant_func(self, x): | ||
| abs_max = x.abs().max(dim=-1, keepdim=True)[0] | ||
| abs_max = torch.clamp(abs_max, min=1e-8) | ||
| scale = abs_max / 448.0 | ||
| quantized = torch.clamp(x / scale, -448, 448).to(torch.float8_e4m3fn) | ||
| return quantized, scale.float() |
There was a problem hiding this comment.
No description provided.