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

Allow flexibility for users to pass attention_mask in data_collator #2234

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions swift/llm/utils/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,13 +1008,19 @@ def data_collator(self, batch: List[Dict[str, Any]], padding_to: Optional[int] =
if 'inputs_embeds' in batch[0]:
inputs_embeds = [b['inputs_embeds'] for b in batch]
res['inputs_embeds'] = inputs_embeds
res['attention_mask'] = [
torch.ones((inputs_embeds[i].shape[0]), dtype=torch.int64) for i in range(len(inputs_embeds))
]
if 'attention_mask' in batch[0]:
res['attention_mask'] = batch[0]['attention_mask']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The batch size may be greater than 1.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, let me think about how to deal with batch size greater than 1.

else:
res['attention_mask'] = [
torch.ones((inputs_embeds[i].shape[0]), dtype=torch.int64) for i in range(len(inputs_embeds))
]
elif 'input_ids' in batch[0]:
input_ids = [torch.tensor(b['input_ids']) for b in batch]
res['input_ids'] = input_ids
res['attention_mask'] = [torch.ones(len(input_ids[i]), dtype=torch.int64) for i in range(len(input_ids))]
if 'attention_mask' in batch[0]:
res['attention_mask'] = batch[0]['attention_mask']
else:
res['attention_mask'] = [torch.ones(len(input_ids[i]), dtype=torch.int64) for i in range(len(input_ids))]

for key in ['labels', 'loss_scale', 'position_ids']:
if key in batch[0]:
Expand Down
Loading