{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "c31446f3-a976-42a0-9dfb-bc873049464b", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/conda/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "import wandb\n", "import torch\n", "import os\n", "import pandas as pd\n", "from grelu.lightning import LightningModel\n", "import pytorch_lightning as pl\n", "from grelu.sequence.utils import get_unique_length, resize" ] }, { "cell_type": "markdown", "id": "45e5daae-fe31-4cc7-b876-683768acd3cf", "metadata": {}, "source": [ "## set up wandb" ] }, { "cell_type": "code", "execution_count": 2, "id": "18c561ab-ba82-4718-abe9-96c44133b314", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[34m\u001b[1mwandb\u001b[0m: Using wandb-core as the SDK backend. Please refer to https://wandb.me/wandb-core for more information.\n", "\u001b[34m\u001b[1mwandb\u001b[0m: Currently logged in as: \u001b[33mavantikalal\u001b[0m (\u001b[33mgrelu\u001b[0m) to \u001b[32mhttps://api.wandb.ai\u001b[0m. Use \u001b[1m`wandb login --relogin`\u001b[0m to force relogin\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wandb.login(host=\"https://api.wandb.ai\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "48cf7ec6-1b32-4f96-a0f1-c7ffb1cb5fcf", "metadata": {}, "outputs": [ { "data": { "text/html": [ "Tracking run with wandb version 0.19.7" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "Run data is saved locally in /code/github/gReLU-applications/borzoi/wandb/run-20250306_055353-qtz95621" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "Syncing run copy-human to Weights & Biases (docs)
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " View project at https://wandb.ai/grelu/borzoi" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " View run at https://wandb.ai/grelu/borzoi/runs/qtz95621" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "run = wandb.init(\n", " entity='grelu', project='borzoi', job_type='copy', name='copy-human',\n", " settings=wandb.Settings(\n", " program_relpath='save_wandb_borzoi_ckpt_human.ipynb',\n", " program_abspath='/code/github/gReLU-applications/borzoi/save_wandb_borzoi_ckpt_human.ipynb'\n", " ))" ] }, { "cell_type": "markdown", "id": "de1c87dc-cacf-48f0-a081-5260e325cb7b", "metadata": {}, "source": [ "## Load tasks and intervals" ] }, { "cell_type": "code", "execution_count": 4, "id": "556b9768-f3e4-466a-b097-0b3226e788dd", "metadata": {}, "outputs": [], "source": [ "tasks_path = '/code/borzoi/data/targets_human.txt.gz'" ] }, { "cell_type": "code", "execution_count": 5, "id": "f8616dde-2567-4a3c-8fb9-de412d342078", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7611\n" ] } ], "source": [ "tasks = pd.read_table(tasks_path, index_col=0)\n", "print(len(tasks))" ] }, { "cell_type": "code", "execution_count": 6, "id": "0bf621ee-bd42-4299-98b1-d4f0e5ebb6ec", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[34m\u001b[1mwandb\u001b[0m: 1 of 1 files downloaded. \n" ] } ], "source": [ "artifact = run.use_artifact('human_intervals:latest')\n", "dir = artifact.download()\n", "intervals = pd.read_table(os.path.join(dir, \"data.tsv\"))" ] }, { "cell_type": "markdown", "id": "882d78ab-832c-4166-b130-91e37cda7327", "metadata": {}, "source": [ "## Process tasks" ] }, { "cell_type": "code", "execution_count": 7, "id": "66f24ff2-74bc-41ee-9cc9-5cf9e75c2ebf", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
namefileclipclip_softscalesum_statstrand_pairdescriptionassaysample
0CNhs10608+/home/drk/tillage/datasets/human/cage/fantom/C...7683841.0sum1CAGE:Clontech Human Universal Reference Total ...CAGEClontech Human Universal Reference Total RNA, ...
1CNhs10608-/home/drk/tillage/datasets/human/cage/fantom/C...7683841.0sum0CAGE:Clontech Human Universal Reference Total ...CAGEClontech Human Universal Reference Total RNA, ...
\n", "
" ], "text/plain": [ " name file clip \\\n", "0 CNhs10608+ /home/drk/tillage/datasets/human/cage/fantom/C... 768 \n", "1 CNhs10608- /home/drk/tillage/datasets/human/cage/fantom/C... 768 \n", "\n", " clip_soft scale sum_stat strand_pair \\\n", "0 384 1.0 sum 1 \n", "1 384 1.0 sum 0 \n", "\n", " description assay \\\n", "0 CAGE:Clontech Human Universal Reference Total ... CAGE \n", "1 CAGE:Clontech Human Universal Reference Total ... CAGE \n", "\n", " sample \n", "0 Clontech Human Universal Reference Total RNA, ... \n", "1 Clontech Human Universal Reference Total RNA, ... " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tasks=tasks.reset_index(drop=True)\n", "tasks[\"assay\"] = tasks[\"description\"].apply(lambda x: x.split(\":\")[0])\n", "tasks[\"assay\"].value_counts()\n", "tasks[\"sample\"] = tasks[\"description\"].apply(lambda x: \":\".join(x.split(\":\")[1:]))\n", "tasks = tasks.rename(columns={\"identifier\":\"name\"})\n", "tasks.head(2)" ] }, { "cell_type": "code", "execution_count": 8, "id": "4f96ecc7-238e-4707-a3af-64b0d1828bd4", "metadata": {}, "outputs": [], "source": [ "tasks = tasks.to_dict(orient=\"list\")" ] }, { "cell_type": "markdown", "id": "3056ecfa-1b75-46b7-9e36-4e72c4cb3618", "metadata": {}, "source": [ "## Process intervals" ] }, { "cell_type": "code", "execution_count": 9, "id": "86aae057-b106-4884-81b3-d1aa563ad4f7", "metadata": {}, "outputs": [], "source": [ "train_intervals = intervals[intervals.split=='train'].iloc[:, :3]\n", "val_intervals = intervals[intervals.split=='val'].iloc[:, :3]\n", "test_intervals = intervals[intervals.split=='test'].iloc[:, :3]\n", "del intervals" ] }, { "cell_type": "markdown", "id": "e4bd10a9-40c2-4299-aa74-6474ea5a71ef", "metadata": {}, "source": [ "## Initialize model" ] }, { "cell_type": "code", "execution_count": 10, "id": "06dba49d-11ba-471b-83d7-05fe45a9bd5a", "metadata": {}, "outputs": [], "source": [ "model_params = {\n", " \"model_type\":\"BorzoiModel\",\n", " \"n_tasks\":7611,\n", " \"final_act_func\": 'softplus',\n", " \"final_pool_func\":None,\n", " \"crop_len\":5120,\n", "}\n", "train_params={\n", " \"task\":\"regression\",\n", " \"loss\":\"mse\",\n", "}\n", "lm = LightningModel(model_params, train_params)" ] }, { "cell_type": "markdown", "id": "ab6a3055-d338-40f0-9fd3-f8f3134df643", "metadata": {}, "source": [ "## Save checkpoints" ] }, { "cell_type": "code", "execution_count": 11, "id": "bbccda9a-7a62-4472-9c0b-34abf98a7e1c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/tmp/ipykernel_3881069/2231986157.py:16: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n", " state_dict = torch.load(f\"/data/borzoi/torch_weights/fold{rep}.h5\")\n", "Trainer will use only 1 of 8 GPUs because it is running inside an interactive / notebook environment. You may try to set `Trainer(devices=8)` but please note that multi-GPU inside interactive / notebook environments is considered experimental and unstable. Your mileage may vary.\n", "GPU available: True (cuda), used: True\n", "TPU available: False, using: 0 TPU cores\n", "HPU available: False, using: 0 HPUs\n", "You are using a CUDA device ('NVIDIA A100-SXM4-80GB') that has Tensor Cores. To properly utilize them, you should set `torch.set_float32_matmul_precision('medium' | 'high')` which will trade-off precision for performance. For more details, read https://pytorch.org/docs/stable/generated/torch.set_float32_matmul_precision.html#torch.set_float32_matmul_precision\n", "LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0,1,2,3,4,5,6,7]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/tmp/ipykernel_3881069/2231986157.py:16: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n", " state_dict = torch.load(f\"/data/borzoi/torch_weights/fold{rep}.h5\")\n", "Trainer will use only 1 of 8 GPUs because it is running inside an interactive / notebook environment. You may try to set `Trainer(devices=8)` but please note that multi-GPU inside interactive / notebook environments is considered experimental and unstable. Your mileage may vary.\n", "GPU available: True (cuda), used: True\n", "TPU available: False, using: 0 TPU cores\n", "HPU available: False, using: 0 HPUs\n", "LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0,1,2,3,4,5,6,7]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/tmp/ipykernel_3881069/2231986157.py:16: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n", " state_dict = torch.load(f\"/data/borzoi/torch_weights/fold{rep}.h5\")\n", "Trainer will use only 1 of 8 GPUs because it is running inside an interactive / notebook environment. You may try to set `Trainer(devices=8)` but please note that multi-GPU inside interactive / notebook environments is considered experimental and unstable. Your mileage may vary.\n", "GPU available: True (cuda), used: True\n", "TPU available: False, using: 0 TPU cores\n", "HPU available: False, using: 0 HPUs\n", "LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0,1,2,3,4,5,6,7]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/tmp/ipykernel_3881069/2231986157.py:16: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n", " state_dict = torch.load(f\"/data/borzoi/torch_weights/fold{rep}.h5\")\n", "Trainer will use only 1 of 8 GPUs because it is running inside an interactive / notebook environment. You may try to set `Trainer(devices=8)` but please note that multi-GPU inside interactive / notebook environments is considered experimental and unstable. Your mileage may vary.\n", "GPU available: True (cuda), used: True\n", "TPU available: False, using: 0 TPU cores\n", "HPU available: False, using: 0 HPUs\n", "LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0,1,2,3,4,5,6,7]\n" ] } ], "source": [ "for rep in range(4):\n", " print(rep)\n", " \n", " lm.data_params[\"tasks\"] = tasks\n", " lm.data_params[\"train\"] = dict()\n", " lm.data_params[\"val\"] = dict()\n", " lm.data_params[\"test\"] = dict()\n", " lm.data_params[\"train\"][\"seq_len\"] = 524288\n", " lm.data_params[\"train\"][\"label_len\"] = 6144*32\n", " lm.data_params[\"train\"][\"genome\"] = \"hg38\"\n", " lm.data_params[\"train\"][\"bin_size\"] = 32\n", " lm.data_params[\"train\"][\"intervals\"] = train_intervals.to_dict(orient='list')\n", " lm.data_params[\"val\"][\"intervals\"] = val_intervals.to_dict(orient='list')\n", " lm.data_params[\"test\"][\"intervals\"] = test_intervals.to_dict(orient='list')\n", "\n", " state_dict = torch.load(f\"/data/borzoi/torch_weights/fold{rep}.h5\")\n", " lm.model.load_state_dict(state_dict)\n", " \n", " trainer = pl.Trainer()\n", " try:\n", " trainer.predict(lm)\n", " except:\n", " pass\n", "\n", " trainer.save_checkpoint(f'/data/borzoi/torch_weights/human_rep{rep}.ckpt')" ] }, { "cell_type": "markdown", "id": "4b5fbc09-abca-4cb7-a8b7-7eeba7b39cf8", "metadata": {}, "source": [ "## Upload to wandb" ] }, { "cell_type": "code", "execution_count": 12, "id": "c53a69b4-9092-44b9-b8a9-20b72aa8b511", "metadata": {}, "outputs": [], "source": [ "metadata={\n", " 'model_params':lm.model_params, \n", " 'train_params':lm.train_params, \n", " 'data_params':lm.data_params\n", " }" ] }, { "cell_type": "code", "execution_count": 13, "id": "2a25b878-ca1f-4872-9549-ab502975ca2e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n", "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Serializing object of type list that is 351064 bytes\n" ] } ], "source": [ "for rep in range(4):\n", " print(rep)\n", " artifact = wandb.Artifact(\n", " f'human_rep{rep}', \n", " type='model',\n", " metadata=metadata,\n", " )\n", " artifact.add_file(local_path=f'/data/borzoi/torch_weights/human_rep{rep}.ckpt', name='model.ckpt')\n", " run.log_artifact(artifact)" ] }, { "cell_type": "code", "execution_count": 14, "id": "98dd40ec-f276-4099-a4a7-39ccf1600a58", "metadata": {}, "outputs": [ { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " View run copy-human at: https://wandb.ai/grelu/borzoi/runs/qtz95621
View project at: https://wandb.ai/grelu/borzoi
Synced 6 W&B file(s), 0 media file(s), 8 artifact file(s) and 0 other file(s)" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "Find logs at: ./wandb/run-20250306_055353-qtz95621/logs" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "run.finish()" ] }, { "cell_type": "code", "execution_count": null, "id": "1c5d6a57-a2a1-495d-90ff-16508e4e236c", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.9" } }, "nbformat": 4, "nbformat_minor": 5 }