Commit f05a068
committed
Raise the per-parameter norms to norm_type in clip_grad_norm_
The p-norm over every gradient is (sum_i ||g_i||_p ** p) ** (1/p), so combining
the per-parameter norms means raising each to norm_type. clip_grad_norm_ squares
them instead and then takes the 1/norm_type root, which is only the same thing
when norm_type is 2:
grads [3, -4] and [2], norm_type=1
torch.nn.utils.clip_grad_norm_ -> 9.0
deepspeed -> 53.0 (7**2 + 2**2)
norm_type=3
torch.nn.utils.clip_grad_norm_ -> 4.626
deepspeed -> 2.894
The returned norm is wrong, and so is the clip coefficient derived from it, so
the gradients are scaled by the wrong factor.
This is a regression from #4915, which vectorized the accumulation. Before it the
loop read `total_norm += param_norm.item()**norm_type`; the rewrite replaced that
with `torch.stack(all_norms).square().sum()` and kept the 1/norm_type root. The
four other norm-combining sites in this file all still raise to norm_type:
get_flattened_grad_norm, get_weight_norm, get_global_norm_of_tensors and
get_norm_with_moe_layers.
norm_type is float()'d at the top of the function, so the default path becomes
pow(2.0), which is bit-identical to square() on float32.
Signed-off-by: Vineeth Sai <vineethsai4444@gmail.com>1 parent 84fd92a commit f05a068
2 files changed
Lines changed: 32 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
401 | 401 | | |
402 | 402 | | |
403 | 403 | | |
404 | | - | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
405 | 408 | | |
406 | 409 | | |
407 | 410 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
76 | 104 | | |
77 | 105 | | |
78 | 106 | | |
| |||
0 commit comments