Text Generation
Transformers
Safetensors
English
trm_text_ism
trm-text
ism
recurrent-transformer
tiny-stories
conversational
custom_code
Instructions to use summerMC/TRM-textV2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use summerMC/TRM-textV2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="summerMC/TRM-textV2", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("summerMC/TRM-textV2", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use summerMC/TRM-textV2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "summerMC/TRM-textV2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "summerMC/TRM-textV2", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/summerMC/TRM-textV2
- SGLang
How to use summerMC/TRM-textV2 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "summerMC/TRM-textV2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "summerMC/TRM-textV2", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "summerMC/TRM-textV2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "summerMC/TRM-textV2", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use summerMC/TRM-textV2 with Docker Model Runner:
docker model run hf.co/summerMC/TRM-textV2
v29: Strict weight key alignment (norm1, norm2, attn_gate, mlp_gate)
Browse files- configuration_trm_text_ism.py +1 -1
- modeling_trm_text_ism.py +13 -8
configuration_trm_text_ism.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 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=
|
| 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
|
|
|
|
| 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
|
modeling_trm_text_ism.py
CHANGED
|
@@ -17,7 +17,8 @@ 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
|
|
|
|
| 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 |
|
|
@@ -25,7 +26,8 @@ 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
|
|
|
|
| 29 |
def forward(self, x, mask, cos, sin):
|
| 30 |
B, S, _ = x.shape
|
| 31 |
q, k, v = self.qkv(x).chunk(3, dim=-1)
|
|
@@ -38,10 +40,10 @@ class TRMBlock(nn.Module):
|
|
| 38 |
def __init__(self, config):
|
| 39 |
super().__init__()
|
| 40 |
self.res = config.residual_scale
|
| 41 |
-
|
| 42 |
-
self.
|
| 43 |
-
self.
|
| 44 |
-
|
| 45 |
self.attn_gate = nn.Parameter(torch.ones(config.dim))
|
| 46 |
self.mlp_gate = nn.Parameter(torch.ones(config.dim))
|
| 47 |
def forward(self, x, mask, c, s):
|
|
@@ -52,7 +54,9 @@ class TRMTextISMForCausalLM(PreTrainedModel, GenerationMixin):
|
|
| 52 |
config_class = TRMTextISMConfig
|
| 53 |
def __init__(self, config):
|
| 54 |
super().__init__(config)
|
| 55 |
-
self.token_emb
|
|
|
|
|
|
|
| 56 |
self.lm_head = nn.Linear(config.dim, config.vocab_size, bias=False)
|
| 57 |
pos = torch.arange(config.max_seq_len).float()
|
| 58 |
theta = 1.0 / (10000.0 ** (torch.arange(0, config.head_dim//2).float() / (config.head_dim//2)))
|
|
@@ -71,7 +75,8 @@ class TRMTextISMForCausalLM(PreTrainedModel, GenerationMixin):
|
|
| 71 |
x = self.token_emb(input_ids)
|
| 72 |
m = torch.tril(torch.ones(S, S, device=input_ids.device)).bool().unsqueeze(0).expand(B, -1, -1)
|
| 73 |
if attention_mask is not None: m = m & attention_mask[:, None, :].bool()
|
|
|
|
| 74 |
for _ in range(self.config.recurrence_steps):
|
| 75 |
-
x = self.block(x, m,
|
| 76 |
logits = self.lm_head(self.norm(x))
|
| 77 |
return CausalLMOutputWithPast(logits=logits)
|
|
|
|
| 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 |
|
|
|
|
| 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)
|
|
|
|
| 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):
|
|
|
|
| 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)))
|
|
|
|
| 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)
|