|
| 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 shutil |
| 17 | +import unittest |
| 18 | + |
| 19 | +from sparseml.transformers.sparsification.sparse_model import SparseAutoModelForCausalLM |
| 20 | +from tests.testing_utils import requires_torch |
| 21 | + |
| 22 | + |
| 23 | +@requires_torch |
| 24 | +class TestGPTQOneShotWithFullScheme(unittest.TestCase): |
| 25 | + def setUp(self): |
| 26 | + import torch |
| 27 | + |
| 28 | + self.output = "./oneshot_output" |
| 29 | + self.model = "roneneldan/TinyStories-1M" |
| 30 | + self.dataset = "open_platypus" |
| 31 | + self.device = "cuda:0" if torch.cuda.is_available() else "cpu" |
| 32 | + |
| 33 | + self.recipe = """ |
| 34 | + first_stage: |
| 35 | + quant_modifiers: |
| 36 | + GPTQModifier: |
| 37 | + ignore: ["lm_head"] |
| 38 | + sequential_update: True |
| 39 | + dampening_frac: 0.001 |
| 40 | + block_size: 128 |
| 41 | + targets: ["Linear"] |
| 42 | + scheme: |
| 43 | + input_activations: null |
| 44 | + output_activations: null |
| 45 | + weights: |
| 46 | + num_bits: 8 |
| 47 | + type: "int" |
| 48 | + symmetric: true |
| 49 | + strategy: "tensor" |
| 50 | + group_size: 128 |
| 51 | + """ |
| 52 | + |
| 53 | + def test_oneshot_application(self): |
| 54 | + from sparseml.transformers import oneshot |
| 55 | + |
| 56 | + oneshot( |
| 57 | + model=self.model, |
| 58 | + dataset=self.dataset, |
| 59 | + output_dir=self.output, |
| 60 | + overwrite_output_dir=True, |
| 61 | + recipe=self.recipe, |
| 62 | + oneshot_device=self.device, |
| 63 | + num_calibration_samples=9, |
| 64 | + ) |
| 65 | + |
| 66 | + model_loaded = SparseAutoModelForCausalLM.from_pretrained(self.output) |
| 67 | + |
| 68 | + # Check that the model is quantized |
| 69 | + assert model_loaded.quantization_config is not None |
| 70 | + |
| 71 | + # Check a specific layer is quantized |
| 72 | + targetted_linear_layer = model_loaded.transformer.h[0].attn.attention.k_proj |
| 73 | + assert hasattr(targetted_linear_layer, "quantization_scheme") |
| 74 | + |
| 75 | + def tearDown(self): |
| 76 | + shutil.rmtree(self.output) |
0 commit comments