swdo commited on
Commit
65384d6
·
verified ·
1 Parent(s): ba2261f

Chess Challenge submission by swdo

Browse files
Files changed (7) hide show
  1. README.md +26 -0
  2. config.json +20 -0
  3. model.safetensors +3 -0
  4. special_tokens_map.json +6 -0
  5. tokenizer.py +199 -0
  6. tokenizer_config.json +44 -0
  7. vocab.json +90 -0
README.md ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ tags:
4
+ - chess
5
+ - llm-course
6
+ - chess-challenge
7
+ license: mit
8
+ ---
9
+
10
+ # chess_swdo_up
11
+
12
+ Chess model submitted to the LLM Course Chess Challenge.
13
+
14
+ ## Submission Info
15
+
16
+ - **Submitted by**: [swdo](https://huggingface.co/swdo)
17
+ - **Parameters**: 871,168
18
+ - **Organization**: LLM-course
19
+
20
+ ## Model Details
21
+
22
+ - **Architecture**: Chess Transformer (GPT-style)
23
+ - **Vocab size**: 88
24
+ - **Embedding dim**: 128
25
+ - **Layers**: 5
26
+ - **Heads**: 4
config.json ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "ChessForCausalLM"
4
+ ],
5
+ "bos_token_id": 1,
6
+ "dropout": 0.1,
7
+ "dtype": "float32",
8
+ "eos_token_id": 2,
9
+ "layer_norm_epsilon": 1e-05,
10
+ "model_type": "chess_transformer",
11
+ "n_ctx": 256,
12
+ "n_embd": 128,
13
+ "n_head": 4,
14
+ "n_inner": 384,
15
+ "n_layer": 5,
16
+ "pad_token_id": 0,
17
+ "tie_weights": true,
18
+ "transformers_version": "4.57.3",
19
+ "vocab_size": 88
20
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6c5a58fe6555f95d4157289e7462a9152909962f3c251f806f621e8bed568a42
3
+ size 3490096
special_tokens_map.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "[BOS]",
3
+ "eos_token": "[EOS]",
4
+ "pad_token": "[PAD]",
5
+ "unk_token": "[UNK]"
6
+ }
tokenizer.py ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ """
4
+ Custom Chess Tokenizer - Decomposed Move Representation.
5
+
6
+ Decomposes chess moves into components: Color, Piece, From square, To square, Modifiers.
7
+ Uses a fixed vocabulary of ~88 tokens.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import json
13
+ import os
14
+ import re
15
+ from typing import Dict, List, Optional
16
+
17
+ from transformers import PreTrainedTokenizer
18
+
19
+
20
+ class ChessTokenizer(PreTrainedTokenizer):
21
+ model_input_names = ["input_ids", "attention_mask"]
22
+
23
+ PAD_TOKEN = "[PAD]"
24
+ BOS_TOKEN = "[BOS]"
25
+ EOS_TOKEN = "[EOS]"
26
+ UNK_TOKEN = "[UNK]"
27
+
28
+ COLORS = ["[W]", "[B]"]
29
+ PIECES = ["P", "N", "B", "R", "Q", "K"]
30
+ SQUARES = [f + r for f in "abcdefgh" for r in "12345678"]
31
+ MODIFIERS = ["x", "+", "#", "+*", "=Q", "=R", "=B", "=N", "O-O", "O-O-O", "o", "O"]
32
+
33
+ MOVE_PATTERN = re.compile(
34
+ r'^([WB])([PNBRQK])([a-h][1-8])([a-h][1-8])(=[QRBN])?(\([xoO+*]+\))?$'
35
+ )
36
+
37
+ def __init__(
38
+ self,
39
+ vocab_file: Optional[str] = None,
40
+ vocab: Optional[Dict[str, int]] = None,
41
+ **kwargs,
42
+ ):
43
+ self._pad_token = self.PAD_TOKEN
44
+ self._bos_token = self.BOS_TOKEN
45
+ self._eos_token = self.EOS_TOKEN
46
+ self._unk_token = self.UNK_TOKEN
47
+
48
+ kwargs.pop("pad_token", None)
49
+ kwargs.pop("bos_token", None)
50
+ kwargs.pop("eos_token", None)
51
+ kwargs.pop("unk_token", None)
52
+
53
+ if vocab is not None:
54
+ self._vocab = vocab
55
+ elif vocab_file is not None and os.path.exists(vocab_file):
56
+ with open(vocab_file, "r", encoding="utf-8") as f:
57
+ self._vocab = json.load(f)
58
+ else:
59
+ self._vocab = self._create_default_vocab()
60
+
61
+ self._ids_to_tokens = {v: k for k, v in self._vocab.items()}
62
+
63
+ super().__init__(
64
+ pad_token=self._pad_token,
65
+ bos_token=self._bos_token,
66
+ eos_token=self._eos_token,
67
+ unk_token=self._unk_token,
68
+ **kwargs,
69
+ )
70
+
71
+ def _create_default_vocab(self) -> Dict[str, int]:
72
+ tokens = [self.PAD_TOKEN, self.BOS_TOKEN, self.EOS_TOKEN, self.UNK_TOKEN]
73
+ tokens.extend(self.COLORS)
74
+ tokens.extend(self.PIECES)
75
+ tokens.extend(self.SQUARES)
76
+ tokens.extend(self.MODIFIERS)
77
+ return {token: idx for idx, token in enumerate(tokens)}
78
+
79
+ def _parse_move(self, move: str) -> List[str]:
80
+ """Parse a single move into component tokens."""
81
+ match = self.MOVE_PATTERN.match(move)
82
+ if not match:
83
+ return [self.UNK_TOKEN]
84
+
85
+ tokens = [f"[{match.group(1)}]", match.group(2), match.group(3), match.group(4)]
86
+
87
+ if match.group(5):
88
+ tokens.append(match.group(5))
89
+
90
+ if match.group(6):
91
+ suffix_content = match.group(6)[1:-1]
92
+ if "x" in suffix_content:
93
+ tokens.append("x")
94
+ if "+*" in suffix_content:
95
+ tokens.append("+*")
96
+ elif "+" in suffix_content:
97
+ tokens.append("+")
98
+ if suffix_content == "o":
99
+ tokens.append("o")
100
+ elif suffix_content == "O":
101
+ tokens.append("O")
102
+
103
+ return tokens
104
+
105
+ def _tokenize(self, text: str) -> List[str]:
106
+ """Tokenize a string of moves into component tokens."""
107
+ tokens = []
108
+ for move in text.strip().split():
109
+ tokens.extend(self._parse_move(move))
110
+ return tokens
111
+
112
+ def _convert_token_to_id(self, token: str) -> int:
113
+ return self._vocab.get(token, self._vocab.get(self.UNK_TOKEN, 0))
114
+
115
+ def _convert_id_to_token(self, index: int) -> str:
116
+ return self._ids_to_tokens.get(index, self.UNK_TOKEN)
117
+
118
+ def convert_tokens_to_string(self, tokens: List[str]) -> str:
119
+ """Reconstruct moves from component tokens."""
120
+ special = {self.PAD_TOKEN, self.BOS_TOKEN, self.EOS_TOKEN, self.UNK_TOKEN}
121
+ result = []
122
+ current_move = []
123
+
124
+ for token in tokens:
125
+ if token in special:
126
+ if current_move:
127
+ result.append(self._reconstruct_move(current_move))
128
+ current_move = []
129
+ continue
130
+
131
+ current_move.append(token)
132
+ if self._is_complete_move(current_move):
133
+ result.append(self._reconstruct_move(current_move))
134
+ current_move = []
135
+
136
+ if current_move:
137
+ result.append(self._reconstruct_move(current_move))
138
+
139
+ return " ".join(result)
140
+
141
+ def _is_complete_move(self, tokens: List[str]) -> bool:
142
+ """Check if tokens form a complete move."""
143
+ if len(tokens) < 4:
144
+ return False
145
+
146
+ if (tokens[0] in self.COLORS and tokens[1] in self.PIECES and
147
+ tokens[2] in self.SQUARES and tokens[3] in self.SQUARES):
148
+ if len(tokens) == 4:
149
+ return True
150
+ for t in tokens[4:]:
151
+ if t in self.COLORS or (t not in self.MODIFIERS and not t.startswith("=")):
152
+ return True
153
+ return True
154
+ return False
155
+
156
+ def _reconstruct_move(self, tokens: List[str]) -> str:
157
+ """Reconstruct a move string from component tokens."""
158
+ if not tokens or len(tokens) < 4:
159
+ return "".join(tokens)
160
+
161
+ color = tokens[0][1] if tokens[0] in self.COLORS else tokens[0]
162
+ move = color + "".join(tokens[1:4])
163
+
164
+ suffixes = []
165
+ for t in tokens[4:]:
166
+ if t.startswith("="):
167
+ move += t
168
+ elif t in ["x", "+", "+*", "o", "O"]:
169
+ suffixes.append(t)
170
+
171
+ if suffixes:
172
+ move += "(" + "".join(suffixes) + ")"
173
+
174
+ return move
175
+
176
+ def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple:
177
+ if not os.path.isdir(save_directory):
178
+ os.makedirs(save_directory, exist_ok=True)
179
+ vocab_file = os.path.join(
180
+ save_directory, (filename_prefix + "-" if filename_prefix else "") + "vocab.json"
181
+ )
182
+ with open(vocab_file, "w", encoding="utf-8") as f:
183
+ json.dump(self._vocab, f, ensure_ascii=False, indent=2)
184
+ return (vocab_file,)
185
+
186
+ @classmethod
187
+ def build_vocab_from_iterator(cls, iterator, min_frequency=1):
188
+ return cls()
189
+
190
+ @classmethod
191
+ def build_vocab_from_dataset(cls, **kwargs):
192
+ return cls()
193
+
194
+ @property
195
+ def vocab_size(self) -> int:
196
+ return len(self._vocab)
197
+
198
+ def get_vocab(self) -> Dict[str, int]:
199
+ return dict(self._vocab)
tokenizer_config.json ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "[PAD]",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "1": {
12
+ "content": "[BOS]",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "2": {
20
+ "content": "[EOS]",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "3": {
28
+ "content": "[UNK]",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ }
35
+ },
36
+ "bos_token": "[BOS]",
37
+ "clean_up_tokenization_spaces": false,
38
+ "eos_token": "[EOS]",
39
+ "extra_special_tokens": {},
40
+ "model_max_length": 1000000000000000019884624838656,
41
+ "pad_token": "[PAD]",
42
+ "tokenizer_class": "ChessTokenizer",
43
+ "unk_token": "[UNK]"
44
+ }
vocab.json ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "[PAD]": 0,
3
+ "[BOS]": 1,
4
+ "[EOS]": 2,
5
+ "[UNK]": 3,
6
+ "[W]": 4,
7
+ "[B]": 5,
8
+ "P": 6,
9
+ "N": 7,
10
+ "B": 8,
11
+ "R": 9,
12
+ "Q": 10,
13
+ "K": 11,
14
+ "a1": 12,
15
+ "a2": 13,
16
+ "a3": 14,
17
+ "a4": 15,
18
+ "a5": 16,
19
+ "a6": 17,
20
+ "a7": 18,
21
+ "a8": 19,
22
+ "b1": 20,
23
+ "b2": 21,
24
+ "b3": 22,
25
+ "b4": 23,
26
+ "b5": 24,
27
+ "b6": 25,
28
+ "b7": 26,
29
+ "b8": 27,
30
+ "c1": 28,
31
+ "c2": 29,
32
+ "c3": 30,
33
+ "c4": 31,
34
+ "c5": 32,
35
+ "c6": 33,
36
+ "c7": 34,
37
+ "c8": 35,
38
+ "d1": 36,
39
+ "d2": 37,
40
+ "d3": 38,
41
+ "d4": 39,
42
+ "d5": 40,
43
+ "d6": 41,
44
+ "d7": 42,
45
+ "d8": 43,
46
+ "e1": 44,
47
+ "e2": 45,
48
+ "e3": 46,
49
+ "e4": 47,
50
+ "e5": 48,
51
+ "e6": 49,
52
+ "e7": 50,
53
+ "e8": 51,
54
+ "f1": 52,
55
+ "f2": 53,
56
+ "f3": 54,
57
+ "f4": 55,
58
+ "f5": 56,
59
+ "f6": 57,
60
+ "f7": 58,
61
+ "f8": 59,
62
+ "g1": 60,
63
+ "g2": 61,
64
+ "g3": 62,
65
+ "g4": 63,
66
+ "g5": 64,
67
+ "g6": 65,
68
+ "g7": 66,
69
+ "g8": 67,
70
+ "h1": 68,
71
+ "h2": 69,
72
+ "h3": 70,
73
+ "h4": 71,
74
+ "h5": 72,
75
+ "h6": 73,
76
+ "h7": 74,
77
+ "h8": 75,
78
+ "x": 76,
79
+ "+": 77,
80
+ "#": 78,
81
+ "+*": 79,
82
+ "=Q": 80,
83
+ "=R": 81,
84
+ "=B": 82,
85
+ "=N": 83,
86
+ "O-O": 84,
87
+ "O-O-O": 85,
88
+ "o": 86,
89
+ "O": 87
90
+ }