Text Generation
Transformers
PyTorch
Safetensors
Tagalog
custom_henyo_culturax
custom-architecture
henyo
Instructions to use MaAIos/Henyo-153M with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use MaAIos/Henyo-153M with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="MaAIos/Henyo-153M")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("MaAIos/Henyo-153M", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use MaAIos/Henyo-153M with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "MaAIos/Henyo-153M" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "MaAIos/Henyo-153M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/MaAIos/Henyo-153M
- SGLang
How to use MaAIos/Henyo-153M 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 "MaAIos/Henyo-153M" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "MaAIos/Henyo-153M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'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 "MaAIos/Henyo-153M" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "MaAIos/Henyo-153M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use MaAIos/Henyo-153M with Docker Model Runner:
docker model run hf.co/MaAIos/Henyo-153M
Upload train_henyo.py with huggingface_hub
Browse files- train_henyo.py +138 -0
train_henyo.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import math
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import torch.nn.functional as F
|
| 6 |
+
from transformers import AutoTokenizer, Trainer, TrainingArguments, PreTrainedModel, PretrainedConfig
|
| 7 |
+
from datasets import load_dataset, IterableDataset
|
| 8 |
+
|
| 9 |
+
# Configuration
|
| 10 |
+
class ModelConfig(PretrainedConfig):
|
| 11 |
+
model_type = "custom_henyo_culturax"
|
| 12 |
+
def __init__(
|
| 13 |
+
self,
|
| 14 |
+
vocab_size=50257,
|
| 15 |
+
dim=768,
|
| 16 |
+
n_layers=12,
|
| 17 |
+
n_heads=12,
|
| 18 |
+
n_kv_heads=4,
|
| 19 |
+
multiple_of=256,
|
| 20 |
+
max_seq_len=1024,
|
| 21 |
+
dropout=0.05,
|
| 22 |
+
**kwargs
|
| 23 |
+
):
|
| 24 |
+
super().__init__(**kwargs)
|
| 25 |
+
self.vocab_size = vocab_size
|
| 26 |
+
self.dim = dim
|
| 27 |
+
self.n_layers = n_layers
|
| 28 |
+
self.n_heads = n_heads
|
| 29 |
+
self.n_kv_heads = n_kv_heads
|
| 30 |
+
self.multiple_of = multiple_of
|
| 31 |
+
self.max_seq_len = max_seq_len
|
| 32 |
+
self.dropout = dropout
|
| 33 |
+
self.head_dim = dim // n_heads
|
| 34 |
+
|
| 35 |
+
# Architecture Components
|
| 36 |
+
class RMSNorm(nn.Module):
|
| 37 |
+
def __init__(self, dim, eps=1e-6):
|
| 38 |
+
super().__init__()
|
| 39 |
+
self.eps = eps
|
| 40 |
+
self.weight = nn.Parameter(torch.ones(dim))
|
| 41 |
+
def _norm(self, x):
|
| 42 |
+
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
|
| 43 |
+
def forward(self, x):
|
| 44 |
+
return self._norm(x.float()).type_as(x) * self.weight
|
| 45 |
+
|
| 46 |
+
def precompute_freqs_cis(dim: int, end: int, theta: float = 10000.0):
|
| 47 |
+
freqs = 1.0 / (theta ** (torch.arange(0, dim, 2)[: (dim // 2)].float() / dim))
|
| 48 |
+
t = torch.arange(end, device=freqs.device)
|
| 49 |
+
freqs = torch.outer(t, freqs).float()
|
| 50 |
+
return torch.polar(torch.ones_like(freqs), freqs)
|
| 51 |
+
|
| 52 |
+
def apply_rotary_emb(xq, xk, freqs_cis):
|
| 53 |
+
xq_ = torch.view_as_complex(xq.float().reshape(*xq.shape[:-1], -1, 2))
|
| 54 |
+
xk_ = torch.view_as_complex(xk.float().reshape(*xk.shape[:-1], -1, 2))
|
| 55 |
+
freqs_cis = freqs_cis.unsqueeze(0).unsqueeze(0)
|
| 56 |
+
xq_out = torch.view_as_real(xq_ * freqs_cis).flatten(3)
|
| 57 |
+
xk_out = torch.view_as_real(xk_ * freqs_cis).flatten(3)
|
| 58 |
+
return xq_out.type_as(xq), xk_out.type_as(xk)
|
| 59 |
+
|
| 60 |
+
class GroupedQueryAttention(nn.Module):
|
| 61 |
+
def __init__(self, args: ModelConfig):
|
| 62 |
+
super().__init__()
|
| 63 |
+
self.n_heads = args.n_heads
|
| 64 |
+
self.n_kv_heads = args.n_kv_heads
|
| 65 |
+
self.head_dim = args.head_dim
|
| 66 |
+
self.n_rep = self.n_heads // args.n_kv_heads
|
| 67 |
+
self.wq = nn.Linear(args.dim, args.n_heads * self.head_dim, bias=False)
|
| 68 |
+
self.wk = nn.Linear(args.dim, args.n_kv_heads * self.head_dim, bias=False)
|
| 69 |
+
self.wv = nn.Linear(args.dim, args.n_kv_heads * self.head_dim, bias=False)
|
| 70 |
+
self.wo = nn.Linear(args.n_heads * self.head_dim, args.dim, bias=False)
|
| 71 |
+
self.dropout = nn.Dropout(args.dropout)
|
| 72 |
+
|
| 73 |
+
def forward(self, x, freqs_cis, mask=None):
|
| 74 |
+
b, s, _ = x.shape
|
| 75 |
+
xq, xk, xv = self.wq(x), self.wk(x), self.wv(x)
|
| 76 |
+
xq = xq.view(b, s, self.n_heads, self.head_dim).transpose(1, 2)
|
| 77 |
+
xk = xk.view(b, s, self.n_kv_heads, self.head_dim).transpose(1, 2)
|
| 78 |
+
xv = xv.view(b, s, self.n_kv_heads, self.head_dim).transpose(1, 2)
|
| 79 |
+
xq, xk = apply_rotary_emb(xq, xk, freqs_cis)
|
| 80 |
+
if self.n_rep > 1:
|
| 81 |
+
xk = xk.repeat_interleave(self.n_rep, dim=1)
|
| 82 |
+
xv = xv.repeat_interleave(self.n_rep, dim=1)
|
| 83 |
+
output = F.scaled_dot_product_attention(xq, xk, xv, attn_mask=mask, dropout_p=self.dropout.p if self.training else 0.0, is_causal=True)
|
| 84 |
+
return self.wo(output.transpose(1, 2).contiguous().view(b, s, -1))
|
| 85 |
+
|
| 86 |
+
class SwiGLU(nn.Module):
|
| 87 |
+
def __init__(self, args: ModelConfig):
|
| 88 |
+
super().__init__()
|
| 89 |
+
hidden_dim = 4 * args.dim
|
| 90 |
+
hidden_dim = int(2 * hidden_dim / 3)
|
| 91 |
+
hidden_dim = args.multiple_of * ((hidden_dim + args.multiple_of - 1) // args.multiple_of)
|
| 92 |
+
self.w1 = nn.Linear(args.dim, hidden_dim, bias=False)
|
| 93 |
+
self.w2 = nn.Linear(hidden_dim, args.dim, bias=False)
|
| 94 |
+
self.w3 = nn.Linear(args.dim, hidden_dim, bias=False)
|
| 95 |
+
def forward(self, x):
|
| 96 |
+
return self.w2(F.silu(self.w1(x)) * self.w3(x))
|
| 97 |
+
|
| 98 |
+
class TransformerBlock(nn.Module):
|
| 99 |
+
def __init__(self, args: ModelConfig):
|
| 100 |
+
super().__init__()
|
| 101 |
+
self.attention_norm = RMSNorm(args.dim)
|
| 102 |
+
self.attention = GroupedQueryAttention(args)
|
| 103 |
+
self.ffn_norm = RMSNorm(args.dim)
|
| 104 |
+
self.feed_forward = SwiGLU(args)
|
| 105 |
+
def forward(self, x, freqs_cis, mask=None):
|
| 106 |
+
x = x + self.attention(self.attention_norm(x), freqs_cis, mask)
|
| 107 |
+
x = x + self.feed_forward(self.ffn_norm(x))
|
| 108 |
+
return x
|
| 109 |
+
|
| 110 |
+
class HenyoModel(PreTrainedModel):
|
| 111 |
+
config_class = ModelConfig
|
| 112 |
+
def __init__(self, config):
|
| 113 |
+
super().__init__(config)
|
| 114 |
+
self.config = config
|
| 115 |
+
self.tok_embeddings = nn.Embedding(config.vocab_size, config.dim)
|
| 116 |
+
self.layers = nn.ModuleList([TransformerBlock(config) for _ in range(config.n_layers)])
|
| 117 |
+
self.norm = RMSNorm(config.dim)
|
| 118 |
+
self.output = nn.Linear(config.dim, config.vocab_size, bias=False)
|
| 119 |
+
self.output.weight = self.tok_embeddings.weight
|
| 120 |
+
self.freqs_cis = precompute_freqs_cis(config.dim // config.n_heads, config.max_seq_len * 2)
|
| 121 |
+
|
| 122 |
+
def forward(self, input_ids, labels=None, **kwargs):
|
| 123 |
+
b, s = input_ids.shape
|
| 124 |
+
h = self.tok_embeddings(input_ids)
|
| 125 |
+
freqs_cis = self.freqs_cis[:s].to(h.device)
|
| 126 |
+
mask = None
|
| 127 |
+
if not hasattr(F, 'scaled_dot_product_attention'):
|
| 128 |
+
mask = torch.triu(torch.full((s, s), float("-inf"), device=h.device), diagonal=1)
|
| 129 |
+
for layer in self.layers:
|
| 130 |
+
h = layer(h, freqs_cis, mask)
|
| 131 |
+
h = self.norm(h)
|
| 132 |
+
logits = self.output(h)
|
| 133 |
+
loss = None
|
| 134 |
+
if labels is not None:
|
| 135 |
+
shift_logits = logits[..., :-1, :].contiguous().view(-1, self.config.vocab_size)
|
| 136 |
+
shift_labels = labels[..., 1:].contiguous().view(-1)
|
| 137 |
+
loss = F.cross_entropy(shift_logits, shift_labels)
|
| 138 |
+
return {"loss": loss, "logits": logits} if loss is not None else {"logits": logits}
|