Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into const_quantization
- Loading branch information
Showing
12 changed files
with
261 additions
and
13 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
model_compression_toolkit/core/common/substitutions/remove_identity.py
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright 2024 Sony Semiconductor Israel, Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
|
||
from model_compression_toolkit.core.common.graph.base_graph import Graph | ||
from model_compression_toolkit.core.common.graph.base_node import BaseNode | ||
|
||
|
||
def remove_identity_node(graph: Graph, | ||
node: BaseNode) -> Graph: | ||
""" | ||
The method to perform the substitution of the identity node by | ||
reconnecting its input directly to its output, effectively removing the node | ||
from the graph. | ||
Args: | ||
graph: The current graph of operations where the node resides. | ||
node: The specific `BaseNode` that is matched to be an Identity operation. | ||
Returns: | ||
Graph: The updated graph after removing the identity node. | ||
""" | ||
# Retrieve the predecessor nodes of the identity node. | ||
prev_identity_nodes = graph.get_prev_nodes(node) | ||
# Ensure there is exactly one predecessor; otherwise, do nothing. | ||
if len(prev_identity_nodes) != 1: | ||
return graph | ||
|
||
# Reconnect the output edges of the identity node to its predecessor, | ||
# effectively bypassing the identity node. | ||
graph.reconnect_out_edges(current_node=node, new_node=prev_identity_nodes[0]) | ||
# Remove the edge from the predecessor to the identity node. | ||
graph.remove_edge(prev_identity_nodes[0], node) | ||
# Remove the identity node from the graph. | ||
graph.remove_node(node_to_remove=node) | ||
|
||
return graph |
51 changes: 51 additions & 0 deletions
51
model_compression_toolkit/core/keras/graph_substitutions/substitutions/remove_identity.py
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright 2024 Sony Semiconductor Israel, Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
|
||
import keras | ||
import tensorflow as tf | ||
|
||
from model_compression_toolkit.core.common.graph.graph_matchers import NodeOperationMatcher | ||
from model_compression_toolkit.core import common | ||
from model_compression_toolkit.core.common.graph.base_graph import Graph | ||
from model_compression_toolkit.core.common.graph.base_node import BaseNode | ||
from model_compression_toolkit.core.common.substitutions.remove_identity import remove_identity_node | ||
|
||
|
||
class RemoveIdentity(common.BaseSubstitution): | ||
""" | ||
Remove Identity layers from the graph. | ||
""" | ||
|
||
def __init__(self): | ||
nodes = NodeOperationMatcher(keras.layers.Identity) | NodeOperationMatcher(tf.identity) | ||
super().__init__(matcher_instance=nodes) | ||
|
||
def substitute(self, | ||
graph: Graph, | ||
node: BaseNode) -> Graph: | ||
""" | ||
The method to perform the substitution of the identity keras node by | ||
reconnecting its input directly to its output, effectively removing the node | ||
from the graph. | ||
Args: | ||
graph: The current graph of operations where the node resides. | ||
node: The specific `BaseNode` that is matched to be an Identity operation. | ||
Returns: | ||
Graph: The updated graph after removing the identity node. | ||
""" | ||
return remove_identity_node(graph, node) | ||
|
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
50 changes: 50 additions & 0 deletions
50
model_compression_toolkit/core/pytorch/graph_substitutions/substitutions/remove_identity.py
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright 2024 Sony Semiconductor Israel, Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
import torch | ||
|
||
from model_compression_toolkit.core.common.substitutions.remove_identity import remove_identity_node | ||
from model_compression_toolkit.core.common.graph.graph_matchers import NodeOperationMatcher | ||
from model_compression_toolkit.core import common | ||
from model_compression_toolkit.core.common.graph.base_graph import Graph | ||
from model_compression_toolkit.core.common.graph.base_node import BaseNode | ||
|
||
|
||
class RemoveIdentity(common.BaseSubstitution): | ||
""" | ||
Remove `torch.nn.Identity` layers from the graph. | ||
""" | ||
|
||
def __init__(self): | ||
nodes = NodeOperationMatcher(torch.nn.Identity) | ||
super().__init__(matcher_instance=nodes) | ||
|
||
def substitute(self, | ||
graph: Graph, | ||
node: BaseNode) -> Graph: | ||
""" | ||
The method to perform the substitution of the `torch.nn.Identity` node by | ||
reconnecting its input directly to its output, effectively removing the node | ||
from the graph. | ||
Args: | ||
graph: The current graph of operations where the node resides. | ||
node: The specific `BaseNode` that is matched to be an Identity operation. | ||
Returns: | ||
Graph: The updated graph after removing the identity node. | ||
""" | ||
return remove_identity_node(graph, node) | ||
|
||
|
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
35 changes: 35 additions & 0 deletions
35
tests/keras_tests/feature_networks_tests/feature_networks/remove_identity_test.py
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright 2024 Sony Semiconductor Israel, Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
from tests.keras_tests.feature_networks_tests.base_keras_feature_test import BaseKerasFeatureNetworkTest | ||
import keras | ||
import tensorflow as tf | ||
|
||
class RemoveIdentityTest(BaseKerasFeatureNetworkTest): | ||
def __init__(self, unit_test): | ||
super().__init__(unit_test) | ||
|
||
def create_networks(self): | ||
inputs = keras.layers.Input(shape=self.get_input_shapes()[0][1:]) | ||
x = keras.layers.Conv2D(3, 3)(inputs) | ||
x = keras.layers.Identity()(x) | ||
x = tf.identity(x) | ||
outputs = keras.layers.BatchNormalization()(x) | ||
return keras.Model(inputs=inputs, outputs=outputs) | ||
|
||
def compare(self, quantized_model, float_model, input_x=None, quantization_info=None): | ||
# Make sure identity and bn layers are not in the final model. | ||
# there should be 4 layers: input, input_quantizer, conv, conv_quantizer | ||
self.unit_test.assertTrue(len(quantized_model.layers)==4) | ||
|
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
50 changes: 50 additions & 0 deletions
50
tests/pytorch_tests/model_tests/feature_models/remove_identity_test.py
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright 2024 Sony Semiconductor Israel, Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
import torch | ||
|
||
from tests.pytorch_tests.model_tests.base_pytorch_feature_test import BasePytorchFeatureNetworkTest | ||
|
||
|
||
class RemoveIdentityNet(torch.nn.Module): | ||
def __init__(self): | ||
super(RemoveIdentityNet, self).__init__() | ||
self.conv1 = torch.nn.Conv2d(3, 3, kernel_size=1, stride=1) | ||
self.identity = torch.nn.Identity() | ||
self.bn1 = torch.nn.BatchNorm2d(3) | ||
|
||
def forward(self, x): | ||
x = self.conv1(x) | ||
x = self.identity(x) | ||
x = self.bn1(x) | ||
return x | ||
|
||
|
||
class RemoveIdentityTest(BasePytorchFeatureNetworkTest): | ||
|
||
def __init__(self, unit_test): | ||
super().__init__(unit_test) | ||
|
||
def create_networks(self): | ||
return RemoveIdentityNet() | ||
|
||
def compare(self, | ||
quantized_model, | ||
float_model, | ||
input_x=None, | ||
quantization_info=None): | ||
for n,m in quantized_model.named_modules(): | ||
# make sure identity was removed and bn was folded into the conv | ||
self.unit_test.assertFalse(isinstance(m, torch.nn.Identity) or isinstance(m, torch.nn.BatchNorm2d)) | ||
|
Oops, something went wrong.