Repository navigation
fix(conv): scale gradients before the fp16 cast so small values survive - #23
Merged
Merged
Conversation
The WMMA backward casts dY to fp16, which rounds anything below about 3e-8 to exactly 0. Per-element gradients are often smaller than that, and shrink as image size grows, so training silently lost accuracy at higher resolution. Multiply dY by a fixed 2^10 in the cast (kernel and astype fallback) and divide it back out of dweights, dbiases and dinputs in the existing kernel epilogues. Gradients stay in true units outside the layer. This lowers the effective per-element dY clip from +-65504 to about +-64.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Conv2d's WMMA backward casts dY to fp16, which rounds anything below about 3e-8 to exactly 0, with no warning. Per-element gradients are often smaller than that, and they shrink as image size grows, so accuracy dropped at higher resolution. On the v1 CIFAR-10 CNN (float16, 25 epochs, seeds 42/43/44, RX 9070 XT, nearest-neighbour upscaled CIFAR), a Python prototype of this fix gave:At 64px, up to 95% of nonzero dY in a layer rounded to 0, and cosine against an fp32 backward pass fell as low as 0.31. With 2^10, at most 0.5% rounded to 0, and cosine was 1.0000 on every layer. The unset precision policy binds the same path, so default GPU training is exposed too, not just
set_precision('float16').dY is now multiplied by a fixed 2^10 in the cast (kernel and
astypefallback), anddweights,dbiasesanddinputsare divided back down in the existing kernel epilogues. There are no new launches, no new Python work on the hot path, and no new option. Gradients stay in true units outside the layer, so the loss, Dense, optimizers and manual loops are unaffected.One commit.
Behavior changes
plan_dweight_launchandplan_dinput_launchnow expect dY already scaled byGRAD_SCALE. Launching them directly with unscaled dY gives results 1024x too small.The fp32 fallback path is unchanged.
Testing
python3 -m unittest discover tests: 1190 tests pass on NumPy and CuPy (78 skipped).test_conv_gpu_backward_tiny_gradients_survive_fp16_cast: dY around 1e-8 against the fp32 fallback, on scalar and vector staging, with and without the cast kernel, plus a hand-computeddbiasescheck. Confirmed to fail on the old code. Existing parity tests missed this because they all use dY of magnitude about 1.tests/ops/test_conv_cast_kernel.pyrewritten for the scaled clip, with2**10hardcoded in the expected values. Confirmed to fail on the old code.dweightsdbiasesdinputsbenchmarks/compare.py, 32px, RX 9070 XT:model.train()went from 4.851 to 4.873 ms/step (+0.5%), with no phase flagged as changed.Not verified:
Follow-ups (out of scope)
Model.set_precisiondocstring: note that underfloat16on CuPy only Dense outputs float16, while Conv2d, BatchNorm and the pools output float32.