Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly Handle complex and float dtypes during directsum #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 11 additions & 4 deletions escnn/group/representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,16 @@ def directsum(reprs: List[escnn.group.Representation],
irreps += r.irreps

size = sum([r.size for r in reprs])

cob = np.zeros((size, size))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is by default constructing np.float matrices.

cob_inv = np.zeros((size, size))

# Determine the dtype for the change of basis diagonal matrix to avoid unsafe casting.
dtype = np.complex if np.any([np.iscomplexobj(rep.change_of_basis) for rep in reprs]) else np.float
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This seems a reasonable approach to handle dtypes assuming user configures default numpy float and complex precisions.

Another approach is to dynamically check the safe nature of a cast, and in the case of unsafe casting update the type of cob.

PS: What cob stands for?


cob = np.zeros((size, size), dtype=dtype)
cob_inv = np.zeros((size, size), dtype=dtype)
p = 0
for r in reprs:
assert np.can_cast(r.change_of_basis.dtype, cob.dtype), \
f"Cannot safely cast {r.change_of_basis.dtype} to {cob.dtype}"
cob[p:p + r.size, p:p + r.size] = r.change_of_basis
cob_inv[p:p + r.size, p:p + r.size] = r.change_of_basis_inv
p += r.size
Expand All @@ -580,7 +585,9 @@ def directsum(reprs: List[escnn.group.Representation],

supported_nonlinearities = set.intersection(*[r.supported_nonlinearities for r in reprs])

return Representation(group, name, irreps, change_of_basis, supported_nonlinearities, change_of_basis_inv=change_of_basis_inv)
return Representation(
group, name, irreps, change_of_basis, supported_nonlinearities, change_of_basis_inv=change_of_basis_inv
)


def disentangle(repr: Representation) -> Tuple[np.ndarray, List[Representation]]:
Expand Down