Caffe2 - C++ API
A deep learning, cross platform ML framework
int8_sigmoid_op.h
1 #ifndef CAFFE2_INT8_SIGMOID_OP_H_
2 #define CAFFE2_INT8_SIGMOID_OP_H_
3 
4 #include <qnnpack.h>
5 
6 #include "caffe2/core/context.h"
7 #include "caffe2/core/operator.h"
8 #include "caffe2/core/tensor_int8.h"
9 #include "caffe2/operators/quantized/int8_utils.h"
10 
11 namespace caffe2 {
12 namespace int8 {
13 
14 class Int8SigmoidOp final : public Operator<CPUContext> {
15  public:
16  explicit Int8SigmoidOp(const OperatorDef& operator_def, Workspace* ws)
17  : Operator<CPUContext>(operator_def, ws), ws_(ws) {}
18 
19  ~Int8SigmoidOp() {
20  if (this->qnnpackOperator_ != nullptr) {
21  qnnp_delete_operator(this->qnnpackOperator_);
22  this->qnnpackOperator_ = nullptr;
23  }
24  }
25 
26  bool RunOnDevice() override {
27  const auto& X = Inputs()[0]->template Get<Int8TensorCPU>();
28  auto* Y = Outputs()[0]->template GetMutable<Int8TensorCPU>();
29  const int32_t Y_zero_point =
30  this->template GetSingleArgument<int>("Y_zero_point", 0);
31  const float Y_scale =
32  this->template GetSingleArgument<float>("Y_scale", 1);
33  CHECK_EQ(Y_zero_point, 0);
34  CHECK_EQ(Y_scale, 1.0f / 256.0f);
35 
36  /*
37  * Record quantization parameters for the input, because if the op is
38  * in-place, we may overwrite these parameters later, when we set
39  * quantization parameters for output tensor.
40  */
41  const uint8_t X_zero_point = X.zero_point;
42  const float X_scale = X.scale;
43 
44  Y->scale = Y_scale;
45  Y->zero_point = Y_zero_point;
46  Y->t.ResizeLike(X.t);
47 
48  initQNNPACK();
49 
50  if (this->qnnpackOperator_ == nullptr) {
51  const qnnp_status createStatus = qnnp_create_sigmoid_nc_q8(
52  1 /* channels */,
53  X_zero_point, X_scale,
54  static_cast<uint8_t>(Y_zero_point), Y_scale,
55  0 /* output min */,
56  255 /* output max */,
57  0 /* flags */,
58  &qnnpackOperator_);
59  CAFFE_ENFORCE(
60  createStatus == qnnp_status_success,
61  "failed to create QNNPACK Sigmoid operator");
62  CAFFE_ENFORCE(this->qnnpackOperator_ != nullptr);
63  }
64 
65  const qnnp_status setupStatus = qnnp_setup_sigmoid_nc_q8(
66  this->qnnpackOperator_,
67  X.t.numel() /* batch size */,
68  X.t.template data<uint8_t>(),
69  1 /* X stride */,
70  Y->t.template mutable_data<uint8_t>(),
71  1 /* Y stride */);
72  CAFFE_ENFORCE(
73  setupStatus == qnnp_status_success,
74  "failed to setup QNNPACK Sigmoid operator");
75 
76 #ifdef FBCODE_CAFFE2
77  const qnnp_status runStatus =
78  qnnp_run_operator(this->qnnpackOperator_, nullptr /* thread pool */);
79 #else
80  pthreadpool_t threadpool =
81  reinterpret_cast<pthreadpool_t>(ws_->GetThreadPool());
82  const qnnp_status runStatus =
83  qnnp_run_operator(this->qnnpackOperator_, threadpool);
84 #endif
85  CAFFE_ENFORCE(
86  runStatus == qnnp_status_success,
87  "failed to run QNNPACK Sigmoid operator");
88 
89  return true;
90  }
91 
92  private:
93  Workspace* ws_;
94  // QNNPACK Sigmoid operator
95  qnnp_operator_t qnnpackOperator_{nullptr};
96 };
97 
98 } // namespace int8
99 } // namespace caffe2
100 #endif // CAFFE2_INT8_SIGMOID_OP_H_
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:47
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13