Text Generation
Transformers
Safetensors
PyTorch
English
pebble_25m
pebble
language-model
base-model
small-language-model
custom-code
mamba2
hybrid
custom_code
Instructions to use basically-ai/Pebble-25M with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use basically-ai/Pebble-25M with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="basically-ai/Pebble-25M", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("basically-ai/Pebble-25M", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use basically-ai/Pebble-25M with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "basically-ai/Pebble-25M" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "basically-ai/Pebble-25M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/basically-ai/Pebble-25M
- SGLang
How to use basically-ai/Pebble-25M 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 "basically-ai/Pebble-25M" \ --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": "basically-ai/Pebble-25M", "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 "basically-ai/Pebble-25M" \ --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": "basically-ai/Pebble-25M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use basically-ai/Pebble-25M with Docker Model Runner:
docker model run hf.co/basically-ai/Pebble-25M
Update modeling_pebble.py
Browse files- modeling_pebble.py +75 -53
modeling_pebble.py
CHANGED
|
@@ -2,13 +2,14 @@ import torch
|
|
| 2 |
import torch.nn as nn
|
| 3 |
import torch.nn.functional as F
|
| 4 |
from transformers import PreTrainedModel
|
| 5 |
-
from .
|
| 6 |
|
| 7 |
try:
|
| 8 |
from mamba_ssm import Mamba2
|
| 9 |
except ImportError:
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
class RMSNorm(nn.Module):
|
| 14 |
def __init__(self, dim, eps=1e-6):
|
|
@@ -23,101 +24,122 @@ class RMSNorm(nn.Module):
|
|
| 23 |
return self.weight * xf.to(dt)
|
| 24 |
|
| 25 |
class AttentionBlock(nn.Module):
|
| 26 |
-
def __init__(self,
|
| 27 |
super().__init__()
|
|
|
|
|
|
|
|
|
|
| 28 |
assert dim % n_heads == 0
|
| 29 |
self.nh, self.hd = n_heads, dim // n_heads
|
| 30 |
-
self.rope_theta = rope_theta
|
| 31 |
self.wqkv = nn.Linear(dim, 3 * dim, bias=False)
|
| 32 |
self.wo = nn.Linear(dim, dim, bias=False)
|
| 33 |
self.fc1 = nn.Linear(dim, hidden, bias=False)
|
| 34 |
self.fc2 = nn.Linear(hidden, dim, bias=False)
|
| 35 |
-
self.ln1 = RMSNorm(dim)
|
| 36 |
-
self.ln2 = RMSNorm(dim)
|
|
|
|
| 37 |
|
| 38 |
def forward(self, x):
|
| 39 |
B, T, C = x.shape
|
| 40 |
h = self.ln1(x)
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
q, k, v = qkv[0], qkv[1], qkv[2]
|
| 43 |
|
| 44 |
half = self.hd // 2
|
| 45 |
invf = 1.0 / (self.rope_theta ** (
|
| 46 |
-
torch.arange(0, half, device=x.device, dtype=torch.float32)
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
cos, sin = ang.cos()[None, None], ang.sin()[None, None]
|
| 49 |
|
| 50 |
q1, q2 = q.float()[..., :half], q.float()[..., half:]
|
| 51 |
k1, k2 = k.float()[..., :half], k.float()[..., half:]
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
k = torch.cat([k1 * cos - k2 * sin,
|
|
|
|
| 55 |
|
| 56 |
y = F.scaled_dot_product_attention(q, k, v, is_causal=True)
|
| 57 |
y = y.transpose(1, 2).reshape(B, T, C)
|
|
|
|
| 58 |
x = x + self.wo(y)
|
| 59 |
x = x + self.fc2(F.gelu(self.fc1(self.ln2(x))))
|
| 60 |
return x
|
| 61 |
|
| 62 |
class MambaBlock(nn.Module):
|
| 63 |
-
def __init__(self,
|
| 64 |
super().__init__()
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
self.ln = RMSNorm(dim)
|
| 68 |
self.mixer = Mamba2(
|
| 69 |
-
d_model=
|
| 70 |
-
d_state=d_state,
|
| 71 |
-
d_conv=d_conv,
|
| 72 |
-
expand=expand,
|
| 73 |
-
headdim=headdim,
|
| 74 |
-
use_mem_eff_path=True,
|
| 75 |
)
|
| 76 |
|
| 77 |
def forward(self, x):
|
| 78 |
return x + self.mixer(self.ln(x))
|
| 79 |
|
| 80 |
-
class
|
| 81 |
config_class = PebbleConfig
|
| 82 |
-
|
| 83 |
-
|
| 84 |
|
| 85 |
def __init__(self, config):
|
| 86 |
super().__init__(config)
|
| 87 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
self.blocks = nn.ModuleList([
|
| 89 |
-
MambaBlock(
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
config.mamba_d_conv,
|
| 93 |
-
config.mamba_expand,
|
| 94 |
-
config.mamba_headdim
|
| 95 |
-
) if i % 4 < 3 else AttentionBlock(
|
| 96 |
-
config.d_model,
|
| 97 |
-
config.n_heads,
|
| 98 |
-
config.att_hidden,
|
| 99 |
-
config.rope_theta
|
| 100 |
-
)
|
| 101 |
-
for i in range(config.n_blocks)
|
| 102 |
])
|
| 103 |
-
|
| 104 |
-
self.
|
| 105 |
-
self.lm_head
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
def forward(self, input_ids=None, labels=None,
|
| 108 |
x = self.wte(input_ids)
|
|
|
|
| 109 |
for blk in self.blocks:
|
| 110 |
x = blk(x)
|
|
|
|
| 111 |
logits = self.lm_head(self.lnf(x))
|
| 112 |
-
|
| 113 |
loss = None
|
| 114 |
if labels is not None:
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
| 2 |
import torch.nn as nn
|
| 3 |
import torch.nn.functional as F
|
| 4 |
from transformers import PreTrainedModel
|
| 5 |
+
from transformers.modeling_outputs import CausalLMOutputWithPast
|
| 6 |
|
| 7 |
try:
|
| 8 |
from mamba_ssm import Mamba2
|
| 9 |
except ImportError:
|
| 10 |
+
raise ImportError("mamba-ssm is required. pip install mamba-ssm causal-conv1d")
|
| 11 |
+
|
| 12 |
+
from .configuration_pebble import PebbleConfig
|
| 13 |
|
| 14 |
class RMSNorm(nn.Module):
|
| 15 |
def __init__(self, dim, eps=1e-6):
|
|
|
|
| 24 |
return self.weight * xf.to(dt)
|
| 25 |
|
| 26 |
class AttentionBlock(nn.Module):
|
| 27 |
+
def __init__(self, config):
|
| 28 |
super().__init__()
|
| 29 |
+
dim = config.hidden_size
|
| 30 |
+
n_heads = config.num_attention_heads
|
| 31 |
+
hidden = config.intermediate_size
|
| 32 |
assert dim % n_heads == 0
|
| 33 |
self.nh, self.hd = n_heads, dim // n_heads
|
|
|
|
| 34 |
self.wqkv = nn.Linear(dim, 3 * dim, bias=False)
|
| 35 |
self.wo = nn.Linear(dim, dim, bias=False)
|
| 36 |
self.fc1 = nn.Linear(dim, hidden, bias=False)
|
| 37 |
self.fc2 = nn.Linear(hidden, dim, bias=False)
|
| 38 |
+
self.ln1 = RMSNorm(dim, eps=config.rms_norm_eps)
|
| 39 |
+
self.ln2 = RMSNorm(dim, eps=config.rms_norm_eps)
|
| 40 |
+
self.rope_theta = config.attention.get("rope_theta", 10000.0)
|
| 41 |
|
| 42 |
def forward(self, x):
|
| 43 |
B, T, C = x.shape
|
| 44 |
h = self.ln1(x)
|
| 45 |
+
|
| 46 |
+
qkv = self.wqkv(h).view(B, T, 3, self.nh, self.hd) \
|
| 47 |
+
.permute(2, 0, 3, 1, 4)
|
| 48 |
q, k, v = qkv[0], qkv[1], qkv[2]
|
| 49 |
|
| 50 |
half = self.hd // 2
|
| 51 |
invf = 1.0 / (self.rope_theta ** (
|
| 52 |
+
torch.arange(0, half, device=x.device, dtype=torch.float32)
|
| 53 |
+
* 2.0 / self.hd))
|
| 54 |
+
ang = torch.outer(
|
| 55 |
+
torch.arange(T, device=x.device, dtype=torch.float32), invf)
|
| 56 |
cos, sin = ang.cos()[None, None], ang.sin()[None, None]
|
| 57 |
|
| 58 |
q1, q2 = q.float()[..., :half], q.float()[..., half:]
|
| 59 |
k1, k2 = k.float()[..., :half], k.float()[..., half:]
|
| 60 |
+
q = torch.cat([q1 * cos - q2 * sin,
|
| 61 |
+
q1 * sin + q2 * cos], dim=-1).to(v.dtype)
|
| 62 |
+
k = torch.cat([k1 * cos - k2 * sin,
|
| 63 |
+
k1 * sin + k2 * cos], dim=-1).to(v.dtype)
|
| 64 |
|
| 65 |
y = F.scaled_dot_product_attention(q, k, v, is_causal=True)
|
| 66 |
y = y.transpose(1, 2).reshape(B, T, C)
|
| 67 |
+
|
| 68 |
x = x + self.wo(y)
|
| 69 |
x = x + self.fc2(F.gelu(self.fc1(self.ln2(x))))
|
| 70 |
return x
|
| 71 |
|
| 72 |
class MambaBlock(nn.Module):
|
| 73 |
+
def __init__(self, config):
|
| 74 |
super().__init__()
|
| 75 |
+
self.ln = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 76 |
+
mamba_cfg = config.mamba2
|
|
|
|
| 77 |
self.mixer = Mamba2(
|
| 78 |
+
d_model=config.hidden_size,
|
| 79 |
+
d_state=mamba_cfg.get("d_state", 128),
|
| 80 |
+
d_conv=mamba_cfg.get("d_conv", 4),
|
| 81 |
+
expand=mamba_cfg.get("expand", 2),
|
| 82 |
+
headdim=mamba_cfg.get("headdim", 64),
|
| 83 |
+
use_mem_eff_path=mamba_cfg.get("use_mem_eff_path", True),
|
| 84 |
)
|
| 85 |
|
| 86 |
def forward(self, x):
|
| 87 |
return x + self.mixer(self.ln(x))
|
| 88 |
|
| 89 |
+
class PebbleForCausalLM(PreTrainedModel):
|
| 90 |
config_class = PebbleConfig
|
| 91 |
+
supports_gradient_checkpointing = False
|
| 92 |
+
_no_split_modules = ["MambaBlock", "AttentionBlock"]
|
| 93 |
|
| 94 |
def __init__(self, config):
|
| 95 |
super().__init__(config)
|
| 96 |
+
self.config = config
|
| 97 |
+
|
| 98 |
+
self.wte = nn.Embedding(config.vocab_size, config.hidden_size)
|
| 99 |
+
|
| 100 |
+
# 3:1 Mamba:Attention ratio layout
|
| 101 |
self.blocks = nn.ModuleList([
|
| 102 |
+
MambaBlock(config) if i % 4 < 3
|
| 103 |
+
else AttentionBlock(config)
|
| 104 |
+
for i in range(config.num_hidden_layers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
])
|
| 106 |
+
|
| 107 |
+
self.lnf = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 108 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 109 |
+
|
| 110 |
+
# Tie weights
|
| 111 |
+
self.tie_weights()
|
| 112 |
+
|
| 113 |
+
def tie_weights(self):
|
| 114 |
+
if self.config.tie_word_embeddings:
|
| 115 |
+
self.lm_head.weight = self.wte.weight
|
| 116 |
|
| 117 |
+
def forward(self, input_ids=None, attention_mask=None, labels=None, past_key_values=None, **kwargs):
|
| 118 |
x = self.wte(input_ids)
|
| 119 |
+
|
| 120 |
for blk in self.blocks:
|
| 121 |
x = blk(x)
|
| 122 |
+
|
| 123 |
logits = self.lm_head(self.lnf(x))
|
| 124 |
+
|
| 125 |
loss = None
|
| 126 |
if labels is not None:
|
| 127 |
+
# Shift so that tokens < n predict n+1
|
| 128 |
+
shift_logits = logits[..., :-1, :].contiguous()
|
| 129 |
+
shift_labels = labels[..., 1:].contiguous()
|
| 130 |
+
loss = F.cross_entropy(
|
| 131 |
+
shift_logits.view(-1, shift_logits.size(-1)),
|
| 132 |
+
shift_labels.view(-1)
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
return CausalLMOutputWithPast(
|
| 136 |
+
loss=loss,
|
| 137 |
+
logits=logits,
|
| 138 |
+
past_key_values=past_key_values,
|
| 139 |
+
)
|
| 140 |
|
| 141 |
+
def prepare_inputs_for_generation(self, input_ids, past_key_values=None, **kwargs):
|
| 142 |
+
return {
|
| 143 |
+
"input_ids": input_ids,
|
| 144 |
+
"past_key_values": past_key_values,
|
| 145 |
+
}
|