Caffe2 - C++ API
A deep learning, cross platform ML framework
quant_decode_op.cc
1 #include "quant_decode_op.h"
2 #include <stdint.h>
3 #include "caffe2/core/tensor.h"
4 #include <c10/util/typeid.h>
5 
6 namespace caffe2 {
7 
8 REGISTER_CPU_OPERATOR(QuantDecode, QuantDecodeOp<QuantDecodeRunTy::RUN_ALWAYS>);
9 REGISTER_CPU_GRADIENT_OPERATOR(QuantDecodeGradient, QuantDecodeGradientOp);
10 #ifdef CAFFE2_USE_MPSCNN
11 REGISTER_CPU_OPERATOR(
12  MPSCNNQuantDecode,
13  QuantDecodeOp<QuantDecodeRunTy::RUN_ONCE>);
14 #endif
15 
16 OPERATOR_SCHEMA(QuantDecode)
17  .NumInputsOutputs([](int in, int out) { return in > 1 && out + 1 == in; })
18  .SetDoc(R"DOC(
19 Decode inputs using codebook. This is a general LUT operator that returns
20 tensors with values from codebook (input 0) based on given indices in
21 codes (input 1 ~ n).
22 
23 
24 Example:
25 
26 
27 Input:
28  codebook = [1.5, 2.5, 3.5]
29  codes_0 = [0, 1, 1, 2]
30  codes_1 = [2, 0, 0]
31 
32 
33 Output:
34  decoded_0 = [1.5, 2.5, 2.5, 3.5]
35  decoded_1 = [3.5, 1.5, 1.5]
36 )DOC")
37  .Input(0, "codebook", "Codebook in 1d tensor (float)")
38  .Input(1, "codes_0", "Encoded codes 0 (uint8/uint16/int32)")
39  .Input(2, "codes_1", "Encoded codes 1 if existed (uint8/uint16/int32)")
40  .Input(3, "codes_n", "Encoded codes n if existed (uint8/uint16/int32)")
41  .Output(0, "decoded_0", "Decoded tensor for codes_0 (float)")
42  .Output(1, "decoded_1", "Decoded tensor for codes_1 (float)")
43  .Output(2, "decoded_n", "Decoded tensor for codes_n (float)");
44 
45 GRADIENT_OPERATOR_SCHEMA(QuantDecodeGradient)
46  .NumInputs([](int in) { return in >= 3 && in % 2 == 1; })
47  .NumOutputs(1);
48 
49 class GetQuantDecodeGradient : public GradientMakerBase {
50  using GradientMakerBase::GradientMakerBase;
51  vector<OperatorDef> GetGradientDefs() override {
52  CAFFE_ENFORCE_EQ(Def().input_size(), Def().output_size() + 1);
53  vector<string> gradient_op_inputs;
54  for (int i = 0; i < Def().input_size(); i++) {
55  gradient_op_inputs.push_back(I(i));
56  }
57  for (int i = 0; i < Def().output_size(); i++) {
58  gradient_op_inputs.push_back(GO(i));
59  }
60  return SingleGradientDef(
61  "QuantDecodeGradient", "", gradient_op_inputs, vector<string>{GI(0)});
62  }
63 };
64 
65 REGISTER_GRADIENT(QuantDecode, GetQuantDecodeGradient);
66 
67 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13