-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Correct masking when the same roles are present in adjacent messages in DataCollatorForCompletionOnlyLM #1994
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
Comments
I'm not sure why we would want to have a dataset in which the role is not interleaved. Moreover, some chat templates explicitly assume that messages are an interleaving of user and assistant messages. |
We encountered this problem because we wanted to fine-tune models on real-human conversations. In natural conversation, it is common to see an utterance followed by another from the same speaker in a context where that pause happens., for example, in counseling conversations. |
And another scenario is NPC dialogue in the game. When a player doesn't interrupt, the LLM could keep saying with the same role @qgallouedec |
Thank you very much for the clarification. We are currently working on a new dataset format that could be linked (for different motivation though). See #2148 |
Thank you! |
…ForCompletionOnlyLM (huggingface#3223) Refactors the masking logic in `DataCollatorForCompletionOnlyLM` to correctly handle conversations with multiple instruction roles (e.g., user, tool) and consecutive assistant turns, enabling its use for more complex dialogue formats like agent trajectories. Previously, the collator assumed a strict alternation of a single instruction template and a response template (e.g., User -> Assistant). This failed for: 1. Datasets with multiple instruction roles (e.g., user prompts and tool calls). 2. Sequences with consecutive assistant messages (e.g., Assistant -> Assistant). This commit addresses these limitations: - Updates `__init__` to accept a list of strings or pre-tokenized IDs for `instruction_template`, allowing multiple distinct instruction roles. - Rewrites the core masking logic in `torch_call`: - It now identifies all occurrences of response and all specified instruction templates. - For each assistant response, it unmasks tokens from the end of its template up to the beginning of the *next* instruction template or the sequence end. - Correctly handles consecutive assistant turns by masking the template tokens of subsequent responses while unmasking their content. - Adds comprehensive unit tests (`test_masking_*`) covering multi-role scenarios, consecutive assistant messages, left-padding, and initialization with tokenized templates. This allows `DataCollatorForCompletionOnlyLM` to process conversational data commonly found in ChatML formats and agent fine-tuning datasets. Related: huggingface#1994, huggingface#2545
Feature request
In the function torch_call of DataCollatorForCompletionOnlyLM, the suggested new feature can support correct masking on user requests even if the user and assistant messages are not present alternately.
The current version requires an assistant message must follow a user message, and a user message follows an assistant message.
Two adjacent messages with the same role will cause wrong masking, as the current codes haven't considered that a large start variable can be paired with a small end variable when two roles don't take turn by turn :
Using two pointers can solve the issue, below is an example solution:
This code can be tested to output:
Motivation
Support flexible and correct masking strategies for DataCollatorForCompletionOnlyLM, especially allowing masking for continuous messages from the same role.
Your contribution
I submit a PR: #2000
The text was updated successfully, but these errors were encountered: