Skip to content
Open
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions test/nn/test_pruning.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,26 @@ def test_pruning_container(self):
with self.assertRaises(TypeError):
container.add_pruning_method("ugh")

def test_pruning_container_add_to_empty_adopts_tensor_name(self):
container = prune.PruningContainer()

p = prune.L1Unstructured(amount=2)
p._tensor_name = "test"
container.add_pruning_method(p)

self.assertEqual(container._tensor_name, "test")
self.assertEqual(len(container), 1)

q = prune.RandomUnstructured(amount=2)
q._tensor_name = "test"
container.add_pruning_method(q)
self.assertEqual(len(container), 2)

r = prune.L1Unstructured(amount=2)
r._tensor_name = "another_test"
with self.assertRaises(ValueError):
container.add_pruning_method(r)

def test_pruning_container_compute_mask(self):
r"""Test `compute_mask` of pruning container with a known `t` and
`default_mask`. Indirectly checks that Ln structured pruning is
Expand Down
8 changes: 6 additions & 2 deletions torch/nn/utils/prune.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,14 @@ def add_pruning_method(self, method) -> None:
# check that we're adding a pruning method to the container
if not isinstance(method, BasePruningMethod) and method is not None:
raise TypeError(f"{type(method)} is not a BasePruningMethod subclass")
elif method is not None and self._tensor_name != method._tensor_name:
tensor_name = getattr(self, "_tensor_name", None)
if method is not None and tensor_name is None:
self._tensor_name = method._tensor_name
tensor_name = self._tensor_name
elif method is not None and tensor_name != method._tensor_name:
raise ValueError(
"Can only add pruning methods acting on "
f"the parameter named '{self._tensor_name}' to PruningContainer {self}."
f"the parameter named '{tensor_name}' to PruningContainer {self}."
+ f" Found '{method._tensor_name}'"
)
# if all checks passed, add to _pruning_methods tuple
Expand Down