-
Notifications
You must be signed in to change notification settings - Fork 234
Add last_response_only parameter to train_on_responses_only #579
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
Open
maximedb
wants to merge
3
commits into
unslothai:main
Choose a base branch
from
maximedb:train_on_last_response_only
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -184,16 +184,22 @@ def _find_common_token_ids(component, tokenizer, force_match = False): | |||||||||||
|
|
||||||||||||
| def train_on_responses_only( | ||||||||||||
| trainer, | ||||||||||||
| instruction_part = None, | ||||||||||||
| response_part = None, | ||||||||||||
| force_match = True, # Match newlines as well! | ||||||||||||
| tokenizer = None, # Optional | ||||||||||||
| return_function = False, # Useful for iterating over lists | ||||||||||||
| num_proc = None, | ||||||||||||
| instruction_part = None, | ||||||||||||
| response_part = None, | ||||||||||||
| force_match = True, # Match newlines as well! | ||||||||||||
| tokenizer = None, # Optional | ||||||||||||
| return_function = False, # Useful for iterating over lists | ||||||||||||
| num_proc = None, | ||||||||||||
| last_response_only = False, # Train only on the last assistant turn | ||||||||||||
| ): | ||||||||||||
| """ | ||||||||||||
| Trains only on responses and not on the instruction by masking out | ||||||||||||
| the labels with -100 for the instruction part. | ||||||||||||
|
|
||||||||||||
| If last_response_only=True, only the final assistant turn has its | ||||||||||||
| labels unmasked; all earlier assistant turns remain masked at -100 | ||||||||||||
| (they are never written, so they keep the initialized -100 values | ||||||||||||
| and are not copied from old_labels either). | ||||||||||||
| """ | ||||||||||||
| # All Unsloth Zoo code licensed under LGPLv3 | ||||||||||||
| if tokenizer is None and trainer is not None: | ||||||||||||
|
|
@@ -249,13 +255,16 @@ def _train_on_responses_only(examples): | |||||||||||
| for input_ids, old_labels in zip(input_ids_, labels_): | ||||||||||||
| n = len(input_ids) | ||||||||||||
| labels = [-100] * n | ||||||||||||
|
|
||||||||||||
| use_old_labels = False | ||||||||||||
| if old_labels is not None: | ||||||||||||
| use_old_labels = True | ||||||||||||
| assert(n == len(old_labels)) | ||||||||||||
| n_minus_1 = n - 1 | ||||||||||||
| j = 0 | ||||||||||||
|
|
||||||||||||
| # Collect all (assistant_k, user_j) spans for this sample | ||||||||||||
| spans = [] | ||||||||||||
| while j < n: | ||||||||||||
| # Find <assistant> | ||||||||||||
| if (input_ids[j] == A_first) and \ | ||||||||||||
|
|
@@ -308,20 +317,27 @@ def _train_on_responses_only(examples): | |||||||||||
| k = n | ||||||||||||
| pass | ||||||||||||
|
|
||||||||||||
| if not use_old_labels: | ||||||||||||
| # Now copy input_ids to labels | ||||||||||||
| labels[assistant_k : user_j] = input_ids [assistant_k : user_j] | ||||||||||||
| # print(assistant_j, assistant_k, user_j, user_k) | ||||||||||||
| else: | ||||||||||||
| # Copy over from old labels! | ||||||||||||
| labels[assistant_k : user_j] = old_labels[assistant_k : user_j] | ||||||||||||
| spans.append((assistant_k, user_j)) | ||||||||||||
| break | ||||||||||||
| pass | ||||||||||||
| j += 1 | ||||||||||||
| pass | ||||||||||||
| pass | ||||||||||||
| j += 1 | ||||||||||||
| pass | ||||||||||||
|
|
||||||||||||
| # Apply labels: only the last assistant turn when last_response_only=True. | ||||||||||||
| # Note: spans[-1:] safely returns [] when spans is empty (no assistant turn | ||||||||||||
| # was found), so a sample with no assistant turn stays fully masked at -100. | ||||||||||||
| apply_spans = spans[-1:] if last_response_only else spans | ||||||||||||
|
Contributor
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. [3/11 reviewers]
Suggested change
|
||||||||||||
| for assistant_k, user_j in apply_spans: | ||||||||||||
| if not use_old_labels: | ||||||||||||
| # Now copy input_ids to labels | ||||||||||||
| labels[assistant_k : user_j] = input_ids [assistant_k : user_j] | ||||||||||||
| else: | ||||||||||||
| # Copy over from old labels! | ||||||||||||
| labels[assistant_k : user_j] = old_labels[assistant_k : user_j] | ||||||||||||
|
|
||||||||||||
| all_labels.append(labels) | ||||||||||||
| pass | ||||||||||||
| return { "labels" : torch.tensor(all_labels, dtype = torch.int64) if use_tensors else all_labels } | ||||||||||||
|
|
||||||||||||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[3/11 reviewers] The new
last_response_onlyparameter is not forwarded toUnslothVisionDataCollator.__init__inunsloth_zoo/vision_utils.py.UnslothVisionDataCollatorcurrently calls_train_on_responses_only(None, ..., return_function=True)on line 734 without passinglast_response_only, so VLM fine-tuning cannot access the new behavior. Since the function signature is unchanged (kwarg with default), this is a forward-compatible addition. Suggested forwarding invision_utils.py(separate file, see that file for the actual edit):No change needed on this line itself — this comment flags the VLM integration to also be added in
vision_utils.pyin the follow-up.