File size: 12,409 Bytes
cb99b35
 
 
28f21ff
 
cb99b35
 
 
 
 
 
28f21ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb99b35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28f21ff
cb99b35
 
 
28f21ff
cb99b35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28f21ff
 
cb99b35
 
 
 
 
 
 
28f21ff
cb99b35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28f21ff
cb99b35
 
 
 
 
28f21ff
 
 
 
cb99b35
 
 
 
 
 
28f21ff
 
 
cb99b35
28f21ff
cb99b35
 
 
 
 
28f21ff
cb99b35
28f21ff
 
 
 
 
 
cb99b35
28f21ff
 
cb99b35
28f21ff
 
cb99b35
28f21ff
cb99b35
 
 
 
 
 
 
 
28f21ff
 
7873f14
 
 
 
 
 
28f21ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb99b35
 
 
28f21ff
 
 
 
cb99b35
 
28f21ff
cb99b35
 
 
 
28f21ff
cb99b35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28f21ff
cb99b35
28f21ff
cb99b35
 
 
 
 
 
28f21ff
cb99b35
 
 
 
 
 
 
 
 
28f21ff
cb99b35
28f21ff
cb99b35
 
28f21ff
cb99b35
 
 
 
 
 
 
28f21ff
cb99b35
 
 
 
 
 
 
 
 
28f21ff
 
cb99b35
28f21ff
 
 
 
 
 
cb99b35
28f21ff
cb99b35
 
 
 
28f21ff
cb99b35
 
 
 
28f21ff
cb99b35
 
28f21ff
cb99b35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28f21ff
cb99b35
 
28f21ff
 
 
 
 
 
 
cb99b35
28f21ff
 
 
 
 
 
 
cb99b35
28f21ff
 
cb99b35
28f21ff
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
from typing import List

import chess

# import tiktoken
import tokenizers
from tokenizers import models, pre_tokenizers, processors
from torch import Tensor as TT
from transformers import PreTrainedTokenizerFast
from transformers.tokenization_utils_fast import BatchEncoding

# def getTiktokenizer() -> tiktoken.Encoding:
#     """
#     Defines a tiktoken-based BPE encoder for UCI chess moves. This
#     tokenizer effectively tokenizes UCI moves by the square names.
#     One notable variation is that promotions must be in upper-case.

#     Vocabulary:
#     Special Tokens (4): "\<|pad|\>", "\<|startoftext|\>", "\<|endoftext|\>", "\<|unknown|\>"
#     Square Tokens (64): a1 through h8
#     Promote Tokens (4): Q, B, R, N
#     UNUSED (8120): Need 8192-4-64-4=8120 unused tokens of the form <|unused####|>
#     """
#     special_tokens = ["<|pad|>", "<|startoftext|>", "<|endoftext|>", "<|unknown|>"]
#     unused_tokens = [f"<|unused{i:04d}" for i in range(8120)]
#     chess_vocab = special_tokens + chess.SQUARE_NAMES + list("QBRN") + unused_tokens
#     mergeable_ranks = {k.encode():v for (v,k) in enumerate(chess_vocab)}
#     chess_pat_str = r'[a-h][1-8]|[QBRN]'

#     enc = tiktoken.Encoding(
#         name="chess_enc",
#         pat_str=chess_pat_str, # or \d|\s
#         mergeable_ranks=mergeable_ranks,
#         special_tokens={k:v for (v,k) in enumerate(special_tokens)},
#     )

#     return enc


