DeepMount00 commited on
Commit
13b8f54
·
verified ·
1 Parent(s): 2b1c4af

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +135 -0
README.md ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ license: apache-2.0
4
+ language:
5
+ - en
6
+ - it
7
+ tags:
8
+ - fill-mask
9
+ - masked-lm
10
+ - long-context
11
+ - modernbert
12
+ pipeline_tag: fill-mask
13
+ ---
14
+
15
+ # ModernBERT
16
+
17
+ ## Table of Contents
18
+ 1. [Model Summary](#model-summary)
19
+ 2. [Usage](#Usage)
20
+ 3. [Evaluation](#Evaluation)
21
+ 4. [Limitations](#limitations)
22
+ 5. [Training](#training)
23
+ 6. [License](#license)
24
+ 7. [Citation](#citation)
25
+
26
+ ## Model Summary
27
+
28
+ ModernBERT is a modernized bidirectional encoder-only Transformer model (BERT-style) pre-trained on 2 trillion tokens of English and code data with a native context length of up to 8,192 tokens. ModernBERT leverages recent architectural improvements such as:
29
+
30
+ - **Rotary Positional Embeddings (RoPE)** for long-context support.
31
+ - **Local-Global Alternating Attention** for efficiency on long inputs.
32
+ - **Unpadding and Flash Attention** for efficient inference.
33
+
34
+ ModernBERT’s native long context length makes it ideal for tasks that require processing long documents, such as retrieval, classification, and semantic search within large corpora. The model was trained on a large corpus of text and code, making it suitable for a wide range of downstream tasks, including code retrieval and hybrid (text + code) semantic search.
35
+
36
+ It is available in the following sizes:
37
+
38
+ - [ModernBERT-base](https://huggingface.co/answerdotai/ModernBERT-base) - 22 layers, 149 million parameters
39
+ - [ModernBERT-large](https://huggingface.co/answerdotai/ModernBERT-large) - 28 layers, 395 million parameters
40
+
41
+ For more information about ModernBERT, we recommend our [release blog post](https://huggingface.co/blog/modernbert) for a high-level overview, and our [arXiv pre-print](https://arxiv.org/abs/2412.13663) for in-depth information.
42
+
43
+ *ModernBERT is a collaboration between [Answer.AI](https://answer.ai), [LightOn](https://lighton.ai), and friends.*
44
+
45
+ ## Usage
46
+
47
+ You can use these models directly with the `transformers` library. Until the next `transformers` release, doing so requires installing transformers from main:
48
+
49
+ ```sh
50
+ pip install git+https://github.com/huggingface/transformers.git
51
+ ```
52
+
53
+ Since ModernBERT is a Masked Language Model (MLM), you can use the `fill-mask` pipeline or load it via `AutoModelForMaskedLM`. To use ModernBERT for downstream tasks like classification, retrieval, or QA, fine-tune it following standard BERT fine-tuning recipes.
54
+
55
+ **⚠️ If your GPU supports it, we recommend using ModernBERT with Flash Attention 2 to reach the highest efficiency. To do so, install Flash Attention as follows, then use the model as normal:**
56
+
57
+ ```bash
58
+ pip install flash-attn
59
+ ```
60
+
61
+ Using `AutoModelForMaskedLM`:
62
+
63
+ ```python
64
+ from transformers import AutoTokenizer, AutoModelForMaskedLM
65
+
66
+ model_id = "DeepMount00/ModernBERT-base-ita"
67
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
68
+ model = AutoModelForMaskedLM.from_pretrained(model_id)
69
+
70
+ text = "La capitale dell'Italia è [MASK]."
71
+ inputs = tokenizer(text, return_tensors="pt")
72
+ outputs = model(**inputs)
73
+
74
+ # To get predictions for the mask:
75
+ masked_index = inputs["input_ids"][0].tolist().index(tokenizer.mask_token_id)
76
+ predicted_token_id = outputs.logits[0, masked_index].argmax(axis=-1)
77
+ predicted_token = tokenizer.decode(predicted_token_id)
78
+ print("Predicted token:", predicted_token)
79
+ # Predicted token: Paris
80
+ ```
81
+
82
+ Using a pipeline:
83
+
84
+ ```python
85
+ import torch
86
+ from transformers import pipeline
87
+ from pprint import pprint
88
+
89
+ pipe = pipeline(
90
+ "fill-mask",
91
+ model="answerdotai/ModernBERT-base",
92
+ torch_dtype=torch.bfloat16,
93
+ )
94
+
95
+ input_text = "He walked to the [MASK]."
96
+ results = pipe(input_text)
97
+ pprint(results)
98
+ ```
99
+
100
+ **Note:** ModernBERT does not use token type IDs, unlike some earlier BERT models. Most downstream usage is identical to standard BERT models on the Hugging Face Hub, except you can omit the `token_type_ids` parameter.
101
+
102
+ ## Evaluation
103
+
104
+ We evaluate ModernBERT across a range of tasks, including natural language understanding (GLUE), general retrieval (BEIR), long-context retrieval (MLDR), and code retrieval (CodeSearchNet and StackQA).
105
+
106
+ **Key highlights:**
107
+ - On GLUE, ModernBERT-base surpasses other similarly-sized encoder models, and ModernBERT-large is second only to Deberta-v3-large.
108
+ - For general retrieval tasks, ModernBERT performs well on BEIR in both single-vector (DPR-style) and multi-vector (ColBERT-style) settings.
109
+ - Thanks to the inclusion of code data in its training mixture, ModernBERT as a backbone also achieves new state-of-the-art code retrieval results on CodeSearchNet and StackQA.
110
+
111
+
112
+ ## Limitations
113
+
114
+ ModernBERT’s training data is primarily English and code, so performance may be lower for other languages. While it can handle long sequences efficiently, using the full 8,192 tokens window may be slower than short-context inference. Like any large language model, ModernBERT may produce representations that reflect biases present in its training data. Verify critical or sensitive outputs before relying on them.
115
+
116
+
117
+ ## License
118
+
119
+ We release the ModernBERT model architectures, model weights, training codebase under the Apache 2.0 license.
120
+
121
+ ## Citation
122
+
123
+ If you use ModernBERT in your work, please cite:
124
+
125
+ ```
126
+ @misc{modernbert,
127
+ title={Smarter, Better, Faster, Longer: A Modern Bidirectional Encoder for Fast, Memory Efficient, and Long Context Finetuning and Inference},
128
+ author={Benjamin Warner and Antoine Chaffin and Benjamin Clavié and Orion Weller and Oskar Hallström and Said Taghadouini and Alexis Gallagher and Raja Biswas and Faisal Ladhak and Tom Aarsen and Nathan Cooper and Griffin Adams and Jeremy Howard and Iacopo Poli},
129
+ year={2024},
130
+ eprint={2412.13663},
131
+ archivePrefix={arXiv},
132
+ primaryClass={cs.CL},
133
+ url={https://arxiv.org/abs/2412.13663},
134
+ }
135
+ ```