Text Generation
Transformers
Safetensors
English
llama
feature-extraction
tiny
slm
small-language-model
sub-1m
from-scratch
gqa
custom_code
text-generation-inference
Instructions to use Compactbot/tinystories-40m with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Compactbot/tinystories-40m with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Compactbot/tinystories-40m", trust_remote_code=True)# Load model directly from transformers import AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("Compactbot/tinystories-40m", trust_remote_code=True) model = AutoModel.from_pretrained("Compactbot/tinystories-40m", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Compactbot/tinystories-40m with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Compactbot/tinystories-40m" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Compactbot/tinystories-40m", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/Compactbot/tinystories-40m
- SGLang
How to use Compactbot/tinystories-40m 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 "Compactbot/tinystories-40m" \ --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": "Compactbot/tinystories-40m", "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 "Compactbot/tinystories-40m" \ --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": "Compactbot/tinystories-40m", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use Compactbot/tinystories-40m with Docker Model Runner:
docker model run hf.co/Compactbot/tinystories-40m
|
Download README.md from Compactbot/tinystories-40m: direct link, hf CLI and curl.
- Browser
- Download file 3.56 kB
-
https://huggingface.co/Compactbot/tinystories-40m/resolve/main/README.md
- Command line
-
hf download hf://Compactbot/tinystories-40m/README.md
-
curl -L -o README.md https://huggingface.co/Compactbot/tinystories-40m/resolve/main/README.md
3.56 kB
| license: apache-2.0 | |
| library_name: transformers | |
| pipeline_tag: text-generation | |
| language: en | |
| datasets: | |
| - roneneldan/TinyStories | |
| tags: | |
| - tiny | |
| - slm | |
| - small-language-model | |
| - sub-1m | |
| - from-scratch | |
| - gqa | |
| - llama | |
| metrics: | |
| - perplexity | |
| # TinyStories-40M | |
| A **39.6M-parameter** LLaMA-style transformer trained **from scratch** on the | |
| full TinyStories corpus. Published as a from-scratch training demonstration at | |
| the ~40M scale β it is *not* a coherent story generator (see Quality below). | |
| ## Architecture | |
| | Parameter | Value | | |
| |---|---| | |
| | Layers | 12 | | |
| | Hidden size | 512 | | |
| | Attention heads (Q) | 8 | | |
| | Attention heads (KV) | 4 (GQA 2:1) | | |
| | Head dim | 64 | | |
| | FFN (SwiGLU) | 1408 | | |
| | Vocab size | 8192 (BPE) | | |
| | Context length | 512 | | |
| | RoPE ΞΈ | 10000 | | |
| | Norm | RMSNorm (pre-norm) | | |
| | Tied embeddings | Yes | | |
| | Precision | FP32 | | |
| **Total parameters: 39,596,544** (86 tensors, head weight tied to the token | |
| embedding). Verified against the published `model.safetensors`. | |
| ## Training | |
| - **Data:** roneneldan/TinyStories (~1.9B chars, ~490M tokens at seq 512) | |
| - **Steps:** 15,000 | |
| - **Batch size:** 64 sequences Γ 512 tokens (32,704 tok/step) | |
| - **Optimizer:** AdamW, lr 3e-4, cosine decay (min-lr-frac 0.1), 300-step warmup | |
| - **Hardware:** RTX 5090 (32 GB), ~4 hours | |
| - **Final train loss:** 3.23 (step 15000) | |
| ## Quality | |
| Honest picture, from running the published weights (temp 0.7, top-k 50): | |
| - On the canonical prompt **"Once upon a time"** the model produces a | |
| story-like first sentence, then degrades: | |
| > "Once upon a time, his mom and his clapped and cheered for him. They all | |
| > enjoyed spending the rest of their special day in the park, his mom's light | |
| > and a Stop being himself." | |
| - On other prompts it is weaker still β often a single short phrase or an | |
| immediate stop: | |
| > "In the forest" β "In the forest things." | |
| > "She opened the door" β "She opened the door." | |
| - Longer generations drift into incoherent, non-grammatical text. | |
| This is what a 40M from-scratch model on a single 490M-token corpus | |
| demonstrably does: it learns the surface distribution of story text (word | |
| order, names, punctuation, the "Once upon a time" register) but does not | |
| sustain coherent narrative. It is published as a training demonstration, not | |
| as a usable story generator. | |
| ## Usage | |
| This is a custom `transformers` model β load with `trust_remote_code=True`: | |
| ```python | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import torch | |
| model = AutoModelForCausalLM.from_pretrained( | |
| "Compactbot/tinystories-40m", trust_remote_code=True, torch_dtype=torch.float32 | |
| ) | |
| tok = AutoTokenizer.from_pretrained("Compactbot/tinystories-40m") | |
| prompt = "Once upon a time" | |
| ids = tok(prompt, return_tensors="pt").input_ids | |
| out = model.generate(ids, max_new_tokens=100, temperature=0.7, top_k=50) | |
| print(tok.decode(out[0], skip_special_tokens=True)) | |
| ``` | |
| Note: the bundled `generate()` always samples (no `do_sample` flag); pass | |
| `temperature` and `top_k` to control it. | |
| ## Notes | |
| - From-scratch training: initialized randomly, trained end-to-end on | |
| TinyStories. No pre-training from any other model. | |
| - GQA (Grouped Query Attention) 2:1, SwiGLU, RoPE β LLaMA-2 recipe at 40M scale. | |
| - ~151 MB in FP32 β under 200 MB. | |
| ## Files | |
| | file | what | | |
| |------|------| | |
| | `model.safetensors` | 39,596,544 params, 86 tensors, FP32 | | |
| | `config.json` | architecture + training metadata | | |
| | `modeling_tinystories.py` | the model class (loaded via `trust_remote_code`) | | |
| | `tokenizer.json` | BPE-8k tokenizer (HF `tokenizers` format) | |