BERTomelo

BERTomelo is a variant of BERT encoders built upon the ModernBERT architecture. It is pretrained from scratch on the Classified Common Crawl Corpus for Portuguese (ClassiCC PT), specifically tailored and specialized for the Portuguese language. BERTomelo try to fill the gap of older Brazilian BERT models with the native long context capability, making it ideal for tasks that require processing extensive documents, mirroring the efficiency of the ModernBERT reference architecture.

Architectural Improvements

BERTomelo leverages several recent advancements in transformer design integrated into the ModernBERT architecture, such as:

  • Rotary Positional Embeddings (RoPE): Facilitates robust long context support by encoding positional information through rotation, allowing for better extrapolation to larger sequence lengths.
  • Local Global Alternating Attention: Optimizes computational efficiency by alternating between local attention (focusing on neighboring tokens) and global attention (capturing dependencies across the entire sequence).
  • Unpadding and FlashAttention 2: Enhances hardware level efficiency by removing unnecessary padding tokens and utilizing an IO aware attention implementation to maximize token throughput.

The pretraining of BERTomelo encompasses 386.48 billion tokens. The custom tokenizer is sourced from ModBERTBr, trained using a Unigram algorithm on the BrWaC corpus and the Wikipedia Portuguese dataset.

Available Models

Model Variant Layers Parameters Context Window
BERTomelo Base 22 136 Million 1,024 tokens
BERTomelo Large 28 377 Million 1,024 tokens

Usage

You can use BERTomelo leveraging the transformers library since version v4.48.0:

pip install -U transformers>=4.48.0

Since BERTomelo is pretrained using a Masked Language Model (MLM) objective, it can be used directly for inference via AutoModelForMaskedLM or the fill-mask pipeline. It also serves as a robust backbone for fine tuning on downstream tasks such as classification, textual similarity, retrieval, or question answering.

⚠️ Note: We recommend using FlashAttention 2 to achieve the highest processing efficiency and throughput.

Using AutoModelForMaskedLM:

from transformers import AutoTokenizer, AutoModelForMaskedLM

model_id = "unb-labia/BERTomelo-ModernBERT-Base-v1"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForMaskedLM.from_pretrained(model_id)
text = "O cachorro mais famoso do brasil é o vira [MASK]."
inputs = tokenizer(text, return_tensors="pt")
outputs = model(**inputs)

masked_index = inputs["input_ids"][0].tolist().index(tokenizer.mask_token_id)
predicted_token_id = outputs.logits[0, masked_index].argmax(axis=-1)
predicted_token = tokenizer.decode(predicted_token_id)
print("Predicted token:", predicted_token)

Using a pipeline:

import torch
from transformers import pipeline
from pprint import pprint
pipe = pipeline(
    "fill-mask",
    model="unb-labia/BERTomelo-ModernBERT-Base-v1",
)
input_text = "O Brasil está presente na [MASK] do sul."
results = pipe(input_text)

for result in results:
    print(f"Token: {result['token_str']} | Score: {result['score']:.4f}")

Note: Unlike traditional BERT models, the ModernBERT architecture utilized by BERTomelo does not require token type IDs. You can omit this parameter in most downstream tasks.

Evaluation

The following table summarizes the performance of BERTomelo compared to other prominent models for Brazilian Portuguese across Semantic Textual Similarity (STS), Recognizing Textual Entailment (RTE), and Named Entity Recognition (NER).

Our evaluation pipeline is designed to ensure statistical robustness by separating hyperparameter tuning from the final multi-seed evaluation.

  • Hyperparameter Optimization (Optuna): We perform a search using Optuna over a single fixed seed. The objective is to maximize the task-specific target metric (F1-Score for NER, Pearson for STS, and Macro-F1 for Classification/RTE) on the validation set. We optimize the Learning Rate, Weight Decay, and Warmup Ratio. To prevent overfitting and save compute, we apply an Early Stopping mechanism with a patience of 3 epochs.
  • Multi-Seed Evaluation (Robustness): Once the optimal hyperparameters are identified, we perform a full fine-tuning of the model across 6 different random seeds.
    • For each seed, the task-specific classification head is initialized from scratch (randomly), while the Transformer backbone loads the pretrained weights.
    • We train for up to 20 epochs, keeping the same 3-epoch Early Stopping rule.
    • Due to load_best_model_at_end=True, the model always reverts to the weights that achieved the highest validation score, regardless of when training halted.
    • Finally, we report the mean and standard deviation (std) of the metrics on the test set across all 6 runs.

Aggregated results (Mean ± Standard Deviation) by model and benchmark.

