-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain.py
More file actions
151 lines (120 loc) · 4.65 KB
/
train.py
File metadata and controls
151 lines (120 loc) · 4.65 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
"""
Entry point for cluster-based continual pre-training.
Single GPU:
python train.py --config configs/default.yaml model.path=gpt2 data.train_dir=data/train data.dev_dir=data/dev
Multi-GPU (torchrun / DDP):
torchrun --nproc_per_node=4 train.py --config configs/default.yaml model.path=gpt2 ...
DeepSpeed ZeRO-3:
deepspeed --num_gpus=4 train.py --config configs/default.yaml deepspeed.enabled=true
DeepSpeed ZeRO-3 + CPU Offload:
deepspeed --num_gpus=4 train.py --config configs/default.yaml deepspeed.enabled=true deepspeed.config_file=configs/ds_zero3_offload.json
CLI overrides use OmegaConf dot-list syntax (key=value pairs appended after --config):
python train.py --config configs/default.yaml training.lr=1e-5 clustering.method=random pmp.window_size=10
"""
from __future__ import annotations
import argparse
import datetime
import logging
import os
import sys
import torch
import torch.distributed as dist
# Allow running from project root without installing as a package
sys.path.insert(0, os.path.dirname(__file__))
from utils.config import load_config
from trainer.integrated_trainer import IntegratedClusterTrainer
def _setup_logging(rank: int = 0):
level = logging.INFO if rank == 0 else logging.WARNING
logging.basicConfig(
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=level,
handlers=[logging.StreamHandler(sys.stdout)],
)
def _init_distributed(use_deepspeed: bool = False) -> tuple[int, int]:
"""
Initialise distributed process group.
When use_deepspeed=True, DeepSpeed handles init_process_group internally,
so we only read LOCAL_RANK from the environment (set by `deepspeed` launcher).
Returns (rank, world_size).
"""
if use_deepspeed:
import deepspeed
deepspeed.init_distributed(dist_backend="nccl")
local_rank = int(os.environ.get("LOCAL_RANK", 0))
torch.cuda.set_device(local_rank)
rank = dist.get_rank()
world_size = dist.get_world_size()
return rank, world_size
local_rank = int(os.environ.get("LOCAL_RANK", -1))
if local_rank == -1:
# Not distributed
return 0, 1
dist.init_process_group(backend="nccl", init_method="env://",
timeout=datetime.timedelta(minutes=60))
rank = dist.get_rank()
world_size = dist.get_world_size()
torch.cuda.set_device(local_rank)
return rank, world_size
def parse_args():
parser = argparse.ArgumentParser(
description="Cluster-based continual pre-training with PMP data selection",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__doc__,
)
parser.add_argument(
"--config",
required=True,
metavar="PATH",
help="Path to YAML config file (e.g. configs/default.yaml)",
)
# DeepSpeed adds its own CLI args (--local_rank, --deepspeed, etc.)
# We use parse_known_args to avoid conflicts.
parser.add_argument(
"--local_rank",
type=int,
default=-1,
help="Local rank passed by deepspeed launcher (do not set manually)",
)
parser.add_argument(
"overrides",
nargs="*",
metavar="KEY=VALUE",
help="OmegaConf dot-list overrides (e.g. training.lr=1e-5 model.path=gpt2)",
)
return parser.parse_args()
def main():
args = parse_args()
# --- Load config (need it early to check deepspeed.enabled) ---
cfg = load_config(args.config, overrides=args.overrides)
# --- Check if DeepSpeed is enabled ---
use_deepspeed = getattr(getattr(cfg, "deepspeed", None), "enabled", False)
# --- Distributed init ---
rank, world_size = _init_distributed(use_deepspeed=use_deepspeed)
_setup_logging(rank)
logger = logging.getLogger(__name__)
logger.info(f"Rank {rank}/{world_size} initialised (deepspeed={use_deepspeed})")
# Basic validation
if not cfg.model.path:
raise ValueError(
"model.path is required. Set it in the YAML or pass model.path=<name_or_path> as override."
)
if not cfg.data.train_dir:
raise ValueError(
"data.train_dir is required. Pass data.train_dir=<path> as override."
)
if not cfg.data.dev_dir:
raise ValueError(
"data.dev_dir is required. Pass data.dev_dir=<path> as override."
)
if rank == 0:
from omegaconf import OmegaConf
logger.info(f"Config:\n{OmegaConf.to_yaml(cfg)}")
# --- Run ---
trainer = IntegratedClusterTrainer(cfg)
trainer.train()
# --- Cleanup ---
if dist.is_initialized():
dist.destroy_process_group()
if __name__ == "__main__":
main()