Skip to content

Preserve precision in NovoGrad gradient norm accumulation - #570

Open
Afloat16 wants to merge 1 commit into
jettify:masterfrom
Afloat16:fix/novograd-norm-precision-20261112
Open

Afloat16 wants to merge 1 commit into
jettify:masterfrom
Afloat16:fix/novograd-norm-precision-20261112

Conversation

@Afloat16

Copy link
Copy Markdown

Problem

NovoGrad always creates its scalar second moments in the default floating dtype and squares gradients before selecting an accumulation dtype. Float64 gradient norms can therefore overflow or underflow when copied into float32 state, while FP16 gradients can overflow or underflow during the square itself. For example, nonzero gradients of magnitude 2000 in FP16 produce an infinite norm and no parameter update.

Float64 checkpoint restoration also changes the scalar-state dtype through PyTorch's native casting rules, so continued training can differ from uninterrupted training.

Change

Keep float64 norm state for float64 parameters and float32 norm state otherwise, including the AMSGrad maximum. Convert gradients to that accumulation dtype before squaring. The default float32 update formula is unchanged.

Validation

Python 3.12.14, PyTorch 2.15.0+cpu:

  • New tests: 22 failed / 4 passed before; 26 passed after. An independent double-precision norm checks float64 range, FP16 overflow/underflow, bfloat16, float32 controls, gradient averaging, and AMSGrad. Actual float64 checkpoint continuation matches uninterrupted training exactly.
  • All 4 existing NovoGrad convergence tests pass before and after.
  • Black 23.3.0, isort 5.9.3, pycodestyle, pyflakes, and git diff --check pass for changed files.

This covers CPU normalization and float64 checkpoint continuation. Half-precision checkpoint loading retains PyTorch's existing state-casting policy; this patch does not add master-state restoration. CUDA and the full optimizer suite are outside the validation scope.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant