codechrl/modernbert-tiny
067
1---2license: apache-2.03base_model: answerdotai/ModernBERT-base4base_model_relation: finetune5library_name: transformers6pipeline_tag: fill-mask7language:8- en9datasets:10- HuggingFaceFW/fineweb-edu11tags:12- modernbert13- distillation14- knowledge-distillation15- model-compression16- fill-mask17---18# modernbert-tiny19 20Smallest ModernBERT (TinyBERT-style distillation) — for edge / low-latency.21 22A **compressed, fine-tunable base encoder** derived from [`answerdotai/ModernBERT-base`](https://huggingface.co/answerdotai/ModernBERT-base) — the *fork/derivative*:23**15.3% of the teacher's size** while keeping **80.4% of its GLUE quality**. Use it as a general base and24fine-tune on your downstream task, exactly like ModernBERT-base.25 26## The family (one exercise)27 28All three were produced in **one ModernBERT compression exercise** — same teacher ([`answerdotai/ModernBERT-base`](https://huggingface.co/answerdotai/ModernBERT-base)), same FineWeb-Edu corpus, same GLUE eval — comparing different compression methods. **Pick the tier that fits your size/quality budget:**29 30- [`codechrl/modernbert-tiny`](https://huggingface.co/codechrl/modernbert-tiny) ← **you are here** — 22.1M params, 15.3% of base size, 80.4% GLUE retained · TinyBERT-style attention+hidden distillation31- [`codechrl/modernbert-mini`](https://huggingface.co/codechrl/modernbert-mini) — 69.4M params, 46.7% of base size, 92.9% GLUE retained · DistilBERT-style depth distillation32- [`codechrl/modernbert-lite`](https://huggingface.co/codechrl/modernbert-lite) — 149.7M params, 50.3% of base size, 99.3% GLUE retained · fp16 half-precision quantization33 34## How it was made (general process)35 361. **Teacher** — `answerdotai/ModernBERT-base` (149.7M params), the distillation target.372. **General-corpus distillation** — the student learns from the teacher on **FineWeb-Edu** (general English web38 text) using the `tinybert` recipe. No task-/domain-specific data, so it stays a general base.393. **Evaluation** — quality measured on **GLUE** (SST-2, MRPC, STS-B, RTE; each model fine-tuned identically),40 reported purely as **% retained vs the teacher**.41 42## Scores (% against the ModernBERT-base teacher)43 44- **Size:** 92.0 MB → **15.3% of baseline** (params 22.1M)45- **GLUE quality retained:** **80.4%**46- **eff_score:** 82.6 / 100 = `0.5 · GLUE_retention% + 0.5 · size_reduction%` (higher is better)47 48### Full tier comparison49 50| model | params (M) | size (MB) | size vs base | GLUE vs base | eff_score |51|---|---|---|---|---|---|52| `ModernBERT-base` (teacher) | 149.7 | 602.2 | 100% | 100% | 50.0 |53| **modernbert-tiny** ⭐ | 22.1 | 92.0 | 15.3% | 80.4% | 82.6 |54| `modernbert-mini` | 69.4 | 281.2 | 46.7% | 92.9% | 73.1 |55| `modernbert-lite` | 149.7 | 302.9 | 50.3% | 99.3% | 74.5 |56 57## Methods & architecture (each tier)58 59Every tier derives from the **same teacher** but uses a different compression method:60 61### `modernbert-tiny` ⭐62*4 transformer layers, hidden size 312, 12 heads (~22M params)*63 64**TinyBERT-style distillation.** A small student mimics multiple internal signals of the teacher: token embeddings, per-layer hidden states (compared L2-normalized for stability), attention probability maps, and output-logit KL. This deep multi-signal supervision lets a much narrower/shallower network recover usable quality.65 66### `modernbert-mini`67*6 transformer layers, hidden size 768 (~69M params)*68 69**DistilBERT-style distillation.** The 6-layer student is initialized from evenly-spaced teacher layers, then trained with masked-LM loss + soft-logit KL divergence + last-hidden cosine. Depth-only reduction (full width kept) is the best quality-per-byte recipe here.70 71### `modernbert-lite`72*full ModernBERT (22 layers, hidden 768, ~150M params), weights stored in float16*73 74**Half-precision (fp16) quantization.** No retraining — weights are cast to 16-bit, roughly halving storage and memory with near-zero quality loss. Re-load in fp32 (or bf16) to fine-tune.75 76 77## Usage78 79```python80from transformers import AutoModelForMaskedLM, AutoTokenizer81tok = AutoTokenizer.from_pretrained("codechrl/modernbert-tiny")82model = AutoModelForMaskedLM.from_pretrained("codechrl/modernbert-tiny")83 84# fine-tune for your task:85# from transformers import AutoModelForSequenceClassification86# clf = AutoModelForSequenceClassification.from_pretrained("codechrl/modernbert-tiny", num_labels=N)87```88 89## Intended use & limitations90 91- **A base to fine-tune**, not a finished classifier.92- Distilled on a **small compute budget** (demo-grade); for production, redistill with more steps/corpus.93- `tiny` trades the most quality for the smallest size; `mini`/`lite` retain more.94 95## Citation96 97Built on ModernBERT (Warner et al., 2024). Distillation recipes: DistilBERT (Sanh 2019), TinyBERT (Jiao 2020).98 