Repository navigation
Conversation
[Medium impact] The PR appears safe to merge within its stated scope. SummaryFixes gradients through
Lynn-hh explicitly defers the known divisor-gradient issue to #2225. No new actionable issues were found. Tests were not run during this review. Reviews (3) · Last reviewed commit: "Fix frac and vector mod gradients (GH-22..." · Reviewed by Greptile |
|
No actionable comments were generated in the recent review. Summary
Change: Bug fix · Severity of issue fixed: Medium Merge Risk:
|
| // MISSINGADJOINT: element-wise: adj_a[i] += adj_ret[i]; adj_b[i] -= floor(a[i] / b[i]) * | ||
| // adj_ret[i] | ||
| for (unsigned i = 0; i < Length; ++i) { | ||
| adj_mod(a[i], b[i], adj_a[i], adj_b[i], adj_ret[i]); |
There was a problem hiding this comment.
There’s a small CPU vectorization opportunity here. In warmed graph replays over 262,144 float32 vectors with retained nonzero gradient seeds, copying adj_ret locally reduced CPU backward times by roughly 9–12% for vec2–vec4 on x86. CUDA timings stayed within ~1%.
Could you snapshot the incoming gradient before the component updates?
const vec_t<Length, Type> grad = adj_ret;
for (unsigned i = 0; i < Length; ++i) {
adj_mod(a[i], b[i], adj_a[i], adj_b[i], grad[i]);
}Please apply the same local copy to the vec2/vec3 overloads and verify whether it improves CPU backward performance on your Apple M3. CUDA validation can be handled separately.
There was a problem hiding this comment.
Forgot to mention that for any subsequent changes, please keep the branch's changes squashed to a single commit and rebased with main, which aids in merging this.
There was a problem hiding this comment.
Thanks! Applied to the generic overload and to the vec2/vec3 specializations, squashed into one commit and rebased onto main.
On an Apple M3 I don't see a measurable difference. I timed warmed CPU graph replays of the backward launch over 262,144 float32 vectors with a retained nonzero gradient seed, alternating the two versions over three runs. Median times in ms, before and after:
| before | after | |
|---|---|---|
| vec2 | 0.462 | 0.462 |
| vec3 | 0.577 | 0.582 |
| vec4 | 0.520 | 0.527 |
The differences are within run-to-run noise on this machine, so the gain looks specific to x86, and nothing regresses on arm64. The frac and modulo tests still pass on CPU.
wp.frac() and the vector overloads of wp.mod() were registered as non-differentiable and their native adjoints were empty stubs, so tape.backward() silently returned zero gradients through them. frac(x) = x - trunc(x) has a unit gradient between integers. The vector adj_mod() now calls the scalar adj_mod() for each component, so scalar and vector modulo share one gradient rule, including the divisor term tracked in NVIDIAGH-2223. Signed-off-by: Lynn <[email protected]>
fde96e9 to
d387c09
Compare
What problem are you trying to solve?
tape.backward()silently returns zero gradients throughwp.frac()and throughwp.mod()on vectors. Away from the discontinuities,d/dx frac(x)is 1, and each component of the vectormod(a, b)has derivative 1 with respect toa. I use Warp through Newton for differentiable robot simulation, where patterns like trajectory interpolation atwp.frac(t)or wrappingvec3positions into a periodic box lose their gradient without any warning. Details and a reproducer are in the issue.Closes #2280
What does this change?
wp.frac()and the vector overloads ofwp.mod()are no longer registered withis_differentiable=False, so codegen emits their reverse calls.adj_frac()inbuiltin.haccumulates the incoming adjoint, sincefrac(x) = x - trunc(x)andtrunc()is piecewise constant.adj_mod()overloads invec.hcall the scalaradj_mod()for each component, the same way the vectoradj_min()andadj_max()do. Scalar and vector modulo now share one gradient rule. Integer vectors keep the empty integer adjoint, andfloat16/bfloat16use the same scalar code as their forward pass.test_float_to_intnow also checks thefracoutput, which it previously skipped, and expects a unit gradient there.test_modulobuilds its kernel with backward enabled for float types and checks the gradient with respect to the dividend for vector lengths 2 to 5.The divisor gradient of scalar
wp.mod()is a separate bug (#2223) that #2225 fixes. Because the vector adjoint calls the scalar one, the vector divisor gradient becomes correct once #2225 lands, and the two pull requests touch different code.Existing pull requests
I searched open pull requests for duplicate or overlapping work.
Related pull requests: Fix the divisor gradient of wp.mod for floating-point scalars #2225 fixes the divisor gradient of scalar
wp.mod(). It does not changewp.frac()or the vector overloads. No other overlapping work found.Validation
test_float_to_int(float16, float32, float64) andtest_modulo(float16, float32, float64) fail onmainwithout this change, with zero gradients where 1 is expected. They pass with it, along with the integer variants. The reproducer in Gradients of wp.frac() and vector wp.mod() are always zero #2280 prints[0. 0. 0.]before the change and[1. 1. 1.]after it. A finite-difference sweep over about 90 differentiable built-ins withwp.autograd.jacobian()andjacobian_fd()in float64 agrees forfracand for the vectormoddividend after the change.test_arithmetic,test_vec_scalar_ops,test_grad,test_grad_debug,test_bf16,test_fp16,test_rounding,test_vec,test_mathandtest_external_buildpass on CPU. ruff, ruff-format, clang-format and typos are clean at the versions pinned in.pre-commit-config.yaml. The generatedexports.hand__init__.pyido not depend onis_differentiable, so they are unchanged.Checklist
mainand contains one commit, unless a maintainer agreed to keep the change as multiple commits.+prefix only for changes that do not require a GitHub issue.