summerMC commited on
Commit
01636ca
·
verified ·
1 Parent(s): 4084e4a

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.9.0",
28
+ "use_cache": false,
29
+ "vocab_size": 50257
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.9.0"
14
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3654eea5d60bd4614874eabab92f00b295f90ac3b4561c5a08668d8f7aedafe2
3
+ size 168691448
modeling_trm_text_ism.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import torch.nn as nn
4
+ import torch.nn.functional as F
5
+ from transformers import PreTrainedModel
6
+ from transformers.generation import GenerationMixin
7
+ from transformers.modeling_outputs import CausalLMOutputWithPast
8
+ from .configuration_trm_text_ism import TRMTextISMConfig
9
+
10
+ def apply_rope(x, cos, sin):
11
+ S = x.shape[2]
12
+ c, s = cos[:, :, :S, :].to(x.dtype), sin[:, :, :S, :].to(x.dtype)
13
+ x1, x2 = x[..., :x.shape[-1]//2], x[..., x.shape[-1]//2:]
14
+ return torch.cat([x1 * c - x2 * s, x2 * c + x1 * s], dim=-1)
15
+
16
+ class SwiGLUMLP(nn.Module):
17
+ def __init__(self, config):
18
+ super().__init__()
19
+ h = config.mlp_hidden_size or int(config.dim * config.mlp_ratio)
20
+ self.gate_proj = nn.Linear(config.dim, h, bias=False)
21
+ self.up_proj = nn.Linear(config.dim, h, bias=False)
22
+ self.down_proj = nn.Linear(h, config.dim, bias=False)
23
+ def forward(self, x): return self.down_proj(F.silu(self.gate_proj(x)) * self.up_proj(x))
24
+
25
+ class TRMAttention(nn.Module):
26
+ def __init__(self, config):
27
+ super().__init__()
28
+ self.n_heads, self.head_dim = config.n_heads, config.head_dim
29
+ self.qkv = nn.Linear(config.dim, 3*config.dim, bias=False)
30
+ self.out = nn.Linear(config.dim, config.dim, bias=False)
31
+ def forward(self, x, mask, cos, sin):
32
+ B, S, _ = x.shape
33
+ q, k, v = self.qkv(x).chunk(3, dim=-1)
34
+ q, k, v = [t.view(B, S, self.n_heads, self.head_dim).transpose(1, 2) for t in (q, k, v)]
35
+ q, k = apply_rope(q, cos, sin), apply_rope(k, cos, sin)
36
+ y = F.scaled_dot_product_attention(q, k, v, attn_mask=mask[:, None, :, :])
37
+ return self.out(y.transpose(1, 2).reshape(B, S, -1))
38
+
39
+ class TRMBlock(nn.Module):
40
+ def __init__(self, config):
41
+ super().__init__()
42
+ self.res = config.residual_scale
43
+ self.norm1 = nn.RMSNorm(config.dim)
44
+ self.attn = TRMAttention(config)
45
+ self.norm2 = nn.RMSNorm(config.dim)
46
+ self.mlp = SwiGLUMLP(config)
47
+ self.attn_gate = nn.Parameter(torch.ones(config.dim))
48
+ self.mlp_gate = nn.Parameter(torch.ones(config.dim))
49
+ def forward(self, x, mask, c, s):
50
+ x = x + self.res * torch.sigmoid(self.attn_gate).view(1,1,-1) * self.attn(self.norm1(x), mask, c, s)
51
+ return x + self.res * torch.sigmoid(self.mlp_gate).view(1,1,-1) * self.mlp(self.norm2(x))
52
+
53
+ class TRMTextISMForCausalLM(PreTrainedModel, GenerationMixin):
54
+ config_class = TRMTextISMConfig
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
+ def get_input_embeddings(self): return self.token_emb
68
+ def get_output_embeddings(self): return self.lm_head
69
+ def tie_weights(self, *args, **kwargs):
70
+ if hasattr(self, 'lm_head'): self.lm_head.weight = self.token_emb.weight
71
+ def prepare_inputs_for_generation(self, input_ids, attention_mask=None, **kwargs):
72
+ return {"input_ids": input_ids, "attention_mask": attention_mask, "use_cache": False}
73
+ def forward(self, input_ids, attention_mask=None, **kwargs):
74
+ B, S = input_ids.shape
75
+ x = self.token_emb(input_ids)
76
+ m = torch.tril(torch.ones(S, S, device=input_ids.device)).bool().unsqueeze(0).expand(B, -1, -1)
77
+ if attention_mask is not None: m = m & attention_mask[:, None, :].bool()
78
+ c, s = self.rope_cos, self.rope_sin
79
+ for _ in range(self.config.recurrence_steps):
80
+ x = self.block(x, m, c, s)
81
+ logits = self.lm_head(self.norm(x))
82
+ 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,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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": false,
12
+ "local_files_only": false,
13
+ "model_max_length": 1024,
14
+ "pad_token": "<|endoftext|>",
15
+ "tokenizer_class": "GPT2Tokenizer",
16
+ "unk_token": "<|endoftext|>"
17
+ }
training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4a890963eda01ff5a4e105510bc22992df7bd6714a4f50cbd20f9ab23fa7f65e
3
+ size 5265