-
Notifications
You must be signed in to change notification settings - Fork 49
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 with reuse layers in torch #1217
Open
reuvenperetz
wants to merge
4
commits into
sony:main
Choose a base branch
from
reuvenperetz:check-torch-reuse-issue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
# limitations under the License. | ||
# ============================================================================== | ||
import torch | ||
|
||
from mct_quantizers import PytorchQuantizationWrapper | ||
from tests.pytorch_tests.model_tests.base_pytorch_test import BasePytorchTest | ||
|
||
""" | ||
|
@@ -25,31 +27,12 @@ def __init__(self): | |
super(ReuseLayerNet, self).__init__() | ||
self.conv1 = torch.nn.Conv2d(3, 3, kernel_size=1, stride=1) | ||
self.conv2 = torch.nn.Conv2d(3, 3, kernel_size=1, stride=1) | ||
self.identity = torch.nn.Identity() | ||
|
||
def forward(self, x, y): | ||
x = self.conv1(x) | ||
x = self.identity(x) | ||
def forward(self, x): | ||
x = self.conv1(x) | ||
x = self.identity(x) | ||
x = self.conv1(x) | ||
x = self.identity(x) | ||
x = self.conv2(x) | ||
x = self.identity(x) | ||
x = self.conv2(x) | ||
x = self.identity(x) | ||
x = self.conv2(x) | ||
x = self.identity(x) | ||
y = self.conv2(y) | ||
y = self.identity(y) | ||
y = self.conv2(y) | ||
y = self.identity(y) | ||
y = self.conv1(y) | ||
y = self.identity(y) | ||
y = self.conv1(y) | ||
y = self.identity(y) | ||
y = self.conv1(y) | ||
return x - y, y - x | ||
x = self.conv1(x) | ||
return x | ||
|
||
|
||
class ReuseLayerNetTest(BasePytorchTest): | ||
|
@@ -61,7 +44,43 @@ def __init__(self, unit_test): | |
super().__init__(unit_test) | ||
|
||
def create_inputs_shape(self): | ||
return [[self.val_batch_size, 3, 32, 32], [self.val_batch_size, 3, 32, 32]] | ||
return [[self.val_batch_size, 3, 32, 32]] | ||
|
||
def create_feature_network(self, input_shape): | ||
return ReuseLayerNet() | ||
model = ReuseLayerNet() | ||
return model | ||
|
||
def compare(self, quantized_models, float_model, input_x=None, quantization_info=None): | ||
|
||
quant_model = quantized_models['all_4bit'] | ||
|
||
######################################################################################### | ||
|
||
# Verify that the shared parameters have identical memory addresses | ||
self.unit_test.assertEqual([p.data_ptr() for p in quant_model.conv1.parameters()], | ||
[p.data_ptr() for p in quant_model.conv1_1.parameters()], | ||
f"Shared parameters between reused layers should have identical memory addresses") | ||
|
||
######################################################################################### | ||
|
||
# Verify that 'conv1' is called twice (thus reused) and 'conv2' is called once | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can use defaultdict |
||
layer_calls = {} | ||
def hook_fn(module, input, output): | ||
layer_name = [name for name, layer in quant_model.named_modules() if layer is module][0] | ||
if layer_name not in layer_calls: | ||
layer_calls[layer_name] = 0 | ||
layer_calls[layer_name] += 1 | ||
|
||
# Register hooks | ||
hooks = [] | ||
for name, module in quant_model.named_modules(): | ||
if isinstance(module, PytorchQuantizationWrapper): | ||
hooks.append(module.register_forward_hook(hook_fn)) | ||
_ = quant_model(input_x) | ||
for hook in hooks: | ||
hook.remove() | ||
|
||
self.unit_test.assertEqual(layer_calls['conv1'], 2, "conv1 should be called twice") | ||
self.unit_test.assertEqual(layer_calls['conv2'], 1, "conv2 should be called once") | ||
|
||
######################################################################################### |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider adding another shared conv and validate it is different from the first one