CoolFace
Modelpublic

Derify/ModChemBERT

sourceHugging Faceapache-2.0updated 9mo agoView on Hugging Face
0likes9downloads
Model Card

ModChemBERT: ModernBERT as a Chemical Language Model

ModChemBERT is a ModernBERT-based chemical language model (CLM), trained on SMILES strings for masked language modeling (MLM) and downstream molecular property prediction (classification & regression).

Usage

Install the transformers library starting from v4.56.1:

bash
pip install -U "transformers>=4.56.1,<5.0.0"

Load Model

python
from transformers import AutoModelForMaskedLM, AutoTokenizer

model_id = "Derify/ModChemBERT"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForMaskedLM.from_pretrained(
    model_id,
    trust_remote_code=True,
    dtype="float16",
    device_map="auto",
)

Fill-Mask Pipeline

python
from transformers import pipeline

fill = pipeline("fill-mask", model=model, tokenizer=tokenizer)
print(fill("c1ccccc1[MASK]"))

Architecture

  • —Backbone: ModernBERT
  • —Hidden size: 768
  • —Intermediate size: 1152
  • —Encoder Layers: 22
  • —Attention heads: 12
  • —Max sequence length: 256 tokens (MLM primarily trained with 128-token sequences)
  • —Tokenizer: BPE tokenizer using MolFormer's vocab (2362 tokens)

Pooling (Classifier / Regressor Head)

Kallergis et al. [1] demonstrated that the CLM embedding method prior to the prediction head was the strongest contributor to downstream performance among evaluated hyperparameters.

Behrendt et al. [2] noted that the last few layers contain task-specific information and that pooling methods leveraging information from multiple layers can enhance model performance. Their results further demonstrated that the max_seq_mha pooling method was particularly effective in low-data regimes, which is often the case for molecular property prediction tasks.

Multiple pooling strategies are supported by ModChemBERT to explore their impact on downstream performance:

  • —cls: Last layer [CLS]
  • —mean: Mean over last hidden layer
  • —max_cls: Max over last k layers of [CLS]
  • —cls_mha: MHA with [CLS] as query
  • —max_seq_mha: MHA with max pooled sequence as KV and max pooled [CLS] as query
  • —sum_mean: Sum over all layers then mean tokens
  • —sum_sum: Sum over all layers then sum tokens
  • —mean_mean: Mean over all layers then mean tokens
  • —mean_sum: Mean over all layers then sum tokens
  • —max_seq_mean: Max over last k layers then mean tokens

Note: ModChemBERT’s max_seq_mha differs from MaxPoolBERT [2]. MaxPoolBERT uses PyTorch nn.MultiheadAttention, whereas ModChemBERT's ModChemBertPoolingAttention adapts ModernBERT’s ModernBertAttention. On ChemBERTa-3 benchmarks this variant produced stronger validation metrics and avoided the training instabilities (sporadic zero / NaN losses and gradient norms) seen with nn.MultiheadAttention. Training instability with ModernBERT has been reported in the past (discussion 1 and discussion 2).

Training Pipeline

<div align="center"> <img src="https://cdn-uploads.huggingface.co/production/uploads/656892962693fa22e18b5331/bxNbpgMkU8m60ypyEJoWQ.png" alt="ModChemBERT Training Pipeline" width="650"/> </div>

Rationale for MTR Stage

Following Sultan et al. [3], multi-task regression (physicochemical properties) biases the latent space toward ADME-related representations prior to narrow TAFT specialization. Sultan et al. observed that MLM + DAPT (MTR) outperforms MLM-only, MTR-only, and MTR + DAPT (MTR).

Checkpoint Averaging Motivation

Inspired by ModernBERT [4], JaColBERTv2.5 [5], and Llama 3.1 [6], where results show that model merging can enhance generalization or performance while mitigating overfitting to any single fine-tune or annealing checkpoint.

Datasets

  • —Pretraining: Derify/augmented_canonical_druglike_QED_Pfizer_15M (canonical_smiles column)
  • —Domain Adaptive Pretraining (DAPT) & Task Adaptive Fine-tuning (TAFT): ADME (6 tasks) + AstraZeneca (4 tasks) datasets that are split using DA4MT's [3] Bemis-Murcko scaffold splitter (see domain-adaptation-molecular-transformers)
  • —Benchmarking:
  • —ChemBERTa-3 [7]
  • —classification: BACE, BBBP, TOX21, HIV, SIDER, CLINTOX
  • —regression: ESOL, FREESOLV, LIPO, BACE, CLEARANCE
  • —Mswahili, et al. [8] proposed additional datasets for benchmarking chemical language models:
  • —classification: Antimalarial [9], Cocrystal [10], COVID19 [11]
  • —DAPT/TAFT stage regression datasets:
  • —ADME [12]: admemicrosomstabh, admemicrosomstabr, admepermeability, admeppbh, admeppbr, admesolubility
  • —AstraZeneca: astrazenecaCL, astrazenecaLogD74, astrazenecaPPB, astrazenecaSolubility

Benchmarking

Benchmarks were conducted using the ChemBERTa-3 framework. DeepChem scaffold splits were utilized for all datasets, with the exception of the Antimalarial dataset, which employed a random split. Each task was trained for 100 epochs, with results averaged across 3 random seeds.

The complete hyperparameter configurations for these benchmarks are available here: ChemBERTa3 configs

Evaluation Methodology

  • —Classification Metric: ROC AUC
  • —Regression Metric: RMSE
  • —Aggregation: Mean ± standard deviation of the triplicate results.
  • —Input Constraints: SMILES truncated / filtered to ≤200 tokens, following ChemBERTa-3's recommendation.

Results

<details><summary>Click to expand</summary>

