Caffe2 - C++ API
A deep learning, cross platform ML framework
tanh.h
1 #pragma once
2 
3 #include "dnnlowp.h"
4 
5 #include <cmath>
6 #include <vector>
7 
8 namespace dnnlowp {
9 
20 template <typename T>
21 class Tanh {
22  public:
23  Tanh(double max_abs_err = DEFAULT_MAX_ABS_ERR);
24 
25  T Compute(T x) const;
26 
27  TensorQuantizationParams GetInputQuantizationParams() const {
28  return in_qparams_;
29  }
30  TensorQuantizationParams GetOutputQuantizationParams() const {
31  return out_qparams_;
32  }
33 
34  int GetPassRegionEnd() const {
35  return x_pq_index_;
36  }
37 
38  float GetPassRegionEndDequantized() const {
39  return fbgemm::Dequantize<T>(
40  static_cast<uint8_t>(x_pq_index_ + in_qparams_.zero_point),
41  in_qparams_);
42  }
43 
44  float GetSaturationRegionBegin() const {
45  return fbgemm::Dequantize<T>(
46  static_cast<T>((1 << num_in_bits_) - 1), in_qparams_);
47  }
48 
49  static constexpr double DEFAULT_MAX_ABS_ERR = 0.02;
50  static constexpr int DEFAULT_NUM_IN_BITS = 8;
51  static constexpr int DEFAULT_NUM_OUT_BITS = 8;
52 
53  private:
54  const double max_abs_err_;
55  const int num_in_bits_ = DEFAULT_NUM_IN_BITS;
56  const int num_out_bits_ = DEFAULT_NUM_OUT_BITS;
57 
58  int x_pq_index_;
59  std::vector<T> processing_region_lut_;
60  TensorQuantizationParams in_qparams_, out_qparams_;
61 }; // class TanhApproximation
62 
63 } // namespace dnnlowp
We use the 3-region approach described in "Efficient VLSI Implementation of Neural Networks with Hype...
Definition: tanh.h:21