Skip to content

fix(autojac): Handle repeated tensors in jac_to_grad - #844

Merged
ValerianRey merged 1 commit into
SimplexLab:mainfrom
SajalDevX:fix/jac-to-grad-duplicate-tensors
Nov 7, 2026
Merged

ValerianRey merged 1 commit into
SimplexLab:mainfrom
SajalDevX:fix/jac-to-grad-duplicate-tensors

Conversation

@SajalDevX

Copy link
Copy Markdown
Contributor

If the same tensor appears more than once in the tensors given to jac_to_grad, it breaks:

  • With the default retain_jac=False, it crashes with AttributeError: 'Parameter' object has no attribute 'jac', because _free_jacs deletes .jac on the first occurrence and then tries again. By that point some .jac fields are already gone and no .grad has been filled.
  • With retain_jac=True, the repeated tensor's Jacobian is concatenated twice. Its gradient gets accumulated twice, and for non-linear aggregators like UPGrad the duplicated columns also change the Gramian, so every gradient comes out wrong.

This happens easily when you merge parameter lists from modules that overlap, for example list(model.parameters()) + list(model.encoder.parameters()). backward and mtl_backward already handle repeated inputs / shared_params by putting them in an OrderedSet. I did the same thing in jac_to_grad, so each tensor is only used once, and I added a sentence about it to the docstring.

Test: I added test_repeated_tensors to tests/unit/autojac/test_jac_to_grad.py. It runs for retain_jac True/False and optimize_gramian_computation True/False. Without the fix, all 4 cases fail (2 with the AttributeError, 2 with a wrong gradient). With the fix, all 4 pass. ruff, ty and pytest tests/unit -W error (CPU) also pass.

@ValerianRey ValerianRey added package: autojac cc: fix Conventional commit type for bug fixes of the actual library (changes to src). labels Nov 7, 2026

@ValerianRey ValerianRey left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch tyvm!

@ValerianRey
ValerianRey merged commit 06349de into SimplexLab:main Nov 7, 2026
20 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cc: fix Conventional commit type for bug fixes of the actual library (changes to src). package: autojac

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants