|
| 1 | +import copy |
| 2 | +import shutil |
| 3 | +import tempfile |
| 4 | +import unittest |
| 5 | + |
| 6 | +from compressed_tensors import QUANTIZATION_CONFIG_NAME |
| 7 | +from compressed_tensors.compressors import ModelCompressor |
| 8 | +from compressed_tensors.quantization import QuantizationStatus |
| 9 | +from parameterized import parameterized_class |
| 10 | +from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer |
| 11 | +from transformers.utils.quantization_config import CompressedTensorsConfig |
| 12 | + |
| 13 | +from tests.testing_utils import parse_params, requires_gpu |
| 14 | + |
| 15 | +CONFIG_DIR = "tests/llmcompressor/transformers/compression/decompression_configs" |
| 16 | + |
| 17 | + |
| 18 | +@requires_gpu |
| 19 | +@parameterized_class(parse_params(CONFIG_DIR)) |
| 20 | +class TestDecompression(unittest.TestCase): |
| 21 | + """ |
| 22 | + Check that HFQuantizer decompression is working as expected. |
| 23 | + Manually decompress a compressed model and compare the generations |
| 24 | +
|
| 25 | + Decompression: |
| 26 | + Given a skeleton model and path to the optimized model, |
| 27 | + write the optimized model's safetensors to the skeleton model and decompress |
| 28 | + Ex. write weight_scale to the skeleton model and then convert from fp4 to fp16 |
| 29 | +
|
| 30 | + """ |
| 31 | + |
| 32 | + compressed_model_stub = None |
| 33 | + skeleton_model_stub = None |
| 34 | + |
| 35 | + SAMPLE_INPUTS = [ |
| 36 | + "I love 4-bit quantization because", |
| 37 | + "What is the capital of France?", |
| 38 | + "def fibonacci(n):", |
| 39 | + ] |
| 40 | + |
| 41 | + @classmethod |
| 42 | + def setUpClass(self): |
| 43 | + self.test_dir = tempfile.mkdtemp() |
| 44 | + self.tokenizer = AutoTokenizer.from_pretrained(self.compressed_model_stub) |
| 45 | + |
| 46 | + # Decompress using HFQuantizer from AutoModelForCausalLM |
| 47 | + self.decompressed_model_hf_quantizer = AutoModelForCausalLM.from_pretrained( |
| 48 | + self.compressed_model_stub, |
| 49 | + torch_dtype="auto", |
| 50 | + device_map="auto", |
| 51 | + quantization_config=CompressedTensorsConfig(run_compressed=False), |
| 52 | + ) |
| 53 | + |
| 54 | + # Manually decompress this model |
| 55 | + self.dense_model = AutoModelForCausalLM.from_pretrained( |
| 56 | + self.skeleton_model_stub, |
| 57 | + torch_dtype=self.decompressed_model_hf_quantizer.dtype, |
| 58 | + device_map=self.decompressed_model_hf_quantizer.device, |
| 59 | + ) |
| 60 | + |
| 61 | + # decompression from HFQuantizer should populate weight_scale |
| 62 | + assert hasattr( |
| 63 | + self.decompressed_model_hf_quantizer.model.layers[0].self_attn.q_proj, |
| 64 | + "weight_scale", |
| 65 | + ) |
| 66 | + |
| 67 | + # dense model should not have weight_scale populated |
| 68 | + assert not hasattr( |
| 69 | + self.dense_model.model.layers[0].self_attn.q_proj, "weight_scale" |
| 70 | + ) |
| 71 | + |
| 72 | + config = AutoConfig.from_pretrained(self.compressed_model_stub) |
| 73 | + |
| 74 | + compression_config = getattr(config, QUANTIZATION_CONFIG_NAME, None) |
| 75 | + self.compressor = ModelCompressor.from_compression_config(compression_config) |
| 76 | + self.compressor.quantization_config.quantization_status = ( |
| 77 | + QuantizationStatus.FROZEN |
| 78 | + ) |
| 79 | + |
| 80 | + # use the model_path to load the decompressed weights into dense_model |
| 81 | + dense_model = copy.deepcopy(self.dense_model) |
| 82 | + |
| 83 | + # overwrite the weights of the dense model |
| 84 | + self.compressor.decompress( |
| 85 | + model_path=self.compressed_model_stub, |
| 86 | + model=self.dense_model, |
| 87 | + ) |
| 88 | + |
| 89 | + # self.dense_model should be decompressed |
| 90 | + assert dense_model is not self.dense_model |
| 91 | + |
| 92 | + self.decompressed_model_manual = self.dense_model |
| 93 | + |
| 94 | + assert hasattr( |
| 95 | + self.decompressed_model_manual.model.layers[0].self_attn.q_proj, |
| 96 | + "weight_scale", |
| 97 | + ) |
| 98 | + |
| 99 | + def test_hf_quantizer_decompress_match_manual_decompress(self): |
| 100 | + manual_device = self.decompressed_model_manual.device |
| 101 | + decompressed_model_hf_quantizer = self.decompressed_model_hf_quantizer.device |
| 102 | + |
| 103 | + self.decompressed_model_manual = self.decompressed_model_manual.to( |
| 104 | + manual_device |
| 105 | + ) |
| 106 | + self.decompressed_model_hf_quantizer = self.decompressed_model_hf_quantizer.to( |
| 107 | + decompressed_model_hf_quantizer |
| 108 | + ) |
| 109 | + |
| 110 | + for input in self.SAMPLE_INPUTS: |
| 111 | + inputs = self.tokenizer(input, return_tensors="pt", padding=True).to( |
| 112 | + self.decompressed_model_manual.device |
| 113 | + ) |
| 114 | + inputs = inputs.to(self.decompressed_model_manual.device) |
| 115 | + |
| 116 | + decompressed_model_manual_output = self.tokenizer.batch_decode( |
| 117 | + self.decompressed_model_manual.generate(**inputs, max_length=50) |
| 118 | + ) |
| 119 | + |
| 120 | + decompressed_model_hf_quantizer_out = self.tokenizer.batch_decode( |
| 121 | + self.decompressed_model_hf_quantizer.generate(**inputs, max_length=50) |
| 122 | + ) |
| 123 | + |
| 124 | + assert ( |
| 125 | + decompressed_model_hf_quantizer_out == decompressed_model_manual_output |
| 126 | + ) |
| 127 | + |
| 128 | + @classmethod |
| 129 | + def tearDownClass(self): |
| 130 | + shutil.rmtree(self.test_dir) |
| 131 | + del self.dense_model |
| 132 | + del self.decompressed_model_hf_quantizer |
| 133 | + del self.decompressed_model_manual |
0 commit comments