Repository navigation
Conversation
Summary of ChangesHello @djsaunde, 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 integrates automatic padding-free Supervised Fine-Tuning (SFT) into Unsloth's Highlights
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.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| from .packing import ( | ||
| configure_padding_free, | ||
| configure_sample_packing, | ||
| enable_padding_free_metadata, | ||
| enable_sample_packing, | ||
| mark_allow_overlength, | ||
| ) |
There was a problem hiding this comment.
Imported padding-free helpers undefined
The new exports in unsloth.utils.__init__ import configure_padding_free, enable_padding_free_metadata, and mark_allow_overlength from .packing, but those symbols do not exist in unsloth/utils/packing.py (repo-wide rg configure_padding_free only finds these import statements). As soon as unsloth.utils or any module that depends on it (e.g., unsloth.trainer) is imported, Python raises ImportError: cannot import name 'configure_padding_free', preventing the package from loading.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Code Review
This pull request refactors the training module to automatically enable padding-free SFT, replacing the previous auto-packing logic. The changes are generally well-structured, especially the explicit handling for Gemma 2 models. My main feedback is to re-introduce the robust error-handling mechanism that was present for auto-packing. This involves a try-except block to gracefully handle potential SFTTrainer initialization failures when padding-free is auto-enabled, ensuring the trainer can fall back to a safe configuration instead of crashing.
| def _should_auto_padding_free(config) -> bool: | ||
| if config is None or _AUTO_PADDING_FREE_ENV_DISABLED: | ||
| return False | ||
| if not getattr(config, "packing", False): | ||
| if getattr(config, "packing", False): | ||
| return False | ||
| return not getattr(config, "_unsloth_disable_auto_packing", False) | ||
|
|
||
|
|
||
| def _disable_sample_packing(config): | ||
| if config is None: | ||
| return | ||
| for attr, value in (("packing", False), ("padding_free", False)): | ||
| if hasattr(config, attr): | ||
| setattr(config, attr, value) | ||
| if hasattr(config, "remove_unused_columns"): | ||
| setattr(config, "remove_unused_columns", True) | ||
| setattr(config, "_unsloth_disable_auto_packing", True) | ||
|
|
||
|
|
||
| _AUTO_PACK_SKIP_MESSAGES = ( | ||
| "packing is not supported", | ||
| "padding-free training", | ||
| "passing a custom data collator", | ||
| ) | ||
|
|
||
|
|
||
| def _should_skip_auto_packing_error(exc: Exception) -> bool: | ||
| message = str(exc).lower() | ||
| return any(msg in message for msg in _AUTO_PACK_SKIP_MESSAGES) | ||
| return not getattr(config, "padding_free", False) |
There was a problem hiding this comment.
To support robust error handling for auto-enabled padding-free training, _should_auto_padding_free should be updated to prevent re-enabling the feature after it has failed once. This can be done by checking for a _unsloth_disable_auto_padding_free flag on the config. I'll provide more context on the full error handling pattern in another comment.
| def _should_auto_padding_free(config) -> bool: | |
| if config is None or _AUTO_PADDING_FREE_ENV_DISABLED: | |
| return False | |
| if not getattr(config, "packing", False): | |
| if getattr(config, "packing", False): | |
| return False | |
| return not getattr(config, "_unsloth_disable_auto_packing", False) | |
| def _disable_sample_packing(config): | |
| if config is None: | |
| return | |
| for attr, value in (("packing", False), ("padding_free", False)): | |
| if hasattr(config, attr): | |
| setattr(config, attr, value) | |
| if hasattr(config, "remove_unused_columns"): | |
| setattr(config, "remove_unused_columns", True) | |
| setattr(config, "_unsloth_disable_auto_packing", True) | |
| _AUTO_PACK_SKIP_MESSAGES = ( | |
| "packing is not supported", | |
| "padding-free training", | |
| "passing a custom data collator", | |
| ) | |
| def _should_skip_auto_packing_error(exc: Exception) -> bool: | |
| message = str(exc).lower() | |
| return any(msg in message for msg in _AUTO_PACK_SKIP_MESSAGES) | |
| return not getattr(config, "padding_free", False) | |
| def _should_auto_padding_free(config) -> bool: | |
| if config is None or _AUTO_PADDING_FREE_ENV_DISABLED: | |
| return False | |
| if getattr(config, "packing", False): | |
| return False | |
| if getattr(config, "_unsloth_disable_auto_padding_free", False): | |
| return False | |
| return not getattr(config, "padding_free", False) |
| auto_padding_free_active = False | ||
| padding_free_requested = getattr(config_arg, "padding_free", None) is True | ||
| if not blocked: | ||
| if padding_free_requested: | ||
| configure_padding_free(config_arg) | ||
| elif not is_unsupported_gemma and _should_auto_padding_free(config_arg): | ||
| configure_padding_free(config_arg) | ||
| auto_padding_free_active = True | ||
| logger.info( | ||
| "Unsloth: Auto sample packing failed because trainer reported an incompatible setup (%s).", | ||
| exc, | ||
| "Unsloth: Padding-free batching auto-enabled for SFTTrainer instance." | ||
| ) | ||
| elif is_unsupported_gemma and _should_auto_padding_free(config_arg): | ||
| logger.info( | ||
| "Unsloth: Padding-free batching auto-disabled for Gemma 2 (requires flash attention)." | ||
| ) | ||
| _disable_sample_packing(config_arg) | ||
| auto_pack_active = False | ||
| original_init(self, *args, **kwargs) | ||
| else: | ||
| raise | ||
|
|
||
| original_init(self, *args, **kwargs) |
There was a problem hiding this comment.
The previous implementation for auto-packing included a robust try...except block to handle SFTTrainer initialization failures. This was a great defensive pattern that seems to have been lost in this refactoring.
Auto-enabling padding_free can also cause SFTTrainer to raise a ValueError. To make this feature more robust, I suggest re-introducing a similar error handling mechanism. This would wrap the original_init call and, upon specific failures, disable padding-free and retry.
To support this, you'll need to add the following helper functions somewhere in the file's scope (e.g., after _should_auto_padding_free):
def _disable_padding_free(config):
if config is None:
return
if hasattr(config, "padding_free"):
setattr(config, "padding_free", False)
setattr(config, "_unsloth_disable_auto_padding_free", True)
_PADDING_FREE_SKIP_MESSAGES = (
"padding-free training is not supported",
"passing a custom data collator",
)
def _should_skip_padding_free_error(exc: Exception) -> bool:
message = str(exc).lower()
return any(msg in message for msg in _PADDING_FREE_SKIP_MESSAGES)With these helpers (and the change to _should_auto_padding_free from my other comment), you can then add the try-except block as suggested.
auto_padding_free_active = False
padding_free_requested = getattr(config_arg, "padding_free", None) is True
if not blocked:
if padding_free_requested:
configure_padding_free(config_arg)
elif not is_unsupported_gemma and _should_auto_padding_free(config_arg):
configure_padding_free(config_arg)
auto_padding_free_active = True
logger.info(
"Unsloth: Padding-free batching auto-enabled for SFTTrainer instance."
)
elif is_unsupported_gemma and _should_auto_padding_free(config_arg):
logger.info(
"Unsloth: Padding-free batching auto-disabled for Gemma 2 (requires flash attention)."
)
try:
original_init(self, *args, **kwargs)
except ValueError as exc:
if auto_padding_free_active and _should_skip_padding_free_error(exc):
logger.warning(
"Unsloth: Auto padding-free failed, falling back to padding. Reason: %s.",
exc,
)
_disable_padding_free(config_arg)
auto_padding_free_active = False
original_init(self, *args, **kwargs)
else:
raise
See #3672 for details.