Skip to content

Commit 676b832

Browse files
author
snoopyisadog
committed
CPU::conv_layer consumes quantized integer weight/bias
1 parent 8613009 commit 676b832

File tree

4 files changed

+75
-23
lines changed

4 files changed

+75
-23
lines changed

include/caffe/layers/base_conv_layer.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,12 @@ class BaseConvolutionLayer : public Layer<Dtype> {
103103

104104
int input_zero_point_; //CUSTOMIZATION
105105
int output_zero_point_; //CUSTOMIZATION
106+
int weight_zero_point_; //CUSTOMIZATION
107+
int bias_zero_point_; //CUSTOMIZATION
106108
Dtype input_scale_; //CUSTOMIZATION
107109
Dtype output_scale_; //CUSTOMIZATION
110+
Dtype weight_scale_; //CUSTOMIZATION
111+
Dtype bias_scale_; //CUSTOMIZATION
108112
Dtype saturate_; //CUSTOMIZATION
109113

110114
private:

src/caffe/layers/base_conv_layer.cpp

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,14 @@ void BaseConvolutionLayer<Dtype>::LayerSetUpInternal(LayerParam conv_param,
7777
}
7878

7979
//<--CUSTOMIZATION
80-
if (conv_param.has_input_scale()) {
81-
input_scale_ = conv_param.input_scale();
82-
} else {
83-
input_scale_ = 1;
84-
}
85-
if (conv_param.has_input_zero_point()) {
86-
input_zero_point_ = conv_param.input_zero_point();
87-
} else {
88-
input_zero_point_ = 0;
89-
}
90-
if (conv_param.has_output_scale()){
91-
output_scale_ = conv_param.output_scale();
92-
} else{
93-
output_scale_ = 1;
94-
}
95-
if (conv_param.has_output_zero_point()) {
96-
output_zero_point_ = conv_param.output_zero_point();
97-
} else {
98-
output_zero_point_ = 0;
99-
}
100-
80+
input_scale_ = conv_param.input_scale();
81+
input_zero_point_ = conv_param.input_zero_point();
82+
output_scale_ = conv_param.output_scale();
83+
output_zero_point_ = conv_param.output_zero_point();
84+
weight_scale_ = conv_param.weight_scale();
85+
weight_zero_point_ = conv_param.weight_zero_point();
86+
bias_scale_ = conv_param.bias_scale();
87+
bias_zero_point_ = conv_param.bias_zero_point();
10188
saturate_ = conv_param.saturate();
10289
//CUSTOMIZATION-->
10390

src/caffe/layers/conv_layer.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <vector>
22

33
#include "caffe/layers/conv_layer.hpp"
4+
#include "caffe/util/math_functions.hpp"
45

56
namespace caffe {
67

@@ -51,10 +52,39 @@ void ConvolutionLayer<Dtype>::compute_output_shape() {
5152
template <typename Dtype>
5253
void ConvolutionLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
5354
const vector<Blob<Dtype>*>& top) {
55+
// set up quantization parameters: scale + zero_point
56+
const Dtype input_scale = this->input_scale_;
57+
const Dtype output_scale = this->output_scale_;
58+
const Dtype weight_scale = this->weight_scale_;
59+
const Dtype bias_scale = this->bias_scale_;
60+
const int input_zero_point = this->input_zero_point_;
61+
const int output_zero_point = this->output_zero_point_;
62+
const int weight_zero_point = this->weight_zero_point_;
63+
const int bias_zero_point = this->bias_zero_point_;
64+
const Dtype saturate = this->saturate_;
65+
const bool quant_in = (input_scale != Dtype(1.0) || input_zero_point != 0);
66+
const bool quant_out = (output_scale != Dtype(1.0) || output_zero_point != 0);
67+
const bool quant_w = (weight_scale != Dtype(1.0) || weight_zero_point != 0);
68+
const bool quant_b = (this->bias_term_&& (bias_scale != Dtype(1.0) || bias_zero_point != 0));
69+
if (quant_w) {
70+
Dtype *qw = this->blobs_[0]->mutable_cpu_data();
71+
caffe_cpu_dequantize<Dtype>(this->blobs_[0]->count(), qw, weight_scale, weight_zero_point);
72+
}
73+
if (quant_b) {
74+
Dtype *qb = this->blobs_[1]->mutable_cpu_data();
75+
caffe_cpu_dequantize<Dtype>(this->blobs_[1]->count(), qb, bias_scale, bias_zero_point);
76+
}
77+
5478
const Dtype* weight = this->blobs_[0]->cpu_data();
5579
for (int i = 0; i < bottom.size(); ++i) {
80+
if (quant_in) {
81+
Dtype* qin = bottom[i]->mutable_cpu_data();
82+
caffe_cpu_dequantize<Dtype>(bottom[i]->count(), qin, input_scale, input_zero_point);
83+
}
84+
5685
const Dtype* bottom_data = bottom[i]->cpu_data();
5786
Dtype* top_data = top[i]->mutable_cpu_data();
87+
5888
for (int n = 0; n < this->num_; ++n) {
5989
this->forward_cpu_gemm(bottom_data + n * this->bottom_dim_, weight,
6090
top_data + n * this->top_dim_);
@@ -63,6 +93,33 @@ void ConvolutionLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
6393
this->forward_cpu_bias(top_data + n * this->top_dim_, bias);
6494
}
6595
}
96+
97+
const int count_t = top[i]->count();
98+
if (quant_out) {
99+
caffe_cpu_quantize<Dtype>(count_t, top_data, output_scale, output_zero_point);
100+
}
101+
if (saturate == ConvolutionParameter_SaturateMethod_Signed)
102+
caffe_cpu_signed_saturate(count_t, top_data);
103+
if (saturate == ConvolutionParameter_SaturateMethod_Unsigned)
104+
caffe_cpu_unsigned_saturate(count_t, top_data);
105+
if (saturate == ConvolutionParameter_SaturateMethod_Signed_8bit)
106+
caffe_cpu_signed_8bit_saturate(count_t, top_data);
107+
if (saturate == ConvolutionParameter_SaturateMethod_Unsigned_8bit)
108+
caffe_cpu_unsigned_8bit_saturate(count_t, top_data);
109+
110+
if (quant_in) { // restore the quantized input blob
111+
Dtype* qin = bottom[i]->mutable_cpu_data();
112+
caffe_cpu_quantize<Dtype>(bottom[i]->count(), qin, input_scale, input_zero_point);
113+
}
114+
}
115+
// restore quantized weight/bias
116+
if (quant_w) {
117+
Dtype *qw = this->blobs_[0]->mutable_cpu_data();
118+
caffe_cpu_quantize<Dtype>(this->blobs_[0]->count(), qw, weight_scale, weight_zero_point);
119+
}
120+
if (quant_b) {
121+
Dtype *qb = this->blobs_[1]->mutable_cpu_data();
122+
caffe_cpu_quantize<Dtype>(this->blobs_[1]->count(), qb, bias_scale, bias_zero_point);
66123
}
67124
}
68125

src/caffe/proto/caffe.proto

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,8 +2608,12 @@ message SqueezeConvolutionParameter {
26082608

26092609
optional float input_scale = 46 [default = 1]; //CUSTOMIZATION, act as dummy param in squeeze_conv layer now
26102610
optional float output_scale = 47 [default = 1]; //CUSTOMIZATION, act as dummy param in squeeze_conv layer now
2611-
optional int32 input_zero_point = 49 [default = 0]; //CUSTOMIZATION, act as dummy param in squeeze_conv layer now
2612-
optional int32 output_zero_point = 50 [default = 0]; //CUSTOMIZATION, act as dummy param in squeeze_conv layer now
2611+
optional float weight_scale = 49 [default = 1]; //CUSTOMIZATION
2612+
optional float bias_scale = 50 [default = 1]; //CUSTOMIZATION
2613+
optional int32 input_zero_point = 51 [default = 0]; //CUSTOMIZATION, act as dummy param in squeeze_conv layer now
2614+
optional int32 output_zero_point = 52 [default = 0]; //CUSTOMIZATION, act as dummy param in squeeze_conv layer now
2615+
optional int32 weight_zero_point = 53 [default = 0]; //CUSTOMIZATION
2616+
optional int32 bias_zero_point = 54 [default = 0]; //CUSTOMIZATION
26132617
//<--CUSTOMIZATION
26142618
enum SaturateMethod {
26152619
None = 0;

0 commit comments

Comments
 (0)