--- pipeline_tag: fill-mask license: apache-2.0 base_model: answerdotai/ModernBERT-base library_name: kerasformers tags: - keras - kerasformers - modernbert - fill-mask - text-encoder - arxiv:2412.13663 - pytorch - jax - tf --- # Run ModernBERT with Keras 3: JAX, PyTorch, or TensorFlow [![GitHub](https://img.shields.io/badge/GitHub-KerasFormers-black?logo=github)](https://github.com/IMvision12/KerasFormers) [![Docs](https://img.shields.io/badge/Docs-ModernBERT-blue)](https://imvision12.github.io/KerasFormers/modernbert/) # kerasformers/modernbert_base Paper: [Smarter, Better, Faster, Longer: A Modern Bidirectional Encoder (arXiv:2412.13663)](https://arxiv.org/abs/2412.13663) · [HF Papers](https://huggingface.co/papers/2412.13663) ModernBERT is Answer.AI / LightOn's modernized bidirectional transformer text encoder: rotary position embeddings, attention that alternates between a global (full) layer and local sliding-window layers, GeGLU feed-forwards, and pre-LayerNorm, with an 8192-token context. Byte-level BPE tokenizer; mask token `[MASK]`. No token-type ids. For more details on the model, please go to the upstream [model card](https://huggingface.co/answerdotai/ModernBERT-base). Pure-**Keras 3** conversion of [`answerdotai/ModernBERT-base`](https://huggingface.co/answerdotai/ModernBERT-base) for [kerasformers](https://github.com/IMvision12/KerasFormers). One implementation runs unmodified on **TensorFlow / Torch / JAX**. This is a **fill-mask / encoder** checkpoint (`ModernBertMaskedLM`, modernbert_base). Task heads (sequence / token classify, QA, multiple choice) load via `hf:` fine-tunes. ## ✨ Quick start (fill-mask) ```python import os os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow" from kerasformers.models.modernbert import ModernBertMaskedLM, ModernBertTokenizer mlm = ModernBertMaskedLM.from_weights("kerasformers/modernbert_base") tokenizer = ModernBertTokenizer.from_weights("kerasformers/modernbert_base") inputs = tokenizer("The capital of France is [MASK].") logits = mlm(inputs) # (1, L, vocab_size) mask = int((inputs["input_ids"][0] == tokenizer.mask_token_id).argmax()) print(tokenizer.decode([int(logits[0, mask].argmax())])) ``` Load any ModernBERT variant the same way with `from_weights("kerasformers/")`: | Variant | Hub | layers | embed_dim | |---|---|---|---| | `modernbert_base` | [`kerasformers/modernbert_base`](https://huggingface.co/kerasformers/modernbert_base) | 22 | 768 | | `modernbert_large` | [`kerasformers/modernbert_large`](https://huggingface.co/kerasformers/modernbert_large) | 28 | 1024 | ## Available classes Load any of these from this repo with `from_weights("kerasformers/modernbert_base")` (or on the fly via the `hf:` prefix). The pretrained backbone is shared; task heads not stored in this checkpoint start randomly initialized, ready for fine-tuning (or load a `hf:` fine-tune). | Class | Task | |---|---| | `ModernBertModel` | Encoder backbone | | `ModernBertMaskedLM` | Masked language modeling (fill-mask) | | `ModernBertSequenceClassify` | Sequence classification | | `ModernBertTokenClassify` | Token classification (NER / POS) | | `ModernBertQnA` | Extractive question answering | | `ModernBertMultipleChoice` | Multiple choice | ```python from kerasformers.models.modernbert import ModernBertSequenceClassify model = ModernBertSequenceClassify.from_weights("kerasformers/modernbert_base") ``` ## Tips - Set `KERAS_BACKEND` **before** importing Keras / kerasformers. - Prefer `ModernBertTokenizer.from_weights(...)` so tokenization matches. - Use `[MASK]` (not ``). - ModernBERT has no token-type ids; the tokenizer emits only `input_ids` / `attention_mask`. - See [ModernBERT docs](https://imvision12.github.io/KerasFormers/modernbert/) and [Loading Weights](https://imvision12.github.io/KerasFormers/loading_weights/). - Community / upstream safetensors still work via the `hf:` prefix, e.g. `ModernBertMaskedLM.from_weights("hf:answerdotai/ModernBERT-base")`. ## Special Thanks A huge thank you to the Answer.AI and LightOn authors for creating and releasing ModernBERT. License: Apache 2.0.