-
Notifications
You must be signed in to change notification settings - Fork 0
Add tensor mask for parity tensor. #10
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |||||||||||||||||||||||||||||||
| __all__ = ["ParityTensor"] | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import dataclasses | ||||||||||||||||||||||||||||||||
| import functools | ||||||||||||||||||||||||||||||||
| import typing | ||||||||||||||||||||||||||||||||
| import torch | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
@@ -20,11 +21,29 @@ class ParityTensor: | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| edges: tuple[tuple[int, int], ...] | ||||||||||||||||||||||||||||||||
| tensor: torch.Tensor | ||||||||||||||||||||||||||||||||
| mask: torch.Tensor | None = None | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def __post_init__(self) -> None: | ||||||||||||||||||||||||||||||||
| assert len(self.edges) == self.tensor.dim(), f"Edges length ({len(self.edges)}) must match tensor dimensions ({self.tensor.dim()})." | ||||||||||||||||||||||||||||||||
| for dim, (even, odd) in zip(self.tensor.shape, self.edges): | ||||||||||||||||||||||||||||||||
| assert even >= 0 and odd >= 0 and dim == even + odd, f"Dimension {dim} must equal sum of even ({even}) and odd ({odd}) parts, and both must be non-negative." | ||||||||||||||||||||||||||||||||
| if self.mask is None: | ||||||||||||||||||||||||||||||||
| self.mask = self._tensor_mask() | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||
| def _unqueeze(cls, tensor: torch.Tensor, index: int, dim: int) -> torch.Tensor: | ||||||||||||||||||||||||||||||||
| return tensor.view([-1 if i == index else 1 for i in range(dim)]) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||
| def _edge_mask(cls, even: int, odd: int) -> torch.Tensor: | ||||||||||||||||||||||||||||||||
| return torch.cat([torch.zeros(even, dtype=torch.bool), torch.ones(odd, dtype=torch.bool)]) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def _tensor_mask(self) -> torch.Tensor: | ||||||||||||||||||||||||||||||||
| return functools.reduce( | ||||||||||||||||||||||||||||||||
| torch.logical_xor, | ||||||||||||||||||||||||||||||||
| (self._unqueeze(self._edge_mask(even, odd), index, self.tensor.dim()) for index, (even, odd) in enumerate(self.edges)), | ||||||||||||||||||||||||||||||||
| torch.ones_like(self.tensor, dtype=torch.bool), | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
Comment on lines
+42
to
+46
|
||||||||||||||||||||||||||||||||
| return functools.reduce( | |
| torch.logical_xor, | |
| (self._unqueeze(self._edge_mask(even, odd), index, self.tensor.dim()) for index, (even, odd) in enumerate(self.edges)), | |
| torch.ones_like(self.tensor, dtype=torch.bool), | |
| ) | |
| # Create a grid of indices for each dimension | |
| grids = torch.meshgrid( | |
| [torch.arange(even + odd) for even, odd in self.edges], indexing="ij" | |
| ) | |
| # Generate masks for each dimension | |
| masks = [ | |
| (grid >= even) for grid, (even, odd) in zip(grids, self.edges) | |
| ] | |
| # Combine masks using logical XOR | |
| return functools.reduce(torch.logical_xor, masks) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using -1 in tensor.view() when the tensor might not have the expected total number of elements could cause runtime errors. The -1 should only be used when the total size is known to be compatible with the new shape.