Caffe2 - C++ API
A deep learning, cross platform ML framework
nnapi.h
1 
17 #include "caffe2/core/operator.h"
18 #include "caffe2/core/tensor.h"
19 #include "caffe2/core/types.h"
20 #include "caffe2/utils/proto_utils.h"
21 
22 #include "NeuralNetworks.h"
23 #include "dlnnapi.h"
24 
25 namespace caffe2 {
26 
27 class NNApi {
28  public:
29  using TensorVector = std::vector<TensorCPU*>;
30 
31  // Three different modes:
32  // ANEURALNETWORKS_PREFER_LOW_POWER
33  // ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER
34  // ANEURALNETWORKS_PREFER_SUSTAINED_SPEED
35  NNApi(
36  const NetDef& init_net,
37  const NetDef& run_net,
38  Workspace* ws = nullptr,
40  : order_(StorageOrder::NHWC),
41  preference_(pref),
42  run_net_(run_net),
43  ws_(ws) {
44  if (!loadNNApiLibrary()) {
45  CAFFE_THROW("NNApi is not supported");
46  }
47  CAFFE_ENFORCE(ws_.RunNetOnce(init_net));
48  }
49 
50  ~NNApi();
51 
52  bool loadNNApiLibrary();
53 
54  bool run(const TensorVector& inputs, TensorVector* outputs);
55 
56  private:
57  dlnnapi libnnapi_;
58  ANeuralNetworksModel* model_{nullptr};
59  ANeuralNetworksCompilation* compilation_{nullptr};
60  ANeuralNetworksExecution* run_{nullptr};
61  ANeuralNetworksEvent* run_end_{nullptr};
62  StorageOrder order_;
63  PreferenceCode preference_;
64  NetDef run_net_;
65  Workspace ws_;
66  OperandCode tensor_type_;
67  uint32_t operand_idx{0};
68  std::unordered_map<std::string, uint32_t> operand_map_;
69  // dimensions for the tensors
70  std::unordered_map<std::string, std::vector<uint32_t>> tensor_dims_;
71 
72  // mapping of the operator name "Conv" to OperatorType CONV
73  enum OperatorType {
74  AVERAGEPOOL,
75  CONV,
76  MAXPOOL,
77  RELU,
78  SOFTMAX,
79  };
80 
81  std::unordered_map<std::string, OperatorType> operator_map_{
82  {"AveragePool", AVERAGEPOOL},
83  {"Conv", CONV},
84  {"MaxPool", MAXPOOL},
85  {"Relu", RELU},
86  {"Softmax", SOFTMAX}};
87 
88  struct ConvPoolArgs {
89  int kernel_h{0};
90  int kernel_w{0};
91  int stride_x{0};
92  int stride_y{0};
93  int pad_t{0};
94  int pad_l{0};
95  int pad_b{0};
96  int pad_r{0};
97  };
98 
99  void getConvPoolArgs(const ArgumentHelper& helper, ConvPoolArgs& args);
100 
101  uint32_t addScalarOperand(int32_t val);
102 
103  uint32_t addFloatOperand(float val);
104 
105  uint32_t addTensorOperand(
106  const std::string& blob,
107  OperandCode type,
108  std::vector<uint32_t>& dims,
109  float scale = 1.0,
110  int32_t zero_point = 0);
111 
112  // lazily initialize model_ in run()
113  void init(const TensorVector& inputs, TensorVector* outputs);
114 
115  void addConv(const OperatorDef& op, bool fuse_relu = false);
116 
117  void addPooling(
118  const OperatorDef& op,
119  OperationCode op_code,
120  bool fuse_relu = false);
121 
122  void addRelu(const OperatorDef& op);
123 
124  void addSoftmax(const OperatorDef& op);
125 };
126 } // namespace caffe2
struct ANeuralNetworksExecution ANeuralNetworksExecution
ANeuralNetworksExecution is an opaque type that can be used to apply a machine learning model to a se...
struct ANeuralNetworksCompilation ANeuralNetworksCompilation
ANeuralNetworksCompilation is an opaque type that can be used to compile a machine learning model...
PreferenceCode
Execution preferences.
A helper class to index into arguments.
Definition: proto_utils.h:198
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:63
OperandCode
Operand types.
Copyright (c) 2016-present, Facebook, Inc.
struct ANeuralNetworksModel ANeuralNetworksModel
ANeuralNetworksModel is an opaque type that contains a description of the mathematical operations tha...
struct ANeuralNetworksEvent ANeuralNetworksEvent
ANeuralNetworksEvent is an opaque type that represents an event that will be signaled once an executi...
Prefer maximizing the throughput of successive frames, for example when processing successive frames ...
OperationCode
Operation types.