Taykhoom commited on
Commit
2633f44
·
0 Parent(s):

Initial DNABERT-4mer Hugging Face port

Browse files
Files changed (7) hide show
  1. .gitattributes +35 -0
  2. README.md +188 -0
  3. config.json +26 -0
  4. model.safetensors +3 -0
  5. special_tokens_map.json +1 -0
  6. tokenizer_config.json +10 -0
  7. vocab.txt +261 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ tags:
4
+ - biology
5
+ - DNA
6
+ - language-model
7
+ - genomics
8
+ license: apache-2.0
9
+ ---
10
+
11
+ # DNABERT-4mer
12
+
13
+ Minimal HuggingFace port of the **4-mer** variant of
14
+ [DNABERT](https://huggingface.co/zhihan1996/DNA_bert_4) -- a BERT-base
15
+ masked language model pre-trained on the human reference genome using
16
+ overlapping 4-mer tokenization.
17
+
18
+ **This repo contains only weights and tokenizer files.** The model code is loaded
19
+ automatically from `Taykhoom/BERT-updated` via `trust_remote_code=True`.
20
+
21
+ ## Architecture
22
+
23
+ Standard BERT-base with a 4-mer DNA vocabulary.
24
+
25
+ | Parameter | Value |
26
+ |---|---|
27
+ | Layers | 12 |
28
+ | Attention heads | 12 |
29
+ | Embedding dimension | 768 |
30
+ | FFN hidden dimension | 3072 (GELU) |
31
+ | Vocabulary size | 261 (5 special + 256 DNA 4-mers) |
32
+ | Positional encoding | Learned absolute |
33
+ | Normalization | Post-LayerNorm (epsilon 1e-12) |
34
+ | Architecture | Bidirectional BERT encoder |
35
+ | Max sequence length | 512 tokens (510 k-mers; 513 nucleotides) |
36
+ | Runtime parameters | 87,034,629 |
37
+
38
+ ### Tokenization
39
+
40
+ Input sequences must be pre-split into overlapping 4-mers (stride 1) with spaces
41
+ between tokens before calling the tokenizer. For example:
42
+
43
+ ```
44
+ ATCGATG -> ATCG TCGA CGAT GATG
45
+ ```
46
+
47
+ ```python
48
+ def seq_to_kmers(seq, k=4):
49
+ return " ".join(seq[i:i+k] for i in range(len(seq) - k + 1))
50
+ ```
51
+
52
+ ## Pretraining
53
+
54
+ - **Objective:** Masked Language Modeling
55
+ - **Data:** Human reference genome (GRCh38)
56
+ - **Source checkpoint:** `pytorch_model.bin` from [zhihan1996/DNA_bert_4](https://huggingface.co/zhihan1996/DNA_bert_4)
57
+
58
+ ## Parity Verification
59
+
60
+ All 13 representation levels (embedding + 12 transformer layers) verified
61
+ against the source implementation (max abs diff = 9.79e-5); MLM logits match
62
+ with max abs diff = 2.52e-4. The source `dnabert_layer.BertModel` is a direct
63
+ subclass of `transformers.BertModel` with no modifications.
64
+ Verified on GPU with PyTorch 2.7.1 / CUDA 12.9.
65
+
66
+ ## Related Models
67
+
68
+ See the full [DNABERT collection](https://huggingface.co/collections/Taykhoom/dnabert-6a20958f8ce004ea4e985e7b).
69
+
70
+ | Model | Architecture | Notes |
71
+ |---|---|---|
72
+ | [DNABERT-3mer](https://huggingface.co/Taykhoom/DNABERT-3mer) | BERT + k-mer | k=3 |
73
+ | **[DNABERT-4mer](https://huggingface.co/Taykhoom/DNABERT-4mer)** | **BERT + k-mer** | **k=4** |
74
+ | [DNABERT-5mer](https://huggingface.co/Taykhoom/DNABERT-5mer) | BERT + k-mer | k=5 |
75
+ | [DNABERT-6mer](https://huggingface.co/Taykhoom/DNABERT-6mer) | BERT + k-mer | k=6 |
76
+ | [DNABERT-2](https://huggingface.co/Taykhoom/DNABERT2) | MosaicBERT + BPE + ALiBi | Multi-species pre-trained |
77
+ | [DNABERT-S](https://huggingface.co/Taykhoom/DNABERT-S) | MosaicBERT + BPE + ALiBi | Species-aware |
78
+
79
+
80
+ ## Usage
81
+
82
+ ### Embedding generation
83
+
84
+ ```python
85
+ import torch
86
+ from transformers import AutoTokenizer, AutoModel
87
+
88
+ def seq_to_kmers(seq, k=4):
89
+ return " ".join(seq[i:i+k] for i in range(len(seq) - k + 1))
90
+
91
+ tokenizer = AutoTokenizer.from_pretrained("Taykhoom/DNABERT-4mer", trust_remote_code=True)
92
+ model = AutoModel.from_pretrained("Taykhoom/DNABERT-4mer", trust_remote_code=True)
93
+ model.eval()
94
+
95
+ sequences = ["ATCGATCGATCG", "GCTAGCTAGCTA"]
96
+ kmer_seqs = [seq_to_kmers(s) for s in sequences]
97
+ enc = tokenizer(kmer_seqs, return_tensors="pt", padding=True)
98
+
99
+ with torch.no_grad():
100
+ out = model(**enc)
101
+
102
+ cls_emb = out.last_hidden_state[:, 0, :] # (batch, 768)
103
+ token_emb = out.last_hidden_state # (batch, seq_len, 768)
104
+
105
+ # Mean-pool DNA k-mers only (exclude CLS, SEP, and padding)
106
+ content_mask = enc["attention_mask"].bool()
107
+ content_mask[:, 0] = False
108
+ sep_positions = enc["attention_mask"].sum(dim=1) - 1
109
+ batch_indices = torch.arange(len(sequences), device=content_mask.device)
110
+ content_mask[batch_indices, sep_positions] = False
111
+ mean_emb = (
112
+ token_emb * content_mask.unsqueeze(-1)
113
+ ).sum(dim=1) / content_mask.sum(dim=1, keepdim=True)
114
+
115
+ # Intermediate layers
116
+ out_all = model(**enc, output_hidden_states=True)
117
+ layer6_emb = out_all.hidden_states[6]
118
+ ```
119
+
120
+ Sequences shorter than 4 nucleotides contain no k-mer tokens and therefore
121
+ cannot be mean-pooled or assigned a k-mer pseudo-likelihood.
122
+
123
+ ### MLM logits
124
+
125
+ ```python
126
+ from transformers import AutoModelForMaskedLM
127
+
128
+ model = AutoModelForMaskedLM.from_pretrained(
129
+ "Taykhoom/DNABERT-4mer", trust_remote_code=True
130
+ )
131
+ tokens = seq_to_kmers("ATCGATCG", k=4).split()
132
+ tokens[2] = tokenizer.mask_token
133
+ enc = tokenizer(" ".join(tokens), return_tensors="pt")
134
+
135
+ with torch.no_grad():
136
+ logits = model(**enc).logits # (1, seq_len, 261)
137
+ ```
138
+
139
+ ### Faster attention backends
140
+
141
+ ```python
142
+ # SDPA (PyTorch 2.0+)
143
+ model = AutoModel.from_pretrained("Taykhoom/DNABERT-4mer", trust_remote_code=True,
144
+ attn_implementation="sdpa")
145
+
146
+ # Flash Attention 2 (requires flash-attn)
147
+ model = AutoModel.from_pretrained("Taykhoom/DNABERT-4mer", trust_remote_code=True,
148
+ attn_implementation="flash_attention_2",
149
+ dtype=torch.float16)
150
+ ```
151
+
152
+ ### Fine-tuning
153
+
154
+ For sequence-level tasks, mean-pool only k-mer positions as above or use the
155
+ CLS token embedding as input to a prediction head.
156
+
157
+ ## Implementation Notes
158
+
159
+ The original DNABERT codebase has `BertModel` as a thin subclass of
160
+ `transformers.BertModel` with no modifications. This HF port uses
161
+ [Taykhoom/BERT-updated](https://huggingface.co/Taykhoom/BERT-updated) which adds
162
+ `attn_implementation="sdpa"` and `attn_implementation="flash_attention_2"`
163
+ support — these were not part of the original codebase.
164
+
165
+ ## Citation
166
+
167
+ ```bibtex
168
+ @article{ji2021_dnabert,
169
+ title = {{DNABERT}: pre-trained Bidirectional Encoder Representations from Transformers model for {DNA}-language in genome},
170
+ author = {Ji, Yanrong and Zhou, Zhihan and Liu, Han and Davuluri, Ramana V},
171
+ journal = {Bioinformatics},
172
+ volume = {37},
173
+ number = {15},
174
+ pages = {2112--2120},
175
+ year = {2021},
176
+ doi = {10.1093/bioinformatics/btab083}
177
+ }
178
+ ```
179
+
180
+ ## Credits
181
+
182
+ Original DNABERT model and code by Ji et al. Source: [GitHub](https://github.com/jerryji1993/DNABERT).
183
+ The HF conversion code was authored primarily by [Claude Code](https://claude.ai/code)
184
+ and reviewed manually by Taykhoom Dalal.
185
+
186
+ ## License
187
+
188
+ Apache License 2.0, following the original repository.
config.json ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "BertForMaskedLM"
4
+ ],
5
+ "model_type": "bert_updated",
6
+ "auto_map": {
7
+ "AutoConfig": "Taykhoom/BERT-updated--configuration_bert_updated.BertUpdatedConfig",
8
+ "AutoModel": "Taykhoom/BERT-updated--modeling_bert.BertModel",
9
+ "AutoModelForMaskedLM": "Taykhoom/BERT-updated--modeling_bert.BertForMaskedLM"
10
+ },
11
+ "vocab_size": 261,
12
+ "hidden_size": 768,
13
+ "num_hidden_layers": 12,
14
+ "num_attention_heads": 12,
15
+ "intermediate_size": 3072,
16
+ "hidden_act": "gelu",
17
+ "hidden_dropout_prob": 0.1,
18
+ "attention_probs_dropout_prob": 0.1,
19
+ "max_position_embeddings": 512,
20
+ "type_vocab_size": 2,
21
+ "initializer_range": 0.02,
22
+ "layer_norm_eps": 1e-12,
23
+ "pad_token_id": 0,
24
+ "kmer": 4,
25
+ "model_max_length": 512
26
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f148b8f5ee0b4e92a3c3a17d27a0f0be78ef9d0f3231fb43fb211f475bbbf768
3
+ size 348162252
special_tokens_map.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]"}
tokenizer_config.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "do_lower_case": false,
3
+ "model_max_length": 512,
4
+ "tokenizer_class": "BertTokenizer",
5
+ "unk_token": "[UNK]",
6
+ "sep_token": "[SEP]",
7
+ "pad_token": "[PAD]",
8
+ "cls_token": "[CLS]",
9
+ "mask_token": "[MASK]"
10
+ }
vocab.txt ADDED
@@ -0,0 +1,261 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [PAD]
2
+ [UNK]
3
+ [CLS]
4
+ [SEP]
5
+ [MASK]
6
+ AAAA
7
+ AAAT
8
+ AAAC
9
+ AAAG
10
+ AATA
11
+ AATT
12
+ AATC
13
+ AATG
14
+ AACA
15
+ AACT
16
+ AACC
17
+ AACG
18
+ AAGA
19
+ AAGT
20
+ AAGC
21
+ AAGG
22
+ ATAA
23
+ ATAT
24
+ ATAC
25
+ ATAG
26
+ ATTA
27
+ ATTT
28
+ ATTC
29
+ ATTG
30
+ ATCA
31
+ ATCT
32
+ ATCC
33
+ ATCG
34
+ ATGA
35
+ ATGT
36
+ ATGC
37
+ ATGG
38
+ ACAA
39
+ ACAT
40
+ ACAC
41
+ ACAG
42
+ ACTA
43
+ ACTT
44
+ ACTC
45
+ ACTG
46
+ ACCA
47
+ ACCT
48
+ ACCC
49
+ ACCG
50
+ ACGA
51
+ ACGT
52
+ ACGC
53
+ ACGG
54
+ AGAA
55
+ AGAT
56
+ AGAC
57
+ AGAG
58
+ AGTA
59
+ AGTT
60
+ AGTC
61
+ AGTG
62
+ AGCA
63
+ AGCT
64
+ AGCC
65
+ AGCG
66
+ AGGA
67
+ AGGT
68
+ AGGC
69
+ AGGG
70
+ TAAA
71
+ TAAT
72
+ TAAC
73
+ TAAG
74
+ TATA
75
+ TATT
76
+ TATC
77
+ TATG
78
+ TACA
79
+ TACT
80
+ TACC
81
+ TACG
82
+ TAGA
83
+ TAGT
84
+ TAGC
85
+ TAGG
86
+ TTAA
87
+ TTAT
88
+ TTAC
89
+ TTAG
90
+ TTTA
91
+ TTTT
92
+ TTTC
93
+ TTTG
94
+ TTCA
95
+ TTCT
96
+ TTCC
97
+ TTCG
98
+ TTGA
99
+ TTGT
100
+ TTGC
101
+ TTGG
102
+ TCAA
103
+ TCAT
104
+ TCAC
105
+ TCAG
106
+ TCTA
107
+ TCTT
108
+ TCTC
109
+ TCTG
110
+ TCCA
111
+ TCCT
112
+ TCCC
113
+ TCCG
114
+ TCGA
115
+ TCGT
116
+ TCGC
117
+ TCGG
118
+ TGAA
119
+ TGAT
120
+ TGAC
121
+ TGAG
122
+ TGTA
123
+ TGTT
124
+ TGTC
125
+ TGTG
126
+ TGCA
127
+ TGCT
128
+ TGCC
129
+ TGCG
130
+ TGGA
131
+ TGGT
132
+ TGGC
133
+ TGGG
134
+ CAAA
135
+ CAAT
136
+ CAAC
137
+ CAAG
138
+ CATA
139
+ CATT
140
+ CATC
141
+ CATG
142
+ CACA
143
+ CACT
144
+ CACC
145
+ CACG
146
+ CAGA
147
+ CAGT
148
+ CAGC
149
+ CAGG
150
+ CTAA
151
+ CTAT
152
+ CTAC
153
+ CTAG
154
+ CTTA
155
+ CTTT
156
+ CTTC
157
+ CTTG
158
+ CTCA
159
+ CTCT
160
+ CTCC
161
+ CTCG
162
+ CTGA
163
+ CTGT
164
+ CTGC
165
+ CTGG
166
+ CCAA
167
+ CCAT
168
+ CCAC
169
+ CCAG
170
+ CCTA
171
+ CCTT
172
+ CCTC
173
+ CCTG
174
+ CCCA
175
+ CCCT
176
+ CCCC
177
+ CCCG
178
+ CCGA
179
+ CCGT
180
+ CCGC
181
+ CCGG
182
+ CGAA
183
+ CGAT
184
+ CGAC
185
+ CGAG
186
+ CGTA
187
+ CGTT
188
+ CGTC
189
+ CGTG
190
+ CGCA
191
+ CGCT
192
+ CGCC
193
+ CGCG
194
+ CGGA
195
+ CGGT
196
+ CGGC
197
+ CGGG
198
+ GAAA
199
+ GAAT
200
+ GAAC
201
+ GAAG
202
+ GATA
203
+ GATT
204
+ GATC
205
+ GATG
206
+ GACA
207
+ GACT
208
+ GACC
209
+ GACG
210
+ GAGA
211
+ GAGT
212
+ GAGC
213
+ GAGG
214
+ GTAA
215
+ GTAT
216
+ GTAC
217
+ GTAG
218
+ GTTA
219
+ GTTT
220
+ GTTC
221
+ GTTG
222
+ GTCA
223
+ GTCT
224
+ GTCC
225
+ GTCG
226
+ GTGA
227
+ GTGT
228
+ GTGC
229
+ GTGG
230
+ GCAA
231
+ GCAT
232
+ GCAC
233
+ GCAG
234
+ GCTA
235
+ GCTT
236
+ GCTC
237
+ GCTG
238
+ GCCA
239
+ GCCT
240
+ GCCC
241
+ GCCG
242
+ GCGA
243
+ GCGT
244
+ GCGC
245
+ GCGG
246
+ GGAA
247
+ GGAT
248
+ GGAC
249
+ GGAG
250
+ GGTA
251
+ GGTT
252
+ GGTC
253
+ GGTG
254
+ GGCA
255
+ GGCT
256
+ GGCC
257
+ GGCG
258
+ GGGA
259
+ GGGT
260
+ GGGC
261
+ GGGG