ta-ESM2 / local_train.py
Smilesjs's picture
Upload folder using huggingface_hub
4887b09 verified
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()