Fix MoE wrapper handling for PR 450#453
Conversation
This reverts commit 169b1ea.
Summary of ChangesHello @danielhanchen, 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 advances the Unsloth framework's capabilities for Mixture of Experts (MoE) models. It introduces comprehensive fixes for LoRA adapter integration, expands support to include GLM4 and Qwen3-VL MoE architectures, and incorporates high-performance grouped GEMM kernels for optimized execution. Furthermore, it refines quantization behaviors for MXFP4 and enhances vLLM's LoRA management, ensuring a more robust and efficient experience for training and inference with MoE models. 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.
Code Review
This pull request introduces significant enhancements for Mixture-of-Experts (MoE) models. Key changes include centralizing MoE forward logic into a new moe_utils.py module, adding support for GLM-4 and Qwen3-VL MoE models, and refactoring LoRA handling for MoE layers to be more robust and performant. Additionally, it adds a custom 4-bit quantization scheme for MoE layers and configuration options for MXFP4 dequantization. The changes are well-structured and address complex integration challenges. I have one suggestion to simplify a part of the LoRA extraction logic for better maintainability. Overall, this is a high-quality contribution.
| hidden_dim = None | ||
| experts_module = getattr(wrapper, "base_layer", None) | ||
| # Traverse base_layer until we find the experts module which might have hidden_dim | ||
| current = wrapper | ||
| while hasattr(current, "base_layer"): | ||
| current = current.base_layer | ||
| if hasattr(current, "hidden_dim"): | ||
| hidden_dim = current.hidden_dim | ||
| break |
There was a problem hiding this comment.
The manual traversal to find hidden_dim can be simplified. The ParamWrapper class in PEFT provides a get_base_layer() method that recursively finds the underlying module. Using this method and getattr makes the code cleaner, more concise, and less fragile to potential changes in the PEFT wrapper structure. The existing code also has an unused experts_module variable on line 198.
| hidden_dim = None | |
| experts_module = getattr(wrapper, "base_layer", None) | |
| # Traverse base_layer until we find the experts module which might have hidden_dim | |
| current = wrapper | |
| while hasattr(current, "base_layer"): | |
| current = current.base_layer | |
| if hasattr(current, "hidden_dim"): | |
| hidden_dim = current.hidden_dim | |
| break | |
| experts_module = wrapper.get_base_layer() if hasattr(wrapper, "get_base_layer") else None | |
| hidden_dim = getattr(experts_module, "hidden_dim", None) |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 14e9c9a51b
ℹ️ About Codex in GitHub
Your team has set up Codex to 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 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| # lora_B.weight: (dim2, E*R) -> view(dim2, E, R) -> permute(1, 0, 2) | ||
| # first_weight (A): (E, in_dim, R) | ||
| first_weight = weight_A.view(num_experts, rank_per_expert, dim1) | ||
| first_weight = first_weight.permute(0, 2, 1).contiguous() # (E, dim1, R) |
There was a problem hiding this comment.
Use lora_B as first weight for standard-format MoE
When the expert weight is in the standard F.linear layout (E, out_dim, in_dim) (the case described in the comments), PEFT’s ParamWrapper treats in_features as out_dim, so weight_A is shaped (E*R, out_dim) while weight_B is (in_dim, E*R). The grouped-mm LoRA path expects the first weight to match the input dimension (E, in_dim, R), which means it must come from weight_B, not weight_A. As written, first_weight becomes (E, out_dim, R), causing a dimension mismatch or incorrect LoRA application when grouped-mm is used with standard-format MoE weights (e.g., older transformer MoE models without a custom extractor). This will break or corrupt LoRA outputs in those configurations.
Useful? React with 👍 / 👎.
Summary
Testing