|
| 1 | +# Copyright (c) 2021 - present / Neuralmagic, 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, |
| 10 | +# software 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 | + |
| 16 | +import pytest |
| 17 | +from compressed_tensors.transforms.transform_args import ( |
| 18 | + ModuleTarget, |
| 19 | + TransformationArgs, |
| 20 | +) |
| 21 | +from compressed_tensors.transforms.transform_config import TransformationConfig |
| 22 | +from compressed_tensors.transforms.transform_scheme import TransformationScheme |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def basic_transform_scheme(): |
| 27 | + targets = ["Embedding"] |
| 28 | + module_targets = [ModuleTarget.INPUT_ACTIVATIONS] |
| 29 | + basic_args = TransformationArgs(targets=targets, module_targets=module_targets) |
| 30 | + |
| 31 | + scheme = TransformationScheme( |
| 32 | + transform_type="hadamard", |
| 33 | + groups=[basic_args], |
| 34 | + transform_creation_args={"size": 1024}, |
| 35 | + ) |
| 36 | + return scheme |
| 37 | + |
| 38 | + |
| 39 | +def test_basic(basic_transform_scheme): |
| 40 | + config = TransformationConfig( |
| 41 | + transform_groups={ |
| 42 | + "transform_0": basic_transform_scheme, |
| 43 | + } |
| 44 | + ) |
| 45 | + assert isinstance(config.transform_groups.get("transform_0"), TransformationScheme) |
| 46 | + |
| 47 | + |
| 48 | +def test_to_dict(basic_transform_scheme): |
| 49 | + config = TransformationConfig( |
| 50 | + transform_groups={ |
| 51 | + "transform_0": basic_transform_scheme, |
| 52 | + } |
| 53 | + ) |
| 54 | + config_dict = config.to_dict() |
| 55 | + assert "transform_groups" in config_dict.keys() |
| 56 | + |
| 57 | + |
| 58 | +def test_multiple_groups(): |
| 59 | + module_targets = [ModuleTarget.WEIGHT] |
| 60 | + |
| 61 | + targets_1 = ["model.layers.0.attn.v_proj"] |
| 62 | + linear_args_1 = TransformationArgs(targets=targets_1, module_targets=module_targets) |
| 63 | + |
| 64 | + targets_2 = ["model.layers.0.attn.q_proj"] |
| 65 | + linear_args_2 = TransformationArgs(targets=targets_2, module_targets=module_targets) |
| 66 | + |
| 67 | + scheme_1 = TransformationScheme( |
| 68 | + transform_type="hadamard", |
| 69 | + groups=[linear_args_1], |
| 70 | + transform_creation_args={"size": 1024}, |
| 71 | + ) |
| 72 | + |
| 73 | + scheme_2 = TransformationScheme( |
| 74 | + transform_type="hadamard", |
| 75 | + groups=[linear_args_2], |
| 76 | + transform_creation_args={"size": 256}, |
| 77 | + ) |
| 78 | + config = TransformationConfig( |
| 79 | + transform_groups={"transform_0": scheme_1, "transform_1": scheme_2} |
| 80 | + ) |
0 commit comments