Spaces:
Running
Running
File size: 13,291 Bytes
ae75aa1 3367165 ae75aa1 b7abdc5 ae75aa1 66b0297 3367165 df00fce 1094a5f 3367165 3e64e9c b7abdc5 ae75aa1 76bb74c ae75aa1 76bb74c 3367165 ae75aa1 3367165 1094a5f 3367165 ae75aa1 3367165 ae75aa1 3367165 ae75aa1 3367165 76bb74c 3367165 df00fce 3367165 ae75aa1 3367165 66b0297 3367165 66b0297 3367165 66b0297 3367165 66b0297 3367165 ae75aa1 df00fce 3367165 ae75aa1 3367165 76bb74c 3367165 1094a5f 3367165 1094a5f 3367165 1094a5f 3367165 1094a5f 3367165 ae75aa1 3367165 ae75aa1 1094a5f 3367165 1094a5f 3367165 1094a5f 3367165 1094a5f 3367165 1094a5f 3367165 df00fce 1094a5f df00fce 1094a5f df00fce 1094a5f ae75aa1 3367165 ae75aa1 3367165 ae75aa1 3367165 ae75aa1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
{
"cells": [
{
"cell_type": "markdown",
"id": "024bb8a8",
"metadata": {},
"source": [
"# 🚀 NTv3 Quickstart — Pre-trained and Post-trained models\n",
"\n",
"This notebook demonstrates how to run **quick inference** with both the pre- and post-trained NTv3 checkpoints:\n",
"\n",
"- **Pre-trained (MLM-focused):** `InstaDeepAI/NTv3_8M_pre`, `InstaDeepAI/NTv3_100M_pre`, `InstaDeepAI/NTv3_650M_pre`\n",
"- **Post-trained (functional tracks and genome annotation):** `InstaDeepAI/NTv3_100M_pos`, `InstaDeepAI/NTv3_650M_pos`\n",
"\n",
"We show how to:\n",
"\n",
"1. Load tokenizers + models\n",
"2. Run a forward pass on a DNA sequence window\n",
"3. Inspect key outputs\n",
"\n",
"> 📝 **Note for Google Colab users:** This notebook is compatible with Colab! For faster inference, make sure to enable GPU: Runtime → Change runtime type → GPU (T4 or better recommended)."
]
},
{
"cell_type": "markdown",
"id": "5827af7e",
"metadata": {},
"source": [
"## 0) 📦 Imports + setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "38cc32a9",
"metadata": {},
"outputs": [],
"source": [
"!pip -q install \"transformers>=4.40\" \"huggingface_hub>=0.23\" safetensors torch numpy"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d56c105b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"device: cpu\n",
"torch_dtype: torch.float32\n"
]
}
],
"source": [
"import os\n",
"import torch\n",
"import numpy as np\n",
"\n",
"from transformers import AutoConfig, AutoModel, AutoTokenizer, AutoModelForMaskedLM\n",
"\n",
"# Optional: if the model is gated/private, set HF_TOKEN to a PERSONAL token (hf_...)\n",
"HF_TOKEN = os.getenv(\"HF_TOKEN\", None)\n",
"\n",
"# -----------------------------\n",
"# Device\n",
"# -----------------------------\n",
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
"print(\"device:\", device)\n",
"\n",
"# Choose dtype (bf16 if supported; else fp16 on GPU; else fp32)\n",
"if device == \"cuda\":\n",
" major, minor = torch.cuda.get_device_capability(0)\n",
" torch_dtype = torch.bfloat16 if major >= 8 else torch.float16\n",
"else:\n",
" torch_dtype = torch.float32\n",
"\n",
"print(\"torch_dtype:\", torch_dtype)"
]
},
{
"cell_type": "markdown",
"id": "82146876",
"metadata": {},
"source": [
"## 1) 🎯 Pre-trained checkpoint (MLM-focused)\n",
"\n",
"This shows the simplest usage: load model + tokenizer, then run a forward pass.\n",
"\n",
"Expected output:\n",
"- `logits`: masked language modeling logits"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "336bb40c",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "411ee47e94ae467f9685c35b65e3e52d",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"tokenizer_config.json: 0%| | 0.00/1.48k [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "30447edb44b849bd936290f3a6b1b863",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"tokenization_ntv3.py: 0%| | 0.00/12.0k [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"A new version of the following files was downloaded from https://huggingface.co/InstaDeepAI/ntv3_base_model:\n",
"- tokenization_ntv3.py\n",
". Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "766f183dcc84421588e5cf0241d3efe7",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"vocab.json: 0%| | 0.00/138 [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b0db83f7cb824d3288a30bebf7891a63",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"special_tokens_map.json: 0%| | 0.00/149 [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "33cf5391dcc549f088e4e927651d1cdb",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"config.json: 0%| | 0.00/1.70k [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "85772d5369234ca286cfa518e1725b12",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"configuration_ntv3.py: 0%| | 0.00/5.90k [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"A new version of the following files was downloaded from https://huggingface.co/InstaDeepAI/ntv3_base_model:\n",
"- configuration_ntv3.py\n",
". Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "ec1153d073e444c5b255ee5adea6ba68",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"modeling_ntv3_base.py: 0%| | 0.00/33.9k [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"A new version of the following files was downloaded from https://huggingface.co/InstaDeepAI/ntv3_base_model:\n",
"- modeling_ntv3_base.py\n",
". Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "94b9bb7fe0da4f4994adb9127d9af7e6",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"model.safetensors: 0%| | 0.00/30.8M [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([2, 128, 11])\n",
"16\n",
"2\n",
"MLM logits shape: (2, 128, 11)\n"
]
}
],
"source": [
"pretrained_model_name = \"InstaDeepAI/NTv3_8M_pre\"\n",
"\n",
"# Load tokenizer/model\n",
"tok_pre = AutoTokenizer.from_pretrained(pretrained_model_name, trust_remote_code=True)\n",
"model_pre = AutoModelForMaskedLM.from_pretrained(pretrained_model_name, trust_remote_code=True)\n",
"\n",
"# Example: human sequence\n",
"seqs = [\"ATCGNATCG\", \"ACGT\"]\n",
"batch = tok_pre(seqs, add_special_tokens=False, padding=True, pad_to_multiple_of=128, return_tensors=\"pt\")\n",
"out = model_pre(**batch, output_hidden_states=True, output_attentions=True)\n",
"\n",
"print(out.logits.shape) # (B, L, V = 11)\n",
"print(len(out.hidden_states)) # convs + transformers + deconvs\n",
"print(len(out.attentions))\n",
"\n",
"# Access MLM logits\n",
"mlm_logits = out[\"logits\"]\n",
"print(\"MLM logits shape:\", tuple(mlm_logits.shape))"
]
},
{
"cell_type": "markdown",
"id": "60a01798",
"metadata": {},
"source": [
"## 2) 🧠 Post-trained checkpoint (task heads: BigWig + BED)\n",
"\n",
"Post-trained checkpoints add task-specific heads for functional track prediction and genome annotation.\n",
"\n",
"In particular:\n",
"- `species_tokenizer` is used to tokenize a species condition like `\"human\"`\n",
"- `species_ids` passes the species tokens to the model\n",
"\n",
"Expected outputs:\n",
"- `bigwig_tracks_logits`: functional track predictions\n",
"- `bed_tracks_logits`: genome annotation predictions\n",
"- `logits`: masked language modeling logits"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "bdb8c4d1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model supported species: TO BE DONE\n"
]
}
],
"source": [
"# Inspect config and supported species\n",
"post_trained_model_name = \"InstaDeepAI/NTv3_100M_pos\"\n",
"\n",
"cfg_post = AutoConfig.from_pretrained(post_trained_model_name, trust_remote_code=True)\n",
"\n",
"species = \"TO BE DONE\"\n",
"print(\"Model supported species:\", species)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6cc5f2df",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([1, 768, 7362])\n",
"torch.Size([1, 768, 21, 2])\n",
"torch.Size([1, 2048, 11])\n"
]
}
],
"source": [
"tok_post = AutoTokenizer.from_pretrained(post_trained_model_name, trust_remote_code=True)\n",
"cond_tok_post = AutoTokenizer.from_pretrained(post_trained_model_name, subfolder='species_tokenizer', trust_remote_code=True)\n",
"model_post = AutoModel.from_pretrained(post_trained_model_name, trust_remote_code=True)\n",
"\n",
"# Prepare inputs\n",
"batch = tok_post([\"ATCGNATCG\", \"ACGT\"], add_special_tokens=False, padding=True, pad_to_multiple_of=128, return_tensors=\"pt\")\n",
"\n",
"# Condition tokens (e.g., species)\n",
"species = 'human'\n",
"species_ids = cond_tok_post([species] * len(batch['input_ids']), add_special_tokens=False, return_tensors='pt')\n",
"\n",
"# Forward pass\n",
"out = model_post(\n",
" input_ids=batch[\"input_ids\"],\n",
" species_ids=species_ids['input_ids'],\n",
" return_dict=True\n",
")\n",
"\n",
"# 7k human tracks over 37.5 % center region of the input sequence\n",
"print(\"bigwig_tracks_logits:\", tuple(out[\"bigwig_tracks_logits\"].shape))\n",
"# Location of 21 genomic elements over 37.5 % center region of the input sequence\n",
"print(\"bed_tracks_logits:\", tuple(out[\"bed_tracks_logits\"].shape))\n",
"# Language model logits for whole sequence over vocabulary\n",
"print(\"language model logits:\", tuple(out[\"logits\"].shape))\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "hf-finetune",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|