delete debug and misc fix
This commit is contained in:
1 parent
80431b10f6
commit
eff26f8a2f
2 files changed
+30
-20
No files matched your search
@@ -199,7 +199,7 @@ def train_one_epoch(model: torch.nn.Module,
|
||||
x_1d=samples_1d,
|
||||
mask_ratio=args.mask_ratio
|
||||
)
|
||||
print(f"DEBUG: Loss value is {loss.item()}, grad_fn is: {loss.grad_fn}")
|
||||
# print(f"DEBUG: Loss value is {loss.item()}, grad_fn is: {loss.grad_fn}")
|
||||
loss_value = loss.item()
|
||||
|
||||
if not math.isfinite(loss_value):
|
||||
|
||||
@@ -268,13 +268,23 @@ class NativeScalerWithGradNormCount:
|
||||
def __call__(self, loss, optimizer, clip_grad=None, parameters=None, create_graph=False, update_grad=True):
|
||||
self._scaler.scale(loss).backward(create_graph=create_graph)
|
||||
if update_grad:
|
||||
if clip_grad is not None:
|
||||
assert parameters is not None
|
||||
self._scaler.unscale_(optimizer)
|
||||
norm = torch.nn.utils.clip_grad_norm_(parameters, clip_grad)
|
||||
# 总是 unscale 优化器
|
||||
self._scaler.unscale_(optimizer)
|
||||
|
||||
# --- [核心修复] ---
|
||||
# 无论 clip_grad 是否设置,我们都使用 clip_grad_norm_ 来计算范数。
|
||||
# 如果 clip_grad 为 None 或 0,它不会进行裁剪,只会返回范数。
|
||||
# 为了安全,我们给一个非常大的 max_norm 值,确保它只计算不裁剪。
|
||||
if clip_grad is not None and clip_grad > 0:
|
||||
max_norm = clip_grad
|
||||
else:
|
||||
self._scaler.unscale_(optimizer)
|
||||
norm = get_grad_norm_(parameters)
|
||||
# 使用一个足够大的数,实际上等同于只计算范数而不裁剪
|
||||
max_norm = float('inf')
|
||||
|
||||
assert parameters is not None, "Parameters must be provided to calculate grad norm."
|
||||
norm = torch.nn.utils.clip_grad_norm_(parameters, max_norm=max_norm)
|
||||
# --- [修复结束] ---
|
||||
|
||||
self._scaler.step(optimizer)
|
||||
self._scaler.update()
|
||||
else:
|
||||
@@ -288,19 +298,19 @@ class NativeScalerWithGradNormCount:
|
||||
self._scaler.load_state_dict(state_dict)
|
||||
|
||||
|
||||
def get_grad_norm_(parameters, norm_type: float = 2.0) -> torch.Tensor:
|
||||
if isinstance(parameters, torch.Tensor):
|
||||
parameters = [parameters]
|
||||
parameters = [p for p in parameters if p.grad is not None]
|
||||
norm_type = float(norm_type)
|
||||
if len(parameters) == 0:
|
||||
return torch.tensor(0.)
|
||||
device = parameters[0].grad.device
|
||||
if norm_type == inf:
|
||||
total_norm = max(p.grad.detach().abs().max().to(device) for p in parameters)
|
||||
else:
|
||||
total_norm = torch.norm(torch.stack([torch.norm(p.grad.detach(), norm_type).to(device) for p in parameters]), norm_type)
|
||||
return total_norm
|
||||
# def get_grad_norm_(parameters, norm_type: float = 2.0) -> torch.Tensor:
|
||||
# if isinstance(parameters, torch.Tensor):
|
||||
# parameters = [parameters]
|
||||
# parameters = [p for p in parameters if p.grad is not None]
|
||||
# norm_type = float(norm_type)
|
||||
# if len(parameters) == 0:
|
||||
# return torch.tensor(0.)
|
||||
# device = parameters[0].grad.device
|
||||
# if norm_type == inf:
|
||||
# total_norm = max(p.grad.detach().abs().max().to(device) for p in parameters)
|
||||
# else:
|
||||
# total_norm = torch.norm(torch.stack([torch.norm(p.grad.detach(), norm_type).to(device) for p in parameters]), norm_type)
|
||||
# return total_norm
|
||||
|
||||
|
||||
def save_model(args, epoch, model, model_without_ddp, optimizer, loss_scaler):
|
||||
|
||||
Reference in new issue
Block a user