|
|
@@ -17,7 +17,7 @@ from torch.utils.data import DataLoader, DistributedSampler
|
|
|
from model.model_minimind import MiniMindConfig
|
|
|
from dataset.lm_dataset import SFTDataset
|
|
|
from model.model_lora import save_lora, apply_lora
|
|
|
-from trainer.trainer_utils import get_lr, Logger, is_main_process, lm_checkpoint, init_distributed_mode, setup_seed, init_model, SkipBatchSampler
|
|
|
+from trainer.trainer_utils import get_lr, Logger, is_main_process, lm_checkpoint, init_distributed_mode, setup_seed, init_model, SkipBatchSampler, resolve_dtype
|
|
|
|
|
|
warnings.filterwarnings('ignore')
|
|
|
|
|
|
@@ -78,7 +78,7 @@ if __name__ == "__main__":
|
|
|
parser.add_argument("--warmup_ratio", type=float, default=0.0, help="线性warmup占总步数比例,0为关闭")
|
|
|
parser.add_argument("--device", type=str, default="cuda:0" if torch.cuda.is_available() else "cpu", help="训练设备")
|
|
|
parser.add_argument("--dtype", type=str, default="bfloat16", help="混合精度类型")
|
|
|
- parser.add_argument("--num_workers", type=int, default=8, help="数据加载线程数")
|
|
|
+ parser.add_argument("--num_workers", type=int, default=0 if os.name == 'nt' else 8, help="数据加载线程数")
|
|
|
parser.add_argument("--accumulation_steps", type=int, default=1, help="梯度累积步数")
|
|
|
parser.add_argument("--grad_clip", type=float, default=1.0, help="梯度裁剪阈值")
|
|
|
parser.add_argument("--log_interval", type=int, default=10, help="日志打印间隔")
|
|
|
@@ -106,7 +106,8 @@ if __name__ == "__main__":
|
|
|
ckp_data = lm_checkpoint(lm_config, weight=args.lora_name, save_dir='../checkpoints') if args.from_resume==1 else None
|
|
|
|
|
|
# ========== 3. 设置混合精度 ==========
|
|
|
- device_type = "cuda" if "cuda" in args.device else "cpu"
|
|
|
+ device_type = "cuda" if torch.device(args.device).type == "cuda" else "cpu"
|
|
|
+ args.dtype = resolve_dtype(args.dtype, args.device)
|
|
|
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
|
|
autocast_ctx = nullcontext() if device_type == "cpu" else torch.cuda.amp.autocast(dtype=dtype)
|
|
|
|
|
|
@@ -150,7 +151,7 @@ if __name__ == "__main__":
|
|
|
if ckp_data:
|
|
|
model.load_state_dict(ckp_data['model'], strict=False)
|
|
|
optimizer.load_state_dict(ckp_data['optimizer'])
|
|
|
- scaler.load_state_dict(ckp_data['scaler'])
|
|
|
+ if ckp_data.get('scaler'): scaler.load_state_dict(ckp_data['scaler'])
|
|
|
start_epoch = ckp_data['epoch']
|
|
|
start_step = ckp_data.get('step', 0)
|
|
|
|