beiken1201 commited on
Commit
1b51026
·
verified ·
1 Parent(s): 51dbc66

Remove W&B tracking and personal paths; complete BioMod naming

Browse files

Remove W&B logging, account settings, CLI options and direct dependencies while preserving local logs, checkpoint saving and resume behavior. Replace historical workstation paths in two dataset summaries with repository-relative paths and refresh their manifest checksum rows. Include the previously reviewed BioMod naming changes required by the updated pretraining entrypoint; retain legacy checkpoint compatibility. Model weights, paired model configuration, original dataset payloads and unrelated files are unchanged.

BioMod/biomod.py CHANGED
@@ -65,6 +65,7 @@
65
  sys.path.insert(0, str(SOURCE_DIR))
66
  from models_biomod import (
67
  BioMod,
 
68
  sanitize_biomod_config,
69
  )
70
  from biomod_dataset import BioModSCDataset, load_biomod_data
@@ -81,8 +82,6 @@
81
  from torch import autocast
82
  from torch.cuda.amp import GradScaler
83
 
84
- import wandb
85
-
86
  # ========== [Step 0: Parameter Analysis] ==========
87
  # Example: Assume the default parameters batch_size=3, gene_num=27874, mask_ratio=0.75, grad_acc=60
88
  parser = argparse.ArgumentParser()
@@ -116,14 +115,14 @@
116
  help='Reconstruct loss aggregation method: gene_mean=masked gene equal weight (current); module_mean=masked module equal weight (MAE style)')
117
  parser.add_argument("--recon_target_level", type=str, default="gene", choices=["gene", "module_mean_value"],
118
  help='Reconstruction target granularity: gene=gene-by-gene reconstruction; module_mean_value=decoder directly outputs the module mean and calculates the loss with the target mean')
119
- parser.add_argument("--decoder_type", type=str, default="performer", choices=["none", "simple", "performer", "cross_attn"],
120
- help='decoder type: none=directly use encoder to output predictions; simple=no token mixing; performer=decoder self-attention; cross_attn=only masked->visible cross-attention')
121
  parser.add_argument("--decoder_gene_agg", action='store_true',
122
  help='The reconstruction head is changed to module->gene aggregation and then gene-level reconstruction (off by default to facilitate comparison with the old module_head)')
123
  parser.add_argument("--dec_embed_dim", type=int, default=512, help='decoder token dimension (can be smaller than encoder base_dim, default 512)')
124
- parser.add_argument("--dec_depth", type=int, default=4, help='Decoder layer number (valid when performer/cross_attn)')
125
- parser.add_argument("--dec_heads", type=int, default=8, help='Number of decoder attention heads (valid when performer/cross_attn)')
126
- parser.add_argument("--dec_dim_head", type=int, default=64, help='Performer decoder dim per head (ignored when cross_attn)')
127
  parser.add_argument("--ckpt_dir", type=str, default=str(PROJECT_ROOT / 'tutorial_runs' / 'pretraining'), help='Directory for generated checkpoints; user relative paths use the current working directory.')
128
  parser.add_argument("--model_name", type=str, default='biomod_27k', help='Pretrained model name.')
