|
| 1 | +import unittest |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import torch |
| 5 | +from torch.testing._internal.common_utils import TestCase |
| 6 | + |
| 7 | +from torchao._models.llama.model import ( |
| 8 | + ModelArgs, |
| 9 | + Transformer, |
| 10 | + prepare_inputs_for_model, |
| 11 | +) |
| 12 | +from torchao._models.llama.tokenizer import get_tokenizer |
| 13 | +from torchao.quantization import Int4WeightOnlyConfig, quantize_ |
| 14 | +from torchao.quantization.utils import compute_error |
| 15 | +from torchao.utils import ( |
| 16 | + TORCH_VERSION_AT_LEAST_2_4, |
| 17 | +) |
| 18 | + |
| 19 | +torch.manual_seed(0) |
| 20 | + |
| 21 | + |
| 22 | +class TestGPTQ(TestCase): |
| 23 | + @unittest.skip("skipping until we get checkpoints for gpt-fast") |
| 24 | + def test_gptq_quantizer_int4_weight_only(self): |
| 25 | + from torchao._models._eval import ( |
| 26 | + LMEvalInputRecorder, |
| 27 | + TransformerEvalWrapper, |
| 28 | + ) |
| 29 | + from torchao.prototype.GPTQ.GPTQ import Int4WeightOnlyGPTQQuantizer |
| 30 | + |
| 31 | + precision = torch.bfloat16 |
| 32 | + device = "cuda" |
| 33 | + checkpoint_path = Path( |
| 34 | + "../../checkpoints/meta-llama/Llama-2-7b-chat-hf/model.pth" |
| 35 | + ) |
| 36 | + model = Transformer.from_name(checkpoint_path.parent.name) |
| 37 | + checkpoint = torch.load(str(checkpoint_path), mmap=True, weights_only=True) |
| 38 | + model.load_state_dict(checkpoint, assign=True) |
| 39 | + model = model.to(dtype=precision, device="cpu") |
| 40 | + model.eval() |
| 41 | + |
| 42 | + tokenizer_path = checkpoint_path.parent / "tokenizer.model" |
| 43 | + assert tokenizer_path.is_file(), tokenizer_path |
| 44 | + tokenizer = get_tokenizer( # pyre-ignore[28] |
| 45 | + tokenizer_path, |
| 46 | + "Llama-2-7b-chat-hf", |
| 47 | + ) |
| 48 | + groupsize = 64 |
| 49 | + blocksize = 128 |
| 50 | + percdamp = 0.01 |
| 51 | + calibration_tasks = ["wikitext"] |
| 52 | + calibration_limit = 1 |
| 53 | + calibration_seq_length = 100 |
| 54 | + input_prep_func = prepare_inputs_for_model |
| 55 | + pad_calibration_inputs = False |
| 56 | + inputs = ( |
| 57 | + LMEvalInputRecorder( |
| 58 | + tokenizer, |
| 59 | + calibration_seq_length, |
| 60 | + input_prep_func, |
| 61 | + model.config.vocab_size, |
| 62 | + pad_calibration_inputs, |
| 63 | + device="cpu", |
| 64 | + ) |
| 65 | + .record_inputs( |
| 66 | + calibration_tasks, |
| 67 | + calibration_limit, |
| 68 | + ) |
| 69 | + .get_inputs() |
| 70 | + ) |
| 71 | + |
| 72 | + quantizer = Int4WeightOnlyGPTQQuantizer( |
| 73 | + groupsize, |
| 74 | + blocksize, |
| 75 | + percdamp, |
| 76 | + ) |
| 77 | + model.setup_caches(max_batch_size=1, max_seq_length=calibration_seq_length) |
| 78 | + |
| 79 | + model = quantizer.quantize(model, *inputs).cuda() |
| 80 | + |
| 81 | + model.reset_caches() |
| 82 | + with torch.device("cuda"): |
| 83 | + model.setup_caches(max_batch_size=1, max_seq_length=model.config.block_size) |
| 84 | + |
| 85 | + limit = 1 |
| 86 | + result = TransformerEvalWrapper( |
| 87 | + model.cuda(), |
| 88 | + tokenizer, |
| 89 | + model.config.block_size, |
| 90 | + prepare_inputs_for_model, |
| 91 | + device, |
| 92 | + ).run_eval( |
| 93 | + ["wikitext"], |
| 94 | + limit, |
| 95 | + ) |
| 96 | + |
| 97 | + assert result["results"]["wikitext"]["word_perplexity,none"] < 7.77, ( |
| 98 | + f"accuracy regressed from 7.76 to {result['results']['wikitext']['word_perplexity,none']}" |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +class TestMultiTensorFlow(TestCase): |
| 103 | + @unittest.skipIf(not TORCH_VERSION_AT_LEAST_2_4, "Test only enabled for 2.4+") |
| 104 | + @unittest.skipIf(not torch.cuda.is_available(), "Need CUDA available") |
| 105 | + def test_multitensor_add_tensors(self): |
| 106 | + from torchao.prototype.GPTQ.GPTQ import MultiTensor |
| 107 | + |
| 108 | + tensor1 = torch.randn(3, 3) |
| 109 | + tensor2 = torch.randn(3, 3) |
| 110 | + mt = MultiTensor(tensor1) |
| 111 | + mt.add_tensors(tensor2) |
| 112 | + self.assertEqual(mt.count, 2) |
| 113 | + self.assertTrue(torch.equal(mt.values[0], tensor1)) |
| 114 | + self.assertTrue(torch.equal(mt.values[1], tensor2)) |
| 115 | + |
| 116 | + @unittest.skipIf(not TORCH_VERSION_AT_LEAST_2_4, "Test only enabled for 2.4+") |
| 117 | + @unittest.skipIf(not torch.cuda.is_available(), "Need CUDA available") |
| 118 | + def test_multitensor_pad_unpad(self): |
| 119 | + from torchao.prototype.GPTQ.GPTQ import MultiTensor |
| 120 | + |
| 121 | + tensor1 = torch.randn(3, 3) |
| 122 | + mt = MultiTensor(tensor1) |
| 123 | + mt.pad_to_length(3) |
| 124 | + self.assertEqual(mt.count, 3) |
| 125 | + mt.unpad() |
| 126 | + self.assertEqual(mt.count, 1) |
| 127 | + |
| 128 | + @unittest.skipIf(not TORCH_VERSION_AT_LEAST_2_4, "Test only enabled for 2.4+") |
| 129 | + @unittest.skipIf(not torch.cuda.is_available(), "Need CUDA available") |
| 130 | + def test_multitensor_inplace_operation(self): |
| 131 | + from torchao.prototype.GPTQ.GPTQ import MultiTensor |
| 132 | + |
| 133 | + tensor1 = torch.ones(3, 3) |
| 134 | + mt = MultiTensor(tensor1) |
| 135 | + mt += 1 # In-place addition |
| 136 | + self.assertTrue(torch.equal(mt.values[0], torch.full((3, 3), 2))) |
| 137 | + |
| 138 | + |
| 139 | +class TestMultiTensorInputRecorder(TestCase): |
| 140 | + def test_multitensor_input_recorder(self): |
| 141 | + from torchao.prototype.GPTQ.GPTQ import MultiTensor, MultiTensorInputRecorder |
| 142 | + |
| 143 | + input_recorder = MultiTensorInputRecorder() |
| 144 | + in1 = ([1], torch.randn(3, 3), (1, "dog", torch.randn(3, 3)), torch.float) |
| 145 | + in2 = ([1], torch.randn(3, 3), (1, "dog", torch.randn(3, 3)), torch.float) |
| 146 | + |
| 147 | + input_recorder(*in1) |
| 148 | + input_recorder(*in2) |
| 149 | + |
| 150 | + MT_input = input_recorder.get_recorded_inputs() |
| 151 | + |
| 152 | + self.assertEqual(MT_input[0], [1]) |
| 153 | + self.assertTrue(isinstance(MT_input[1], MultiTensor)) |
| 154 | + self.assertTrue(isinstance(MT_input[2], tuple)) |
| 155 | + self.assertEqual(MT_input[2][0], 1) |
| 156 | + self.assertEqual(MT_input[2][1], "dog") |
| 157 | + self.assertTrue(isinstance(MT_input[2][2], MultiTensor)) |
| 158 | + self.assertEqual(MT_input[3], torch.float) |
| 159 | + |
| 160 | + def test_gptq_with_input_recorder(self): |
| 161 | + from torchao.prototype.GPTQ.GPTQ import ( |
| 162 | + Int4WeightOnlyGPTQQuantizer, |
| 163 | + MultiTensorInputRecorder, |
| 164 | + ) |
| 165 | + |
| 166 | + torch.set_default_dtype(torch.bfloat16) |
| 167 | + |
| 168 | + config = ModelArgs(n_layer=2) |
| 169 | + |
| 170 | + with torch.device("cuda"): |
| 171 | + model = Transformer(config) |
| 172 | + model.setup_caches(max_batch_size=2, max_seq_length=100) |
| 173 | + idx = torch.randint(1, 10000, (10, 2, 50)).to(torch.int32) |
| 174 | + test_input = prepare_inputs_for_model(idx[0]) |
| 175 | + import copy |
| 176 | + |
| 177 | + model2 = copy.deepcopy(model) |
| 178 | + out = model(*test_input) |
| 179 | + quantize_(model2, Int4WeightOnlyConfig()) |
| 180 | + |
| 181 | + outq = model2(*test_input) |
| 182 | + del model2 |
| 183 | + |
| 184 | + input_recorder = MultiTensorInputRecorder() |
| 185 | + for i in range(10): |
| 186 | + input = prepare_inputs_for_model(idx[i]) |
| 187 | + input_recorder(*input) |
| 188 | + |
| 189 | + multi_tensor_inputs = input_recorder.get_recorded_inputs() |
| 190 | + |
| 191 | + quantizer = Int4WeightOnlyGPTQQuantizer() |
| 192 | + |
| 193 | + quantizer.quantize(model, multi_tensor_inputs) |
| 194 | + |
| 195 | + outgptq = model(*test_input) |
| 196 | + |
| 197 | + self.assertGreater(compute_error(outgptq, out), 30) |
| 198 | + self.assertGreater(compute_error(outgptq, out), compute_error(outq, out)) |
| 199 | + |
| 200 | + |
| 201 | +if __name__ == "__main__": |
| 202 | + unittest.main() |
0 commit comments