ChemBERTa-3 Classification Datasets (ROC AUC - Higher is better)
ModelBACE↑BBBP↑CLINTOX↑HIV↑SIDER↑TOX21↑AVG†
Tasks11212712
ChemBERTa-100M-MLM*0.781 ± 0.0190.700 ± 0.0270.979 ± 0.0220.740 ± 0.0130.611 ± 0.0020.718 ± 0.0110.7548
c3-MoLFormer-1.1B*0.819 ± 0.0190.735 ± 0.0190.839 ± 0.0130.762 ± 0.0050.618 ± 0.0050.723 ± 0.0120.7493
MoLFormer-LHPC*0.887 ± 0.0040.908 ± 0.0130.993 ± 0.0040.750 ± 0.0030.622 ± 0.0070.791 ± 0.0140.8252
MLM0.8065 ± 0.01030.7222 ± 0.01500.9709 ± 0.0227*0.7800 ± 0.0133*0.6419 ± 0.01130.7400 ± 0.00440.7769
MLM + DAPT0.8224 ± 0.01560.7402 ± 0.00950.9820 ± 0.01380.7702 ± 0.00200.6303 ± 0.00390.7360 ± 0.00360.7802
MLM + TAFT0.7924 ± 0.01550.7282 ± 0.00580.9725 ± 0.02130.7770 ± 0.00470.6542 ± 0.01280.7646 ± 0.00390.7815
MLM + DAPT + TAFT0.8213 ± 0.00510.7356 ± 0.00940.9664 ± 0.02020.7750 ± 0.00480.6415 ± 0.00940.7263 ± 0.00360.7777
MLM + DAPT + TAFT OPT0.8346 ± 0.00450.7573 ± 0.0120*0.9938 ± 0.0017*0.7737 ± 0.0034*0.6600 ± 0.0061*0.7518 ± 0.00470.7952
ChemBERTa-3 Regression Datasets (RMSE - Lower is better)
ModelBACE↓CLEARANCE↓ESOL↓FREESOLV↓LIPO↓AVG‡
Tasks11111
ChemBERTa-100M-MLM*1.011 ± 0.03851.582 ± 3.0790.920 ± 0.0110.536 ± 0.0160.758 ± 0.0130.8063 / 10.9614
c3-MoLFormer-1.1B*1.094 ± 0.12652.058 ± 2.7670.829 ± 0.0190.572 ± 0.0230.728 ± 0.0160.8058 / 11.0562
MoLFormer-LHPC*1.201 ± 0.10045.74 ± 2.6370.848 ± 0.0310.683 ± 0.0400.895 ± 0.0800.9068 / 9.8734
MLM1.0893 ± 0.131949.0005 ± 1.27870.8456 ± 0.04060.5491 ± 0.01340.7147 ± 0.00620.7997 / 10.4398
MLM + DAPT0.9931 ± 0.025845.4951 ± 0.71120.9319 ± 0.01530.6049 ± 0.06660.6874 ± 0.00400.8043 / 9.7425
MLM + TAFT1.0304 ± 0.114647.8418 ± 0.4070*0.7669 ± 0.0024*0.5293 ± 0.02670.6708 ± 0.00740.7493 / 10.1678
MLM + DAPT + TAFT0.9713 ± 0.0224*42.8010 ± 3.3475*0.8169 ± 0.02680.5445 ± 0.02570.6820 ± 0.00280.7537 / 9.1631
MLM + DAPT + TAFT OPT*0.9665 ± 0.0250*44.0137 ± 1.11100.8158 ± 0.0115*0.4979 ± 0.0158**0.6505 ± 0.0126*0.7327 / 9.3889
Mswahili, et al. [8] Proposed Classification Datasets (ROC AUC - Higher is better)
ModelAntimalarial↑Cocrystal↑COVID19↑AVG†
Tasks111
MLM0.8707 ± 0.00320.7967 ± 0.01240.8106 ± 0.01700.8260
MLM + DAPT0.8756 ± 0.00560.8288 ± 0.01430.8029 ± 0.01590.8358
MLM + TAFT0.8832 ± 0.00510.7866 ± 0.0204*0.8308 ± 0.0026*0.8335
MLM + DAPT + TAFT0.8819 ± 0.00520.8550 ± 0.01060.8013 ± 0.01180.8461
MLM + DAPT + TAFT OPT*0.8966 ± 0.0045**0.8654 ± 0.0080*0.8132 ± 0.01950.8584
ADME/AstraZeneca Regression Datasets (RMSE - Lower is better)

Hyperparameter optimization for the TAFT stage appears to induce overfitting, as the MLM + DAPT + TAFT OPT model shows slightly degraded performance on the ADME/AstraZeneca datasets compared to the MLM + DAPT + TAFT model. The MLM + DAPT + TAFT model, a merge of unoptimized TAFT checkpoints trained with max_seq_mean pooling, achieved the best overall performance across the ADME/AstraZeneca datasets.

ADMEAstraZeneca
Modelmicrosomstabh↓microsomstabr↓permeability↓ppb_h↓ppb_r↓solubility↓CL↓LogD74↓PPB↓Solubility↓AVG†
Tasks1111111111
MLM0.4489 ± 0.01140.4685 ± 0.02250.5423 ± 0.00760.8041 ± 0.03780.7849 ± 0.03940.5191 ± 0.01470.4812 ± 0.00730.8204 ± 0.00700.1365 ± 0.00660.9614 ± 0.01890.5967
MLM + DAPT0.4199 ± 0.00640.4568 ± 0.00910.5042 ± 0.01350.8376 ± 0.06290.8446 ± 0.07560.4800 ± 0.01180.5351 ± 0.00360.8191 ± 0.00660.1237 ± 0.00220.9280 ± 0.00880.5949
MLM + TAFT0.4375 ± 0.00270.4542 ± 0.00240.5202 ± 0.01410.7618 ± 0.01380.7027 ± 0.00230.5023 ± 0.01070.5104 ± 0.01100.7599 ± 0.00500.1233 ± 0.00880.8730 ± 0.01120.5645
MLM + DAPT + TAFT0.4206 ± 0.00710.4400 ± 0.00390.4899 ± 0.00680.8927 ± 0.01630.6942 ± 0.03970.4641 ± 0.00820.5022 ± 0.01360.7467 ± 0.00410.1195 ± 0.00260.8564 ± 0.02650.5626
MLM + DAPT + TAFT OPT0.4248 ± 0.00410.4403 ± 0.00460.5025 ± 0.00290.8901 ± 0.01230.7268 ± 0.00900.4627 ± 0.00830.4932 ± 0.00790.7596 ± 0.00440.1150 ± 0.00020.8735 ± 0.00530.5689

