This commit is contained in:
yilv committed 2025-10-03 00:40:18 +08:00
1 parent 808b59db45
commit e338564ce1
1 file changed
-17
@@ -210,23 +210,6 @@ def train_one_epoch(model: torch.nn.Module,
loss = loss / accum_iter
if loss_scaler is not None:
optimizer.zero_grad() # 先清空梯度
scaled_loss = loss_scaler.get_scale() * loss
scaled_loss.backward()
if (data_iter_step + 1) % accum_iter == 0: # 只在应该更新的步骤检查
print("\n--- Gradient Inspection ---")
# 检查模型最开始的参数(patch_embed)
first_param = model_without_ddp.patch_embed.stem[0].weight
if first_param.grad is not None:
print(f"First layer grad norm: {torch.linalg.norm(first_param.grad).item()}")
else:
print("First layer grad is None!")
last_param = model_without_ddp.decoder_pred_cwt[-1].weight
if last_param.grad is not None:
print(f"Last layer grad norm: {torch.linalg.norm(last_param.grad).item()}")
else:
print("Last layer grad is None!")
print("-------------------------\n")
loss_scaler(loss, optimizer, parameters=model.parameters(), update_grad=((data_iter_step + 1) % accum_iter == 0), clip_grad=args.clip_grad if hasattr(args, 'clip_grad') else None)
else:
loss.backward()