129
  parser.add_argument("--data_path", type=str, required=True,
@@ -140,7 +139,6 @@
140
  parser.add_argument("--fusion_method", type=str, default="multiply",
141
  choices=["multiply", "add", "gated_residual"],
142
  help='Fusion method: "multiply"=multiply; "add"=add to gene2vec after expressing MLP; "gated_residual"=inject expression information into the gated residual')
143
- parser.add_argument("-use_wandb", action='store_true', default=False)
144
  parser.add_argument("-compile", action='store_true')
145
  parser.add_argument("--model_path", type=str, default='None',
146
  help='Checkpoint file to load; use its matching configuration for continued pretraining.')
@@ -160,10 +158,6 @@
160
  parser.add_argument("--latest_ckpt_every", type=int, default=0,
161
  help='The latest checkpoint saves the cadence; 0 means closed (only timing and epoch are saved)')
162
  parser.add_argument("--amp_dtype", type=str, default="bfloat16")
163
- parser.add_argument("--api_key", type=str, default="None")
164
- parser.add_argument("--wandb_resume", action='store_true', help='Resume wandb run from previous checkpoint')
165
- parser.add_argument("--wandb_run_id", type=str, default=None, help='WandB run ID to resume (e.g., nbi8qzmn)')
166
- parser.add_argument("--resume_train_log_step", type=int, default=-1, help='Resume train_log_step from this value (set to override auto-calculation)')# Not in use yet
167
  parser.add_argument("--load_opt_and_sched", action='store_true')
168
  parser.add_argument("--lr_step_within_epoch", action='store_true',
169
  help='Update the learning rate according to the optimizer update step; the cycle length is updates_per_epoch (gradually decreasing in each epoch)')
@@ -311,82 +305,13 @@ def restore_model_args_from_config(args, config):
311
  elif args.pretrain_arch == "encdec":
312
  if args.mask_style != "replace":
313
  raise ValueError("pretrain_arch=encdec requires --mask_style replace")
314
- if args.decoder_type not in {"performer", "cross_attn"}:
315
- raise ValueError("pretrain_arch=encdec requires decoder_type in {'performer', 'cross_attn'}")
316
 
317
  local_rank = args.local_rank
318
  rank = int(os.environ["RANK"])
319
  is_master = rank == 0
320
 
321
- if is_master and args.use_wandb:
322
- api_key = args.api_key
323
- if api_key != "None":
324
- wandb.login(key=api_key)
325
-
326
- # WandB resume logic
327
- wandb_init_kwargs = {
328
- "project": "pretrain_biomod",
329
- "name": args.model_name,
330
- "entity": "icml_2023_mlb",
331
- "config": {
332
- "architecture": "biomod",
333
- "gene_num": args.gene_num,
334
- "cell_num": args.cell_num,
335
- "load model": args.model_path,
336
- "start epoch": args.start_epoch,
337
- "epochs to train": args.epoch,
338
- "valid_every": args.valid_every,
339
- "batch_size": args.batch_size,
340
- "mask_ratio": args.mask_ratio,
341
- "pretrain_arch": args.pretrain_arch,
342
- "mask_style": args.mask_style,
343
- "mask_token_mode": args.mask_token_mode,
344
- "pos_embed_mode": args.pos_embed_mode,
345
- "loss_mode": args.loss_mode,
346
- "recon_target_level": args.recon_target_level,
347
- "decoder_type": args.decoder_type,
348
- "decoder_gene_agg": args.decoder_gene_agg,
349
- "dec_embed_dim": args.dec_embed_dim,
350
- "use_cls_token": args.use_cls_token,
351
- "cls_loss_weight": args.cls_loss_weight if args.use_cls_token else 0.0,
352
- "cls_head_hidden_dim": args.cls_head_hidden_dim,
353
- "use_module": True,
354
- "use_go": False,
355
- "encoder_only": True,
356
- "shuffle_module_genes": args.shuffle_module_genes,
357
- "save_init_ckpt_and_exit": args.save_init_ckpt_and_exit,
358
- }
359
- }
360
-
361
- # Support resuming previous wandb run
362
- if args.wandb_resume:
363
- if args.wandb_run_id:
364
- wandb_init_kwargs["id"] = args.wandb_run_id
365
- wandb_init_kwargs["resume"] = "must"
366
- print(f"[WandB] Resuming run with ID: {args.wandb_run_id}")
367
- else:
368
- wandb_init_kwargs["resume"] = "allow"
369
- print("[WandB] Resuming run (auto-detect)")
370
-
371
- wandb.init(**wandb_init_kwargs)
372
- # ========== wandb indicator definition ==========
373
- # Define a custom x-axis so that epoch-level metrics have epoch as the abscissa
374
- wandb.define_metric("epoch")
375
- # Epoch level loss (put on the same picture for easy comparison)
376
- wandb.define_metric("train_epoch_loss", step_metric="epoch")
377
- wandb.define_metric("val_epoch_loss", step_metric="epoch")
378
- wandb.define_metric("val_in_train_loss", step_metric="epoch")
379
- # step level metrics (training process)
380
- wandb.define_metric("train_log_step")
381
- wandb.define_metric("train_loss", step_metric="train_log_step")
382
- wandb.define_metric("learning_rate", step_metric="train_log_step")
383
-
384
- # ===== Old code (commented, valid_log_step sharing issue exists) =====
385
- # wandb.define_metric("valid_log_step")
386
- # wandb.define_metric("val_step_loss", step_metric="valid_log_step")
387
- # wandb.define_metric("val_loss", step_metric="valid_log_step")
388
- # ========================================================
389
-
390
  SEED = args.seed
391
  EPOCHS = args.epoch
392
  BATCH_SIZE = args.batch_size # Example value: 3
@@ -576,7 +501,7 @@ def resolve_total_cells_for_split():
576
  "use_gene2vec": use_gene2vec, # Whether to use pretrained gene2vec (ablation experiment)
577
  "loss_mode": args.loss_mode, # Loss aggregation method: gene_mean / module_mean
578
  "recon_target_level": args.recon_target_level, # Reconstruction target granularity: gene/module_mean_value
579
- "decoder_type": args.decoder_type, # decoder type: simple / performer
580
  "decoder_gene_agg": args.decoder_gene_agg, # True: module->gene aggregation and reconstruction; False: module_head reconstruction
581
  "dec_embed_dim": args.dec_embed_dim,
582
  "dec_depth": args.dec_depth,
@@ -690,8 +615,6 @@ def save_initialized_checkpoint_and_exit(model_obj):
690
  f"contains loaded weights rather than random initialization."
691
  )
692
  dist.barrier()
693
- if is_master and args.use_wandb:
694
- wandb.finish()
695
  dist.destroy_process_group()
696
  raise SystemExit(0)
697
 
@@ -825,22 +748,6 @@ def any_rank_true(local_flag: bool) -> bool:
825
  model = torch.compile(model)
826
 
827
  # ========== [Step 4: Training Cycle] ==========
828
- # train_log_step: training step counter, used for the x-axis of wandb logs
829
- if args.resume_train_log_step >= 0:
830
- # User manually specifies the connection location
831
- train_log_step = args.resume_train_log_step
832
- if is_master:
833
- print(f"[Resume] Using specified train_log_step: {train_log_step}")
834
- elif args.start_epoch > 0:
835
- # Automatic calculation: Each epoch has len(train_loader) steps, recorded once every LOG_EVERY step
836
- steps_per_epoch = len(train_loader)
837
- train_log_step = (args.start_epoch * steps_per_epoch) // LOG_EVERY
838
- if is_master:
839
- print(f"[Resume] Calculated train_log_step: {train_log_step} from start_epoch: {args.start_epoch}")
840
- else:
841
- train_log_step = 0
842
- # valid_log_step = 0 #Old code (annotated): shared by step verification and epoch verification, resulting in confusing curves
843
-
844
  assert EPOCHS != 0
