Text Generation
Transformers
Safetensors
Russian
English
ppai
pacperlar
mixture-of-experts
russian
code
small-language-model
custom_code
Instructions to use AdminHugging/PP-Hydrogen with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use AdminHugging/PP-Hydrogen with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="AdminHugging/PP-Hydrogen", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("AdminHugging/PP-Hydrogen", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use AdminHugging/PP-Hydrogen with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "AdminHugging/PP-Hydrogen" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "AdminHugging/PP-Hydrogen", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/AdminHugging/PP-Hydrogen
- SGLang
How to use AdminHugging/PP-Hydrogen 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 "AdminHugging/PP-Hydrogen" \ --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": "AdminHugging/PP-Hydrogen", "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 "AdminHugging/PP-Hydrogen" \ --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": "AdminHugging/PP-Hydrogen", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use AdminHugging/PP-Hydrogen with Docker Model Runner:
docker model run hf.co/AdminHugging/PP-Hydrogen
Upload folder using huggingface_hub
Browse files- configuration_ppai.py +12 -8
- model.safetensors +1 -1
- modeling_ppai.py +57 -30
configuration_ppai.py
CHANGED
|
@@ -73,15 +73,19 @@ class PPAIConfig(PretrainedConfig):
|
|
| 73 |
|
| 74 |
|
| 75 |
def param_report(c: PPAIConfig) -> dict:
|
| 76 |
-
"""Посчитать параметры без создания модели."""
|
| 77 |
# wq + wk + wv + wo
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
emb = (c.vocab_size * c.d_model + len(c.ngram_sizes) * c.ngram_buckets * c.ngram_dim
|
| 84 |
+ c.d_model * len(c.ngram_sizes) * c.ngram_dim + c.d_model + c.d_model)
|
| 85 |
-
total = c.n_layers * (
|
| 86 |
-
active = c.n_layers * (
|
| 87 |
return {"total": total, "active": active, "embedding": emb}
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
def param_report(c: PPAIConfig) -> dict:
|
| 76 |
+
"""Посчитать параметры без создания модели (moE и dense)."""
|
| 77 |
# wq + wk + wv + wo
|
| 78 |
+
per_layer = 2 * c.n_heads * c.head_dim * c.d_model + 2 * c.n_kv_heads * c.head_dim * c.d_model
|
| 79 |
+
per_layer += 2 * c.d_model + c.n_heads * c.head_dim + c.n_kv_heads * c.head_dim + 2 + c.n_heads * c.gate_dim
|
| 80 |
+
if c.num_experts:
|
| 81 |
+
per_layer += 2 * c.ffn_shared_dim * c.d_model + c.d_model * c.ffn_shared_dim # shared expert
|
| 82 |
+
per_layer += c.num_experts * c.d_model + c.num_experts # router
|
| 83 |
+
ffn_total = c.num_experts * (2 * c.ffn_dim * c.d_model + c.d_model * c.ffn_dim)
|
| 84 |
+
ffn_active = 2 * c.ffn_dim * c.d_model + c.d_model * c.ffn_dim # top-1
|
| 85 |
+
else:
|
| 86 |
+
ffn_total = ffn_active = 2 * c.ffn_dim * c.d_model + c.d_model * c.ffn_dim # dense, без shared
|
| 87 |
emb = (c.vocab_size * c.d_model + len(c.ngram_sizes) * c.ngram_buckets * c.ngram_dim
|
| 88 |
+ c.d_model * len(c.ngram_sizes) * c.ngram_dim + c.d_model + c.d_model)
|
| 89 |
+
total = c.n_layers * (per_layer + ffn_total) + emb
|
| 90 |
+
active = c.n_layers * (per_layer + ffn_active) + emb
|
| 91 |
return {"total": total, "active": active, "embedding": emb}
|
model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 399931944
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f199cb3a66dde338d47fbe05875cc86ee3f05462ca2563c9fb46e6a4f64db571
|
| 3 |
size 399931944
|
modeling_ppai.py
CHANGED
|
@@ -68,12 +68,18 @@ class PPAIBlock(nn.Module):
|
|
| 68 |
self.wo = nn.Parameter(torch.empty(d, hq * hd))
|
| 69 |
self.q_norm = nn.Parameter(torch.ones(hq, hd))
|
| 70 |
self.k_norm = nn.Parameter(torch.ones(hkv, hd))
|
| 71 |
-
self.router_w = nn.Parameter(torch.empty(c.num_experts, d))
|
| 72 |
-
self.router_b = nn.Parameter(torch.zeros(c.num_experts))
|
| 73 |
-
self.s13 = nn.Parameter(torch.empty(2 * c.ffn_shared_dim, d))
|
| 74 |
-
self.s2 = nn.Parameter(torch.empty(d, c.ffn_shared_dim))
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
self.lam = nn.Parameter(torch.tensor([1.0, 0.0])) # x <- lam0*x + lam1*x0
|
| 78 |
self.ag_w = nn.Parameter(torch.zeros(hq, c.gate_dim)) # per-head gate
|
| 79 |
if idx > 0:
|
|
@@ -130,9 +136,16 @@ class PPAIBlock(nn.Module):
|
|
| 130 |
o = o.transpose(1, 2) * gate.unsqueeze(-1)
|
| 131 |
x = x + F.linear(o.reshape(B, T, hq * hd), self.wo.to(dt))
|
| 132 |
m = _rms(x, self.ln2, c.rms_norm_eps)
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
return x + s + e, v_raw
|
| 137 |
|
| 138 |
|
|
@@ -149,12 +162,17 @@ class PPAIPreTrainedModel(PreTrainedModel):
|
|
| 149 |
if isinstance(module, nn.Embedding):
|
| 150 |
module.weight.normal_(0, std)
|
| 151 |
elif isinstance(module, PPAIBlock):
|
| 152 |
-
for p in (module.wq, module.wk, module.wv, module.
|
| 153 |
p.normal_(0, std)
|
| 154 |
-
|
|
|
|
|
|
|
| 155 |
p.normal_(0, ostd)
|
| 156 |
-
module.
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
| 158 |
module.ln1.fill_(1.0)
|
| 159 |
module.ln2.fill_(1.0)
|
| 160 |
module.q_norm.fill_(1.0)
|
|
@@ -181,35 +199,44 @@ class PPAIModel(PPAIPreTrainedModel):
|
|
| 181 |
self.blocks = nn.ModuleList(PPAIBlock(c, i) for i in range(c.n_layers))
|
| 182 |
self.post_init()
|
| 183 |
|
| 184 |
-
def forward(self, input_ids, past_key_values=None, use_cache=False):
|
| 185 |
c = self.config
|
| 186 |
-
|
| 187 |
-
dev =
|
| 188 |
-
# bf16 только там, где он аппаратный; на T4 (sm_75) — fp16
|
| 189 |
-
if dev.type == "
|
|
|
|
|
|
|
| 190 |
compute_dtype = torch.bfloat16
|
| 191 |
elif dev.type == "cuda":
|
| 192 |
compute_dtype = torch.float16
|
| 193 |
else:
|
| 194 |
compute_dtype = torch.float32
|
| 195 |
|
| 196 |
-
ids = input_ids.long()
|
| 197 |
if use_cache and past_key_values is None:
|
| 198 |
past_key_values = DynamicCache()
|
| 199 |
cache = past_key_values if use_cache else None
|
| 200 |
past_len = cache.get_seq_length() if cache is not None else 0
|
| 201 |
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
pos = torch.arange(past_len, past_len + T, device=dev).float()
|
| 215 |
inv = 1.0 / (c.rope_theta ** (torch.arange(0, c.head_dim, 2, device=dev).float() / c.head_dim))
|
|
|
|
| 68 |
self.wo = nn.Parameter(torch.empty(d, hq * hd))
|
| 69 |
self.q_norm = nn.Parameter(torch.ones(hq, hd))
|
| 70 |
self.k_norm = nn.Parameter(torch.ones(hkv, hd))
|
| 71 |
+
self.router_w = nn.Parameter(torch.empty(c.num_experts, d)) if c.num_experts else None
|
| 72 |
+
self.router_b = nn.Parameter(torch.zeros(c.num_experts)) if c.num_experts else None
|
| 73 |
+
self.s13 = nn.Parameter(torch.empty(2 * c.ffn_shared_dim, d)) if c.num_experts else None
|
| 74 |
+
self.s2 = nn.Parameter(torch.empty(d, c.ffn_shared_dim)) if c.num_experts else None
|
| 75 |
+
if c.num_experts:
|
| 76 |
+
# top-1 MoE: свой FFN на каждого эксперта
|
| 77 |
+
self.w13 = nn.Parameter(torch.empty(c.num_experts, 2 * c.ffn_dim, d))
|
| 78 |
+
self.w2 = nn.Parameter(torch.empty(c.num_experts, d, c.ffn_dim))
|
| 79 |
+
else:
|
| 80 |
+
# dense: один общий FFN (PP-OSR-H)
|
| 81 |
+
self.w13 = nn.Parameter(torch.empty(2 * c.ffn_dim, d))
|
| 82 |
+
self.w2 = nn.Parameter(torch.empty(d, c.ffn_dim))
|
| 83 |
self.lam = nn.Parameter(torch.tensor([1.0, 0.0])) # x <- lam0*x + lam1*x0
|
| 84 |
self.ag_w = nn.Parameter(torch.zeros(hq, c.gate_dim)) # per-head gate
|
| 85 |
if idx > 0:
|
|
|
|
| 136 |
o = o.transpose(1, 2) * gate.unsqueeze(-1)
|
| 137 |
x = x + F.linear(o.reshape(B, T, hq * hd), self.wo.to(dt))
|
| 138 |
m = _rms(x, self.ln2, c.rms_norm_eps)
|
| 139 |
+
if c.num_experts:
|
| 140 |
+
# MoE: shared-эксперт + один роутед
|
| 141 |
+
sg, su = F.linear(m, self.s13.to(dt)).chunk(2, -1)
|
| 142 |
+
s = F.linear(F.silu(sg) * su, self.s2.to(dt))
|
| 143 |
+
e = self.moe(m.reshape(B * T, d)).view(B, T, d)
|
| 144 |
+
else:
|
| 145 |
+
# dense: один общий FFN, shared-эксперт не нужен
|
| 146 |
+
h = F.linear(m, self.w13.to(dt))
|
| 147 |
+
s = torch.zeros_like(m)
|
| 148 |
+
e = F.linear(F.silu(h[..., : c.ffn_dim]) * h[..., c.ffn_dim:], self.w2.to(dt))
|
| 149 |
return x + s + e, v_raw
|
| 150 |
|
| 151 |
|
|
|
|
| 162 |
if isinstance(module, nn.Embedding):
|
| 163 |
module.weight.normal_(0, std)
|
| 164 |
elif isinstance(module, PPAIBlock):
|
| 165 |
+
for p in (module.wq, module.wk, module.wv, module.w13):
|
| 166 |
p.normal_(0, std)
|
| 167 |
+
if module.s13 is not None:
|
| 168 |
+
module.s13.normal_(0, std)
|
| 169 |
+
for p in (module.wo, module.w2):
|
| 170 |
p.normal_(0, ostd)
|
| 171 |
+
if module.s2 is not None:
|
| 172 |
+
module.s2.normal_(0, ostd)
|
| 173 |
+
if module.router_w is not None:
|
| 174 |
+
module.router_w.normal_(0, 0.01)
|
| 175 |
+
module.router_b.zero_()
|
| 176 |
module.ln1.fill_(1.0)
|
| 177 |
module.ln2.fill_(1.0)
|
| 178 |
module.q_norm.fill_(1.0)
|
|
|
|
| 199 |
self.blocks = nn.ModuleList(PPAIBlock(c, i) for i in range(c.n_layers))
|
| 200 |
self.post_init()
|
| 201 |
|
| 202 |
+
def forward(self, input_ids=None, inputs_embeds=None, past_key_values=None, use_cache=False):
|
| 203 |
c = self.config
|
| 204 |
+
src = input_ids if input_ids is not None else inputs_embeds
|
| 205 |
+
dev = src.device
|
| 206 |
+
# bf16 только там, где он аппаратный; на T4 (sm_75) — fp16, на TPU — bf16
|
| 207 |
+
if dev.type == "xla":
|
| 208 |
+
compute_dtype = torch.bfloat16
|
| 209 |
+
elif dev.type == "cuda" and torch.cuda.get_device_capability(dev)[0] >= 8:
|
| 210 |
compute_dtype = torch.bfloat16
|
| 211 |
elif dev.type == "cuda":
|
| 212 |
compute_dtype = torch.float16
|
| 213 |
else:
|
| 214 |
compute_dtype = torch.float32
|
| 215 |
|
|
|
|
| 216 |
if use_cache and past_key_values is None:
|
| 217 |
past_key_values = DynamicCache()
|
| 218 |
cache = past_key_values if use_cache else None
|
| 219 |
past_len = cache.get_seq_length() if cache is not None else 0
|
| 220 |
|
| 221 |
+
if inputs_embeds is not None:
|
| 222 |
+
# готовые эмбеддинги (например, картинка + текст): n-gram не считаем
|
| 223 |
+
x = inputs_embeds.to(compute_dtype)
|
| 224 |
+
x0 = x
|
| 225 |
+
T = x.shape[1]
|
| 226 |
+
else:
|
| 227 |
+
ids = input_ids.long()
|
| 228 |
+
B, T = ids.shape
|
| 229 |
+
# n-gram признаки требуют до 3 предыдущих токенов
|
| 230 |
+
hist = getattr(cache, "_ppai_hist", None) if cache is not None else None
|
| 231 |
+
full = ids if hist is None else torch.cat([hist.to(dev), ids], 1)
|
| 232 |
+
feats = [F.embedding(_ngram_hash(full, n, c.ngram_seed, c.ngram_buckets)[:, -T:],
|
| 233 |
+
self.ng_emb[i]).float()
|
| 234 |
+
for i, n in enumerate(c.ngram_sizes)]
|
| 235 |
+
if cache is not None:
|
| 236 |
+
cache._ppai_hist = full[:, -3:].clone()
|
| 237 |
+
x = self.tok_emb(ids).float() + self.ng_gate.float() * F.linear(torch.cat(feats, -1), self.ng_proj.float())
|
| 238 |
+
x = x.to(compute_dtype)
|
| 239 |
+
x0 = x
|
| 240 |
|
| 241 |
pos = torch.arange(past_len, past_len + T, device=dev).float()
|
| 242 |
inv = 1.0 / (c.rope_theta ** (torch.arange(0, c.head_dim, 2, device=dev).float() / c.head_dim))
|