Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update quantize.py #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions micronet/compression/quantization/wqaq/iao/quantize.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,22 @@ def forward(self, input):
return output


class QuantReLU6(nn.ReLU6):
def __init__(self, inplace=False, a_bits=8, q_type=0, device='cpu', qaft=False):
super(QuantReLU6, self).__init__(inplace)
if q_type == 0:
self.activation_quantizer = SymmetricQuantizer(bits=a_bits, observer=MovingAverageMinMaxObserver(
q_level='L', out_channels=None, device=device), activation_weight_flag=1, qaft=qaft)
else:
self.activation_quantizer = AsymmetricQuantizer(bits=a_bits, observer=MovingAverageMinMaxObserver(
q_level='L', out_channels=None, device=device), activation_weight_flag=1, qaft=qaft)

def forward(self, input):
quant_input = self.activation_quantizer(input)
output = F.relu6(quant_input, self.inplace)
return output


class QuantSigmoid(nn.Sigmoid):
def __init__(self, a_bits=8, q_type=0, device='cpu', qaft=False):
super(QuantSigmoid, self).__init__()
Expand Down Expand Up @@ -752,6 +768,10 @@ def add_quant_op(module, a_bits=8, w_bits=8, q_type=0, q_level=0, device='cpu',
quant_relu = QuantReLU(inplace=child.inplace, a_bits=a_bits,
q_type=q_type, device=device, qaft=qaft)
module._modules[name] = quant_relu
elif isinstance(child, nn.ReLU6):
quant_relu = QuantReLU6(inplace=child.inplace, a_bits=a_bits,
q_type=q_type, device=device, qaft=qaft)
module._modules[name] = quant_relu6
elif isinstance(child, nn.Sigmoid):
quant_sigmoid = QuantSigmoid(a_bits=a_bits, q_type=q_type,
device=device, qaft=qaft)
Expand Down