Fill-Mask
Transformers
Safetensors
Upper Grand Valley Dani
bert
DNA
BERT
language-model
genomics
custom_code
Instructions to use Taykhoom/DNABERT2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Taykhoom/DNABERT2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("fill-mask", model="Taykhoom/DNABERT2", trust_remote_code=True)# Load model directly from transformers import AutoTokenizer, AutoModelForMaskedLM tokenizer = AutoTokenizer.from_pretrained("Taykhoom/DNABERT2", trust_remote_code=True) model = AutoModelForMaskedLM.from_pretrained("Taykhoom/DNABERT2", trust_remote_code=True) - Notebooks
- Google Colab
- Kaggle
Upload folder using huggingface_hub
Browse files- README.md +155 -0
- config.json +32 -0
- model.safetensors +3 -0
- special_tokens_map.json +7 -0
- tokenizer.json +0 -0
- tokenizer_config.json +9 -0
README.md
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- dna
|
| 4 |
+
library_name: transformers
|
| 5 |
+
tags:
|
| 6 |
+
- DNA
|
| 7 |
+
- BERT
|
| 8 |
+
- language-model
|
| 9 |
+
- genomics
|
| 10 |
+
license: mit
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# DNABERT-2
|
| 14 |
+
|
| 15 |
+
Weights and tokenizer for [DNABERT-2](https://arxiv.org/abs/2306.15006)
|
| 16 |
+
(Zhou et al., arXiv 2023), loaded with the shared MosaicBERT implementation
|
| 17 |
+
from [Taykhoom/MosaicBERT-updated](https://huggingface.co/Taykhoom/MosaicBERT-updated).
|
| 18 |
+
|
| 19 |
+
DNABERT-2 is a foundation model trained on large-scale multi-species genome data.
|
| 20 |
+
It replaces k-mer tokenization with Byte Pair Encoding (BPE), uses ALiBi positional
|
| 21 |
+
biases instead of learned embeddings, and incorporates a GLU-based FFN for improved
|
| 22 |
+
efficiency.
|
| 23 |
+
|
| 24 |
+
**This repo contains only weights and tokenizer files.** The model code is loaded
|
| 25 |
+
automatically from `Taykhoom/MosaicBERT-updated` via `trust_remote_code=True`.
|
| 26 |
+
|
| 27 |
+
## Architecture
|
| 28 |
+
|
| 29 |
+
| Parameter | Value |
|
| 30 |
+
|---|---|
|
| 31 |
+
| Layers | 12 |
|
| 32 |
+
| Attention heads | 12 |
|
| 33 |
+
| Embedding dimension | 768 |
|
| 34 |
+
| Intermediate size | 3072 |
|
| 35 |
+
| Vocabulary size | 4096 (BPE) |
|
| 36 |
+
| Positional encoding | ALiBi (no hard length limit) |
|
| 37 |
+
| Max sequence length | ~10000 nt (practical; ALiBi resizes dynamically) |
|
| 38 |
+
| Parameters | ~117M |
|
| 39 |
+
|
| 40 |
+
### Tokenization
|
| 41 |
+
|
| 42 |
+
Uses Byte Pair Encoding (BPE) tokenization via `PreTrainedTokenizerFast`.
|
| 43 |
+
No k-mer pre-processing required.
|
| 44 |
+
|
| 45 |
+
## Pretraining
|
| 46 |
+
|
| 47 |
+
- **Objective:** Masked Language Modeling
|
| 48 |
+
- **Data:** Large-scale multi-species genome (GRCh38 and others)
|
| 49 |
+
- **Source checkpoint:** `pytorch_model.bin` from [zhihan1996/DNABERT-2-117M](https://huggingface.co/zhihan1996/DNABERT-2-117M)
|
| 50 |
+
|
| 51 |
+
## Parity Verification
|
| 52 |
+
|
| 53 |
+
Hidden-state representations verified identical (max abs diff = 0.00) to the original
|
| 54 |
+
implementation at all 13 representation levels (embedding + 12 transformer layers).
|
| 55 |
+
SDPA verified (max abs diff < 1e-4). Verified on GPU with PyTorch 2.7 / CUDA 12.9.
|
| 56 |
+
|
| 57 |
+
## Related Models
|
| 58 |
+
|
| 59 |
+
See the full [DNABERT collection](https://huggingface.co/collections/Taykhoom/dnabert-6a20958f8ce004ea4e985e7b).
|
| 60 |
+
|
| 61 |
+
| Model | Architecture | Notes |
|
| 62 |
+
|---|---|---|
|
| 63 |
+
| [DNABERT-3mer](https://huggingface.co/Taykhoom/DNABERT-3mer) | BERT + k-mer | k=3 |
|
| 64 |
+
| [DNABERT-4mer](https://huggingface.co/Taykhoom/DNABERT-4mer) | BERT + k-mer | k=4 |
|
| 65 |
+
| [DNABERT-5mer](https://huggingface.co/Taykhoom/DNABERT-5mer) | BERT + k-mer | k=5 |
|
| 66 |
+
| [DNABERT-6mer](https://huggingface.co/Taykhoom/DNABERT-6mer) | BERT + k-mer | k=6 |
|
| 67 |
+
| **[DNABERT-2](https://huggingface.co/Taykhoom/DNABERT2)** | **MosaicBERT + BPE + ALiBi** | **This model** |
|
| 68 |
+
| [DNABERT-S](https://huggingface.co/Taykhoom/DNABERT-S) | MosaicBERT + BPE + ALiBi | Species-aware contrastive fine-tune |
|
| 69 |
+
|
| 70 |
+
## Usage
|
| 71 |
+
|
| 72 |
+
### Embedding generation
|
| 73 |
+
|
| 74 |
+
```python
|
| 75 |
+
import torch
|
| 76 |
+
from transformers import AutoTokenizer, AutoModel
|
| 77 |
+
|
| 78 |
+
tokenizer = AutoTokenizer.from_pretrained("Taykhoom/DNABERT2", trust_remote_code=True)
|
| 79 |
+
model = AutoModel.from_pretrained("Taykhoom/DNABERT2", trust_remote_code=True)
|
| 80 |
+
model.eval()
|
| 81 |
+
|
| 82 |
+
sequences = ["ACGTAGCATCGGATCTATCTATCGACACTTGG", "ATCGATCGATCGATCG"]
|
| 83 |
+
enc = tokenizer(sequences, return_tensors="pt", padding=True)
|
| 84 |
+
|
| 85 |
+
with torch.no_grad():
|
| 86 |
+
out = model(**enc)
|
| 87 |
+
|
| 88 |
+
cls_emb = out.last_hidden_state[:, 0, :] # (batch, 768)
|
| 89 |
+
mean_emb = out.last_hidden_state.mean(dim=1) # (batch, 768) -- mean pooling
|
| 90 |
+
|
| 91 |
+
# Intermediate layers
|
| 92 |
+
out_all = model(**enc, output_hidden_states=True)
|
| 93 |
+
layer6_emb = out_all.hidden_states[6]
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
### MLM logits
|
| 97 |
+
|
| 98 |
+
```python
|
| 99 |
+
import torch
|
| 100 |
+
from transformers import AutoTokenizer, AutoModelForMaskedLM
|
| 101 |
+
|
| 102 |
+
tokenizer = AutoTokenizer.from_pretrained("Taykhoom/DNABERT2", trust_remote_code=True)
|
| 103 |
+
model = AutoModelForMaskedLM.from_pretrained("Taykhoom/DNABERT2", trust_remote_code=True)
|
| 104 |
+
model.eval()
|
| 105 |
+
|
| 106 |
+
enc = tokenizer(["ACGTAGCAT[MASK]GGATCTATC"], return_tensors="pt")
|
| 107 |
+
with torch.no_grad():
|
| 108 |
+
logits = model(**enc).logits # (1, seq_len, 4096)
|
| 109 |
+
```
|
| 110 |
+
|
| 111 |
+
### Attention implementation
|
| 112 |
+
|
| 113 |
+
```python
|
| 114 |
+
# SDPA (default on PyTorch >= 2.0)
|
| 115 |
+
model = AutoModel.from_pretrained("Taykhoom/DNABERT2", trust_remote_code=True,
|
| 116 |
+
attn_implementation="sdpa")
|
| 117 |
+
|
| 118 |
+
# Flash Attention 2
|
| 119 |
+
model = AutoModel.from_pretrained("Taykhoom/DNABERT2", trust_remote_code=True,
|
| 120 |
+
attn_implementation="flash_attention_2",
|
| 121 |
+
torch_dtype=torch.bfloat16)
|
| 122 |
+
```
|
| 123 |
+
|
| 124 |
+
## Implementation Notes
|
| 125 |
+
|
| 126 |
+
The original DNABERT-2 codebase uses a Triton-based flash attention implementation
|
| 127 |
+
(`flash_attn_triton.py`). This HF port uses
|
| 128 |
+
[Taykhoom/MosaicBERT-updated](https://huggingface.co/Taykhoom/MosaicBERT-updated)
|
| 129 |
+
which replaces it with the standard `flash-attn` package, and also adds
|
| 130 |
+
`attn_implementation="sdpa"` support. These were not part of the original codebase.
|
| 131 |
+
|
| 132 |
+
## Citation
|
| 133 |
+
|
| 134 |
+
```bibtex
|
| 135 |
+
@misc{zhou2023_dnabert2,
|
| 136 |
+
title = {{DNABERT}-2: Efficient Foundation Model and Benchmark For Multi-Species Genome},
|
| 137 |
+
author = {Zhou, Zhihan and Ji, Yanrong and Li, Weijian and Dutta, Pratik and
|
| 138 |
+
Davuluri, Ramana and Liu, Han},
|
| 139 |
+
year = {2023},
|
| 140 |
+
eprint = {2306.15006},
|
| 141 |
+
archivePrefix = {arXiv},
|
| 142 |
+
primaryClass = {q-bio.GN}
|
| 143 |
+
}
|
| 144 |
+
```
|
| 145 |
+
|
| 146 |
+
## Credits
|
| 147 |
+
|
| 148 |
+
Original DNABERT-2 model and code by Zhou et al.
|
| 149 |
+
Source: [GitHub](https://github.com/MAGICS-LAB/DNABERT_2).
|
| 150 |
+
The HF conversion code was authored primarily by [Claude Code](https://claude.ai/code)
|
| 151 |
+
and reviewed manually by Taykhoom Dalal.
|
| 152 |
+
|
| 153 |
+
## License
|
| 154 |
+
|
| 155 |
+
MIT, following the original repository.
|
config.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "Taykhoom/DNABERT2",
|
| 3 |
+
"alibi_starting_size": 512,
|
| 4 |
+
"architectures": [
|
| 5 |
+
"BertForMaskedLM"
|
| 6 |
+
],
|
| 7 |
+
"attention_probs_dropout_prob": 0.0,
|
| 8 |
+
"auto_map": {
|
| 9 |
+
"AutoConfig": "Taykhoom/MosaicBERT-updated--configuration_bert.BertConfig",
|
| 10 |
+
"AutoModel": "Taykhoom/MosaicBERT-updated--bert_layers.BertModel",
|
| 11 |
+
"AutoModelForMaskedLM": "Taykhoom/MosaicBERT-updated--bert_layers.BertForMaskedLM",
|
| 12 |
+
"AutoModelForSequenceClassification": "Taykhoom/MosaicBERT-updated--bert_layers.BertForSequenceClassification"
|
| 13 |
+
},
|
| 14 |
+
"classifier_dropout": null,
|
| 15 |
+
"hidden_act": "gelu",
|
| 16 |
+
"hidden_dropout_prob": 0.1,
|
| 17 |
+
"hidden_size": 768,
|
| 18 |
+
"initializer_range": 0.02,
|
| 19 |
+
"intermediate_size": 3072,
|
| 20 |
+
"layer_norm_eps": 1e-12,
|
| 21 |
+
"max_position_embeddings": 512,
|
| 22 |
+
"model_max_length": 10000,
|
| 23 |
+
"model_type": "bert",
|
| 24 |
+
"num_attention_heads": 12,
|
| 25 |
+
"num_hidden_layers": 12,
|
| 26 |
+
"pad_token_id": 3,
|
| 27 |
+
"position_embedding_type": "absolute",
|
| 28 |
+
"transformers_version": "4.57.6",
|
| 29 |
+
"type_vocab_size": 2,
|
| 30 |
+
"use_cache": true,
|
| 31 |
+
"vocab_size": 4096
|
| 32 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:92a85bb75c431027162899e60f80f26eff64cba9218cc2bdb42c881d9461e852
|
| 3 |
+
size 480895944
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": "[CLS]",
|
| 3 |
+
"mask_token": "[MASK]",
|
| 4 |
+
"pad_token": "[PAD]",
|
| 5 |
+
"sep_token": "[SEP]",
|
| 6 |
+
"unk_token": "[UNK]"
|
| 7 |
+
}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": "[CLS]",
|
| 3 |
+
"mask_token": "[MASK]",
|
| 4 |
+
"model_max_length": 10000,
|
| 5 |
+
"pad_token": "[PAD]",
|
| 6 |
+
"sep_token": "[SEP]",
|
| 7 |
+
"tokenizer_class": "PreTrainedTokenizerFast",
|
| 8 |
+
"unk_token": "[UNK]"
|
| 9 |
+
}
|