Caffe2 - C++ API
A deep learning, cross platform ML framework
quant_decode_op.cc
1 
17 #include "quant_decode_op.h"
18 #include <stdint.h>
19 #include "caffe2/core/tensor.h"
20 #include "caffe2/core/typeid.h"
21 
22 namespace caffe2 {
23 
24 REGISTER_CPU_OPERATOR(QuantDecode, QuantDecodeOp<QuantDecodeRunTy::RUN_ALWAYS>);
25 REGISTER_CPU_OPERATOR(QuantDecodeGradient, QuantDecodeGradientOp);
26 #ifdef CAFFE2_USE_MPSCNN
27 REGISTER_CPU_OPERATOR(
28  MPSCNNQuantDecode,
29  QuantDecodeOp<QuantDecodeRunTy::RUN_ONCE>);
30 #endif
31 
32 OPERATOR_SCHEMA(QuantDecode)
33  .NumInputsOutputs([](int in, int out) { return in > 1 && out + 1 == in; })
34  .SetDoc(R"DOC(
35 Decode inputs using codebook. This is a general LUT operator that returns
36 tensors with values from codebook (input 0) based on given indices in
37 codes (input 1 ~ n).
38 
39 
40 Example:
41 
42 
43 Input:
44  codebook = [1.5, 2.5, 3.5]
45  codes_0 = [0, 1, 1, 2]
46  codes_1 = [2, 0, 0]
47 
48 
49 Output:
50  decoded_0 = [1.5, 2.5, 2.5, 3.5]
51  decoded_1 = [3.5, 1.5, 1.5]
52 )DOC")
53  .Input(0, "codebook", "Codebook in 1d tensor (float)")
54  .Input(1, "codes_0", "Encoded codes 0 (uint8/uint16/int32)")
55  .Input(2, "codes_1", "Encoded codes 1 if existed (uint8/uint16/int32)")
56  .Input(3, "codes_n", "Encoded codes n if existed (uint8/uint16/int32)")
57  .Output(0, "decoded_0", "Decoded tensor for codes_0 (float)")
58  .Output(1, "decoded_1", "Decoded tensor for codes_1 (float)")
59  .Output(2, "decoded_n", "Decoded tensor for codes_n (float)");
60 
61 OPERATOR_SCHEMA(QuantDecodeGradient)
62  .NumInputs([](int in) { return in >= 3 && in % 2 == 1; })
63  .NumOutputs(1);
64 
65 class GetQuantDecodeGradient : public GradientMakerBase {
66  using GradientMakerBase::GradientMakerBase;
67  vector<OperatorDef> GetGradientDefs() override {
68  CAFFE_ENFORCE_EQ(Def().input_size(), Def().output_size() + 1);
69  vector<string> gradient_op_inputs;
70  for (int i = 0; i < Def().input_size(); i++) {
71  gradient_op_inputs.push_back(I(i));
72  }
73  for (int i = 0; i < Def().output_size(); i++) {
74  gradient_op_inputs.push_back(GO(i));
75  }
76  return SingleGradientDef(
77  "QuantDecodeGradient", "", gradient_op_inputs, vector<string>{GI(0)});
78  }
79 };
80 
81 REGISTER_GRADIENT(QuantDecode, GetQuantDecodeGradient);
82 
83 } // namespace caffe2
Copyright (c) 2016-present, Facebook, Inc.