845
  dist.barrier()
846
 
@@ -955,18 +862,7 @@ def any_rank_true(local_flag: bool) -> bool:
955
  )
956
  else:
957
  print(f"- epoch {i} step {index} train_loss {loss.item()} -")
958
- if is_master and args.use_wandb:
959
- wandb_payload = {
960
- "train_loss": loss.item(),
961
- "learning_rate": scheduler.optimizer.param_groups[0]['lr'],
962
- "train_log_step": train_log_step,
963
- }
964
- loss_breakdown = get_loss_breakdown(model)
965
- if loss_breakdown:
966
- wandb_payload["train_recon_loss"] = loss_breakdown.get("recon_loss", 0.0)
967
- wandb_payload["train_cls_loss"] = loss_breakdown.get("cls_loss", 0.0)
968
- wandb.log(wandb_payload)# train every LOG_EVERY step train_log_step train_loss, learning_rate
969
- train_log_step += 1
970
  if args.latest_ckpt_every > 0 and (index % args.latest_ckpt_every == 0):
971
  latest_ckpt_dir = os.path.join(ckpt_dir, 'latest')
972
  os.makedirs(latest_ckpt_dir, exist_ok=True)
@@ -1005,10 +901,6 @@ def any_rank_true(local_flag: bool) -> bool:
1005
 
1006
  # Step 5.2: Print verification progress regularly
1007
  if val_index % LOG_EVERY == 0:
1008
- # Commented out old code: each step of verification loss is recorded to wandb (leading to confusing curves)
1009
- # if is_master and args.use_wandb:
1010
- # wandb.log({"val_step_loss": loss_val.item(), "valid_log_step": valid_log_step})
1011
- # valid_log_step += 1
1012
  if is_master:
1013
  current_time = time.time()
1014
  if current_time - last_save_time >= save_time_interval:
@@ -1058,14 +950,11 @@ def any_rank_true(local_flag: bool) -> bool:
1058
  # Make sure all processes are in sync
1059
  dist.barrier()
1060
 
1061
- # Step 5.5: Print verification results and record wandb
1062
  if is_master:
1063
  current_time = time.time()
1064
  time_stamp = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
1065
  print(f' == Epoch: {i} Step {index} Time {time_stamp} | Validation Loss: {loss_val_reduced:.6f}')
1066
- if args.use_wandb:
1067
- # val_in_train_loss: The average loss verified during training, with epoch as the x-axis
1068
- wandb.log({"val_in_train_loss": loss_val_reduced, "epoch": i})
1069
  model.train() # Switch back to training mode
1070
  dist.barrier()
1071
 
@@ -1075,8 +964,6 @@ def any_rank_true(local_flag: bool) -> bool:
1075
  epoch_loss = get_reduced(epoch_loss, local_rank, 0, world_size) # Multi-process average
1076
  if is_master:
1077
  print(f' == Epoch: {i} | Training Loss: {epoch_loss:.6f}')
1078
- if args.use_wandb:
1079
- wandb.log({"train_epoch_loss": epoch_loss, "epoch": i}) # Epoch training loss, with epoch as the x-axis
1080
  dist.barrier()
1081
  if not args.lr_step_within_epoch:
1082
  scheduler.step() # Update learning rate
@@ -1114,10 +1001,6 @@ def any_rank_true(local_flag: bool) -> bool:
1114
  if index % LOG_EVERY == 0:
1115
  if is_master:
1116
  print(f"- epoch {i} step {index} val_loss {loss.item()} -")
1117
- # Commented out old code: each step of verification loss is recorded to wandb (leading to confusing curves)
1118
- # if is_master and args.use_wandb:
1119
- # wandb.log({"val_loss": loss.item(), "valid_log_step": valid_log_step})
1120
- # valid_log_step += 1
1121
  if is_master:
1122
  current_time = time.time()
1123
  if current_time - last_save_time >= save_time_interval:
@@ -1162,13 +1045,10 @@ def any_rank_true(local_flag: bool) -> bool:
1162
  finally:
1163
  dist.barrier()
1164
 
1165
- # Step 7.6: Print epoch verification results and record wandb
1166
  if is_master:
1167
  t_epoch_end = time.time()
1168
  print(f' == Epoch: {i} | Validation Loss: {val_loss:.6f} | epoch_cost_time: {t_epoch_end-t_epoch_start:.3f}')
1169
- if args.use_wandb:
1170
- # val_epoch_loss: The average loss of epoch verification, with epoch as the x-axis
1171
- wandb.log({"val_epoch_loss": val_loss, "epoch": i})
1172
 
1173
  # ========== [Step 8: Save Checkpoint] ==========
1174
  # Save model checkpoint at the end of each epoch
 
65
  sys.path.insert(0, str(SOURCE_DIR))
66
  from models_biomod import (
67
  BioMod,
68
+ normalize_biomod_decoder_type,
69
  sanitize_biomod_config,
70
  )
71
  from biomod_dataset import BioModSCDataset, load_biomod_data
 
82
  from torch import autocast
83
  from torch.cuda.amp import GradScaler
84
 
 
 
85
  # ========== [Step 0: Parameter Analysis] ==========
86
  # Example: Assume the default parameters batch_size=3, gene_num=27874, mask_ratio=0.75, grad_acc=60
87
  parser = argparse.ArgumentParser()
 
115
  help='Reconstruct loss aggregation method: gene_mean=masked gene equal weight (current); module_mean=masked module equal weight (MAE style)')
