Compare commits
4 Commits
46c6eab291
...
dbad85e7c7
| Author | SHA1 | Date |
|---|---|---|
|
|
dbad85e7c7 | |
|
|
c367572702 | |
|
|
9c088dd3a5 | |
|
|
655d4705eb |
|
|
@ -67,7 +67,7 @@ st.markdown("""
|
||||||
</style>
|
</style>
|
||||||
""", unsafe_allow_html=True)
|
""", unsafe_allow_html=True)
|
||||||
|
|
||||||
device = "cuda" if torch.cuda.is_available() else "cpu"
|
device = "cuda" if torch.cuda.is_available() else ("mps" if torch.backends.mps.is_available() else "cpu")
|
||||||
|
|
||||||
# 多语言文本
|
# 多语言文本
|
||||||
LANG_TEXTS = {
|
LANG_TEXTS = {
|
||||||
|
|
|
||||||
|
|
@ -278,9 +278,10 @@ def rl_train_epoch(epoch, loader, iters, rollout_engine, ref_model, reward_model
|
||||||
|
|
||||||
rewards = calculate_rewards(prompts, completions, gt_batch, tools_batch, args.num_generations, reward_model, device=args.device, turn_outputs_batch=turn_outputs_batch, unfinished_batch=unfinished_batch)
|
rewards = calculate_rewards(prompts, completions, gt_batch, tools_batch, args.num_generations, reward_model, device=args.device, turn_outputs_batch=turn_outputs_batch, unfinished_batch=unfinished_batch)
|
||||||
|
|
||||||
model_unwrapped = model.module if isinstance(model, DistributedDataParallel) else model
|
|
||||||
with autocast_ctx:
|
with autocast_ctx:
|
||||||
res = model_unwrapped(input_ids, attention_mask=full_mask)
|
# 反向传播必须经过 DDP 包装后的模块:直接调用 .module 会跳过
|
||||||
|
# DDP 的 prepare_for_backward,梯度不会 all-reduce,各卡静默发散。
|
||||||
|
res = model(input_ids, attention_mask=full_mask)
|
||||||
aux_loss = res.aux_loss if lm_config.use_moe else torch.tensor(0.0, device=args.device)
|
aux_loss = res.aux_loss if lm_config.use_moe else torch.tensor(0.0, device=args.device)
|
||||||
logits = res.logits[:, :-1, :]
|
logits = res.logits[:, :-1, :]
|
||||||
per_token_logps = F.log_softmax(logits, dim=-1).gather(2, input_ids[:, 1:].unsqueeze(-1)).squeeze(-1)
|
per_token_logps = F.log_softmax(logits, dim=-1).gather(2, input_ids[:, 1:].unsqueeze(-1)).squeeze(-1)
|
||||||
|
|
@ -427,9 +428,9 @@ if __name__ == "__main__":
|
||||||
max_seq_len=args.max_seq_len + args.max_gen_len, use_moe=bool(args.use_moe))
|
max_seq_len=args.max_seq_len + args.max_gen_len, use_moe=bool(args.use_moe))
|
||||||
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume == 1 else None
|
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume == 1 else None
|
||||||
|
|
||||||
device_type = "cuda" if "cuda" in args.device else "cpu"
|
device_type = "cuda" if "cuda" in args.device else ("mps" if "mps" in args.device else "cpu")
|
||||||
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
||||||
autocast_ctx = nullcontext() if device_type == "cpu" else torch.cuda.amp.autocast(dtype=dtype)
|
autocast_ctx = nullcontext() if device_type == "cpu" else torch.amp.autocast(device_type, dtype=dtype)
|
||||||
|
|
||||||
wandb = None
|
wandb = None
|
||||||
if args.use_wandb and is_main_process():
|
if args.use_wandb and is_main_process():
|
||||||
|
|
|
||||||
|
|
@ -188,9 +188,9 @@ if __name__ == "__main__":
|
||||||
ckp_data = lm_checkpoint(lm_config_student, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
ckp_data = lm_checkpoint(lm_config_student, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
||||||
|
|
||||||
# ========== 3. 设置混合精度 ==========
|
# ========== 3. 设置混合精度 ==========
|
||||||
device_type = "cuda" if "cuda" in args.device else "cpu"
|
device_type = "cuda" if "cuda" in args.device else ("mps" if "mps" in args.device else "cpu")
|
||||||
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
||||||
autocast_ctx = nullcontext() if device_type == "cpu" else torch.cuda.amp.autocast(dtype=dtype)
|
autocast_ctx = nullcontext() if device_type == "cpu" else torch.amp.autocast(device_type, dtype=dtype)
|
||||||
|
|
||||||
# ========== 4. 配wandb ==========
|
# ========== 4. 配wandb ==========
|
||||||
wandb = None
|
wandb = None
|
||||||
|
|
@ -210,7 +210,7 @@ if __name__ == "__main__":
|
||||||
Logger(f'教师模型总参数量:{sum(p.numel() for p in teacher_model.parameters()) / 1e6:.3f} M')
|
Logger(f'教师模型总参数量:{sum(p.numel() for p in teacher_model.parameters()) / 1e6:.3f} M')
|
||||||
train_ds = SFTDataset(args.data_path, tokenizer, max_length=args.max_seq_len)
|
train_ds = SFTDataset(args.data_path, tokenizer, max_length=args.max_seq_len)
|
||||||
train_sampler = DistributedSampler(train_ds) if dist.is_initialized() else None
|
train_sampler = DistributedSampler(train_ds) if dist.is_initialized() else None
|
||||||
scaler = torch.cuda.amp.GradScaler(enabled=(args.dtype == 'float16'))
|
scaler = torch.amp.GradScaler(device_type, enabled=(args.dtype == 'float16' and device_type == 'cuda'))
|
||||||
optimizer = optim.AdamW(model.parameters(), lr=args.learning_rate)
|
optimizer = optim.AdamW(model.parameters(), lr=args.learning_rate)
|
||||||
|
|
||||||
# ========== 6. 从ckp恢复状态 ==========
|
# ========== 6. 从ckp恢复状态 ==========
|
||||||
|
|
|
||||||
|
|
@ -166,9 +166,9 @@ if __name__ == "__main__":
|
||||||
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
||||||
|
|
||||||
# ========== 3. 设置混合精度 ==========
|
# ========== 3. 设置混合精度 ==========
|
||||||
device_type = "cuda" if "cuda" in args.device else "cpu"
|
device_type = "cuda" if "cuda" in args.device else ("mps" if "mps" in args.device else "cpu")
|
||||||
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
||||||
autocast_ctx = nullcontext() if device_type == "cpu" else torch.cuda.amp.autocast(dtype=dtype)
|
autocast_ctx = nullcontext() if device_type == "cpu" else torch.amp.autocast(device_type, dtype=dtype)
|
||||||
|
|
||||||
# ========== 4. 配wandb ==========
|
# ========== 4. 配wandb ==========
|
||||||
wandb = None
|
wandb = None
|
||||||
|
|
@ -190,7 +190,7 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
train_ds = DPODataset(args.data_path, tokenizer, max_length=args.max_seq_len)
|
train_ds = DPODataset(args.data_path, tokenizer, max_length=args.max_seq_len)
|
||||||
train_sampler = DistributedSampler(train_ds) if dist.is_initialized() else None
|
train_sampler = DistributedSampler(train_ds) if dist.is_initialized() else None
|
||||||
scaler = torch.cuda.amp.GradScaler(enabled=(args.dtype == 'float16'))
|
scaler = torch.amp.GradScaler(device_type, enabled=(args.dtype == 'float16' and device_type == 'cuda'))
|
||||||
optimizer = optim.AdamW(model.parameters(), lr=args.learning_rate)
|
optimizer = optim.AdamW(model.parameters(), lr=args.learning_rate)
|
||||||
|
|
||||||
# ========== 6. 从ckp恢复状态 ==========
|
# ========== 6. 从ckp恢复状态 ==========
|
||||||
|
|
|
||||||
|
|
@ -119,9 +119,9 @@ if __name__ == "__main__":
|
||||||
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
||||||
|
|
||||||
# ========== 3. 设置混合精度 ==========
|
# ========== 3. 设置混合精度 ==========
|
||||||
device_type = "cuda" if "cuda" in args.device else "cpu"
|
device_type = "cuda" if "cuda" in args.device else ("mps" if "mps" in args.device else "cpu")
|
||||||
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
||||||
autocast_ctx = nullcontext() if device_type == "cpu" else torch.cuda.amp.autocast(dtype=dtype)
|
autocast_ctx = nullcontext() if device_type == "cpu" else torch.amp.autocast(device_type, dtype=dtype)
|
||||||
|
|
||||||
# ========== 4. 配wandb ==========
|
# ========== 4. 配wandb ==========
|
||||||
wandb = None
|
wandb = None
|
||||||
|
|
@ -136,7 +136,7 @@ if __name__ == "__main__":
|
||||||
model, tokenizer = init_model(lm_config, args.from_weight, device=args.device)
|
model, tokenizer = init_model(lm_config, args.from_weight, device=args.device)
|
||||||
train_ds = SFTDataset(args.data_path, tokenizer, max_length=args.max_seq_len)
|
train_ds = SFTDataset(args.data_path, tokenizer, max_length=args.max_seq_len)
|
||||||
train_sampler = DistributedSampler(train_ds) if dist.is_initialized() else None
|
train_sampler = DistributedSampler(train_ds) if dist.is_initialized() else None
|
||||||
scaler = torch.cuda.amp.GradScaler(enabled=(args.dtype == 'float16'))
|
scaler = torch.amp.GradScaler(device_type, enabled=(args.dtype == 'float16' and device_type == 'cuda'))
|
||||||
optimizer = optim.AdamW(model.parameters(), lr=args.learning_rate)
|
optimizer = optim.AdamW(model.parameters(), lr=args.learning_rate)
|
||||||
|
|
||||||
# ========== 6. 从ckp恢复状态 ==========
|
# ========== 6. 从ckp恢复状态 ==========
|
||||||
|
|
|
||||||
|
|
@ -95,9 +95,10 @@ def grpo_train_epoch(epoch, loader, iters, rollout_engine, ref_model, reward_mod
|
||||||
|
|
||||||
rewards = calculate_rewards(prompts, completions, reward_model).to(args.device) # [B*num_gen]
|
rewards = calculate_rewards(prompts, completions, reward_model).to(args.device) # [B*num_gen]
|
||||||
|
|
||||||
model_unwrapped = model.module if isinstance(model, DistributedDataParallel) else model
|
|
||||||
with autocast_ctx:
|
with autocast_ctx:
|
||||||
res = model_unwrapped(outputs, attention_mask=full_mask)
|
# 反向传播必须经过 DDP 包装后的模块:直接调用 .module 会跳过
|
||||||
|
# DDP 的 prepare_for_backward,梯度不会 all-reduce,各卡静默发散。
|
||||||
|
res = model(outputs, attention_mask=full_mask)
|
||||||
aux_loss = res.aux_loss if lm_config.use_moe else torch.tensor(0.0, device=args.device)
|
aux_loss = res.aux_loss if lm_config.use_moe else torch.tensor(0.0, device=args.device)
|
||||||
per_token_logps = F.log_softmax(res.logits[:, :-1, :], dim=-1).gather(2, outputs[:, 1:].unsqueeze(-1)).squeeze(-1).gather(1, logp_pos)
|
per_token_logps = F.log_softmax(res.logits[:, :-1, :], dim=-1).gather(2, outputs[:, 1:].unsqueeze(-1)).squeeze(-1).gather(1, logp_pos)
|
||||||
|
|
||||||
|
|
@ -256,9 +257,9 @@ if __name__ == "__main__":
|
||||||
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
||||||
|
|
||||||
# ========== 3. 设置混合精度 ==========
|
# ========== 3. 设置混合精度 ==========
|
||||||
device_type = "cuda" if "cuda" in args.device else "cpu"
|
device_type = "cuda" if "cuda" in args.device else ("mps" if "mps" in args.device else "cpu")
|
||||||
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
||||||
autocast_ctx = nullcontext() if device_type == "cpu" else torch.cuda.amp.autocast(dtype=dtype)
|
autocast_ctx = nullcontext() if device_type == "cpu" else torch.amp.autocast(device_type, dtype=dtype)
|
||||||
|
|
||||||
# ========== 4. 配wandb ==========
|
# ========== 4. 配wandb ==========
|
||||||
wandb = None
|
wandb = None
|
||||||
|
|
|
||||||
|
|
@ -112,9 +112,9 @@ if __name__ == "__main__":
|
||||||
ckp_data = lm_checkpoint(lm_config, weight=args.lora_name, save_dir='../checkpoints') if args.from_resume==1 else None
|
ckp_data = lm_checkpoint(lm_config, weight=args.lora_name, save_dir='../checkpoints') if args.from_resume==1 else None
|
||||||
|
|
||||||
# ========== 3. 设置混合精度 ==========
|
# ========== 3. 设置混合精度 ==========
|
||||||
device_type = "cuda" if "cuda" in args.device else "cpu"
|
device_type = "cuda" if "cuda" in args.device else ("mps" if "mps" in args.device else "cpu")
|
||||||
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
||||||
autocast_ctx = nullcontext() if device_type == "cpu" else torch.cuda.amp.autocast(dtype=dtype)
|
autocast_ctx = nullcontext() if device_type == "cpu" else torch.amp.autocast(device_type, dtype=dtype)
|
||||||
|
|
||||||
# ========== 4. 配wandb ==========
|
# ========== 4. 配wandb ==========
|
||||||
wandb = None
|
wandb = None
|
||||||
|
|
@ -148,7 +148,7 @@ if __name__ == "__main__":
|
||||||
# ========== 6. 定义数据和优化器 ==========
|
# ========== 6. 定义数据和优化器 ==========
|
||||||
train_ds = SFTDataset(args.data_path, tokenizer, max_length=args.max_seq_len)
|
train_ds = SFTDataset(args.data_path, tokenizer, max_length=args.max_seq_len)
|
||||||
train_sampler = DistributedSampler(train_ds) if dist.is_initialized() else None
|
train_sampler = DistributedSampler(train_ds) if dist.is_initialized() else None
|
||||||
scaler = torch.cuda.amp.GradScaler(enabled=(args.dtype == 'float16'))
|
scaler = torch.amp.GradScaler(device_type, enabled=(args.dtype == 'float16' and device_type == 'cuda'))
|
||||||
optimizer = optim.AdamW(lora_params, lr=args.learning_rate)
|
optimizer = optim.AdamW(lora_params, lr=args.learning_rate)
|
||||||
|
|
||||||
# ========== 7. 从ckp恢复状态 ==========
|
# ========== 7. 从ckp恢复状态 ==========
|
||||||
|
|
|
||||||
|
|
@ -159,8 +159,6 @@ def ppo_train_epoch(epoch, loader, iters, rollout_engine, ref_model, actor_sched
|
||||||
clipfrac_sum = 0.0
|
clipfrac_sum = 0.0
|
||||||
aux_loss_sum = 0.0
|
aux_loss_sum = 0.0
|
||||||
log_count = 0
|
log_count = 0
|
||||||
actor_unwrapped = actor_model.module if isinstance(actor_model, DistributedDataParallel) else actor_model
|
|
||||||
critic_unwrapped = critic_model.module if isinstance(critic_model, DistributedDataParallel) else critic_model
|
|
||||||
for ppo_epoch in range(args.ppo_update_iters):
|
for ppo_epoch in range(args.ppo_update_iters):
|
||||||
if stop_ppo:
|
if stop_ppo:
|
||||||
break
|
break
|
||||||
|
|
@ -168,11 +166,13 @@ def ppo_train_epoch(epoch, loader, iters, rollout_engine, ref_model, actor_sched
|
||||||
for i in range(0, B, mb_size):
|
for i in range(0, B, mb_size):
|
||||||
inds = b_inds[i:i + mb_size]
|
inds = b_inds[i:i + mb_size]
|
||||||
|
|
||||||
mb_values_seq = critic_unwrapped(input_ids=gen_out[inds], attention_mask=full_mask[inds])
|
# 反向传播必须经过 DDP 包装后的模块:直接调用 .module 会跳过
|
||||||
|
# DDP 的 prepare_for_backward,梯度不会 all-reduce,各卡静默发散。
|
||||||
|
mb_values_seq = critic_model(input_ids=gen_out[inds], attention_mask=full_mask[inds])
|
||||||
mb_resp_values = mb_values_seq.gather(1, logp_pos[inds])
|
mb_resp_values = mb_values_seq.gather(1, logp_pos[inds])
|
||||||
|
|
||||||
with autocast_ctx:
|
with autocast_ctx:
|
||||||
res = actor_unwrapped(input_ids=gen_out[inds], attention_mask=full_mask[inds])
|
res = actor_model(input_ids=gen_out[inds], attention_mask=full_mask[inds])
|
||||||
aux_loss = res.aux_loss if lm_config.use_moe else torch.tensor(0.0, device=args.device)
|
aux_loss = res.aux_loss if lm_config.use_moe else torch.tensor(0.0, device=args.device)
|
||||||
# 在 autocast 内计算 log_softmax,避免直接对 fp16/bf16 logits
|
# 在 autocast 内计算 log_softmax,避免直接对 fp16/bf16 logits
|
||||||
# 计算造成额外数值偏差。
|
# 计算造成额外数值偏差。
|
||||||
|
|
@ -191,7 +191,7 @@ def ppo_train_epoch(epoch, loader, iters, rollout_engine, ref_model, actor_sched
|
||||||
f"ratio_max={torch.exp(_lrv).max().item():.6f} "
|
f"ratio_max={torch.exp(_lrv).max().item():.6f} "
|
||||||
f"ratio_min={torch.exp(_lrv).min().item():.6f} "
|
f"ratio_min={torch.exp(_lrv).min().item():.6f} "
|
||||||
f"dropout={getattr(lm_config, 'dropout', None)} "
|
f"dropout={getattr(lm_config, 'dropout', None)} "
|
||||||
f"training={actor_unwrapped.training}")
|
f"training={actor_model.training}")
|
||||||
approx_kl = (0.5 * (log_ratio ** 2) * resp_policy_mask[inds]).sum() / resp_policy_mask[inds].sum().clamp(min=1)
|
approx_kl = (0.5 * (log_ratio ** 2) * resp_policy_mask[inds]).sum() / resp_policy_mask[inds].sum().clamp(min=1)
|
||||||
|
|
||||||
# 同步各卡的 approx_kl,防止某卡 break 而其它卡继续导致 DDP 死锁
|
# 同步各卡的 approx_kl,防止某卡 break 而其它卡继续导致 DDP 死锁
|
||||||
|
|
@ -364,9 +364,9 @@ if __name__ == "__main__":
|
||||||
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
||||||
|
|
||||||
# ========== 3. 设置混合精度 ==========
|
# ========== 3. 设置混合精度 ==========
|
||||||
device_type = "cuda" if "cuda" in args.device else "cpu"
|
device_type = "cuda" if "cuda" in args.device else ("mps" if "mps" in args.device else "cpu")
|
||||||
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
||||||
autocast_ctx = nullcontext() if device_type == "cpu" else torch.cuda.amp.autocast(dtype=dtype)
|
autocast_ctx = nullcontext() if device_type == "cpu" else torch.amp.autocast(device_type, dtype=dtype)
|
||||||
|
|
||||||
# ========== 4. 配wandb ==========
|
# ========== 4. 配wandb ==========
|
||||||
wandb = None
|
wandb = None
|
||||||
|
|
|
||||||
|
|
@ -118,9 +118,9 @@ if __name__ == "__main__":
|
||||||
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
ckp_data = lm_checkpoint(lm_config, weight=args.save_weight, save_dir='../checkpoints') if args.from_resume==1 else None
|
||||||
|
|
||||||
# ========== 3. 设置混合精度 ==========
|
# ========== 3. 设置混合精度 ==========
|
||||||
device_type = "cuda" if "cuda" in args.device else "cpu"
|
device_type = "cuda" if "cuda" in args.device else ("mps" if "mps" in args.device else "cpu")
|
||||||
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
dtype = torch.bfloat16 if args.dtype == "bfloat16" else torch.float16
|
||||||
autocast_ctx = nullcontext() if device_type == "cpu" else torch.cuda.amp.autocast(dtype=dtype)
|
autocast_ctx = nullcontext() if device_type == "cpu" else torch.amp.autocast(device_type, dtype=dtype)
|
||||||
|
|
||||||
# ========== 4. 配wandb ==========
|
# ========== 4. 配wandb ==========
|
||||||
wandb = None
|
wandb = None
|
||||||
|
|
@ -135,7 +135,7 @@ if __name__ == "__main__":
|
||||||
model, tokenizer = init_model(lm_config, args.from_weight, device=args.device)
|
model, tokenizer = init_model(lm_config, args.from_weight, device=args.device)
|
||||||
train_ds = PretrainDataset(args.data_path, tokenizer, max_length=args.max_seq_len)
|
train_ds = PretrainDataset(args.data_path, tokenizer, max_length=args.max_seq_len)
|
||||||
train_sampler = DistributedSampler(train_ds) if dist.is_initialized() else None
|
train_sampler = DistributedSampler(train_ds) if dist.is_initialized() else None
|
||||||
scaler = torch.cuda.amp.GradScaler(enabled=(args.dtype == 'float16'))
|
scaler = torch.amp.GradScaler(device_type, enabled=(args.dtype == 'float16' and device_type == 'cuda'))
|
||||||
optimizer = optim.AdamW(model.parameters(), lr=args.learning_rate)
|
optimizer = optim.AdamW(model.parameters(), lr=args.learning_rate)
|
||||||
|
|
||||||
# ========== 6. 从ckp恢复状态 ==========
|
# ========== 6. 从ckp恢复状态 ==========
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue