Caffe2 - C++ API
A deep learning, cross platform ML framework
sigmoid.cc
1 #include "sigmoid.h"
2 
3 namespace dnnlowp {
4 
5 using namespace std;
6 
7 template <typename T>
8 Sigmoid<T>::Sigmoid(double max_abs_err) : tanh_(max_abs_err) {
9  float x_sq = tanh_.GetSaturationRegionBegin();
10 
11  in_qparams_.scale = 2 * x_sq / ((1 << (num_in_bits_ - 1)) - 1);
12  in_qparams_.zero_point = 1 << (num_in_bits_ - 1);
13  in_qparams_.precision = num_in_bits_;
14  // -2 x_sq is mapped to -127, 0 is mapped to 0, 2 x_sq is mapped to 127
15 
16  out_qparams_.scale = 0.5 / ((1 << (num_out_bits_ - 1)) - 1);
17  out_qparams_.zero_point = 0;
18  out_qparams_.precision = num_out_bits_;
19  // 0 is mapped to 0, 1/2 is mapped to 127, 1 is mapped to 254
20 }
21 
22 template <typename T>
23 T Sigmoid<T>::Compute(T x) const {
24  T temp = tanh_.Compute(x);
25  assert(temp >= 1);
26  assert(temp < (1 << num_out_bits_));
27  return temp - 1;
28 }
29 
30 template class Sigmoid<uint8_t>;
31 template class Sigmoid<uint16_t>;
32 template class Sigmoid<int32_t>;
33 
34 } // namespace dnnlowp