MDaytek commited on
Commit
eec4ee4
·
verified ·
1 Parent(s): 7dc9b8a

Manual Force Upload

Browse files
__init__.py ADDED
File without changes
__pycache__/configuration_chess.cpython-312.pyc ADDED
Binary file (1.05 kB). View file
 
__pycache__/modeling_chess.cpython-312.pyc ADDED
Binary file (5.4 kB). View file
 
configuration_chess.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from transformers import PretrainedConfig
3
+
4
+ class ChessConfig(PretrainedConfig):
5
+ model_type = "chess"
6
+
7
+ def __init__(
8
+ self,
9
+ vocab_size=1340,
10
+ n_embd=128,
11
+ n_layer=6,
12
+ n_head=8,
13
+ n_inner=256,
14
+ n_ctx=256,
15
+ **kwargs
16
+ ):
17
+ super().__init__(**kwargs)
18
+ self.vocab_size = vocab_size
19
+ self.n_embd = n_embd
20
+ self.n_layer = n_layer
21
+ self.n_head = n_head
22
+ self.n_inner = n_inner
23
+ self.n_ctx = n_ctx
24
+ self.num_hidden_layers = n_layer
25
+ self.hidden_size = n_embd
26
+ self.num_attention_heads = n_head
modeling_chess.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import torch.nn as nn
4
+ from transformers import PreTrainedModel, GenerationMixin
5
+ from transformers.modeling_outputs import CausalLMOutput
6
+ from .configuration_chess import ChessConfig
7
+
8
+ class Block(nn.Module):
9
+ def __init__(self, config):
10
+ super().__init__()
11
+ self.ln1 = nn.LayerNorm(config.n_embd)
12
+ self.attn = nn.MultiheadAttention(config.n_embd, config.n_head, batch_first=True)
13
+ self.ln2 = nn.LayerNorm(config.n_embd)
14
+ self.mlp = nn.Sequential(
15
+ nn.Linear(config.n_embd, config.n_inner),
16
+ nn.GELU(),
17
+ nn.Linear(config.n_inner, config.n_embd)
18
+ )
19
+
20
+ def forward(self, x, mask=None):
21
+ attn_out, _ = self.attn(self.ln1(x), self.ln1(x), self.ln1(x), attn_mask=mask, need_weights=False)
22
+ return x + attn_out + self.mlp(self.ln2(x + attn_out))
23
+
24
+ class ChessForCausalLM(PreTrainedModel, GenerationMixin):
25
+ config_class = ChessConfig
26
+
27
+ def __init__(self, config):
28
+ super().__init__(config)
29
+ self.config = config
30
+ self.token_emb = nn.Embedding(config.vocab_size, config.n_embd)
31
+ self.pos_emb = nn.Embedding(config.n_ctx, config.n_embd)
32
+ self.blocks = nn.ModuleList([Block(config) for _ in range(config.n_layer)])
33
+ self.ln_f = nn.LayerNorm(config.n_embd)
34
+ self.lm_head = nn.Linear(config.n_embd, config.vocab_size, bias=False)
35
+
36
+ # Weight Tying (Important pour le comptage param)
37
+ self.lm_head.weight = self.token_emb.weight
38
+
39
+ self.apply(self._init_weights)
40
+
41
+ def _init_weights(self, module):
42
+ if isinstance(module, (nn.Linear, nn.Embedding)):
43
+ torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
44
+
45
+ def prepare_inputs_for_generation(self, input_ids, **kwargs):
46
+ return {"input_ids": input_ids}
47
+
48
+ def forward(self, input_ids, attention_mask=None, labels=None, **kwargs):
49
+ B, T = input_ids.shape
50
+ x = self.token_emb(input_ids) + self.pos_emb(torch.arange(T, device=input_ids.device))
51
+ mask = torch.triu(torch.ones(T, T, device=input_ids.device) * float('-inf'), diagonal=1)
52
+ for block in self.blocks: x = block(x, mask=mask)
53
+ logits = self.lm_head(self.ln_f(x))
54
+ loss = None
55
+ if labels is not None:
56
+ loss = nn.CrossEntropyLoss(ignore_index=-100)(logits.view(-1, logits.size(-1)), labels.view(-1))
57
+ return CausalLMOutput(loss=loss, logits=logits)