Skip to content

Commit 27e42fb

Browse files
[SSS Review] Stack bugfix, Delete nightly release, and Update version (#1523)
1 parent f04e64a commit 27e42fb

File tree

6 files changed

+152
-45
lines changed

6 files changed

+152
-45
lines changed

.github/workflows/nightly.yml

Lines changed: 0 additions & 42 deletions
This file was deleted.

model_compression_toolkit/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
from model_compression_toolkit import pruning
2828
from model_compression_toolkit.trainable_infrastructure.keras.load_model import keras_load_quantized_model
2929

30-
__version__ = "2.4.2"
30+
__version__ = "2.4.3"

model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v1/tpc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,11 @@ def generate_tpc(default_config: OpQuantizationConfig,
166166
operator_set = []
167167
fusing_patterns = []
168168

169+
activation_quantization_config = (default_configuration_options.clone_and_edit_weight_attribute(enable_weights_quantization=False))
170+
operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.STACK, qc_options=activation_quantization_config))
171+
169172
no_quantization_config = (default_configuration_options.clone_and_edit(enable_activation_quantization=False)
170173
.clone_and_edit_weight_attribute(enable_weights_quantization=False))
171-
172-
operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.STACK, qc_options=no_quantization_config))
173174
operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.UNSTACK, qc_options=no_quantization_config))
174175
operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.DROPOUT, qc_options=no_quantization_config))
175176
operator_set.append(schema.OperatorsSet(name=schema.OperatorSetNames.FLATTEN, qc_options=no_quantization_config))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2025 Sony Semiconductor Solutions, Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ==============================================================================
15+
from model_compression_toolkit.target_platform_capabilities.tpc_models.imx500_tpc.v1.tpc import get_tpc as get_tpc_imx500_v1
16+
17+
18+
def test_tpc_stack():
19+
20+
tpc = get_tpc_imx500_v1() # only imx500 supported
21+
assert 'Stack' in [opset.name for opset in tpc.operator_set]
22+
23+
for opset in tpc.operator_set:
24+
if opset.name == 'Stack':
25+
for qc in opset.qc_options.quantization_configurations:
26+
assert qc.default_weight_attr_config.enable_weights_quantization == False
27+
assert qc.attr_weights_configs_mapping == {}
28+
assert qc.enable_activation_quantization == True
29+
assert qc.activation_n_bits == 8
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2025 Sony Semiconductor Solutions, Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ==============================================================================
15+
from typing import Iterator, List
16+
import numpy as np
17+
import tensorflow as tf
18+
from tensorflow.keras import layers
19+
import keras
20+
import model_compression_toolkit as mct
21+
from mct_quantizers import KerasActivationQuantizationHolder
22+
23+
24+
def get_model():
25+
26+
inputs = keras.layers.Input((32, 32, 3))
27+
out1 = layers.Conv2D(16, kernel_size=3, padding='same', activation='relu')(inputs)
28+
out2 = layers.Conv2D(16, kernel_size=3, padding='same', activation='relu')(inputs)
29+
outputs = tf.stack([out1, out2], -1)
30+
return keras.Model(inputs, outputs)
31+
32+
33+
def get_representative_dataset(n_iter=1):
34+
35+
def representative_dataset() -> Iterator[List]:
36+
for _ in range(n_iter):
37+
yield [np.random.randn(1, 32, 32, 3)]
38+
return representative_dataset
39+
40+
41+
def test_stack():
42+
43+
model = get_model()
44+
tpc = mct.get_target_platform_capabilities('tensorflow', 'imx500') # only imx500 supported
45+
q_model, _ = mct.ptq.keras_post_training_quantization(model,
46+
get_representative_dataset(n_iter=1),
47+
target_resource_utilization=None,
48+
core_config=mct.core.CoreConfig(),
49+
target_platform_capabilities=tpc)
50+
51+
assert getattr(q_model.layers[-2], "function") is tf.stack
52+
53+
stack_activation_holder = q_model.layers[-1] # activation holder for stack layer
54+
assert isinstance(stack_activation_holder, KerasActivationQuantizationHolder)
55+
assert stack_activation_holder.activation_holder_quantizer.num_bits == 8
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright 2025 Sony Semiconductor Solutions, Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ==============================================================================
15+
from typing import Iterator, List
16+
import torch
17+
import torch.nn as nn
18+
import model_compression_toolkit as mct
19+
from mct_quantizers import PytorchActivationQuantizationHolder
20+
21+
22+
def get_model():
23+
24+
class StackModel(nn.Module):
25+
def __init__(self):
26+
super().__init__()
27+
self.conv1 = nn.Sequential(
28+
nn.Conv2d(3, 16, kernel_size=3, padding=1),
29+
nn.ReLU()
30+
)
31+
self.conv2 = nn.Sequential(
32+
nn.Conv2d(3, 16, kernel_size=3, padding=1),
33+
nn.ReLU()
34+
)
35+
36+
def forward(self, x):
37+
out1 = self.conv1(x)
38+
out2 = self.conv2(x)
39+
output = torch.stack([out1, out2], dim=-1)
40+
return output
41+
return StackModel()
42+
43+
44+
def get_representative_dataset(n_iter=1):
45+
46+
def representative_dataset() -> Iterator[List]:
47+
for _ in range(n_iter):
48+
yield [torch.randn(1, 3, 32, 32)]
49+
return representative_dataset
50+
51+
52+
def test_stack():
53+
54+
model = get_model()
55+
tpc = mct.get_target_platform_capabilities('pytorch', 'imx500') # only imx500 supported
56+
q_model, _ = mct.ptq.pytorch_post_training_quantization(model,
57+
get_representative_dataset(n_iter=1),
58+
target_resource_utilization=None,
59+
core_config=mct.core.CoreConfig(),
60+
target_platform_capabilities=tpc)
61+
62+
assert hasattr(q_model, 'stack_activation_holder_quantizer') # activation holder for stack layer
63+
assert isinstance(q_model.stack_activation_holder_quantizer, PytorchActivationQuantizationHolder)
64+
assert q_model.stack_activation_holder_quantizer.activation_holder_quantizer.num_bits == 8

0 commit comments

Comments
 (0)