Fix correctness bugs across multiple model files#3813
Conversation
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".
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 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
🧠 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 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.
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".
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/K→Qn/Knin QK normalization. CausedNameErrorwhenuse_qk_norm=True(e.g.,c4ai-command-r-plusmodels).cohere.py:482 - Fixed wrong object reference
self.mlp→decoder_layer.mlpin inference loop. CausedAttributeErrorduring inference.falcon_h1.py:459,461 - Fixed wrong attribute names
post_attention_layernorm/mlp→pre_ff_layernorm/feed_forward. CausedAttributeErrorduring generation.qwen3_moe.py:210 - Fixed wrong module path
Qwen3Moe→qwen3_moe(lowercase). CausedAttributeErrorwhen patching rotary embeddings.qwen3_moe.py:239 - Fixed wrong model_patcher
FastQwen3Model→FastQwen3MoeModel. 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. Themax_seq_length > model_max_seq_lengthwarning 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
use_qk_norm=Truenow use correct variable names