-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Mistral packing, train on completions only, simplifications #3709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
danielhanchen
merged 5 commits into
unslothai:main
from
djsaunde:mistral-packing-padding-fix
Dec 11, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,15 +107,12 @@ def configure_sample_packing(config): | |
| _ensure_trl_warning_filter() | ||
| setattr(config, "packing", True) | ||
| setattr(config, "padding_free", True) | ||
| setattr(config, "remove_unused_columns", False) | ||
|
|
||
|
|
||
| def configure_padding_free(config): | ||
| """Mutate an ``SFTConfig`` so TRL enables padding-free batching without packing.""" | ||
| _ensure_trl_warning_filter() | ||
| setattr(config, "padding_free", True) | ||
| if hasattr(config, "remove_unused_columns"): | ||
| setattr(config, "remove_unused_columns", False) | ||
|
|
||
|
|
||
| def enable_sample_packing( | ||
|
|
@@ -150,48 +147,15 @@ def torch_call_with_lengths(examples: Sequence[dict]): | |
| batch = original_torch_call(examples) | ||
| if examples and isinstance(examples[0], dict): | ||
| seq_lengths: list[int] = [] | ||
| per_example_counts: list[int] = [] | ||
| for example in examples: | ||
| lengths = example.get(sequence_lengths_key) | ||
| if isinstance(lengths, Iterable): | ||
| numeric_lengths = [int(length) for length in lengths] | ||
| seq_lengths.extend(numeric_lengths) | ||
| per_example_counts.append(len(numeric_lengths)) | ||
| else: | ||
| per_example_counts.append(0) | ||
| seq_lengths.extend(int(length) for length in lengths) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplified this since |
||
| if seq_lengths: | ||
| batch["packed_seq_lengths"] = torch.tensor( | ||
| seq_lengths, dtype = torch.int32 | ||
| ) | ||
|
|
||
| position_ids = batch.get("position_ids") | ||
| input_ids = batch.get("input_ids") | ||
| if position_ids is None and input_ids is not None: | ||
| position_ids = torch.zeros_like( | ||
| input_ids, dtype = torch.long, device = input_ids.device | ||
| ) | ||
|
|
||
| if position_ids is not None and input_ids is not None: | ||
| seq_index = 0 | ||
| for row_idx, count in enumerate(per_example_counts): | ||
| cursor = 0 | ||
| for _ in range(count): | ||
| length = seq_lengths[seq_index] | ||
| if length > 0: | ||
| position_ids[row_idx, cursor : cursor + length] = ( | ||
| torch.arange( | ||
| length, | ||
| dtype = torch.long, | ||
| device = position_ids.device, | ||
| ) | ||
| ) | ||
| cursor += length | ||
| seq_index += 1 | ||
| batch["position_ids"] = position_ids | ||
|
|
||
| if "attention_mask" in batch and getattr( | ||
| collator, "return_position_ids", False | ||
| ): | ||
| if "attention_mask" in batch: | ||
| batch.pop("attention_mask") | ||
| return batch | ||
|
|
||
|
|
@@ -201,23 +165,12 @@ def torch_call_with_lengths(examples: Sequence[dict]): | |
|
|
||
| def enable_padding_free_metadata(model, trainer): | ||
| """Inject seq-length metadata when padding-free batching is enabled without packing.""" | ||
|
|
||
| trainer_args = getattr(trainer, "args", None) | ||
| if ( | ||
| trainer_args is not None | ||
| and hasattr(trainer_args, "remove_unused_columns") | ||
| and trainer_args.remove_unused_columns | ||
| ): | ||
| trainer_args.remove_unused_columns = False | ||
|
|
||
| _ensure_trl_warning_filter() | ||
| collator = getattr(trainer, "data_collator", None) | ||
| if ( | ||
| collator is None | ||
| or getattr(collator, "_unsloth_padding_free_lengths_wrapped", False) | ||
| or not getattr(collator, "padding_free", False) | ||
| ): | ||
| # Nothing to do if there's no collator, we've already wrapped it, or padding-free is off. | ||
| return | ||
|
|
||
| mark_allow_overlength(model) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.