corentincaris commited on
Commit
c807cd8
·
verified ·
1 Parent(s): d572b47

Chess Challenge submission by corentincaris

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 +143 -0
  6. tokenizer_config.json +44 -0
  7. vocab.json +74 -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-CC-try6
11
+
12
+ Chess model submitted to the LLM Course Chess Challenge.
13
+
14
+ ## Submission Info
15
+
16
+ - **Submitted by**: [corentincaris](https://huggingface.co/corentincaris)
17
+ - **Parameters**: 977,840
18
+ - **Organization**: LLM-course
19
+
20
+ ## Model Details
21
+
22
+ - **Architecture**: Chess Transformer (GPT-style)
23
+ - **Vocab size**: 72
24
+ - **Embedding dim**: 136
25
+ - **Layers**: 5
26
+ - **Heads**: 8
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": 136,
13
+ "n_head": 8,
14
+ "n_inner": 408,
15
+ "n_layer": 5,
16
+ "pad_token_id": 0,
17
+ "tie_weights": true,
18
+ "transformers_version": "4.57.5",
19
+ "vocab_size": 72
20
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:214a6b9057bfd6040b6015301ed21675ebdbb366fa139694dda772f7938066fa
3
+ size 3916792
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,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Custom Chess Tokenizer for the Chess Challenge.
3
+
4
+ This tokenizer treats each move as a single token using the extended UCI notation
5
+ from the Lichess dataset (e.g., WPe2e4, BNg8f6).
6
+
7
+ The dataset format uses:
8
+ - W/B prefix for White/Black
9
+ - Piece letter: P=Pawn, N=Knight, B=Bishop, R=Rook, Q=Queen, K=King
10
+ - Source and destination squares (e.g., e2e4)
11
+ - Special suffixes: (x)=capture, (+)=check, (+*)=checkmate, (o)/(O)=castling
12
+ - Promotion: (Q)=queen, (R)=rook, (B)=bishop, (N)=knight
13
+
14
+ New token strategy:
15
+ - we only retain the squares involved in the move and the promotion piece if any
16
+ - everything else (piece type, capture flag, check flag, etc.) is discarded
17
+ - the vocabulary size is thus minimal (72 tokens): 64 squares + 4 promotion pieces + 4 special tokens
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import json
23
+ import os
24
+ from pathlib import Path
25
+ from typing import Dict, List, Optional
26
+ import re
27
+
28
+ from transformers import PreTrainedTokenizer
29
+
30
+
31
+ class ChessTokenizer(PreTrainedTokenizer):
32
+ model_input_names = ["input_ids", "attention_mask"]
33
+
34
+ PAD_TOKEN = "[PAD]"
35
+ BOS_TOKEN = "[BOS]"
36
+ EOS_TOKEN = "[EOS]"
37
+ UNK_TOKEN = "[UNK]"
38
+
39
+ def __init__(
40
+ self,
41
+ vocab_file: Optional[str] = None,
42
+ vocab: Optional[Dict[str, int]] = None,
43
+ **kwargs,
44
+ ):
45
+ self._pad_token = self.PAD_TOKEN
46
+ self._bos_token = self.BOS_TOKEN
47
+ self._eos_token = self.EOS_TOKEN
48
+ self._unk_token = self.UNK_TOKEN
49
+
50
+ kwargs.pop("pad_token", None)
51
+ kwargs.pop("bos_token", None)
52
+ kwargs.pop("eos_token", None)
53
+ kwargs.pop("unk_token", None)
54
+
55
+ # Updated regex to match uppercase promotion tokens
56
+ self.token_pattern = re.compile(r'[a-h][1-8]|[QRBN]')
57
+
58
+ if vocab is not None:
59
+ self._vocab = vocab
60
+ elif vocab_file is not None and os.path.exists(vocab_file):
61
+ with open(vocab_file, "r", encoding="utf-8") as f:
62
+ self._vocab = json.load(f)
63
+ else:
64
+ self._vocab = self._create_default_vocab()
65
+
66
+ self._ids_to_tokens = {v: k for k, v in self._vocab.items()}
67
+
68
+ super().__init__(
69
+ pad_token=self._pad_token,
70
+ bos_token=self._bos_token,
71
+ eos_token=self._eos_token,
72
+ unk_token=self._unk_token,
73
+ **kwargs,
74
+ )
75
+
76
+ def _create_default_vocab(self) -> Dict[str, int]:
77
+ special_tokens = [self.PAD_TOKEN, self.BOS_TOKEN, self.EOS_TOKEN, self.UNK_TOKEN]
78
+ vocab = {token: idx for idx, token in enumerate(special_tokens)}
79
+ idx = len(vocab)
80
+
81
+ for f in 'abcdefgh':
82
+ for r in '12345678':
83
+ vocab[f"{f}{r}"] = idx
84
+ idx += 1
85
+
86
+ for p in ['Q', 'R', 'B', 'N']:
87
+ vocab[p] = idx
88
+ idx += 1
89
+ return vocab
90
+
91
+ def _tokenize(self, text: str) -> List[str]:
92
+ text = (text.replace("(Q)", "Q")
93
+ .replace("(R)", "R")
94
+ .replace("(B)", "B")
95
+ .replace("(N)", "N"))
96
+
97
+ return self.token_pattern.findall(text)
98
+
99
+ def _convert_token_to_id(self, token: str) -> int:
100
+ return self._vocab.get(token, self._vocab.get(self.UNK_TOKEN, 0))
101
+
102
+ def _convert_id_to_token(self, index: int) -> str:
103
+ return self._ids_to_tokens.get(index, self.UNK_TOKEN)
104
+
105
+ def convert_tokens_to_string(self, tokens: List[str]) -> str:
106
+ special = {self.PAD_TOKEN, self.BOS_TOKEN, self.EOS_TOKEN, self.UNK_TOKEN}
107
+ clean_tokens = [t for t in tokens if t not in special]
108
+
109
+ output = []
110
+ for token in clean_tokens:
111
+ if token in ['Q', 'R', 'B', 'N'] and output:
112
+ output[-1] += token
113
+ elif output and len(output[-1]) == 2 and output[-1][0] in 'abcdefgh':
114
+ output[-1] += token
115
+ else:
116
+ output.append(token)
117
+
118
+ return " ".join(output)
119
+
120
+ def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple:
121
+ if not os.path.isdir(save_directory):
122
+ os.makedirs(save_directory, exist_ok=True)
123
+ vocab_file = os.path.join(
124
+ save_directory, (filename_prefix + "-" if filename_prefix else "") + "vocab.json"
125
+ )
126
+ with open(vocab_file, "w", encoding="utf-8") as f:
127
+ json.dump(self._vocab, f, ensure_ascii=False, indent=2)
128
+ return (vocab_file,)
129
+
130
+ @classmethod
131
+ def build_vocab_from_iterator(cls, iterator, min_frequency=1):
132
+ return cls()
133
+
134
+ @classmethod
135
+ def build_vocab_from_dataset(cls, **kwargs):
136
+ return cls()
137
+
138
+ @property
139
+ def vocab_size(self) -> int:
140
+ return len(self._vocab)
141
+
142
+ def get_vocab(self) -> Dict[str, int]:
143
+ 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,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "[PAD]": 0,
3
+ "[BOS]": 1,
4
+ "[EOS]": 2,
5
+ "[UNK]": 3,
6
+ "a1": 4,
7
+ "a2": 5,
8
+ "a3": 6,
9
+ "a4": 7,
10
+ "a5": 8,
11
+ "a6": 9,
12
+ "a7": 10,
13
+ "a8": 11,
14
+ "b1": 12,
15
+ "b2": 13,
16
+ "b3": 14,
17
+ "b4": 15,
18
+ "b5": 16,
19
+ "b6": 17,
20
+ "b7": 18,
21
+ "b8": 19,
22
+ "c1": 20,
23
+ "c2": 21,
24
+ "c3": 22,
25
+ "c4": 23,
26
+ "c5": 24,
27
+ "c6": 25,
28
+ "c7": 26,
29
+ "c8": 27,
30
+ "d1": 28,
31
+ "d2": 29,
32
+ "d3": 30,
33
+ "d4": 31,
34
+ "d5": 32,
35
+ "d6": 33,
36
+ "d7": 34,
37
+ "d8": 35,
38
+ "e1": 36,
39
+ "e2": 37,
40
+ "e3": 38,
41
+ "e4": 39,
42
+ "e5": 40,
43
+ "e6": 41,
44
+ "e7": 42,
45
+ "e8": 43,
46
+ "f1": 44,
47
+ "f2": 45,
48
+ "f3": 46,
49
+ "f4": 47,
50
+ "f5": 48,
51
+ "f6": 49,
52
+ "f7": 50,
53
+ "f8": 51,
54
+ "g1": 52,
55
+ "g2": 53,
56
+ "g3": 54,
57
+ "g4": 55,
58
+ "g5": 56,
59
+ "g6": 57,
60
+ "g7": 58,
61
+ "g8": 59,
62
+ "h1": 60,
63
+ "h2": 61,
64
+ "h3": 62,
65
+ "h4": 63,
66
+ "h5": 64,
67
+ "h6": 65,
68
+ "h7": 66,
69
+ "h8": 67,
70
+ "Q": 68,
71
+ "R": 69,
72
+ "B": 70,
73
+ "N": 71
74
+ }