116
  parser.add_argument("--recon_target_level", type=str, default="gene", choices=["gene", "module_mean_value"],
117
  help='Reconstruction target granularity: gene=gene-by-gene reconstruction; module_mean_value=decoder directly outputs the module mean and calculates the loss with the target mean')
118
+ parser.add_argument("--decoder_type", type=normalize_biomod_decoder_type, default="biomod", choices=["none", "simple", "biomod", "cross_attn"],
119
+ help='decoder type: none=directly use encoder to output predictions; simple=no token mixing; biomod=decoder self-attention; cross_attn=only masked->visible cross-attention')
120
  parser.add_argument("--decoder_gene_agg", action='store_true',
121
  help='The reconstruction head is changed to module->gene aggregation and then gene-level reconstruction (off by default to facilitate comparison with the old module_head)')
122
  parser.add_argument("--dec_embed_dim", type=int, default=512, help='decoder token dimension (can be smaller than encoder base_dim, default 512)')
123
+ parser.add_argument("--dec_depth", type=int, default=4, help='Decoder layer number (valid when biomod/cross_attn)')
124
+ parser.add_argument("--dec_heads", type=int, default=8, help='Number of decoder attention heads (valid when biomod/cross_attn)')
125
+ parser.add_argument("--dec_dim_head", type=int, default=64, help='BioMod decoder dimension per head (ignored when cross_attn)')
126
  parser.add_argument("--ckpt_dir", type=str, default=str(PROJECT_ROOT / 'tutorial_runs' / 'pretraining'), help='Directory for generated checkpoints; user relative paths use the current working directory.')
127
  parser.add_argument("--model_name", type=str, default='biomod_27k', help='Pretrained model name.')
128
  parser.add_argument("--data_path", type=str, required=True,
 
139
  parser.add_argument("--fusion_method", type=str, default="multiply",
140
  choices=["multiply", "add", "gated_residual"],
141
  help='Fusion method: "multiply"=multiply; "add"=add to gene2vec after expressing MLP; "gated_residual"=inject expression information into the gated residual')
 
142
  parser.add_argument("-compile", action='store_true')
143
  parser.add_argument("--model_path", type=str, default='None',
144
  help='Checkpoint file to load; use its matching configuration for continued pretraining.')
 
158
  parser.add_argument("--latest_ckpt_every", type=int, default=0,
159
  help='The latest checkpoint saves the cadence; 0 means closed (only timing and epoch are saved)')
160
  parser.add_argument("--amp_dtype", type=str, default="bfloat16")
 
 
 
 
161
  parser.add_argument("--load_opt_and_sched", action='store_true')
162
  parser.add_argument("--lr_step_within_epoch", action='store_true',
163
  help='Update the learning rate according to the optimizer update step; the cycle length is updates_per_epoch (gradually decreasing in each epoch)')
 
305
  elif args.pretrain_arch == "encdec":
306
  if args.mask_style != "replace":
307
  raise ValueError("pretrain_arch=encdec requires --mask_style replace")
308
+ if args.decoder_type not in {"biomod", "cross_attn"}:
309
+ raise ValueError("pretrain_arch=encdec requires decoder_type in {'biomod', 'cross_attn'}")
310
 
311
  local_rank = args.local_rank
312
  rank = int(os.environ["RANK"])
313
  is_master = rank == 0
314
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
315
  SEED = args.seed
316
  EPOCHS = args.epoch
317
  BATCH_SIZE = args.batch_size # Example value: 3
 
501
  "use_gene2vec": use_gene2vec, # Whether to use pretrained gene2vec (ablation experiment)
502
  "loss_mode": args.loss_mode, # Loss aggregation method: gene_mean / module_mean
503
  "recon_target_level": args.recon_target_level, # Reconstruction target granularity: gene/module_mean_value
504
+ "decoder_type": args.decoder_type, # decoder type: simple / biomod
505
  "decoder_gene_agg": args.decoder_gene_agg, # True: module->gene aggregation and reconstruction; False: module_head reconstruction
506
  "dec_embed_dim": args.dec_embed_dim,
507
  "dec_depth": args.dec_depth,
 
615
  f"contains loaded weights rather than random initialization."
616
  )
617
  dist.barrier()
 
 
618
  dist.destroy_process_group()
619
  raise SystemExit(0)
620
 
 
748
  model = torch.compile(model)
749
 
750
  # ========== [Step 4: Training Cycle] ==========
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
751
  assert EPOCHS != 0
752
  dist.barrier()
753
 
 
862
  )
863
  else:
864
  print(f"- epoch {i} step {index} train_loss {loss.item()} -")
865
+ if is_master:
 
 
 
 
 
 
 
 
 
 
 
866
  if args.latest_ckpt_every > 0 and (index % args.latest_ckpt_every == 0):
867
  latest_ckpt_dir = os.path.join(ckpt_dir, 'latest')
868
  os.makedirs(latest_ckpt_dir, exist_ok=True)
 
901
 
902
  # Step 5.2: Print verification progress regularly
903
  if val_index % LOG_EVERY == 0:
 
 
 
 
904
  if is_master:
905
  current_time = time.time()
906
  if current_time - last_save_time >= save_time_interval:
 
950
  # Make sure all processes are in sync
951
  dist.barrier()
952
 
953
+ # Step 5.5: Print validation results
954
  if is_master:
955
  current_time = time.time()
956
  time_stamp = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
957
  print(f' == Epoch: {i} Step {index} Time {time_stamp} | Validation Loss: {loss_val_reduced:.6f}')
 
 
 
958
  model.train() # Switch back to training mode
