Text Generation
Transformers
Safetensors
English
gpt
causal-lm
decoder-only
grouped-query-attention
rope
swiglu
boundlessbpe
curriculum-learning
xsa
custom_code
Instructions to use UniversalComputingResearch/Limen0.2B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use UniversalComputingResearch/Limen0.2B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="UniversalComputingResearch/Limen0.2B", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("UniversalComputingResearch/Limen0.2B", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use UniversalComputingResearch/Limen0.2B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "UniversalComputingResearch/Limen0.2B" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "UniversalComputingResearch/Limen0.2B", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/UniversalComputingResearch/Limen0.2B
- SGLang
How to use UniversalComputingResearch/Limen0.2B 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 "UniversalComputingResearch/Limen0.2B" \ --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": "UniversalComputingResearch/Limen0.2B", "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 "UniversalComputingResearch/Limen0.2B" \ --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": "UniversalComputingResearch/Limen0.2B", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use UniversalComputingResearch/Limen0.2B with Docker Model Runner:
docker model run hf.co/UniversalComputingResearch/Limen0.2B
File size: 14,066 Bytes
dc64c03 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | import math
from typing import Optional
import torch
import torch.nn as nn
from torch import Tensor
from torch.nn import functional as F
from transformers import PreTrainedModel
from transformers.cache_utils import DynamicCache
from transformers.generation.utils import GenerationMixin
from transformers.modeling_outputs import CausalLMOutputWithPast
from .config import GPTConfig
CONTROL_TENSOR_NAME_PATTERNS = (
"scale",
"gate",
"gain",
"norm",
"ln_",
"rms",
)
def _parameter_count(module: nn.Module) -> int:
return sum(parameter.numel() for parameter in module.parameters())
def _format_parameter_count(count: int) -> str:
return f"{count:,} ({count / 1_000_000:.3f}M)"
def parameter_breakdown(model: nn.Module) -> str:
"""Return a tree-style parameter summary for the GPT architecture."""
transformer = model.transformer
blocks = list(transformer["h"])
embedding = transformer["wte"]
final_norm = transformer["ln_f"]
first = blocks[0] if blocks else None
embedding_params = _parameter_count(embedding)
final_norm_params = _parameter_count(final_norm)
block_params = _parameter_count(first) if first is not None else 0
attention_params = _parameter_count(first.attn) if first is not None else 0
mlp_params = _parameter_count(first.mlp) if first is not None else 0
ln1_params = _parameter_count(first.ln_1) if first is not None else 0
ln2_params = _parameter_count(first.ln_2) if first is not None else 0
lm_head_params = _parameter_count(model.lm_head)
tied_lm_head = bool(model.lm_head.weight is embedding.weight)
unique_total = _parameter_count(model)
def component_lines(prefix: str = "β β ") -> list[str]:
if first is None:
return []
attn = first.attn
mlp = first.mlp
return [
f"{prefix}ββ norm 1 ................ {_format_parameter_count(ln1_params)}",
f"{prefix}ββ attention .............. {_format_parameter_count(attention_params)}",
f"{prefix}β ββ q_proj .............. {_format_parameter_count(_parameter_count(attn.q_proj))}",
f"{prefix}β ββ k_proj .............. {_format_parameter_count(_parameter_count(attn.k_proj))}",
f"{prefix}β ββ v_proj .............. {_format_parameter_count(_parameter_count(attn.v_proj))}",
f"{prefix}β ββ o_proj .............. {_format_parameter_count(_parameter_count(attn.o_proj))}",
f"{prefix}ββ norm 2 ................ {_format_parameter_count(ln2_params)}",
f"{prefix}ββ mlp ................... {_format_parameter_count(mlp_params)}",
f"{prefix} ββ w_gate ............ {_format_parameter_count(_parameter_count(mlp.w_gate))}",
f"{prefix} ββ w_up .............. {_format_parameter_count(_parameter_count(mlp.w_up))}",
f"{prefix} ββ w_down ............ {_format_parameter_count(_parameter_count(mlp.w_down))}",
]
lines = [
"parameter breakdown:",
f"ββ embedding (wte) ........ {_format_parameter_count(embedding_params)}",
f"ββ transformer blocks Γ {len(blocks)}",
f"β ββ one block ........... {_format_parameter_count(block_params)}",
"β ββ per-block detail",
*component_lines(),
f"β ββ all blocks .......... {_format_parameter_count(block_params * len(blocks))}",
f"ββ final norm (ln_f) ....... {_format_parameter_count(final_norm_params)}",
]
if tied_lm_head:
lines.append(
f"ββ lm_head ................ tied to embedding (+0 unique; raw {_format_parameter_count(lm_head_params)})"
)
else:
lines.append(f"ββ lm_head ................ {_format_parameter_count(lm_head_params)}")
lines.append(f"ββ total unique ............ {_format_parameter_count(unique_total)}")
return "\n".join(lines)
class CastedLinear(nn.Linear):
"""Store linear params in FP32, cast to activation dtype for matmul."""
def forward(self, x: Tensor) -> Tensor:
weight = self.weight.to(dtype=x.dtype)
bias = self.bias.to(dtype=x.dtype) if self.bias is not None else None
return F.linear(x, weight, bias)
def restore_fp32_params(model: nn.Module) -> None:
"""Keep linear weights and control params in FP32 after dtype conversion."""
for module in model.modules():
if isinstance(module, CastedLinear):
module.float()
for name, param in model.named_parameters():
if (
param.ndim < 2
or any(pattern in name for pattern in CONTROL_TENSOR_NAME_PATTERNS)
) and param.dtype != torch.float32:
param.data = param.data.float()
class RMSNorm(nn.Module):
def __init__(self, dim, eps=1e-6):
super().__init__()
self.eps = eps
self.weight = nn.Parameter(torch.ones(dim))
def forward(self, x):
rms = torch.rsqrt(x.float().pow(2).mean(-1, keepdim=True) + self.eps)
return (x.float() * rms).to(dtype=x.dtype) * self.weight.to(dtype=x.dtype)
def build_rope_inv_freq(head_dim, theta=2500.0):
return 1.0 / (theta ** (torch.arange(0, head_dim, 2, dtype=torch.float32) / head_dim))
def precompute_rope_cos_sin(head_dim, seq_len, theta=2500.0):
freqs = build_rope_inv_freq(head_dim, theta)
t = torch.arange(seq_len, dtype=torch.float32)
freqs = torch.outer(t, freqs)
return freqs.cos(), freqs.sin()
def _apply_rope(x, cos, sin):
x_float = x.float()
x_pair = x_float.reshape(*x_float.shape[:-1], -1, 2)
even = x_pair[..., 0]
odd = x_pair[..., 1]
cos = cos.unsqueeze(0).unsqueeze(0)
sin = sin.unsqueeze(0).unsqueeze(0)
x_rot = torch.stack((even * cos - odd * sin, even * sin + odd * cos), dim=-1)
return x_rot.flatten(-2).type_as(x)
def apply_rotary_emb(q, k, freqs_cis):
cos, sin = freqs_cis
return _apply_rope(q, cos, sin), _apply_rope(k, cos, sin)
class GPTAttention(nn.Module):
def __init__(self, config, layer_idx):
super().__init__()
self.layer_idx = layer_idx
self.n_head = config.num_attention_heads
self.n_kv_heads = config.num_key_value_heads
self.head_dim = config.head_dim
self.n_rep = self.n_head // self.n_kv_heads
self.xsa_projection = config.xsa_projection
self.q_proj = CastedLinear(config.hidden_size, self.n_head * self.head_dim, bias=False)
self.k_proj = CastedLinear(config.hidden_size, self.n_kv_heads * self.head_dim, bias=False)
self.v_proj = CastedLinear(config.hidden_size, self.n_kv_heads * self.head_dim, bias=False)
self.o_proj = CastedLinear(self.n_head * self.head_dim, config.hidden_size, bias=False)
def _xsa_efficient(self, y: Tensor, v_current: Tensor) -> Tensor:
# y: [B, H, T, D]
# v_current: [B, Hkv, T, D]
B, H, T, D = y.shape
Hkv = v_current.size(1)
group = H // Hkv
y_g = y.reshape(B, Hkv, group, T, D)
v_n = F.normalize(v_current, dim=-1).unsqueeze(2)
proj = (y_g * v_n).sum(dim=-1, keepdim=True) * v_n
return (y_g - proj).reshape(B, H, T, D)
def forward(self, x, freqs_cis, past_key_value=None, use_cache=False, attention_mask=None):
B, T, _ = x.size()
q = self.q_proj(x).view(B, T, self.n_head, self.head_dim).transpose(1, 2)
k_current = self.k_proj(x).view(B, T, self.n_kv_heads, self.head_dim).transpose(1, 2)
v_current = self.v_proj(x).view(B, T, self.n_kv_heads, self.head_dim).transpose(1, 2)
q, k_current = apply_rotary_emb(q, k_current, freqs_cis)
if past_key_value is not None:
k, v = past_key_value.update(k_current, v_current, self.layer_idx)
else:
k, v = k_current, v_current
S = k.size(2)
is_causal = past_key_value is None or past_key_value.get_seq_length(self.layer_idx) == T
attn_mask = None
if attention_mask is not None:
key_pad = attention_mask.to(torch.bool)[:, None, None, :]
if is_causal and T > 1:
causal = torch.ones(T, S, dtype=torch.bool, device=x.device).tril(diagonal=S - T)
attn_mask = key_pad & causal[None, None, :, :]
else:
attn_mask = key_pad.expand(B, 1, T, S)
is_causal = False
y = F.scaled_dot_product_attention(
q,
k,
v,
attn_mask=attn_mask,
is_causal=is_causal,
enable_gqa=(self.n_kv_heads != self.n_head),
)
if self.xsa_projection:
y = self._xsa_efficient(y, v_current)
y = y.transpose(1, 2).contiguous().view(B, T, self.n_head * self.head_dim)
return self.o_proj(y)
class GPTMLP(nn.Module):
def __init__(self, config):
super().__init__()
self.w_gate = CastedLinear(config.hidden_size, config.intermediate_size, bias=False)
self.w_up = CastedLinear(config.hidden_size, config.intermediate_size, bias=False)
self.w_down = CastedLinear(config.intermediate_size, config.hidden_size, bias=False)
def forward(self, x):
return self.w_down(F.silu(self.w_gate(x)) * self.w_up(x))
class GPTBlock(nn.Module):
def __init__(self, config, layer_idx):
super().__init__()
self.ln_1 = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
self.attn = GPTAttention(config, layer_idx)
self.ln_2 = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
self.mlp = GPTMLP(config)
def forward(self, x, freqs_cis, past_key_value=None, use_cache=False, attention_mask=None):
x = x + self.attn(self.ln_1(x), freqs_cis, past_key_value, use_cache, attention_mask=attention_mask)
x = x + self.mlp(self.ln_2(x))
return x
class GPTPreTrainedModel(PreTrainedModel):
config_class = GPTConfig
base_model_prefix = "transformer"
supports_gradient_checkpointing = False
def _init_weights(self, module):
std = self.config.hidden_size ** -0.5
if isinstance(module, nn.Linear):
torch.nn.init.normal_(module.weight, mean=0.0, std=std)
elif isinstance(module, nn.Embedding):
torch.nn.init.normal_(module.weight, mean=0.0, std=std)
class GPTForCausalLM(GPTPreTrainedModel, GenerationMixin):
_tied_weights_keys = {"lm_head.weight": "transformer.wte.weight"}
def __init__(self, config):
super().__init__(config)
self.config = config
self.transformer = nn.ModuleDict(dict(
wte=nn.Embedding(config.vocab_size, config.hidden_size),
h=nn.ModuleList([GPTBlock(config, i) for i in range(config.num_hidden_layers)]),
ln_f=RMSNorm(config.hidden_size, eps=config.rms_norm_eps),
))
self.lm_head = CastedLinear(config.hidden_size, config.vocab_size, bias=False)
if config.tie_word_embeddings:
self.lm_head.weight = self.transformer["wte"].weight
self._freqs_cis_cache = None
self.post_init()
restore_fp32_params(self)
def _apply(self, fn):
module = super()._apply(fn)
restore_fp32_params(self)
return module
def get_input_embeddings(self):
return self.transformer["wte"]
def set_input_embeddings(self, value):
self.transformer["wte"] = value
def get_output_embeddings(self):
return self.lm_head
def set_output_embeddings(self, new_embeddings):
self.lm_head = new_embeddings
def prepare_inputs_for_generation(self, input_ids, past_key_values=None, attention_mask=None, **kwargs):
if past_key_values is not None and past_key_values.get_seq_length() > 0:
input_ids = input_ids[:, -1:]
return {
"input_ids": input_ids,
"attention_mask": attention_mask,
"past_key_values": past_key_values,
"use_cache": True,
}
def _get_freqs_cis(self, seq_len, device):
cache = self._freqs_cis_cache
if cache is None or cache[0].device != device or cache[0].size(0) < seq_len:
cache = tuple(
tensor.to(device)
for tensor in precompute_rope_cos_sin(self.config.head_dim, seq_len, self.config.rope_theta)
)
if torch.is_inference_mode_enabled():
return cache[0][:seq_len], cache[1][:seq_len]
self._freqs_cis_cache = cache
return cache[0][:seq_len], cache[1][:seq_len]
def forward(
self,
input_ids,
attention_mask=None,
labels=None,
past_key_values: Optional[DynamicCache] = None,
use_cache=False,
**kwargs,
):
B, T = input_ids.size()
if use_cache and past_key_values is None:
past_key_values = DynamicCache()
past_len = past_key_values.get_seq_length() if past_key_values is not None else 0
x = self.transformer["wte"](input_ids)
cos, sin = self._get_freqs_cis(past_len + T, input_ids.device)
freqs_cis = cos[past_len:], sin[past_len:]
for block in self.transformer["h"]:
x = block(x, freqs_cis, past_key_values if use_cache else None, use_cache, attention_mask=attention_mask)
x = self.transformer["ln_f"](x)
logits = self.lm_head(x)
loss = None
if labels is not None:
if getattr(self.config, "labels_are_shifted", False):
loss = F.cross_entropy(logits.float().reshape(-1, logits.size(-1)), labels.reshape(-1))
else:
shift_logits = logits[..., :-1, :].contiguous()
shift_labels = labels[..., 1:].contiguous()
loss = F.cross_entropy(shift_logits.float().view(-1, shift_logits.size(-1)), shift_labels.reshape(-1))
return CausalLMOutputWithPast(
loss=loss,
logits=logits,
past_key_values=past_key_values if use_cache else None,
)
|