Taykhoom commited on
Commit
2b4d944
·
verified ·
1 Parent(s): 38ef1bf

Upload folder using huggingface_hub

Browse files
README.md ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - dna
4
+ library_name: transformers
5
+ tags:
6
+ - DNA
7
+ - BERT
8
+ - language-model
9
+ - genomics
10
+ license: apache-2.0
11
+ ---
12
+
13
+ # DNABERT-S
14
+
15
+ Weights and tokenizer for [DNABERT-S](https://arxiv.org/abs/2402.08777)
16
+ (Zhou et al., arXiv 2024), loaded with the shared MosaicBERT implementation
17
+ from [Taykhoom/MosaicBERT-updated](https://huggingface.co/Taykhoom/MosaicBERT-updated).
18
+
19
+ DNABERT-S is a species-aware DNA embedding model fine-tuned from DNABERT-2 using
20
+ curriculum contrastive learning. It generates embeddings that naturally cluster and
21
+ segregate genomes from different species, enabling species identification,
22
+ metagenomics binning, and evolutionary analysis.
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, identical to DNABERT-2) |
36
+ | Positional encoding | ALiBi (no hard length limit) |
37
+ | Max sequence length | ~10000 nt (practical; ALiBi resizes dynamically) |
38
+ | Parameters | ~110M (backbone only, no MLM head) |
39
+
40
+ ### Tokenization
41
+
42
+ Uses Byte Pair Encoding (BPE) tokenization via `PreTrainedTokenizerFast`,
43
+ identical vocabulary to DNABERT-2. No k-mer pre-processing required.
44
+
45
+ ## Pretraining
46
+
47
+ - **Objective:** Curriculum contrastive learning (same-species pairs with i-Mix)
48
+ - **Initialization:** Fine-tuned from [zhihan1996/DNABERT-2-117M](https://huggingface.co/zhihan1996/DNABERT-2-117M)
49
+ - **Source checkpoint:** `pytorch_model.bin` from [zhihan1996/DNABERT-S](https://huggingface.co/zhihan1996/DNABERT-S)
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 | Pre-trained |
68
+ | **[DNABERT-S](https://huggingface.co/Taykhoom/DNABERT-S)** | **MosaicBERT + BPE + ALiBi** | **This model** |
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/DNABERT-S", trust_remote_code=True)
79
+ model = AutoModel.from_pretrained("Taykhoom/DNABERT-S", 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
+
92
+ ### Attention implementation
93
+
94
+ ```python
95
+ # SDPA (default on PyTorch >= 2.0)
96
+ model = AutoModel.from_pretrained("Taykhoom/DNABERT-S", trust_remote_code=True,
97
+ attn_implementation="sdpa")
98
+
99
+ # Flash Attention 2
100
+ model = AutoModel.from_pretrained("Taykhoom/DNABERT-S", trust_remote_code=True,
101
+ attn_implementation="flash_attention_2",
102
+ torch_dtype=torch.bfloat16)
103
+ ```
104
+
105
+ ## Implementation Notes
106
+
107
+ The original DNABERT-S codebase uses a Triton-based flash attention implementation
108
+ (`flash_attn_triton.py`). This HF port uses
109
+ [Taykhoom/MosaicBERT-updated](https://huggingface.co/Taykhoom/MosaicBERT-updated)
110
+ which replaces it with the standard `flash-attn` package, and also adds
111
+ `attn_implementation="sdpa"` support. These were not part of the original codebase.
112
+
113
+ ## Citation
114
+
115
+ ```bibtex
116
+ @misc{zhou2024_dnaberts,
117
+ title = {{DNABERT}-S: Learning Species-Aware {DNA} Embedding with Genome Foundation Models},
118
+ author = {Zhou, Zhihan and Wu, Winmin and Ho, Harrison and Wang, Jiayi and
119
+ Shi, Lizhen and Davuluri, Ramana V and Wang, Zhong and Liu, Han},
120
+ year = {2024},
121
+ eprint = {2402.08777},
122
+ archivePrefix = {arXiv},
123
+ primaryClass = {q-bio.GN}
124
+ }
125
+ ```
126
+
127
+ ## Credits
128
+
129
+ Original DNABERT-S model and code by Zhou et al.
130
+ Source: [GitHub](https://github.com/MAGICS-LAB/DNABERT_S).
131
+ The HF conversion code was authored primarily by [Claude Code](https://claude.ai/code)
132
+ and reviewed manually by Taykhoom Dalal.
133
+
134
+ ## License
135
+
136
+ Apache 2.0, following the original repository.
config.json ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "Taykhoom/DNABERT-S",
3
+ "alibi_starting_size": 512,
4
+ "architectures": [
5
+ "BertModel"
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:7100412d0dc474f911d01c43003fc9bb1bcea9506ea04e969bba16015b2189b9
3
+ size 468289360
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
+ }