Skip to content

Fix num_items_in_batch GA for Gemma4#4998

Merged
danielhanchen merged 2 commits into
unslothai:mainfrom
Datta0:issue-4982-gemma4-ga-main
Apr 14, 2026
Merged

Fix num_items_in_batch GA for Gemma4#4998
danielhanchen merged 2 commits into
unslothai:mainfrom
Datta0:issue-4982-gemma4-ga-main

Conversation

@Datta0

@Datta0 Datta0 commented Apr 13, 2026

Copy link
Copy Markdown
Collaborator

Fixes: #4982
The argument is not in Gemma4ForConditionalGeneration, It is in the .model of the same

@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 updates the patch_gradient_accumulation_fix in unsloth/models/_utils.py to correctly identify the accepts_loss_kwargs flag in wrapped models, specifically addressing issues with Gemma2. However, the use of direct string replacement for patching the source code is fragile and likely to cause indentation errors. It is recommended to use a regular expression that captures and preserves the existing indentation levels to ensure the patched code remains syntactically valid.

Comment thread unsloth/models/_utils.py
Comment on lines +2113 to 2119
init_function = init_function.replace(
"self.model_accepts_loss_kwargs = unwrapped_model.accepts_loss_kwargs\n else:",
"self.model_accepts_loss_kwargs = unwrapped_model.accepts_loss_kwargs\n"
' elif hasattr(getattr(unwrapped_model, "model", None), "accepts_loss_kwargs"):\n'
" self.model_accepts_loss_kwargs = unwrapped_model.model.accepts_loss_kwargs\n"
" else:",
)

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.

critical

The implementation using init_function.replace with hardcoded strings is highly likely to fail and cause an IndentationError.

  1. Indentation Mismatch: textwrap.dedent removes common leading whitespace, but the line self.model_accepts_loss_kwargs = ... remains indented by 8 spaces (relative to the dedented def). Your search string starts at column 0, so it won't match.
  2. Indentation Error: Even if it did match, the replacement string would insert code with 0 or 4 spaces of indentation where 8 spaces are expected, breaking the Python syntax.
  3. Robustness: Using re.sub is much safer for patching source code as it can handle varying whitespace and capture existing indentation levels.

Additionally, consider if unwrapped_model.model is sufficient for PEFT-wrapped models, where the attribute might be nested deeper (e.g., unwrapped_model.model.model).

Suggested change
init_function = init_function.replace(
"self.model_accepts_loss_kwargs = unwrapped_model.accepts_loss_kwargs\n else:",
"self.model_accepts_loss_kwargs = unwrapped_model.accepts_loss_kwargs\n"
' elif hasattr(getattr(unwrapped_model, "model", None), "accepts_loss_kwargs"):\n'
" self.model_accepts_loss_kwargs = unwrapped_model.model.accepts_loss_kwargs\n"
" else:",
)
init_function = re.sub(
r'(\s+self\.model_accepts_loss_kwargs = unwrapped_model\.accepts_loss_kwargs\n)(\s+)(else:)',
r'\1\2elif hasattr(getattr(unwrapped_model, "model", None), "accepts_loss_kwargs"):\n'
r'\2 self.model_accepts_loss_kwargs = unwrapped_model.model.accepts_loss_kwargs\n'
r'\2\3',
init_function,
)

@Datta0 Datta0 marked this pull request as ready for review April 13, 2026 14:33
@danielhanchen danielhanchen merged commit 4328d0b into unslothai:main Apr 14, 2026
1 check passed
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.

[Question] High Gradient Norm and Loss During Initial Training with Gemma-4-E2B

2 participants