56m commited on
Commit
34aa9f1
·
verified ·
1 Parent(s): 3b77ddd

Upload folder using huggingface_hub

Browse files
README.md ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: en
3
+ license: mit
4
+ tags:
5
+ - chess
6
+ - transformer
7
+ - custom-architecture
8
+ pipeline_tag: robotics
9
+ ---
10
+
11
+ # ChessDumb-2.5M (`dumbc`)
12
+
13
+ **ChessDumb-2.5M** is a hyper-expressive, ultra-compact Chess AI architecture designed for extreme representation capacity with only **1.69M parameters**.
14
+
15
+ ## Key Features
16
+ - **DumbChessRetina**: Non-Euclidean grid folding with 4-axis directional convolutions and ray-tracing threat embeddings.
17
+ - **DumbAttention**: 4-way fused attention featuring Hadamard tensor braids, 3-body trinity tensor coupling, material differential biases, and dynamic temperature Softmax.
18
+ - **DumbFractalFFN**: Channel-shuffled polynomial interaction FFN.
19
+ - **DumbRecurrentEngine**: Virtual 15-layer deep thinking engine powered by a 5-layer parameter-reusing loop with step embeddings.
20
+
21
+ ## Usage
22
+ ```python
23
+ import torch
24
+ from transformers import AutoModel, AutoTokenizer
25
+
26
+ model = AutoModel.from_pretrained("YOUR_USERNAME/ChessDumb", trust_remote_code=True)
27
+ tokenizer = AutoTokenizer.from_pretrained("YOUR_USERNAME/ChessDumb", trust_remote_code=True)
28
+
29
+ # Dummy Forward Test
30
+ board = torch.randint(0, 14, (1, 64))
31
+ mat_diff = torch.randn(1, 64, 64)
32
+ mat_weights = torch.randn(1, 64)
33
+
34
+ outputs = model(board_state=board, mat_diff_matrix=mat_diff, material_weights=mat_weights)
35
+ print("Policy shape:", outputs["policy_matrix"].shape) # (1, 64, 64)
36
+ print("Value shape:", outputs["value_logits"].shape) # (1, 3)
37
+
38
+ ```
__pycache__/configuration_dumbc.cpython-312.pyc ADDED
Binary file (1.02 kB). View file
 
__pycache__/modeling_dumbc.cpython-312.pyc ADDED
Binary file (12.4 kB). View file
 
