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
|
# mypy: allow-untyped-defs
import typing
import torch
__all__ = [
"ReferenceQuantizedModule",
]
class ReferenceQuantizedModule(torch.nn.Module):
def _init_weight_qparams(self, weight_qparams, device):
if weight_qparams is None:
weight_qparams = {
"qscheme": torch.per_tensor_affine,
"dtype": torch.quint8,
"scale": 1.0,
"zero_point": 0,
}
self.weight_qscheme: torch.qscheme = weight_qparams["qscheme"]
self.weight_dtype = weight_qparams["dtype"]
assert self.weight_qscheme in [
None,
torch.per_tensor_affine,
torch.per_channel_affine,
torch.per_channel_affine_float_qparams,
], f"qscheme: {self.weight_qscheme} is not support in reference quantized {self._get_name()}"
if self.weight_dtype in [
torch.quint8,
torch.qint8,
torch.quint4x2,
torch.qint32,
]:
zero_point_dtype = (
weight_qparams["zero_point"].dtype
if isinstance(weight_qparams["zero_point"], torch.Tensor)
else torch.int
)
w_scale = weight_qparams["scale"]
w_scale_tensor = (
w_scale.detach().clone()
if isinstance(w_scale, torch.Tensor)
else torch.tensor(w_scale, dtype=torch.float, device=device)
)
self.register_buffer("weight_scale", w_scale_tensor)
w_zp = weight_qparams["zero_point"]
w_zp_tensor = (
w_zp.detach().clone()
if isinstance(w_zp, torch.Tensor)
else torch.tensor(w_zp, dtype=zero_point_dtype, device=device)
)
self.register_buffer("weight_zero_point", w_zp_tensor)
if self.weight_qscheme in [
torch.per_channel_affine,
torch.per_channel_affine_float_qparams,
]:
w_axis = weight_qparams["axis"]
w_axis_tensor = (
w_axis.detach().clone()
if isinstance(w_axis, torch.Tensor)
else torch.tensor(w_axis, dtype=torch.int, device=device)
)
self.register_buffer("weight_axis", w_axis_tensor)
else:
# added for TorchScriptability, not used
self.register_buffer(
"weight_axis", torch.tensor(0, dtype=torch.int, device=device)
)
else:
# added for TorchScriptability, and for torch.float
self.register_buffer(
"weight_scale", torch.tensor(1.0, dtype=torch.float, device=device)
)
self.register_buffer(
"weight_zero_point", torch.tensor(0, dtype=torch.int, device=device)
)
self.register_buffer(
"weight_axis", torch.tensor(0, dtype=torch.int, device=device)
)
self.is_decomposed: bool = weight_qparams.get("is_decomposed", False)
# store weight_axis as weight_axis_int due to some constraints of torchdynamo.export
# for capturing `.item` operations
self.weight_axis_int: int = self.weight_axis.item() # type: ignore[operator, assignment]
self.weight_quant_min: typing.Optional[int] = weight_qparams.get(
"quant_min", None
)
self.weight_quant_max: typing.Optional[int] = weight_qparams.get(
"quant_max", None
)
def get_weight(self):
"""
Fake quantize (quantize and dequantize) the weight with
the quantization parameters for weight, this is used to
simulate the numerics for the quantized weight in a quantized
model
"""
# suppress mypy warning
assert isinstance(self.weight_scale, torch.Tensor)
assert isinstance(self.weight_zero_point, torch.Tensor)
if self.is_decomposed:
return _quantize_and_dequantize_weight_decomposed(
self.weight, # type: ignore[arg-type]
self.weight_qscheme,
self.weight_dtype,
self.weight_scale,
self.weight_zero_point,
self.weight_axis_int,
self.weight_quant_min,
self.weight_quant_max,
)
else:
return _quantize_and_dequantize_weight(
self.weight, # type: ignore[arg-type]
self.weight_qscheme,
self.weight_dtype,
self.weight_scale,
self.weight_zero_point,
self.weight_axis_int,
)
def get_quantized_weight(self):
# suppress mypy warning
assert isinstance(self.weight_scale, torch.Tensor)
assert isinstance(self.weight_zero_point, torch.Tensor)
# assert isinstance(self.weight_axis, torch.Tensor)
if self.is_decomposed:
return _quantize_weight_decomposed(
self.weight, # type: ignore[arg-type]
self.weight_qscheme,
self.weight_dtype,
self.weight_scale,
self.weight_zero_point,
self.weight_axis_int,
self.weight_quant_min,
self.weight_quant_max,
)
else:
return _quantize_weight(
self.weight, # type: ignore[arg-type]
self.weight_qscheme,
self.weight_dtype,
self.weight_scale,
self.weight_zero_point,
self.weight_axis_int,
)
def _save_to_state_dict(self, destination, prefix, keep_vars):
super()._save_to_state_dict(destination, prefix, keep_vars)
_save_weight_qparams(
destination,
prefix,
self.weight_qscheme,
self.weight_dtype,
self.weight_scale,
self.weight_zero_point,
self.weight_axis,
)
def _load_from_state_dict(
self,
state_dict,
prefix,
local_metadata,
strict,
missing_keys,
unexpected_keys,
error_msgs,
):
for key in _get_weight_qparam_keys(state_dict, prefix):
setattr(self, key, state_dict[prefix + key])
state_dict.pop(prefix + key)
super()._load_from_state_dict(
state_dict,
prefix,
local_metadata,
False,
missing_keys,
unexpected_keys,
error_msgs,
)
def _quantize_weight_decomposed(
weight: torch.Tensor,
weight_qscheme: torch.qscheme,
weight_dtype: torch.dtype,
weight_scale: torch.Tensor,
weight_zero_point: torch.Tensor,
weight_axis: int,
weight_quant_min: typing.Optional[int],
weight_quant_max: typing.Optional[int],
) -> torch.Tensor:
_DTYPE_TO_QVALUE_BOUNDS = {
torch.uint8: (0, 255),
torch.int8: (-128, 127),
torch.int32: (-(2**31), 2**31 - 1),
}
# TODO: add an util function for converting qdtype to dtype
_QDTYPE_TO_UNDERLYING_INT_REPR_DTYPE = {
torch.quint8: torch.uint8,
torch.qint8: torch.int8,
torch.qint32: torch.int32,
}
if weight_qscheme == torch.per_tensor_affine:
if weight_dtype in [torch.quint8, torch.qint8, torch.qint32]:
weight_dtype_ = _QDTYPE_TO_UNDERLYING_INT_REPR_DTYPE[weight_dtype]
if weight_quant_min is None or weight_quant_max is None:
weight_quant_min, weight_quant_max = _DTYPE_TO_QVALUE_BOUNDS[
weight_dtype_
]
weight = torch.ops.quantized_decomposed.quantize_per_tensor(
weight,
weight_scale,
weight_zero_point,
weight_quant_min,
weight_quant_max,
weight_dtype_,
)
return weight
elif weight_qscheme in [
torch.per_channel_affine,
torch.per_channel_affine_float_qparams,
]:
# TODO: torch.quint4x2 is not supported
if weight_dtype in [torch.quint8, torch.qint8, torch.qint32]:
weight_dtype_ = _QDTYPE_TO_UNDERLYING_INT_REPR_DTYPE[weight_dtype]
if weight_quant_min is None or weight_quant_max is None:
weight_quant_min, weight_quant_max = _DTYPE_TO_QVALUE_BOUNDS[
weight_dtype_
]
weight = torch.ops.quantized_decomposed.quantize_per_channel(
weight,
weight_scale,
weight_zero_point,
weight_axis,
weight_quant_min,
weight_quant_max,
weight_dtype_,
) # type: ignore[arg-type]
return weight
raise ValueError(f"Unsupported dtype and qscheme: {weight_dtype}, {weight_qscheme}")
def _dequantize_weight_decomposed(
weight: torch.Tensor,
weight_qscheme: torch.qscheme,
weight_dtype: torch.dtype,
weight_scale: torch.Tensor,
weight_zero_point: torch.Tensor,
weight_axis: int,
weight_quant_min: typing.Optional[int],
weight_quant_max: typing.Optional[int],
) -> torch.Tensor:
# TODO: get the quant_min and quant_max from activation_post_process
_DTYPE_TO_QVALUE_BOUNDS = {
torch.uint8: (0, 255),
torch.int8: (-128, 127),
torch.int32: (-(2**31), 2**31 - 1),
}
# TODO: add an util function for converting qdtype to dtype
_QDTYPE_TO_UNDERLYING_INT_REPR_DTYPE = {
torch.quint8: torch.uint8,
torch.qint8: torch.int8,
torch.qint32: torch.int32,
}
weight_dtype_ = _QDTYPE_TO_UNDERLYING_INT_REPR_DTYPE[weight_dtype]
if weight_quant_min is None or weight_quant_max is None:
weight_quant_min, weight_quant_max = _DTYPE_TO_QVALUE_BOUNDS[weight_dtype_]
if weight_qscheme == torch.per_tensor_affine:
if weight_dtype in [torch.quint8, torch.qint8, torch.qint32]:
weight = torch.ops.quantized_decomposed.dequantize_per_tensor(
weight,
weight_scale,
weight_zero_point,
weight_quant_min,
weight_quant_max,
weight_dtype_,
)
return weight
elif weight_qscheme in [
torch.per_channel_affine,
torch.per_channel_affine_float_qparams,
]:
# TODO: torch.quint4x2 is not supported
if weight_dtype in [torch.quint8, torch.qint8, torch.qint32]:
weight = torch.ops.quantized_decomposed.dequantize_per_channel(
weight,
weight_scale,
weight_zero_point,
weight_axis,
weight_quant_min,
weight_quant_max,
weight_dtype_,
) # type: ignore[arg-type]
return weight
raise ValueError(f"Unsupported dtype and qscheme: {weight_dtype}, {weight_qscheme}")
def _quantize_weight(
weight: torch.Tensor,
weight_qscheme: torch.qscheme,
weight_dtype: torch.dtype,
weight_scale: torch.Tensor,
weight_zero_point: torch.Tensor,
weight_axis_int: int,
) -> torch.Tensor:
if weight_dtype == torch.float16:
weight = weight.to(weight_dtype)
return weight
if weight_qscheme == torch.per_tensor_affine:
if weight_dtype in [torch.quint8, torch.qint8, torch.qint32]:
weight = torch.quantize_per_tensor(
weight, weight_scale, weight_zero_point, weight_dtype
)
return weight
elif weight_qscheme in [
torch.per_channel_affine,
torch.per_channel_affine_float_qparams,
]:
if weight_dtype in [torch.quint8, torch.qint8, torch.quint4x2, torch.qint32]:
weight = torch.quantize_per_channel(
weight, weight_scale, weight_zero_point, weight_axis_int, weight_dtype
) # type: ignore[arg-type]
return weight
raise ValueError(f"Unsupported dtype and qscheme: {weight_dtype}, {weight_qscheme}")
def _quantize_and_dequantize_weight_decomposed(
weight: torch.Tensor,
weight_qscheme: torch.qscheme,
weight_dtype: torch.dtype,
weight_scale: torch.Tensor,
weight_zero_point: torch.Tensor,
weight_axis_int: int,
weight_quant_min: typing.Optional[int],
weight_quant_max: typing.Optional[int],
) -> torch.Tensor:
"""Quantize and then dequantize the weight based on
the quantization parameters
"""
if weight_qscheme in [
torch.per_tensor_affine,
torch.per_channel_affine,
torch.per_channel_affine_float_qparams,
]:
weight_quant = _quantize_weight_decomposed(
weight,
weight_qscheme,
weight_dtype,
weight_scale,
weight_zero_point,
weight_axis_int,
weight_quant_min,
weight_quant_max,
)
weight_dequant = _dequantize_weight_decomposed(
weight_quant,
weight_qscheme,
weight_dtype,
weight_scale,
weight_zero_point,
weight_axis_int,
weight_quant_min,
weight_quant_max,
)
else:
weight_dequant = weight
return weight_dequant
def _quantize_and_dequantize_weight(
weight: torch.Tensor,
weight_qscheme: torch.qscheme,
weight_dtype: torch.dtype,
weight_scale: torch.Tensor,
weight_zero_point: torch.Tensor,
weight_axis_int: int,
) -> torch.Tensor:
"""Quantize and then dequantize the weight based on
the quantization parameters
"""
if weight_qscheme in [
torch.per_tensor_affine,
torch.per_channel_affine,
torch.per_channel_affine_float_qparams,
]:
weight_quant = _quantize_weight(
weight,
weight_qscheme,
weight_dtype,
weight_scale,
weight_zero_point,
weight_axis_int,
)
weight_dequant = weight_quant.dequantize()
else:
weight_dequant = weight
return weight_dequant
def _save_weight_qparams(
destination,
prefix,
weight_qscheme,
weight_dtype,
weight_scale,
weight_zero_point,
weight_axis,
):
destination[prefix + "weight_qscheme"] = weight_qscheme
destination[prefix + "weight_dtype"] = weight_dtype
if weight_qscheme is not None:
destination[prefix + "weight_scale"] = weight_scale
destination[prefix + "weight_zero_point"] = weight_zero_point
if weight_qscheme == torch.per_channel_affine:
destination[prefix + "weight_axis"] = weight_axis
def _get_weight_qparam_keys(state_dict: typing.Dict[str, typing.Any], prefix: str):
keys = ["weight_qscheme", "weight_dtype"]
weight_qscheme = state_dict[prefix + "weight_qscheme"]
if weight_qscheme is not None:
keys.append("weight_scale")
keys.append("weight_zero_point")
if weight_qscheme == torch.quantize_per_channel:
keys.append("weight_axis")
return keys
|