Caffe2 - C++ API
A deep learning, cross platform ML framework
copy_op.cc
1 #include "caffe2/mobile/contrib/arm-compute/core/context.h"
2 #include "caffe2/mobile/contrib/arm-compute/core/operator.h"
3 
4 namespace caffe2 {
5 
6 template <typename T> class CopyFromGLOp final : public Operator<GLContext> {
7 public:
8  CopyFromGLOp(const OperatorDef &operator_def, Workspace *ws)
9  : Operator<GLContext>(operator_def, ws) {}
10  virtual ~CopyFromGLOp() noexcept {}
11  USE_OPERATOR_FUNCTIONS(GLContext);
12  bool RunOnDevice() override;
13 private:
14  bool first_run_ = true, second_run_ = true;
15  std::vector<GLContext::deleted_unique_ptr<const GLTensor<T>>> inputs_;
16 };
17 
18 template <typename T>
20 
21  auto *X0blob = OperatorBase::Inputs()[0];
22  auto X0 = GLContext::getGLTensor<T>(X0blob);
23 
24  if (first_run_) {
25  inputs_.push_back(std::move(X0));
26  }
27  std::vector<const Blob*> inputsBlob;
28  inputsBlob.push_back(X0blob);
29 
30  if (first_run_) {
31  for (int i = 1; i < Inputs().size(); ++i) {
32  auto *Xblob = OperatorBase::Inputs()[i];
33  auto X = GLContext::getGLTensor<T>(Xblob);
34  inputs_.push_back(std::move(X));
35  }
36  }
37 
38  for (int i = 1; i < Inputs().size(); ++i) {
39  auto *Xblob = OperatorBase::Inputs()[i];
40  inputsBlob.push_back(Xblob);
41  }
42 
43  if (first_run_) {
44  first_run_ = false;
45  } else {
46  for (int i = 0; i < inputs_.size(); ++i) {
47  auto* X = inputs_[i].get();
48  auto* Xblob = inputsBlob[i];
49  X->lazy_allocate(Xblob, second_run_, second_run_);
50  }
51  if (second_run_) {
52  // Don't need to allocate output
53  second_run_ = false;
54  }
55  for (auto i = 0; i < Inputs().size(); ++i) {
56  // Blob
57  auto* Xblob = inputsBlob[i];
58  // GLTensor
59  auto* X = inputs_[i].get();
60  Output(i)->Resize(X->dims());
61  Output(i)->template mutable_data<float>();
62  // hardcoding CPU Tensors to be float
63  CAFFE_ENFORCE_EQ(Output(i)->nbytes(), X->size() * sizeof(float));
64  getTensorCPU(*X, *(OperatorBase::Outputs()[i]->template GetMutable<TensorCPU>()));
65  }
66  }
67  return true;
68 }
69 
70 REGISTER_GL_OPERATOR(CopyFromGL, CopyFromGLOp<DataType>);
71 
72 } // namespace caffe2
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:47
void Resize(Ts...dim_source)
Resizes a tensor.
Definition: tensor.h:288
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...