-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtrain_protein_llm.py
More file actions
1229 lines (1101 loc) · 51.9 KB
/
train_protein_llm.py
File metadata and controls
1229 lines (1101 loc) · 51.9 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Set envioronment variables
import os
os.environ["TOKENIZERS_PARALLELISM"] = "false"
os.environ["NCCL_CUMEM_ENABLE"] = "1"
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
os.environ["UNSLOTH_DISABLE_FAST_GENERATION"] = "1"
# Import unsloth
import unsloth
from unsloth import FastLanguageModel
# Standard library imports
import os
import time
from argparse import ArgumentParser
from functools import partial
from typing import Optional, Dict
# Third-party imports
import pytorch_lightning as pl
import torch
import wandb
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
from transformers import BitsAndBytesConfig
from pytorch_lightning.callbacks import (
LearningRateMonitor,
ModelCheckpoint,
DeviceStatsMonitor,
Callback,
)
from pytorch_lightning.loggers import WandbLogger
from pytorch_lightning.profilers import AdvancedProfiler
from torch.optim import AdamW
from torch.utils.data import DataLoader
from torch.optim.lr_scheduler import LinearLR, CosineAnnealingLR, SequentialLR
from transformers import logging
from datasets import Value, concatenate_datasets
# Local imports
from bioreason2.dataset.cafa5.collate import qwen_protein_collate_fn
from bioreason2.dataset.cafa5.generate import generate_single_response
from bioreason2.dataset.cafa5.load import load_cafa5_dataset
from bioreason2.models.pl.processing_pl import PLProcessor
from bioreason2.models.protein_llm import (
ProteinLLMModel,
_get_target_modules,
)
from bioreason2.utils import str2bool
# Set start method to 'spawn' for CUDA compatibility with multiprocessing
torch.multiprocessing.set_sharing_strategy("file_system")
logging.set_verbosity_error()
class EpochCheckpointFromN(Callback):
"""Custom callback to save checkpoints every epoch starting from a specific epoch."""
def __init__(self, checkpoint_dir, run_name, start_epoch):
super().__init__()
self.checkpoint_dir = checkpoint_dir
self.run_name = run_name
self.start_epoch = start_epoch
def on_train_epoch_end(self, trainer, pl_module):
"""Save checkpoint at the end of each epoch if epoch >= start_epoch."""
if trainer.current_epoch >= self.start_epoch:
checkpoint_path = os.path.join(
self.checkpoint_dir,
f"{self.run_name}-epoch={trainer.current_epoch:02d}.ckpt"
)
trainer.save_checkpoint(checkpoint_path)
print(f"✓ Saved epoch checkpoint: {checkpoint_path}")
class ProteinLLMFineTuner(pl.LightningModule):
"""
PyTorch Lightning module for fine-tuning Protein-LLM models.
"""
def __init__(self, hparams, train_dataset=None, val_dataset=None, test_dataset=None):
"""
Initialize the ProteinLLMFineTuner.
Args:
hparams: Hyperparameters for the model and training
train_dataset: Pre-loaded training dataset
val_dataset: Pre-loaded validation dataset
test_dataset: Pre-loaded test dataset
"""
super().__init__()
self.save_hyperparameters(hparams)
self.text_model_name = self.hparams.text_model_name
self.protein_model_name = self.hparams.protein_model_name
self.cache_dir = self.hparams.cache_dir
self.learning_rate = self.hparams.learning_rate
self.weight_decay = self.hparams.weight_decay
self.warmup_ratio = self.hparams.warmup_ratio
self.text_model_finetune = self.hparams.text_model_finetune
self.protein_model_finetune = self.hparams.protein_model_finetune
self.protein_train_layer_start = self.hparams.protein_train_layer_start
self.protein_embedding_layer = self.hparams.protein_embedding_layer
self.go_model_finetune = self.hparams.go_model_finetune
self.attn_implementation = self.hparams.attn_implementation
self.go_obo_path = self.hparams.go_obo_path
self.precomputed_embeddings_path = self.hparams.precomputed_embeddings_path
self.go_hidden_dim = self.hparams.go_hidden_dim
self.go_num_gat_layers = self.hparams.go_num_gat_layers
self.go_num_heads = self.hparams.go_num_heads
self.go_num_reduced_embeddings = self.hparams.go_num_reduced_embeddings
self.go_embedding_dim = self.hparams.go_embedding_dim
self.lora_rank = self.hparams.lora_rank
self.lora_alpha = self.hparams.lora_alpha
self.lora_dropout = self.hparams.lora_dropout
self.max_length_protein = self.hparams.max_length_protein
self.max_length_text = self.hparams.max_length_text
self.return_answer_in_batch = self.hparams.return_answer_in_batch
self.training_stage = self.hparams.training_stage
self.projector_checkpoint_path = self.hparams.projector_checkpoint_path
self.go_projection_checkpoint_path = self.hparams.go_projection_checkpoint_path
self.go_encoder_checkpoint_path = self.hparams.go_encoder_checkpoint_path
self.enable_sample_generation = self.hparams.enable_sample_generation
self.verbose_sample_generation = self.hparams.verbose_sample_generation
self.every_n_train_steps = self.hparams.every_n_train_steps
self.unified_go_encoder = self.hparams.unified_go_encoder
self.use_unsloth = self.hparams.use_unsloth
# Store dataset configuration
self.dataset_type = self.hparams.dataset_type
# Store datasets
self._train_dataset = train_dataset
self._val_dataset = val_dataset
self._test_dataset = test_dataset
# Create quantization config if QLoRA is enabled
quantization_config = None
if self.hparams.use_qlora:
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type=self.hparams.bnb_4bit_quant_type,
bnb_4bit_compute_dtype=self.hparams.bnb_4bit_compute_dtype,
bnb_4bit_use_double_quant=self.hparams.bnb_4bit_use_double_quant,
)
print(f"🔧 QLoRA enabled with {self.hparams.bnb_4bit_quant_type} quantization")
# Load model
self.model = ProteinLLMModel(
text_model_name=self.text_model_name,
protein_model_name=self.protein_model_name,
cache_dir=self.cache_dir,
max_length_protein=self.max_length_protein,
max_length_text=self.max_length_text,
text_model_finetune=self.text_model_finetune,
protein_model_finetune=self.protein_model_finetune,
protein_train_layer_start=self.protein_train_layer_start,
protein_embedding_layer=self.protein_embedding_layer,
go_model_finetune=self.go_model_finetune,
attn_implementation=self.attn_implementation,
go_obo_path=self.go_obo_path,
precomputed_embeddings_path=self.precomputed_embeddings_path,
go_hidden_dim=self.go_hidden_dim,
go_num_gat_layers=self.go_num_gat_layers,
go_num_heads=self.go_num_heads,
go_num_reduced_embeddings=self.go_num_reduced_embeddings,
go_embedding_dim=self.go_embedding_dim,
quantization_config=quantization_config,
unified_go_encoder=self.unified_go_encoder,
use_unsloth=self.use_unsloth,
)
# Load projector weights if provided (for stage 2)
if self.training_stage == 2 and self.projector_checkpoint_path and not self.hparams.ckpt_path:
print(f"Loading projector weights from: {self.projector_checkpoint_path}")
projector_state_dict = torch.load(self.projector_checkpoint_path, map_location=self.device)
self.model.protein_projection.load_state_dict(projector_state_dict)
print("✓ Projector weights loaded successfully.")
# Also load GO projection weights if available
if (
self.go_projection_checkpoint_path
and os.path.exists(self.go_projection_checkpoint_path)
and hasattr(self.model, "go_projection")
and self.model.go_projection is not None
):
print(f"Loading GO projection weights from: {self.go_projection_checkpoint_path}")
go_projection_state_dict = torch.load(self.go_projection_checkpoint_path, map_location=self.device)
self.model.go_projection.load_state_dict(go_projection_state_dict)
print("✓ GO projection weights loaded successfully.")
# Also load GO encoder weights if available
if (
self.go_encoder_checkpoint_path
and os.path.exists(self.go_encoder_checkpoint_path)
and hasattr(self.model, "go_encoder")
and self.model.go_encoder is not None
):
print(f"Loading GO encoder weights from: {self.go_encoder_checkpoint_path}")
go_encoder_state_dict = torch.load(self.go_encoder_checkpoint_path, map_location=self.device)
# Use strict=False to handle architecture changes (old vs new GO encoder)
missing_keys, unexpected_keys = self.model.go_encoder.load_state_dict(go_encoder_state_dict, strict=False)
if missing_keys:
print(f"⚠️ Missing keys in GO encoder checkpoint (will be randomly initialized): {len(missing_keys)}")
# Only show key names for new architecture components
new_arch_missing = [k for k in missing_keys if "all_cross_attention_reducer" in k]
if new_arch_missing:
print(f" - New 'all' cross-attention reducer parameters: {len(new_arch_missing)} keys")
other_missing = [k for k in missing_keys if "all_cross_attention_reducer" not in k]
if other_missing:
print(f" - Other missing keys: {other_missing[:3]}{'...' if len(other_missing) > 3 else ''}")
if unexpected_keys:
print(f"⚠️ Unexpected keys in GO encoder checkpoint (ignored): {len(unexpected_keys)}")
print("✓ GO encoder weights loaded successfully (with architecture compatibility).")
self.text_model = self.model.text_model
self.protein_model = self.model.protein_model
self.protein_projection = self.model.protein_projection
self.go_projection = self.model.go_projection
self.go_encoder = self.model.go_encoder
self.tokenizer = self.model.text_tokenizer
self.lora_config = self._setup_training_strategy()
# --- Detailed Parameter Count ---
protein_trainable = sum(p.numel() for p in self.protein_model.parameters() if p.requires_grad)
protein_total = sum(p.numel() for p in self.protein_model.parameters())
projector_trainable = sum(p.numel() for p in self.protein_projection.parameters() if p.requires_grad)
text_model_trainable = sum(p.numel() for p in self.text_model.parameters() if p.requires_grad)
embed_tokens_trainable = sum(
p.numel() for p in self.text_model.get_input_embeddings().weight if p.requires_grad
)
lm_head_trainable = sum(p.numel() for p in self.text_model.get_output_embeddings().weight if p.requires_grad)
# Count GO encoder and GO projection parameters
go_encoder_trainable = 0
go_projection_trainable = 0
if hasattr(self.model, "go_encoder") and self.model.go_encoder is not None:
go_encoder_trainable = sum(p.numel() for p in self.model.go_encoder.parameters() if p.requires_grad)
if hasattr(self.model, "go_projection") and self.model.go_projection is not None:
go_projection_trainable = sum(p.numel() for p in self.model.go_projection.parameters() if p.requires_grad)
total_trainable = sum(p.numel() for p in self.parameters() if p.requires_grad)
print(f"--- Trainable Parameters (Stage {self.training_stage}) ---")
if protein_total > 0:
protein_pct = (protein_trainable / protein_total) * 100
print(f" - Protein Model: {protein_trainable:,} / {protein_total:,} ({protein_pct:.1f}%)")
else:
print(f" - Protein Model: {protein_trainable:,}")
print(f" - Projector MLP: {projector_trainable:,}")
print(f" - GO Encoder: {go_encoder_trainable:,}")
print(f" - GO Projection: {go_projection_trainable:,}")
print(f" - Text Model (LoRA): {text_model_trainable:,}")
print(f" - Embed Tokens: {embed_tokens_trainable:,}")
print(f" - LM Head: {lm_head_trainable:,}")
print(" ----------------------------------")
print(f" - Total Trainable: {total_trainable:,}")
print("------------------------------------")
def _setup_projection_training(self):
"""
Set up projection layers for training (always trainable).
"""
# Protein projection: always trainable
self.model.protein_projection.train()
for param in self.model.protein_projection.parameters():
param.requires_grad = True
print(" - Protein projection: trainable")
# GO projection: always trainable if available
if hasattr(self.model, "go_projection") and self.model.go_projection is not None:
self.model.go_projection.train()
for param in self.model.go_projection.parameters():
param.requires_grad = True
print(" - GO projection: trainable")
def _setup_training_strategy(self) -> Optional[LoraConfig]:
"""
Configures the training strategy based on the current training stage.
This involves freezing/unfreezing model parts and setting up LoRA if needed.
"""
print("✓ Setting up training configuration...")
# Protein encoder training is now handled automatically during model creation
if self.protein_model_finetune:
print(f" - Protein encoder training enabled (layer start: {self.protein_train_layer_start})")
else:
print(" - Protein encoder: keeping frozen")
# Setup GO encoder training
if hasattr(self.model, "go_encoder") and self.model.go_encoder is not None:
if self.go_model_finetune:
print(" - Enabling GO encoder training")
self.model.go_encoder.train()
for param in self.model.go_encoder.parameters():
param.requires_grad = True
else:
print(" - GO encoder: keeping frozen")
# Setup projection layers (always trainable)
self._setup_projection_training()
# Setup text model training
if self.text_model_finetune:
print(" - Enabling text model LoRA training")
else:
print(" - Text model: keeping frozen")
# Stage 1: Train only the projectors
if self.training_stage == 1:
print("Setting up for Stage 1: Projector training only.")
# Freeze the text model completely and keep in eval mode
self.model.text_model.eval()
for param in self.model.text_model.parameters():
param.requires_grad = False
if hasattr(self.model.text_model, "config"):
self.model.text_model.config.use_cache = False
if hasattr(self.model.text_model, "generation_config"):
self.model.text_model.generation_config.use_cache = False
print(" Text model: frozen")
return None # No LoRA config in stage 1
# Stage 2: Full model fine-tuning (with optional LoRA)
elif self.training_stage == 2:
print("Setting up for Stage 2: Full model fine-tuning.")
lora_config = None
if self.text_model_finetune:
target_modules = _get_target_modules(self.model)
if self.use_unsloth:
self.model.text_model = FastLanguageModel.get_peft_model(
self.model.text_model,
r=self.lora_rank,
target_modules=target_modules,
lora_alpha=self.lora_alpha,
lora_dropout=self.lora_dropout,
bias="none",
use_gradient_checkpointing = "unsloth",
random_state=self.hparams.seed,
use_rslora=False,
loftq_config=None,
)
else:
lora_config = LoraConfig(
r=self.lora_rank,
lora_alpha=self.lora_alpha,
lora_dropout=self.lora_dropout,
target_modules=target_modules,
init_lora_weights="gaussian",
bias="none",
task_type="CAUSAL_LM",
)
self.model.text_model = prepare_model_for_kbit_training(self.model.text_model)
self.model.text_model = get_peft_model(self.model.text_model, lora_config)
self.model.text_model.train()
else:
# Keep text model frozen and in eval mode
print(" Text model remaining frozen")
for param in self.model.text_model.parameters():
param.requires_grad = False
self.model.text_model.eval()
return lora_config
else:
raise ValueError(f"Invalid training stage: {self.training_stage}")
def _step(self, batch: Dict, batch_idx: int, prefix: str) -> torch.Tensor:
"""
Performs a single step for training, validation, or testing.
Args:
batch: Dictionary containing the batch data
batch_idx: Integer indicating the batch index
prefix: String indicating the step type ('train', 'val', or 'test')
Returns:
torch.Tensor: The computed loss for this batch
"""
# Get batch data from the collate function
input_ids = batch["input_ids"].to(self.device)
attention_mask = batch["attention_mask"].to(self.device)
labels = batch["labels"].to(self.device) if "labels" in batch else None
protein_sequences = batch.get("protein_sequences")
batch_idx_map = batch.get("batch_idx_map")
structure_coords = batch.get("structure_coords")
go_aspects = batch.get("batch_go_aspects")
# Forward pass through the model
outputs = self.model(
input_ids=input_ids,
attention_mask=attention_mask,
protein_sequences=protein_sequences,
batch_idx_map=batch_idx_map,
structure_coords=structure_coords,
labels=labels,
go_aspects=go_aspects,
)
# Get the loss from model outputs
loss = outputs.loss
# Logging metrics
self.log(
f"{prefix}_loss_epoch",
loss,
on_step=False,
on_epoch=True,
prog_bar=True,
logger=True,
sync_dist=True,
)
# Rank-0 live per-step loss for progress bar without cross-GPU sync
if self.trainer.is_global_zero:
self.log(
f"{prefix}_loss",
loss.detach(),
on_step=True,
on_epoch=False,
prog_bar=True,
logger=True,
sync_dist=False,
)
self.log(
"lr",
self.lr_schedulers().get_last_lr()[0],
on_step=True,
on_epoch=True,
prog_bar=True,
logger=True,
sync_dist=False,
)
self.log(
"step",
self.global_step,
on_step=True,
on_epoch=False,
prog_bar=False,
logger=True,
sync_dist=False,
)
# Sample generation for debugging and monitoring
if self.enable_sample_generation and (
(prefix == "train" and (self.global_step % 5_000 == 0)) or (prefix == "val" and (batch_idx % 5_000 == 0))
):
self._log_sample_generation(
batch,
prefix,
batch_idx,
input_ids,
attention_mask,
labels,
protein_sequences,
structure_coords,
batch_idx_map,
go_aspects,
)
return loss
def _log_sample_generation(
self,
batch: Dict,
prefix: str,
batch_idx: int,
input_ids,
attention_mask,
labels,
protein_sequences,
structure_coords,
batch_idx_map,
go_aspects,
):
"""Generates, prints, and logs a single sample generation."""
example_idx = 0 # Select first example from batch
if self.verbose_sample_generation:
print(
f"\n=== Sample Generation {prefix} (step {self.global_step} / {self.trainer.estimated_stepping_batches}) ==="
)
if self.use_unsloth:
# Unsloth does not support model.generate() during training
return
result = generate_single_response(
model=self.model,
tokenizer=self.tokenizer,
input_ids=input_ids,
attention_mask=attention_mask,
labels=labels,
protein_sequences=protein_sequences,
structure_coords=structure_coords,
batch_idx_map=batch_idx_map,
go_aspects=go_aspects,
example_idx=example_idx,
max_new_tokens=64,
do_sample=False,
)
if result["success"]:
if self.verbose_sample_generation:
print(
f"=====[Sample {prefix} | Batch {batch_idx} | Example {example_idx} | Step {self.global_step}]====="
)
print(f"=====[User input]=====\n{result['user_input']}")
print(f"=====[Complete generation]=====\n{result['generation']}")
print(f"=====[Ground truth]=====\n{result['ground_truth']}")
# Log to wandb
timestamp = time.time()
step_id = f"gen_{self.global_step}-{timestamp}"
wandb_logger = self.logger.experiment
wandb_logger.log(
{
step_id: wandb.Table(
columns=[
"timestamp",
"prefix",
"batch_idx",
"user_input",
"generation",
"ground_truth",
],
data=[
[
timestamp,
prefix,
batch_idx,
result["user_input"],
result["generation"],
result["ground_truth"],
]
],
)
}
)
elif self.verbose_sample_generation:
print(f"=====[Generation failed for this example {example_idx}]=====")
def training_step(self, batch: Dict, batch_idx: int) -> torch.Tensor:
"""Perform a single training step."""
return self._step(batch, batch_idx, prefix="train")
def validation_step(self, batch: Dict, batch_idx: int) -> torch.Tensor:
"""Perform a single validation step."""
return self._step(batch, batch_idx, prefix="val")
def configure_optimizers(self):
"""
Configure optimizers and learning rate schedulers.
Returns:
Tuple[List, List]: A tuple containing a list of optimizers and schedulers
"""
# In Stage 1, we optimize the projector and GO components (if available)
if self.training_stage == 1:
# Collect all trainable parameters for Stage 1
trainable_params = list(self.model.protein_projection.parameters())
components = ["protein projector"]
# Add GO projection parameters if available
if hasattr(self.model, "go_projection") and self.model.go_projection is not None:
trainable_params.extend(list(self.model.go_projection.parameters()))
components.append("GO projection")
# Add GO encoder parameters if available and trainable
if (hasattr(self.model, "go_encoder") and self.model.go_encoder is not None and
self.go_model_finetune):
trainable_params.extend(list(self.model.go_encoder.parameters()))
components.append("GO encoder")
component_str = " + ".join(components)
print(f"Optimizer configured for Stage 1 ({component_str}) with LR: {self.learning_rate}")
optimizer = AdamW(trainable_params, lr=self.learning_rate, weight_decay=self.weight_decay)
else: # Stage 2 optimizes all trainable parameters (LoRA + projector)
trainable_params = self.parameters()
print(f"Optimizer configured for Stage 2 (full) with LR: {self.learning_rate}")
optimizer = AdamW(
trainable_params,
lr=self.learning_rate,
weight_decay=self.weight_decay,
)
total_steps = self.trainer.estimated_stepping_batches
warmup_steps = int(self.warmup_ratio * total_steps)
decay_steps = total_steps - warmup_steps
warmup = LinearLR(
optimizer,
start_factor=1e-8,
end_factor=1.0,
total_iters=warmup_steps,
)
decay = CosineAnnealingLR(
optimizer,
T_max=decay_steps,
eta_min=self.learning_rate * 0.1,
)
scheduler = SequentialLR(
optimizer=optimizer,
schedulers=[warmup, decay],
milestones=[warmup_steps],
)
return [optimizer], [{"scheduler": scheduler, "interval": "step"}]
def _create_dataloader(
self, dataset, split: str, shuffle: bool = False, return_answers: bool = False
) -> DataLoader:
"""Helper function to create dataloaders with common logic."""
if dataset is None or len(dataset) == 0:
print("No dataset provided. Creating empty dataloader")
return DataLoader([], batch_size=self.hparams.batch_size, shuffle=shuffle)
try:
print(f"Creating {split} dataloader: {len(dataset)} samples")
# Create processor
processor = PLProcessor(
tokenizer=self.model.text_tokenizer,
# protein_tokenizer=None, # ESM3 handles this internally
)
# Create collate function
collate_fn = partial(
qwen_protein_collate_fn,
processor=processor,
max_length_text=self.max_length_text,
max_length_protein=self.max_length_protein,
return_answer_in_batch=return_answers or self.return_answer_in_batch,
)
return DataLoader(
dataset,
batch_size=self.hparams.batch_size,
shuffle=shuffle,
collate_fn=collate_fn,
num_workers=self.hparams.num_workers,
persistent_workers=True,
pin_memory=True,
)
except Exception as e:
print(f"Failed to create dataloader: {e}")
return DataLoader([], batch_size=self.hparams.batch_size, shuffle=shuffle)
def train_dataloader(self) -> DataLoader:
"""Create and return the training DataLoader."""
return self._create_dataloader(self._train_dataset, split="train", shuffle=True)
def val_dataloader(self) -> DataLoader:
"""Create and return the validation DataLoader."""
return self._create_dataloader(self._val_dataset, split="val", shuffle=False)
def test_dataloader(self) -> DataLoader:
"""Create and return the test DataLoader."""
return self.val_dataloader()
def on_test_epoch_end(self):
"""Called at the end of test epoch."""
pass
def load_state_dict(self, state_dict, strict=True):
"""
Override load_state_dict to ignore missing/unexpected keys from protein model.
Since we don't care about protein model weights (frozen anyway), we can safely ignore these.
"""
# Filter out structure encoder keys that might not be present
filtered_state_dict = {}
unexpected_keys = []
for key, value in state_dict.items():
if "_structure_encoder" in key:
# Skip structure encoder keys - these are from ESM3 and we don't need them
unexpected_keys.append(key)
else:
filtered_state_dict[key] = value
if unexpected_keys:
print(f"⚠️ Ignoring {len(unexpected_keys)} unexpected structure encoder keys from checkpoint")
print(" These keys are from ESM3's structure encoder which may not be initialized in current model")
for key in sorted(unexpected_keys)[:5]: # Show first 5 sorted
print(f" - {key}")
# Call parent's load_state_dict with strict=False to handle any other mismatches gracefully
result = super().load_state_dict(filtered_state_dict, strict=False)
# Log any other unexpected issues
if result.missing_keys:
print(f"⚠️ Missing keys in checkpoint: {len(result.missing_keys)} keys")
if result.unexpected_keys:
print(f"⚠️ Other unexpected keys (non-structure encoder): {len(result.unexpected_keys)} keys")
print(f"Missing keys:\n{result.missing_keys}")
print(f"Unexpected keys:\n{result.unexpected_keys}")
return result
def main(args: ArgumentParser):
"""
Main function to run the Protein-Text fine-tuning process.
Args:
args (ArgumentParser): Parsed command-line arguments
"""
# Set random seed and environment variables
pl.seed_everything(args.seed)
torch.cuda.empty_cache()
torch.set_float32_matmul_precision("medium")
# Load and split datasets
print("Loading and splitting datasets...")
if args.dataset_type == "cafa5":
# Handle multiple dataset names (comma-separated or single)
dataset_names = [name.strip() for name in args.cafa5_dataset_name.split(",")]
# Parse dataset weights if provided
dataset_weights = None
if args.cafa5_dataset_weights:
try:
dataset_weights = [int(w.strip()) for w in args.cafa5_dataset_weights.split(",")]
if len(dataset_weights) != len(dataset_names):
raise ValueError(
f"Number of weights ({len(dataset_weights)}) must match number of datasets ({len(dataset_names)})"
)
print(f"Using dataset weights: {dict(zip(dataset_names, dataset_weights))}")
except ValueError as e:
print(f"Error parsing dataset weights: {e}")
print("Using equal weights for all datasets")
dataset_weights = None
if dataset_weights is None:
dataset_weights = [1] * len(dataset_names)
print(f"Loading {len(dataset_names)} CAFA5 dataset(s): {dataset_names}")
print(f"Dataset weights: {dataset_weights}")
all_train_datasets = []
all_val_datasets = []
all_test_datasets = []
for i, dataset_name in enumerate(dataset_names):
weight = dataset_weights[i]
print(f"Loading dataset: {dataset_name} (weight: {weight}x)")
train_ds, val_ds, test_ds = load_cafa5_dataset(
dataset=args.cafa5_dataset,
dataset_name=dataset_name,
cache_dir=args.dataset_cache_dir,
dataset_subset=args.cafa5_dataset_subset,
max_length=args.max_length_protein,
seed=args.seed,
val_split_ratio=args.val_split_ratio,
return_as_chat_template=True,
structure_dir=args.structure_dir,
debug=args.debug,
include_go_defs=args.include_go_defs,
interpro_dataset_name=args.interpro_dataset_name,
split_go_aspects=args.split_go_aspects,
interpro_in_prompt=args.interpro_in_prompt,
ppi_in_prompt=args.ppi_in_prompt,
predict_interpro=args.predict_interpro,
include_protein_function_summary=args.include_protein_function_summary,
reasoning_dataset_name=args.reasoning_dataset_name,
include_ground_truth_in_final_answer=args.include_ground_truth_in_final_answer,
add_uniprot_summary=args.add_uniprot_summary,
is_swissprot=args.is_swissprot,
min_go_mf_freq=args.min_go_mf_freq,
min_go_bp_freq=args.min_go_bp_freq,
min_go_cc_freq=args.min_go_cc_freq,
apply_go_filtering_to_val_test=args.apply_go_filtering_to_val_test,
go_gpt_predictions_column=args.go_gpt_predictions_column,
)
print(f" - Original sizes - Train: {len(train_ds)}, Val: {len(val_ds)}, Test: {len(test_ds)} samples")
# Repeat datasets according to their weights
for repeat_idx in range(weight):
all_train_datasets.append(train_ds)
all_val_datasets.append(val_ds)
all_test_datasets.append(test_ds)
print(
f" - After weighting ({weight}x) - Train: {len(train_ds) * weight}, Val: {len(val_ds) * weight}, Test: {len(test_ds) * weight} effective samples"
)
# Fix 'length' field type mismatch before concatenation
def fix_length_type(dataset):
"""Convert 'length' field from float64 to int64."""
if "length" in dataset.features:
return dataset.cast_column("length", Value("int64"))
return dataset
# Apply the fix to all datasets
all_train_datasets = [fix_length_type(ds) for ds in all_train_datasets]
all_val_datasets = [fix_length_type(ds) for ds in all_val_datasets]
all_test_datasets = [fix_length_type(ds) for ds in all_test_datasets]
# Concatenate all datasets using HuggingFace datasets concatenate_datasets
train_dataset = (
concatenate_datasets(all_train_datasets) if len(all_train_datasets) > 1 else all_train_datasets[0]
)
val_dataset = concatenate_datasets(all_val_datasets) if len(all_val_datasets) > 1 else all_val_datasets[0]
test_dataset = concatenate_datasets(all_test_datasets) if len(all_test_datasets) > 1 else all_test_datasets[0]
# Re-shuffle the concatenated datasets to properly mix samples from different sources
if len(dataset_names) > 1:
print("Re-shuffling concatenated datasets to properly mix samples from different sources...")
train_dataset = train_dataset.shuffle(seed=args.seed)
val_dataset = val_dataset.shuffle(seed=args.seed)
test_dataset = test_dataset.shuffle(seed=args.seed)
print(
f"Mixed dataset totals - Train: {len(train_dataset)}, Val: {len(val_dataset)}, Test: {len(test_dataset)} samples"
)
# Setup directories
os.makedirs(args.checkpoint_dir, exist_ok=True)
if args.run_name:
run_name = args.run_name
else:
run_name = f"{args.wandb_project}-{args.dataset_type}-{args.text_model_name.split('/')[-1]}"
# Initialize model with pre-loaded datasets
model = ProteinLLMFineTuner(
args,
train_dataset=train_dataset,
val_dataset=val_dataset,
test_dataset=test_dataset,
)
# Setup callbacks
callbacks = [
LearningRateMonitor(logging_interval="step"),
]
# Only enable model checkpointing for Stage 2, as we only need the projector weights from Stage 1
if args.training_stage == 2:
# 1) Keep the single best by lowest val_loss_epoch (saved at validation end)
best_val_ckpt = ModelCheckpoint(
dirpath=args.checkpoint_dir,
filename=f"{run_name}-best-epoch={{epoch:02d}}-val={{val_loss_epoch:.4f}}",
monitor="val_loss_epoch",
mode="min",
save_top_k=1,
save_last=False,
save_on_train_epoch_end=False,
verbose=True,
)
# 2) Keep the most recent, saved every N training steps
recent_ckpts = ModelCheckpoint(
dirpath=args.checkpoint_dir,
filename=f"{run_name}-recent-epoch={{epoch:02d}}-step={{step:06d}}",
save_top_k=args.save_top_k,
monitor="step",
mode="max",
save_last=True,
every_n_train_steps=args.every_n_train_steps,
save_on_train_epoch_end=False,
save_weights_only=False,
verbose=True,
)
# 3) Save every epoch from checkpoint_start_epoch onwards
epoch_ckpt = EpochCheckpointFromN(
checkpoint_dir=args.checkpoint_dir,
run_name=run_name,
start_epoch=args.checkpoint_start_epoch,
)
callbacks.extend([recent_ckpts, best_val_ckpt, epoch_ckpt])
# Setup logger
is_resuming = args.ckpt_path is not None
logger = WandbLogger(
project=args.wandb_project,
entity=args.wandb_entity,
save_dir=args.log_dir,
name=run_name,
resume="allow" if is_resuming else None, # Allow resuming existing run
log_model=False,
)
# Configure Lightning AdvancedProfiler (simple and robust)
profiler = None
if args.enable_profiler:
os.makedirs(args.profiler_dir, exist_ok=True)
profiler = AdvancedProfiler(dirpath=args.profiler_dir, filename=args.profiler_filename)
# Optionally add device stats monitor
if args.enable_device_stats_monitor:
callbacks.append(DeviceStatsMonitor(cpu_stats=args.device_stats_cpu))
# Initialize the PyTorch Lightning Trainer
trainer = pl.Trainer(
max_epochs=args.max_epochs,
max_steps=(args.max_steps if (args.max_steps is not None and args.max_steps > 0) else -1),
accelerator="gpu",
devices=args.num_gpus,
strategy=args.strategy,
precision="bf16-mixed",
callbacks=callbacks,
logger=logger,
accumulate_grad_batches=args.gradient_accumulation_steps,
gradient_clip_val=1.0,
val_check_interval=args.val_check_interval,
num_nodes=args.num_nodes,
profiler=profiler,
limit_train_batches=args.limit_train_batches,
limit_val_batches=args.limit_val_batches,
log_every_n_steps=args.log_every_n_steps,
num_sanity_val_steps=args.num_sanity_val_steps,
enable_model_summary=True,
enable_progress_bar=True,
sync_batchnorm=False,
)
# Start the training process
trainer.fit(model, ckpt_path=args.ckpt_path)
# After stage 1, save the projector weights
if args.training_stage == 1 and trainer.global_rank == 0:
projector_weights_path = os.path.join(args.checkpoint_dir, "projector_weights.pt")
print(f"Stage 1 finished. Saving projector weights to {projector_weights_path}")
torch.save(model.model.protein_projection.state_dict(), projector_weights_path)
print("✓ Projector weights saved.")
# Also save GO projection weights if available
if hasattr(model.model, "go_projection") and model.model.go_projection is not None:
go_projection_weights_path = os.path.join(args.checkpoint_dir, "go_projection_weights.pt")
print(f"Saving GO projection weights to {go_projection_weights_path}")
torch.save(model.model.go_projection.state_dict(), go_projection_weights_path)
print("✓ GO projection weights saved.")
# Also save GO encoder weights if available
if hasattr(model.model, "go_encoder") and model.model.go_encoder is not None:
go_encoder_weights_path = os.path.join(args.checkpoint_dir, "go_encoder_weights.pt")
print(f"Saving GO encoder weights to {go_encoder_weights_path}")
torch.save(model.model.go_encoder.state_dict(), go_encoder_weights_path)
print("✓ GO encoder weights saved.")
# trainer.test(model, ckpt_path=args.ckpt_path if args.ckpt_path else "best")
if __name__ == "__main__":
parser = ArgumentParser()
# Add command-line arguments
parser.add_argument("--seed", type=int, default=23)
parser.add_argument(
"--checkpoint_dir",
type=str,
default="checkpoints",
help="Directory to save model checkpoints.",
)
parser.add_argument("--log_dir", type=str, default="logs", help="Directory to save logs.")
parser.add_argument(
"--cache_dir",
type=str,
default=os.path.expanduser("~/.cache/huggingface/hub"),
help="Directory for HuggingFace model cache.",
)
parser.add_argument("--text_model_name", type=str, default="Qwen/Qwen3-1.7B")
parser.add_argument(
"--protein_model_name",
type=str,
default="esm3_sm_open_v1",
choices=["esm3_sm_open_v1", "esmc_300m", "esmc_600m"],
help="Protein model name. Supported models: ESM3 (esm3_sm_open_v1) and ESM-C (esmc_300m, esmc_600m).",
)
parser.add_argument("--model_type", type=str, default="protein-llm")
parser.add_argument("--batch_size", type=int, default=1)
parser.add_argument("--max_epochs", type=int, default=3)
parser.add_argument("--learning_rate", type=float, default=1e-4)
parser.add_argument("--weight_decay", type=float, default=0.01)
parser.add_argument("--num_workers", type=int, default=4)
parser.add_argument("--num_gpus", type=int, default=1)
parser.add_argument("--num_nodes", type=int, default=1)
parser.add_argument("--gradient_accumulation_steps", type=int, default=8)
parser.add_argument("--ckpt_path", type=str, default=None)
parser.add_argument("--max_length_protein", type=int, default=2000)
parser.add_argument("--max_length_text", type=int, default=4000)
parser.add_argument("--max_assistant_reasoning_length", type=int, default=4000)
parser.add_argument("--text_model_finetune", type=str2bool, default=True)
parser.add_argument("--protein_model_finetune", type=str2bool, default=False)
parser.add_argument(
"--protein_train_layer_start",
type=int,
default=36,