Skip to content
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

[Fix]Fix attention mask in default_collate_fn #567

Merged
merged 1 commit into from
Apr 15, 2024
Merged
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
8 changes: 7 additions & 1 deletion xtuner/dataset/collate_fns/default_collate_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def default_collate_fn(instances: Sequence[Dict],
if has_image:
pixel_values.append(example['pixel_values'])

ori_length = [len(ids) for ids in input_ids]
if len(instances) > 1:
input_ids = pad_sequence(
input_ids, batch_first=True, padding_value=pad_index)
Expand All @@ -53,7 +54,12 @@ def default_collate_fn(instances: Sequence[Dict],
attention_mask = None
position_ids = torch.stack(position_ids, dim=0)
else:
attention_mask = input_ids.ne(pad_index)
# Some tokenizers have the same eos token and pad token, so input_ids
# cannot be masked directly based on the pad token id.
attention_mask = torch.zeros_like(input_ids).bool()
for i in ori_length:
attention_mask[:i] = True

bs, seq_len = input_ids.shape
position_ids = torch.arange(seq_len).unsqueeze(0).long().repeat(bs, 1)

Expand Down
Loading