Caffe2 - C++ API
A deep learning, cross platform ML framework
conv_relu_op.cc
1 #include "conv_relu_op.h"
2 
3 namespace caffe2 {
4 
5 template <typename T, class Context>
6 bool ConvReluOp<T, Context>::RunOnDeviceWithOrderNCHW() {
7  // Delegate to local conv operator
8  for (int i = 0; i < this->InputSize(); ++i) {
9  local_input_blobs_[i]->ShareExternal(
10  const_cast<void*>(this->Inputs()[i]->GetRaw()),
11  this->Inputs()[i]->meta());
12  }
13 
14  if (!local_op_->RunOnDeviceWithOrderNCHW()) {
15  return false;
16  }
17 
18  // Apply Relu
19  Tensor* local_output =
20  BlobGetMutableTensor(local_output_blobs_[0], Context::GetDeviceType());
21  const T* output_local_data = local_output->template data<T>();
22 
23  Tensor* output =
24  Operator<Context>::Output(0, local_output->sizes(), at::dtype<T>());
25  T* output_data = output->template mutable_data<T>();
26 #ifdef _OPENMP
27 #pragma omp parallel for
28 #endif
29  for (int i = 0; i < output->numel(); ++i) {
30  output_data[i] = std::max(static_cast<T>(0), output_local_data[i]);
31  }
32 
33  return true;
34 }
35 
36 template <typename T, class Context>
37 bool ConvReluOp<T, Context>::RunOnDeviceWithOrderNHWC() {
38  // Delegate to local conv operator
39  for (int i = 0; i < this->InputSize(); ++i) {
40  local_input_blobs_[i]->ShareExternal(
41  const_cast<void*>(this->Inputs()[i]->GetRaw()),
42  this->Inputs()[i]->meta());
43  }
44 
45  if (!local_op_->RunOnDeviceWithOrderNHWC()) {
46  return false;
47  }
48 
49  // Apply Relu
50  Tensor* local_output =
51  BlobGetMutableTensor(local_output_blobs_[0], Context::GetDeviceType());
52  const T* output_local_data = local_output->template data<T>();
53 
54  Tensor* output =
55  Operator<Context>::Output(0, local_output->sizes(), at::dtype<T>());
56  T* output_data = output->template mutable_data<T>();
57 #ifdef _OPENMP
58 #pragma omp parallel for
59 #endif
60  for (int i = 0; i < output->numel(); ++i) {
61  output_data[i] = std::max(static_cast<T>(0), output_local_data[i]);
62  }
63 
64  return true;
65 }
66 
67 REGISTER_CPU_OPERATOR(ConvRelu, ConvReluOp<float, CPUContext>);
68 
69 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13