Text Generation
Transformers
Safetensors
English
llama
small-language-model
educational
from-scratch
text-generation-inference
Instructions to use tegancao/tinystories-22m with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use tegancao/tinystories-22m with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="tegancao/tinystories-22m")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("tegancao/tinystories-22m") model = AutoModelForCausalLM.from_pretrained("tegancao/tinystories-22m", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use tegancao/tinystories-22m with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "tegancao/tinystories-22m" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "tegancao/tinystories-22m", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/tegancao/tinystories-22m
- SGLang
How to use tegancao/tinystories-22m 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 "tegancao/tinystories-22m" \ --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": "tegancao/tinystories-22m", "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 "tegancao/tinystories-22m" \ --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": "tegancao/tinystories-22m", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use tegancao/tinystories-22m with Docker Model Runner:
docker model run hf.co/tegancao/tinystories-22m
Initial upload: weights (safetensors), tokenizer, model card
Browse files- README.md +102 -0
- config.json +32 -0
- generation_config.json +10 -0
- model.safetensors +3 -0
- tokenizer.json +0 -0
- tokenizer_config.json +9 -0
README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
library_name: transformers
|
| 6 |
+
pipeline_tag: text-generation
|
| 7 |
+
datasets:
|
| 8 |
+
- roneneldan/TinyStories
|
| 9 |
+
tags:
|
| 10 |
+
- llama
|
| 11 |
+
- small-language-model
|
| 12 |
+
- educational
|
| 13 |
+
- from-scratch
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
# TinyStories 22M
|
| 17 |
+
|
| 18 |
+
A **22.7M parameter** language model trained **from scratch** on [TinyStories](https://huggingface.co/datasets/roneneldan/TinyStories) (V2, GPT-4-generated short children's stories), as a learning project. It is a
|
| 19 |
+
small **educational** model: useful for learning how language models work and for quick experiments, **not** for
|
| 20 |
+
any real application. Its stories are fluent and usually complete, with simple plots and morals.
|
| 21 |
+
|
| 22 |
+
## How to use
|
| 23 |
+
|
| 24 |
+
```python
|
| 25 |
+
import torch
|
| 26 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 27 |
+
|
| 28 |
+
tok = AutoTokenizer.from_pretrained("tegancao/tinystories-22m")
|
| 29 |
+
model = AutoModelForCausalLM.from_pretrained("tegancao/tinystories-22m")
|
| 30 |
+
|
| 31 |
+
torch.manual_seed(0)
|
| 32 |
+
ids = tok("<|endoftext|>" + "Once upon a time, a little fox", return_tensors="pt").input_ids # start like a new document
|
| 33 |
+
out = model.generate(ids, max_new_tokens=100, do_sample=True, temperature=0.8, top_p=0.9)
|
| 34 |
+
print(tok.decode(out[0, 1:], skip_special_tokens=True))
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
Sample output (the code above, as is):
|
| 38 |
+
|
| 39 |
+
```text
|
| 40 |
+
Once upon a time, a little fox named Tim went to the woods to find food. He was very hungry and wanted to eat. He walked and walked until he saw a big, red apple. Tim thought, "This apple looks delicious!"
|
| 41 |
+
Tim tried to take the apple, but it was too heavy. He tried and tried, but he could not move it. Then, a friendly bird named Sue saw Tim. Sue flew down and said, "I can help you!"
|
| 42 |
+
Sue helped Tim get the apple. They took it
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
It is a **base model**: it continues text and does not follow instructions or answer questions. Prompts that look
|
| 46 |
+
like the start of its training documents work best.
|
| 47 |
+
|
| 48 |
+
## Training
|
| 49 |
+
|
| 50 |
+
| | |
|
| 51 |
+
|---|---|
|
| 52 |
+
| Training data | [TinyStories](https://huggingface.co/datasets/roneneldan/TinyStories) (V2, GPT-4-generated short children's stories) |
|
| 53 |
+
| Tokenizer | byte-level BPE trained on the same data, 10,000-token vocabulary, GPT-2 pre-tokenization pattern; `<|endoftext|>` separates documents |
|
| 54 |
+
| Architecture | Llama-style decoder: 4 layers, d_model 512, 16 heads (head dim 32), SwiGLU FFN (d_ff 1344), RMSNorm (pre-norm), RoPE (θ = 10,000), no biases, untied input/output embeddings |
|
| 55 |
+
| Context length | 256 tokens |
|
| 56 |
+
| Optimizer | AdamW (β₁ 0.9, β₂ 0.95, ε 1e-8, weight decay 0.1), gradient clipping at 1.0 |
|
| 57 |
+
| Learning rate | 3e-3 peak, 1,000 linear warm-up steps, cosine decay to 3e-4 |
|
| 58 |
+
| Batch | 32 sequences × 256 tokens = 8,192 tokens per step |
|
| 59 |
+
| Steps / tokens | 40,000 steps = 327.68M tokens |
|
| 60 |
+
| Precision / hardware | float32, one NVIDIA L4 GPU |
|
| 61 |
+
| Training time | ~3.1 hours |
|
| 62 |
+
|
| 63 |
+
The model was implemented and trained with my own from-scratch PyTorch code, then converted to the standard
|
| 64 |
+
`LlamaForCausalLM` format (RoPE pairs reordered in the Q/K projections). The conversion was verified: the converted
|
| 65 |
+
model's logits match the original within ~2 × 10⁻⁵, greedy generations are identical, and the tokenizer produces
|
| 66 |
+
identical token ids on 2 MB of validation text.
|
| 67 |
+
|
| 68 |
+
## Evaluation
|
| 69 |
+
|
| 70 |
+
| Metric | Value |
|
| 71 |
+
|---|---|
|
| 72 |
+
| Validation loss (cross-entropy, nats per token) | **1.382** |
|
| 73 |
+
| Validation perplexity | 4.0 |
|
| 74 |
+
|
| 75 |
+
Measured on held-out validation text at the end of training, averaged over 20 random batches (about 164K tokens).
|
| 76 |
+
The model saw about 61% of its training data (one partial pass).
|
| 77 |
+
|
| 78 |
+
## Intended use
|
| 79 |
+
|
| 80 |
+
- Learning how language models work: inspecting a small model, experimenting with sampling (temperature, top-p),
|
| 81 |
+
seeing how training data shapes behavior.
|
| 82 |
+
- Fast experiments that need a tiny model: decoding methods, interpretability, testing inference code.
|
| 83 |
+
- Demos of what a model trained from scratch on a small budget can and cannot do.
|
| 84 |
+
|
| 85 |
+
## Out of scope
|
| 86 |
+
|
| 87 |
+
- **Any factual use:** the model makes things up.
|
| 88 |
+
- **Anything user-facing or in production:** there is no instruction tuning and no safety tuning.
|
| 89 |
+
- Decisions about people, or any application where mistakes matter.
|
| 90 |
+
|
| 91 |
+
## Limitations
|
| 92 |
+
|
| 93 |
+
- Continues text instead of answering it (base model, no instruction tuning).
|
| 94 |
+
- Loose logic and weak tracking of who is who over longer passages; falls into repetition loops, especially with
|
| 95 |
+
greedy decoding.
|
| 96 |
+
- Sees at most the last 256 tokens.
|
| 97 |
+
- Knows only the world of simple children's stories: no general knowledge.
|
| 98 |
+
|
| 99 |
+
## Acknowledgments
|
| 100 |
+
|
| 101 |
+
The architecture and training setup follow the publicly available materials of Stanford CS336 (Language Modeling
|
| 102 |
+
from Scratch). Training data: [TinyStories](https://huggingface.co/datasets/roneneldan/TinyStories) (V2, GPT-4-generated short children's stories). Please see the dataset's own card for its license and terms.
|
config.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"LlamaForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_bias": false,
|
| 6 |
+
"attention_dropout": 0.0,
|
| 7 |
+
"bos_token_id": 256,
|
| 8 |
+
"dtype": "float32",
|
| 9 |
+
"eos_token_id": 256,
|
| 10 |
+
"head_dim": 32,
|
| 11 |
+
"hidden_act": "silu",
|
| 12 |
+
"hidden_size": 512,
|
| 13 |
+
"initializer_range": 0.02,
|
| 14 |
+
"intermediate_size": 1344,
|
| 15 |
+
"max_position_embeddings": 256,
|
| 16 |
+
"mlp_bias": false,
|
| 17 |
+
"model_type": "llama",
|
| 18 |
+
"num_attention_heads": 16,
|
| 19 |
+
"num_hidden_layers": 4,
|
| 20 |
+
"num_key_value_heads": 16,
|
| 21 |
+
"pad_token_id": null,
|
| 22 |
+
"pretraining_tp": 1,
|
| 23 |
+
"rms_norm_eps": 1e-05,
|
| 24 |
+
"rope_parameters": {
|
| 25 |
+
"rope_theta": 10000.0,
|
| 26 |
+
"rope_type": "default"
|
| 27 |
+
},
|
| 28 |
+
"tie_word_embeddings": false,
|
| 29 |
+
"transformers_version": "5.19.0",
|
| 30 |
+
"use_cache": true,
|
| 31 |
+
"vocab_size": 10000
|
| 32 |
+
}
|
generation_config.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 256,
|
| 4 |
+
"eos_token_id": 256,
|
| 5 |
+
"output_attentions": false,
|
| 6 |
+
"output_hidden_states": false,
|
| 7 |
+
"pad_token_id": 256,
|
| 8 |
+
"transformers_version": "5.19.0",
|
| 9 |
+
"use_cache": true
|
| 10 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a0cf564778b26ab6d33b3decd60ac16610f97cbbe4a4104f697cb11245c20fd7
|
| 3 |
+
size 90790024
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"backend": "tokenizers",
|
| 3 |
+
"bos_token": "<|endoftext|>",
|
| 4 |
+
"clean_up_tokenization_spaces": false,
|
| 5 |
+
"eos_token": "<|endoftext|>",
|
| 6 |
+
"model_max_length": 256,
|
| 7 |
+
"tokenizer_class": "TokenizersBackend",
|
| 8 |
+
"unk_token": null
|
| 9 |
+
}
|