Instructions to use sztyberj/IncunabuLM-111M with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use sztyberj/IncunabuLM-111M with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="sztyberj/IncunabuLM-111M")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("sztyberj/IncunabuLM-111M", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use sztyberj/IncunabuLM-111M with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "sztyberj/IncunabuLM-111M" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "sztyberj/IncunabuLM-111M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/sztyberj/IncunabuLM-111M
- SGLang
How to use sztyberj/IncunabuLM-111M 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 "sztyberj/IncunabuLM-111M" \ --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": "sztyberj/IncunabuLM-111M", "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 "sztyberj/IncunabuLM-111M" \ --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": "sztyberj/IncunabuLM-111M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use sztyberj/IncunabuLM-111M with Docker Model Runner:
docker model run hf.co/sztyberj/IncunabuLM-111M
Upload model.py
Browse files
model.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from torch.nn import functional as F
|
| 4 |
+
|
| 5 |
+
class Head(nn.Module):
|
| 6 |
+
def __init__(self, head_size, n_embd, block_size, dropout):
|
| 7 |
+
super().__init__()
|
| 8 |
+
self.key = nn.Linear(n_embd, head_size, bias=False)
|
| 9 |
+
self.query = nn.Linear(n_embd, head_size, bias=False)
|
| 10 |
+
self.value = nn.Linear(n_embd, head_size, bias=False)
|
| 11 |
+
self.register_buffer('tril', torch.tril(torch.ones(block_size, block_size)))
|
| 12 |
+
self.dropout = nn.Dropout(dropout)
|
| 13 |
+
|
| 14 |
+
def forward(self, x):
|
| 15 |
+
B, T, C = x.shape
|
| 16 |
+
k = self.key(x)
|
| 17 |
+
q = self.query(x)
|
| 18 |
+
|
| 19 |
+
wei = q @ k.transpose(-2, -1) * k.size(-1)**-0.5
|
| 20 |
+
wei = wei.masked_fill(self.tril[:T, :T] == 0, float('-inf'))
|
| 21 |
+
wei = F.softmax(wei, dim=-1)
|
| 22 |
+
wei = self.dropout(wei)
|
| 23 |
+
|
| 24 |
+
v = self.value(x)
|
| 25 |
+
out = wei @ v
|
| 26 |
+
return out
|
| 27 |
+
|
| 28 |
+
class MultiHeadAttention(nn.Module):
|
| 29 |
+
def __init__(self, num_heads, head_size, n_embd, dropout, block_size):
|
| 30 |
+
super().__init__()
|
| 31 |
+
self.heads = nn.ModuleList([Head(head_size, n_embd, block_size, dropout) for _ in range(num_heads)])
|
| 32 |
+
self.proj = nn.Linear(head_size * num_heads, n_embd)
|
| 33 |
+
self.dropout = nn.Dropout(dropout)
|
| 34 |
+
|
| 35 |
+
def forward(self, x):
|
| 36 |
+
out = torch.cat([h(x) for h in self.heads], dim=-1)
|
| 37 |
+
out = self.proj(out)
|
| 38 |
+
return out
|
| 39 |
+
|
| 40 |
+
class FeedForward(nn.Module):
|
| 41 |
+
def __init__(self, n_embd, dropout):
|
| 42 |
+
super().__init__()
|
| 43 |
+
hidden_dim = 4 * n_embd
|
| 44 |
+
self.net = nn.Sequential(
|
| 45 |
+
nn.Linear(n_embd, hidden_dim, bias=False),
|
| 46 |
+
nn.SiLU(),
|
| 47 |
+
nn.Linear(hidden_dim, n_embd, bias=False),
|
| 48 |
+
nn.Dropout(dropout),
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
def forward(self, x):
|
| 52 |
+
return self.net(x)
|
| 53 |
+
|
| 54 |
+
class RMSNorm(nn.Module):
|
| 55 |
+
def __init__(self, dim: int, eps: float = 1e-6):
|
| 56 |
+
super().__init__()
|
| 57 |
+
self.eps = eps
|
| 58 |
+
self.weight = nn.Parameter(torch.ones(dim))
|
| 59 |
+
|
| 60 |
+
def _norm(self, x):
|
| 61 |
+
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
|
| 62 |
+
|
| 63 |
+
def forward(self, x):
|
| 64 |
+
output = self._norm(x.float()).type_as(x)
|
| 65 |
+
return output * self.weight
|
| 66 |
+
|
| 67 |
+
class Block(nn.Module):
|
| 68 |
+
def __init__(self, n_embd, n_head, dropout, block_size):
|
| 69 |
+
super().__init__()
|
| 70 |
+
head_size = n_embd // n_head
|
| 71 |
+
self.sa = MultiHeadAttention(n_head, head_size, n_embd, dropout, block_size)
|
| 72 |
+
self.ffwd = FeedForward(n_embd, dropout)
|
| 73 |
+
self.RMSN1 = RMSNorm(n_embd)
|
| 74 |
+
self.RMSN2 = RMSNorm(n_embd)
|
| 75 |
+
|
| 76 |
+
def forward(self, x):
|
| 77 |
+
x = x + self.sa(self.RMSN1(x))
|
| 78 |
+
x = x + self.ffwd(self.RMSN2(x))
|
| 79 |
+
return x
|
| 80 |
+
|
| 81 |
+
class TransformerDecoder(nn.Module):
|
| 82 |
+
def __init__(self, vocab_size, n_embd, block_size, n_head, n_layer, dropout):
|
| 83 |
+
super().__init__()
|
| 84 |
+
self.block_size = block_size
|
| 85 |
+
self.token_embedding_table = nn.Embedding(vocab_size, n_embd)
|
| 86 |
+
self.position_embeddings_table = nn.Embedding(block_size, n_embd)
|
| 87 |
+
self.blocks = nn.Sequential(*[Block(n_embd, n_head=n_head, dropout=dropout, block_size=block_size) for _ in range(n_layer)])
|
| 88 |
+
self.RMSN_f = RMSNorm(n_embd)
|
| 89 |
+
self.lm_head = nn.Linear(n_embd, vocab_size)
|
| 90 |
+
|
| 91 |
+
def forward(self, idx, targets=None):
|
| 92 |
+
B, T = idx.shape
|
| 93 |
+
tok_emb = self.token_embedding_table(idx)
|
| 94 |
+
pos_emb = self.position_embeddings_table(torch.arange(T, device=idx.device))
|
| 95 |
+
x = tok_emb + pos_emb
|
| 96 |
+
x = self.blocks(x)
|
| 97 |
+
x = self.RMSN_f(x)
|
| 98 |
+
logits = self.lm_head(x)
|
| 99 |
+
|
| 100 |
+
if targets is None:
|
| 101 |
+
loss = None
|
| 102 |
+
else:
|
| 103 |
+
B, T, C = logits.shape
|
| 104 |
+
logits = logits.view(B*T, C)
|
| 105 |
+
targets = targets.view(B*T)
|
| 106 |
+
loss = F.cross_entropy(logits, targets)
|
| 107 |
+
|
| 108 |
+
return logits, loss
|
| 109 |
+
|
| 110 |
+
def generate(self, idx, max_new_tokens, temperature=1.0, top_k=None, repetition_penalty=1.2):
|
| 111 |
+
for _ in range(max_new_tokens):
|
| 112 |
+
idx_cond = idx if idx.size(1) <= self.block_size else idx[:, -self.block_size:]
|
| 113 |
+
logits, _ = self(idx_cond)
|
| 114 |
+
logits = logits[:, -1, :]
|
| 115 |
+
|
| 116 |
+
if repetition_penalty != 1.0:
|
| 117 |
+
for i in range(idx_cond.shape[0]):
|
| 118 |
+
for token_id in set(idx_cond[i].tolist()):
|
| 119 |
+
if logits[i, token_id] > 0:
|
| 120 |
+
logits[i, token_id] /= repetition_penalty
|
| 121 |
+
else:
|
| 122 |
+
logits[i, token_id] *= repetition_penalty
|
| 123 |
+
|
| 124 |
+
if temperature != 1.0:
|
| 125 |
+
logits = logits / temperature
|
| 126 |
+
|
| 127 |
+
if top_k is not None:
|
| 128 |
+
v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
|
| 129 |
+
logits[logits < v[:, [-1]]] = -float('Inf')
|
| 130 |
+
|
| 131 |
+
probs = F.softmax(logits, dim=-1)
|
| 132 |
+
idx_next = torch.multinomial(probs, num_samples=1)
|
| 133 |
+
|
| 134 |
+
idx = torch.cat((idx, idx_next), dim=1)
|
| 135 |
+
|
| 136 |
+
return idx
|