quantize
AdaptiveQuantizer (DecimalQuantizer)
¶
The quantizer that implements the algorithm 2 of the MDPI paper.
optimize(self, x, bits, weight=None, channel_index=-1, batched=False, **kwargs)
¶
return the updated weight for each step
Source code in qsparse/quantize.py
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 |
|
BaseQuantizer (Module)
¶
Base class for quantizer, interface for the callback function of quantize.
forward(self, tensor, bits, weight=None, batched=False, channel_index=-1)
¶
return quantized tensor
Source code in qsparse/quantize.py
267 268 269 |
|
optimize(self, tensor, bits, weight=None, batched=False, channel_index=-1)
¶
return the updated weight for each step
Source code in qsparse/quantize.py
263 264 265 |
|
DecimalQuantization (Function)
¶
Straight-Through Gradient Estimator (with shift).
Please look for detailed description on arguments in quantize_with_decimal.
backward(ctx, grad_output)
staticmethod
¶
gradient computation for quantization operation.
Source code in qsparse/quantize.py
65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
forward(ctx, input, bits=8, decimal=5, channel_index=1, use_uint=False, backward_passthrough=False, flip_axis=False)
staticmethod
¶
quantize the input tensor and prepare for backward computation.
Source code in qsparse/quantize.py
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 |
|
DecimalQuantizer (BaseQuantizer)
¶
The quantizer that implements the algorithm 3 of the MDPI paper. The forward
function covers the quantization logic and the optimize
function covers the parameter update.
It always restricts the scaling factor to be power of 2.
__init__(self, use_uint=False, backward_passthrough=False, flip_axis=False, group_num=-1, group_timeout=512)
special
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
use_uint |
bool |
See quantize_with_decimal. Defaults to False. |
False |
backward_passthrough |
bool |
See quantize_with_decimal. Defaults to False. |
False |
flip_axis |
bool |
See quantize_with_decimal. Defaults to False. |
False |
group_num |
int |
Number of groups used for groupwise quantization. Defaults to -1, which disables groupwise quantization. |
-1 |
group_timeout |
int |
Number of steps when the clustering starts after the activation of the quantization operator. Defaults to 512. |
512 |
Source code in qsparse/quantize.py
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 |
|
forward(self, tensor, bits, scaler, channel_index=-1, **kwargs)
¶
return quantized tensor
Source code in qsparse/quantize.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
|
optimize(self, x, bits, weight=None, batched=False, channel_index=-1, **kwargs)
¶
return the updated weight for each step
Source code in qsparse/quantize.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
|
LineQuantization (Function)
¶
Straight-Through Gradient Estimator (asymmetric).
Please look for detailed description on arguments in quantize_with_line.
backward(ctx, grad_output)
staticmethod
¶
Defines a formula for differentiating the operation with backward mode automatic differentiation (alias to the vjp function).
This function is to be overridden by all subclasses.
It must accept a context :attr:ctx
as the first argument, followed by
as many outputs as the :func:forward
returned (None will be passed in
for non tensor outputs of the forward function),
and it should return as many tensors, as there were inputs to
:func:forward
. Each argument is the gradient w.r.t the given output,
and each returned value should be the gradient w.r.t. the
corresponding input. If an input is not a Tensor or is a Tensor not
requiring grads, you can just pass None as a gradient for that input.
The context can be used to retrieve tensors saved during the forward
pass. It also has an attribute :attr:ctx.needs_input_grad
as a tuple
of booleans representing whether each input needs gradient. E.g.,
:func:backward
will have ctx.needs_input_grad[0] = True
if the
first input to :func:forward
needs gradient computated w.r.t. the
output.
Source code in qsparse/quantize.py
183 184 185 |
|
forward(ctx, x, bits=8, lines=(-0.1, 0.9), channel_index=-1, inplace=False, float_zero_point=True)
staticmethod
¶
Performs the operation.
This function is to be overridden by all subclasses.
It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types).
The context can be used to store arbitrary data that can be then
retrieved during the backward pass. Tensors should not be stored
directly on ctx
(though this is not currently enforced for
backward compatibility). Instead, tensors should be saved either with
:func:ctx.save_for_backward
if they are intended to be used in
backward
(equivalently, vjp
) or :func:ctx.save_for_forward
if they are intended to be used for in jvp
.
Source code in qsparse/quantize.py
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 |
|
QuantizeLayer (Module)
¶
Applies quantization over input tensor.
Please look for detailed description in quantize
initted: bool
property
readonly
¶
whether the parameters of the quantize layer are initialized.
forward(self, x)
¶
Quantize input tensor according to given configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
torch.Tensor |
tensor to be quantized |
required |
Returns:
Type | Description |
---|---|
torch.Tensor |
quantized tensor |
Source code in qsparse/quantize.py
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
|
ScalerQuantization (Function)
¶
Straight-Through Gradient Estimator (with scaler).
Please look for detailed description on arguments in quantize_with_scaler.
backward(ctx, grad_output)
staticmethod
¶
gradient computation for quantization operation.
Source code in qsparse/quantize.py
119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
forward(ctx, input, bits=8, scaler=0.1, channel_index=1, use_uint=False, backward_passthrough=False, flip_axis=False)
staticmethod
¶
quantize the input tensor and prepare for backward computation.
Source code in qsparse/quantize.py
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 |
|
ScalerQuantizer (DecimalQuantizer)
¶
The quantizer that implements the algorithm 3 of the MDPI paper, without the power of 2 restriction.
quantize(inp=None, bits=8, channelwise=1, timeout=1000, callback=None, bias_bits=-1, name='')
¶
Creates a QuantizeLayer which is usually used for feature quantization if no input module is provided, or creates a weight-quantized version of the input module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inp |
nn.Module |
input module whose weight is to be quantized. Defaults to None. |
None |
bits |
int |
bitwidth for weight. Defaults to 8. |
8 |
channelwise |
int |
dimension index for channel. Defaults to 1. When channelwise >= 0, channel-wise quantization is enabled. When set to -1, channel-wise quantization is disabled. |
1 |
timeout |
int |
the steps to compute the best decimal bits. Defaults to 1000. |
1000 |
callback |
BaseQuantizer |
callback module for actual operation of quantizing tensor and finding quantization parameters. Defaults to ScalerQuantizer. |
None |
bias_bits |
int |
bitwidth for bias. Defaults to -1, means not quantizing bias. |
-1 |
name |
str |
name of the quantize layer created, used for better logging. Defaults to "". |
'' |
Returns:
Type | Description |
---|---|
nn.Module |
input module with its weight quantized or a instance of QuantizeLayer for feature quantization |
Source code in qsparse/quantize.py
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 |
|
quantize_with_decimal(input, bits=8, decimal=5, channel_index=-1, use_uint=False, backward_passthrough=False, flip_axis=False)
¶
Applying power-of-2 uniform quantization over input tensor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input |
torch.Tensor |
tensor to be quantized |
required |
bits |
int |
Bitwidth. Defaults to 8. |
8 |
decimal |
TensorOrInt |
Number of bits used to represent fractional number (shift). Defaults to 5. |
5 |
channel_index |
int |
Channel axis, for channelwise quantization. Defaults to -1, which means tensorwise. |
-1 |
use_uint |
bool |
Whether use uint to quantize input. If so, it will ignores the negative number, which could be used for ReLU output. Defaults to False. |
False |
backward_passthrough |
bool |
Whether to skip the saturation operation of STE on gradients during the backward pass. Defaults to False. |
False |
flip_axis |
bool |
Whether use flip the axis to represent numbers (the largest positive number increases from |
False |
Returns:
Type | Description |
---|---|
torch.Tensor |
quantized tensor |
Source code in qsparse/quantize.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
quantize_with_line(x, bits=8, lines=(-0.1, 0.9), channel_index=-1, inplace=False, float_zero_point=True)
¶
Applying asymmetric uniform quantization over input tensor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
torch.Tensor |
tensor to be quantized |
required |
bits |
int |
Bitwidth. Defaults to 8. |
8 |
lines |
Union[Tuple[float, float], List[Tuple[float, float]]] |
The estimated lower and upper bound of input data. Defaults to (-0.1, 0.9). |
(-0.1, 0.9) |
channel_index |
int |
Channel axis, for channelwise quantization. Defaults to -1, which means tensorwise. |
-1 |
inplace |
bool |
Whether the operation is inplace. Defaults to False. |
False |
float_zero_point |
bool |
Whether use floating-point value to store zero-point. Defaults to True, recommend to turn on for training and off for evaluation. |
True |
Returns:
Type | Description |
---|---|
torch.Tensor |
quantized tensor |
Source code in qsparse/quantize.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
quantize_with_scaler(input, bits=8, scaler=0.1, channel_index=-1, use_uint=False, backward_passthrough=False, flip_axis=False)
¶
Applying scaling-factor based uniform quantization over input tensor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input |
torch.Tensor |
tensor to be quantized |
required |
bits |
int |
Bitwidth. Defaults to 8. |
8 |
scaler |
TensorOrFloat |
Scaling factor. Defaults to 0.1. |
0.1 |
channel_index |
int |
Channel axis, for channelwise quantization. Defaults to -1, which means tensorwise. |
-1 |
use_uint |
bool |
Whether use uint to quantize input. If so, it will ignores the negative number, which could be used for ReLU output. Defaults to False. |
False |
backward_passthrough |
bool |
Whether to skip the saturation operation of STE on gradients during the backward pass. Defaults to False. |
False |
flip_axis |
bool |
Whether use flip the axis to represent numbers (the largest positive number increases from |
False |
Returns:
Type | Description |
---|---|
torch.Tensor |
quantized tensor |
Source code in qsparse/quantize.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
|