Caffe2 - C++ API
A deep learning, cross platform ML framework
byte_weight_dequant_op.h
1 #ifndef CAFFE2_OPERATORS_BYTE_WEIGHT_DEQUANT_OP_H_
2 #define CAFFE2_OPERATORS_BYTE_WEIGHT_DEQUANT_OP_H_
3 
4 #include "caffe2/core/operator.h"
5 #include "caffe2/utils/eigen_utils.h"
6 #include "caffe2/utils/math.h"
7 
8 namespace caffe2 {
9 
10 template <typename Context>
11 class ByteWeightDequantOp : public Operator<Context> {
12  public:
13  ByteWeightDequantOp(const OperatorDef& operator_def, Workspace* ws)
14  : Operator<Context>(operator_def, ws),
15  min_(this->template GetSingleArgument<float>("min", -3)),
16  max_(this->template GetSingleArgument<float>("max", 3)),
17  shape_(this->template GetRepeatedArgument<int64_t>("shape")) {}
18 
19  USE_OPERATOR_FUNCTIONS(Context);
21 
22  bool RunOnDevice() override {
23  const auto& WI = Input(0);
24 
25  auto* Y = Output(0, shape_, at::dtype<float>());
26  float bin_interval = (max_ - min_) / 255.0;
27  int total = 1;
28  for (int i = 0; i < shape_.size(); i++) {
29  total *= Y->size(i);
30  }
31  const uint8_t* Xdata;
32  if (WI.template IsType<uint8_t>()) {
33  CAFFE_ENFORCE(total, WI.nbytes());
34  Xdata = WI.template data<uint8_t>();
35  } else {
36  CAFFE_ENFORCE(total, WI.template data<std::string>()[0].size());
37  Xdata = reinterpret_cast<const uint8_t*>(
38  WI.template data<std::string>()[0].c_str());
39  }
40  auto* Ydata = Y->template mutable_data<float>();
41  ConstEigenVectorMap<uint8_t> index(&Xdata[0], total);
42  EigenVectorMap<float> weights(&Ydata[0], total);
43  weights = (index.cast<float>().array() * bin_interval) + min_;
44  return true;
45  }
46 
47  private:
48  float min_;
49  float max_;
50  std::vector<int64_t> shape_;
51 };
52 
53 } // namespace caffe2
54 
55 #endif // CAFFE2_OPERATORS_BYTE_WEIGHT_DEQUANT_OP_H_
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:47
const Tensor & Input(int idx, DeviceType type=Context::GetDeviceType())
Retrieve a non-owning reference to the input at position &#39;idx&#39; for this operator. ...
Definition: operator.h:702
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13