-
Notifications
You must be signed in to change notification settings - Fork 10
feat(train): hook in assistant_only_loss for TRL SFT path #23
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7d41ae4
feat(train): expose assistant_only_loss on start_training
paulpak58 6a4b636
docs(train): document assistant_only_loss on start_training
paulpak58 c52c271
test(train): run the assistant-mask regression in CI on a hermetic to…
paulpak58 3dae39a
fix(train): drop rows whose assistant turn falls past max_seq_length
paulpak58 2358e58
fix(train): reject a base model whose chat template cannot mask assis…
paulpak58 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
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
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 |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| """assistant_only_loss labels assistant turns and nothing else.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| MESSAGES = [ | ||
| { | ||
| "role": "user", | ||
| "content": ( | ||
| "project 01 | tempo 70\nStereo Out | out | vol 6.0 | clip\n" | ||
| "Why is the master clipping?" | ||
| ), | ||
| }, | ||
| { | ||
| "role": "assistant", | ||
| "content": ( | ||
| "findings:\n- [critical] Stereo Out | clip: clipping latched at 6 dB\n" | ||
| '<|tool_call_start|>[set_volume(track="Stereo Out", db=0.0)]<|tool_call_end|>' | ||
| ), | ||
| }, | ||
| {"role": "user", "content": "And the bass?"}, | ||
| {"role": "assistant", "content": "Trap Bass sits at -3 dB and does not clip."}, | ||
| ] | ||
|
|
||
| # The shape of LFM2.5's template: the assistant header is outside the block, so | ||
| # only the reply body and its terminator carry a live label. | ||
| TEMPLATE = ( | ||
| "{%- for m in messages -%}" | ||
| "{{- '<|im_start|>' + m.role + '\n' -}}" | ||
| "{%- if m.role == 'assistant' -%}{%- generation -%}" | ||
| "{{- m.content + '<|im_end|>\n' -}}" | ||
| "{%- endgeneration -%}" | ||
| "{%- else -%}{{- m.content + '<|im_end|>\n' -}}{%- endif -%}" | ||
| "{%- endfor -%}" | ||
| ) | ||
|
|
||
|
|
||
| def make_tokenizer(template: str = TEMPLATE): | ||
| """A character-level tokenizer built in memory: no hub, no cache, exact offsets.""" | ||
| pytest.importorskip("transformers") | ||
| from tokenizers import Regex, Tokenizer, decoders, models, pre_tokenizers | ||
| from transformers import PreTrainedTokenizerFast | ||
|
|
||
| chars = [chr(c) for c in range(32, 127)] + ["\n"] | ||
| inner = Tokenizer( | ||
| models.WordLevel( | ||
| vocab={t: i for i, t in enumerate(["<|pad|>", "<|unk|>", *chars])}, | ||
| unk_token="<|unk|>", | ||
| ) | ||
| ) | ||
| inner.pre_tokenizer = pre_tokenizers.Split(Regex("."), behavior="isolated") | ||
| inner.decoder = decoders.Fuse() | ||
| return PreTrainedTokenizerFast( | ||
| tokenizer_object=inner, | ||
| pad_token="<|pad|>", | ||
| eos_token="\n", | ||
| chat_template=template, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def tokenizer(): | ||
| return make_tokenizer() | ||
|
|
||
|
|
||
| def encode(tokenizer, messages=MESSAGES): | ||
| return tokenizer.apply_chat_template( | ||
| messages, tokenize=True, return_dict=True, return_assistant_tokens_mask=True | ||
| ) | ||
|
|
||
|
|
||
| def test_template_marks_only_the_assistant_replies(tokenizer): | ||
| encoded = encode(tokenizer) | ||
| ids, mask = encoded["input_ids"], encoded["assistant_masks"] | ||
| assert len(ids) == len(mask) | ||
| assert 0 < sum(mask) < len(ids) | ||
|
|
||
| live = tokenizer.decode([i for i, m in zip(ids, mask) if m]) | ||
| assert live == "".join( | ||
| f"{m['content']}<|im_end|>\n" for m in MESSAGES if m["role"] == "assistant" | ||
| ) | ||
| masked = tokenizer.decode([i for i, m in zip(ids, mask) if not m]) | ||
| for m in MESSAGES: | ||
| if m["role"] == "user": | ||
| assert m["content"] in masked | ||
| assert "<|im_start|>assistant" in masked | ||
|
|
||
|
|
||
| def test_collator_masks_every_non_assistant_token(tokenizer): | ||
| pytest.importorskip("torch") | ||
| from trl.trainer.sft_trainer import DataCollatorForLanguageModeling | ||
|
|
||
| encoded = encode(tokenizer) | ||
| ids, mask = encoded["input_ids"], encoded["assistant_masks"] | ||
| batch = DataCollatorForLanguageModeling(pad_token_id=tokenizer.pad_token_id)( | ||
| [{"input_ids": ids, "assistant_masks": mask}] | ||
| ) | ||
| labels = batch["labels"][0] | ||
| assert (labels == -100).tolist() == [m == 0 for m in mask] | ||
|
|
||
|
|
||
| def test_rows_truncated_past_their_assistant_turn_are_dropped(tokenizer): | ||
| from lqh.train.data_utils import drop_rows_without_assistant_labels | ||
|
|
||
| rows = [{"messages": MESSAGES}] | ||
| first_label = encode(tokenizer)["assistant_masks"].index(1) | ||
|
|
||
| kept, dropped = drop_rows_without_assistant_labels(rows, tokenizer, first_label) | ||
| assert (kept, dropped) == ([], 1) | ||
| kept, dropped = drop_rows_without_assistant_labels(rows, tokenizer, first_label + 1) | ||
| assert (kept, dropped) == (rows, 0) | ||
| assert drop_rows_without_assistant_labels(rows, tokenizer, None) == (rows, 0) | ||
|
|
||
|
|
||
| def test_a_template_without_a_generation_block_is_rejected_before_launch(tmp_path): | ||
| from lqh.tools.handlers import _assistant_mask_unsupported | ||
|
|
||
| blocked = TEMPLATE.replace("{%- generation -%}", "").replace( | ||
| "{%- endgeneration -%}", "" | ||
| ) | ||
| make_tokenizer(blocked).save_pretrained(tmp_path / "tok") | ||
| # base_model given project-relative, as a local checkpoint would be | ||
| assert _assistant_mask_unsupported(tmp_path, "tok") == ( | ||
| "its chat template has no {% generation %} block" | ||
| ) | ||
|
|
||
| make_tokenizer().save_pretrained(tmp_path / "tok") | ||
| assert _assistant_mask_unsupported(tmp_path, "tok") is None | ||
| # unloadable tokenizer fails open: the trainer is the backstop | ||
| assert _assistant_mask_unsupported(tmp_path, str(tmp_path / "absent")) is None |
Oops, something went wrong.
Oops, something went wrong.
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.
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.
[P1] Avoid truncating away every trainable token. SFTConfig defaults to truncation_mode keep_start. With assistant_only_loss enabled and the existing 2048-token limit, a long user turn can leave all assistant tokens beyond the cutoff. TRL checks the mask before truncation, so this is not rejected. I reproduced a 3013-token LFM2.5 row that yielded zero labels other than -100, which can produce zero or NaN-loss batches exactly for the user-heavy rows this option targets. Set truncation_mode to keep_end when this flag is enabled, or explicitly reject or drop rows with no assistant labels after truncation.