summerMC commited on
Commit
8736371
·
verified ·
1 Parent(s): 3957905

Upload folder using huggingface_hub

Browse files
chat_template.jinja ADDED
@@ -0,0 +1 @@
 
 
1
+ {% for message in messages %}{{'<|' + message['role'] + '|>\n' + message['content'] + '<|end|>\n'}}{% endfor %}
config.json ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "TRMTextISMForCausalLM"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "configuration_trm_text_ism.TRMTextISMConfig",
7
+ "AutoModelForCausalLM": "modeling_trm_text_ism.TRMTextISMForCausalLM"
8
+ },
9
+ "bos_token_id": 50256,
10
+ "dim": 768,
11
+ "dropout": 0.0,
12
+ "dtype": "bfloat16",
13
+ "eos_token_id": 50256,
14
+ "gate_init": -1.5,
15
+ "gate_style": "stable",
16
+ "head_dim": 64,
17
+ "max_seq_len": 512,
18
+ "mlp_hidden_size": null,
19
+ "mlp_ratio": 2.6666666667,
20
+ "model_type": "trm_text_ism",
21
+ "n_heads": 12,
22
+ "num_hidden_layers": 1,
23
+ "pad_token_id": 50256,
24
+ "recurrence_steps": 4,
25
+ "residual_scale": 0.5,
26
+ "tie_word_embeddings": true,
27
+ "transformers_version": "5.10.2",
28
+ "use_cache": false,
29
+ "vocab_size": 50259
30
+ }
configuration_trm_text_ism.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from transformers import PretrainedConfig
3
+ class TRMTextISMConfig(PretrainedConfig):
4
+ model_type = "trm_text_ism"
5
+ def __init__(self, vocab_size=50257, max_seq_len=512, dim=768, n_heads=12, head_dim=64, recurrence_steps=4, mlp_ratio=2.6666666667, mlp_hidden_size=2048, dropout=0.0, gate_style="stable", gate_init=-1.5, residual_scale=0.5, tie_word_embeddings=True, **kwargs):
6
+ super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs)
7
+ self.vocab_size, self.max_seq_len, self.dim, self.n_heads, self.head_dim = vocab_size, max_seq_len, dim, n_heads, head_dim
8
+ self.recurrence_steps, self.mlp_ratio, self.mlp_hidden_size, self.dropout = recurrence_steps, mlp_ratio, mlp_hidden_size, dropout
9
+ self.gate_style, self.gate_init, self.residual_scale = gate_style, gate_init, residual_scale
10
+ self.num_hidden_layers = 1
generation_config.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 50256,
3
+ "do_sample": true,
4
+ "eos_token_id": [
5
+ 50256,
6
+ 50256
7
+ ],
8
+ "max_new_tokens": 128,
9
+ "pad_token_id": 50256,
10
+ "temperature": 0.8,
11
+ "top_k": 50,
12
+ "top_p": 0.95,
13
+ "transformers_version": "5.10.2"
14
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cebfc9837ba38cf64a8406db683862f844b5ccf199a5ce4f759e6e6bcc887d45
3
+ size 168691448
modeling_trm_text_ism.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ from transformers import PreTrainedModel
5
+ from transformers.generation import GenerationMixin
6
+ from transformers.modeling_outputs import CausalLMOutputWithPast
7
+ from .configuration_trm_text_ism import TRMTextISMConfig
8
+
9
+ def apply_rope(x, cos, sin):
10
+ S = x.shape[2]
11
+ c, s = cos[:, :, :S, :].to(x.dtype), sin[:, :, :S, :].to(x.dtype)
12
+ x1, x2 = x[..., :x.shape[-1]//2], x[..., x.shape[-1]//2:]
13
+ return torch.cat([x1 * c - x2 * s, x2 * c + x1 * s], dim=-1)
14
+
15
+ class SwiGLUMLP(nn.Module):
16
+ def __init__(self, config):
17
+ super().__init__()
18
+ h = config.mlp_hidden_size or int(config.dim * config.mlp_ratio)
19
+ self.gate_proj = nn.Linear(config.dim, h, bias=False)
20
+ self.up_proj = nn.Linear(config.dim, h, bias=False)
21
+ self.down_proj = nn.Linear(h, config.dim, bias=False)
22
+ def forward(self, x): return self.down_proj(F.silu(self.gate_proj(x)) * self.up_proj(x))
23
+
24
+ class TRMAttention(nn.Module):
25
+ def __init__(self, config):
26
+ super().__init__()
27
+ self.n_heads, self.head_dim = config.n_heads, config.head_dim
28
+ self.qkv = nn.Linear(config.dim, 3*config.dim, bias=False)
29
+ self.out = nn.Linear(config.dim, config.dim, bias=False)
30
+ def forward(self, x, mask, cos, sin):
31
+ B, S, _ = x.shape
32
+ q, k, v = self.qkv(x).chunk(3, dim=-1)
33
+ q, k, v = [t.view(B, S, self.n_heads, self.head_dim).transpose(1, 2) for t in (q, k, v)]
34
+ q, k = apply_rope(q, cos, sin), apply_rope(k, cos, sin)
35
+ y = F.scaled_dot_product_attention(q, k, v, attn_mask=mask[:, None, :, :])
36
+ return self.out(y.transpose(1, 2).reshape(B, S, -1))
37
+
38
+ class TRMBlock(nn.Module):
39
+ def __init__(self, config):
40
+ super().__init__()
41
+ self.res = config.residual_scale
42
+ self.norm1 = nn.RMSNorm(config.dim)
43
+ self.attn = TRMAttention(config)
44
+ self.norm2 = nn.RMSNorm(config.dim)
45
+ self.mlp = SwiGLUMLP(config)
46
+ self.attn_gate = nn.Parameter(torch.ones(config.dim))
47
+ self.mlp_gate = nn.Parameter(torch.ones(config.dim))
48
+ def forward(self, x, mask, c, s):
49
+ x = x + self.res * torch.sigmoid(self.attn_gate).view(1,1,-1) * self.attn(self.norm1(x), mask, c, s)
50
+ return x + self.res * torch.sigmoid(self.mlp_gate).view(1,1,-1) * self.mlp(self.norm2(x))
51
+
52
+ class TRMTextISMForCausalLM(PreTrainedModel, GenerationMixin):
53
+ config_class = TRMTextISMConfig
54
+
55
+ def __init__(self, config):
56
+ super().__init__(config)
57
+ self.token_emb = nn.Embedding(config.vocab_size, config.dim)
58
+ self.block = TRMBlock(config)
59
+ self.norm = nn.RMSNorm(config.dim)
60
+ self.lm_head = nn.Linear(config.dim, config.vocab_size, bias=False)
61
+ pos = torch.arange(config.max_seq_len).float()
62
+ theta = 1.0 / (10000.0 ** (torch.arange(0, config.head_dim//2).float() / (config.head_dim//2)))
63
+ f = torch.outer(pos, theta)
64
+ self.register_buffer("rope_cos", f.cos().view(1, 1, config.max_seq_len, -1))
65
+ self.register_buffer("rope_sin", f.sin().view(1, 1, config.max_seq_len, -1))
66
+ self.post_init()
67
+
68
+ def get_input_embeddings(self):
69
+ return self.token_emb
70
+
71
+ def set_input_embeddings(self, value):
72
+ self.token_emb = value
73
+
74
+ def get_output_embeddings(self):
75
+ return self.lm_head
76
+
77
+ def set_output_embeddings(self, value):
78
+ self.lm_head = value
79
+
80
+ # tie_weights は削除 → lm_head と token_emb を独立したweightとして保持
81
+
82
+ def prepare_inputs_for_generation(self, input_ids, attention_mask=None, **kwargs):
83
+ return {"input_ids": input_ids, "attention_mask": attention_mask, "use_cache": False}
84
+
85
+ def forward(self, input_ids, attention_mask=None, **kwargs):
86
+ B, S = input_ids.shape
87
+ x = self.token_emb(input_ids)
88
+ m = torch.tril(torch.ones(S, S, device=input_ids.device)).bool().unsqueeze(0).expand(B, -1, -1)
89
+ if attention_mask is not None:
90
+ m = m & attention_mask[:, None, :].bool()
91
+ c, s = self.rope_cos, self.rope_sin
92
+ for _ in range(self.config.recurrence_steps):
93
+ x = self.block(x, m, c, s)
94
+ logits = self.lm_head(self.norm(x))
95
+ return CausalLMOutputWithPast(logits=logits)
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "backend": "tokenizers",
4
+ "bos_token": "<|endoftext|>",
5
+ "eos_token": "<|endoftext|>",
6
+ "errors": "replace",
7
+ "extra_special_tokens": [
8
+ "<|im_start|>",
9
+ "<|im_end|>"
10
+ ],
11
+ "is_local": true,
12
+ "local_files_only": false,
13
+ "max_length": 256,
14
+ "model_max_length": 1024,
15
+ "pad_to_multiple_of": null,
16
+ "pad_token": "<|endoftext|>",
17
+ "pad_token_type_id": 0,
18
+ "padding_side": "right",
19
+ "stride": 0,
20
+ "tokenizer_class": "GPT2Tokenizer",
21
+ "truncation_side": "right",
22
+ "truncation_strategy": "longest_first",
23
+ "unk_token": "<|endoftext|>"
24
+ }