Fix NaN gradients in Gaussian backend probability backward#165
Open
Hugh-888 wants to merge 2 commits into
Open
Fix NaN gradients in Gaussian backend probability backward#165Hugh-888 wants to merge 2 commits into
Hugh-888 wants to merge 2 commits into
Conversation
sansiro77
reviewed
Jun 12, 2026
sansiro77
left a comment
Contributor
There was a problem hiding this comment.
这里建议按最终乘积的幅度做 pruning,而不是按 poly_list 的单个因子判断。
当前写法会漏掉这类情况:每个因子都大于 1e-30,但多个因子相乘后落到 complex64 的极小区间,比如 ~1e-40,反传仍可能 NaN。可以考虑用 log 空间判断:
real_dtype = poly_list.real.dtype if poly_list.is_complex() else poly_list.dtype
threshold = 1 / torch.finfo(real_dtype).max
keep = torch.log(poly_list.detach().abs()).sum() > math.log(threshold)另外纯态路径里建议把 abs(hafnian(...)) ** 2 改成 haf.real.square() + haf.imag.square()。两者数学等价,但后者避开 complex AbsBackward:
z = torch.tensor(1e-40 + 1e-41j, dtype=torch.cfloat, requires_grad=True)
loss = z.abs() ** 2
loss.backward()
print(z.grad) # tensor(nan+nanj)
z = torch.tensor(1e-40 + 1e-41j, dtype=torch.cfloat, requires_grad=True)
loss = z.real.square() + z.imag.square()
loss.backward()
print(z.grad) # finite
sansiro77
reviewed
Jun 16, 2026
|
|
||
|
|
||
| def poly_lambda(submat: torch.Tensor, int_partition: list, power: int, loop: bool = False) -> torch.Tensor: | ||
| def poly_lambda( |
Contributor
There was a problem hiding this comment.
不管是对prod的单个元素还是最终乘积做阈值判断都不能正确解决问题(比如阈值以下的元素被乘以0,导致其梯度强制变为0了)。感觉这里最方便的处理还是先把submat变为cdouble,然后return的时候变回原始类型
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
This PR adds thresholding to the polynomial product term in the hafnian coefficient calculation to improve numerical stability for very small intermediate values.
Motivation
In Gaussian probability calculations, the hafnian polynomial expansion can produce product terms whose factors are extremely small. These terms may underflow or create unstable gradients in downstream autograd paths, especially when computing full probability dictionaries.
Changes
thresholdargument topoly_lambda.Notes
This thresholding acts as a numerical pruning step, so very small polynomial product contributions may be set to zero.