Skip to content

Commit 7440dae

Browse files
committed
try to add EV style rounding for innerproduct
1 parent 960d0d4 commit 7440dae

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/caffe/layers/conv_layer.ev.inc

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ static void normalize_fractional(Scale_type F, unsigned &mpy, unsigned &shift) {
9595
}
9696
}
9797

98+
9899
template<typename Dtype>
99100
void caffe_cpu_scale_better_round(const std::string &name, const int n,
100101
const Scale_type scale, Dtype* x) {
@@ -112,10 +113,15 @@ void caffe_cpu_scale_better_round(const std::string &name, const int n,
112113
(printf("Unrecognized rounding mode %s\n",QR), R_double_round);
113114
};
114115
static const Rmode QR = tell();
116+
117+
static bool show_data_bool = getenv("CAFFE_SHOW_DATA") != 0;
118+
115119
switch (QR) {
116-
case R_double_round:
120+
case R_double_round: {
121+
caffe_cpu_scale_double_round (n, scale, x);
122+
} break;
117123
case R_single_round: {
118-
if (QR != R_double_round)
124+
if (show_data_bool)
119125
printf(" Layer %s: round mode %d by %18.15f\n", name.c_str(), QR,
120126
scale);
121127
bool SR = QR == R_single_round;
@@ -129,31 +135,31 @@ void caffe_cpu_scale_better_round(const std::string &name, const int n,
129135
x[i] = SR ? x[i] * mul : std::round(x[i] * mul);
130136
x[i] = std::round(x[i] / shift);
131137
}
132-
}
133-
break;
138+
} break;
134139
case R_ev_round: {
135140
#define LLSHL1(x) (1LL<<(x))
136141
#define LL_ROUND(X,shift) /* (unbiased) round-to-even */ \
137-
((X + ((X >> (shift)) & 1) + (LLSHL1(shift-1)-1)) >> (shift))
142+
((X + ((X >> (shift)) & 1) + (LLSHL1(shift-1)-1)) >> (shift))
138143
unsigned mpy, shift;
139144
// Produces 15-bit mantissa and an exponent. The mantissa is
140145
// thus less precise than that of a 32-bit floating-point number.
141146
normalize_fractional(scale, mpy, shift);
142-
printf(" Layer %s: round mode %d by %18.15f = mpy %d shift %d\n",
143-
name.c_str(), QR, scale, mpy, shift);
147+
if (show_data_bool)
148+
printf(" Layer %s: round mode %d by %18.15f = mpy %d shift %d\n",
149+
name.c_str(), QR, scale, mpy, shift);
144150
typedef signed long long SLL;
145151
for (int i = 0; i < n; ++i) {
146152
SLL acc = SLL(x[i]); // Assumed to be an integer already.
147153
acc *= mpy;
148154
x[i] = LL_ROUND(acc, shift);
149155
}
150-
}
151-
break;
156+
} break;
152157
}
153158
}
154159

155-
//#define caffe_cpu_scale_double_round(A,B,C) \
156-
// caffe_cpu_scale_better_round(this->layer_param_.name(),A,B,C)
160+
#define caffe_cpu_scale_double_round(A,B,C) \
161+
caffe_cpu_scale_better_round(this->layer_param_.name(),A,B,C)
162+
157163

158164
template<typename Dtype>
159165
void Multiply_better(const int n, Dtype* x, const int mul, const int shift,

src/caffe/layers/inner_product_layer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ void InnerProductLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
9292
}
9393
}
9494

95+
#include "conv_layer.ev.inc"
9596
template <typename Dtype>
9697
void InnerProductLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
9798
const vector<Blob<Dtype>*>& top) {

0 commit comments

Comments
 (0)