Sammy972 commited on
Commit
a846d1f
·
verified ·
1 Parent(s): b8d64cd

Chess Challenge submission by Sammy972

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 +256 -0
  6. tokenizer_config.json +50 -0
  7. vocab.json +87 -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-sam-model-so
11
+
12
+ Chess model submitted to the LLM Course Chess Challenge.
13
+
14
+ ## Submission Info
15
+
16
+ - **Submitted by**: [Sammy972](https://huggingface.co/Sammy972)
17
+ - **Parameters**: 1,004,572
18
+ - **Organization**: LLM-course
19
+
20
+ ## Model Details
21
+
22
+ - **Architecture**: Chess Transformer (GPT-style)
23
+ - **Vocab size**: 85
24
+ - **Embedding dim**: 100
25
+ - **Layers**: 8
26
+ - **Heads**: 5
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": 512,
12
+ "n_embd": 100,
13
+ "n_head": 5,
14
+ "n_inner": 384,
15
+ "n_layer": 8,
16
+ "pad_token_id": 0,
17
+ "tie_weights": true,
18
+ "transformers_version": "4.57.3",
19
+ "vocab_size": 85
20
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:45390890f615813c27392ca7fff5ae77b95a1a4da7883e9bed82ae60997c9432
3
+ size 4026776
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,256 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Factorized Chess Tokenizer for the Chess Challenge.
3
+
4
+ Instead of "1 move = 1 token", we represent a move as multiple tokens:
5
+ - Side: [W] / [B]
6
+ - Piece: [P], [N], [BISHOP], [R], [Q], [K]
7
+ - Squares: [e2], [e4], ...
8
+ - Optional suffix: [x], [+], [#], [O-O], [O-O-O]
9
+ - Optional promotion: [prom_Q], [prom_R], [prom_B], [prom_N]
10
+
11
+ Important:
12
+ - We KEEP squares as tokens so evaluation (regex [a-h][1-8]) can extract UCI moves.
13
+ - We decode squares to plain "e2" etc, and promotions to "q/r/b/n" so evaluate.py can detect promotions.
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ import json
19
+ import os
20
+ import re
21
+ from typing import Dict, List, Optional
22
+
23
+ from transformers import PreTrainedTokenizer
24
+
25
+
26
+ MOVE_RE = re.compile(
27
+ r"^(?P<side>[WB])"
28
+ r"(?P<piece>[PNBRQK])"
29
+ r"(?P<src>[a-h][1-8])"
30
+ r"(?P<dst>[a-h][1-8])"
31
+ r"(?P<suffix>.*)$"
32
+ )
33
+
34
+ SQUARE_TOKEN_RE = re.compile(r"^\[[a-h][1-8]\]$")
35
+
36
+
37
+
38
+ class ChessTokenizer(PreTrainedTokenizer):
39
+ model_input_names = ["input_ids", "attention_mask"]
40
+ vocab_files_names = {"vocab_file": "vocab.json"}
41
+
42
+ PAD_TOKEN = "[PAD]"
43
+ BOS_TOKEN = "[BOS]"
44
+ EOS_TOKEN = "[EOS]"
45
+ UNK_TOKEN = "[UNK]"
46
+
47
+ def __init__(
48
+ self,
49
+ vocab_file: Optional[str] = None,
50
+ vocab: Optional[Dict[str, int]] = None,
51
+ **kwargs,
52
+ ):
53
+ # Special tokens
54
+ self._pad_token = self.PAD_TOKEN
55
+ self._bos_token = self.BOS_TOKEN
56
+ self._eos_token = self.EOS_TOKEN
57
+ self._unk_token = self.UNK_TOKEN
58
+
59
+ # Avoid duplicate kwargs
60
+ kwargs.pop("pad_token", None)
61
+ kwargs.pop("bos_token", None)
62
+ kwargs.pop("eos_token", None)
63
+ kwargs.pop("unk_token", None)
64
+
65
+ if vocab is not None:
66
+ self._vocab = vocab
67
+ elif vocab_file is not None and os.path.exists(vocab_file):
68
+ with open(vocab_file, "r", encoding="utf-8") as f:
69
+ self._vocab = json.load(f)
70
+ else:
71
+ self._vocab = self._create_default_vocab()
72
+
73
+ self._ids_to_tokens = {v: k for k, v in self._vocab.items()}
74
+
75
+ super().__init__(
76
+ pad_token=self._pad_token,
77
+ bos_token=self._bos_token,
78
+ eos_token=self._eos_token,
79
+ unk_token=self._unk_token,
80
+ **kwargs,
81
+ )
82
+
83
+ def _create_default_vocab(self) -> Dict[str, int]:
84
+ special_tokens = [self.PAD_TOKEN, self.BOS_TOKEN, self.EOS_TOKEN, self.UNK_TOKEN]
85
+
86
+ side_tokens = ["[W]", "[B]"]
87
+ piece_tokens = ["[P]", "[N]", "[BISHOP]", "[R]", "[Q]", "[K]"]
88
+
89
+ square_tokens = [f"[{file}{rank}]" for rank in "12345678" for file in "abcdefgh"]
90
+
91
+ suffix_tokens = [
92
+ "[x]", "[+]", "[#]",
93
+ "[O-O]", "[O-O-O]",
94
+ "[prom_Q]", "[prom_R]", "[prom_B]", "[prom_N]",
95
+ ]
96
+
97
+ vocab_list = special_tokens + side_tokens + piece_tokens + square_tokens + suffix_tokens
98
+ return {tok: i for i, tok in enumerate(vocab_list)}
99
+
100
+ # IMPORTANT: prevent HF from auto-adding BOS/EOS on top of your text
101
+ def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
102
+ if token_ids_1 is None:
103
+ return token_ids_0
104
+ return token_ids_0 + token_ids_1
105
+
106
+ @classmethod
107
+ def build_vocab_from_iterator(cls, iterator, min_frequency: int = 1) -> "ChessTokenizer":
108
+ # Fixed vocab (we ignore dataset frequency)
109
+ return cls()
110
+
111
+ @classmethod
112
+ def build_vocab_from_dataset(
113
+ cls,
114
+ dataset_name: str = "dlouapre/lichess_2025-01_1M",
115
+ split: str = "train",
116
+ column: str = "text",
117
+ min_frequency: int = 500,
118
+ max_samples: Optional[int] = 100000,
119
+ ) -> "ChessTokenizer":
120
+ # Fixed vocab (we ignore dataset frequency)
121
+ return cls()
122
+
123
+ @property
124
+ def vocab_size(self) -> int:
125
+ return len(self._vocab)
126
+
127
+ def get_vocab(self) -> Dict[str, int]:
128
+ return dict(self._vocab)
129
+
130
+ def _tokenize(self, text: str) -> List[str]:
131
+ tokens: List[str] = []
132
+ parts = str(text).strip().split()
133
+
134
+ specials = {self.PAD_TOKEN, self.BOS_TOKEN, self.EOS_TOKEN, self.UNK_TOKEN}
135
+
136
+ for p in parts:
137
+ if p in specials:
138
+ tokens.append(p)
139
+ continue
140
+
141
+ m = MOVE_RE.match(p)
142
+ if not m:
143
+ tokens.append(self.UNK_TOKEN)
144
+ continue
145
+
146
+ side = m.group("side")
147
+ piece = m.group("piece")
148
+ src = m.group("src")
149
+ dst = m.group("dst")
150
+ suffix = m.group("suffix") or ""
151
+
152
+ tokens.append("[W]" if side == "W" else "[B]")
153
+
154
+ if piece == "B":
155
+ tokens.append("[BISHOP]")
156
+ else:
157
+ tokens.append(f"[{piece}]")
158
+
159
+ tokens.append(f"[{src}]")
160
+ tokens.append(f"[{dst}]")
161
+
162
+ # capture/check/checkmate
163
+ if "x" in suffix:
164
+ tokens.append("[x]")
165
+ if "*" in suffix:
166
+ tokens.append("[#]")
167
+ elif "+" in suffix:
168
+ tokens.append("[+]")
169
+
170
+ # castling annotation (optional, squares already encode it)
171
+ if piece == "K":
172
+ if (src, dst) in (("e1", "g1"), ("e8", "g8")) or "(o)" in suffix:
173
+ tokens.append("[O-O]")
174
+ elif (src, dst) in (("e1", "c1"), ("e8", "c8")) or "(O)" in suffix:
175
+ tokens.append("[O-O-O]")
176
+
177
+ # promotion
178
+ if "=" in suffix:
179
+ i = suffix.find("=")
180
+ if i != -1 and i + 1 < len(suffix):
181
+ promo = suffix[i + 1].upper()
182
+ if promo in ("Q", "R", "B", "N"):
183
+ tokens.append(f"[prom_{promo}]")
184
+
185
+ return tokens
186
+
187
+ def _convert_token_to_id(self, token: str) -> int:
188
+ return self._vocab.get(token, self._vocab.get(self.UNK_TOKEN, 3))
189
+
190
+ def _convert_id_to_token(self, index: int) -> str:
191
+ return self._ids_to_tokens.get(index, self.UNK_TOKEN)
192
+
193
+ def convert_tokens_to_string(self, tokens: List[str]) -> str:
194
+ """
195
+ Decode tokens into a compact string so evaluate.py can extract squares easily.
196
+
197
+ Examples:
198
+ [W] [P] [e2] [e4] -> "WPe2e4"
199
+ ... [e7] [e8] [prom_Q] -> "WPe7e8q" (promotion detectable)
200
+ """
201
+ out: List[str] = []
202
+ for t in tokens:
203
+ if t in (self.PAD_TOKEN,):
204
+ continue
205
+
206
+ # keep these literal so evaluator can compare EOS if needed
207
+ if t in (self.BOS_TOKEN, self.EOS_TOKEN, self.UNK_TOKEN):
208
+ out.append(t)
209
+ continue
210
+
211
+ if t == "[W]":
212
+ out.append("W")
213
+ elif t == "[B]":
214
+ out.append("B")
215
+ elif t == "[BISHOP]":
216
+ out.append("B")
217
+ elif t in ("[P]", "[N]", "[R]", "[Q]", "[K]"):
218
+ out.append(t.strip("[]"))
219
+ elif SQUARE_TOKEN_RE.match(t):
220
+ out.append(t[1:-1]) # "[e2]" -> "e2"
221
+ elif t == "[x]":
222
+ out.append("(x)")
223
+ elif t == "[+]":
224
+ out.append("(+)")
225
+ elif t == "[#]":
226
+ out.append("(+*)")
227
+ elif t == "[O-O]":
228
+ out.append("(o)")
229
+ elif t == "[O-O-O]":
230
+ out.append("(O)")
231
+ elif t == "[prom_Q]":
232
+ out.append("q")
233
+ elif t == "[prom_R]":
234
+ out.append("r")
235
+ elif t == "[prom_B]":
236
+ out.append("b")
237
+ elif t == "[prom_N]":
238
+ out.append("n")
239
+ else:
240
+ out.append(t)
241
+
242
+ return "".join(out)
243
+
244
+ def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple:
245
+ if not os.path.isdir(save_directory):
246
+ os.makedirs(save_directory, exist_ok=True)
247
+
248
+ vocab_file = os.path.join(
249
+ save_directory,
250
+ (filename_prefix + "-" if filename_prefix else "") + "vocab.json",
251
+ )
252
+
253
+ with open(vocab_file, "w", encoding="utf-8") as f:
254
+ json.dump(self._vocab, f, ensure_ascii=False, indent=2)
255
+
256
+ return (vocab_file,)
tokenizer_config.json ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "auto_map": {
37
+ "AutoTokenizer": [
38
+ "tokenizer.ChessTokenizer",
39
+ null
40
+ ]
41
+ },
42
+ "bos_token": "[BOS]",
43
+ "clean_up_tokenization_spaces": false,
44
+ "eos_token": "[EOS]",
45
+ "extra_special_tokens": {},
46
+ "model_max_length": 1000000000000000019884624838656,
47
+ "pad_token": "[PAD]",
48
+ "tokenizer_class": "ChessTokenizer",
49
+ "unk_token": "[UNK]"
50
+ }
vocab.json ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "[BISHOP]": 8,
11
+ "[R]": 9,
12
+ "[Q]": 10,
13
+ "[K]": 11,
14
+ "[a1]": 12,
15
+ "[b1]": 13,
16
+ "[c1]": 14,
17
+ "[d1]": 15,
18
+ "[e1]": 16,
19
+ "[f1]": 17,
20
+ "[g1]": 18,
21
+ "[h1]": 19,
22
+ "[a2]": 20,
23
+ "[b2]": 21,
24
+ "[c2]": 22,
25
+ "[d2]": 23,
26
+ "[e2]": 24,
27
+ "[f2]": 25,
28
+ "[g2]": 26,
29
+ "[h2]": 27,
30
+ "[a3]": 28,
31
+ "[b3]": 29,
32
+ "[c3]": 30,
33
+ "[d3]": 31,
34
+ "[e3]": 32,
35
+ "[f3]": 33,
36
+ "[g3]": 34,
37
+ "[h3]": 35,
38
+ "[a4]": 36,
39
+ "[b4]": 37,
40
+ "[c4]": 38,
41
+ "[d4]": 39,
42
+ "[e4]": 40,
43
+ "[f4]": 41,
44
+ "[g4]": 42,
45
+ "[h4]": 43,
46
+ "[a5]": 44,
47
+ "[b5]": 45,
48
+ "[c5]": 46,
49
+ "[d5]": 47,
50
+ "[e5]": 48,
51
+ "[f5]": 49,
52
+ "[g5]": 50,
53
+ "[h5]": 51,
54
+ "[a6]": 52,
55
+ "[b6]": 53,
56
+ "[c6]": 54,
57
+ "[d6]": 55,
58
+ "[e6]": 56,
59
+ "[f6]": 57,
60
+ "[g6]": 58,
61
+ "[h6]": 59,
62
+ "[a7]": 60,
63
+ "[b7]": 61,
64
+ "[c7]": 62,
65
+ "[d7]": 63,
66
+ "[e7]": 64,
67
+ "[f7]": 65,
68
+ "[g7]": 66,
69
+ "[h7]": 67,
70
+ "[a8]": 68,
71
+ "[b8]": 69,
72
+ "[c8]": 70,
73
+ "[d8]": 71,
74
+ "[e8]": 72,
75
+ "[f8]": 73,
76
+ "[g8]": 74,
77
+ "[h8]": 75,
78
+ "[x]": 76,
79
+ "[+]": 77,
80
+ "[#]": 78,
81
+ "[O-O]": 79,
82
+ "[O-O-O]": 80,
83
+ "[prom_Q]": 81,
84
+ "[prom_R]": 82,
85
+ "[prom_B]": 83,
86
+ "[prom_N]": 84
87
+ }