Skip to content

fix mask_user_labels #130

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions chat/dialogues.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,17 @@ def _save_pretrained(self, save_directory: Union[str, Path]) -> None:

@classmethod
def _from_pretrained(
cls: Type[T],
*,
model_id: str,
revision: Optional[str],
cache_dir: Optional[Union[str, Path]],
force_download: bool,
proxies: Optional[Dict],
resume_download: bool,
local_files_only: bool,
token: Optional[Union[str, bool]],
**model_kwargs,
cls: Type[T],
*,
model_id: str,
revision: Optional[str],
cache_dir: Optional[Union[str, Path]],
force_download: bool,
proxies: Optional[Dict],
resume_download: bool,
local_files_only: bool,
token: Optional[Union[str, bool]],
**model_kwargs,
) -> T:
"""Loads the dialogue template from a local directory or the Huggingface Hub.

Expand Down Expand Up @@ -232,10 +232,11 @@ def prepare_dialogue(example, dialogue_template, is_train=True):
def mask_user_labels(tokenizer, dialogue_template, labels):
"""Masks the user turns of a dialogue from the loss"""
user_token_id = tokenizer.convert_tokens_to_ids(dialogue_template.user_token)
system_token_id = tokenizer.convert_tokens_to_ids(dialogue_template.system_token)
assistant_token_id = tokenizer.convert_tokens_to_ids(dialogue_template.assistant_token)
for idx, label_id in enumerate(labels):
if label_id == user_token_id:
if label_id in [user_token_id, system_token_id]:
current_idx = idx
while labels[current_idx] != assistant_token_id and current_idx < len(labels):
while current_idx < len(labels) and labels[current_idx] != assistant_token_id:
labels[current_idx] = IGNORE_INDEX
current_idx += 1
3 changes: 2 additions & 1 deletion chat/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ def group_texts(examples):
for k, t in concatenated_examples.items()
}
labels = result["input_ids"].copy()
mask_user_labels(tokenizer, dialogue_template, labels)
for label in labels:
mask_user_labels(tokenizer, dialogue_template, label)
result["labels"] = labels
return result

Expand Down