oweller2 commited on
Commit
5448b02
1 Parent(s): 3807a72
Files changed (1) hide show
  1. tokenizer.py +83 -0
tokenizer.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PreTrainedTokenizerFast
2
+ import numpy
3
+ import torch
4
+
5
+ class ModernDecoderBERTTokenizer(PreTrainedTokenizerFast):
6
+
7
+ def _batch_encode_plus(self, *args, **kwargs):
8
+ outputs = super()._batch_encode_plus(*args, **kwargs)
9
+ del outputs["token_type_ids"]
10
+
11
+ # Get the input_ids to check for EOS tokens
12
+ input_ids = outputs['input_ids']
13
+
14
+ # Function to check if sequence ends with EOS token
15
+ def ends_with_eos(sequence):
16
+ if len(sequence) == 0:
17
+ return False
18
+ return sequence[-1] == self.eos_token_id
19
+
20
+ # Check for EOS tokens using input_ids only
21
+ if isinstance(input_ids, torch.Tensor):
22
+ last_token_is_eos = torch.tensor([
23
+ ends_with_eos(seq) for seq in input_ids
24
+ ], dtype=torch.bool)
25
+
26
+ if last_token_is_eos.all():
27
+ # If all sequences have EOS, just truncate all
28
+ for key in ['input_ids', 'attention_mask']:
29
+ outputs[key] = outputs[key][..., :-1]
30
+ elif last_token_is_eos.any():
31
+ # Process each sequence individually
32
+ batch_size = input_ids.shape[0]
33
+ for i in range(batch_size):
34
+ if last_token_is_eos[i]:
35
+ for key in ['input_ids', 'attention_mask']:
36
+ # Remove last token and add padding at start for this sequence
37
+ truncated = outputs[key][i, :-1]
38
+ outputs[key][i] = torch.cat([
39
+ torch.zeros_like(truncated[:1]),
40
+ truncated
41
+ ])
42
+
43
+ elif isinstance(input_ids, numpy.ndarray):
44
+ last_token_is_eos = numpy.array([
45
+ ends_with_eos(seq) for seq in input_ids
46
+ ], dtype=bool)
47
+
48
+ if last_token_is_eos.all():
49
+ # If all sequences have EOS, just truncate all
50
+ for key in ['input_ids', 'attention_mask']:
51
+ outputs[key] = outputs[key][..., :-1]
52
+ elif last_token_is_eos.any():
53
+ batch_size = input_ids.shape[0]
54
+ for i in range(batch_size):
55
+ if last_token_is_eos[i]:
56
+ for key in ['input_ids', 'attention_mask']:
57
+ # Remove last token and add padding at start for this sequence
58
+ truncated = outputs[key][i, :-1]
59
+ outputs[key][i] = numpy.concatenate([
60
+ numpy.zeros_like(truncated[:1]),
61
+ truncated
62
+ ])
63
+
64
+ elif isinstance(input_ids, list):
65
+ last_token_is_eos = [ends_with_eos(seq) for seq in input_ids]
66
+
67
+ if all(last_token_is_eos):
68
+ # If all sequences have EOS, just truncate all
69
+ for key in ['input_ids', 'attention_mask']:
70
+ outputs[key] = [sequence[:-1] for sequence in outputs[key]]
71
+ elif any(last_token_is_eos):
72
+ for key in ['input_ids', 'attention_mask']:
73
+ outputs[key] = [
74
+ [0] + sequence[:-1] if is_eos else sequence
75
+ for sequence, is_eos in zip(outputs[key], last_token_is_eos)
76
+ ]
77
+
78
+ return outputs
79
+
80
+
81
+ # Register the class
82
+ from transformers import AutoTokenizer
83
+ AutoTokenizer.register(ModernDecoderBERTTokenizer, fast_tokenizer_class=ModernDecoderBERTTokenizer[])