We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 0a1ecfd commit f30a835Copy full SHA for f30a835
src/torchjd/_linalg/_gramian.py
@@ -51,11 +51,12 @@ def normalize(gramian: PSDMatrix, eps: float) -> PSDMatrix:
51
sqrt of the sum of the diagonal elements. The gramian of the (Frobenius) normalization of `A` is
52
therefore `G` divided by the sum of its diagonal elements.
53
"""
54
+
55
squared_frobenius_norm = gramian.diagonal().sum()
- if squared_frobenius_norm < eps:
56
- output = torch.zeros_like(gramian)
57
- else:
58
- output = gramian / squared_frobenius_norm
+ condition = squared_frobenius_norm < eps
+ # Use torch.where rather than a if-else to avoid cuda synchronization.
59
+ output = torch.where(condition, torch.zeros_like(gramian), gramian / squared_frobenius_norm)
60
return cast(PSDMatrix, output)
61
62
0 commit comments