|
| 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 torch |
| 17 | +from compressed_tensors.quantization.lifecycle import ( |
| 18 | + apply_quantization_config, |
| 19 | + freeze_module_quantization, |
| 20 | +) |
| 21 | +from compressed_tensors.quantization.quant_config import QuantizationConfig |
| 22 | +from transformers import AutoModelForCausalLM |
| 23 | + |
| 24 | + |
| 25 | +def test_apply_tinyllama_dynamic_activations(): |
| 26 | + quant_config = get_sample_dynamic_tinyllama_quant_config() |
| 27 | + model = get_tinyllama_model() |
| 28 | + |
| 29 | + # check that model is not already quantized |
| 30 | + for module in model.modules(): |
| 31 | + _test_layer_dynamic_quantization_status(module, inputs=False, weights=False) |
| 32 | + |
| 33 | + # apply quant config to model |
| 34 | + apply_quantization_config(model, quant_config) |
| 35 | + |
| 36 | + # test linears are dynamically quantized for calibration |
| 37 | + _test_linears_dynamic_quantization_status(model, quant_config, frozen=False) |
| 38 | + # verify forward works w/ dynamic during calibration |
| 39 | + model(torch.zeros((1, 1), dtype=int), torch.zeros((1, 1), dtype=int)) |
| 40 | + |
| 41 | + # freeze and test that only weight observers are deleted |
| 42 | + model.apply(freeze_module_quantization) |
| 43 | + _test_linears_dynamic_quantization_status(model, quant_config, frozen=True) |
| 44 | + # verify forward works w/ dynamic after freeze |
| 45 | + model(torch.zeros((1, 1), dtype=int), torch.zeros((1, 1), dtype=int)) |
| 46 | + |
| 47 | + |
| 48 | +def _test_linears_dynamic_quantization_status(model, quant_config, frozen: bool): |
| 49 | + # check for correct application of quant config |
| 50 | + num_linears = 0 |
| 51 | + for name, module in model.named_modules(): |
| 52 | + if name in quant_config.ignore: |
| 53 | + continue |
| 54 | + module_type = module.__class__.__name__ |
| 55 | + if module_type == "Linear": |
| 56 | + num_linears += 1 |
| 57 | + _test_layer_dynamic_quantization_status( |
| 58 | + module, inputs=True, weights=True, frozen=frozen |
| 59 | + ) |
| 60 | + |
| 61 | + # sanity check correct number of layers targeted |
| 62 | + assert num_linears == 154 # 155 Linear layers - 1 that gets ignored |
| 63 | + |
| 64 | + |
| 65 | +def _test_layer_dynamic_quantization_status( |
| 66 | + module, inputs: bool, weights: bool, frozen: bool = False |
| 67 | +): |
| 68 | + # check if quantization is applied at all (true if inputs or weights targeted) |
| 69 | + quantized = inputs or weights |
| 70 | + assert hasattr(module, "quantization_scheme") == quantized |
| 71 | + assert hasattr(module, "quantization_status") == quantized |
| 72 | + |
| 73 | + # check inputs always have an observer if quantized but never scale/zp |
| 74 | + assert not hasattr(module, "input_scale") |
| 75 | + assert not hasattr(module, "input_zero_point") |
| 76 | + assert hasattr(module, "input_observer") == inputs |
| 77 | + |
| 78 | + # check weights always have scale/zp and observer only if not frozen |
| 79 | + assert hasattr(module, "weight_scale") == weights |
| 80 | + assert hasattr(module, "weight_zero_point") == weights |
| 81 | + assert hasattr(module, "weight_observer") == (weights and not frozen) |
| 82 | + |
| 83 | + |
| 84 | +def get_tinyllama_model(): |
| 85 | + return AutoModelForCausalLM.from_pretrained( |
| 86 | + "TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T" |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +def get_sample_dynamic_tinyllama_quant_config(): |
| 91 | + config_dict = { |
| 92 | + "quant_method": "sparseml", |
| 93 | + "format": "fakequant", |
| 94 | + "quantization_status": "calibration", |
| 95 | + "global_compression_ratio": None, |
| 96 | + "config_groups": { |
| 97 | + "group_1": { |
| 98 | + "weights": { |
| 99 | + "num_bits": 8, |
| 100 | + "type": "int", |
| 101 | + "symmetric": True, |
| 102 | + "strategy": "tensor", |
| 103 | + "dynamic": False, |
| 104 | + }, |
| 105 | + "input_activations": { |
| 106 | + "num_bits": 8, |
| 107 | + "type": "int", |
| 108 | + "symmetric": True, |
| 109 | + "strategy": "tensor", |
| 110 | + "dynamic": True, |
| 111 | + }, |
| 112 | + "targets": ["Linear"], |
| 113 | + }, |
| 114 | + }, |
| 115 | + "ignore": ["LlamaRotaryEmbedding", "model.layers.1.mlp.down_proj"], |
| 116 | + } |
| 117 | + return QuantizationConfig.parse_obj(config_dict) |
0 commit comments