Add Bnb4bit support for MoE models on transformers v5 - #4032 #527
Add Bnb4bit support for MoE models on transformers v5 - #4032 #527sensai99 wants to merge 23 commits intounslothai:mainfrom
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 introduces crucial functionality to enable 4-bit quantization for Mixture-of-Experts (MoE) models within the Transformers library, particularly for versions 5 and above. It addresses the challenge of quantizing MoE expert parameters that are defined as 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
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 PR introduces support for quantization of MoE parameters in transformers v5 by converting expert parameters to nn.Params4bit and handling quantization/dequantization for PEFT LoRA compatibility. It includes a new module moe_bnb_transformers.py with patching functions and modifications to misc.py and __init__.py to integrate the new functionality.
| # If the parameter is a Params4bit, dequantize it | ||
| if _check_bnb_available() and isinstance(param, Params4bit): | ||
| # Dequantize the parameter | ||
| return bnb.functional.dequantize_4bit(param.data, param.quant_state) |
There was a problem hiding this comment.
Consider adding a check to ensure param.quant_state is not None before dequantizing. If quant_state is None, it could lead to an error during dequantization.
| # If the parameter is a Params4bit, dequantize it | |
| if _check_bnb_available() and isinstance(param, Params4bit): | |
| # Dequantize the parameter | |
| return bnb.functional.dequantize_4bit(param.data, param.quant_state) | |
| # If the parameter is a Params4bit, dequantize it | |
| if _check_bnb_available() and isinstance(param, Params4bit) and param.quant_state is not None: | |
| # Dequantize the parameter | |
| return bnb.functional.dequantize_4bit(param.data, param.quant_state) |
| except Exception as e: | ||
| return raise_error("transformers.quantizers.quantizers_utils.should_convert_module", e) |
| if not has_been_replaced: | ||
| logger.warning(f"Unsloth: No expert parameters were found to be replaced for {model.name_or_path}") |
| except Exception as e: | ||
| logger.warning(f"Unsloth: Error handling expert param quantization for {full_layer_name}: {e}") |
| # TODO: Can we raise an error here? | ||
| logger.warning( | ||
| f"Unsloth: Error checking MoE expert param_needs_quantization for {param_name}: {e}" | ||
| ) |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f3f2c6eba9
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d5b567c528
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fb69ead7ca
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Hi!
This PR adds support for quantization for MoE parameters of
nn.Parameter.With transformers v5, MoE parameters of
nn.Parameterwon't get quantized. This PR adds support for the quantization by doing the folllowing:nn.Parametertonn.Params4bitAnalysis using
GLM-4.7-Flash: