From 3e7c071aced585808cc3294ef2e3f85b55a1048a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Perceval=20Wajsb=C3=BCrt?= Date: Mon, 9 Sep 2024 11:35:59 +0200 Subject: [PATCH 1/3] feat: improve error message when refolding with missing dims --- foldedtensor/__init__.py | 13 ++++++++++--- tests/test_folded_tensor.py | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/foldedtensor/__init__.py b/foldedtensor/__init__.py index e3eb63f..318d83a 100644 --- a/foldedtensor/__init__.py +++ b/foldedtensor/__init__.py @@ -402,9 +402,16 @@ def refold(self, *dims: Union[Sequence[Union[int, str]], int, str]): "sequence or each arguments to be ints or strings" ) dims = dims[0] - dims = tuple( - dim if isinstance(dim, int) else self.full_names.index(dim) for dim in dims - ) + try: + dims = tuple( + dim if isinstance(dim, int) else self.full_names.index(dim) + for dim in dims + ) + except ValueError: + raise ValueError( + f"Folded tensor with available dimensions {self.full_names} " + f"could not be refolded with dimensions {list(dims)}" + ) if dims == self.data_dims: return self diff --git a/tests/test_folded_tensor.py b/tests/test_folded_tensor.py index c8f9e13..a4d2e24 100644 --- a/tests/test_folded_tensor.py +++ b/tests/test_folded_tensor.py @@ -431,3 +431,18 @@ def test_hashable_lengths(): assert tensor.lengths is embedding(tensor).lengths assert hash(tensor.lengths) is not None assert hash(tensor.lengths) == hash(embedding(tensor).lengths) + + +def test_missing_dims(): + tensor = as_folded_tensor( + [ + [0, 1, 2], + [3, 4], + ], + full_names=("sample", "token"), + dtype=torch.long, + ) + with pytest.raises(ValueError) as e: + tensor.refold("line", "token") + + assert "line" in str(e.value) From b7074edfbfa2eac76da07f229a36cf91ef7c6784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Perceval=20Wajsb=C3=BCrt?= Date: Mon, 9 Sep 2024 11:37:04 +0200 Subject: [PATCH 2/3] chore: bump version to 0.3.5 --- changelog.md | 3 ++- foldedtensor/__init__.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 4c74ffa..c964610 100644 --- a/changelog.md +++ b/changelog.md @@ -1,8 +1,9 @@ # Changelog -## Unreleased +## v0.3.5 - Support hashing the `folded_tensor.length` field (via a UserList), which is convenient for caching +- Improve error messaging when refolding with missing dims ## v0.3.4 diff --git a/foldedtensor/__init__.py b/foldedtensor/__init__.py index 318d83a..10bf098 100644 --- a/foldedtensor/__init__.py +++ b/foldedtensor/__init__.py @@ -45,7 +45,7 @@ except AttributeError: DisableTorchFunctionSubclass = torch._C.DisableTorchFunction -__version__ = "0.3.4" +__version__ = "0.3.5" class FoldedTensorLengths(UserList): From 575f19c16be5e2cc9dff4030b11f1b4953e18191 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Perceval=20Wajsb=C3=BCrt?= Date: Mon, 16 Sep 2024 09:40:43 +0200 Subject: [PATCH 3/3] ci: add include-hidden-files param to upload coverage files --- .github/workflows/tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0033da8..8892dea 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -60,6 +60,7 @@ jobs: name: coverage-data-${{ matrix.python-version }} path: .coverage.* if-no-files-found: ignore + include-hidden-files: true Coverage: needs: Pytest