|
10 | 10 | import tempfile |
11 | 11 | import unittest |
12 | 12 |
|
| 13 | +import torch |
| 14 | + |
13 | 15 | from datasets import load_dataset |
14 | 16 | from models.model_test import ModelTest |
15 | 17 | from parameterized import parameterized |
16 | 18 | from transformers import AutoTokenizer |
17 | 19 |
|
18 | 20 | from gptqmodel.nn_modules.qlinear.qqq import QQQLinear |
19 | 21 | from gptqmodel.quantization import FORMAT, METHOD, QUANT_CONFIG_FILENAME |
| 22 | +from gptqmodel.quantization.qqq import QQQ |
20 | 23 | from gptqmodel.utils.torch import torch_empty_cache |
21 | 24 |
|
22 | 25 |
|
@@ -107,3 +110,57 @@ def assert_qqq_linear(self, model): |
107 | 110 | has_qqq = True |
108 | 111 | break |
109 | 112 | self.assertTrue(has_qqq) |
| 113 | + |
| 114 | + |
| 115 | +class TestQQQHessian(unittest.TestCase): |
| 116 | + """Verify QQQ Hessian accumulation matches the closed-form reference.""" |
| 117 | + |
| 118 | + def _reference_hessian(self, *tensors): |
| 119 | + """Compute 2/N * (X^T X) for one or more (B, S, C) activation tensors.""" |
| 120 | + target = tensors[0].device |
| 121 | + x = torch.cat([t.to(target) for t in tensors], dim=0).reshape(-1, tensors[0].shape[-1]).float() |
| 122 | + return (2.0 / x.shape[0]) * x.t().matmul(x) |
| 123 | + |
| 124 | + def test_single_batch_matches_reference(self): |
| 125 | + torch.manual_seed(42) |
| 126 | + layer = torch.nn.Linear(16, 8, dtype=torch.float32) |
| 127 | + qqq = QQQ(layer) |
| 128 | + x = torch.randn(4, 8, 16, dtype=torch.float32) |
| 129 | + qqq.add_batch(x, None) |
| 130 | + H = qqq.materialize_hessian() |
| 131 | + H_ref = self._reference_hessian(x) |
| 132 | + self.assertTrue(torch.allclose(H, H_ref, rtol=1e-4, atol=1e-5)) |
| 133 | + |
| 134 | + def test_multiple_batches_matches_reference(self): |
| 135 | + torch.manual_seed(42) |
| 136 | + layer = torch.nn.Linear(16, 8, dtype=torch.float32) |
| 137 | + qqq = QQQ(layer) |
| 138 | + batches = [torch.randn(2, 8, 16, dtype=torch.float32) for _ in range(3)] |
| 139 | + for x in batches: |
| 140 | + qqq.add_batch(x, None) |
| 141 | + H = qqq.materialize_hessian() |
| 142 | + H_ref = self._reference_hessian(*batches) |
| 143 | + self.assertTrue(torch.allclose(H, H_ref, rtol=1e-4, atol=1e-5)) |
| 144 | + |
| 145 | + def test_multi_device_matches_reference(self): |
| 146 | + if not torch.cuda.is_available(): |
| 147 | + self.skipTest("CUDA not available") |
| 148 | + torch.manual_seed(42) |
| 149 | + layer = torch.nn.Linear(16, 8, dtype=torch.float32, device="cpu") |
| 150 | + qqq = QQQ(layer) |
| 151 | + x_cpu = torch.randn(2, 8, 16, dtype=torch.float32, device="cpu") |
| 152 | + x_gpu = torch.randn(2, 8, 16, dtype=torch.float32, device="cuda:0") |
| 153 | + qqq.add_batch(x_cpu, None) |
| 154 | + qqq.add_batch(x_gpu, None) |
| 155 | + H = qqq.materialize_hessian() |
| 156 | + H_ref = self._reference_hessian(x_cpu, x_gpu) |
| 157 | + self.assertTrue(torch.allclose(H, H_ref, rtol=1e-4, atol=1e-5)) |
| 158 | + |
| 159 | + def test_empty_batch_is_noop(self): |
| 160 | + torch.manual_seed(42) |
| 161 | + layer = torch.nn.Linear(16, 8, dtype=torch.float32) |
| 162 | + qqq = QQQ(layer) |
| 163 | + qqq.add_batch(torch.zeros(0, 8, 16, dtype=torch.float32), None) |
| 164 | + H = qqq.materialize_hessian() |
| 165 | + self.assertEqual(H.shape, (16, 16)) |
| 166 | + self.assertTrue(torch.allclose(H, torch.zeros_like(H))) |
0 commit comments