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

Feat (nn): cache modules that require subtensor slicing #628

Merged
merged 1 commit into from
Jun 15, 2023
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
22 changes: 16 additions & 6 deletions src/brevitas/nn/mixin/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(self, weight_quant: Optional[WeightQuantType], **kwargs):
kwargs_prefix='weight_',
proxy_prefix='weight_',
**kwargs)
self._cached_sub_tensor_slice_list_modules = None

@property
@abstractmethod
Expand Down Expand Up @@ -64,9 +65,17 @@ def quant_weight(
# prepare the quantizer for a subtensor input, if any modifications are required
# we set a list of tuples rather than a list of slices so that it's jit friendly
# slices generation is handled by each module internally
for m in self.weight_quant.modules():
if hasattr(m, 'subtensor_slice_list'):
m.setattr('subtensor_slice_list', subtensor_slice_list)

# we cache which modules require the attribute
if self._cached_sub_tensor_slice_list_modules is not None:
for m in self._cached_sub_tensor_slice_list_modules:
m.subtensor_slice_list = subtensor_slice_list
else:
self._cached_sub_tensor_slice_list_modules = []
for m in self.weight_quant.modules():
if hasattr(m, 'subtensor_slice_list'):
self._cached_sub_tensor_slice_list_modules.append(m)
m.subtensor_slice_list = subtensor_slice_list
# generate slices for the weight tensor based on the list passed in
weight_slice_tuple = tuple(
slice(*s) if s is not None else slice(s) for s in subtensor_slice_list)
Expand All @@ -91,9 +100,10 @@ def quant_weight(
out = self.weight_quant(self.weight[weight_slice_tuple])
if subtensor_slice_list is not None:
# Restore the quantizer behaviour to full tensor quantization
for m in self.weight_quant.modules():
if hasattr(m, 'subtensor_slice_list'):
m.setattr('subtensor_slice_list', None)
# The modules to slice should have been cached already at this point
assert self._cached_sub_tensor_slice_list_modules is not None, "Missing cache of modules to slice."
for m in self._cached_sub_tensor_slice_list_modules:
m.subtensor_slice_list = None
return out

def int_weight(self, float_datatype=False):
Expand Down