-
Notifications
You must be signed in to change notification settings - Fork 0
test(#22): add unit tests for essential functions #31
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
Changes from all commits
2fdf67f
0f86f04
223567a
aa0078c
9e35506
247497a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,5 +3,7 @@ env | |
| __pycache__ | ||
| parity_tensor/_version.py | ||
| build | ||
| .idea | ||
| .venv | ||
| .coverage | ||
| coverage.xml | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| """ | ||
| [#22](https://github.com/hzhangxyz/parity_tensor/issues/22) | ||
| Test essential functions. | ||
| """ | ||
| from typing import Tuple | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个是老版本的python type annotation,例如,新版本可以直接使用 |
||
|
|
||
| import torch | ||
| import pytest | ||
|
|
||
| from parity_tensor import ParityTensor | ||
|
|
||
|
|
||
| @pytest.fixture(name="parity_instance_fx", | ||
| params=[( | ||
| ParityTensor((False, False), ((2, 2), (1, 3)), torch.randn([4, 4])), | ||
| ParityTensor((False, False), ((2, 2), (1, 3)), torch.randn([4, 4])), | ||
| ), ( | ||
| ParityTensor((True, False, True), ((1, 1), (2, 2), (3, 1)), torch.randn([2, 4, 4])), | ||
| ParityTensor((True, False, True), ((1, 1), (2, 2), (3, 1)), torch.randn([2, 4, 4])), | ||
| ), | ||
| (ParityTensor((True, True, False, False), ((1, 2), (2, 2), (1, 1), (3, 1)), | ||
| torch.randn([3, 4, 2, 4])), ParityTensor((True, True, False, False), ((1, 2), (2, 2), (1, 1), (3, 1)), torch.randn([3, 4, 2, 4])))]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. python的格式化工具会根据列表中最后一个元素后是否由逗号来判断是否需要换行,你这个前面2个tuple都是由逗号的,但是第三个没有,所以看起来第三格式很奇怪。 |
||
| def _parity_instance_fixture(request: pytest.FixtureRequest) -> Tuple[ParityTensor, ParityTensor]: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixture的name可以直接使用函数的名称,不需要这样设置的, |
||
| """Fixture for a parity tensor instance.""" | ||
| return request.param | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def permute_tuple_fx(parity_instance_fx: Tuple[ParityTensor, ParityTensor]) -> Tuple[int, ...]: | ||
| """Fixture for a permutation tuple.""" | ||
| parity_tensor, _ = parity_instance_fx | ||
| rank = parity_tensor.tensor.ndim | ||
| return tuple(reversed(range(rank))) | ||
|
|
||
|
|
||
| def test_arithmetic(parity_instance_fx: Tuple[ParityTensor, ParityTensor]) -> None: | ||
| """Test the arithmetic operators on ParityTensor.""" | ||
| parity_tensor, instance = parity_instance_fx | ||
|
|
||
| # Test __pos__ method. | ||
| print("-" * 5, "Test __pos__ method", "-" * 5) | ||
| print(parity_tensor) | ||
| print(+parity_tensor) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不要在测试中使用print来输出到屏幕,不会有人对此进行任何检查。 |
||
|
|
||
| # Test __neg__ method. | ||
| print("-" * 5, "Test __neg__ method", "-" * 5) | ||
| print(parity_tensor) | ||
| print(-parity_tensor) | ||
|
|
||
| # Test __add__ method. | ||
| print("-" * 5, "Test __add__ method", "-" * 5) | ||
| print(parity_tensor) | ||
| print(parity_tensor + 1) | ||
| print(parity_tensor + instance) | ||
|
|
||
| # Test __radd__ method. | ||
| print("-" * 5, "Test __radd__ method", "-" * 5) | ||
| print(parity_tensor) | ||
| print(1 + parity_tensor) | ||
| print(instance + parity_tensor) | ||
|
|
||
| # Test __iadd__ method. | ||
| print("-" * 5, "Test __iadd__ method", "-" * 5) | ||
| print(parity_tensor) | ||
| parity_tensor += 1 | ||
| print(parity_tensor) | ||
| parity_tensor += instance | ||
| print(parity_tensor) | ||
|
|
||
| # Test __sub__ method. | ||
| print("-" * 5, "Test __sub__ method", "-" * 5) | ||
| print(parity_tensor) | ||
| print(parity_tensor - 1) | ||
| print(parity_tensor - instance) | ||
|
|
||
| # Test __rsub__ method. | ||
| print("-" * 5, "Test __rsub__ method", "-" * 5) | ||
| print(parity_tensor) | ||
| print(1 - parity_tensor) | ||
| print(instance - parity_tensor) | ||
|
|
||
| # Test __isub__ method. | ||
| print("-" * 5, "Test __isub__ method", "-" * 5) | ||
| print(parity_tensor) | ||
| parity_tensor -= 1 | ||
| print(parity_tensor) | ||
| parity_tensor -= instance | ||
| print(parity_tensor) | ||
|
|
||
| # Test __mul__ method. | ||
| print("-" * 5, "Test __mul__ method", "-" * 5) | ||
| print(parity_tensor) | ||
| print(parity_tensor * 2) | ||
|
|
||
| # Test __rmul__ method. | ||
| print("-" * 5, "Test __rmul__ method", "-" * 5) | ||
| print(parity_tensor) | ||
| print(2 * parity_tensor) | ||
|
|
||
| # Test __imul__ method. | ||
| print("-" * 5, "Test __imul__ method", "-" * 5) | ||
| print(parity_tensor) | ||
| parity_tensor *= 2 | ||
| print(parity_tensor) | ||
|
|
||
| # Test __truediv__ method. | ||
| print("-" * 5, "Test __truediv__ method", "-" * 5) | ||
| print(parity_tensor) | ||
| print(parity_tensor / 2) | ||
| print(parity_tensor / instance) | ||
|
|
||
| # Test __rtruediv__ method. | ||
| print("-" * 5, "Test __rtruediv__ method", "-" * 5) | ||
| print(parity_tensor) | ||
| print(2 / parity_tensor) | ||
|
|
||
| # Test __itruediv__ method. | ||
| print("-" * 5, "Test __itruediv__ method", "-" * 5) | ||
| print(parity_tensor) | ||
| parity_tensor /= 2 | ||
| print(parity_tensor) | ||
|
|
||
|
|
||
| def test_masking(parity_instance_fx: tuple[ParityTensor, ParityTensor]) -> None: | ||
| """Test masking.""" | ||
| parity_tensor, _ = parity_instance_fx | ||
|
|
||
| mask = parity_tensor.mask | ||
| assert mask.shape == parity_tensor.tensor.shape, "Mask shape mismatch" | ||
|
|
||
| assert mask.dtype == torch.bool, "Mask must be of dtype bool" | ||
|
|
||
| original_tensor = parity_tensor.tensor.clone() | ||
| update_tensor = parity_tensor.update_mask().tensor | ||
|
|
||
| assert torch.all(update_tensor[~mask] == 0), "Masked-out positions must be zero" | ||
|
|
||
| assert torch.all(update_tensor[mask] == original_tensor[mask]), ("Unmasked positions must be preserved") | ||
|
|
||
| assert parity_tensor.mask is not None, "Mask should be cached after first access" | ||
|
|
||
|
|
||
| def test_permute(parity_instance_fx: Tuple[ParityTensor, ParityTensor], permute_tuple_fx: Tuple[int, ...]) -> None: | ||
| """Test permute.""" | ||
| parity_tensor, _ = parity_instance_fx | ||
| print("Before permute:", parity_tensor) | ||
| parity_tensor.permute(permute_tuple_fx) | ||
| print("After permute:", parity_tensor) | ||
|
|
||
|
|
||
| def test_reverse() -> None: | ||
| """Test tensor reverse.""" | ||
| assert True | ||
|
|
||
|
|
||
| def test_split_edge() -> None: | ||
| """Test split edge.""" | ||
| assert True | ||
|
|
||
|
|
||
| def test_merge_edge() -> None: | ||
| """Test merge edge.""" | ||
| assert True | ||
|
|
||
|
|
||
| def test_contract() -> None: | ||
| """Test contract.""" | ||
| assert True | ||
|
|
||
|
|
||
| def test_trace() -> None: | ||
| """Test trace.""" | ||
| assert True | ||
|
|
||
|
|
||
| def test_conjugate() -> None: | ||
| """Test conjugate.""" | ||
| assert True | ||
|
|
||
|
|
||
| def test_svd() -> None: | ||
| """Test svd.""" | ||
| assert True | ||
|
|
||
|
|
||
| def test_qr() -> None: | ||
| """Test qr.""" | ||
| assert True | ||
|
|
||
|
|
||
| def test_identity() -> None: | ||
| """Test identity.""" | ||
| assert True | ||
|
|
||
|
|
||
| def test_exponential() -> None: | ||
| """Test tensor exponential.""" | ||
| assert True | ||
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.
不要把对各个功能的测试集中在一个文件中,也不要把各个测试的改动集中在一个PR里,提交一个完整的PR就很好了。
不要在测试文件中标注这个,以后看代码的人如果真的想知道这个代码怎么来的,他可以用git blame。