Skip to content

Commit 930a1e8

Browse files
committed
quantized qdense params-> folded instead of quantized folded params
1 parent b5ed41b commit 930a1e8

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

qkeras/qdense_batchnorm.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ def call(self, inputs, training=None):
159159
data_format="channels_last")
160160
else:
161161
bias = 0
162+
# If loaded from a ckpt, bias_quantizer is the ckpt value
163+
# Else if the layer is called for the first time, in this case bias
164+
# quantizer is None and we need to calculate bias quantizer
165+
# type according to accumulator type
166+
if self.bias_quantizer_internal is not None:
167+
q_bias = self.bias_quantizer_internal(bias)
168+
else:
169+
q_bias = bias
162170

163171
# begin batchnorm
164172
_ = self.batchnorm(qdense_outputs, training=bn_training)
@@ -195,8 +203,9 @@ def call(self, inputs, training=None):
195203
inv = math_ops.rsqrt(new_variance + self.batchnorm.epsilon)
196204
if gamma is not None:
197205
inv *= gamma
206+
198207
# fold bias with bn stats
199-
folded_bias = inv * (bias - new_mean) + beta
208+
folded_bias = inv * (q_bias - new_mean) + beta
200209

201210
elif self.folding_mode == "ema_stats_folding":
202211
# We always scale the weights with a correction factor to the long term
@@ -218,35 +227,25 @@ def call(self, inputs, training=None):
218227
batch_inv *= gamma
219228
folded_bias = tf_utils.smart_cond(
220229
bn_training,
221-
lambda: batch_inv * (bias - mean) + beta,
222-
lambda: mv_inv * (bias - moving_mean) + beta)
230+
lambda: batch_inv * (q_bias - mean) + beta,
231+
lambda: mv_inv * (q_bias - moving_mean) + beta)
223232
# moving stats is always used to fold kernel in tflite; before bn freeze
224233
# an additional correction factor will be applied to the conv2d output
225234
# end batchnorm
226235
inv = mv_inv
227236
else:
228237
assert ValueError
229238

230-
# wrap qdense kernel with bn parameters
231-
folded_kernel = inv * kernel
232239
# quantize the folded kernel
233240
if self.kernel_quantizer is not None:
234-
q_folded_kernel = self.kernel_quantizer_internal(folded_kernel)
241+
q_kernel = self.kernel_quantizer_internal(kernel)
235242
else:
236-
q_folded_kernel = folded_kernel
237-
238-
# If loaded from a ckpt, bias_quantizer is the ckpt value
239-
# Else if the layer is called for the first time, in this case bias
240-
# quantizer is None and we need to calculate bias quantizer
241-
# type according to accumulator type
242-
243-
if self.bias_quantizer_internal is not None:
244-
q_folded_bias = self.bias_quantizer_internal(folded_bias)
245-
else:
246-
q_folded_bias = folded_bias
243+
q_kernel = kernel
244+
# wrap qdense kernel with bn parameters
245+
folded_kernel = inv * q_kernel
247246

248-
applied_kernel = q_folded_kernel
249-
applied_bias = q_folded_bias
247+
applied_kernel = folded_kernel
248+
applied_bias = folded_bias
250249

251250
#calculate qdense output using the quantized folded kernel
252251
folded_outputs = tf.keras.backend.dot(inputs, applied_kernel)

0 commit comments

Comments
 (0)