config.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "auto_map": {
3
+ "AutoConfig": "configuration_dumbc.DumbcConfig",
4
+ "AutoModel": "modeling_dumbc.DumbcModel"
5
+ },
6
+ "bottleneck": 32,
7
+ "dim": 192,
8
+ "heads": 8,
9
+ "initializer_range": 0.02,
10
+ "model_type": "dumbc",
11
+ "num_loops": 3,
12
+ "num_unique_layers": 5,
13
+ "transformers_version": "5.13.1",
14
+ "vocab_size": 64
15
+ }
configuration_dumbc.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from transformers import PretrainedConfig
3
+
4
+ class DumbcConfig(PretrainedConfig):
5
+ model_type = "dumbc"
6
+
7
+ def __init__(
8
+ self,
9
+ dim=192,
10
+ num_unique_layers=5,
11
+ num_loops=3,
12
+ heads=8,
13
+ bottleneck=32,
14
+ vocab_size=64,
15
+ initializer_range=0.02,
16
+ **kwargs
17
+ ):
18
+ self.dim = dim
19
+ self.num_unique_layers = num_unique_layers
20
+ self.num_loops = num_loops
21
+ self.heads = heads
22
+ self.bottleneck = bottleneck
23
+ self.vocab_size = vocab_size
24
+ self.initializer_range = initializer_range
25
+ super().__init__(**kwargs)
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b6e35816fd9e7496f14fdb42e7d1000e92d25efa59d533f4d625aed1a89d4344
3
+ size 6779604
modeling_dumbc.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import torch
3
+ import torch.nn as nn
4
+ import torch.nn.functional as F
5
+ from transformers import PreTrainedModel
6
+
7
+ # リモート読み込みとローカル直接インポートの両方に対応する互換インポート
8
+ try:
9
+ from .configuration_dumbc import DumbcConfig
10
+ except ImportError:
11
+ from configuration_dumbc import DumbcConfig
12
+
13
+ class DumbChessRetina(nn.Module):
14
+ def __init__(self, dim=192):
15
+ super().__init__()
16
+ self.piece_embed = nn.Embedding(14, 32)
17
+ self.conv_rank_file = nn.Conv1d(32, 32, kernel_size=3, padding=1, groups=32)
18
+ self.conv_diag = nn.Conv1d(32, 32, kernel_size=3, padding=1, groups=32)
19
+ self.conv_antidiag = nn.Conv1d(32, 32, kernel_size=3, padding=1, groups=32)
20
+ self.conv_knight = nn.Conv1d(32, 32, kernel_size=3, padding=1, groups=32)
21
+ self.tension_mlp = nn.Sequential(
22
+ nn.Linear(32 * 4 + 1, 64),
23
+ nn.GELU(),
24
+ nn.Linear(64, dim)
25
+ )
26
+
27
+ def forward(self, board_state, material_weights):
28
+ x = self.piece_embed(board_state)
29
+ x_t = x.transpose(1, 2)
30
+ f1 = self.conv_rank_file(x_t)
31
+ f2 = self.conv_diag(x_t)
32
+ f3 = self.conv_antidiag(x_t)
33
+ f4 = self.conv_knight(x_t)
34
+ f_all = torch.cat([f1, f2, f3, f4], dim=1).transpose(1, 2)
35
+ tension_in = torch.cat([f_all, material_weights.unsqueeze(-1)], dim=-1)
36
+ return self.tension_mlp(tension_in)
37
+
38
+ class DumbAttention(nn.Module):
39
+ def __init__(self, dim=192, heads=8, bottleneck=32):
40
+ super().__init__()
41
+ self.dim = dim
42
+ self.heads = heads
43
+ self.head_dim = dim // heads
44
+ self.qkv_proj = nn.Linear(dim, dim * 3, bias=False)
45
+ self.out_proj = nn.Linear(dim, dim, bias=False)
46
+ self.hadamard_mlp = nn.Sequential(
47
+ nn.Linear(dim, bottleneck),
48
+ nn.SiLU(),
49
+ nn.Linear(bottleneck, 1)
50
+ )
51
+ self.trinity_g = nn.Linear(dim, 16, bias=False)
52
+ self.temp_mlp = nn.Sequential(
53
+ nn.Linear(dim, 16),
54
+ nn.SiLU(),
55
+ nn.Linear(16, 1)
56
+ )
57
+ self.threat_weight = nn.Parameter(torch.ones(1) * 0.5)
58
+
59
+ def forward(self, x, mat_diff_matrix):
60
+ B, N, C = x.shape
61
+ q, k, v = self.qkv_proj(x).chunk(3, dim=-1)
62
+ q_h = q.view(B, N, self.heads, self.head_dim).transpose(1, 2)
63
+ k_h = k.view(B, N, self.heads, self.head_dim).transpose(1, 2)
64
+ v_h = v.view(B, N, self.heads, self.head_dim).transpose(1, 2)
65
+ S_base = (q_h @ k_h.transpose(-2, -1)) / math.sqrt(self.head_dim)
66
+
67
+ q_k_hadamard = q.unsqueeze(2) * k.unsqueeze(1)
68
+ S_tensor = self.hadamard_mlp(q_k_hadamard).squeeze(-1).unsqueeze(1)
69
+
70
+ g_q = torch.sigmoid(self.trinity_g(q))
71
+ g_k = torch.sigmoid(self.trinity_g(k))
72
+ S_trinity = (g_q @ g_k.transpose(-2, -1)).unsqueeze(1)
73
+
74
+ B_material = F.relu(mat_diff_matrix).unsqueeze(1) * self.threat_weight
75
+
76
+ tau = torch.sigmoid(self.temp_mlp(x.mean(dim=1))) * 0.5 + 0.75
77
+ tau = tau.unsqueeze(-1).unsqueeze(-1)
78
+
79
+ S_total = (S_base + S_tensor + S_trinity + B_material) / tau
80
+ A = F.softmax(S_total, dim=-1)
81
+ out = (A @ v_h).transpose(1, 2).reshape(B, N, C)
82
+ return self.out_proj(out)
83
+
84
+ class DumbFractalFFN(nn.Module):
85
+ def __init__(self, dim=192, hidden_dim=288):
86
+ super().__init__()
87
+ self.w1 = nn.Linear(dim, hidden_dim, bias=False)
88
+ self.w2 = nn.Linear(dim, hidden_dim, bias=False)
89
+ self.w3 = nn.Linear(hidden_dim, dim, bias=False)
90
+
91
+ def forward(self, x):
92
+ h1 = F.silu(self.w1(x))
93
+ h2 = self.w2(x)
94
+ chunk_size = h2.shape[-1] // 2
95
+ h2_a, h2_b = torch.split(h2, chunk_size, dim=-1)
96
+ fractal_interaction = torch.cat([h2_a * h2_b, h2_b**2], dim=-1)
97
+ return self.w3(h1 * fractal_interaction)
98
+
99
+ class DumbBlock(nn.Module):
100
+ def __init__(self, dim=192):
101
+ super().__init__()
102
+ self.norm1 = nn.LayerNorm(dim)
103
+ self.attn = DumbAttention(dim=dim)
104
+ self.norm2 = nn.LayerNorm(dim)
105
+ self.ffn = DumbFractalFFN(dim=dim)
106
+ self.gate = nn.Parameter(torch.ones(1) * 0.1)
107
+
108
+ def forward(self, x, mat_diff_matrix):
109
+ x = x + self.gate * self.attn(self.norm1(x), mat_diff_matrix)
110
+ x = x + self.gate * self.ffn(self.norm2(x))
111
+ return x
112
+
113
+ class DumbcPreTrainedModel(PreTrainedModel):
114
+ config_class = DumbcConfig
115
+ base_model_prefix = "dumbc"
116
+
117
+ def _init_weights(self, module):
118
+ if isinstance(module, (nn.Linear, nn.Conv1d)):
119
+ module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
120
+ if module.bias is not None:
121
+ module.bias.data.zero_()
122
+
123
+ class DumbcModel(DumbcPreTrainedModel):
124
+ def __init__(self, config):
125
+ super().__init__(config)
126
+ self.config = config
127
+ self.retina = DumbChessRetina(dim=config.dim)
128
+ self.layers = nn.ModuleList([DumbBlock(dim=config.dim) for _ in range(config.num_unique_layers)])
129
+ self.step_embed = nn.Parameter(torch.randn(config.num_loops, 1, 1, config.dim) * 0.02)
130
+
131
+ self.from_head = nn.Linear(config.dim, 64)
132
+ self.to_head = nn.Linear(config.dim, 64)
133
+ self.value_head = nn.Sequential(
134
+ nn.Linear(config.dim, 64),
135
+ nn.GELU(),
136
+ nn.Linear(64, 3)
137
+ )
138
+ self.post_init()
139
+
140
+ def forward(self, board_state, mat_diff_matrix, material_weights, **kwargs):
141
+ x = self.retina(board_state, material_weights)
142
+ for loop_idx in range(self.config.num_loops):
143
+ x = x + self.step_embed[loop_idx]
144
+ for layer in self.layers:
145
+ x = layer(x, mat_diff_matrix)
146
+
147
+ from_logits = self.from_head(x)
148
+ to_logits = self.to_head(x)
149
+ policy_matrix = torch.bmm(from_logits, to_logits.transpose(1, 2))
150
+
151
+ global_pool = x.mean(dim=1)
152
+ value_logits = self.value_head(global_pool)
153
+
154
+ return {"policy_matrix": policy_matrix, "value_logits": value_logits}
tokenizer.json ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "version": "1.0",
3
+ "truncation": null,
4
+ "padding": null,
5
+ "added_tokens": [],
6
+ "normalizer": null,
7
+ "pre_tokenizer": {
8
+ "type": "Whitespace"
9
+ },
10
+ "post_processor": null,
11
+ "decoder": null,
12
+ "model": {
13
+ "type": "WordLevel",
14
+ "vocab": {
15
+ "[PAD]": 0,
16
+ "[UNK]": 1,
17
+ "[BOS]": 2,
18
+ "[EOS]": 3,
19
+ "P": 4,
20
+ "N": 5,
21
+ "B": 6,
22
+ "R": 7,
23
+ "Q": 8,
24
+ "K": 9,
25
+ "p": 10,
26
+ "n": 11,
27
+ "b": 12,
28
+ "r": 13,
29
+ "q": 14,
30
+ "k": 15,
31
+ ".": 16,
32
+ "a1": 17,
33
+ "a2": 18,
34
+ "a3": 19,
35
+ "a4": 20,
36
+ "a5": 21,
37
+ "a6": 22,
38
+ "a7": 23,
39
+ "a8": 24,
40
+ "b1": 25,
41
+ "b2": 26,
42
+ "b3": 27,
43
+ "b4": 28,
44
+ "b5": 29,
45
+ "b6": 30,
46
+ "b7": 31,
47
+ "b8": 32,
48
+ "c1": 33,
49
+ "c2": 34,
50
+ "c3": 35,
51
+ "c4": 36,
52
+ "c5": 37,
53
+ "c6": 38,
54
+ "c7": 39,
55
+ "c8": 40,
56
+ "d1": 41,
57
+ "d2": 42,
58
+ "d3": 43,
59
+ "d4": 44,
60
+ "d5": 45,
61
+ "d6": 46,
62
+ "d7": 47,
63
+ "d8": 48,
64
+ "e1": 49,
65
+ "e2": 50,
66
+ "e3": 51,
67
+ "e4": 52,
68
+ "e5": 53,
69
+ "e6": 54,
70
+ "e7": 55,
71
+ "e8": 56,
72
+ "f1": 57,
73
+ "f2": 58,
74
+ "f3": 59,
75
+ "f4": 60,
76
+ "f5": 61,
77
+ "f6": 62,
78
+ "f7": 63,
79
+ "f8": 64,
80
+ "g1": 65,
81
+ "g2": 66,
82
+ "g3": 67,
83
+ "g4": 68,
84
+ "g5": 69,
85
+ "g6": 70,
86
+ "g7": 71,
87
+ "g8": 72,
88
+ "h1": 73,
89
+ "h2": 74,
90
+ "h3": 75,
91
+ "h4": 76,
92
+ "h5": 77,
93
+ "h6": 78,
94
+ "h7": 79,
95
+ "h8": 80
96
+ },
97
+ "unk_token": "[UNK]"
98
+ }
99
+ }
tokenizer_config.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "tokenizer_class": "PreTrainedTokenizerFast",
3
+ "unk_token": "[UNK]",
4
+ "pad_token": "[PAD]",
5
+ "bos_token": "[BOS]",
6
+ "eos_token": "[EOS]",
7
+ "model_max_length": 64
8
+ }