File: sigmoid.cc

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (35 lines) | stat: -rw-r--r-- 986 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "sigmoid.h"

namespace dnnlowp {

using namespace std;

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
template <typename T>
Sigmoid<T>::Sigmoid(double max_abs_err) : tanh_(max_abs_err) {
  float x_sq = tanh_.GetSaturationRegionBegin();

  in_qparams_.scale = 2 * x_sq / ((1 << (num_in_bits_ - 1)) - 1);
  in_qparams_.zero_point = 1 << (num_in_bits_ - 1);
  in_qparams_.precision = num_in_bits_;
  // -2 x_sq is mapped to -127, 0 is mapped to 0, 2 x_sq is mapped to 127

  out_qparams_.scale = 0.5 / ((1 << (num_out_bits_ - 1)) - 1);
  out_qparams_.zero_point = 0;
  out_qparams_.precision = num_out_bits_;
  // 0 is mapped to 0, 1/2 is mapped to 127, 1 is mapped to 254
}

template <typename T>
T Sigmoid<T>::Compute(T x) const {
  T temp = tanh_.Compute(x);
  assert(temp >= 1);
  assert(temp < (1 << num_out_bits_));
  return temp - 1;
}

template class Sigmoid<uint8_t>;
template class Sigmoid<uint16_t>;
template class Sigmoid<int32_t>;

} // namespace dnnlowp