1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
|
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
import shutil
from collections import OrderedDict
import pytest
import torch
from compressed_tensors import PackedQuantizationCompressor
from compressed_tensors.compressors.quantized_compressors.pack_quantized import (
pack_to_int32,
unpack_from_int32,
)
from compressed_tensors.quantization import (
QuantizationArgs,
QuantizationConfig,
QuantizationScheme,
QuantizationStatus,
QuantizationStrategy,
apply_quantization_config,
)
from compressed_tensors.quantization.lifecycle.forward import fake_quantize
from compressed_tensors.quantization.quant_args import ActivationOrdering
from safetensors.torch import save_file
from torch.nn.modules import Linear, Sequential
def get_dummy_quant_config(
num_bits=4, strategy=None, group_size=None, actorder=None, symmetric=True
):
config_groups = {
"group_1": QuantizationScheme(
targets=["Linear"],
weights=QuantizationArgs(
num_bits=num_bits,
strategy=strategy,
group_size=group_size,
actorder=actorder,
symmetric=symmetric,
),
),
}
return QuantizationConfig(config_groups=config_groups)
def make_dummy_g_idx(columns: int, group_size: int) -> torch.Tensor:
perm = torch.randperm(columns)
return torch.nn.Parameter(
(torch.arange(columns, dtype=torch.int) // group_size)[perm],
requires_grad=False,
)
@pytest.mark.parametrize(
"shape",
[
(512, 1024),
(830, 545),
(342, 512),
(256, 700),
],
)
def test_quant_format(shape):
dense_state_dict = {
"dummy.weight": torch.rand(shape),
"dummy.weight_scale": torch.tensor(0.01, dtype=torch.float32),
"dummy.weight_zero_point": torch.tensor(0, dtype=torch.int8),
}
quant_config = get_dummy_quant_config()
compressor = PackedQuantizationCompressor(config=quant_config)
quantized_modules_to_args = {"dummy": quant_config.config_groups["group_1"].weights}
compressed_state_dict = compressor.compress(
dense_state_dict, names_to_scheme=quantized_modules_to_args
)
# compressed state_dict adds one entry for shape
# but removes the zero points since we are symmetric
assert len(dense_state_dict) == len(compressed_state_dict)
# check compressed and packed
assert compressed_state_dict["dummy.weight_packed"].dtype == torch.int32
expected_rows = shape[0]
expected_columns = math.ceil(shape[1] / 8) # round each row up to nearest int32
assert compressed_state_dict["dummy.weight_packed"].shape == (
expected_rows,
expected_columns,
)
assert torch.equal(compressed_state_dict["dummy.weight_shape"], torch.tensor(shape))
assert compressed_state_dict["dummy.weight_scale"].dtype == torch.float32
@pytest.mark.parametrize(
"value",
[
torch.tensor([[1, 2], [3, 4]]),
torch.tensor([[1, 2, 3, 4, 5, 6, 7, 0], [-1, -2, -3, -4, -5, -6, -7, -8]]),
(torch.rand((32, 100)) * 16 - 8),
],
)
def test_repack_4bit(value):
value = value.to(torch.int8)
shape = value.shape
assert not torch.any(value > 7).item()
assert not torch.any(value < -8).item()
packed = pack_to_int32(value, 4)
unpacked = unpack_from_int32(packed, 4, shape)
assert torch.equal(value, unpacked)
@pytest.mark.parametrize(
"value",
[
torch.tensor([[30, 40], [50, 60]]),
torch.tensor(
[[10, 15, 20, 25, 30, 35, 40, 45], [-10, -20, -30, -40, -50, -60, -70, -80]]
),
(torch.rand((32, 100)) * 256 - 128),
],
)
def test_repack_8bit(value):
value = value.to(torch.int8)
shape = value.shape
assert not torch.any(value > 127).item()
assert not torch.any(value < -128).item()
packed = pack_to_int32(value, 8)
unpacked = unpack_from_int32(packed, 8, shape)
assert torch.equal(value, unpacked)
@pytest.mark.parametrize("num_bits", [4, 8])
def test_reload_match(tmp_path, num_bits):
dense_state_dict = {
"dummy.weight": torch.rand((511, 350)),
"dummy.weight_scale": torch.tensor(0.01, dtype=torch.float32),
"dummy.weight_zero_point": torch.tensor(0, dtype=torch.int8),
"dummy2.weight": torch.rand((128, 280)),
"dummy2.weight_scale": torch.tensor(0.02, dtype=torch.float32),
"dummy2.weight_zero_point": torch.tensor(15, dtype=torch.int8),
}
# pack-compressor only needs the number of bits from the quant-args to decompress
# all other information is extracted from the compressed data directly
names_to_scheme = {
"dummy": QuantizationArgs(num_bits=num_bits),
"dummy2": QuantizationArgs(num_bits=num_bits),
}
quant_config = get_dummy_quant_config(num_bits, symmetric=False)
compressor = PackedQuantizationCompressor(config=quant_config)
quantized_modules_to_args = {
"dummy": quant_config.config_groups["group_1"].weights,
"dummy2": quant_config.config_groups["group_1"].weights,
}
compressed_state_dict = compressor.compress(
dense_state_dict, names_to_scheme=quantized_modules_to_args
)
save_file(compressed_state_dict, tmp_path / "model.safetensors")
reconstructed_dense_gen = compressor.decompress(
tmp_path, names_to_scheme=names_to_scheme
)
reconstructed_dense = {}
for name, value in reconstructed_dense_gen:
reconstructed_dense[name] = value
fake_quant_dummy = fake_quantize(
dense_state_dict["dummy.weight"],
scale=dense_state_dict["dummy.weight_scale"],
zero_point=dense_state_dict["dummy.weight_zero_point"],
args=quantized_modules_to_args["dummy"],
)
assert torch.equal(
fake_quant_dummy, reconstructed_dense["dummy"].get("weight").to(torch.float32)
)
fake_quant_dummy2 = fake_quantize(
dense_state_dict["dummy2.weight"],
scale=dense_state_dict["dummy2.weight_scale"],
zero_point=dense_state_dict["dummy2.weight_zero_point"],
args=quantized_modules_to_args["dummy2"],
)
assert torch.equal(
fake_quant_dummy2, reconstructed_dense["dummy2"].get("weight").to(torch.float32)
)
shutil.rmtree(tmp_path)
@pytest.mark.parametrize(
"strategy",
{QuantizationStrategy.GROUP, QuantizationStrategy.CHANNEL},
)
def test_asymmetric_packed_support(strategy):
shape = (1024, 1024)
group_size = None
if strategy == QuantizationStrategy.GROUP:
group_size = 128
if strategy == QuantizationStrategy.CHANNEL:
expected_shape = (shape[0], 1)
elif strategy == QuantizationStrategy.GROUP:
num_groups = shape[1] // group_size
expected_shape = (shape[0], max(num_groups, 1))
dense_state_dict = {
"dummy.weight": torch.rand(shape),
"dummy.weight_scale": torch.rand(expected_shape).to(torch.float32),
"dummy.weight_zero_point": torch.rand(expected_shape).to(torch.int8),
}
quant_config = get_dummy_quant_config(
strategy=strategy.value, symmetric=False, group_size=group_size
)
compressor = PackedQuantizationCompressor(config=quant_config)
quantized_modules_to_args = {"dummy": quant_config.config_groups["group_1"].weights}
compressed_state_dict = compressor.compress(
dense_state_dict, names_to_scheme=quantized_modules_to_args
)
# compressed state_dict adds one entry for shape
assert len(dense_state_dict) + 1 == len(compressed_state_dict)
assert compressed_state_dict["dummy.weight_packed"].dtype == torch.int32
assert compressed_state_dict["dummy.weight_zero_point"].dtype == torch.int32
assert compressed_state_dict["dummy.weight_scale"].dtype == torch.float32
# check weight compressed and packed
expected_rows = shape[0]
expected_columns = math.ceil(shape[1] / 8) # round each row up to nearest int32
assert compressed_state_dict["dummy.weight_packed"].shape == (
expected_rows,
expected_columns,
)
assert torch.equal(compressed_state_dict["dummy.weight_shape"], torch.tensor(shape))
# check zp compressed and packed
packed_size_zp = math.ceil(shape[0] / 8)
zp_factor = group_size if strategy == QuantizationStrategy.GROUP else shape[-1]
assert compressed_state_dict["dummy.weight_zero_point"].shape == (
packed_size_zp,
shape[-1] // zp_factor,
)
@pytest.mark.parametrize(
"actorder",
[
ActivationOrdering.GROUP,
ActivationOrdering.WEIGHT,
None,
],
)
def test_actorder_reload_match(actorder, tmp_path, mock_per_group_calibration):
model = Sequential(OrderedDict([("dummy", Linear(512, 1024, bias=None))]))
group_size = 128
quant_config = get_dummy_quant_config(
strategy="group", group_size=group_size, actorder=actorder
)
apply_quantization_config(model, quant_config)
# run calibration
model.quantization_status = QuantizationStatus.CALIBRATION
mock_per_group_calibration(
model.dummy, base_name="weight", value=model.dummy.weight, group_size=group_size
)
# apply gptq
if actorder == ActivationOrdering.GROUP:
init_g_idx = make_dummy_g_idx(512, group_size)
model.dummy.register_parameter("weight_g_idx", init_g_idx)
# compress
compressor = PackedQuantizationCompressor(config=quant_config)
quantized_modules_to_args = {
"dummy": quant_config.config_groups["group_1"].weights,
}
compressed_state_dict = compressor.compress(
model.state_dict(), names_to_scheme=quantized_modules_to_args
)
save_file(compressed_state_dict, tmp_path / "model.safetensors")
# decompress
reconstructed_dense_gen = compressor.decompress(
tmp_path, names_to_scheme=quantized_modules_to_args
)
reconstructed_dense = {}
for name, value in reconstructed_dense_gen:
reconstructed_dense[name] = value
fake_quant_dummy = fake_quantize(
model.dummy.weight,
scale=model.dummy.weight_scale,
zero_point=model.dummy.weight_zero_point,
g_idx=getattr(model.dummy, "weight_g_idx", None),
args=quantized_modules_to_args["dummy"],
)
assert torch.equal(fake_quant_dummy, reconstructed_dense["dummy"].get("weight"))
shutil.rmtree(tmp_path)
@pytest.mark.parametrize(
"num_bits,values,expected_values",
[
(
4,
torch.tensor([[1]]),
torch.tensor([[9]], dtype=torch.int32),
),
(
8,
torch.tensor([[1]]),
torch.tensor([[129]], dtype=torch.int32),
),
# 0000 0000 0000 0000 1100 1011 1010 1001
(4, torch.tensor([[1, 2, 3, 4]]), torch.tensor([[52137]], dtype=torch.int32)),
# 0111 0110 0101 0100 0011 0010 0001 0000
(
4,
torch.tensor([[-8, -7, -6, -5, -4, -3, -2, -1]]),
torch.tensor([[1985229328]], dtype=torch.int32),
),
# 10000100 10000011 10000010 10000001
(
8,
torch.tensor([[1, 2, 3, 4]]),
torch.tensor([[-2071756159]], dtype=torch.int32),
),
# 00000011 00000010 00000001 00000000
(
8,
torch.tensor([[-128, -127, -126, -125]]),
torch.tensor([[50462976]], dtype=torch.int32),
),
(
4,
torch.tensor([[-8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4]]),
torch.tensor([[1985229328, 52137]], dtype=torch.int32),
),
(
4,
torch.tensor(
[
[-8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, -8, -8, -8, -8],
[1, 2, 3, 4, -8, -8, -8, -8, -8, -7, -6, -5, -4, -3, -2, -1],
]
),
torch.tensor([[1985229328, 52137], [52137, 1985229328]], dtype=torch.int32),
),
(
8,
torch.tensor(
[
[1, 2, 3, 4],
[-128, -127, -126, -125],
]
),
torch.tensor([[-2071756159], [50462976]], dtype=torch.int32),
),
(
8,
torch.tensor(
[
[1, 2, 3, 4, -128, -127, -126, -125],
[-128, -127, -126, -125, 1, 2, 3, 4],
]
),
torch.tensor(
[[-2071756159, 50462976], [50462976, -2071756159]], dtype=torch.int32
),
),
],
)
def test_pack_to_int32(num_bits, values, expected_values):
values = values.to(torch.int8)
packed_values = pack_to_int32(values, num_bits)
assert torch.equal(packed_values, expected_values)
assert packed_values.dtype == expected_values.dtype
@pytest.mark.parametrize(
"num_bits,values,expected_tensor",
[
(
4,
torch.tensor([[9]], dtype=torch.int32),
torch.tensor([[1]], dtype=torch.int8),
),
(
8,
torch.tensor([[129]], dtype=torch.int32),
torch.tensor([[1]], dtype=torch.int8),
),
(
4,
torch.tensor([[52137]], dtype=torch.int32),
torch.tensor([[1, 2, 3, 4]], dtype=torch.int8),
),
(
4,
torch.tensor([[1985229328]], dtype=torch.int32),
torch.tensor([[-8, -7, -6, -5, -4, -3, -2, -1]], dtype=torch.int8),
),
(
8,
torch.tensor([[-2071756159]], dtype=torch.int32),
torch.tensor([[1, 2, 3, 4]], dtype=torch.int8),
),
(
8,
torch.tensor([[50462976]], dtype=torch.int32),
torch.tensor([[-128, -127, -126, -125]], dtype=torch.int8),
),
(
4,
torch.tensor([[1985229328, 52137]], dtype=torch.int32),
torch.tensor(
[[-8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4]], dtype=torch.int8
),
),
(
4,
torch.tensor([[1985229328, 52137], [52137, 1985229328]], dtype=torch.int32),
torch.tensor(
[
[-8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, -8, -8, -8, -8],
[1, 2, 3, 4, -8, -8, -8, -8, -8, -7, -6, -5, -4, -3, -2, -1],
],
dtype=torch.int8,
),
),
(
8,
torch.tensor([[-2071756159], [50462976]], dtype=torch.int32),
torch.tensor(
[
[1, 2, 3, 4],
[-128, -127, -126, -125],
],
dtype=torch.int8,
),
),
(
8,
torch.tensor(
[[-2071756159, 50462976], [50462976, -2071756159]], dtype=torch.int32
),
torch.tensor(
[
[1, 2, 3, 4, -128, -127, -126, -125],
[-128, -127, -126, -125, 1, 2, 3, 4],
],
dtype=torch.int8,
),
),
],
)
def test_unpack_from_int32(num_bits, values, expected_tensor):
unpacked_tensor = unpack_from_int32(values, num_bits, expected_tensor.shape)
assert torch.equal(unpacked_tensor, unpacked_tensor)
assert unpacked_tensor.dtype == unpacked_tensor.dtype
|