--- language: - en - de - es - fr - it - nl - pl - pt - ar - hi - ja - ru - tr - vi - zh tags: - liquid - lfm2 - lfm2.5 - bidirectional - masked-lm - encoder library_name: transformers license: other license_name: lfm1.0 license_link: LICENSE pipeline_tag: fill-mask base_model: LiquidAI/LFM2.5-230M-Base ---
# LFM2.5-Encoder-230M **LFM2.5-Encoder** is a family of multilingual bidirectional encoders built on the LFM2 architecture, available in two sizes: - **LFM2.5-Encoder-230M** *(this model)* — a lightweight encoder for tight latency and memory budgets, punching above its size class. - [**LFM2.5-Encoder-350M**](https://huggingface.co/LiquidAI/LFM2.5-Encoder-350M) — a larger sibling for maximum downstream quality. Both are masked language models with full bidirectional attention, designed to be fine-tuned into task-specific models (classification, token classification, retrieval, reranking, and semantic similarity) across 15 languages, and to run efficiently on-device. Find more details about our encoders in our [blog post](https://www.liquid.ai/blog/lfm2-5-encoders). **Key highlights:** - **Highly capable for its size.** On par with the best similarly sized encoders and well ahead of our own retrieval siblings. - **General-purpose.** 8k context, strong across NLI, paraphrase, sentiment, and multilingual tasks. - **Fast and on-device.** Matches or beats ModernBERT throughput, with a long-context edge on CPU; runs in the browser on WebGPU. > [!NOTE] > 💻 **Demos**: We built the demos below from fine-tuned LFM2.5-Encoders. Each one runs in a CPU-only Hugging Face space: > - **[Zero-shot prompt routing](https://huggingface.co/spaces/LiquidAI/prompt-routing)** — define your own routing lanes as free text. The model scores the whole prompt against every lane in one pass. > - **[Zero-shot policy linting](https://huggingface.co/spaces/LiquidAI/policy-linting)** — check text against your company's rules, written as free text. It scores every token against every rule in one pass. > - **[Spell checking](https://huggingface.co/spaces/LiquidAI/spellchecker)** — correct misspellings token by token. > - **[PII detection](https://huggingface.co/spaces/LiquidAI/pii-detection)** — spot and remove 40 kinds of personal information across 16 languages. > - **[Masked-diffusion text generation](https://huggingface.co/spaces/LiquidAI/masked-diffusion)** — bonus: run the encoder as a chatbot that generates text by iteratively unmasking instead of left to right. ## 📄 Model details | Property | LFM2.5-Encoder-230M | LFM2.5-Encoder-350M | |---|---|---| | Type | Bidirectional encoder (masked language model) | Bidirectional encoder (masked language model) | | Backbone | LFM2 | LFM2 | | Total parameters | ~229.7M | ~354.5M | | Hidden size | 1024 | 1024 | | Vocabulary size | 65,536 | 65,536 | | Context length | 8,192 tokens | 8,192 tokens | | License | LFM Open License v1.0 | LFM Open License v1.0 | **Supported languages:** English, German, Spanish, French, Italian, Dutch, Polish, Portuguese, Arabic, Hindi, Japanese, Russian, Turkish, Vietnamese, Chinese (15). **Architecture.** LFM2.5-Encoder is built on the LFM2 hybrid backbone, which interleaves gated short-convolution blocks with grouped-query attention. For encoder use, the causal mask is replaced with full bidirectional (non-causal) attention and the model is trained with a masked language modeling head. The encoder body is exposed as `Lfm2BidirectionalModel`; masked-LM loading uses `Lfm2BidirectionalForMaskedLM`. Both are wired through `auto_map` and require `trust_remote_code=True`. ``` Lfm2BidirectionalForMaskedLM( (lfm2): Lfm2BidirectionalModel (lm_head): Linear(in_features=1024, out_features=65536, bias=False) ) ``` **Training.** LFM2.5-Encoder-230M is adapted from the LFM2 base and trained with a masked language modeling objective on a large multilingual corpus. Pre-training uses a two-stage schedule that extends the context window to up to 8,192 tokens. We recommend fine-tuning LFM2.5-Encoder-230M for a range of downstream tasks, such as: - **Text classification**: sentiment, topic, intent/routing, moderation, and business-text linting. - **Token classification**: named-entity recognition, span extraction, and sequence labeling. - **Retrieval and reranking**: a backbone for dense embedding or late-interaction (ColBERT-style) retrievers. - **Semantic similarity**: STS, paraphrase, and duplicate detection. - **Natural language inference and extractive QA**: sentence-pair reasoning and answer-span extraction. Its small footprint makes it especially well-suited to on-device and browser (WebGPU) deployment. ## 🏃 How to run Install the latest version of `transformers`: ```bash pip install -U transformers ``` Run masked-token prediction: ```python from transformers import AutoModelForMaskedLM, AutoTokenizer import torch tok = AutoTokenizer.from_pretrained("LiquidAI/LFM2.5-Encoder-230M", trust_remote_code=True) mlm = AutoModelForMaskedLM.from_pretrained("LiquidAI/LFM2.5-Encoder-230M", trust_remote_code=True) text = f"The capital of France is {tok.mask_token}." enc = tok(text, return_tensors="pt") with torch.no_grad(): logits = mlm(**enc).logits pos = (enc["input_ids"][0] == tok.mask_token_id).nonzero()[0].item() print([tok.decode([t]).strip() for t in logits[0, pos].topk(5).indices.tolist()]) # -> ['Paris', 'Strasbourg', 'Paris', 'Lyon', 'Versailles'] ``` For downstream tasks, load the encoder body and attach your own head (classification, token classification, regression, retrieval): ```python from transformers import AutoModel body = AutoModel.from_pretrained("LiquidAI/LFM2.5-Encoder-230M", trust_remote_code=True) ``` If your GPU supports it, we recommend using LFM2.5-Encoder-230M with Flash Attention 2 to reach the highest efficiency. To do so, install Flash Attention as follows, then use the model as normal: ```bash pip install flash-attn ``` ## 📊 Performance For each benchmark task, we run a full supervised fine-tune and report that fine-tuned model's score. The results below span 14 models across 17 tasks from GLUE, SuperGLUE, and multilingual classification tasks. The full evaluation harness is open-sourced in the [`eurobert-repro`](https://github.com/Liquid4All/encoder_eval) repository.  ### 17-task results (avg@5 fresh seeds ± std) | Rank | Model | Params | 17-task mean | ± std | |---:|---|---:|---:|---:| | 1 | XLM-R XL (3.5B) | 3.5B | 83.06 | ±1.16 | | 2 | ModernBERT-large (395M) | 395M | 81.68 | ±2.49 | | 3 | XLM-R large (560M) | 560M | 81.34 | ±1.66 | | 4 | LFM2.5-Encoder-350M (ours) | 350M | 81.02 | ±1.00 | | 5 | mDeBERTa-v3 (280M) | 280M | 80.37 | ±1.06 | | **6** | **LFM2.5-Encoder-230M (ours)** | 230M | **79.29** | ±1.02 | | 7 | ModernBERT-base (149M) | 149M | 78.19 | ±1.39 | | 8 | XLM-R base (280M) | 280M | 77.46 | ±1.63 | | 9 | EuroBERT-210M | 210M | 76.87 | ±2.00 | | 10 | mGTE-MLM (305M) | 305M | 76.53 | ±1.85 | | 11 | LFM2.5-ColBERT-350M | 350M | 76.18 | ±1.25 | | 12 | EuroBERT-610M | 610M | 75.87 | ±2.03 | | 13 | LFM2.5-Embedding-350M | 350M | 75.68 | ±0.83 | | 14 | EuroBERT-2.1B | 2.1B | 72.19 | ±5.59 |