|
Download README.md from Kawly33/Mochi-20M: direct link, hf CLI and curl.
- Browser
- Download file 6.31 kB
-
https://huggingface.co/Kawly33/Mochi-20M/resolve/main/README.md
- Command line
-
hf download hf://Kawly33/Mochi-20M/README.md
-
curl -L -o README.md https://huggingface.co/Kawly33/Mochi-20M/resolve/main/README.md
6.31 kB
| license: apache-2.0 | |
| language: | |
| - en | |
| pipeline_tag: text-generation | |
| library_name: numpy | |
| tags: | |
| - tiny-llm | |
| - numpy | |
| - from-scratch | |
| - chat | |
| - chain-of-thought | |
| - scratch | |
| - 20M | |
| # Mochi AI 20M | |
| Mochi AI 20M is a small chat language model (~22.9M parameters): a decoder-only | |
| transformer (originally built for Scratch, then fine-tuned), with **pure NumPy inference** (no PyTorch, | |
| no `transformers`). It answers short questions and holds simple conversations, | |
| and uses **chain-of-thought (CoT)**: it writes a brief `<think>...</think>` | |
| reasoning line before giving its final answer. It can sometimes anwser complex questions correctly. | |
| ## Usage | |
| ```bash | |
| pip install numpy | |
| python mochi_run_ftv5.py "What is the capital of France?" | |
| # -> <think>the capital of France: Paris</think> The capital of France is Paris. | |
| python mochi_run_ftv5.py # interactive chat mode | |
| ``` | |
| Options: | |
| | Flag | Default | Description | | |
| |---|---|---| | |
| | `--temp` | 0.5 | Sampling temperature | | |
| | `--topk` | 5 | Top-k sampling | | |
| | `--max` | 128 | Maximum new tokens | | |
| | `--seed` | none | Random seed for reproducibility | | |
| | `--scratch-compat` | off | Lowercases the input, to match the Scratch version of the model | | |
| From Python: | |
| ```python | |
| from mochi_run_ftv5 import Mochi | |
| m = Mochi() | |
| print(m.generate("Hello!", temp=0.5, topk=5, max_new=128)) | |
| ``` | |
| ## Model details | |
| | | | | |
| |---|---| | |
| | Architecture | Decoder-only transformer, pre-norm (RMSNorm), learned positional embeddings | | |
| | Parameters | ~22.9M | | |
| | Layers | 12 | | |
| | Hidden size (d_model) | 384 | | |
| | Attention heads | 6 (head dim 64) | | |
| | Feed-forward size | 1536 (quick-GELU activation) | | |
| | Vocabulary | 4096 (BPE-style merges, byte fallback tokens) | | |
| | Context length | 256 tokens | | |
| | Embeddings | Tied input/output | | |
| | Residual scaling | 0.204124 | | |
| | Weights | float16 (44 MB), computed in float32 at inference | | |
| | Default sampling | temperature 0.5, top-k 5, max 128 new tokens | | |
| ## Training data | |
| The fine-tuning data was written from scratch by me and claude. No third-party datasets were used to fine-tune. | |
| The model was fine-tuned using the Scratch version of Mochi: | |
| <https://scratch.mit.edu/projects/1299699155/>. The checkpoint in this | |
| repository (`ftv5`) is the fifth fine-tuning iteration. | |
| ## Chain of thought | |
| Answers follow the format `<think>short reasoning</think> final answer`. | |
| The `<think>` part is generated by the model itself and is kept in the output | |
| of `mochi_run_ftv5.py`. To show only the final answer, strip it, for example: | |
| ```python | |
| import re | |
| answer = re.sub(r"<think>.*?</think>\s*", "", output, flags=re.S) | |
| ``` | |
| Like the answer itself, the reasoning can be wrong. | |
| ## Capabilities | |
| Thanks to its chain-of-thought training, the model can sometimes handle more | |
| than simple greetings, for example: | |
| - **Geography:** capitals, continents, rivers (e.g. "What is the capital of Japan?"). | |
| - **Simple word problems / arithmetic:** it reasons step by step in the | |
| `<think>` part (e.g. "If I have 3 apples and buy 5 more..." -> `3+5=8`). | |
| - **Basic science and general knowledge:** e.g. why the sky is blue. | |
| - **Culture questions:** it can answer some of them when they are covered by its | |
| training data. | |
| Example: | |
| ``` | |
| Q: Which continent is Brazil in? | |
| A: <think>continent of Brazil: South America</think> Brazil is in South America. | |
| ``` | |
| Results vary between runs (sampling is random unless you set `--seed`) and are | |
| best on topics close to the training data. | |
| ## Evaluation | |
| A small, informal benchmark of 54 short questions (`eval_mochi.py`; raw outputs in | |
| `results.json`): geography (16), math and word problems (12), science (10), | |
| culture (10) and conversation (6). An answer counts as correct if the final | |
| answer (after `</think>`) contains an accepted keyword, e.g. "Paris" or "8". | |
| Two settings were tested: greedy decoding (1 run) and the default sampling | |
| (`temp 0.5`, `top-k 5`, 5 seeds, 270 generations). | |
| | Category | Greedy | Default sampling (5 seeds) | | |
| |---|---|---| | |
| | Geography | 93.8% (15/16) | 93.8% (75/80) | | |
| | Conversation | 100% (6/6) | 100% (30/30) | | |
| | Culture | 70.0% (7/10) | 70.0% (35/50) | | |
| | Math / word problems | 75.0% (9/12) | 61.7% (37/60) | | |
| | Science | 60.0% (6/10) | 52.0% (26/50) | | |
| | **Overall** | **79.6% (43/54)** | **75.2% (203/270)** | | |
| Other results: | |
| - Chain-of-thought format (`<think>...</think>` then an answer): **100%** of generations, 0 empty answers. | |
| - Consistency under sampling: 38 of 54 questions were answered correctly on all 5 seeds; 10 were never answered correctly. | |
| - Speed: about 0.5 s per answer on a CPU, pure NumPy (measured in a cloud container, so your machine may differ). | |
| Typical failures: simple arithmetic slips (`9-4=3`), the reasoning and the answer | |
| agreeing with each other even when both are wrong, and confident made-up | |
| definitions for rarer cultural terms (e.g. "Sushi is a water bird"). | |
| **Caveats:** the question set is small and written by hand by the evaluator, not | |
| an established benchmark, and keyword matching can miss correct paraphrases or | |
| accept lucky matches. The questions are not guaranteed to be absent from the | |
| training data, so the scores reflect performance on familiar-style questions, | |
| not general knowledge. Do not compare them with scores on standard benchmarks | |
| (MMLU, GSM8K, etc.), which this model is not expected to do well on. | |
| ## Intended use and limitations | |
| - A hobby / educational model: a small, readable example of a complete LLM | |
| you can run on a CPU with only NumPy. | |
| - With only ~23M parameters and a small hand-written dataset, it **often fails | |
| on topics it was not trained on**. It may invent an answer with confidence | |
| (for example, a made-up definition for a cultural term), give wrong or | |
| repetitive text, or answer an unrelated question. | |
| - Multi-step problems may work in simple cases but are not reliable; always | |
| check the result. | |
| - English only. Inputs longer than the 256-token context are not supported. | |
| - Not suitable for medical, legal, financial or other high-stakes decisions. | |
| ## Files | |
| - `mochi20m_ftv5_fp16.npz`: model weights | |
| - `mochi_tokenizer.json`: tokenizer (vocab, merges) and model config | |
| - `mochi_run_ftv5.py`: inference script | |
| - `requirements.txt`: dependencies (`numpy`) | |
| - `eval_mochi.py` / `results.json`: evaluation script and raw results | |
| ## License | |
| Released under the [Apache License 2.0](LICENSE). |