Model Pearson ↑
(ASSIN 2)
MSE ↓
(ASSIN 2)
Macro F1 ↑
(ASSIN 2)
Accuracy ↑
(ASSIN 2)
F1 ↑
(LENER-BR)
Precision ↑
(LENER-BR)
Recall ↑
(LENER-BR)
Base Models
mBERT 0.8171 ± 0.0057 0.5576 ± 0.0126 0.8667 ± 0.0074 0.8672 ± 0.0073 0.8614 ± 0.0045 0.8417 ± 0.0094 0.8823 ± 0.0085
BERTimbau 0.8345 ± 0.0035 0.5723 ± 0.0199 0.8851 ± 0.0065 0.8855 ± 0.0063 0.8892 ± 0.0085 0.8667 ± 0.0123 0.9130 ± 0.0058
BERTuguês 0.8352 ± 0.0044 0.5694 ± 0.0222 0.8775 ± 0.0053 0.8781 ± 0.0053 0.8745 ± 0.0095 0.8576 ± 0.0122 0.8922 ± 0.0083
ModernBERT 0.7730 ± 0.0072 0.5643 ± 0.0208 0.8174 ± 0.0086 0.8184 ± 0.0083 0.7575 ± 0.0113 0.7292 ± 0.0154 0.7883 ± 0.0081
ModBERTBr 0.8065 ± 0.0026 0.5283 ± 0.0094 0.8336 ± 0.0090 0.8354 ± 0.0087 0.8916 ± 0.0083 0.8763 ± 0.0107 0.9075 ± 0.0070
BERTomelo-Base 0.8375 ± 0.0019 0.4570 ± 0.0077 0.8693 ± 0.0144 0.8701 ± 0.0139 0.8987 ± 0.0104 0.8845 ± 0.0152 0.9135 ± 0.0073
modernJabuticaBERT-Base-1k 0.8507 ± 0.0033 0.4468 ± 0.0801 0.8678 ± 0.0087 0.8683 ± 0.0085 0.8503 ± 0.0068 0.8280 ± 0.0093 0.8738 ± 0.0057
modernJabuticaBERT-Base-8k 0.8460 ± 0.0032 0.4167 ± 0.0063 0.8820 ± 0.0027 0.8824 ± 0.0027 0.8685 ± 0.0092 0.8444 ± 0.0150 0.8940 ± 0.0059
NorBERTo-Base 0.8544 ± 0.0023 0.4086 ± 0.0056 0.8918 ± 0.0060 0.8922 ± 0.0061 0.8545 ± 0.0128 0.8356 ± 0.0199 0.8743 ± 0.0086
Large Models
ModernBERT-Large 0.8107 ± 0.0052 0.4792 ± 0.0131 0.8525 ± 0.0109 0.8532 ± 0.0105 0.7921 ± 0.0181 0.7721 ± 0.0295 0.8136 ± 0.0097
BERTomelo-Large 0.8577 ± 0.0012 0.4078 ± 0.0075 0.8857 ± 0.0085 0.8860 ± 0.0084 0.8917 ± 0.0047 0.8755 ± 0.0070 0.9084 ± 0.0080
modernJabuticaBERT-Large-1k 0.8585 ± 0.0032 0.3994 ± 0.0055 0.8879 ± 0.0079 0.8884 ± 0.0078 0.8697 ± 0.0156 0.8461 ± 0.0173 0.8947 ± 0.0162
modernJabuticaBERT-Large-8k 0.8576 ± 0.0024 0.3981 ± 0.0082 0.8988 ± 0.0049 0.8991 ± 0.0048 0.8846 ± 0.0158 0.8656 ± 0.0235 0.9046 ± 0.0094
NorBERTo-Large 0.8506 ± 0.0021 0.4120 ± 0.0071 0.8916 ± 0.0105 0.8920 ± 0.0104 0.8743 ± 0.0120 0.8550 ± 0.0227 0.8949 ± 0.0075

Limitations

  • The evaluation process lacks the availability of standardized long context benchmarks specifically designed for the Portuguese language.
  • The current release does not yet incorporate the sequence packing mechanism, which may affect token throughput in specific training scenarios.
  • As with all models trained on large scale web data, BERTomelo may reflect biases present in the ClassiCC PT dataset.

Acknowledgments

This work was supported in part by Advanced Micro Devices, Inc. under the AMD AI & HPC Cluster Program. Furthermore, the respective authors are appreciated for providing the ClassiCC-PT, ASSIN2, and LeNER-BR datasets, as well as the developers of ModBERTBr for their foundational work in adapting this architecture for the Brazilian Portuguese language.

Downloads last month
455
Safetensors
Model size
0.1B params
Tensor type
F32
·
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Model tree for unb-labia/BERTomelo-ModernBERT-Base-v1

Finetuned
(1364)
this model

Dataset used to train unb-labia/BERTomelo-ModernBERT-Base-v1