data_utils.py
2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from collections import defaultdict
from dataclasses import dataclass
from itertools import chain
import numpy as np
from datasets.features import ClassLabel, Features, List, Sequence
from transformers.data.data_collator import DataCollator, DataCollatorMixin, pad_without_fast_tokenizer_warning
from transformers.tokenization_utils_base import PreTrainedTokenizerBase
from transformers.utils.generic import PaddingStrategy
from .constants import MASK_VALUE
# based on transformers.data.data_collator.DataCollatorForTokenClassification
@dataclass
class DataCollatorForHydra(DataCollatorMixin):
tokenizer: PreTrainedTokenizerBase
features: Features
padding: bool | str | PaddingStrategy = True
max_length: int | None = None
pad_to_multiple_of: int | None = None
label_pad_token_id: int = MASK_VALUE
return_tensors: str = 'pt'
def _pad_labels(self, labels: list[int], sequence_length: int) -> list[int]:
if self.tokenizer.padding_side == 'right':
return list(labels) + [MASK_VALUE] * (sequence_length - len(labels))
else:
return [MASK_VALUE] * (sequence_length - len(labels)) + list(labels)
def torch_call(self, batch): # type: ignore[no-untyped-def]
import torch
input_features = [{k: v for k, v in instance.items() if k in self.tokenizer.model_input_names} for instance in batch]
padded_batch = pad_without_fast_tokenizer_warning( # type: ignore[no-untyped-call]
self.tokenizer,
input_features,
padding=self.padding,
max_length=self.max_length,
pad_to_multiple_of=self.pad_to_multiple_of,
return_tensors='pt',
)
sequence_length = padded_batch['input_ids'].shape[1]
for category, feat in self.features.items():
if isinstance(feat.feature, ClassLabel):
padded_labels = [self._pad_labels(instance[category], sequence_length) for instance in batch]
padded_batch[category] = torch.tensor(padded_labels, dtype=torch.long)
elif isinstance(feat.feature, (Sequence, List)):
# pad the matrix rows to sequence_length
# and add empty rows to pad to obtain a sequence_length x sequence_length matrix
nested_padded_labels = [
[
self._pad_labels(row, sequence_length) for row in instance[category]
] + [
self._pad_labels([], sequence_length) for _ in range(sequence_length - len(instance[category]))
]
for instance in batch
]
padded_batch[category] = torch.tensor(nested_padded_labels, dtype=torch.long)
else:
raise ValueError(f'Unsupported feature type {type(feat.feature)} for "{category}".')
return padded_batch
def numpy_call(self, batch): # type: ignore[no-untyped-def]
raise NotImplementedError