class UciTokenizer(PreTrainedTokenizerFast):
    _PAD_TOKEN: str
    _UNK_TOKEN: str
    _EOS_TOKEN: str
    _BOS_TOKEN: str

    stoi: dict[str, int]
    """Integer to String mapping"""

    itos: dict[int, str]
    """String to Integer Mapping. This is the vocab"""

    def __init__(
        self,
        stoi,
        itos,
        pad_token,
        unk_token,
        bos_token,
        eos_token,
        name_or_path,
        **kwargs,
    ):
        self.stoi = stoi
        self.itos = itos

        self._PAD_TOKEN = pad_token
        self._UNK_TOKEN = unk_token
        self._EOS_TOKEN = eos_token
        self._BOS_TOKEN = bos_token

        # Define the model
        tok_model = models.WordLevel(vocab=self.stoi, unk_token=self._UNK_TOKEN)

        slow_tokenizer = tokenizers.Tokenizer(tok_model)
        slow_tokenizer.pre_tokenizer = self._init_pretokenizer()

        # post processing adds special tokens unless explicitly ignored
        post_proc = processors.TemplateProcessing(
            single=f"{bos_token} $0",
            pair=None,
            special_tokens=[(bos_token, 1)],
        )
        slow_tokenizer.post_processor = post_proc

        super().__init__(
            tokenizer_object=slow_tokenizer,
            unk_token=self._UNK_TOKEN,
            bos_token=self._BOS_TOKEN,
            eos_token=self._EOS_TOKEN,
            pad_token=self._PAD_TOKEN,
            name_or_path=name_or_path,
            **kwargs,
        )

        # Override the decode behavior to ensure spaces are correctly handled
        def _decode(
            token_ids: int | List[int] | dict | TT,
            skip_special_tokens=False,
            clean_up_tokenization_spaces=False,
        ) -> int | List[int]:

            if isinstance(token_ids, int):
                return self.itos.get(token_ids, self._UNK_TOKEN)

            if isinstance(token_ids, dict):
                token_ids = token_ids["input_ids"]

            if isinstance(token_ids, TT):
                token_ids = token_ids.tolist()

            if isinstance(token_ids, list):
                tokens_str = [self.itos.get(xi, self._UNK_TOKEN) for xi in token_ids]
                processed_tokens = self._process_str_tokens(tokens_str)

                return " ".join(processed_tokens)

            raise ValueError(
                f"Unknown input type to decode() for argument 'token_ids'. Received: {type(token_ids)} "
            )

        self._decode = _decode

    def _init_pretokenizer(self) -> pre_tokenizers.PreTokenizer:
        raise NotImplementedError

    def _process_str_tokens(
        self, tokens_str: list[str], return_player_ids: bool
    ) -> list[str]:
        raise NotImplementedError

    def get_id2square_list() -> list[int]:
        raise NotImplementedError


