Skip to content

Commit eacba53

Browse files
Disable weight quantization for Sin, Cos, and Exp (#1647)
1 parent ef85725 commit eacba53

4 files changed

Lines changed: 117 additions & 6 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,9 @@ def generate_tpc(default_config: OpQuantizationConfig,
342342
gelu = schema.OperatorsSet(name=schema.OperatorSetNames.GELU, qc_options=default_config_options_16bit)
343343
tanh = schema.OperatorsSet(name=schema.OperatorSetNames.TANH, qc_options=default_config_options_16bit)
344344
hard_tanh = schema.OperatorsSet(name=schema.OperatorSetNames.HARD_TANH, qc_options=default_config_options_16bit)
345-
exp = schema.OperatorsSet(name=schema.OperatorSetNames.EXP, qc_options=const_configuration_options_inout16)
346-
sin = schema.OperatorsSet(name=schema.OperatorSetNames.SIN, qc_options=const_configuration_options_inout16)
347-
cos = schema.OperatorsSet(name=schema.OperatorSetNames.COS, qc_options=const_configuration_options_inout16)
345+
exp = schema.OperatorsSet(name=schema.OperatorSetNames.EXP, qc_options=default_config_options_16bit)
346+
sin = schema.OperatorsSet(name=schema.OperatorSetNames.SIN, qc_options=default_config_options_16bit)
347+
cos = schema.OperatorsSet(name=schema.OperatorSetNames.COS, qc_options=default_config_options_16bit)
348348

349349
operator_set.extend(
350350
[conv, conv_transpose, depthwise_conv, fc, relu, relu6, leaky_relu, add, sub, mul, div, prelu, swish, hardswish,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ def generate_tpc(default_config: OpQuantizationConfig,
346346
gelu = schema.OperatorsSet(name=schema.OperatorSetNames.GELU, qc_options=default_config_options_16bit)
347347
tanh = schema.OperatorsSet(name=schema.OperatorSetNames.TANH, qc_options=default_config_options_16bit)
348348
hard_tanh = schema.OperatorsSet(name=schema.OperatorSetNames.HARD_TANH, qc_options=default_config_options_16bit)
349-
exp = schema.OperatorsSet(name=schema.OperatorSetNames.EXP, qc_options=const_configuration_options_inout16)
350-
sin = schema.OperatorsSet(name=schema.OperatorSetNames.SIN, qc_options=const_configuration_options_inout16)
351-
cos = schema.OperatorsSet(name=schema.OperatorSetNames.COS, qc_options=const_configuration_options_inout16)
349+
exp = schema.OperatorsSet(name=schema.OperatorSetNames.EXP, qc_options=default_config_options_16bit)
350+
sin = schema.OperatorsSet(name=schema.OperatorSetNames.SIN, qc_options=default_config_options_16bit)
351+
cos = schema.OperatorsSet(name=schema.OperatorSetNames.COS, qc_options=default_config_options_16bit)
352352

353353
operator_set.extend(
354354
[conv, conv_transpose, depthwise_conv, fc, relu, relu6, leaky_relu, add, sub, mul, div, prelu, swish, hardswish,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2026 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+
import pytest
16+
from model_compression_toolkit import get_target_platform_capabilities
17+
18+
19+
@pytest.mark.parametrize("tpc_version", [
20+
'5.0',
21+
'6.0',
22+
])
23+
def test_sin_cos_exp(tpc_version):
24+
25+
tpc = get_target_platform_capabilities(tpc_version=tpc_version)
26+
operators = [opset.name for opset in tpc.operator_set]
27+
assert 'Sin' in operators
28+
assert 'Cos' in operators
29+
assert 'Exp' in operators
30+
31+
for opset in tpc.operator_set:
32+
if opset.name in ['Sin', 'Cos', 'Exp']:
33+
for qc in opset.qc_options.quantization_configurations:
34+
assert qc.default_weight_attr_config.enable_weights_quantization == False
35+
assert qc.enable_activation_quantization == True
36+
assert qc.supported_input_activation_n_bits == (8, 16)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Copyright 2026 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+
import pytest
16+
from typing import Iterator, List
17+
import torch
18+
import torch.nn as nn
19+
import model_compression_toolkit as mct
20+
from mct_quantizers import PytorchQuantizationWrapper
21+
22+
23+
class Model(nn.Module):
24+
25+
def __init__(self, name):
26+
super().__init__()
27+
self.name = name
28+
29+
self.conv = nn.Conv2d(3, 3, kernel_size=3, padding=1)
30+
self.const = nn.Parameter(torch.ones([32, 32]))
31+
32+
def forward(self, x):
33+
x = self.conv(x)
34+
35+
if self.name == 'sin':
36+
y = torch.sin(self.const) * x
37+
elif self.name == 'cos':
38+
y = torch.cos(self.const) * x
39+
elif self.name == 'exp':
40+
y = torch.exp(self.const) * x
41+
42+
return y
43+
44+
45+
def get_representative_dataset(n_iter=1):
46+
47+
def representative_dataset() -> Iterator[List]:
48+
for _ in range(n_iter):
49+
yield [torch.randn(1, 3, 32, 32)]
50+
return representative_dataset
51+
52+
53+
@pytest.mark.parametrize("tpc_version", [
54+
'5.0',
55+
'6.0',
56+
])
57+
@pytest.mark.parametrize("layer", [
58+
'sin',
59+
'cos',
60+
'exp',
61+
])
62+
def test_constant_sin_cos_exp(tpc_version, layer):
63+
64+
weight_quantizers = []
65+
66+
float_model = Model(layer)
67+
tpc = mct.get_target_platform_capabilities(tpc_version=tpc_version)
68+
quantized_model, _ = mct.ptq.pytorch_post_training_quantization(float_model,
69+
get_representative_dataset(n_iter=1),
70+
target_platform_capabilities=tpc)
71+
72+
weight_quantizers.extend([name for name, module in quantized_model.named_modules() if isinstance(module, PytorchQuantizationWrapper)])
73+
74+
assert f'{layer}' not in weight_quantizers # Check that sin, cos, and exp layers do not have the weight quantizer
75+
assert hasattr(quantized_model, f'{layer}_activation_holder_quantizer')

0 commit comments

Comments
 (0)