mct-nightly 2.2.0.20240916.525__py3-none-any.whl → 2.2.0.20240918.448__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {mct_nightly-2.2.0.20240916.525.dist-info → mct_nightly-2.2.0.20240918.448.dist-info}/METADATA +1 -1
- {mct_nightly-2.2.0.20240916.525.dist-info → mct_nightly-2.2.0.20240918.448.dist-info}/RECORD +30 -20
- {mct_nightly-2.2.0.20240916.525.dist-info → mct_nightly-2.2.0.20240918.448.dist-info}/top_level.txt +1 -0
- model_compression_toolkit/__init__.py +1 -1
- model_compression_toolkit/core/common/graph/base_node.py +3 -0
- model_compression_toolkit/core/common/graph/functional_node.py +1 -1
- model_compression_toolkit/core/keras/back2framework/keras_model_builder.py +1 -1
- model_compression_toolkit/core/keras/reader/node_builder.py +23 -1
- model_compression_toolkit/core/pytorch/back2framework/pytorch_model_builder.py +5 -1
- model_compression_toolkit/core/pytorch/reader/graph_builders.py +13 -4
- model_compression_toolkit/exporter/model_wrapper/keras/builder/fully_quantized_model_builder.py +12 -3
- model_compression_toolkit/exporter/model_wrapper/pytorch/builder/fully_quantized_model_builder.py +10 -1
- model_compression_toolkit/gptq/__init__.py +17 -5
- model_compression_toolkit/gptq/common/gptq_config.py +88 -75
- model_compression_toolkit/gptq/pytorch/gptq_training.py +18 -9
- model_compression_toolkit/gptq/pytorch/quantization_facade.py +49 -29
- model_compression_toolkit/gptq/pytorch/quantizer/gradual_activation_quantization.py +80 -0
- model_compression_toolkit/gptq/pytorch/quantizer/regularization_factory.py +10 -10
- model_compression_toolkit/gptq/pytorch/quantizer/soft_rounding/soft_quantizer_reg.py +6 -49
- model_compression_toolkit/trainable_infrastructure/pytorch/annealing_schedulers.py +39 -0
- model_compression_toolkit/trainable_infrastructure/pytorch/util.py +29 -0
- tests_pytest/__init__.py +14 -0
- tests_pytest/pytorch/__init__.py +14 -0
- tests_pytest/pytorch/gptq/__init__.py +14 -0
- tests_pytest/pytorch/gptq/test_annealing_cfg.py +40 -0
- tests_pytest/pytorch/gptq/test_gradual_act_quantization.py +100 -0
- tests_pytest/pytorch/trainable_infrastructure/__init__.py +14 -0
- tests_pytest/pytorch/trainable_infrastructure/test_linear_annealing.py +49 -0
- {mct_nightly-2.2.0.20240916.525.dist-info → mct_nightly-2.2.0.20240918.448.dist-info}/LICENSE.md +0 -0
- {mct_nightly-2.2.0.20240916.525.dist-info → mct_nightly-2.2.0.20240918.448.dist-info}/WHEEL +0 -0
@@ -0,0 +1,100 @@
|
|
1
|
+
# Copyright 2024 Sony Semiconductor Israel, 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 unittest.mock import Mock
|
16
|
+
|
17
|
+
import pytest
|
18
|
+
import torch
|
19
|
+
|
20
|
+
from model_compression_toolkit.core.pytorch.pytorch_device_config import get_working_device
|
21
|
+
from model_compression_toolkit.trainable_infrastructure.pytorch.annealing_schedulers import LinearAnnealingScheduler
|
22
|
+
from model_compression_toolkit.gptq import GradientPTQConfig, GradualActivationQuantizationConfig, QFractionLinearAnnealingConfig
|
23
|
+
from model_compression_toolkit.gptq.pytorch.quantizer.gradual_activation_quantization import (
|
24
|
+
GradualActivationQuantizerWrapper, get_gradual_activation_quantizer_wrapper_factory)
|
25
|
+
|
26
|
+
|
27
|
+
@pytest.fixture
|
28
|
+
def x():
|
29
|
+
return torch.randn((2, 5, 6, 7), generator=torch.Generator().manual_seed(42)).to(device=get_working_device())
|
30
|
+
|
31
|
+
|
32
|
+
class Quantizer:
|
33
|
+
def __call__(self, x, training):
|
34
|
+
self.training = training
|
35
|
+
return 3*x + 1
|
36
|
+
|
37
|
+
|
38
|
+
class TestGradualActivationQuantization:
|
39
|
+
|
40
|
+
def test_gradual_act_quant_wrapper(self, x):
|
41
|
+
quantizer = Quantizer()
|
42
|
+
qw = GradualActivationQuantizerWrapper(quantizer, q_fraction_scheduler=lambda t: t / (t + 1))
|
43
|
+
|
44
|
+
y0, y1, y2 = [qw(x) for _ in range(3)]
|
45
|
+
assert torch.equal(y0, x) # t=0
|
46
|
+
assert torch.allclose(y1, 0.5 * x + (1.5 * x + 0.5)) # t=1
|
47
|
+
assert torch.allclose(y2, x / 3 + (2 * x + 2 / 3)) # t=2
|
48
|
+
assert quantizer.training is True
|
49
|
+
|
50
|
+
_ = qw(x, False)
|
51
|
+
assert quantizer.training is False # correct flag was propagated
|
52
|
+
|
53
|
+
def test_factory_no_qdrop(self):
|
54
|
+
quantizer_wrapper, quantizer = self._run_factory_test(qdrop_cfg=None, get_grad_steps_fn=None)
|
55
|
+
assert quantizer_wrapper is quantizer
|
56
|
+
|
57
|
+
@pytest.mark.parametrize('end_step', (20, None))
|
58
|
+
def test_factory_linear(self, x, end_step):
|
59
|
+
qdrop_cfg = GradualActivationQuantizationConfig(
|
60
|
+
QFractionLinearAnnealingConfig(initial_q_fraction=0.3, target_q_fraction=0.8, start_step=10, end_step=end_step)
|
61
|
+
)
|
62
|
+
|
63
|
+
def get_total_steps():
|
64
|
+
if end_step is None:
|
65
|
+
return 50
|
66
|
+
assert False # should not be called if end_step is passed
|
67
|
+
|
68
|
+
quantizer_wrapper, quantizer = self._run_factory_test(qdrop_cfg, get_total_steps)
|
69
|
+
|
70
|
+
scheduler = quantizer_wrapper.q_fraction_scheduler
|
71
|
+
assert isinstance(scheduler, LinearAnnealingScheduler)
|
72
|
+
exp_end_step = 50 if end_step is None else end_step
|
73
|
+
assert scheduler.t_start == 10
|
74
|
+
assert scheduler.t_end == exp_end_step
|
75
|
+
assert scheduler.initial_val == 0.3
|
76
|
+
assert scheduler.target_val == 0.8
|
77
|
+
|
78
|
+
y = [quantizer_wrapper(x) for _ in range(exp_end_step+1)]
|
79
|
+
assert torch.allclose(y[9], 0.7 * x + 0.3 * quantizer(x, True))
|
80
|
+
assert torch.allclose(y[10], 0.7 * x + 0.3 * quantizer(x, True))
|
81
|
+
assert torch.allclose(y[-1], 0.2 * x + 0.8 * quantizer(x, True))
|
82
|
+
|
83
|
+
def test_factory_linear_common_case(self, x):
|
84
|
+
# validate that we actually implemented the right thing - on first call float input, on last call fully quantized
|
85
|
+
qdrop_cfg = GradualActivationQuantizationConfig(
|
86
|
+
QFractionLinearAnnealingConfig(initial_q_fraction=0, target_q_fraction=1, start_step=0, end_step=None)
|
87
|
+
)
|
88
|
+
quantizer_wrapper, quantizer = self._run_factory_test(qdrop_cfg, lambda: 15)
|
89
|
+
y0, *_, y_last = [quantizer_wrapper(x) for _ in range(16)]
|
90
|
+
assert torch.equal(y0, x)
|
91
|
+
assert torch.allclose(y_last, quantizer(x, True))
|
92
|
+
|
93
|
+
def _run_factory_test(self, qdrop_cfg, get_grad_steps_fn):
|
94
|
+
# Mocks are used to just pass anything
|
95
|
+
gptq_cfg = GradientPTQConfig(n_epochs=5, optimizer=Mock(), loss=Mock(),
|
96
|
+
gradual_activation_quantization_config=qdrop_cfg)
|
97
|
+
factory = get_gradual_activation_quantizer_wrapper_factory(gptq_cfg, get_grad_steps_fn)
|
98
|
+
quantizer = Quantizer()
|
99
|
+
quantizer_wrapper = factory(quantizer)
|
100
|
+
return quantizer_wrapper, quantizer
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# Copyright 2024 Sony Semiconductor Israel, 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
|
+
# ==============================================================================
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# Copyright 2024 Sony Semiconductor Israel, 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 torch
|
16
|
+
import pytest
|
17
|
+
|
18
|
+
from model_compression_toolkit.trainable_infrastructure.pytorch.annealing_schedulers import LinearAnnealingScheduler
|
19
|
+
|
20
|
+
|
21
|
+
def test_linear_annealing():
|
22
|
+
scheduler = LinearAnnealingScheduler(t_start=10, t_end=35, initial_val=3.4, target_val=-1.6)
|
23
|
+
for t in [0, 9, 10]:
|
24
|
+
assert _isclose(scheduler(t), 3.4)
|
25
|
+
|
26
|
+
for t in [35, 36, 1000]:
|
27
|
+
assert _isclose(scheduler(t), -1.6)
|
28
|
+
|
29
|
+
assert _isclose(scheduler(11), 3.2)
|
30
|
+
assert _isclose(scheduler(27), 0.)
|
31
|
+
assert _isclose(scheduler(34), -1.4)
|
32
|
+
|
33
|
+
|
34
|
+
def test_linear_annealing_ascending():
|
35
|
+
scheduler = LinearAnnealingScheduler(t_start=0, t_end=5, initial_val=-0.5, target_val=1.5)
|
36
|
+
assert _isclose(scheduler(0), -0.5)
|
37
|
+
assert _isclose(scheduler(1), -0.1)
|
38
|
+
assert _isclose(scheduler(4), 1.1)
|
39
|
+
assert _isclose(scheduler(5), 1.5)
|
40
|
+
|
41
|
+
|
42
|
+
@pytest.mark.parametrize('start', [5, -1])
|
43
|
+
def test_invalid(start):
|
44
|
+
with pytest.raises(ValueError):
|
45
|
+
LinearAnnealingScheduler(t_start=start, t_end=4, initial_val=1, target_val=0)
|
46
|
+
|
47
|
+
|
48
|
+
def _isclose(x, y):
|
49
|
+
return torch.isclose(x, torch.tensor(y))
|
{mct_nightly-2.2.0.20240916.525.dist-info → mct_nightly-2.2.0.20240918.448.dist-info}/LICENSE.md
RENAMED
File without changes
|
File without changes
|