Bold indicates the best result in the column; italic indicates the best result among ModChemBERT checkpoints.<br/> \* Published results from the ChemBERTa-3 [7] paper for optimized chemical language models using DeepChem scaffold splits.<br/> † AVG column shows the mean score across classification tasks.<br/> ‡ AVG column shows the mean scores across regression tasks without and with the clearance score.

</details>

Optimized ModChemBERT Hyperparameters

<details><summary>Click to expand</summary>

TAFT Datasets

Optimal parameters (per dataset) for the MLM + DAPT + TAFT OPT merged model:

DatasetLearning RateBatch SizeWarmup RatioClassifier PoolingLast k Layers
admemicrosomstab_h3e-580.0maxseqmean5
admemicrosomstab_r3e-5160.2max_cls3
adme_permeability3e-580.0max_cls3
admeppbh1e-5320.1maxseqmean5
admeppbr1e-5320.0sum_meanN/A
adme_solubility3e-5320.0sum_meanN/A
astrazeneca_CL3e-580.1maxseqmha3
astrazeneca_LogD741e-580.0maxseqmean5
astrazeneca_PPB1e-5320.0max_cls3
astrazeneca_Solubility1e-5320.0maxseqmean5

Benchmarking Datasets

Optimal parameters (per dataset) for the MLM + DAPT + TAFT OPT merged model:

DatasetBatch SizeClassifier PoolingLast k LayersPooling Attention DropoutClassifier DropoutEmbedding Dropout
bace_classification32maxseqmha30.00.00.0
bbbp64max_cls30.10.00.0
clintox32maxseqmha50.10.00.0
hiv32maxseqmha30.00.00.0
sider32meanN/A0.10.00.1
tox2132maxseqmha50.10.00.0
base_regression32maxseqmha50.10.00.0
clearance32maxseqmha50.10.00.0
esol64sum_meanN/A0.10.00.1
freesolv32maxseqmha50.10.00.0
lipo32maxseqmha30.10.10.1
antimalarial16maxseqmha30.10.10.1
cocrystal16max_cls30.10.00.1
covid1916sum_meanN/A0.10.00.1

</details>

Intended Use

  • —Primary: Research and development for molecular property prediction, experimentation with pooling strategies, and as a foundational model for downstream applications.
  • —Appropriate for: Binary / multi-class classification (e.g., toxicity, activity) and single-task or multi-task regression (e.g., solubility, clearance) after fine-tuning.
  • —Not intended for generating novel molecules.

Limitations

  • —Out-of-domain performance may degrade for: very long (>128 token) SMILES, inorganic / organometallic compounds, polymers, or charged / enumerated tautomers are not well represented in training.
  • —No guarantee of synthesizability, safety, or biological efficacy.

Ethical Considerations & Responsible Use

  • —Potential biases arise from training corpora skewed to drug-like space.
  • —Do not deploy in clinical or regulatory settings without rigorous, domain-specific validation.

Hardware

Training and experiments were performed on 2 NVIDIA RTX 3090 GPUs.

