Add local training logger that writes to {save_dir}/{save_weight}.log
Browse files- src/trainers/lm/agent.py +2 -1
- src/trainers/lm/distillation.py +1 -0
- src/trainers/lm/dpo.py +1 -0
- src/trainers/lm/full_sft.py +1 -0
- src/trainers/lm/grpo.py +2 -1
- src/trainers/lm/lora.py +1 -0
- src/trainers/lm/ppo.py +2 -1
- src/trainers/lm/pretrain.py +1 -0
- src/trainers/vam/full_sft.py +1 -0
- src/trainers/vlm/full_sft.py +1 -0
- src/trainers/vlm/pretrain.py +1 -0
- src/utils/training.py +19 -2
src/trainers/lm/agent.py
CHANGED
|
@@ -21,7 +21,7 @@ from torch.optim.lr_scheduler import CosineAnnealingLR
|
|
| 21 |
from transformers import AutoTokenizer
|
| 22 |
from models import LMConfig, LMForCausalLM
|
| 23 |
from dataset import AgentRLDataset
|
| 24 |
-
from utils.training import Logger, is_main_process, lm_checkpoint, init_distributed_mode, setup_seed, SkipBatchSampler, init_model, LMForRewardModel
|
| 25 |
from utils.training import apply_config # noqa: F401
|
| 26 |
from trainers.lm.rollout_engine import create_rollout_engine, compute_per_token_logps
|
| 27 |
|
|
@@ -416,6 +416,7 @@ if __name__ == "__main__":
|
|
| 416 |
setup_seed(42 + (dist.get_rank() if dist.is_initialized() else 0))
|
| 417 |
|
| 418 |
os.makedirs(args.save_dir, exist_ok=True)
|
|
|
|
| 419 |
lm_config = LMConfig(hidden_size=args.hidden_size, num_hidden_layers=args.num_hidden_layers,
|
| 420 |
max_seq_len=args.max_seq_len + args.max_gen_len, use_moe=bool(args.use_moe))
|
| 421 |
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume == 1 else None
|
|
|
|
| 21 |
from transformers import AutoTokenizer
|
| 22 |
from models import LMConfig, LMForCausalLM
|
| 23 |
from dataset import AgentRLDataset
|
| 24 |
+
from utils.training import init_logger, Logger, is_main_process, lm_checkpoint, init_distributed_mode, setup_seed, SkipBatchSampler, init_model, LMForRewardModel
|
| 25 |
from utils.training import apply_config # noqa: F401
|
| 26 |
from trainers.lm.rollout_engine import create_rollout_engine, compute_per_token_logps
|
| 27 |
|
|
|
|
| 416 |
setup_seed(42 + (dist.get_rank() if dist.is_initialized() else 0))
|
| 417 |
|
| 418 |
os.makedirs(args.save_dir, exist_ok=True)
|
| 419 |
+
init_logger(args.save_dir, getattr(args, "save_weight", "train"))
|
| 420 |
lm_config = LMConfig(hidden_size=args.hidden_size, num_hidden_layers=args.num_hidden_layers,
|
| 421 |
max_seq_len=args.max_seq_len + args.max_gen_len, use_moe=bool(args.use_moe))
|
| 422 |
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume == 1 else None
|
src/trainers/lm/distillation.py
CHANGED
|
@@ -182,6 +182,7 @@ if __name__ == "__main__":
|
|
| 182 |
|
| 183 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 184 |
os.makedirs(args.save_dir, exist_ok=True)
|
|
|
|
| 185 |
lm_config_student = LMConfig(hidden_size=args.student_hidden_size, num_hidden_layers=args.student_num_layers, use_moe=bool(args.student_use_moe))
|
| 186 |
lm_config_teacher = LMConfig(hidden_size=args.teacher_hidden_size, num_hidden_layers=args.teacher_num_layers, use_moe=bool(args.teacher_use_moe))
|
| 187 |
ckp_data = lm_checkpoint(lm_config_student, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
|
|
|
| 182 |
|
| 183 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 184 |
os.makedirs(args.save_dir, exist_ok=True)
|
| 185 |
+
init_logger(args.save_dir, getattr(args, "save_weight", "train"))
|
| 186 |
lm_config_student = LMConfig(hidden_size=args.student_hidden_size, num_hidden_layers=args.student_num_layers, use_moe=bool(args.student_use_moe))
|
| 187 |
lm_config_teacher = LMConfig(hidden_size=args.teacher_hidden_size, num_hidden_layers=args.teacher_num_layers, use_moe=bool(args.teacher_use_moe))
|
| 188 |
ckp_data = lm_checkpoint(lm_config_student, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
src/trainers/lm/dpo.py
CHANGED
|
@@ -161,6 +161,7 @@ if __name__ == "__main__":
|
|
| 161 |
|
| 162 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 163 |
os.makedirs(args.save_dir, exist_ok=True)
|
|
|
|
| 164 |
lm_config = LMConfig(**vars(args))
|
| 165 |
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
| 166 |
|
|
|
|
| 161 |
|
| 162 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 163 |
os.makedirs(args.save_dir, exist_ok=True)
|
| 164 |
+
init_logger(args.save_dir, getattr(args, "save_weight", "train"))
|
| 165 |
lm_config = LMConfig(**vars(args))
|
| 166 |
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
| 167 |
|
src/trainers/lm/full_sft.py
CHANGED
|
@@ -113,6 +113,7 @@ def main(default_config=None):
|
|
| 113 |
|
| 114 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 115 |
os.makedirs(args.save_dir, exist_ok=True)
|
|
|
|
| 116 |
lm_config = LMConfig(**vars(args))
|
| 117 |
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
| 118 |
|
|
|
|
| 113 |
|
| 114 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 115 |
os.makedirs(args.save_dir, exist_ok=True)
|
| 116 |
+
init_logger(args.save_dir, getattr(args, "save_weight", "train"))
|
| 117 |
lm_config = LMConfig(**vars(args))
|
| 118 |
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
| 119 |
|
src/trainers/lm/grpo.py
CHANGED
|
@@ -19,7 +19,7 @@ from torch.optim.lr_scheduler import CosineAnnealingLR
|
|
| 19 |
from transformers import AutoModel
|
| 20 |
from models import LMConfig, LMForCausalLM
|
| 21 |
from dataset import RLAIFDataset
|
| 22 |
-
from utils.training import Logger, is_main_process, lm_checkpoint, init_distributed_mode, setup_seed, SkipBatchSampler, init_model, LMForRewardModel
|
| 23 |
from utils.training import apply_config # noqa: F401
|
| 24 |
from trainers.lm.rollout_engine import create_rollout_engine
|
| 25 |
|
|
@@ -249,6 +249,7 @@ if __name__ == "__main__":
|
|
| 249 |
|
| 250 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 251 |
os.makedirs(args.save_dir, exist_ok=True)
|
|
|
|
| 252 |
lm_config = LMConfig(hidden_size=args.hidden_size, num_hidden_layers=args.num_hidden_layers,
|
| 253 |
max_seq_len=args.max_seq_len + args.max_gen_len, use_moe=bool(args.use_moe))
|
| 254 |
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
|
|
|
| 19 |
from transformers import AutoModel
|
| 20 |
from models import LMConfig, LMForCausalLM
|
| 21 |
from dataset import RLAIFDataset
|
| 22 |
+
from utils.training import init_logger, Logger, is_main_process, lm_checkpoint, init_distributed_mode, setup_seed, SkipBatchSampler, init_model, LMForRewardModel
|
| 23 |
from utils.training import apply_config # noqa: F401
|
| 24 |
from trainers.lm.rollout_engine import create_rollout_engine
|
| 25 |
|
|
|
|
| 249 |
|
| 250 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 251 |
os.makedirs(args.save_dir, exist_ok=True)
|
| 252 |
+
init_logger(args.save_dir, getattr(args, "save_weight", "train"))
|
| 253 |
lm_config = LMConfig(hidden_size=args.hidden_size, num_hidden_layers=args.num_hidden_layers,
|
| 254 |
max_seq_len=args.max_seq_len + args.max_gen_len, use_moe=bool(args.use_moe))
|
| 255 |
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
src/trainers/lm/lora.py
CHANGED
|
@@ -107,6 +107,7 @@ if __name__ == "__main__":
|
|
| 107 |
|
| 108 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 109 |
os.makedirs(args.save_dir, exist_ok=True)
|
|
|
|
| 110 |
lm_config = LMConfig(**vars(args))
|
| 111 |
ckp_data = lm_checkpoint(lm_config, weight=args.lora_name, save_dir='../checkpoints') if args.from_resume==1 else None
|
| 112 |
|
|
|
|
| 107 |
|
| 108 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 109 |
os.makedirs(args.save_dir, exist_ok=True)
|
| 110 |
+
init_logger(args.save_dir, getattr(args, "save_weight", "train"))
|
| 111 |
lm_config = LMConfig(**vars(args))
|
| 112 |
ckp_data = lm_checkpoint(lm_config, weight=args.lora_name, save_dir='../checkpoints') if args.from_resume==1 else None
|
| 113 |
|
src/trainers/lm/ppo.py
CHANGED
|
@@ -18,7 +18,7 @@ from torch.nn.utils import clip_grad_norm_
|
|
| 18 |
from torch.optim.lr_scheduler import CosineAnnealingLR
|
| 19 |
from models import LMConfig, LMForCausalLM
|
| 20 |
from dataset import RLAIFDataset
|
| 21 |
-
from utils.training import Logger, is_main_process, lm_checkpoint, init_distributed_mode, setup_seed, SkipBatchSampler, init_model, LMForRewardModel
|
| 22 |
from utils.training import apply_config # noqa: F401
|
| 23 |
from trainers.lm.rollout_engine import create_rollout_engine
|
| 24 |
|
|
@@ -344,6 +344,7 @@ if __name__ == "__main__":
|
|
| 344 |
|
| 345 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 346 |
os.makedirs(args.save_dir, exist_ok=True)
|
|
|
|
| 347 |
lm_config = LMConfig(**vars(args))
|
| 348 |
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
| 349 |
|
|
|
|
| 18 |
from torch.optim.lr_scheduler import CosineAnnealingLR
|
| 19 |
from models import LMConfig, LMForCausalLM
|
| 20 |
from dataset import RLAIFDataset
|
| 21 |
+
from utils.training import init_logger, Logger, is_main_process, lm_checkpoint, init_distributed_mode, setup_seed, SkipBatchSampler, init_model, LMForRewardModel
|
| 22 |
from utils.training import apply_config # noqa: F401
|
| 23 |
from trainers.lm.rollout_engine import create_rollout_engine
|
| 24 |
|
|
|
|
| 344 |
|
| 345 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 346 |
os.makedirs(args.save_dir, exist_ok=True)
|
| 347 |
+
init_logger(args.save_dir, getattr(args, "save_weight", "train"))
|
| 348 |
lm_config = LMConfig(**vars(args))
|
| 349 |
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
| 350 |
|
src/trainers/lm/pretrain.py
CHANGED
|
@@ -112,6 +112,7 @@ if __name__ == "__main__":
|
|
| 112 |
|
| 113 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 114 |
os.makedirs(args.save_dir, exist_ok=True)
|
|
|
|
| 115 |
lm_config = LMConfig(**vars(args))
|
| 116 |
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
| 117 |
|
|
|
|
| 112 |
|
| 113 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 114 |
os.makedirs(args.save_dir, exist_ok=True)
|
| 115 |
+
init_logger(args.save_dir, getattr(args, "save_weight", "train"))
|
| 116 |
lm_config = LMConfig(**vars(args))
|
| 117 |
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
| 118 |
|
src/trainers/vam/full_sft.py
CHANGED
|
@@ -170,6 +170,7 @@ def main(default_config=None):
|
|
| 170 |
|
| 171 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 172 |
os.makedirs(args.save_dir, exist_ok=True)
|
|
|
|
| 173 |
omni_config = VAMConfig(**vars(args))
|
| 174 |
ckp_data = omni_checkpoint(omni_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
| 175 |
|
|
|
|
| 170 |
|
| 171 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 172 |
os.makedirs(args.save_dir, exist_ok=True)
|
| 173 |
+
init_logger(args.save_dir, getattr(args, "save_weight", "train"))
|
| 174 |
omni_config = VAMConfig(**vars(args))
|
| 175 |
ckp_data = omni_checkpoint(omni_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
| 176 |
|
src/trainers/vlm/full_sft.py
CHANGED
|
@@ -117,6 +117,7 @@ def main(default_config=None):
|
|
| 117 |
|
| 118 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 119 |
os.makedirs(args.save_dir, exist_ok=True)
|
|
|
|
| 120 |
vlm_config = VLMConfig(**vars(args))
|
| 121 |
ckp_data = vlm_checkpoint(vlm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
| 122 |
|
|
|
|
| 117 |
|
| 118 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 119 |
os.makedirs(args.save_dir, exist_ok=True)
|
| 120 |
+
init_logger(args.save_dir, getattr(args, "save_weight", "train"))
|
| 121 |
vlm_config = VLMConfig(**vars(args))
|
| 122 |
ckp_data = vlm_checkpoint(vlm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
| 123 |
|
src/trainers/vlm/pretrain.py
CHANGED
|
@@ -117,6 +117,7 @@ if __name__ == "__main__":
|
|
| 117 |
|
| 118 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 119 |
os.makedirs(args.save_dir, exist_ok=True)
|
|
|
|
| 120 |
vlm_config = VLMConfig(**vars(args))
|
| 121 |
ckp_data = vlm_checkpoint(vlm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
| 122 |
|
|
|
|
| 117 |
|
| 118 |
# ========== 2. 配置目录、模型参数、检查ckp ==========
|
| 119 |
os.makedirs(args.save_dir, exist_ok=True)
|
| 120 |
+
init_logger(args.save_dir, getattr(args, "save_weight", "train"))
|
| 121 |
vlm_config = VLMConfig(**vars(args))
|
| 122 |
ckp_data = vlm_checkpoint(vlm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
| 123 |
|
src/utils/training.py
CHANGED
|
@@ -57,9 +57,26 @@ def is_main_process():
|
|
| 57 |
return not dist.is_initialized() or dist.get_rank() == 0
|
| 58 |
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
def Logger(content):
|
| 61 |
-
if is_main_process():
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
|
| 65 |
def get_lr(current_step, total_steps, lr):
|
|
|
|
| 57 |
return not dist.is_initialized() or dist.get_rank() == 0
|
| 58 |
|
| 59 |
|
| 60 |
+
_log_file = None
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def init_logger(save_dir='../checkpoint', name='train'):
|
| 64 |
+
global _log_file
|
| 65 |
+
if not is_main_process():
|
| 66 |
+
return
|
| 67 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 68 |
+
log_path = os.path.join(save_dir, f'{name}.log')
|
| 69 |
+
_log_file = open(log_path, 'a', encoding='utf-8')
|
| 70 |
+
Logger(f'日志写入: {os.path.abspath(log_path)}')
|
| 71 |
+
|
| 72 |
+
|
| 73 |
def Logger(content):
|
| 74 |
+
if not is_main_process():
|
| 75 |
+
return
|
| 76 |
+
print(content)
|
| 77 |
+
if _log_file is not None:
|
| 78 |
+
_log_file.write(str(content) + '\n')
|
| 79 |
+
_log_file.flush()
|
| 80 |
|
| 81 |
|
| 82 |
def get_lr(current_step, total_steps, lr):
|