File size: 8,895 Bytes
b83b66b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""
Utility functions for the Chess Challenge.

This module provides helper functions for:
- Parameter counting and budget analysis
- Model registration with Hugging Face
- Move validation with python-chess
"""

from __future__ import annotations

from typing import Dict, Optional, TYPE_CHECKING

import torch.nn as nn

if TYPE_CHECKING:
    from src.model import ChessConfig


def count_parameters(model: nn.Module, trainable_only: bool = True) -> int:
    """
    Count the number of parameters in a model.
    
    Args:
        model: The PyTorch model.
        trainable_only: If True, only count trainable parameters.
    
    Returns:
        Total number of parameters.
    """
    if trainable_only:
        return sum(p.numel() for p in model.parameters() if p.requires_grad)
    return sum(p.numel() for p in model.parameters())


def count_parameters_by_component(model: nn.Module) -> Dict[str, int]:
    """
    Count parameters broken down by model component.
    
    Args:
        model: The PyTorch model.
    
    Returns:
        Dictionary mapping component names to parameter counts.
    """
    counts = {}
    for name, module in model.named_modules():
        if len(list(module.children())) == 0:  # Leaf module
            param_count = sum(p.numel() for p in module.parameters(recurse=False))
            if param_count > 0:
                counts[name] = param_count
    return counts


def estimate_parameters(config: "ChessConfig") -> Dict[str, int]:
    """
    Estimate the parameter count for a given configuration.
    
    This is useful for planning your architecture before building the model.
    
    Args:
        config: Model configuration.
    
    Returns:
        Dictionary with estimated parameter counts by component.
    """
    V = config.vocab_size
    d = config.n_embd
    L = config.n_layer
    n_ctx = config.n_ctx
    n_inner = config.n_inner
    
    estimates = {
        "token_embeddings": V * d,
        "position_embeddings": n_ctx * d,
        "attention_qkv_per_layer": 3 * d * d,
        "attention_proj_per_layer": d * d,
        "ffn_per_layer": 2 * d * n_inner,
        "layernorm_per_layer": 4 * d,  # 2 LayerNorms, each with weight and bias
        "final_layernorm": 2 * d,
    }
    
    # Calculate totals
    per_layer = (
        estimates["attention_qkv_per_layer"] +
        estimates["attention_proj_per_layer"] +
        estimates["ffn_per_layer"] +
        estimates["layernorm_per_layer"]
    )
    
    estimates["total_transformer_layers"] = L * per_layer
    
    # LM head (tied with embeddings by default)
    if config.tie_weights:
        estimates["lm_head"] = 0
        estimates["lm_head_note"] = "Tied with token embeddings"
    else:
        estimates["lm_head"] = V * d
    
    # Grand total
    estimates["total"] = (
        estimates["token_embeddings"] +
        estimates["position_embeddings"] +
        estimates["total_transformer_layers"] +
        estimates["final_layernorm"] +
        estimates["lm_head"]
    )
    
    return estimates


def print_parameter_budget(config: "ChessConfig", limit: int = 1_000_000) -> None:
    """
    Print a formatted parameter budget analysis.
    
    Args:
        config: Model configuration.
        limit: Parameter limit to compare against.
    """
    estimates = estimate_parameters(config)
    
    print("=" * 60)
    print("PARAMETER BUDGET ANALYSIS")
    print("=" * 60)
    print(f"\nConfiguration:")
    print(f"  vocab_size (V) = {config.vocab_size}")
    print(f"  n_embd (d)     = {config.n_embd}")
    print(f"  n_layer (L)    = {config.n_layer}")
    print(f"  n_head         = {config.n_head}")
    print(f"  n_ctx          = {config.n_ctx}")
    print(f"  n_inner        = {config.n_inner}")
    print(f"  tie_weights    = {config.tie_weights}")
    
    print(f"\nParameter Breakdown:")
    print(f"  Token Embeddings:    {estimates['token_embeddings']:>10,}")
    print(f"  Position Embeddings: {estimates['position_embeddings']:>10,}")
    print(f"  Transformer Layers:  {estimates['total_transformer_layers']:>10,}")
    print(f"  Final LayerNorm:     {estimates['final_layernorm']:>10,}")
    
    if config.tie_weights:
        print(f"  LM Head:             {'(tied)':>10}")
    else:
        print(f"  LM Head:             {estimates['lm_head']:>10,}")
    
    print(f"  " + "-" * 30)
    print(f"  TOTAL:               {estimates['total']:>10,}")
    
    print(f"\nBudget Status:")
    print(f"  Limit:    {limit:>10,}")
    print(f"  Used:     {estimates['total']:>10,}")
    print(f"  Remaining:{limit - estimates['total']:>10,}")
    
    if estimates['total'] <= limit:
        print(f"\n Within budget! ({estimates['total'] / limit * 100:.1f}% used)")
    else:
        print(f"\n OVER BUDGET by {estimates['total'] - limit:,} parameters!")
    
    print("=" * 60)


def validate_move_with_chess(move: str, board_fen: Optional[str] = None) -> bool:
    """
    Validate a move using python-chess.
    
    This function converts the dataset's extended UCI format to standard UCI
    and validates it against the current board state.
    
    Args:
        move: Move in extended UCI format (e.g., "WPe2e4", "BNg8f6(x)").
        board_fen: FEN string of the current board state (optional).
    
    Returns:
        True if the move is legal, False otherwise.
    """
    try:
        import chess
    except ImportError:
        raise ImportError("python-chess is required for move validation. "
                         "Install it with: pip install python-chess")
    
    # Parse the extended UCI format
    # Format: [W|B][Piece][from_sq][to_sq][suffix]
    # Example: WPe2e4, BNg8f6(x), WKe1g1(o)
    
    if len(move) < 6:
        return False
    
    # Extract components
    color = move[0]  # W or B
    piece = move[1]  # P, N, B, R, Q, K
    from_sq = move[2:4]  # e.g., "e2"
    to_sq = move[4:6]  # e.g., "e4"
    
    # Check for promotion
    promotion = None
    if "=" in move:
        promo_idx = move.index("=")
        promotion = move[promo_idx + 1].lower()
    
    # Create board
    board = chess.Board(board_fen) if board_fen else chess.Board()
    
    # Build UCI move string
    uci_move = from_sq + to_sq
    if promotion:
        uci_move += promotion
    
    try:
        move_obj = chess.Move.from_uci(uci_move)
        return move_obj in board.legal_moves
    except (ValueError, chess.InvalidMoveError):
        return False


def convert_extended_uci_to_uci(move: str) -> str:
    """
    Convert extended UCI format to standard UCI format.
    
    Args:
        move: Move in extended UCI format (e.g., "WPe2e4").
    
    Returns:
        Move in standard UCI format (e.g., "e2e4").
    """
    if len(move) < 6:
        return move
    
    # Extract squares
    from_sq = move[2:4]
    to_sq = move[4:6]
    
    # Check for promotion
    promotion = ""
    if "=" in move:
        promo_idx = move.index("=")
        promotion = move[promo_idx + 1].lower()
    
    return from_sq + to_sq + promotion


def convert_uci_to_extended(
    uci_move: str,
    board_fen: str,
) -> str:
    """
    Convert standard UCI format to extended UCI format.
    
    Args:
        uci_move: Move in standard UCI format (e.g., "e2e4").
        board_fen: FEN string of the current board state.
    
    Returns:
        Move in extended UCI format (e.g., "WPe2e4").
    """
    try:
        import chess
    except ImportError:
        raise ImportError("python-chess is required for move conversion.")
    
    board = chess.Board(board_fen)
    move = chess.Move.from_uci(uci_move)
    
    # Get color
    color = "W" if board.turn == chess.WHITE else "B"
    
    # Get piece
    piece = board.piece_at(move.from_square)
    piece_letter = piece.symbol().upper() if piece else "P"
    
    # Build extended UCI
    from_sq = chess.square_name(move.from_square)
    to_sq = chess.square_name(move.to_square)
    
    result = f"{color}{piece_letter}{from_sq}{to_sq}"
    
    # Add promotion
    if move.promotion:
        result += f"={chess.piece_symbol(move.promotion).upper()}"
    
    # Add suffix for captures
    if board.is_capture(move):
        result += "(x)"
    
    # Add suffix for check/checkmate
    board.push(move)
    if board.is_checkmate():
        if "(x)" in result:
            result = result.replace("(x)", "(x+*)")
        else:
            result += "(+*)"
    elif board.is_check():
        if "(x)" in result:
            result = result.replace("(x)", "(x+)")
        else:
            result += "(+)"
    board.pop()
    
    # Handle castling notation
    if board.is_castling(move):
        if move.to_square in [chess.G1, chess.G8]:  # Kingside
            result = result.replace("(x)", "").replace("(+)", "") + "(o)"
        else:  # Queenside
            result = result.replace("(x)", "").replace("(+)", "") + "(O)"
    
    return result