File size: 3,136 Bytes
ea51a3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from __future__ import annotations

import json
import os

from transformers import PreTrainedTokenizer


class TransWaveByteTokenizer(PreTrainedTokenizer):

    vocab_files_names = {
        "vocab_file": "vocab.json"
    }

    model_input_names = [
        "input_ids"
    ]


    def __init__(
        self,
        vocab_file=None,
        model_max_length=20480,
        **kwargs,
    ):

        self.vocab_file = vocab_file

        self._id_to_token = [
            f"<0x{i:02X}>"
            for i in range(256)
        ]

        self._token_to_id = {
            token: i
            for i, token
            in enumerate(self._id_to_token)
        }

        # Values may already arrive through tokenizer_config.json.
        # Never pass the same keyword twice.

        kwargs.setdefault(
            "model_max_length",
            model_max_length,
        )

        kwargs.setdefault(
            "clean_up_tokenization_spaces",
            False,
        )

        super().__init__(**kwargs)


    @property
    def vocab_size(self):

        return 256


    def get_vocab(self):

        vocab = dict(
            self._token_to_id
        )

        vocab.update(
            self.added_tokens_encoder
        )

        return vocab


    def _tokenize(
        self,
        text,
        **kwargs,
    ):

        return [
            self._id_to_token[b]
            for b in text.encode("utf-8")
        ]


    def _convert_token_to_id(
        self,
        token,
    ):

        if token not in self._token_to_id:
            raise ValueError(
                f"Unknown byte token: {token}"
            )

        return self._token_to_id[token]


    def _convert_id_to_token(
        self,
        index,
    ):

        index = int(index)

        if not 0 <= index < 256:
            raise ValueError(
                f"Byte id out of range: {index}"
            )

        return self._id_to_token[index]


    def convert_tokens_to_string(
        self,
        tokens,
    ):

        data = bytearray()

        for token in tokens:

            if token in self._token_to_id:
                data.append(
                    self._token_to_id[token]
                )

        return bytes(data).decode(
            "utf-8",
            errors="replace",
        )


    def save_vocabulary(
        self,
        save_directory,
        filename_prefix=None,
    ):

        os.makedirs(
            save_directory,
            exist_ok=True,
        )

        filename = (
            (
                filename_prefix + "-"
            )
            if filename_prefix
            else ""
        ) + "vocab.json"

        path = os.path.join(
            save_directory,
            filename,
        )

        vocab = {
            token: i
            for i, token
            in enumerate(self._id_to_token)
        }

        with open(
            path,
            "w",
            encoding="utf-8",
        ) as f:

            json.dump(
                vocab,
                f,
                ensure_ascii=False,
                indent=2,
            )

        return (path,)