Skip to content

fix: Promote mixed-dtype Jacobians before computing their Gramian - #847

Merged
ValerianRey merged 2 commits into
SimplexLab:mainfrom
AryanRG:fix/mixed-dtype-autogram-gramian
Nov 22, 2026
Merged

ValerianRey merged 2 commits into
SimplexLab:mainfrom
AryanRG:fix/mixed-dtype-autogram-gramian

Conversation

@AryanRG

@AryanRG AryanRG commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

A mixed-dtype autogram.Engine can return an infinite Gramian even when the explicit Jacobian product is finite. Each block is multiplied at its input dtype before accumulation.

Promote each Jacobian block before multiplication and preserve the common dtype through accumulation. The regression compares both Engine paths against row-wise autograd.

CPU autogram and autojac passed 915 tests per dtype on float32 and float64. Ruff passed. CUDA remains unverified.

@AryanRG
AryanRG marked this pull request as ready for review November 19, 2026 07:34
@ValerianRey ValerianRey added cc: fix Conventional commit type for bug fixes of the actual library (changes to src). package: autogram labels Nov 22, 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.

Interesting edge case. I think we have two choices there:

  1. (what you did): compute jacobians in the parameter's dtype, and then, just before computing the gramian of this parameter, cast the jacobian to the promoted dtype. The gramian will thus be in the promoted dtype. Pros: A bit less precision lost during the higher precision matmul that computes the gramian. Cons: Larger memory cost (could be drastically larger if e.g. the parameter's dtype is fp16 and the gramian dtype is fp64, for a very large parameter).
  2. Compute jacobians in the parameter's dtype, then compute the related gramian in the parameter's dtype too, but cast it to the promoted dtype during gramian accumulation.

To implement 2, the changes to gramian_computer would have to be reverted, and gramian_accumulator would take a dtype at init, and accumulate the gramian always in the promoted dtype.

I'm not sure what's best between 1 and 2 honestly. Both seem quite easy to implement, and both have some pros and cons. What do you think @AryanRG ?

Also, this whole fix can be mirrored in jac_to_grad with optimize_gramian_computation=True, which currently suffers the same "bug", and for which we could also do either 1 or 2. I think to preserve uniformity between autojac and autogram it would be cool to make the same fix there too, in this PR or in another one (whatever you prefer).

Comment thread tests/unit/autogram/test_engine.py Resolved
@github-actions github-actions Bot changed the title Promote mixed-dtype Jacobians before computing their Gramian fix: Promote mixed-dtype Jacobians before computing their Gramian Nov 22, 2026
@AryanRG

AryanRG commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

I’d keep the cast before the Gram product. A finite FP16 derivative of 512 already gives inf when squared in FP16, so casting the Gramian during accumulation is too late. The cost is a temporary FP64 copy for each FP16 block as it’s processed. The optimized path still avoids concatenating the Jacobians.

I’ve made the same change in optimized jac_to_grad. Both paths now have FP16/FP64 coverage, and the focused cases pass on PyTorch 2.3 and 2.14.

@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.

LGTM, thanks for the contribution!

@ValerianRey
ValerianRey merged commit 34b7957 into SimplexLab:main Nov 22, 2026
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).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants