ilanou20 commited on
Commit
7ec364f
·
verified ·
1 Parent(s): 9e780af

Chess Challenge submission by ilanou20

Browse files
README.md ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ tags:
4
+ - chess
5
+ - llm-course
6
+ - chess-challenge
7
+ license: mit
8
+ ---
9
+
10
+ # chess-ilan-v6
11
+
12
+ Chess model submitted to the LLM Course Chess Challenge.
13
+
14
+ - Submitted by: ilanou20
15
+ - Parameters: 798,592
config.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "ChessForCausalLM"
4
+ ],
5
+ "bos_token_id": 22,
6
+ "dropout": 0.0,
7
+ "dtype": "float32",
8
+ "eos_token_id": 23,
9
+ "hidden_size": 128,
10
+ "is_decoder": true,
11
+ "model_type": "chess_transformer",
12
+ "n_ctx": 256,
13
+ "n_embd": 128,
14
+ "n_head": 4,
15
+ "n_inner": 341,
16
+ "n_layer": 4,
17
+ "num_attention_heads": 4,
18
+ "num_hidden_layers": 4,
19
+ "pad_token_id": 24,
20
+ "rms_norm_eps": 1e-06,
21
+ "tie_weights": true,
22
+ "transformers_version": "4.57.3",
23
+ "vocab_size": 90
24
+ }
generation_config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 22,
4
+ "eos_token_id": [
5
+ 23
6
+ ],
7
+ "pad_token_id": 24,
8
+ "transformers_version": "4.57.3"
9
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5aa32d0f1681550d5efd389caf1e346f5f34dd20b63d53f6e9183ca15d58f75d
3
+ size 3198168
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,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+ import json
3
+ import os
4
+ import re
5
+ from typing import Dict, List, Optional
6
+ from transformers import PreTrainedTokenizer, AutoTokenizer
7
+
8
+ class ChessTokenizer(PreTrainedTokenizer):
9
+ vocab_files_names = {"vocab_file": "vocab.json"}
10
+ model_input_names = ["input_ids", "attention_mask"]
11
+
12
+ PIECES = ["WP", "WN", "WB", "WR", "WQ", "WK", "BP", "BN", "BB", "BR", "BQ", "BK"]
13
+ SQUARES = [f"{c}{r}" for c in "abcdefgh" for r in "12345678"]
14
+ SUFFIXES = ["(-)", "(x)", "(+)", "(#)", "(x+)", "(x#)", "(O)", "(o)", "(Q)", "=Q"]
15
+
16
+ PAD_TOKEN = "[PAD]"
17
+ BOS_TOKEN = "[BOS]"
18
+ EOS_TOKEN = "[EOS]"
19
+ UNK_TOKEN = "[UNK]"
20
+
21
+
22
+ def __init__(self, vocab_file: Optional[str] = None, vocab: Optional[Dict[str, int]] = None, **kwargs):
23
+ # 1. Build or Load Vocab
24
+ self._vocab = vocab
25
+ if vocab_file and os.path.exists(vocab_file):
26
+ with open(vocab_file, "r", encoding="utf-8") as f:
27
+ self._vocab = json.load(f)
28
+
29
+ if not self._vocab:
30
+ self._vocab = self._build_split_vocab()
31
+
32
+ self._ids_to_tokens = {v: k for k, v in self._vocab.items()}
33
+
34
+ pad_token = kwargs.pop("pad_token", self.PAD_TOKEN)
35
+ bos_token = kwargs.pop("bos_token", self.BOS_TOKEN)
36
+ eos_token = kwargs.pop("eos_token", self.EOS_TOKEN)
37
+ unk_token = kwargs.pop("unk_token", self.UNK_TOKEN)
38
+
39
+ super().__init__(
40
+ pad_token=pad_token,
41
+ bos_token=bos_token,
42
+ eos_token=eos_token,
43
+ unk_token=unk_token,
44
+ **kwargs,
45
+ )
46
+
47
+ def _build_split_vocab(self):
48
+ tokens = [self.PAD_TOKEN, self.BOS_TOKEN, self.EOS_TOKEN, self.UNK_TOKEN]
49
+ tokens += self.PIECES + self.SQUARES + self.SUFFIXES
50
+ unique_tokens = sorted(list(set(tokens)))
51
+ return {t: i for i, t in enumerate(unique_tokens)}
52
+
53
+ def get_vocab(self) -> Dict[str, int]:
54
+ return dict(self._vocab)
55
+
56
+ @property
57
+ def vocab_size(self) -> int:
58
+ return len(self._vocab)
59
+
60
+ def _tokenize(self, text: str) -> List[str]:
61
+ moves = text.strip().split()
62
+ tokens = []
63
+
64
+
65
+ pattern = re.compile(r"([WB][PNBRQK])([a-h][1-8])([a-h][1-8])(.*)")
66
+
67
+ for move in moves:
68
+ match = pattern.match(move)
69
+ if match:
70
+ p, s, t, suf = match.groups()
71
+ tokens.extend([p, s, t])
72
+ tokens.append(self._normalize_suffix(suf))
73
+ else:
74
+ tokens.extend(["WP", "a1", "a1", "(-)"])
75
+
76
+ return tokens
77
+
78
+ def _normalize_suffix(self, suf: str) -> str:
79
+ suf = suf.strip()
80
+ if not suf:
81
+ return "(-)"
82
+ if suf.startswith("x"):
83
+ if "+" in suf: return "(x+)"
84
+ if "#" in suf: return "(x#)"
85
+ return "(x)"
86
+ if suf == "+": return "(+)"
87
+ if suf == "#": return "(#)"
88
+ if suf in {"O", "o"}: return f"({suf})"
89
+ if suf in {"Q", "=Q"}: return "=Q"
90
+ return "(-)"
91
+
92
+
93
+ def _convert_token_to_id(self, token: str) -> int:
94
+ return self._vocab.get(token, self._vocab.get(self.UNK_TOKEN))
95
+
96
+ def _convert_id_to_token(self, index: int) -> str:
97
+ return self._ids_to_tokens.get(index, self.UNK_TOKEN)
98
+
99
+ def convert_tokens_to_string(self, tokens: List[str]) -> str:
100
+ out = []
101
+ specials = {self.PAD_TOKEN, self.BOS_TOKEN, self.EOS_TOKEN, self.UNK_TOKEN}
102
+ clean = [t for t in tokens if t not in specials]
103
+
104
+ current_move = ""
105
+ for i, t in enumerate(clean):
106
+ if t == "(-)":
107
+ pass
108
+ else:
109
+ current_move += t
110
+
111
+ if (i + 1) % 4 == 0:
112
+ out.append(current_move)
113
+ current_move = ""
114
+
115
+ if current_move: out.append(current_move)
116
+ return " ".join(out)
117
+
118
+ def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple:
119
+ path = os.path.join(save_directory, (filename_prefix + "-" if filename_prefix else "") + "vocab.json")
120
+ with open(path, "w") as f:
121
+ json.dump(self._vocab, f)
122
+ return (path,)
123
+
124
+ @classmethod
125
+ def build_vocab_from_dataset(cls, *args, **kwargs):
126
+ print("Using static 4-Step Split vocabulary.")
127
+ return cls()
128
+
129
+ # Register
130
+ AutoTokenizer.register("ChessTokenizer", ChessTokenizer)
tokenizer_config.json ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "22": {
4
+ "content": "[BOS]",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "23": {
12
+ "content": "[EOS]",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "24": {
20
+ "content": "[PAD]",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "25": {
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 @@
 
 
1
+ {"(#)": 0, "(+)": 1, "(-)": 2, "(O)": 3, "(Q)": 4, "(o)": 5, "(x#)": 6, "(x)": 7, "(x+)": 8, "=Q": 9, "BB": 10, "BK": 11, "BN": 12, "BP": 13, "BQ": 14, "BR": 15, "WB": 16, "WK": 17, "WN": 18, "WP": 19, "WQ": 20, "WR": 21, "[BOS]": 22, "[EOS]": 23, "[PAD]": 24, "[UNK]": 25, "a1": 26, "a2": 27, "a3": 28, "a4": 29, "a5": 30, "a6": 31, "a7": 32, "a8": 33, "b1": 34, "b2": 35, "b3": 36, "b4": 37, "b5": 38, "b6": 39, "b7": 40, "b8": 41, "c1": 42, "c2": 43, "c3": 44, "c4": 45, "c5": 46, "c6": 47, "c7": 48, "c8": 49, "d1": 50, "d2": 51, "d3": 52, "d4": 53, "d5": 54, "d6": 55, "d7": 56, "d8": 57, "e1": 58, "e2": 59, "e3": 60, "e4": 61, "e5": 62, "e6": 63, "e7": 64, "e8": 65, "f1": 66, "f2": 67, "f3": 68, "f4": 69, "f5": 70, "f6": 71, "f7": 72, "f8": 73, "g1": 74, "g2": 75, "g3": 76, "g4": 77, "g5": 78, "g6": 79, "g7": 80, "g8": 81, "h1": 82, "h2": 83, "h3": 84, "h4": 85, "h5": 86, "h6": 87, "h7": 88, "h8": 89}