Skip to content

Commit 7e93aef

Browse files
committed
Quantized LeakyReLU: handle SaturateMethod_Unsigned_8bit
1 parent d6735fb commit 7e93aef

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/caffe/layers/relu_layer.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ void QuantizeLeakyRelu(const int n, const Dtype *in, Dtype *out, Dtype alpha, do
3232
unclamped_output = out_zp + tfl_MultiplyByQuantizedMultiplier(
3333
input_value, mul_alpha, shift_alpha);
3434
}
35-
if (unclamped_output < -128) unclamped_output = -128;
36-
if (unclamped_output > 127) unclamped_output = 127;
35+
// if (unclamped_output < clip_min) unclamped_output = clip_min; // will do caffe_cpu_saturate later
36+
// if (unclamped_output > clip_max) unclamped_output = clip_max;
3737
out[i] = Dtype(unclamped_output);
3838
}
3939
}
@@ -52,13 +52,15 @@ void ReLULayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
5252
double output_scale_ = this->layer_param_.relu_param().output_scale(); //CUSTOMIZATION
5353
int input_zero_point_ = this->layer_param_.relu_param().input_zero_point(); //CUSTOMIZATION
5454
int output_zero_point_ = this->layer_param_.relu_param().output_zero_point(); //CUSTOMIZATION
55+
Dtype saturate_ = this->layer_param_.relu_param().saturate(); //CUSTOMIZATION
5556
if (bottom.size() > 1) //bottom[1] provides the maximum case
5657
maximum = bottom[1]->cpu_data()[0];
5758
const bool quant_in = (input_scale_ != Dtype(1.0) || input_zero_point_ != 0);
5859
const bool quant_out = (output_scale_ != Dtype(1.0) || output_zero_point_ != 0);
5960
if (negative_slope != Dtype(0) && quant_in && quant_out) {
6061
QuantizeLeakyRelu(bottom[0]->count(), bottom[0]->cpu_data(), top[0]->mutable_cpu_data(),
6162
negative_slope, input_scale_, input_zero_point_, output_scale_, output_zero_point_);
63+
caffe_cpu_saturate(top[0]->count(), top[0]->mutable_cpu_data(), saturate_); // if None nothing happens
6264
return;
6365
}
6466
if (quant_in) {

0 commit comments

Comments
 (0)