-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
120 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
examples/quantization_aware_training/cifar10/basecase/qconfig_lsq_dampen.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
BACKEND: virtual | ||
W: | ||
QSCHEME: per-channel-symmetric | ||
QUANTIZER: | ||
TYPE: lsq | ||
BIT: 4 | ||
A: | ||
QSCHEME: per-tensor-affine | ||
QUANTIZER: | ||
TYPE: lsq | ||
BIT: 4 | ||
REGULARIZER: | ||
ENABLE: True | ||
TYPE: dampen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,6 @@ A: | |
QUANTIZER: | ||
TYPE: pact | ||
BIT: 4 | ||
REGULARIZER: | ||
ENABLE: True | ||
TYPE: pact |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
REGULARIZERS_MAP = {} | ||
|
||
|
||
def register_regularizer(regularizer): | ||
REGULARIZERS_MAP[regularizer.TYPE.lower()] = regularizer | ||
return regularizer | ||
|
||
|
||
from .base import Regularizer | ||
from . import dampen, pact | ||
|
||
|
||
def build_regularizer(config): | ||
regularizer = REGULARIZERS_MAP[config.REGULARIZER.TYPE.lower()](config) | ||
return regularizer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Regularizer(object): | ||
def __init__(self, config): | ||
self.config = config | ||
|
||
def __call__(self): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import torch | ||
|
||
from sparsebit.quantization.regularizers import Regularizer as BaseRegularizer | ||
from sparsebit.quantization.regularizers import register_regularizer | ||
|
||
|
||
@register_regularizer | ||
class Regularizer(BaseRegularizer): | ||
TYPE = "Dampen" | ||
|
||
def __init__(self, config): | ||
super(Regularizer, self).__init__(config) | ||
self.config = config | ||
|
||
def _get_loss(self, x, quantizer): | ||
|
||
x_q = quantizer(x) | ||
|
||
qmin, qmax = quantizer.qdesc.qrange | ||
|
||
scale, zero_point = quantizer._qparams_preprocess(x) | ||
|
||
scale = scale.detach() | ||
zero_point = zero_point.detach() | ||
|
||
min_val = (qmin - zero_point) * scale | ||
|
||
max_val = (qmax - zero_point) * scale | ||
|
||
x_c = torch.min(torch.max(x, min_val), max_val) | ||
|
||
loss = (x_q - x_c) ** 2 | ||
|
||
loss = loss.sum() | ||
|
||
return loss | ||
|
||
def __call__(self, model): | ||
loss = 0.0 | ||
for n, m in model.named_modules(): | ||
if ( | ||
hasattr(m, "weight") | ||
and hasattr(m, "weight_quantizer") | ||
and m.weight_quantizer | ||
and m.weight_quantizer.is_enable | ||
): | ||
loss += self._get_loss(m.weight, m.weight_quantizer) | ||
return loss |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import torch | ||
|
||
from sparsebit.quantization.regularizers import Regularizer as BaseRegularizer | ||
from sparsebit.quantization.regularizers import register_regularizer | ||
|
||
|
||
@register_regularizer | ||
class Regularizer(BaseRegularizer): | ||
TYPE = "Pact" | ||
|
||
def __init__(self, config): | ||
super(Regularizer, self).__init__(config) | ||
self.config = config | ||
|
||
def __call__(self, model): | ||
loss = 0.0 | ||
for n, p in model.named_parameters(): | ||
if "alpha" in n: | ||
loss += (p ** 2).sum() | ||
return loss |