|
| 1 | +# Copyright 2025 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 numpy as np |
| 16 | +import pytest |
| 17 | + |
| 18 | +from model_compression_toolkit.core.common.graph.virtual_activation_weights_node import VirtualActivationWeightsNode |
| 19 | +from tests_pytest._test_util.graph_builder_utils import build_node, DummyLayer, build_nbits_qc |
| 20 | + |
| 21 | + |
| 22 | +class DummyLayerWKernel: |
| 23 | + pass |
| 24 | + |
| 25 | + |
| 26 | +class TestVirtualActivationWeightsNode: |
| 27 | + # TODO tests only cover combining weights from activation and weight nodes and errors. |
| 28 | + def test_activation_with_weights(self, fw_info_mock): |
| 29 | + """ Tests that weights from activation and weight node are combined correctly. """ |
| 30 | + # Each node has a unique weight attr and a unique positional weights. In addition, both nodes have |
| 31 | + # an identical canonical attribute (but different full name), and an identical positional weight. |
| 32 | + # All weights have different quantization. |
| 33 | + a_node = build_node('a', layer_class=DummyLayer, |
| 34 | + final_weights={'aaweightaa': np.ones((3, 14)), 'foo': np.ones(15), |
| 35 | + 1: np.ones(15), 2: np.ones((5, 9))}, |
| 36 | + qcs=[build_nbits_qc(a_nbits=5, |
| 37 | + w_attr={'aaweightaa': (2, True), 'foo': (3, True)}, |
| 38 | + pos_attr=(4, True, [1, 2]), |
| 39 | + convert_canonical_attr=False)]) |
| 40 | + w_node = build_node('w', layer_class=DummyLayerWKernel, |
| 41 | + final_weights={'wwweightww': np.ones((2, 71)), 'bar': np.ones(8), |
| 42 | + 1: np.ones(28), 3: np.ones(18)}, |
| 43 | + qcs=[build_nbits_qc(a_nbits=6, |
| 44 | + w_attr={'wwweightww': (5, True), 'bar': (6, True)}, |
| 45 | + pos_attr=(7, True, [1, 3]), |
| 46 | + convert_canonical_attr=False)]) |
| 47 | + |
| 48 | + fw_info_mock.get_kernel_op_attributes = lambda nt: ['weight'] if nt is DummyLayerWKernel else [None] |
| 49 | + |
| 50 | + v_node = VirtualActivationWeightsNode(a_node, w_node, fw_info_mock) |
| 51 | + assert len(v_node.weights) == 8 |
| 52 | + |
| 53 | + assert len(w_node.weights) == len(a_node.weights) == 4 |
| 54 | + # weights from weight node are unchanged |
| 55 | + for k, v in w_node.weights.items(): |
| 56 | + assert np.array_equal(v_node.weights.pop(k), v) |
| 57 | + # unique weights from activation node are unchanged |
| 58 | + assert np.array_equal(v_node.weights.pop('foo'), a_node.weights['foo']) |
| 59 | + assert np.array_equal(v_node.weights.pop(2), a_node.weights[2]) |
| 60 | + # duplicate positional weight |
| 61 | + assert np.array_equal(v_node.weights.pop(101), a_node.weights[1]) |
| 62 | + # duplicate weight attribute |
| 63 | + [(new_attr, w)] = v_node.weights.items() |
| 64 | + assert 'weight' not in new_attr |
| 65 | + assert np.array_equal(w, a_node.weights['aaweightaa']) |
| 66 | + |
| 67 | + assert len(v_node.candidates_quantization_cfg) == 1 |
| 68 | + v_qc = v_node.candidates_quantization_cfg[0] |
| 69 | + v_attr_cfg = v_qc.weights_quantization_cfg.attributes_config_mapping |
| 70 | + v_pos_cfg = v_qc.weights_quantization_cfg.pos_attributes_config_mapping |
| 71 | + a_qc = a_node.candidates_quantization_cfg[0] |
| 72 | + w_qc = w_node.candidates_quantization_cfg[0] |
| 73 | + |
| 74 | + assert v_attr_cfg == { |
| 75 | + 'wwweightww': w_qc.weights_quantization_cfg.attributes_config_mapping['wwweightww'], |
| 76 | + 'bar': w_qc.weights_quantization_cfg.attributes_config_mapping['bar'], |
| 77 | + 'foo': a_qc.weights_quantization_cfg.attributes_config_mapping['foo'], |
| 78 | + new_attr: a_qc.weights_quantization_cfg.attributes_config_mapping['aaweightaa'] |
| 79 | + } |
| 80 | + assert v_pos_cfg == { |
| 81 | + 1: w_qc.weights_quantization_cfg.pos_attributes_config_mapping[1], |
| 82 | + 101: a_qc.weights_quantization_cfg.pos_attributes_config_mapping[1], |
| 83 | + 2: a_qc.weights_quantization_cfg.pos_attributes_config_mapping[2], |
| 84 | + 3: w_qc.weights_quantization_cfg.pos_attributes_config_mapping[3] |
| 85 | + } |
| 86 | + |
| 87 | + def test_invalid_configurable_w_node_weight(self, fw_info_mock): |
| 88 | + w_node = build_node('w', layer_class=DummyLayerWKernel, |
| 89 | + canonical_weights={'kernel': np.ones(3), 'foo': np.ones(14)}, |
| 90 | + qcs=[ |
| 91 | + build_nbits_qc(w_attr={'kernel': (8, True), 'foo': (8, True)}), |
| 92 | + build_nbits_qc(w_attr={'kernel': (8, True), 'foo': (4, True)}) |
| 93 | + ]) |
| 94 | + a_node = build_node('a', qcs=[build_nbits_qc()]) |
| 95 | + |
| 96 | + fw_info_mock.get_kernel_op_attributes = lambda nt: ['kernel'] if nt is DummyLayerWKernel else [None] |
| 97 | + |
| 98 | + with pytest.raises(NotImplementedError, match='Only kernel weight can be configurable. Got configurable .*foo'): |
| 99 | + VirtualActivationWeightsNode(a_node, w_node, fw_info_mock) |
| 100 | + |
| 101 | + def test_invalid_a_node_configurable_weight(self, fw_info_mock): |
| 102 | + w_node = build_node('w', layer_class=DummyLayerWKernel, |
| 103 | + canonical_weights={'kernel': np.ones(3), 'foo': np.ones(14)}, |
| 104 | + qcs=[ |
| 105 | + build_nbits_qc(w_attr={'kernel': (8, True), 'foo': (8, True)}), |
| 106 | + build_nbits_qc(w_attr={'kernel': (4, True), 'foo': (8, True)}) |
| 107 | + ]) |
| 108 | + a_node = build_node('aaa', canonical_weights={'bar': np.ones(3), 'baz': np.ones(14)}, |
| 109 | + qcs=[ |
| 110 | + build_nbits_qc(w_attr={'bar': (8, True), 'baz': (8, True)}), |
| 111 | + build_nbits_qc(w_attr={'bar': (8, True), 'baz': (4, True)}) |
| 112 | + ]) |
| 113 | + fw_info_mock.get_kernel_op_attributes = lambda nt: ['kernel'] if nt is DummyLayerWKernel else [None] |
| 114 | + |
| 115 | + with pytest.raises(NotImplementedError, match='Node .*aaa with a configurable weight cannot be used as ' |
| 116 | + 'activation for VirtualActivationWeightsNode'): |
| 117 | + VirtualActivationWeightsNode(a_node, w_node, fw_info_mock) |
| 118 | + |
| 119 | + def test_invalid_a_node_kernel(self, fw_info_mock): |
| 120 | + w_node = build_node('w', layer_class=DummyLayerWKernel, canonical_weights={'weight': np.ones(3)}, |
| 121 | + qcs=[build_nbits_qc(w_attr={'weight': (8, True)})]) |
| 122 | + a_node = build_node('aaa', canonical_weights={'kernel': np.ones(3)}, |
| 123 | + qcs=[build_nbits_qc(w_attr={'kernel': (8, True)})]) |
| 124 | + fw_info_mock.get_kernel_op_attributes = lambda nt: ['weight'] if nt is DummyLayerWKernel else ['kernel'] |
| 125 | + |
| 126 | + with pytest.raises(NotImplementedError, match='Node .*aaa with kernel cannot be used as ' |
| 127 | + 'activation for VirtualActivationWeightsNode'): |
| 128 | + VirtualActivationWeightsNode(a_node, w_node, fw_info_mock) |
| 129 | + |
0 commit comments