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 minor bug in has_hooks #3460

Merged
merged 1 commit into from
Nov 4, 2024
Merged
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
23 changes: 15 additions & 8 deletions TrainingExtensions/torch/src/python/aimet_torch/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
import torch.nn
import torch
from torch.utils.data import DataLoader, Dataset
from torch.nn.modules.module import (
_global_backward_pre_hooks,
_global_backward_hooks,
_global_forward_pre_hooks,
_global_forward_hooks,
)
from torchvision import datasets, transforms

from aimet_common.defs import QuantScheme, QuantizationDataType, MAP_QUANT_SCHEME_TO_PYMO
Expand Down Expand Up @@ -543,14 +549,15 @@ def get_input_shape_batch_size(data_loader):

def has_hooks(module: torch.nn.Module):
""" Returns True if the module uses hooks. """

for hooks in (module._forward_pre_hooks, # pylint: disable=protected-access
module._forward_hooks, module._backward_hooks): # pylint: disable=protected-access
if hooks:
logger.warning("The specified model has registered hooks which might break winnowing")
return True
return False

# pylint: disable=protected-access
return module._backward_hooks or\
module._backward_pre_hooks or\
module._forward_hooks or\
module._forward_pre_hooks or\
_global_backward_pre_hooks or\
_global_backward_hooks or\
_global_forward_hooks or\
_global_forward_pre_hooks

def get_one_positions_in_binary_mask(mask):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ def __init__(self, model: torch.nn.Module, input_shape: Tuple,
"""

super().__init__(list_of_modules_to_winnow, reshape, in_place, verbose)
model.apply(has_hooks)

if any(has_hooks(module) for module in model.modules()):
logger.warning("The specified model has registered hooks which might break winnowing")

debug_level = logger.getEffectiveLevel()
logger.debug("Current log level: %s", debug_level)
Expand Down
Loading