|
| 1 | +import copy |
1 | 2 | import shutil
|
2 | 3 | import tempfile
|
3 | 4 | import unittest
|
4 | 5 |
|
5 |
| -import torch |
6 | 6 | from compressed_tensors import QUANTIZATION_CONFIG_NAME
|
7 | 7 | from compressed_tensors.compressors import ModelCompressor
|
8 | 8 | from compressed_tensors.quantization import QuantizationStatus
|
9 | 9 | from parameterized import parameterized_class
|
10 | 10 | from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
|
| 11 | +from transformers.utils.quantization_config import CompressedTensorsConfig |
11 | 12 |
|
12 | 13 | from tests.testing_utils import parse_params, requires_gpu
|
13 | 14 |
|
14 |
| -CONFIG_DIR = "tests/llmcompressor/transformers/compression/run_compressed_configs" |
| 15 | +CONFIG_DIR = "tests/llmcompressor/transformers/compression/decompression_configs" |
15 | 16 |
|
16 | 17 |
|
17 | 18 | @requires_gpu
|
18 | 19 | @parameterized_class(parse_params(CONFIG_DIR))
|
19 |
| -class TestQuantizationMatches(unittest.TestCase): |
20 |
| - model_stub = None |
21 |
| - empty_model = None |
| 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 | + ] |
22 | 40 |
|
23 | 41 | @classmethod
|
24 |
| - def setUpClass(cls): |
25 |
| - cls.test_dir = tempfile.mkdtemp() |
| 42 | + def setUpClass(self): |
| 43 | + self.test_dir = tempfile.mkdtemp() |
| 44 | + self.tokenizer = AutoTokenizer.from_pretrained(self.compressed_model_stub) |
26 | 45 |
|
27 |
| - # TODO: Give option on HFQuantizer to run run_compressed True/False |
28 |
| - # currently hardcoded to True |
29 |
| - cls.compressed_model = AutoModelForCausalLM.from_pretrained( |
30 |
| - cls.model_stub, |
| 46 | + # Decompress using HFQuantizer from AutoModelForCausalLM |
| 47 | + self.decompressed_model_hf_quantizer = AutoModelForCausalLM.from_pretrained( |
| 48 | + self.compressed_model_stub, |
31 | 49 | torch_dtype="auto",
|
32 | 50 | device_map="auto",
|
33 |
| - # run_compressed=True, # TODO: Give option on HFQuantizer |
| 51 | + quantization_config=CompressedTensorsConfig(run_compressed=False), |
34 | 52 | )
|
35 |
| - # TODO: Use ModelCompressor until decompression is supported through |
36 |
| - # HFQuant/run_compressed can be turned off. |
37 |
| - cls.uncompressed_model = AutoModelForCausalLM.from_pretrained( |
38 |
| - cls.empty_model, |
39 |
| - torch_dtype=cls.compressed_model.dtype, |
40 |
| - device_map=cls.compressed_model.device, |
| 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" |
41 | 70 | )
|
42 |
| - config = AutoConfig.from_pretrained(cls.model_stub) |
| 71 | + |
| 72 | + config = AutoConfig.from_pretrained(self.compressed_model_stub) |
| 73 | + |
43 | 74 | compression_config = getattr(config, QUANTIZATION_CONFIG_NAME, None)
|
44 |
| - cls.compressor = ModelCompressor.from_compression_config(compression_config) |
45 |
| - cls.compressor.quantization_config.quantization_status = ( |
| 75 | + self.compressor = ModelCompressor.from_compression_config(compression_config) |
| 76 | + self.compressor.quantization_config.quantization_status = ( |
46 | 77 | QuantizationStatus.FROZEN
|
47 | 78 | )
|
48 |
| - cls.compressor.decompress( |
49 |
| - model_path=cls.model_stub, model=cls.uncompressed_model |
| 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, |
50 | 87 | )
|
51 | 88 |
|
52 |
| - cls.tokenizer = AutoTokenizer.from_pretrained(cls.model_stub) |
| 89 | + # self.dense_model should be decompressed |
| 90 | + assert dense_model is not self.dense_model |
53 | 91 |
|
54 |
| - def test_compressed_matches_uncompressed(self): |
55 |
| - SAMPLE_INPUT = [ |
56 |
| - "I love 4-bit quantization because", |
57 |
| - "What is the capital of France?", |
58 |
| - "def fibonacci(n):", |
59 |
| - ] |
| 92 | + self.decompressed_model_manual = self.dense_model |
60 | 93 |
|
61 |
| - inputs = self.tokenizer(SAMPLE_INPUT, return_tensors="pt", padding=True).to( |
62 |
| - self.compressed_model.device |
| 94 | + assert hasattr( |
| 95 | + self.decompressed_model_manual.model.layers[0].self_attn.q_proj, |
| 96 | + "weight_scale", |
63 | 97 | )
|
64 |
| - compressed_output = self.tokenizer.batch_decode( |
65 |
| - self.compressed_model.generate(**inputs, max_length=50) |
| 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 |
66 | 105 | )
|
67 |
| - uncompressed_output = self.tokenizer.batch_decode( |
68 |
| - self.uncompressed_model.generate(**inputs, max_length=50) |
| 106 | + self.decompressed_model_hf_quantizer = self.decompressed_model_hf_quantizer.to( |
| 107 | + decompressed_model_hf_quantizer |
69 | 108 | )
|
70 | 109 |
|
71 |
| - for idx in range(len(SAMPLE_INPUT)): |
72 |
| - assert compressed_output[idx] == uncompressed_output[idx] |
| 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 | + ) |
73 | 127 |
|
74 | 128 | @classmethod
|
75 |
| - def tearDownClass(cls): |
76 |
| - shutil.rmtree(cls.test_dir) |
77 |
| - del cls.compressed_model |
78 |
| - del cls.uncompressed_model |
79 |
| - torch.cuda.empty_cache() |
| 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