959
  dist.barrier()
960
 
 
964
  epoch_loss = get_reduced(epoch_loss, local_rank, 0, world_size) # Multi-process average
965
  if is_master:
966
  print(f' == Epoch: {i} | Training Loss: {epoch_loss:.6f}')
 
 
967
  dist.barrier()
968
  if not args.lr_step_within_epoch:
969
  scheduler.step() # Update learning rate
 
1001
  if index % LOG_EVERY == 0:
1002
  if is_master:
1003
  print(f"- epoch {i} step {index} val_loss {loss.item()} -")
 
 
 
 
1004
  if is_master:
1005
  current_time = time.time()
1006
  if current_time - last_save_time >= save_time_interval:
 
1045
  finally:
1046
  dist.barrier()
1047
 
1048
+ # Step 7.6: Print epoch validation results
1049
  if is_master:
1050
  t_epoch_end = time.time()
1051
  print(f' == Epoch: {i} | Validation Loss: {val_loss:.6f} | epoch_cost_time: {t_epoch_end-t_epoch_start:.3f}')
 
 
 
1052
 
1053
  # ========== [Step 8: Save Checkpoint] ==========
1054
  # Save model checkpoint at the end of each epoch
BioMod/cell_annotation/train.py CHANGED
@@ -695,7 +695,7 @@ def infer_model_config(args, checkpoint: dict, pkl_config: Optional[dict], biomo
695
  elif isinstance(checkpoint, dict) and "config" in checkpoint:
696
  config = sanitize_biomod_config(
697
  checkpoint["config"],
698
- source=str(resolve_path(args.performer_checkpoint)),
699
  log_fn=logger.info,
700
  )
701
  logger.info("Using config from checkpoint")
@@ -767,7 +767,7 @@ def load_backbone(args, device: torch.device) -> Tuple["BioMod", List[List[int]]
767
  else:
768
  logger.warning("Config PKL not found at %s; fallback to checkpoint config", config_path)
769
 
770
- ckpt_path = resolve_path(args.performer_checkpoint)
771
  checkpoint = torch.load(ckpt_path, map_location=device)
772
 
773
  biomod_data = None
@@ -1173,11 +1173,12 @@ def parse_args():
1173
  parser.add_argument("--max_cells", type=int, default=None)
1174
 
1175
  parser.add_argument("--config_pkl", type=str, default=str(WEIGHTS_DIR / "BioMod.pkl"))
 
1176
  parser.add_argument(
1177
  "--biomod_checkpoint", "--performer_checkpoint",
1178
  type=str,
1179
  default=str(WEIGHTS_DIR / "BioMod.pth"),
1180
- dest='performer_checkpoint',
1181
  )
1182
  parser.add_argument("--biomod_data_dir", "--module_data_dir", type=str, default=str(ASSET_DIR / "biomod_data_all"), dest='module_data_dir')
1183
  parser.add_argument("--gene_list_path", type=str, default=str(ASSET_DIR / "selected_genes_27k.txt"))
 
695
  elif isinstance(checkpoint, dict) and "config" in checkpoint:
696
  config = sanitize_biomod_config(
697
  checkpoint["config"],
698
+ source=str(resolve_path(args.biomod_checkpoint)),
699
  log_fn=logger.info,
700
  )
701
  logger.info("Using config from checkpoint")
 
767
  else:
768
  logger.warning("Config PKL not found at %s; fallback to checkpoint config", config_path)
769
 
770
+ ckpt_path = resolve_path(args.biomod_checkpoint)
771
  checkpoint = torch.load(ckpt_path, map_location=device)
772
 
773
  biomod_data = None
 
1173
  parser.add_argument("--max_cells", type=int, default=None)
1174
 
1175
  parser.add_argument("--config_pkl", type=str, default=str(WEIGHTS_DIR / "BioMod.pkl"))
1176
+ # Retain the legacy CLI spelling as a compatibility alias.
1177
  parser.add_argument(
1178
  "--biomod_checkpoint", "--performer_checkpoint",
1179
  type=str,
1180
  default=str(WEIGHTS_DIR / "BioMod.pth"),
1181
+ dest='biomod_checkpoint',
1182
  )
1183
  parser.add_argument("--biomod_data_dir", "--module_data_dir", type=str, default=str(ASSET_DIR / "biomod_data_all"), dest='module_data_dir')
1184
  parser.add_argument("--gene_list_path", type=str, default=str(ASSET_DIR / "selected_genes_27k.txt"))
BioMod/command.txt CHANGED
@@ -28,4 +28,3 @@ torchrun --standalone --nproc_per_node="${NPROC_PER_NODE:-1}" \
28
  --dec_embed_dim 1024
29
 
30
  # Adjust cell count and optimization settings to your own dataset and experiment.
31
- # Add -use_wandb --api_key "$WANDB_API_KEY" to enable optional experiment logging.
 
28
  --dec_embed_dim 1024
29
 
30
  # Adjust cell count and optimization settings to your own dataset and experiment.
 
BioMod/environment.yml CHANGED
@@ -62,7 +62,6 @@ dependencies:
62
  - sentence-transformers>=2.7.0
63
  - transformers>=4.40.0
64
  - openai>=1.40.0
65
- - wandb==0.23.0
66
 
67
  # Only needed if running legacy pickles on Python < 3.8.
68
  - pickle5>=0.0.12; python_version < "3.8"
 
62
  - sentence-transformers>=2.7.0
63
  - transformers>=4.40.0
64
  - openai>=1.40.0
 
65
 
66
  # Only needed if running legacy pickles on Python < 3.8.
67
  - pickle5>=0.0.12; python_version < "3.8"
BioMod/gene_perturbation/components/model.py CHANGED
@@ -11,7 +11,7 @@
11
  - Use attention pooling to aggregate genetic information within the module
12
 
13
  2. BioMod: Encoder-only Transformer model
14
- - Use Performer (Linear Complexity Attention) to process large numbers of module tokens
15
  - Support Masked Autoencoder (MAE) pre-training paradigm
16
 
17
  3. SCFM_BioMod: wrapper for pre-trained models
@@ -45,8 +45,8 @@
45
  import scipy
46
 
47
  # ============================================================
48
- # Import Performer related components (does not depend on gears_reduce)
49
- # Performer is a linear complexity Transformer variant suitable for processing long sequences
50
  # ============================================================
51
  try:
52
  from BioMod.models_biomod import (
 
11
  - Use attention pooling to aggregate genetic information within the module
12
 
13
  2. BioMod: Encoder-only Transformer model
14
+ - Use the BioMod encoder to contextualize module tokens
15
  - Support Masked Autoencoder (MAE) pre-training paradigm
16
 
17
  3. SCFM_BioMod: wrapper for pre-trained models
 
45
  import scipy
46
 
47
  # ============================================================
48
+ # Import BioMod model components (does not depend on gears_reduce)
49
+ # Attention and encoder behavior are defined in BioMod.models_biomod.
50
  # ============================================================
51
  try:
52
  from BioMod.models_biomod import (
BioMod/gene_perturbation/components/trainer.py CHANGED
@@ -68,8 +68,7 @@ class GEARS:
68
  [Core functions]
69
  1. Manage the entire training process (data, model, optimization, evaluation)
70
  2. Support distributed training (DDP)
71
- 3. Support Weights & Biases experiment tracking
72
- 4. Support model saving/loading
73
 
74
  [Usage process]
75
  ```python
@@ -86,7 +85,6 @@ def __init__(self, pert_data,
86
  train_bs = 48,
87
  test_bs = 48,
88
  device = 'cuda',
89
- weight_bias_track = True,
90
  proj_name = 'GEARS',
91
  exp_name = 'GEARS',):
92
  """
@@ -97,20 +95,12 @@ def __init__(self, pert_data,
97
  2. Control group expression value (used for direction loss calculation)
98
  3. dict_filter: Non-zero gene index corresponding to each perturbation (affects the training loss calculation range)
99
  """
100
- self.weight_bias_track = weight_bias_track
101
  self.is_master = is_master
102
  self.local_rank = local_rank
103
  self.world_size = world_size
104
  self.proj_name = proj_name
105
  self.exp_name = exp_name
106
 
107
- # Weights & Biases Experiment Tracking
108
- if self.weight_bias_track:
109
- import wandb
110
- wandb.init(project=proj_name, name=exp_name, entity = "deanghoss")
111
- self.wandb = wandb
112
- else:
113
- self.wandb = None
114
 
115
  self.device = f'cuda:{local_rank}'
116
  torch.cuda.set_device(device)
@@ -309,8 +299,6 @@ def model_initialize(self, hidden_size = 64,
309
  print('Use mode:',mode)
310
  self.config.update(kwargs)
311
 
312
- if self.wandb:
313
- self.wandb.config.update(self.config)
314
 
315
  # ============================================================
316
  # [Key] Constructing a co-expression graph
@@ -634,8 +622,6 @@ def train(self,
634
 
635
  if (step + 1) % 50 == 0:
636
  if self.is_master:
637
- if self.wandb:
638
- self.wandb.log({'training_loss': loss.item()})
639
  log = "Epoch {} Step {} Train Loss: {:.4f}"
640
  print_sys(log.format(epoch + 1, step + 1, loss.item()))
641
  dist.barrier()
@@ -655,10 +641,6 @@ def train(self,
655
 
656
  if self.is_master:
657
  print_sys(log.format(epoch + 1, val_metrics['mse'], val_metrics['mse_de']))
658
- if self.wandb:
659
- metrics = ['mse', 'pearson']
660
- for m in metrics:
661
- self.wandb.log({'val_'+m: val_metrics[m],'val_de_'+m: val_metrics[m + '_de']})
662
  # Use `mse_de` as early stop/select optimal criterion: pay more attention to DE gene prediction quality.
663
  if val_metrics['mse_de'] < min_val:
664
  min_val = val_metrics['mse_de']
@@ -702,12 +684,6 @@ def train(self,
702
  log = "Best performing model: Test Top 20 DE MSE: {:.4f}"
703
  if self.is_master:
704
  print_sys(log.format(test_metrics['mse_de']))
705
- if self.wandb:
706
- metrics = ['mse', 'pearson']
707
- for m in metrics:
708
- self.wandb.log({'test_' + m: test_metrics[m],
709
- 'test_de_'+m: test_metrics[m + '_de']
710
- })
711
 
712
  out = deeper_analysis(self.adata, test_res)
713
  out_non_dropout = non_dropout_analysis(self.adata, test_res)
@@ -729,8 +705,6 @@ def train(self,
729
  for name, result in subgroup_analysis.items():
730
  for m in result.keys():
731
  subgroup_analysis[name][m] = np.mean(subgroup_analysis[name][m])
732
- if self.wandb:
733
- self.wandb.log({'test_' + name + '_' + m: subgroup_analysis[name][m]})
734
 
735
  print_sys('test_' + name + '_' + m + ': ' + str(subgroup_analysis[name][m]))
736
 
@@ -756,8 +730,6 @@ def train(self,
756
  for name, result in subgroup_analysis.items():
757
  for m in result.keys():
758
  subgroup_analysis[name][m] = np.mean(subgroup_analysis[name][m])
759
- if self.wandb:
760
- self.wandb.log({'test_' + name + '_' + m: subgroup_analysis[name][m]})
761
 
762
  print_sys('test_' + name + '_' + m + ': ' + str(subgroup_analysis[name][m]))
763
  print_sys('Done!')
 
68
  [Core functions]
69
  1. Manage the entire training process (data, model, optimization, evaluation)
70
  2. Support distributed training (DDP)
71
+ 3. Support model saving/loading
 
72
 
73
  [Usage process]
74
  ```python
 
85
  train_bs = 48,
86
  test_bs = 48,
87
  device = 'cuda',
 
88
  proj_name = 'GEARS',
89
  exp_name = 'GEARS',):
90
  """
 
95
  2. Control group expression value (used for direction loss calculation)
96
  3. dict_filter: Non-zero gene index corresponding to each perturbation (affects the training loss calculation range)
97
  """
 
98
  self.is_master = is_master
99
  self.local_rank = local_rank
100
  self.world_size = world_size
101
  self.proj_name = proj_name
102
  self.exp_name = exp_name
103
 
 
 
 
 
 
 
 
104
 
105
  self.device = f'cuda:{local_rank}'
106
  torch.cuda.set_device(device)
 
299
  print('Use mode:',mode)
300
  self.config.update(kwargs)
301
 
 
 
302
 
303
  # ============================================================
304
  # [Key] Constructing a co-expression graph
 
622
 
623
  if (step + 1) % 50 == 0:
624
  if self.is_master:
 
 
625
  log = "Epoch {} Step {} Train Loss: {:.4f}"
626
  print_sys(log.format(epoch + 1, step + 1, loss.item()))
627
  dist.barrier()
 
641
 
642
  if self.is_master:
643
  print_sys(log.format(epoch + 1, val_metrics['mse'], val_metrics['mse_de']))
 
 
 
 
644
  # Use `mse_de` as early stop/select optimal criterion: pay more attention to DE gene prediction quality.
645
  if val_metrics['mse_de'] < min_val:
646
  min_val = val_metrics['mse_de']
 
684
  log = "Best performing model: Test Top 20 DE MSE: {:.4f}"
685
  if self.is_master:
686
  print_sys(log.format(test_metrics['mse_de']))
 
 
 
 
 
 
687
 
688
  out = deeper_analysis(self.adata, test_res)
689
  out_non_dropout = non_dropout_analysis(self.adata, test_res)
 
705
  for name, result in subgroup_analysis.items():
706
  for m in result.keys():
707
  subgroup_analysis[name][m] = np.mean(subgroup_analysis[name][m])
 
 
708
 
709
  print_sys('test_' + name + '_' + m + ': ' + str(subgroup_analysis[name][m]))
710
 
 
730
  for name, result in subgroup_analysis.items():
731
  for m in result.keys():
732
  subgroup_analysis[name][m] = np.mean(subgroup_analysis[name][m])
 
 
733
 
734
  print_sys('test_' + name + '_' + m + ': ' + str(subgroup_analysis[name][m]))
735
  print_sys('Done!')
BioMod/gene_perturbation/train.py CHANGED
@@ -169,7 +169,6 @@ def main(parser):
169
  local_rank=local_rank,
170
  is_master=is_master,
171
  world_size=world_size,
172
- weight_bias_track=args.wandb,
173
  train_bs=args.batch_size,
174
  test_bs=args.test_batch_size,
175
  device=device,
@@ -271,7 +270,6 @@ def main(parser):
271
  parser.add_argument('--finetune_method', type=str, default=None)
272
  parser.add_argument('--mode', type=str, default='v1')
273
  parser.add_argument('--accumulation_steps', type=int, default=1)
274
- parser.add_argument('-wandb', action='store_true', help='Enable Weights & Biases logging')
275
  parser.add_argument('-record_pred', action='store_true', help='Record predictions for analysis')
276
 
277
  # ============================================================
 
169
  local_rank=local_rank,
170
  is_master=is_master,
171
  world_size=world_size,
 
172
  train_bs=args.batch_size,
173
  test_bs=args.test_batch_size,
174
  device=device,
 
270
  parser.add_argument('--finetune_method', type=str, default=None)
271
  parser.add_argument('--mode', type=str, default='v1')
272
  parser.add_argument('--accumulation_steps', type=int, default=1)
 
273
  parser.add_argument('-record_pred', action='store_true', help='Record predictions for analysis')
274
 
275
  # ============================================================
BioMod/gene_perturbation/tutorial.ipynb CHANGED
@@ -139,7 +139,6 @@
139
  "run_env.setdefault('NUMBA_NUM_THREADS', '1')\n",
140
  "run_env['NUMBA_DISABLE_JIT'] = '1'\n",
141
  "run_env.setdefault('NUMBA_CACHE_DIR', str(Path(tempfile.gettempdir()) / f'biomod_perturbation_{os.getpid()}'))\n",
142
- "run_env['WANDB_MODE'] = 'offline'\n",
143
  "run_env.setdefault('TORCH_NCCL_ASYNC_ERROR_HANDLING', '1')\n",
144
  "run_env.pop('NCCL_ASYNC_ERROR_HANDLING', None)\n",
145
  "run_env.setdefault('NCCL_DEBUG', 'WARN')\n",
 
139
  "run_env.setdefault('NUMBA_NUM_THREADS', '1')\n",
140
  "run_env['NUMBA_DISABLE_JIT'] = '1'\n",
141
  "run_env.setdefault('NUMBA_CACHE_DIR', str(Path(tempfile.gettempdir()) / f'biomod_perturbation_{os.getpid()}'))\n",
 
142
  "run_env.setdefault('TORCH_NCCL_ASYNC_ERROR_HANDLING', '1')\n",
143
  "run_env.pop('NCCL_ASYNC_ERROR_HANDLING', None)\n",
144
  "run_env.setdefault('NCCL_DEBUG', 'WARN')\n",
BioMod/models_biomod.py CHANGED
The diff for this file is too large to render. See raw diff
 
BioMod/requirements.txt CHANGED
@@ -58,7 +58,6 @@ lifelines>=0.27.0
58
  sentence-transformers>=2.7.0
59
  transformers>=4.40.0
60
  openai>=1.40.0
61
- wandb==0.23.0
62
 
63
  # Only needed if running legacy pickles on Python < 3.8.
64
  pickle5>=0.0.12; python_version < "3.8"
 
58
  sentence-transformers>=2.7.0
59
  transformers>=4.40.0
60
  openai>=1.40.0
 
61
 
62
  # Only needed if running legacy pickles on Python < 3.8.
63
  pickle5>=0.0.12; python_version < "3.8"
data/downstream/final_data/drug_combination_synergy/MANIFEST.csv CHANGED
@@ -27,4 +27,4 @@ data_summary/test_data/independent_set_newcombinatioin.csv,249186,0.2376,7b7aadb
27
  data_summary/test_data/README.md,2680,0.0026,ae5fa6fe381c77e3e75411a556c43a8c2adbbff49354793f08baa9b17ec4d751
28
  data_summary/test_data/readme.txt,210,0.0002,eb984e1c5f106090fbc532371bf950dfe7f54bd7045ea344209423c544128481
29
  README.md,1469,0.0014,0f394dd952fcce76519f20f20ccb8ffbe37235a105ca5cf6becfe838dab4392b
30
- SUMMARY.json,705,0.0007,f5061ffd83cc75fe82b936ce9e57f836219e31fba8a27a064f3c88b74dee413b
 
27
  data_summary/test_data/README.md,2680,0.0026,ae5fa6fe381c77e3e75411a556c43a8c2adbbff49354793f08baa9b17ec4d751
28
  data_summary/test_data/readme.txt,210,0.0002,eb984e1c5f106090fbc532371bf950dfe7f54bd7045ea344209423c544128481
29
  README.md,1469,0.0014,0f394dd952fcce76519f20f20ccb8ffbe37235a105ca5cf6becfe838dab4392b
30
+ SUMMARY.json,653,0.0006,c28072fba34d958ed863ab81aeb4b94a67f69c8c4d54b0f784f461986aed74a5
data/downstream/final_data/drug_combination_synergy/SUMMARY.json CHANGED
@@ -1,5 +1,5 @@
1
  {
2
- "runnable_data_path": "C:\\Users\\JXZ\\Desktop\\文章的相关材料\\data\\downstream\\final_data\\drug_combination_synergy\\data_summary",
3
  "train_labels_shape": [
4
  12415,
5
  4
 
1
  {
2
+ "runnable_data_path": "data/downstream/final_data/drug_combination_synergy/data_summary",
3
  "train_labels_shape": [
4
  12415,
5
  4
data/downstream/final_data/single_drug_response/MANIFEST.csv CHANGED
@@ -238,4 +238,4 @@ optional_reference/DeepCDR_with_mut_with_gexp_with_methy_256_256_256_bn_relu_GAP
238
  optional_reference/GDSC/drug_graph_feat.zip,576374,0.5497,a059696577dcf04c37dbcd41ba67018fbe3ac800e450a275f0ed8201a012e4c2
239
  optional_reference/Missing_data_pre/records_pre_all.txt,1257750,1.1995,9298474435977de7934303f73627078954e99e453ba0588029a0315da04c9c60
240
  README.md,1443,0.0014,389085fc257709861b0cb3e48043058b8be8632b006ec69290d055af2fdfec99
241
- SUMMARY.json,364,0.0003,c63b210710307366068b68321dfc1b07eb567a9bb37ae2fd8689bb56413f83f7
 
238
  optional_reference/GDSC/drug_graph_feat.zip,576374,0.5497,a059696577dcf04c37dbcd41ba67018fbe3ac800e450a275f0ed8201a012e4c2
239
  optional_reference/Missing_data_pre/records_pre_all.txt,1257750,1.1995,9298474435977de7934303f73627078954e99e453ba0588029a0315da04c9c60
240
  README.md,1443,0.0014,389085fc257709861b0cb3e48043058b8be8632b006ec69290d055af2fdfec99
241
+ SUMMARY.json,312,0.0003,3fc48a122addd1c117877d8bd3323c7ea8fe5df0fd13f7f732718c2f15e20865
data/downstream/final_data/single_drug_response/SUMMARY.json CHANGED
@@ -1,5 +1,5 @@
1
  {
2
- "runnable_data_path": "C:\\Users\\JXZ\\Desktop\\文章的相关材料\\data\\downstream\\final_data\\single_drug_response\\data",
3
  "drug_info_shape": [
4
  266,
5
  8
 
1
  {
2
+ "runnable_data_path": "data/downstream/final_data/single_drug_response/data",
3
  "drug_info_shape": [
4
  266,
5
  8