Skip to content
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

Fix issue #72 Forward Hooks Persist After Destroying FeatureExtractor #91

Merged
merged 2 commits into from
Jul 29, 2024
Merged
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
15 changes: 14 additions & 1 deletion bdpy/dl/torch/torch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'''PyTorch module.'''

from typing import Iterable, List, Dict, Union, Tuple, Any, Callable, Optional

from collections import OrderedDict
import os

import numpy as np
Expand Down Expand Up @@ -93,6 +93,19 @@ def run(self, x: _tensor_t) -> Dict[str, _tensor_t]:
}

return features

def __del__(self):
'''
Remove forward hooks for the FeatureExtractor while keeping
other forward hooks in the model.
'''
for layer in self.__layers:
if self.__layer_map is not None:
layer = self.__layer_map[layer]
layer_object = models._parse_layer_name(self._encoder, layer)
for key, hook in layer_object._forward_hooks.items():
if hook == self._extractor:
del layer_object._forward_hooks[key]


class FeatureExtractorHandle(object):
Expand Down
Loading