File size: 3,151 Bytes
32c143f
 
 
 
 
 
 
 
4887b09
 
 
32c143f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4887b09
cdc040c
4887b09
 
cdc040c
 
32c143f
4887b09
32c143f
cdc040c
32c143f
 
db5c7bf
 
32c143f
 
 
 
 
 
4887b09
 
 
 
 
 
 
 
 
 
 
 
32c143f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import subprocess
import argparse

def main():
    parser = argparse.ArgumentParser(description="Local training launcher for Taxon-aware ESM2")
    parser.add_argument("--dry_run", action="store_true", help="Run a quick dry run to verify pipeline")
    parser.add_argument("--resume", action="store_true", help="Attempt to resume from latest_model.pth")
    parser.add_argument("--skip_eval", action="store_true", help="Skip GPU evaluation")
    parser.add_argument("--epochs", type=int, default=20, help="Number of epochs to train")
    
    args = parser.parse_args()

    # Define paths
    ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
    SRC_DIR = os.path.join(ROOT_DIR, "src")
    DATASET_DIR = os.path.join(ROOT_DIR, "dataset")

    # Verify input directories
    if not os.path.exists(SRC_DIR):
        print(f"Error: Source directory not found at {SRC_DIR}")
        return
    if not os.path.exists(DATASET_DIR):
        print(f"Error: Dataset directory not found at {DATASET_DIR}")
        return

    print(f"Root Dir: {ROOT_DIR}")
    print(f"Src Dir:  {SRC_DIR}")
    print(f"Data Dir: {DATASET_DIR}")

    # Construct the command
    # We run from SRC_DIR to match Azure ML behavior and allow relative imports
    cmd = [
        sys.executable, "train.py",
        "--data_path", DATASET_DIR,
        "--epochs", str(args.epochs),
        "--batch_size", "32",
        "--lr", "1e-4",
        "--min_lr", "1e-5",
        "--num_workers", "10",  # 0 for local windows debugging usually safer
        "--esm_model_name", "facebook/esm2_t33_650M_UR50D",
        "--use_lora", "True",
        "--lora_rank", "512",
        # Asymmetric Loss defaults
        "--gamma_neg", "4",
        "--gamma_pos", "0",
        "--clip", "0.05",
        "--max_grad_norm", "1.0",
        
        
        # Absolute locations for output
        "--output_dir", os.path.join(ROOT_DIR, "outputs"),
        "--mlflow_dir", os.path.join(ROOT_DIR, "mlruns")
    ]
    
    # Auto-Resume Logic
    if args.resume:
        checkpoint_path = os.path.join(ROOT_DIR, "outputs", "latest_model.pth")
        if os.path.exists(checkpoint_path):
            print(f"Auto-resume: Found checkpoint at {checkpoint_path}")
            cmd.extend(["--resume_checkpoint", checkpoint_path])
        else:
            print(f"Warning: --resume flag set but no checkpoint found at {checkpoint_path}. Starting fresh.")
    
    if args.skip_eval:
        cmd.append("--skip_eval")

    if args.dry_run:
        cmd.append("--dry_run")

    print(f"Running command: {' '.join(cmd)}")
    print("-" * 50)

    # Run the training script
    try:
        # cwd=SRC_DIR is crucial for relative imports
        subprocess.run(cmd, cwd=SRC_DIR, check=True)
    except subprocess.CalledProcessError as e:
        print(f"Training failed with error code {e.returncode}")
    except KeyboardInterrupt:
        print("\nTraining interrupted by user.")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

if __name__ == "__main__":
    main()