class UciTileTokenizer(UciTokenizer):
    """Uci tokenizer converting start/end tiles and promotion types each into individual tokens"""

    SPECIAL_TOKENS = (_PAD_TOKEN, _UNK_TOKEN, _BOS_TOKEN, _EOS_TOKEN) = [
        "<|pad|>",
        "<|startoftext|>",
        "<|endoftext|>",
        "<|unknown|>",
    ]

    stoi: dict[str, int]
    itos: dict[int, str]

    _split_regex: str
    _promote_chars: str

    id2square: List[int] = list(range(4, 68))
    """
    List mapping token IDs to squares on the chess board. Order is file then rank, i.e.: 
    `A1, B1, C1, ..., F8, G8, H8`    
    """

    def get_id2square_list(self) -> List[int]:
        return self.id2square

    def __init__(self, *, upper_promotions: bool, **kwargs):
        # Remove conflicting arguments from kwargs if they exist
        kwargs.pop("pad_token", None)
        kwargs.pop("unk_token", None)
        kwargs.pop("bos_token", None)
        kwargs.pop("eos_token", None)
        kwargs.pop("clean_up_tokenization_spaces", None)
        kwargs.pop("name_or_path", None)

        self.upper_promotions = upper_promotions

        if upper_promotions:
            self._promote_chars = "QRBN"
            self._split_regex = r"[a-h][1-8]|[QRBN]"
        else:
            self._promote_chars = "qrbn"
            self._split_regex = r"[a-h][1-8]|[qrnb]"

        self.stoi = {
            tok: idx
            for tok, idx in list(
                zip(
                    self.SPECIAL_TOKENS
                    + chess.SQUARE_NAMES
                    + list(self._promote_chars),
                    range(72),
                )
            )
        }

        self.itos = {
            idx: tok
            for tok, idx in list(
                zip(
                    self.SPECIAL_TOKENS
                    + chess.SQUARE_NAMES
                    + list(self._promote_chars),
                    range(72),
                )
            )
        }

        super().__init__(
            self.stoi,
            self.itos,
            pad_token=self._PAD_TOKEN,
            unk_token=self._UNK_TOKEN,
            bos_token=self._BOS_TOKEN,
            eos_token=self._EOS_TOKEN,
            name_or_path="austindavis/uci_tile_tokenizer",
            clean_up_tokenization_spaces=False,
            **kwargs,
        )

    def _init_pretokenizer(self):
        # Pre-tokenizer to split input into UCI moves
        pattern = tokenizers.Regex(self._split_regex)
        pre_tokenizer = pre_tokenizers.Sequence(
            [
                pre_tokenizers.Whitespace(),
                pre_tokenizers.Split(pattern=pattern, behavior="merged_with_previous"),
            ]
        )
        return pre_tokenizer

    def _process_str_tokens(self, token_str: list[str]):
        moves = []
        next_move = ""
        for token in token_str:

            # skip special tokens
            if token in self.all_special_tokens:
                continue

            # handle promotions
            if len(token) == 1:
                next_move += token
                continue

            # handle regular tokens if there's room
            if len(next_move) < 4:
                next_move += token
                continue

            moves.append(next_move)
            next_move = token

        moves.append(next_move)
        return moves

    @staticmethod
    def compute_players(encoding: BatchEncoding, according_to="output"):
        """
        Determines which player (white=True, black=False) is associated with each token in the sequence.
        This method works based on chess move sequences tokenized using the UciTileTokenizer.

        # Parameters:
        ----------
        **`encoding`** : BatchEncoding
            Tokenized input of a chess game, where each token represents a move or special token.

        **`according_to`** : str (optional, default='output')
            Specifies the perspective for associating players:
            - 'output': Returns the player whose next move is predicted by the sequence (the output move).
            - Otherwise: Returns the player associated with the input tokens (i.e., which player made each move).

        # Returns:
        -------
        List[bool]
            A list of boolean values indicating the player for each token:
            - True for white (player 1),
            - False for black (player 2).

            The list length corresponds to the number of tokens in the sequence, including special tokens if any.

        # Example Usage:
        ```
        >>> tok = UciTileTokenizer()
        >>> encoding = tok('e2e4 d7d5 e4d5 e7e6 d5e6 d8g5 e6e7 g5f6 e7f8Q')
        >>> print(encoding['input_ids'])
        [1, 16, 32, 55, 39, 32, 39, 56, 48, 39, 48, 63, 42, 48, 56, 42, 49, 56, 65, 68]
        >>> tok.compute_players(encoding)
        [True, True, False, False, True, True, False, False, True, True, False, False, True, True, False, False, True, True, True, False]
        >>> tok.compute_players(encoding, according_to='input')
        [True, True, True, False, False, True, True, False, False, True, True, False, False, True, True, False, False, True, True, True]
        ```

        # Notes:
        -------
        This method does not rely on board position calculations. Therefore, when
        using `according_to='output'`, it cannot reliably predict which player is
        responsible for selecting the final token of the sequence. For instance,
        if a pawn is moved to the back rank (e.g., 'e7e8'), then white must select
        the promotion class on the next token; however, this algorithm will predict
        that black is responsible for selecting the next token instead of white.
        """

        return [
            UciTileTokenizer._compute_players_single(encoding[i].ids)
            for i in range(len(encoding["input_ids"]))
        ]

    @staticmethod
    def _compute_players_single(input_ids: list[int], according_to: str = "output"):
        players = [] if according_to == "output" else [True]
        current_player = False
        num_tokens_in_ply = 0
        has_specials = False

        for i, token_id in enumerate(input_ids):
            if token_id == 1:
                has_specials = True
                continue

            if num_tokens_in_ply == 0:
                # check if promotion OR unknown token ID
                if token_id > 67 or token_id == 3:
                    players.append(current_player)
                    num_tokens_in_ply = 0
                else:
                    num_tokens_in_ply += 1
                    current_player = not current_player
                    players.append(current_player)
            elif num_tokens_in_ply == 1:
                num_tokens_in_ply = 0
                players.append(current_player)
            else:
                raise ValueError("Illegal move sequence")

        if according_to == "output":
            # anticipate what output should be based on the final input token
            # see notes for more detail
            if num_tokens_in_ply == 0:
                if token_id > 67:
                    players.append(not current_player)
                else:
                    players.append(current_player)
            else:
                players.append(current_player)

        return players if has_specials else players[1:]


if __name__ == "__main__":
    tok = UciTileTokenizer()
    encoding = tok("e2e4Q b7b8N e2e7 a1", add_special_tokens=True)
    print(
        f"{encoding['input_ids']=}\n{tok.compute_players(encoding,  according_to='output')=}"
    )
    print(
        f"{encoding['input_ids']=}\n{tok.compute_players(encoding, according_to='input')=}"
    )

    encoding = tok("e2e4Q b7b8N e2e7 a1", add_special_tokens=False)
    print(
        f"{encoding['input_ids']=}\n{tok.compute_players(encoding,  according_to='output')=}"
    )
    print(
        f"{encoding['input_ids']=}\n{tok.compute_players(encoding, according_to='input')=}"
    )

    encoding = tok("e2e4 d7d5 e4d5 e7e6 d5e6 d8g5 e6e7 g5f6 e7f8Q")
    print(encoding["input_ids"])
    print(tok.compute_players(encoding))
    print(tok.compute_players(encoding, according_to="input"))