Fix Select::jvp tangent indexing (silent zero JVP through mx.where)#3643
Closed
nac7 wants to merge 1 commit into
Closed
Fix Select::jvp tangent indexing (silent zero JVP through mx.where)#3643nac7 wants to merge 1 commit into
nac7 wants to merge 1 commit into
Conversation
The tangents vector passed to primitive jvp methods is compact: tangents[i] corresponds to argnums[i], not to argnums[j] for any j. Select::jvp had three related errors: - assert(tangents.size() == 3) assumed a full-length vector; corrected to assert(tangents.size() == argnums.size()). - jvp_fun was called as jvp_fun(argnums[j]), passing argument numbers (1 or 2) as the positional index i; the lambda then accessed argnums[i] out-of-bounds. Corrected to jvp_fun(j). - Inside jvp_fun, tangents[1] and tangents[2] were hard-coded absolute indices; corrected to tangents[i] (the positional index). Together these caused mx.jvp to silently return zero when mx.where(cond, constant, traced_value) was differentiated and only the false branch carried a tangent.
Collaborator
|
Thanks for the PR but this had been covered by #3633, please let me know if the PR was missing anything. |
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.
Summary
Fixes #3627.
mx.jvpsilently returned zero when differentiating throughmx.where(cond, constant, traced_value)— when the constant is in the true branch and the traced value is in the false branch.Root Cause
Select::jvpinmlx/primitives.cpphad three related indexing errors.The JVP driver in
transforms.cppbuilds a compact tangent vector:tangents[i]is the tangent forargnums[i], not for argument numberi. Every other primitive JVP follows this positional convention.Select::jvpdid not:Wrong assertion —
assert(tangents.size() == 3)assumed a full-length vector; corrected toassert(tangents.size() == argnums.size()).Wrong call sites —
jvp_fun(argnums[0])andjvp_fun(argnums[i])passed argument numbers (1 or 2) as the positional indexi. Inside the lambda,int arg = argnums[i]then accessedargnumsout-of-bounds (UB in release mode). In practice the OOB read returned 0, hitting thearg == 0branch and returningzeros_like. Corrected tojvp_fun(0)/jvp_fun(i).Wrong tangent access —
tangents[1]andtangents[2]were hard-coded absolute argument indices instead oftangents[i]. Corrected totangents[i].Tests
Added
test_jvp_where_tangent_indexingintest_autograd.pycovering all four variants:where(cond, traced, const)where(cond, const, traced)where(cond, traced, traced)