-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from tanganke/llama
Fix bug in config/clip-vit-base-patch32_robustness_corrupted.yaml
- Loading branch information
Showing
5 changed files
with
144 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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 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,53 @@ | ||
from typing import Dict, List, Optional | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
from torch.nn.utils.rnn import pad_sequence | ||
|
||
|
||
def padded_collate_sft( | ||
batch: List[Dict[str, List[int]]], | ||
padding_idx: int = 0, | ||
input_ids_key: str = "input_ids", | ||
attention_mask_key: Optional[str] = "attention_mask", | ||
labels_key: Optional[str] = "labels", | ||
ignore_idx: int = -100, | ||
) -> Dict[str, torch.Tensor]: | ||
"""Pad a batch of sequences to the longest sequence length in the batch, and | ||
convert integer lists to tensors. | ||
Args: | ||
batch (List[Dict[str, List[int]]]): A list of dictionaries containing input, label pairs. | ||
padding_idx (int): Padding index for input ids. Defaults to 0. | ||
ignore_idx (int): Padding index for labels. Defaults to -100. | ||
Returns: | ||
Dict[str, torch.Tensor]: Collated input and label tensors. | ||
""" | ||
input_ids = pad_sequence( | ||
[torch.tensor(x[input_ids_key]) for x in batch], | ||
batch_first=True, | ||
padding_value=padding_idx, | ||
) | ||
if attention_mask_key is not None and attention_mask_key in batch[0]: | ||
attention_mask = pad_sequence( | ||
[torch.tensor(x[attention_mask_key]) for x in batch], | ||
batch_first=True, | ||
padding_value=0, | ||
) | ||
else: | ||
attention_mask = None | ||
labels = pad_sequence( | ||
[torch.tensor(x[labels_key]) for x in batch], | ||
batch_first=True, | ||
padding_value=ignore_idx, | ||
) | ||
|
||
if attention_mask is not None: | ||
return { | ||
input_ids_key: input_ids, | ||
attention_mask_key: attention_mask, | ||
labels_key: labels, | ||
} | ||
else: | ||
return {input_ids_key: input_ids, labels_key: labels} |
This file contains 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 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 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