Caffe2 - C++ API
A deep learning, cross platform ML framework
nnapi.h
1 #include "caffe2/core/operator.h"
2 #include "caffe2/core/tensor.h"
3 #include "caffe2/core/types.h"
4 #include "caffe2/utils/proto_utils.h"
5 
6 #include "NeuralNetworks.h"
7 #include "dlnnapi.h"
8 
9 namespace caffe2 {
10 
11 class NNApi {
12  public:
13  using TensorVector = std::vector<TensorCPU*>;
14 
15  // Three different modes:
16  // ANEURALNETWORKS_PREFER_LOW_POWER
17  // ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER
18  // ANEURALNETWORKS_PREFER_SUSTAINED_SPEED
19  NNApi(
20  const NetDef& init_net,
21  const NetDef& run_net,
22  Workspace* ws = nullptr,
24  : order_(StorageOrder::NHWC),
25  preference_(pref),
26  run_net_(run_net),
27  ws_(ws) {
28  if (!loadNNApiLibrary()) {
29  CAFFE_THROW("NNApi is not supported");
30  }
31  CAFFE_ENFORCE(ws_.RunNetOnce(init_net));
32  }
33 
34  ~NNApi();
35 
36  bool loadNNApiLibrary();
37 
38  bool run(const TensorVector& inputs, TensorVector* outputs);
39 
40  private:
41  dlnnapi libnnapi_;
42  ANeuralNetworksModel* model_{nullptr};
43  ANeuralNetworksCompilation* compilation_{nullptr};
44  ANeuralNetworksExecution* run_{nullptr};
45  ANeuralNetworksEvent* run_end_{nullptr};
46  StorageOrder order_;
47  PreferenceCode preference_;
48  NetDef run_net_;
49  Workspace ws_;
50  OperandCode tensor_type_;
51  uint32_t operand_idx{0};
52  std::unordered_map<std::string, uint32_t> operand_map_;
53  // dimensions for the tensors
54  std::unordered_map<std::string, std::vector<uint32_t>> tensor_dims_;
55 
56  // mapping of the operator name "Conv" to OperatorType CONV
57  enum OperatorType {
58  AVERAGEPOOL,
59  CONV,
60  MAXPOOL,
61  RELU,
62  SOFTMAX,
63  };
64 
65  std::unordered_map<std::string, OperatorType> operator_map_{
66  {"AveragePool", AVERAGEPOOL},
67  {"Conv", CONV},
68  {"MaxPool", MAXPOOL},
69  {"Relu", RELU},
70  {"Softmax", SOFTMAX}};
71 
72  struct ConvPoolArgs {
73  int kernel_h{0};
74  int kernel_w{0};
75  int stride_x{0};
76  int stride_y{0};
77  int pad_t{0};
78  int pad_l{0};
79  int pad_b{0};
80  int pad_r{0};
81  };
82 
83  void getConvPoolArgs(const ArgumentHelper& helper, ConvPoolArgs& args);
84 
85  uint32_t addScalarOperand(int32_t val);
86 
87  uint32_t addFloatOperand(float val);
88 
89  uint32_t addTensorOperand(
90  const std::string& blob,
91  OperandCode type,
92  std::vector<uint32_t>& dims,
93  float scale = 1.0,
94  int32_t zero_point = 0);
95 
96  // lazily initialize model_ in run()
97  void init(const TensorVector& inputs, TensorVector* outputs);
98 
99  void addConv(const OperatorDef& op, bool fuse_relu = false);
100 
101  void addPooling(
102  const OperatorDef& op,
103  OperationCode op_code,
104  bool fuse_relu = false);
105 
106  void addRelu(const OperatorDef& op);
107 
108  void addSoftmax(const OperatorDef& op);
109 };
110 } // 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:200
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:47
OperandCode
Operand types.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
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.