Caffe2 - C++ API
A deep learning, cross platform ML framework
ios_caffe_predictor.cc
1 
17 #include "caffe2/mobile/contrib/ios/ios_caffe_predictor.h"
18 #include "caffe2/core/flags.h"
19 #include "caffe2/core/tensor.h"
20 
21 #if defined(CAFFE2_USE_MPSCNN) && CAFFE2_MOBILE
22 #include "caffe2/mobile/contrib/ios/mpscnn/mpscnn.h"
23 #endif
24 
25 CAFFE2_DECLARE_bool(caffe2_force_shared_col_buffer);
26 
28  const caffe2::NetDef& predict_net,
29  bool disableMultithreadProcessing,
30  bool allowMetalOperators) {
31  caffe2::NetDef metal_predict_net;
32  bool usingMetalOperators = false;
33 #if defined(CAFFE2_USE_MPSCNN) && CAFFE2_MOBILE
34  if (allowMetalOperators) {
35  caffe2::dumpDef(predict_net);
36  if (caffe2::tryConvertToMPSCNN(init_net, predict_net, &metal_predict_net)) {
37  LOG(INFO) << "Successfully converted to MPSCNN";
38  caffe2::dumpDef(metal_predict_net);
39  usingMetalOperators = true;
40  } else {
41  LOG(ERROR) << "Failed converting model to MPSCNN";
42  }
43  }
44 #endif
45 
46  return new Caffe2IOSPredictor(init_net,
47  usingMetalOperators ? metal_predict_net : predict_net,
48  disableMultithreadProcessing,
49  usingMetalOperators);
50 }
51 
52 Caffe2IOSPredictor::Caffe2IOSPredictor(const caffe2::NetDef& init_net,
53  const caffe2::NetDef& predict_net,
54  bool disableMultithreadProcessing,
55  bool usingMetalOperators)
56  : usingMetalOperators(usingMetalOperators), predictor_(init_net, predict_net) {
57 #if CAFFE2_MOBILE
58  if (disableMultithreadProcessing) {
59  caffe2::ThreadPool* threadpool = predictor_.ws()->GetThreadPool();
60  if (threadpool != nullptr) {
61  threadpool->setMinWorkSize(std::numeric_limits<size_t>::max());
62  }
63  }
64 #endif
65 }
66 
67 void Caffe2IOSPredictor::run(const Tensor& inData, Tensor& outData, std::string& errorMessage) {
68  caffe2::FLAGS_caffe2_force_shared_col_buffer = true;
69  caffe2::TensorCPU input;
70  input.Resize(inData.dims);
71  input.ShareExternalPointer(inData.data);
72  caffe2::Predictor::TensorVector input_vec{&input};
73  caffe2::Predictor::TensorVector output_vec;
74  try {
75  predictor_.run(input_vec, &output_vec);
76  } catch (const caffe2::EnforceNotMet& e) {
77  std::string error = e.msg();
78  errorMessage.swap(error);
79  return;
80  } catch (const std::exception& e) {
81  std::string error = e.what();
82  errorMessage.swap(error);
83  return;
84  }
85  caffe2::TensorCPU* output = output_vec.front();
86  outData.data = output->mutable_data<uint8_t>();
87  outData.dims = output->dims();
88 }
void ShareExternalPointer(T *src, size_t capacity=0, Deleter d=nullptr)
Shares the data with an externally managed pointer.
Definition: tensor.h:444
T * mutable_data()
Returns a typed pointer of the underlying storage.
Definition: tensor.h:594
const vector< TIndex > & dims() const
Returns the dimensions of the tensor as a vector.
Definition: tensor.h:627
void Resize(Ts...dim_source)
Resizes a tensor.
Definition: tensor.h:304
Copyright (c) 2016-present, Facebook, Inc.
Copyright (c) 2016-present, Facebook, Inc.
static Caffe2IOSPredictor * NewCaffe2IOSPredictor(const caffe2::NetDef &init_net, const caffe2::NetDef &predict_net, bool disableMultithreadProcessing, bool allowMetalOperators)
Allow converting eligible operators to Metal GPU framework accelerated operators. ...