Text Generation
Safetensors
GGUF
English
picolm_v3
picolm
slm
on-device
custom_code
causal-lm
reasoning
conversational
Instructions to use aethertp/PicoLM-V3-Pro-82M-Instruct 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 aethertp/PicoLM-V3-Pro-82M-Instruct 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 aethertp/PicoLM-V3-Pro-82M-Instruct:Q4_K_M # Run inference directly in the terminal: llama cli -hf aethertp/PicoLM-V3-Pro-82M-Instruct:Q4_K_M
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama serve -hf aethertp/PicoLM-V3-Pro-82M-Instruct:Q4_K_M # Run inference directly in the terminal: llama cli -hf aethertp/PicoLM-V3-Pro-82M-Instruct:Q4_K_M
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 aethertp/PicoLM-V3-Pro-82M-Instruct:Q4_K_M # Run inference directly in the terminal: ./build/bin/llama cli -hf aethertp/PicoLM-V3-Pro-82M-Instruct:Q4_K_M
Use Docker
docker model run hf.co/aethertp/PicoLM-V3-Pro-82M-Instruct:Q4_K_M
- Llama new
- LM Studio
- Jan
- vLLM
How to use aethertp/PicoLM-V3-Pro-82M-Instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "aethertp/PicoLM-V3-Pro-82M-Instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "aethertp/PicoLM-V3-Pro-82M-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/aethertp/PicoLM-V3-Pro-82M-Instruct:Q4_K_M
- Ollama
How to use aethertp/PicoLM-V3-Pro-82M-Instruct with Ollama:
ollama run hf.co/aethertp/PicoLM-V3-Pro-82M-Instruct:Q4_K_M
- Unsloth Desktop
- Docker Model Runner
How to use aethertp/PicoLM-V3-Pro-82M-Instruct with Docker Model Runner:
docker model run hf.co/aethertp/PicoLM-V3-Pro-82M-Instruct:Q4_K_M
- Lemonade
How to use aethertp/PicoLM-V3-Pro-82M-Instruct with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull aethertp/PicoLM-V3-Pro-82M-Instruct:Q4_K_M
Run and chat with the model
lemonade run user.PicoLM-V3-Pro-82M-Instruct-Q4_K_M
List all available models
lemonade list
- Atomic Chat
Download modeling_picolm_v3.py from aethertp/PicoLM-V3-Pro-82M-Instruct: direct link, hf CLI and curl.
- Browser
- Download file 7.15 kB
-
https://huggingface.co/aethertp/PicoLM-V3-Pro-82M-Instruct/resolve/main/modeling_picolm_v3.py
- Command line
-
hf download hf://aethertp/PicoLM-V3-Pro-82M-Instruct/modeling_picolm_v3.py
-
curl -L -o modeling_picolm_v3.py https://huggingface.co/aethertp/PicoLM-V3-Pro-82M-Instruct/resolve/main/modeling_picolm_v3.py
7.15 kB
| import math | |
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| from transformers import PreTrainedModel, GenerationMixin | |
| from transformers.modeling_outputs import CausalLMOutputWithPast | |
| from .configuration_picolm_v3 import PicoLMV3Config | |
| class RMSNorm(nn.Module): | |
| def __init__(self, dim: int, eps: float = 1e-5): | |
| super().__init__() | |
| self.eps = eps | |
| self.weight = nn.Parameter(torch.ones(dim)) | |
| def forward(self, x): | |
| return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps) * self.weight | |
| def precompute_rope_cis_yarn(head_dim=64, max_seq_len=4096, orig_len=2048, theta=10000.0, device="cpu"): | |
| half = head_dim // 2 | |
| scale = max_seq_len / orig_len | |
| idx = torch.arange(half, dtype=torch.float64, device=device) | |
| inv_freq = theta ** (-2.0 * idx / head_dim) | |
| if scale > 1.0: | |
| def corr_dim(n_rot): | |
| return head_dim * math.log(orig_len / (n_rot * 2 * math.pi)) / (2 * math.log(theta)) | |
| low = max(math.floor(corr_dim(32.0)), 0) | |
| high = min(math.ceil(corr_dim(1.0)), half - 1) | |
| gamma = ((idx - low) / (high - low)).clamp(0.0, 1.0) | |
| inv_freq = inv_freq * (1.0 - gamma) + (inv_freq / scale) * gamma | |
| attn_scale = 0.1 * math.log(scale) + 1.0 | |
| else: | |
| attn_scale = 1.0 | |
| t = torch.arange(max_seq_len, dtype=torch.float64, device=device) | |
| angles = torch.outer(t, inv_freq) | |
| cos = (torch.cos(angles) * attn_scale).to(torch.float32) | |
| sin = (torch.sin(angles) * attn_scale).to(torch.float32) | |
| return cos, sin | |
| def apply_rope(x, cos, sin): | |
| B, H, S, D = x.shape | |
| cos = cos[:S, :].unsqueeze(0).unsqueeze(0) | |
| sin = sin[:S, :].unsqueeze(0).unsqueeze(0) | |
| x1, x2 = x[..., : D // 2], x[..., D // 2 :] | |
| return torch.cat([x1 * cos - x2 * sin, x1 * sin + x2 * cos], dim=-1) | |
| class Attention(nn.Module): | |
| def __init__(self, config): | |
| super().__init__() | |
| self.head_dim = config.head_dim | |
| self.n_heads = config.n_heads | |
| self.n_kv_heads = config.n_kv_heads | |
| self.num_reps = config.n_heads // config.n_kv_heads | |
| self.q_proj = nn.Linear(config.dim, config.n_heads * config.head_dim, bias=False) | |
| self.k_proj = nn.Linear(config.dim, config.n_kv_heads * config.head_dim, bias=False) | |
| self.v_proj = nn.Linear(config.dim, config.n_kv_heads * config.head_dim, bias=False) | |
| self.out_proj = nn.Linear(config.n_heads * config.head_dim, config.dim, bias=False) | |
| self.q_norm = RMSNorm(config.head_dim, eps=config.norm_eps) | |
| self.k_norm = RMSNorm(config.head_dim, eps=config.norm_eps) | |
| def forward(self, x, cos, sin): | |
| B, S, C = x.shape | |
| q = apply_rope(self.q_norm(self.q_proj(x).view(B, S, self.n_heads, self.head_dim).transpose(1, 2)), cos, sin) | |
| k = apply_rope(self.k_norm(self.k_proj(x).view(B, S, self.n_kv_heads, self.head_dim).transpose(1, 2)), cos, sin) | |
| v = self.v_proj(x).view(B, S, self.n_kv_heads, self.head_dim).transpose(1, 2) | |
| if self.num_reps > 1: | |
| k = k[:, :, None, :, :].expand(B, self.n_kv_heads, self.num_reps, S, self.head_dim).reshape(B, self.n_heads, S, self.head_dim) | |
| v = v[:, :, None, :, :].expand(B, self.n_kv_heads, self.num_reps, S, self.head_dim).reshape(B, self.n_heads, S, self.head_dim) | |
| attn_out = F.scaled_dot_product_attention(q, k, v, is_causal=True) | |
| return self.out_proj(attn_out.transpose(1, 2).contiguous().view(B, S, C)) | |
| class SwiGLUMLP(nn.Module): | |
| def __init__(self, config): | |
| super().__init__() | |
| self.gate_proj = nn.Linear(config.dim, config.intermediate_dim, bias=False) | |
| self.up_proj = nn.Linear(config.dim, config.intermediate_dim, bias=False) | |
| self.down_proj = nn.Linear(config.intermediate_dim, config.dim, bias=False) | |
| def forward(self, x): | |
| return self.down_proj(F.silu(self.gate_proj(x)) * self.up_proj(x)) | |
| class TransformerBlockV3(nn.Module): | |
| def __init__(self, config): | |
| super().__init__() | |
| self.attn = Attention(config) | |
| self.mlp = SwiGLUMLP(config) | |
| self.attn_norm_1 = RMSNorm(config.dim, eps=config.norm_eps) | |
| self.mlp_norm_1 = RMSNorm(config.dim, eps=config.norm_eps) | |
| self.attn_norm_2 = RMSNorm(config.dim, eps=config.norm_eps) | |
| self.mlp_norm_2 = RMSNorm(config.dim, eps=config.norm_eps) | |
| def forward(self, x, cos, sin, pass_idx=0): | |
| norm_attn = self.attn_norm_1 if pass_idx == 0 else self.attn_norm_2 | |
| norm_mlp = self.mlp_norm_1 if pass_idx == 0 else self.mlp_norm_2 | |
| x = x + self.attn(norm_attn(x), cos, sin) | |
| x = x + self.mlp(norm_mlp(x)) | |
| return x | |
| class PicoLMV3ForCausalLM(PreTrainedModel, GenerationMixin): | |
| config_class = PicoLMV3Config | |
| _supports_cache_class = False | |
| def __init__(self, config): | |
| super().__init__(config) | |
| self.config = config | |
| self.tok_embeddings = nn.Embedding(config.vocab_size, config.emb_dim) | |
| self.emb_proj = nn.Linear(config.emb_dim, config.dim, bias=False) | |
| self.blocks = nn.ModuleList([TransformerBlockV3(config) for _ in range(config.n_layers)]) | |
| self.final_norm = RMSNorm(config.dim, eps=config.norm_eps) | |
| # Meta-device tuzağını kıran dinamik RoPE önbelleği | |
| self.rope_cos = None | |
| self.rope_sin = None | |
| self.post_init() | |
| def get_input_embeddings(self): | |
| return self.tok_embeddings | |
| def set_input_embeddings(self, value): | |
| self.tok_embeddings = value | |
| def _get_rope(self, device): | |
| if self.rope_cos is None or self.rope_cos.device != device: | |
| cos, sin = precompute_rope_cis_yarn( | |
| self.config.head_dim, | |
| self.config.max_seq_len, | |
| self.config.orig_seq_len, | |
| self.config.rope_theta, | |
| device=device | |
| ) | |
| self.rope_cos = cos | |
| self.rope_sin = sin | |
| return self.rope_cos, self.rope_sin | |
| def forward(self, input_ids=None, attention_mask=None, labels=None, past_key_values=None, **kwargs): | |
| B, S = input_ids.shape | |
| x_low = self.tok_embeddings(input_ids) | |
| x = self.emb_proj(x_low) | |
| # RoPE tensörlerini doğrudan modelin GPU'sunda taze ve hatasız alıyoruz | |
| rope_cos, rope_sin = self._get_rope(x.device) | |
| cos, sin = rope_cos[:S], rope_sin[:S] | |
| for block in self.blocks: | |
| for p_idx in range(self.config.layer_repeat): | |
| x = block(x, cos, sin, pass_idx=p_idx) | |
| h = self.final_norm(x) | |
| h_low = F.linear(h, self.emb_proj.weight.t()) | |
| logits = F.linear(h_low, self.tok_embeddings.weight) | |
| loss = None | |
| if labels is not None: | |
| loss = F.cross_entropy(logits.reshape(-1, self.config.vocab_size), labels.reshape(-1), ignore_index=-100) | |
| return CausalLMOutputWithPast(loss=loss, logits=logits) | |
| def prepare_inputs_for_generation(self, input_ids, attention_mask=None, **kwargs): | |
| model_inputs = {"input_ids": input_ids} | |
| if attention_mask is not None: | |
| model_inputs["attention_mask"] = attention_mask | |
| return model_inputs | |