Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/torchjd/_linalg/_gramian.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ def normalize(gramian: PSDMatrix, eps: float) -> PSDMatrix:
sqrt of the sum of the diagonal elements. The gramian of the (Frobenius) normalization of `A` is
therefore `G` divided by the sum of its diagonal elements.
"""

squared_frobenius_norm = gramian.diagonal().sum()
if squared_frobenius_norm < eps:
output = torch.zeros_like(gramian)
else:
output = gramian / squared_frobenius_norm
condition = squared_frobenius_norm < eps

# Use torch.where rather than a if-else to avoid cuda synchronization.
output = torch.where(condition, torch.zeros_like(gramian), gramian / squared_frobenius_norm)
return cast(PSDMatrix, output)


Expand Down