Repository navigation
Preserve the signed-log derivative at zero in PyTorch - #536
sylvesterkaczmarek wants to merge 1 commit into
Conversation
|
The fix is well scoped, and the validation matrix is unusually strong for this class of autograd edge case. In particular, testing both analytical and finite-difference gradients, non-contiguous tensors, mixed dtypes, and the zero-initialized SGD step covers the failure modes that a value-only roundtrip test would miss. One implementation detail worth preserving in the final code is that the zero branch must be explicit rather than relying on Given the clean merge state and the reported 40 passing tests, this looks ready from the numerical/API side. I would be happy to review any follow-up covering the same invariant in another backend if that becomes necessary. |
|
Thanks for checking this. The explicit zero branch and separate value/gradient checks are retained. Could a maintainer approve the pending build and review the PR? |
Summary
Fixes #535.
Choose a nonzero branch sign at zero when evaluating the signed-log transform and its inverse. The previous sign(x)*f(abs(x)) expression returned a zero automatic derivative at zero, even though both functions have slope one there.
Preserve forward values, nonzero derivatives, shapes, dtypes, devices, registry lookup and the other transforms. The backward-compatible import path inherits the correction. This is limited to the PyTorch optional signed-log transform; the default identity transform and MLX backend are unchanged.
Validation
Five methods with sixteen subtests extend the existing primitives suite. Tests check analytical derivatives, finite-difference gradcheck, tangent roundtrips, noncontiguous tensors, empty/nonfinite inputs, float16/bfloat16/float32/float64 values and an SGD update from zero initialization. Backward execution is also tested through torch.compile with the aot_eager backend and fullgraph=True.
python -m pytest src/timesfm3/torch/primitives_test.py src/timesfm3/torch/transformer_test.py src/timesfm3/torch/cpm_revin_refine_test.py src/timesfm3/torch/model_test.py -q: 40 passed, 16 subtests passed.git diff --checkpassed.The model tests use their existing small randomly initialized fixtures. Pretrained forecasting benchmarks, GPU/MPS, TorchInductor and MLX were not tested. No dependencies, workflows or existing test expectations change. This corrects the first derivative; it does not assert higher-order differentiability at the origin.
Based on master at
e51928e27119cb17bebc005be2696b75e0a9e688.