Skip to content

Fix correctness bugs across multiple model files#3813

Merged
danielhanchen merged 1 commit into
mainfrom
fix/correctness-bugs-batch2
Jan 1, 2026
Merged

Fix correctness bugs across multiple model files#3813
danielhanchen merged 1 commit into
mainfrom
fix/correctness-bugs-batch2

Conversation

@danielhanchen
Copy link
Copy Markdown
Member

Summary

This PR fixes 9 correctness bugs that would cause crashes or incorrect behavior:

Critical Fixes (would crash)

  • cohere.py:347-348 - Fixed wrong variable names Q/KQn/Kn in QK normalization. Caused NameError when use_qk_norm=True (e.g., c4ai-command-r-plus models).

  • cohere.py:482 - Fixed wrong object reference self.mlpdecoder_layer.mlp in inference loop. Caused AttributeError during inference.

  • falcon_h1.py:459,461 - Fixed wrong attribute names post_attention_layernorm/mlppre_ff_layernorm/feed_forward. Caused AttributeError during generation.

  • qwen3_moe.py:210 - Fixed wrong module path Qwen3Moeqwen3_moe (lowercase). Caused AttributeError when patching rotary embeddings.

  • qwen3_moe.py:239 - Fixed wrong model_patcher FastQwen3ModelFastQwen3MoeModel. Caused incorrect patching for Qwen3 MoE models.

High Priority Fixes (incorrect behavior)

  • hf_hub.py:21-22 - Fixed floor division /// for millions, and added missing return for values >= 1B.

  • save.py:550 - Fixed self-assignment sharded_ram_usage = sharded_ram_usage= max_shard_size. Integer shard sizes were being ignored.

  • rl.py:562-567 - Fixed orphan string literal that wasn't concatenated to length_check. The max_seq_length > model_max_seq_length warning was silently skipped.

  • granite.py:49-52 - Fixed wrong model name "Gemma2" → "Granite" and version "4.42.3" → "4.45.0" in error message.

Test plan

  • Verified all fixes compile without syntax errors
  • Verified Cohere models with use_qk_norm=True now use correct variable names
  • Verified Falcon H1 uses correct attribute names matching the model architecture
  • Verified Qwen3 MoE uses correct module path and model patcher

1. cohere.py:347-348 - Fixed wrong variable names in QK normalization.
   Used `Q`/`K` but variables were named `Qn`/`Kn`. This caused NameError
   when `use_qk_norm=True` (e.g., c4ai-command-r-plus models).

2. cohere.py:482 - Fixed wrong object reference in inference loop.
   Used `self.mlp` but should be `decoder_layer.mlp` since we're
   iterating through decoder layers. Caused AttributeError during inference.

3. falcon_h1.py:459,461 - Fixed wrong attribute names in inference path.
   Used `post_attention_layernorm` and `mlp` but Falcon H1 uses
   `pre_ff_layernorm` and `feed_forward`. Caused AttributeError during generation.

4. qwen3_moe.py:210 - Fixed wrong module path with incorrect capitalization.
   Used `transformers.models.Qwen3Moe` but should be `transformers.models.qwen3_moe`.
   Caused AttributeError when patching rotary embeddings.

5. qwen3_moe.py:239 - Fixed wrong model_patcher class.
   Used `FastQwen3Model` but should be `FastQwen3MoeModel` for MoE models.
   Caused incorrect patching for Qwen3 MoE models.

6. hf_hub.py:21-22 - Fixed floor division and missing return for billion values.
   Used `//` instead of `/` for millions, and had no return for values >= 1B.
   Caused incorrect formatting and None return for large numbers.

7. save.py:550 - Fixed self-assignment that did nothing.
   `sharded_ram_usage = sharded_ram_usage` should be `= max_shard_size`.
   Caused integer shard sizes to be ignored.

8. rl.py:562-567 - Fixed orphan string not included in length_check.
   The elif branch for max_seq_length validation was a standalone string
   expression, not concatenated to length_check. Caused silent skip of
   the max_seq_length > model_max_seq_length warning.

9. granite.py:49-52 - Fixed wrong model name and version in error message.
   Said "Gemma2" and "4.42.3" but should be "Granite" and "4.45.0".
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @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 delivers a comprehensive set of bug fixes across multiple model files, targeting both critical issues that led to crashes and high-priority problems causing incorrect behavior. The changes improve the overall robustness and accuracy of the model implementations, ensuring smoother operation and more reliable output for users interacting with Cohere, Falcon H1, Qwen3 MoE, and other related functionalities.

Highlights

  • Critical Fixes for Model Crashes: Addressed several critical bugs that caused crashes or AttributeError/NameError in Cohere, Falcon H1, and Qwen3 MoE models by correcting variable names, object references, and module paths.
  • High Priority Correctness Fixes: Resolved issues leading to incorrect behavior, including fixing floor division and missing return for large numbers in hf_hub.py, correcting a self-assignment in save.py, ensuring a max_seq_length warning is properly displayed in rl.py, and updating error messages in granite.py.
  • Improved Model Compatibility and Robustness: The fixes enhance the stability and correctness of various model implementations, ensuring they function as expected and provide accurate feedback to users.

🧠 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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a 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 provides a collection of important correctness fixes across various model files. The changes address several critical bugs that would lead to crashes, such as NameError and AttributeError from incorrect variable and attribute references. Additionally, it resolves high-priority issues that caused incorrect behavior, including a logical error in a division operation and an orphan string literal that was being ignored. All the fixes are well-targeted and correct, significantly improving the robustness and reliability of the codebase. The changes are approved.

@danielhanchen danielhanchen merged commit c0436a2 into main Jan 1, 2026
4 checks passed
abiswas-realadvice pushed a commit to abiswas-realadvice/unsloth that referenced this pull request May 14, 2026
1. cohere.py:347-348 - Fixed wrong variable names in QK normalization.
   Used `Q`/`K` but variables were named `Qn`/`Kn`. This caused NameError
   when `use_qk_norm=True` (e.g., c4ai-command-r-plus models).

2. cohere.py:482 - Fixed wrong object reference in inference loop.
   Used `self.mlp` but should be `decoder_layer.mlp` since we're
   iterating through decoder layers. Caused AttributeError during inference.

3. falcon_h1.py:459,461 - Fixed wrong attribute names in inference path.
   Used `post_attention_layernorm` and `mlp` but Falcon H1 uses
   `pre_ff_layernorm` and `feed_forward`. Caused AttributeError during generation.

4. qwen3_moe.py:210 - Fixed wrong module path with incorrect capitalization.
   Used `transformers.models.Qwen3Moe` but should be `transformers.models.qwen3_moe`.
   Caused AttributeError when patching rotary embeddings.

5. qwen3_moe.py:239 - Fixed wrong model_patcher class.
   Used `FastQwen3Model` but should be `FastQwen3MoeModel` for MoE models.
   Caused incorrect patching for Qwen3 MoE models.

6. hf_hub.py:21-22 - Fixed floor division and missing return for billion values.
   Used `//` instead of `/` for millions, and had no return for values >= 1B.
   Caused incorrect formatting and None return for large numbers.

7. save.py:550 - Fixed self-assignment that did nothing.
   `sharded_ram_usage = sharded_ram_usage` should be `= max_shard_size`.
   Caused integer shard sizes to be ignored.

8. rl.py:562-567 - Fixed orphan string not included in length_check.
   The elif branch for max_seq_length validation was a standalone string
   expression, not concatenated to length_check. Caused silent skip of
   the max_seq_length > model_max_seq_length warning.

9. granite.py:49-52 - Fixed wrong model name and version in error message.
   Said "Gemma2" and "4.42.3" but should be "Granite" and "4.45.0".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant