-
Notifications
You must be signed in to change notification settings - Fork 0
Add the basic parity tensor class and its arithmetic operators. #2
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
Conversation
db6994f to
17a277e
Compare
17a277e to
6bf1100
Compare
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.
Pull Request Overview
This PR introduces a basic ParityTensor class that wraps PyTorch tensors with additional edge information representing even/odd parts for each dimension, along with comprehensive arithmetic operator support.
- Implements a dataclass-based
ParityTensorwith validation for edge-tensor dimension compatibility - Adds full arithmetic operator support (addition, subtraction, multiplication, division) with both ParityTensor and scalar operands
- Exports the new class through the package's
__init__.pymodule
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| parity_tensor/parity_tensor.py | Implements the core ParityTensor class with edge validation and arithmetic operators |
| parity_tensor/init.py | Exports the new ParityTensor class in the package's public API |
|
|
||
| def __mul__(self, other: typing.Any) -> ParityTensor: | ||
| if isinstance(other, ParityTensor): | ||
| self._validate_edge_compatibility(other) |
Copilot
AI
Jul 30, 2025
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.
Element-wise multiplication between ParityTensors with the same edges may not preserve the parity structure. Consider whether multiplication should validate edge compatibility or if it should have different behavior than addition/subtraction.
| self._validate_edge_compatibility(other) | |
| self._validate_edge_compatibility(other) | |
| self._validate_parity_structure(other) |
| return ParityTensor( | ||
| edges=self.edges, | ||
| tensor=self.tensor / other.tensor, | ||
| ) |
Copilot
AI
Jul 30, 2025
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.
Element-wise division between ParityTensors with the same edges may not preserve the parity structure. Consider whether division should validate edge compatibility or if it should have different behavior than addition/subtraction.
| return ParityTensor( | |
| edges=self.edges, | |
| tensor=self.tensor / other.tensor, | |
| ) | |
| result = ParityTensor( | |
| edges=self.edges, | |
| tensor=self.tensor / other.tensor, | |
| ) | |
| result._validate_parity_preservation() | |
| return result |
| return ParityTensor( | ||
| edges=self.edges, | ||
| tensor=self.tensor + other.tensor, | ||
| ) |
Copilot
AI
Jul 30, 2025
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.
No input validation is performed on the 'other' parameter before attempting tensor operations. This could lead to unexpected behavior or errors if 'other' is not a valid operand for tensor arithmetic.
| ) | |
| ) | |
| if not isinstance(other, (int, float, torch.Tensor)): | |
| raise TypeError(f"Unsupported operand type(s) for +: 'ParityTensor' and '{type(other).__name__}'") |
No description provided.