Citation

If you use ModChemBERT in your research, please cite the checkpoint and the following:

@software{cortes-2025-modchembert,
  author = {Emmanuel Cortes},
  title = {ModChemBERT: ModernBERT as a Chemical Language Model},
  year = {2025},
  publisher = {GitHub},
  howpublished = {GitHub repository},
  url = {https://github.com/emapco/ModChemBERT}
}

References

  1. 1.Kallergis, G., Asgari, E., Empting, M. et al. Domain adaptable language modeling of chemical compounds identifies potent pathoblockers for Pseudomonas aeruginosa. Commun Chem 8, 114 (2025). https://doi.org/10.1038/s42004-025-01484-4
  2. 2.Behrendt, Maike, Stefan Sylvius Wagner, and Stefan Harmeling. "MaxPoolBERT: Enhancing BERT Classification via Layer-and Token-Wise Aggregation." arXiv preprint arXiv:2505.15696 (2025).
  3. 3.Sultan, Afnan, et al. "Transformers for molecular property prediction: Domain adaptation efficiently improves performance." arXiv preprint arXiv:2503.03360 (2025).
  4. 4.Warner, Benjamin, et al. "Smarter, better, faster, longer: A modern bidirectional encoder for fast, memory efficient, and long context finetuning and inference." arXiv preprint arXiv:2412.13663 (2024).
  5. 5.Clavié, Benjamin. "JaColBERTv2.5: Optimising Multi-Vector Retrievers to Create State-of-the-Art Japanese Retrievers with Constrained Resources." arXiv preprint arXiv:2407.20750 (2024).
  6. 6.Grattafiori, Aaron, et al. "The llama 3 herd of models." arXiv preprint arXiv:2407.21783 (2024).
  7. 7.Singh R, Barsainyan AA, Irfan R, Amorin CJ, He S, Davis T, et al. ChemBERTa-3: An Open Source Training Framework for Chemical Foundation Models. ChemRxiv. 2025; doi:10.26434/chemrxiv-2025-4glrl-v2 This content is a preprint and has not been peer-reviewed.
  8. 8.Mswahili, M.E., Hwang, J., Rajapakse, J.C. et al. Positional embeddings and zero-shot learning using BERT for molecular-property prediction. J Cheminform 17, 17 (2025). https://doi.org/10.1186/s13321-025-00959-9
  9. 9.Mswahili, M.E.; Ndomba, G.E.; Jo, K.; Jeong, Y.-S. Graph Neural Network and BERT Model for Antimalarial Drug Predictions Using Plasmodium Potential Targets. Applied Sciences, 2024, 14(4), 1472. https://doi.org/10.3390/app14041472
  10. 10.Mswahili, M.E.; Lee, M.-J.; Martin, G.L.; Kim, J.; Kim, P.; Choi, G.J.; Jeong, Y.-S. Cocrystal Prediction Using Machine Learning Models and Descriptors. Applied Sciences, 2021, 11, 1323. https://doi.org/10.3390/app11031323
  11. 11.Harigua-Souiai, E.; Heinhane, M.M.; Abdelkrim, Y.Z.; Souiai, O.; Abdeljaoued-Tej, I.; Guizani, I. Deep Learning Algorithms Achieved Satisfactory Predictions When Trained on a Novel Collection of Anticoronavirus Molecules. Frontiers in Genetics, 2021, 12:744170. https://doi.org/10.3389/fgene.2021.744170
  12. 12.Cheng Fang, Ye Wang, Richard Grater, Sudarshan Kapadnis, Cheryl Black, Patrick Trapa, and Simone Sciabola. "Prospective Validation of Machine Learning Algorithms for Absorption, Distribution, Metabolism, and Excretion Prediction: An Industrial Perspective" Journal of Chemical Information and Modeling 2023 63 (11), 3263-3274 https://doi.org/10.1021/acs.jcim.3c00160