Chess Challenge submission by gabriel-mariadass
Browse files- README.md +2 -2
- config.json +2 -1
- model.safetensors +3 -0
- tokenizer.py +210 -105
- vocab.json +104 -140
README.md
CHANGED
|
@@ -14,13 +14,13 @@ Chess model submitted to the LLM Course Chess Challenge.
|
|
| 14 |
## Submission Info
|
| 15 |
|
| 16 |
- **Submitted by**: [gabriel-mariadass](https://huggingface.co/gabriel-mariadass)
|
| 17 |
-
- **Parameters**:
|
| 18 |
- **Organization**: LLM-course
|
| 19 |
|
| 20 |
## Model Details
|
| 21 |
|
| 22 |
- **Architecture**: Chess Transformer (GPT-style)
|
| 23 |
-
- **Vocab size**:
|
| 24 |
- **Embedding dim**: 64
|
| 25 |
- **Layers**: 2
|
| 26 |
- **Heads**: 2
|
|
|
|
| 14 |
## Submission Info
|
| 15 |
|
| 16 |
- **Submitted by**: [gabriel-mariadass](https://huggingface.co/gabriel-mariadass)
|
| 17 |
+
- **Parameters**: 98,688
|
| 18 |
- **Organization**: LLM-course
|
| 19 |
|
| 20 |
## Model Details
|
| 21 |
|
| 22 |
- **Architecture**: Chess Transformer (GPT-style)
|
| 23 |
+
- **Vocab size**: 108
|
| 24 |
- **Embedding dim**: 64
|
| 25 |
- **Layers**: 2
|
| 26 |
- **Heads**: 2
|
config.json
CHANGED
|
@@ -6,6 +6,7 @@
|
|
| 6 |
"dropout": 0.1,
|
| 7 |
"dtype": "float32",
|
| 8 |
"eos_token_id": 2,
|
|
|
|
| 9 |
"model_type": "chess_transformer",
|
| 10 |
"n_ctx": 128,
|
| 11 |
"n_embd": 64,
|
|
@@ -15,5 +16,5 @@
|
|
| 15 |
"pad_token_id": 0,
|
| 16 |
"tie_weights": true,
|
| 17 |
"transformers_version": "4.57.3",
|
| 18 |
-
"vocab_size":
|
| 19 |
}
|
|
|
|
| 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": 128,
|
| 12 |
"n_embd": 64,
|
|
|
|
| 16 |
"pad_token_id": 0,
|
| 17 |
"tie_weights": true,
|
| 18 |
"transformers_version": "4.57.3",
|
| 19 |
+
"vocab_size": 108
|
| 20 |
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:da32936787cc88a6bb980274667ecc7c7255633fbbc49ffcfeaad73828d8228d
|
| 3 |
+
size 397024
|
tokenizer.py
CHANGED
|
@@ -1,16 +1,14 @@
|
|
| 1 |
"""
|
| 2 |
-
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
- Color + Piece : WP, BN, ...
|
| 7 |
-
- From square : e2_f
|
| 8 |
-
- To square : e4_t
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
"""
|
| 15 |
|
| 16 |
from __future__ import annotations
|
|
@@ -24,41 +22,70 @@ from transformers import PreTrainedTokenizer
|
|
| 24 |
|
| 25 |
|
| 26 |
class ChessTokenizer(PreTrainedTokenizer):
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
model_input_names = ["input_ids", "attention_mask"]
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
PAD_TOKEN = "[PAD]"
|
| 32 |
BOS_TOKEN = "[BOS]"
|
| 33 |
EOS_TOKEN = "[EOS]"
|
| 34 |
UNK_TOKEN = "[UNK]"
|
| 35 |
-
|
| 36 |
def __init__(
|
| 37 |
self,
|
| 38 |
vocab_file: Optional[str] = None,
|
| 39 |
vocab: Optional[Dict[str, int]] = None,
|
| 40 |
**kwargs,
|
| 41 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
self._pad_token = self.PAD_TOKEN
|
| 43 |
self._bos_token = self.BOS_TOKEN
|
| 44 |
self._eos_token = self.EOS_TOKEN
|
| 45 |
self._unk_token = self.UNK_TOKEN
|
| 46 |
|
|
|
|
|
|
|
| 47 |
kwargs.pop("pad_token", None)
|
| 48 |
kwargs.pop("bos_token", None)
|
| 49 |
kwargs.pop("eos_token", None)
|
| 50 |
kwargs.pop("unk_token", None)
|
| 51 |
-
|
|
|
|
| 52 |
if vocab is not None:
|
| 53 |
self._vocab = vocab
|
| 54 |
elif vocab_file is not None and os.path.exists(vocab_file):
|
| 55 |
with open(vocab_file, "r", encoding="utf-8") as f:
|
| 56 |
self._vocab = json.load(f)
|
| 57 |
else:
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
| 60 |
self._ids_to_tokens = {v: k for k, v in self._vocab.items()}
|
| 61 |
-
|
|
|
|
| 62 |
super().__init__(
|
| 63 |
pad_token=self._pad_token,
|
| 64 |
bos_token=self._bos_token,
|
|
@@ -66,108 +93,186 @@ class ChessTokenizer(PreTrainedTokenizer):
|
|
| 66 |
unk_token=self._unk_token,
|
| 67 |
**kwargs,
|
| 68 |
)
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
idx = len(vocab)
|
| 81 |
-
|
| 82 |
-
# Pieces with color
|
| 83 |
-
for color in ["W", "B"]:
|
| 84 |
-
for piece in ["P", "N", "B", "R", "Q", "K"]:
|
| 85 |
-
vocab[f"{color}{piece}"] = idx
|
| 86 |
-
idx += 1
|
| 87 |
-
|
| 88 |
-
# Board squares
|
| 89 |
-
files = "abcdefgh"
|
| 90 |
-
ranks = "12345678"
|
| 91 |
-
for f in files:
|
| 92 |
-
for r in ranks:
|
| 93 |
-
vocab[f"{f}{r}_f"] = idx
|
| 94 |
-
idx += 1
|
| 95 |
-
vocab[f"{f}{r}_t"] = idx
|
| 96 |
-
idx += 1
|
| 97 |
-
|
| 98 |
return vocab
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
@property
|
| 104 |
def vocab_size(self) -> int:
|
|
|
|
| 105 |
return len(self._vocab)
|
| 106 |
-
|
| 107 |
def get_vocab(self) -> Dict[str, int]:
|
|
|
|
| 108 |
return dict(self._vocab)
|
| 109 |
-
|
| 110 |
-
# --------------------------------------------------
|
| 111 |
-
# TOKENIZATION LOGIC
|
| 112 |
-
# --------------------------------------------------
|
| 113 |
def _tokenize(self, text: str) -> List[str]:
|
| 114 |
"""
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
| 120 |
"""
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
for move in moves:
|
| 125 |
-
if len(move) < 6:
|
| 126 |
-
continue
|
| 127 |
-
|
| 128 |
-
color = move[0]
|
| 129 |
-
piece = move[1]
|
| 130 |
-
from_sq = move[2:4]
|
| 131 |
-
to_sq = move[4:6]
|
| 132 |
-
|
| 133 |
-
tokens.append(f"{color}{piece}")
|
| 134 |
-
tokens.append(f"{from_sq}_f")
|
| 135 |
-
tokens.append(f"{to_sq}_t")
|
| 136 |
-
|
| 137 |
-
return tokens
|
| 138 |
-
|
| 139 |
def _convert_token_to_id(self, token: str) -> int:
|
| 140 |
-
|
| 141 |
-
|
|
|
|
| 142 |
def _convert_id_to_token(self, index: int) -> str:
|
|
|
|
| 143 |
return self._ids_to_tokens.get(index, self.UNK_TOKEN)
|
| 144 |
-
|
| 145 |
def convert_tokens_to_string(self, tokens: List[str]) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
"""
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
"""
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
cp = tokens[i]
|
| 154 |
-
f = tokens[i + 1].replace("_f", "")
|
| 155 |
-
t = tokens[i + 2].replace("_t", "")
|
| 156 |
-
out.append(cp + f + t)
|
| 157 |
-
except Exception:
|
| 158 |
-
pass
|
| 159 |
-
i += 3
|
| 160 |
-
return " ".join(out)
|
| 161 |
-
|
| 162 |
-
# --------------------------------------------------
|
| 163 |
-
# SAVE / LOAD
|
| 164 |
-
# --------------------------------------------------
|
| 165 |
-
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None):
|
| 166 |
-
os.makedirs(save_directory, exist_ok=True)
|
| 167 |
-
path = os.path.join(
|
| 168 |
save_directory,
|
| 169 |
(filename_prefix + "-" if filename_prefix else "") + "vocab.json",
|
| 170 |
)
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
"""
|
| 13 |
|
| 14 |
from __future__ import annotations
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
class ChessTokenizer(PreTrainedTokenizer):
|
| 25 |
+
"""
|
| 26 |
+
A custom tokenizer for chess moves using extended UCI notation.
|
| 27 |
+
|
| 28 |
+
This tokenizer maps each possible chess move to a unique token ID.
|
| 29 |
+
The vocabulary is built from the training dataset to ensure all moves
|
| 30 |
+
encountered during training have a corresponding token.
|
| 31 |
+
|
| 32 |
+
Example:
|
| 33 |
+
>>> tokenizer = ChessTokenizer()
|
| 34 |
+
>>> tokenizer.encode("WPe2e4 BPe7e5")
|
| 35 |
+
[1, 42, 87, 2] # [BOS, e2e4, e7e5, EOS]
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
model_input_names = ["input_ids", "attention_mask"]
|
| 39 |
+
vocab_files_names = {"vocab_file": "vocab.json"}
|
| 40 |
+
|
| 41 |
+
# Special tokens
|
| 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 |
+
"""
|
| 54 |
+
Initialize the chess tokenizer.
|
| 55 |
+
|
| 56 |
+
Args:
|
| 57 |
+
vocab_file: Path to a JSON file containing the vocabulary mapping.
|
| 58 |
+
vocab: Dictionary mapping tokens to IDs (alternative to vocab_file).
|
| 59 |
+
**kwargs: Additional arguments passed to PreTrainedTokenizer.
|
| 60 |
+
"""
|
| 61 |
+
# Initialize special tokens
|
| 62 |
self._pad_token = self.PAD_TOKEN
|
| 63 |
self._bos_token = self.BOS_TOKEN
|
| 64 |
self._eos_token = self.EOS_TOKEN
|
| 65 |
self._unk_token = self.UNK_TOKEN
|
| 66 |
|
| 67 |
+
# Remove any duplicate special-token entries passed through kwargs
|
| 68 |
+
# to avoid "multiple values for keyword" errors when loading from disk.
|
| 69 |
kwargs.pop("pad_token", None)
|
| 70 |
kwargs.pop("bos_token", None)
|
| 71 |
kwargs.pop("eos_token", None)
|
| 72 |
kwargs.pop("unk_token", None)
|
| 73 |
+
|
| 74 |
+
# Load or create vocabulary
|
| 75 |
if vocab is not None:
|
| 76 |
self._vocab = vocab
|
| 77 |
elif vocab_file is not None and os.path.exists(vocab_file):
|
| 78 |
with open(vocab_file, "r", encoding="utf-8") as f:
|
| 79 |
self._vocab = json.load(f)
|
| 80 |
else:
|
| 81 |
+
# Create a minimal vocabulary with just special tokens
|
| 82 |
+
# The full vocabulary should be built from the dataset
|
| 83 |
+
self._vocab = self._create_default_vocab()
|
| 84 |
+
|
| 85 |
+
# Create reverse mapping
|
| 86 |
self._ids_to_tokens = {v: k for k, v in self._vocab.items()}
|
| 87 |
+
|
| 88 |
+
# Call parent init AFTER setting up vocab
|
| 89 |
super().__init__(
|
| 90 |
pad_token=self._pad_token,
|
| 91 |
bos_token=self._bos_token,
|
|
|
|
| 93 |
unk_token=self._unk_token,
|
| 94 |
**kwargs,
|
| 95 |
)
|
| 96 |
+
|
| 97 |
+
def _create_default_vocab(self) -> Dict[str, int]:
|
| 98 |
+
"""
|
| 99 |
+
Create a minimal default vocabulary with just special tokens.
|
| 100 |
+
|
| 101 |
+
For the full vocabulary, use `build_vocab_from_dataset()`.
|
| 102 |
+
This minimal vocab is just a placeholder - you should build from data.
|
| 103 |
+
"""
|
| 104 |
+
special_tokens = [self.PAD_TOKEN, self.BOS_TOKEN, self.EOS_TOKEN, self.UNK_TOKEN]
|
| 105 |
+
vocab = {token: idx for idx, token in enumerate(special_tokens)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
return vocab
|
| 107 |
+
|
| 108 |
+
@classmethod
|
| 109 |
+
def build_vocab_from_iterator(
|
| 110 |
+
cls,
|
| 111 |
+
iterator,
|
| 112 |
+
min_frequency: int = 1,
|
| 113 |
+
) -> "ChessTokenizer":
|
| 114 |
+
"""
|
| 115 |
+
Build a tokenizer vocabulary from an iterator of game strings.
|
| 116 |
+
|
| 117 |
+
Args:
|
| 118 |
+
iterator: An iterator yielding game strings (space-separated moves).
|
| 119 |
+
min_frequency: Minimum frequency for a token to be included.
|
| 120 |
+
|
| 121 |
+
Returns:
|
| 122 |
+
A ChessTokenizer with the built vocabulary.
|
| 123 |
+
"""
|
| 124 |
+
from collections import Counter
|
| 125 |
+
|
| 126 |
+
token_counts = Counter()
|
| 127 |
+
|
| 128 |
+
for game in iterator:
|
| 129 |
+
moves = game.strip().split()
|
| 130 |
+
token_counts.update(moves)
|
| 131 |
+
|
| 132 |
+
# Filter by frequency
|
| 133 |
+
tokens = [
|
| 134 |
+
token for token, count in token_counts.items()
|
| 135 |
+
if count >= min_frequency
|
| 136 |
+
]
|
| 137 |
+
|
| 138 |
+
# Sort for reproducibility
|
| 139 |
+
tokens = sorted(tokens)
|
| 140 |
+
|
| 141 |
+
# Build vocabulary
|
| 142 |
+
special_tokens = [cls.PAD_TOKEN, cls.BOS_TOKEN, cls.EOS_TOKEN, cls.UNK_TOKEN]
|
| 143 |
+
vocab = {token: idx for idx, token in enumerate(special_tokens + tokens)}
|
| 144 |
+
|
| 145 |
+
return cls(vocab=vocab)
|
| 146 |
+
|
| 147 |
+
@classmethod
|
| 148 |
+
def build_vocab_from_dataset(
|
| 149 |
+
cls,
|
| 150 |
+
dataset_name: str = "dlouapre/lichess_2025-01_1M",
|
| 151 |
+
split: str = "train",
|
| 152 |
+
column: str = "text",
|
| 153 |
+
min_frequency: int = 500,
|
| 154 |
+
max_samples: Optional[int] = 100000,
|
| 155 |
+
) -> "ChessTokenizer":
|
| 156 |
+
"""
|
| 157 |
+
Build a tokenizer vocabulary from a Hugging Face dataset.
|
| 158 |
+
|
| 159 |
+
Args:
|
| 160 |
+
dataset_name: Name of the dataset on Hugging Face Hub.
|
| 161 |
+
split: Dataset split to use.
|
| 162 |
+
column: Column containing the game strings.
|
| 163 |
+
min_frequency: Minimum frequency for a token to be included (default: 500).
|
| 164 |
+
max_samples: Maximum number of samples to process (default: 100k).
|
| 165 |
+
|
| 166 |
+
Returns:
|
| 167 |
+
A ChessTokenizer with the built vocabulary.
|
| 168 |
+
"""
|
| 169 |
+
from datasets import load_dataset
|
| 170 |
+
|
| 171 |
+
dataset = load_dataset(dataset_name, split=split)
|
| 172 |
+
|
| 173 |
+
if max_samples is not None:
|
| 174 |
+
dataset = dataset.select(range(min(max_samples, len(dataset))))
|
| 175 |
+
|
| 176 |
+
def game_iterator():
|
| 177 |
+
for example in dataset:
|
| 178 |
+
yield example[column]
|
| 179 |
+
|
| 180 |
+
return cls.build_vocab_from_iterator(game_iterator(), min_frequency=min_frequency)
|
| 181 |
+
|
| 182 |
@property
|
| 183 |
def vocab_size(self) -> int:
|
| 184 |
+
"""Return the size of the vocabulary."""
|
| 185 |
return len(self._vocab)
|
| 186 |
+
|
| 187 |
def get_vocab(self) -> Dict[str, int]:
|
| 188 |
+
"""Return the vocabulary as a dictionary."""
|
| 189 |
return dict(self._vocab)
|
| 190 |
+
|
|
|
|
|
|
|
|
|
|
| 191 |
def _tokenize(self, text: str) -> List[str]:
|
| 192 |
"""
|
| 193 |
+
Tokenize a string of moves into a list of tokens.
|
| 194 |
+
|
| 195 |
+
Args:
|
| 196 |
+
text: A string of space-separated moves.
|
| 197 |
+
|
| 198 |
+
Returns:
|
| 199 |
+
List of move tokens.
|
| 200 |
"""
|
| 201 |
+
return text.strip().split()
|
| 202 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
def _convert_token_to_id(self, token: str) -> int:
|
| 204 |
+
"""Convert a token to its ID."""
|
| 205 |
+
return self._vocab.get(token, self._vocab.get(self.UNK_TOKEN, 0))
|
| 206 |
+
|
| 207 |
def _convert_id_to_token(self, index: int) -> str:
|
| 208 |
+
"""Convert an ID to its token."""
|
| 209 |
return self._ids_to_tokens.get(index, self.UNK_TOKEN)
|
| 210 |
+
|
| 211 |
def convert_tokens_to_string(self, tokens: List[str]) -> str:
|
| 212 |
+
"""Convert a list of tokens back to a string."""
|
| 213 |
+
# Filter out special tokens for cleaner output
|
| 214 |
+
special = {self.PAD_TOKEN, self.BOS_TOKEN, self.EOS_TOKEN, self.UNK_TOKEN}
|
| 215 |
+
return " ".join(t for t in tokens if t not in special)
|
| 216 |
+
|
| 217 |
+
def save_vocabulary(
|
| 218 |
+
self,
|
| 219 |
+
save_directory: str,
|
| 220 |
+
filename_prefix: Optional[str] = None,
|
| 221 |
+
) -> tuple:
|
| 222 |
"""
|
| 223 |
+
Save the vocabulary to a JSON file.
|
| 224 |
+
|
| 225 |
+
Args:
|
| 226 |
+
save_directory: Directory to save the vocabulary.
|
| 227 |
+
filename_prefix: Optional prefix for the filename.
|
| 228 |
+
|
| 229 |
+
Returns:
|
| 230 |
+
Tuple containing the path to the saved vocabulary file.
|
| 231 |
"""
|
| 232 |
+
if not os.path.isdir(save_directory):
|
| 233 |
+
os.makedirs(save_directory, exist_ok=True)
|
| 234 |
+
|
| 235 |
+
vocab_file = os.path.join(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
save_directory,
|
| 237 |
(filename_prefix + "-" if filename_prefix else "") + "vocab.json",
|
| 238 |
)
|
| 239 |
+
|
| 240 |
+
with open(vocab_file, "w", encoding="utf-8") as f:
|
| 241 |
+
json.dump(self._vocab, f, ensure_ascii=False, indent=2)
|
| 242 |
+
|
| 243 |
+
return (vocab_file,)
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
def count_vocab_from_dataset(
|
| 247 |
+
dataset_name: str = "dlouapre/lichess_2025-01_1M",
|
| 248 |
+
split: str = "train",
|
| 249 |
+
column: str = "text",
|
| 250 |
+
max_samples: Optional[int] = 10000,
|
| 251 |
+
) -> Dict[str, int]:
|
| 252 |
+
"""
|
| 253 |
+
Count token frequencies in a dataset (useful for vocabulary analysis).
|
| 254 |
+
|
| 255 |
+
Args:
|
| 256 |
+
dataset_name: Name of the dataset on Hugging Face Hub.
|
| 257 |
+
split: Dataset split to use.
|
| 258 |
+
column: Column containing the game strings.
|
| 259 |
+
max_samples: Maximum number of samples to process.
|
| 260 |
+
|
| 261 |
+
Returns:
|
| 262 |
+
Dictionary mapping tokens to their frequencies.
|
| 263 |
+
"""
|
| 264 |
+
from collections import Counter
|
| 265 |
+
from datasets import load_dataset
|
| 266 |
+
|
| 267 |
+
dataset = load_dataset(dataset_name, split=split)
|
| 268 |
+
|
| 269 |
+
if max_samples is not None:
|
| 270 |
+
dataset = dataset.select(range(min(max_samples, len(dataset))))
|
| 271 |
+
|
| 272 |
+
token_counts = Counter()
|
| 273 |
+
|
| 274 |
+
for example in dataset:
|
| 275 |
+
moves = example[column].strip().split()
|
| 276 |
+
token_counts.update(moves)
|
| 277 |
+
|
| 278 |
+
return dict(token_counts)
|
vocab.json
CHANGED
|
@@ -3,144 +3,108 @@
|
|
| 3 |
"[BOS]": 1,
|
| 4 |
"[EOS]": 2,
|
| 5 |
"[UNK]": 3,
|
| 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 |
-
"f7_f": 108,
|
| 111 |
-
"f7_t": 109,
|
| 112 |
-
"f8_f": 110,
|
| 113 |
-
"f8_t": 111,
|
| 114 |
-
"g1_f": 112,
|
| 115 |
-
"g1_t": 113,
|
| 116 |
-
"g2_f": 114,
|
| 117 |
-
"g2_t": 115,
|
| 118 |
-
"g3_f": 116,
|
| 119 |
-
"g3_t": 117,
|
| 120 |
-
"g4_f": 118,
|
| 121 |
-
"g4_t": 119,
|
| 122 |
-
"g5_f": 120,
|
| 123 |
-
"g5_t": 121,
|
| 124 |
-
"g6_f": 122,
|
| 125 |
-
"g6_t": 123,
|
| 126 |
-
"g7_f": 124,
|
| 127 |
-
"g7_t": 125,
|
| 128 |
-
"g8_f": 126,
|
| 129 |
-
"g8_t": 127,
|
| 130 |
-
"h1_f": 128,
|
| 131 |
-
"h1_t": 129,
|
| 132 |
-
"h2_f": 130,
|
| 133 |
-
"h2_t": 131,
|
| 134 |
-
"h3_f": 132,
|
| 135 |
-
"h3_t": 133,
|
| 136 |
-
"h4_f": 134,
|
| 137 |
-
"h4_t": 135,
|
| 138 |
-
"h5_f": 136,
|
| 139 |
-
"h5_t": 137,
|
| 140 |
-
"h6_f": 138,
|
| 141 |
-
"h6_t": 139,
|
| 142 |
-
"h7_f": 140,
|
| 143 |
-
"h7_t": 141,
|
| 144 |
-
"h8_f": 142,
|
| 145 |
-
"h8_t": 143
|
| 146 |
}
|
|
|
|
| 3 |
"[BOS]": 1,
|
| 4 |
"[EOS]": 2,
|
| 5 |
"[UNK]": 3,
|
| 6 |
+
"BBc8b7": 4,
|
| 7 |
+
"BBc8d7": 5,
|
| 8 |
+
"BBc8e6": 6,
|
| 9 |
+
"BBc8f5": 7,
|
| 10 |
+
"BBc8g4": 8,
|
| 11 |
+
"BBf8c5": 9,
|
| 12 |
+
"BBf8d6": 10,
|
| 13 |
+
"BBf8e7": 11,
|
| 14 |
+
"BBf8g7": 12,
|
| 15 |
+
"BKe8c8(O)": 13,
|
| 16 |
+
"BKe8g8(o)": 14,
|
| 17 |
+
"BKg8h8": 15,
|
| 18 |
+
"BNb8c6": 16,
|
| 19 |
+
"BNb8d7": 17,
|
| 20 |
+
"BNf6e4": 18,
|
| 21 |
+
"BNf6e4(x)": 19,
|
| 22 |
+
"BNg8e7": 20,
|
| 23 |
+
"BNg8f6": 21,
|
| 24 |
+
"BPa7a5": 22,
|
| 25 |
+
"BPa7a6": 23,
|
| 26 |
+
"BPb5b4": 24,
|
| 27 |
+
"BPb7b5": 25,
|
| 28 |
+
"BPb7b6": 26,
|
| 29 |
+
"BPb7c6(x)": 27,
|
| 30 |
+
"BPc5d4(x)": 28,
|
| 31 |
+
"BPc6c5": 29,
|
| 32 |
+
"BPc6d5(x)": 30,
|
| 33 |
+
"BPc7c5": 31,
|
| 34 |
+
"BPc7c6": 32,
|
| 35 |
+
"BPd5d4": 33,
|
| 36 |
+
"BPd5e4(x)": 34,
|
| 37 |
+
"BPd6d5": 35,
|
| 38 |
+
"BPd6e5(x)": 36,
|
| 39 |
+
"BPd7d5": 37,
|
| 40 |
+
"BPd7d6": 38,
|
| 41 |
+
"BPe5d4(x)": 39,
|
| 42 |
+
"BPe5e4": 40,
|
| 43 |
+
"BPe6d5(x)": 41,
|
| 44 |
+
"BPe6e5": 42,
|
| 45 |
+
"BPe7e5": 43,
|
| 46 |
+
"BPe7e6": 44,
|
| 47 |
+
"BPf7f5": 45,
|
| 48 |
+
"BPf7f6": 46,
|
| 49 |
+
"BPg7g5": 47,
|
| 50 |
+
"BPg7g6": 48,
|
| 51 |
+
"BPh7h5": 49,
|
| 52 |
+
"BPh7h6": 50,
|
| 53 |
+
"BQd8c7": 51,
|
| 54 |
+
"BQd8e7": 52,
|
| 55 |
+
"BRa8b8": 53,
|
| 56 |
+
"BRa8c8": 54,
|
| 57 |
+
"BRa8d8": 55,
|
| 58 |
+
"BRf8e8": 56,
|
| 59 |
+
"WBc1b2": 57,
|
| 60 |
+
"WBc1d2": 58,
|
| 61 |
+
"WBc1e3": 59,
|
| 62 |
+
"WBc1f4": 60,
|
| 63 |
+
"WBc1g5": 61,
|
| 64 |
+
"WBf1c4": 62,
|
| 65 |
+
"WBf1d3": 63,
|
| 66 |
+
"WBf1e2": 64,
|
| 67 |
+
"WBf1g2": 65,
|
| 68 |
+
"WKe1c1(O)": 66,
|
| 69 |
+
"WKe1g1(o)": 67,
|
| 70 |
+
"WKg1h1": 68,
|
| 71 |
+
"WNb1c3": 69,
|
| 72 |
+
"WNb1d2": 70,
|
| 73 |
+
"WNc3d5": 71,
|
| 74 |
+
"WNf3d4(x)": 72,
|
| 75 |
+
"WNf3e5": 73,
|
| 76 |
+
"WNf3e5(x)": 74,
|
| 77 |
+
"WNf3g5": 75,
|
| 78 |
+
"WNg1f3": 76,
|
| 79 |
+
"WPa2a3": 77,
|
| 80 |
+
"WPa2a4": 78,
|
| 81 |
+
"WPb2b3": 79,
|
| 82 |
+
"WPb2b4": 80,
|
| 83 |
+
"WPc2c3": 81,
|
| 84 |
+
"WPc2c4": 82,
|
| 85 |
+
"WPc3c4": 83,
|
| 86 |
+
"WPc3d4(x)": 84,
|
| 87 |
+
"WPc4d5(x)": 85,
|
| 88 |
+
"WPd2d3": 86,
|
| 89 |
+
"WPd2d4": 87,
|
| 90 |
+
"WPd4d5": 88,
|
| 91 |
+
"WPd4e5(x)": 89,
|
| 92 |
+
"WPe2e3": 90,
|
| 93 |
+
"WPe2e4": 91,
|
| 94 |
+
"WPe3e4": 92,
|
| 95 |
+
"WPe4d5(x)": 93,
|
| 96 |
+
"WPe4e5": 94,
|
| 97 |
+
"WPf2f3": 95,
|
| 98 |
+
"WPf2f4": 96,
|
| 99 |
+
"WPg2g3": 97,
|
| 100 |
+
"WPg2g4": 98,
|
| 101 |
+
"WPh2h3": 99,
|
| 102 |
+
"WPh2h4": 100,
|
| 103 |
+
"WPh4h5": 101,
|
| 104 |
+
"WQd1d2": 102,
|
| 105 |
+
"WQd1e2": 103,
|
| 106 |
+
"WRa1b1": 104,
|
| 107 |
+
"WRa1c1": 105,
|
| 108 |
+
"WRa1d1": 106,
|
| 109 |
+
"WRf1e1": 107
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
}
|