Text Generation
Transformers
Safetensors
English
llama
tiny
toy-model
from-scratch
text-generation-inference
Instructions to use aaaaaaaieuyd/Small-LLM1 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use aaaaaaaieuyd/Small-LLM1 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="aaaaaaaieuyd/Small-LLM1")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("aaaaaaaieuyd/Small-LLM1") model = AutoModelForCausalLM.from_pretrained("aaaaaaaieuyd/Small-LLM1", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use aaaaaaaieuyd/Small-LLM1 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "aaaaaaaieuyd/Small-LLM1" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "aaaaaaaieuyd/Small-LLM1", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/aaaaaaaieuyd/Small-LLM1
- SGLang
How to use aaaaaaaieuyd/Small-LLM1 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 "aaaaaaaieuyd/Small-LLM1" \ --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": "aaaaaaaieuyd/Small-LLM1", "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 "aaaaaaaieuyd/Small-LLM1" \ --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": "aaaaaaaieuyd/Small-LLM1", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use aaaaaaaieuyd/Small-LLM1 with Docker Model Runner:
docker model run hf.co/aaaaaaaieuyd/Small-LLM1
File size: 3,938 Bytes
16dce9d | 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 | """Small LLM1: ~5M parameter Llama-style model trained from scratch on TinyStories."""
import math, os, time
import torch
from datasets import load_dataset
from tokenizers import Tokenizer, models, pre_tokenizers, decoders, trainers, processors
from transformers import LlamaConfig, LlamaForCausalLM, PreTrainedTokenizerFast
OUT = "small-llm1"
N_STORIES = 400_000
VOCAB = 4096
SEQ = 256
BATCH = 64
STEPS = 6000
LR = 2e-3
torch.manual_seed(0)
# --- data ---
print("loading data...")
ds = load_dataset("roneneldan/TinyStories", split="train", streaming=True)
texts = [ex["text"].strip() for _, ex in zip(range(N_STORIES), ds) if ex["text"].strip()]
print(f"{len(texts)} stories")
# --- tokenizer (byte-level BPE) ---
tok = Tokenizer(models.BPE())
tok.pre_tokenizer = pre_tokenizers.ByteLevel(add_prefix_space=False)
tok.decoder = decoders.ByteLevel()
trainer = trainers.BpeTrainer(vocab_size=VOCAB, special_tokens=["<|endoftext|>"],
initial_alphabet=pre_tokenizers.ByteLevel.alphabet())
tok.train_from_iterator(texts[:100_000], trainer)
tok.post_processor = processors.ByteLevel(trim_offsets=False)
hf_tok = PreTrainedTokenizerFast(tokenizer_object=tok, bos_token="<|endoftext|>",
eos_token="<|endoftext|>", unk_token="<|endoftext|>",
pad_token="<|endoftext|>")
eos = hf_tok.eos_token_id
ids = []
for i in range(0, len(texts), 10_000):
for seq in hf_tok(texts[i:i + 10_000])["input_ids"]:
ids.extend(seq + [eos])
data = torch.tensor(ids, dtype=torch.long)
n_val = SEQ * 2000
train_data, val_data = data[:-n_val], data[-n_val:]
print(f"{len(data)/1e6:.1f}M tokens")
# --- model ---
config = LlamaConfig(vocab_size=VOCAB, hidden_size=256, intermediate_size=512,
num_hidden_layers=6, num_attention_heads=8, num_key_value_heads=8,
max_position_embeddings=SEQ, tie_word_embeddings=True,
bos_token_id=eos, eos_token_id=eos, pad_token_id=eos)
dev = "cuda"
model = LlamaForCausalLM(config).to(dev)
n_params = sum(p.numel() for p in model.parameters())
print(f"params: {n_params/1e6:.2f}M")
opt = torch.optim.AdamW(model.parameters(), lr=LR, weight_decay=0.1, betas=(0.9, 0.95), fused=True)
warmup = 200
sched = torch.optim.lr_scheduler.LambdaLR(opt, lambda s: min(1, (s + 1) / warmup) *
(0.1 + 0.9 * 0.5 * (1 + math.cos(math.pi * min(s, STEPS) / STEPS))))
def batch(src):
ix = torch.randint(len(src) - SEQ - 1, (BATCH,))
x = torch.stack([src[i:i + SEQ] for i in ix]).to(dev, non_blocking=True)
return x
@torch.no_grad()
def evaluate():
model.eval()
losses = []
for i in range(0, len(val_data) - SEQ, SEQ * BATCH):
x = torch.stack([val_data[j:j + SEQ] for j in range(i, min(i + SEQ * BATCH, len(val_data) - SEQ), SEQ)]).to(dev)
with torch.autocast("cuda", dtype=torch.bfloat16):
losses.append(model(input_ids=x, labels=x).loss.item())
model.train()
return sum(losses) / len(losses)
t0 = time.time()
model.train()
for step in range(STEPS):
x = batch(train_data)
with torch.autocast("cuda", dtype=torch.bfloat16):
loss = model(input_ids=x, labels=x).loss
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
opt.step(); sched.step(); opt.zero_grad(set_to_none=True)
if step % 500 == 0 or step == STEPS - 1:
print(f"step {step} loss {loss.item():.3f} lr {sched.get_last_lr()[0]:.2e} {time.time()-t0:.0f}s", flush=True)
val_loss = evaluate()
print(f"val loss {val_loss:.3f} ppl {math.exp(val_loss):.2f} time {time.time()-t0:.0f}s")
model.generation_config.pad_token_id = eos
model.save_pretrained(OUT)
hf_tok.save_pretrained(OUT)
with open(os.path.join(OUT, "train_stats.txt"), "w") as f:
f.write(f"params={n_params}\ntokens={len(data)}\nsteps={STEPS}\nval_loss={val_loss:.4f}\n")
print("saved to", OUT)
|