Skip to content

Fix bare except clauses and remove duplicate MAX_FUSED_SIZE definition - #7138

Merged
danielhanchen merged 2 commits into
unslothai:mainfrom
lxcxjxhx:fix/bare-except-and-duplicate-constant
Jul 15, 2026
Merged

danielhanchen merged 2 commits into
unslothai:mainfrom
lxcxjxhx:fix/bare-except-and-duplicate-constant

Conversation

@lxcxjxhx

Copy link
Copy Markdown
Contributor

Changes

1. Replace bare except clauses (rms_layernorm.py)

  • Replaced 3 bare except: clauses with except (ImportError, AttributeError):
  • Lines 273, 284, 296: More specific exception handling for import failures
  • Prevents accidentally catching unrelated exceptions (KeyboardInterrupt, SystemExit, etc.)

2. Remove duplicate MAX_FUSED_SIZE definition (cross_entropy_loss.py)

  • Removed duplicate MAX_FUSED_SIZE = 65536 definition at line 288
  • This constant is already imported from .utils at line 20
  • Eliminates code redundancy and potential maintenance issues

Testing

Note: Full test suite could not be run locally due to environment constraints (requires unsloth_zoo, datasets dependencies and GPU for Triton kernels). Changes are based on static analysis.

  • Changes are safe (exception handling improvement, code cleanup)
  • No functional behavior changes
  • Rely on CI/CD automated testing for verification

Impact

  • Improves code quality and maintainability
  • Better exception handling practices
  • No breaking changes

@lxcxjxhx
lxcxjxhx requested a review from danielhanchen as a code owner July 15, 2026 08:54

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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 removes an unused constant MAX_FUSED_SIZE in cross_entropy_loss.py and replaces bare except: blocks with specific exception handling (ImportError, AttributeError) in rms_layernorm.py. The review feedback correctly identifies that if the referenced RMSNorm classes are not defined due to import failures, referencing them will raise a NameError which is not caught by the new exception handlers. It is recommended to include NameError in the caught exceptions to prevent potential runtime crashes.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread unsloth/kernels/rms_layernorm.py Outdated
import transformers.models.mllama.modeling_mllama
transformers.models.mllama.modeling_mllama.MllamaTextRMSNorm = Unsloth_MllamaTextRMSNorm
except:
except (ImportError, AttributeError):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If the module-level import of MllamaTextRMSNorm fails (e.g., if the class is missing but the mllama module exists), Unsloth_MllamaTextRMSNorm will not be defined. In patch_rms_layernorm, the import of mllama will succeed, but referencing Unsloth_MllamaTextRMSNorm will raise a NameError. Since NameError is not caught by (ImportError, AttributeError), this will cause a runtime crash. Catching NameError prevents this regression.

Suggested change
except (ImportError, AttributeError):
except (ImportError, AttributeError, NameError):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 117b540: added NameError to this fallback. If the mllama module exists but MllamaTextRMSNorm was never bound (module-level import failed), the import here still succeeds and the RHS reference would raise NameError, so catching it keeps patch a no-op.

Comment thread unsloth/kernels/rms_layernorm.py Outdated
import transformers.models.mllama.modeling_mllama
transformers.models.mllama.modeling_mllama.MllamaTextRMSNorm = MllamaTextRMSNorm
except:
except (ImportError, AttributeError):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If the module-level import of MllamaTextRMSNorm fails, MllamaTextRMSNorm will not be defined in the module scope. In unpatch_rms_layernorm, the import of mllama will succeed, but referencing MllamaTextRMSNorm will raise a NameError. Since NameError is not caught by (ImportError, AttributeError), this will cause a runtime crash. Catching NameError prevents this regression.

Suggested change
except (ImportError, AttributeError):
except (ImportError, AttributeError, NameError):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 117b540: same for unpatch. Added NameError so this fallback stays a no-op when MllamaTextRMSNorm is unbound in that version-mismatch case.

If mllama exists but MllamaTextRMSNorm is missing, the module-level import
fails so Unsloth_MllamaTextRMSNorm/MllamaTextRMSNorm stay undefined. The
patch/unpatch module imports then succeed and reference the undefined name,
raising NameError. Add NameError so these fallbacks stay no-ops as before.
@danielhanchen

Copy link
Copy Markdown
Member

Pushed a small follow-up: added NameError to the two mllama patch/unpatch fallbacks.

If the mllama module exists but MllamaTextRMSNorm is missing, the module-level import fails, so Unsloth_MllamaTextRMSNorm and MllamaTextRMSNorm never get defined. In that case the imports inside patch_rms_layernorm / unpatch_rms_layernorm still succeed and then reference the undefined name, raising NameError, which (ImportError, AttributeError) would not catch. Adding NameError keeps those paths as no-ops, matching the previous bare except.

The MAX_FUSED_SIZE removal and the module-level except narrowing look good. Thanks for the cleanup.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 117b5402d2

ℹ️ 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".

@danielhanchen
danielhanchen merged commit ee73bcb into unslothai:main Jul 15, 2026
1 check passed
@lxcxjxhx

Copy link
Copy Markdown
Contributor Author

Thanks

Pushed a small follow-up: added NameError to the two mllama patch/unpatch fallbacks.

If the mllama module exists but MllamaTextRMSNorm is missing, the module-level import fails, so Unsloth_MllamaTextRMSNorm and MllamaTextRMSNorm never get defined. In that case the imports inside patch_rms_layernorm / unpatch_rms_layernorm still succeed and then reference the undefined name, raising NameError, which (ImportError, AttributeError) would not catch. Adding NameError keeps those paths as no-ops, matching the previous bare except.

The MAX_FUSED_SIZE removal and the module-level except narrowing look good. Thanks for the cleanup.

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.

2 participants