Instructions to use mineself2016/GeneMamba with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use mineself2016/GeneMamba with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="mineself2016/GeneMamba", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("mineself2016/GeneMamba", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Remove stale path: examples/downstream/legacy_from_gene_mamba/mamba2_classification_finetune_without_label.py
Browse files
examples/downstream/legacy_from_gene_mamba/mamba2_classification_finetune_without_label.py
DELETED
|
@@ -1,161 +0,0 @@
|
|
| 1 |
-
# %%
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import Trainer
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
import pyarrow as pa
|
| 7 |
-
import pandas as pd
|
| 8 |
-
import numpy as np
|
| 9 |
-
|
| 10 |
-
from matplotlib import pyplot as plt
|
| 11 |
-
|
| 12 |
-
from torch.utils.data import Dataset
|
| 13 |
-
from transformers import AutoTokenizer, TrainingArguments
|
| 14 |
-
|
| 15 |
-
import argparse
|
| 16 |
-
|
| 17 |
-
from mamba_ssm.models.mixer_seq_simple import MambaLMHeadModel
|
| 18 |
-
from transformers import AutoTokenizer, TrainingArguments, MambaForCausalLM
|
| 19 |
-
|
| 20 |
-
from dotmap import DotMap
|
| 21 |
-
|
| 22 |
-
import sys
|
| 23 |
-
import os
|
| 24 |
-
import torch
|
| 25 |
-
|
| 26 |
-
sys.path.append("/project/zhiwei/cq5/PythonWorkSpace/gene_mamba")
|
| 27 |
-
|
| 28 |
-
from models import Classifier, GeneMamba, GeneMambaForCellAnnotation, GeneMambaForGeneClassification, GeneMamba2, GeneMamba2ForCellClassification
|
| 29 |
-
from utils import permute_genes_by_expression, build_downstream_dataset
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
import importlib
|
| 33 |
-
importlib.reload(sys.modules['models'])
|
| 34 |
-
importlib.reload(sys.modules['utils'])
|
| 35 |
-
|
| 36 |
-
# %%
|
| 37 |
-
import scanpy as sc
|
| 38 |
-
|
| 39 |
-
import argparse
|
| 40 |
-
|
| 41 |
-
parser = argparse.ArgumentParser()
|
| 42 |
-
parser.add_argument("--dataset_name", type=str)
|
| 43 |
-
|
| 44 |
-
args2 = parser.parse_args()
|
| 45 |
-
|
| 46 |
-
# Load the .h5ad file
|
| 47 |
-
dataset_name = args2.dataset_name
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
assert dataset_name in ["pbmc12k", "perirhinal_cortex", "covid19"]
|
| 51 |
-
|
| 52 |
-
adata = sc.read_h5ad(f'/project/zhiwei/cq5/PythonWorkSpace/gene_mamba/dataset/downstream/processed/{dataset_name}_processed.h5ad')
|
| 53 |
-
|
| 54 |
-
assert "celltype" in adata.obs
|
| 55 |
-
|
| 56 |
-
print(adata)
|
| 57 |
-
|
| 58 |
-
# %%
|
| 59 |
-
from sklearn.preprocessing import LabelEncoder
|
| 60 |
-
|
| 61 |
-
y_names = np.array(adata.obs['celltype'].values.tolist())
|
| 62 |
-
|
| 63 |
-
label_encoder = LabelEncoder()
|
| 64 |
-
y = label_encoder.fit_transform(y_names)
|
| 65 |
-
|
| 66 |
-
num_class = len(label_encoder.classes_)
|
| 67 |
-
|
| 68 |
-
# %%
|
| 69 |
-
from transformers import PretrainedConfig
|
| 70 |
-
|
| 71 |
-
config = PretrainedConfig.from_dict({
|
| 72 |
-
"d_model": 512,
|
| 73 |
-
"mamba_layer": 24,
|
| 74 |
-
})
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
# %%
|
| 78 |
-
model = GeneMamba2(config, model_path="/project/zhiwei/cq5/LLM_checkpoints/GeneMamba/GeneMamba2_24l_512d/1/16m/checkpoint-31250", tokenizer_path="/project/zhiwei/cq5/PythonWorkSpace/gene_mamba/gene_tokenizer.json", args=None)
|
| 79 |
-
|
| 80 |
-
# %%
|
| 81 |
-
permuted_gene_ids = permute_genes_by_expression(adata, dataset_name, model.tokenizer, model.symbol2id)
|
| 82 |
-
permuted_gene_ids
|
| 83 |
-
|
| 84 |
-
# %%
|
| 85 |
-
num_samples = permuted_gene_ids.shape[0]
|
| 86 |
-
num_avaliable_gpu = torch.cuda.device_count()
|
| 87 |
-
|
| 88 |
-
# %%
|
| 89 |
-
from dotmap import DotMap
|
| 90 |
-
|
| 91 |
-
args = DotMap(
|
| 92 |
-
{
|
| 93 |
-
# "model": "state-spaces/mamba-130m-hf",
|
| 94 |
-
# "tokenizer": "state-spaces/mamba-130m-hf",
|
| 95 |
-
"learning_rate": 5e-5,
|
| 96 |
-
"batch_size": 16,
|
| 97 |
-
"gradient_accumulation_steps": 1,
|
| 98 |
-
"optim": "adamw_torch",
|
| 99 |
-
# "data_path": "/home/cong/study/codeSpace/VSCodeSpace/PythonWorkSpace/TCRPrediction/mamba_transformer/smiles_data.txt",
|
| 100 |
-
# "num_epochs": args2.num_epochs,
|
| 101 |
-
"seq_len": 2048,
|
| 102 |
-
"num_samples": num_samples,
|
| 103 |
-
"num_gpus": num_avaliable_gpu,
|
| 104 |
-
"output_dir": "/project/zhiwei/cq5/PythonWorkSpace/gene_mamba/analysis/cell_type_annotation/fine-tuned/debug",
|
| 105 |
-
}
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
-
# %%
|
| 109 |
-
input_data = permuted_gene_ids[:, :args.seq_len]
|
| 110 |
-
|
| 111 |
-
# %%
|
| 112 |
-
input_data.shape
|
| 113 |
-
|
| 114 |
-
#%%
|
| 115 |
-
# check if cls_token in the tokenizer:
|
| 116 |
-
if model.tokenizer.cls_token_id is None:
|
| 117 |
-
model.tokenizer.add_special_tokens({'cls_token': '[CLS]'})
|
| 118 |
-
|
| 119 |
-
#%%
|
| 120 |
-
input_data = np.hstack([np.array([model.tokenizer.cls_token_id for _ in range(input_data.shape[0])]).reshape(-1, 1), input_data])
|
| 121 |
-
|
| 122 |
-
#%%
|
| 123 |
-
input_data.shape
|
| 124 |
-
|
| 125 |
-
# %%
|
| 126 |
-
sample_dataset = build_downstream_dataset(input_data, model.tokenizer)
|
| 127 |
-
sample_dataset
|
| 128 |
-
|
| 129 |
-
# input_data = np.hstack([np.array([model.tokenizer.cls_token_id for _ in range(input_data.shape[0])]).reshape(-1, 1), input_data])
|
| 130 |
-
# input_data
|
| 131 |
-
|
| 132 |
-
# %%
|
| 133 |
-
args=TrainingArguments(
|
| 134 |
-
learning_rate=args.learning_rate,
|
| 135 |
-
num_train_epochs = 4,
|
| 136 |
-
per_device_train_batch_size=args.batch_size,
|
| 137 |
-
gradient_accumulation_steps=args.gradient_accumulation_steps,
|
| 138 |
-
optim=args.optim,
|
| 139 |
-
output_dir=os.path.join(args.output_dir, dataset_name),
|
| 140 |
-
# output_dir=f"/scratch/zhiwei/cq5/logs/mamba/test/context_length",
|
| 141 |
-
# logging_dir=f"{args.output_dir}/{args.num_epochs}/{args.num_samples // 1000000 + args.bulk_id}m_logging",
|
| 142 |
-
logging_steps=args.num_samples // args.batch_size // 10,
|
| 143 |
-
save_steps=args.num_samples // args.batch_size // 10,
|
| 144 |
-
)
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
# %%
|
| 148 |
-
model.finetune(sample_dataset, args)
|
| 149 |
-
|
| 150 |
-
# %%
|
| 151 |
-
# ckpt_pth = get_last_checkpoint(os.path.join(args.output_dir, dataset_name))
|
| 152 |
-
# ckpt_pth
|
| 153 |
-
|
| 154 |
-
# #%%
|
| 155 |
-
# model = GeneMamba2(config, model_path=, tokenizer_path="/project/zhiwei/cq5/PythonWorkSpace/gene_mamba/gene_tokenizer.json", args=None)
|
| 156 |
-
|
| 157 |
-
#%%
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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