Instructions to use nowefnyieryfner/llama-50m-base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- llama.cpp
How to use nowefnyieryfner/llama-50m-base with llama.cpp:
Install (macOS, Linux)
curl -LsSf https://llama.app/install.sh | sh # Start a local OpenAI-compatible server with a web UI: llama serve -hf nowefnyieryfner/llama-50m-base # Run inference directly in the terminal: llama cli -hf nowefnyieryfner/llama-50m-base
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama serve -hf nowefnyieryfner/llama-50m-base # Run inference directly in the terminal: llama cli -hf nowefnyieryfner/llama-50m-base
Build from source code
git clone https://github.com/ggml-org/llama.cpp.git cd llama.cpp cmake -B build cmake --build build -j --target llama-app # Start a local OpenAI-compatible server with a web UI: ./build/bin/llama serve -hf nowefnyieryfner/llama-50m-base # Run inference directly in the terminal: ./build/bin/llama cli -hf nowefnyieryfner/llama-50m-base
Use Docker
docker model run hf.co/nowefnyieryfner/llama-50m-base
- Llama new
- LM Studio
- Jan
- vLLM
How to use nowefnyieryfner/llama-50m-base with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "nowefnyieryfner/llama-50m-base" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "nowefnyieryfner/llama-50m-base", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/nowefnyieryfner/llama-50m-base
- Ollama
How to use nowefnyieryfner/llama-50m-base with Ollama:
ollama run hf.co/nowefnyieryfner/llama-50m-base
- Unsloth Desktop
- Docker Model Runner
How to use nowefnyieryfner/llama-50m-base with Docker Model Runner:
docker model run hf.co/nowefnyieryfner/llama-50m-base
- Lemonade
How to use nowefnyieryfner/llama-50m-base with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull nowefnyieryfner/llama-50m-base
Run and chat with the model
lemonade run user.llama-50m-base-{{QUANT_TAG}}List all available models
lemonade list
- Atomic Chat
Download model.py from nowefnyieryfner/llama-50m-base: direct link, hf CLI and curl.
- Browser
- Download file 7.14 kB
-
https://huggingface.co/nowefnyieryfner/llama-50m-base/resolve/main/model.py
- Command line
-
hf download hf://nowefnyieryfner/llama-50m-base/model.py
-
curl -L -o model.py https://huggingface.co/nowefnyieryfner/llama-50m-base/resolve/main/model.py
7.14 kB
| import math | |
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| from dataclasses import dataclass | |
| class GPTConfig: | |
| vocab_size: int = 16384 | |
| max_seq_len: int = 512 | |
| d_model: int = 256 | |
| n_heads: int = 8 | |
| n_layers: int = 8 | |
| d_ff: int = 680 # FFN dimension (for SwiGLU) | |
| dropout: float = 0.0 | |
| tie_embeddings: bool = True | |
| class RMSNorm(nn.Module): | |
| def __init__(self, dim: int, eps: float = 1e-6): | |
| super().__init__() | |
| self.eps = eps | |
| self.weight = nn.Parameter(torch.ones(dim)) | |
| def forward(self, x): | |
| variance = x.pow(2).mean(-1, keepdim=True) | |
| return x * torch.rsqrt(variance + self.eps) * self.weight | |
| class RoPE(nn.Module): | |
| def __init__(self, dim, max_seq_len=512, theta=10000.0): | |
| super().__init__() | |
| # dim is head_dim | |
| inv_freq = 1.0 / (theta ** (torch.arange(0, dim, 2).float() / dim)) | |
| self.register_buffer("inv_freq", inv_freq, persistent=False) | |
| t = torch.arange(max_seq_len, dtype=torch.float32) | |
| freqs = torch.outer(t, self.inv_freq) | |
| emb = torch.cat((freqs, freqs), dim=-1) | |
| self.register_buffer("cos_cached", emb.cos(), persistent=False) | |
| self.register_buffer("sin_cached", emb.sin(), persistent=False) | |
| def forward(self, x, seq_len): | |
| return self.cos_cached[:seq_len], self.sin_cached[:seq_len] | |
| def rotate_half(x): | |
| x1 = x[..., :x.shape[-1] // 2] | |
| x2 = x[..., x.shape[-1] // 2:] | |
| return torch.cat((-x2, x1), dim=-1) | |
| def apply_rotary_pos_emb(q, k, cos, sin): | |
| cos = cos.unsqueeze(0).unsqueeze(1) # (1, 1, T, head_dim) | |
| sin = sin.unsqueeze(0).unsqueeze(1) | |
| q_embed = (q * cos) + (rotate_half(q) * sin) | |
| k_embed = (k * cos) + (rotate_half(k) * sin) | |
| return q_embed, k_embed | |
| class CausalSelfAttention(nn.Module): | |
| def __init__(self, config: GPTConfig): | |
| super().__init__() | |
| assert config.d_model % config.n_heads == 0 | |
| self.n_heads = config.n_heads | |
| self.d_model = config.d_model | |
| self.head_dim = config.d_model // config.n_heads | |
| # Separate projection weights matching HF Llama keys | |
| self.q_proj = nn.Linear(config.d_model, config.d_model, bias=False) | |
| self.k_proj = nn.Linear(config.d_model, config.d_model, bias=False) | |
| self.v_proj = nn.Linear(config.d_model, config.d_model, bias=False) | |
| self.o_proj = nn.Linear(config.d_model, config.d_model, bias=False) | |
| self.rope = RoPE(self.head_dim, max_seq_len=config.max_seq_len) | |
| self.attn_dropout = config.dropout | |
| self.resid_dropout = nn.Dropout(config.dropout) | |
| def forward(self, x): | |
| B, T, C = x.size() | |
| q = self.q_proj(x).view(B, T, self.n_heads, self.head_dim).transpose(1, 2) | |
| k = self.k_proj(x).view(B, T, self.n_heads, self.head_dim).transpose(1, 2) | |
| v = self.v_proj(x).view(B, T, self.n_heads, self.head_dim).transpose(1, 2) | |
| cos, sin = self.rope(q, T) | |
| q, k = apply_rotary_pos_emb(q, k, cos, sin) | |
| y = F.scaled_dot_product_attention( | |
| q, k, v, | |
| attn_mask=None, | |
| dropout_p=self.attn_dropout if self.training else 0.0, | |
| is_causal=True | |
| ) | |
| y = y.transpose(1, 2).contiguous().view(B, T, C) | |
| return self.resid_dropout(self.o_proj(y)) | |
| class LlamaMLP(nn.Module): | |
| def __init__(self, config: GPTConfig): | |
| super().__init__() | |
| self.gate_proj = nn.Linear(config.d_model, config.d_ff, bias=False) | |
| self.up_proj = nn.Linear(config.d_model, config.d_ff, bias=False) | |
| self.down_proj = nn.Linear(config.d_ff, config.d_model, bias=False) | |
| self.dropout = nn.Dropout(config.dropout) | |
| def forward(self, x): | |
| return self.dropout(self.down_proj(F.silu(self.gate_proj(x)) * self.up_proj(x))) | |
| class LlamaBlock(nn.Module): | |
| def __init__(self, config: GPTConfig): | |
| super().__init__() | |
| self.input_layernorm = RMSNorm(config.d_model) | |
| self.self_attn = CausalSelfAttention(config) | |
| self.post_attention_layernorm = RMSNorm(config.d_model) | |
| self.mlp = LlamaMLP(config) | |
| def forward(self, x): | |
| x = x + self.self_attn(self.input_layernorm(x)) | |
| x = x + self.mlp(self.post_attention_layernorm(x)) | |
| return x | |
| class GPT(nn.Module): | |
| def __init__(self, config: GPTConfig): | |
| super().__init__() | |
| self.config = config | |
| self.model = nn.ModuleDict({ | |
| "embed_tokens": nn.Embedding(config.vocab_size, config.d_model), | |
| "layers": nn.ModuleList([LlamaBlock(config) for _ in range(config.n_layers)]), | |
| "norm": RMSNorm(config.d_model) | |
| }) | |
| self.lm_head = nn.Linear(config.d_model, config.vocab_size, bias=False) | |
| if config.tie_embeddings: | |
| self.model.embed_tokens.weight = self.lm_head.weight | |
| self.apply(self._init_weights) | |
| # Scale residual projection weights | |
| for name, p in self.named_parameters(): | |
| if name.endswith('o_proj.weight') or name.endswith('down_proj.weight'): | |
| torch.nn.init.normal_(p, mean=0.0, std=0.02 / math.sqrt(2 * config.n_layers)) | |
| def _init_weights(self, module): | |
| if isinstance(module, nn.Linear): | |
| torch.nn.init.normal_(module.weight, mean=0.0, std=0.02) | |
| if module.bias is not None: | |
| torch.nn.init.zeros_(module.bias) | |
| elif isinstance(module, nn.Embedding): | |
| torch.nn.init.normal_(module.weight, mean=0.0, std=0.02) | |
| def get_num_params(self): | |
| return sum(p.numel() for p in self.parameters()) | |
| def forward(self, idx, targets=None): | |
| b, t = idx.size() | |
| assert t <= self.config.max_seq_len, f"Sequence length {t} exceeds max_seq_len {self.config.max_seq_len}" | |
| x = self.model.embed_tokens(idx) | |
| for block in self.model.layers: | |
| x = block(x) | |
| x = self.model.norm(x) | |
| if targets is not None: | |
| logits = self.lm_head(x) | |
| loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1), ignore_index=-1) | |
| else: | |
| # inference optimization: only compute logits for the last position | |
| logits = self.lm_head(x[:, [-1], :]) | |
| loss = None | |
| return logits, loss | |
| def generate(self, idx, max_new_tokens, temperature=1.0, top_k=None): | |
| for _ in range(max_new_tokens): | |
| idx_cond = idx if idx.size(1) <= self.config.max_seq_len else idx[:, -self.config.max_seq_len:] | |
| logits, _ = self(idx_cond) | |
| logits = logits[:, -1, :] / temperature | |
| if top_k is not None: | |
| v, _ = torch.topk(logits, min(top_k, logits.size(-1))) | |
| logits[logits < v[:, [-1]]] = -float('Inf') | |
| probs = F.softmax(logits, dim=-1) | |
| idx_next = torch.multinomial(probs, num_samples=1) | |
| idx = torch.cat((idx, idx_next), dim=1) | |
| return idx | |