Caffe2 - C++ API
A deep learning, cross platform ML framework
relu_cpu.cc
1 #include <ATen/core/dispatch/KernelRegistration.h>
2 #include "caffe2/operators/experimental/c10/schemas/relu.h"
3 #include "caffe2/utils/eigen_utils.h"
4 #include "caffe2/utils/math.h"
5 #include "caffe2/core/tensor.h"
6 
7 using caffe2::Tensor;
8 
9 namespace caffe2 {
10 namespace {
11 template <class DataType>
12 void relu_op_cpu_impl(
13  const at::Tensor& input_,
14  const at::Tensor& output_) {
15  Tensor input{C10Tensor(input_)};
16  Tensor output{C10Tensor(output_)};
17 
18  output.ResizeLike(input);
19 
20 #ifdef CAFFE2_USE_ACCELERATE
21  const float zero = 0.0f;
22  vDSP_vthres(
23  input.data<float>(),
24  1,
25  &zero,
26  output.mutable_data<float>(),
27  1,
28  input.size());
29 #else
30  caffe2::EigenVectorMap<float>(output.mutable_data<float>(), input.numel()) =
31  caffe2::ConstEigenVectorMap<float>(input.data<float>(), input.numel())
32  .cwiseMax(0.f);
33 #endif
34  /* Naive implementation
35  const float* input_data = input.data<float>();
36  float* output_data = output.mutable_data<float>();
37  for (int i = 0; i < input.size(); ++i) {
38  output_data[i] = std::max(input_data[i], 0.f);
39  }
40  */
41 }
42 } // namespace
43 } // namespace caffe2
44 
45 namespace c10 {
46 C10_REGISTER_KERNEL(caffe2::ops::Relu)
47  .kernel<decltype(caffe2::relu_op_cpu_impl<float>), &caffe2::relu_op_cpu_impl<float>>()
48  .dispatchKey(CPUTensorId());
49 } // namespace c10
Tensor class holds a shared pointer to the implementation TensorImpl, redirects API calls to TensorIm...
Definition: tensor.h:25
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
To register your own kernel for an operator, do in one (!) cpp file: C10_REGISTER_KERNEL(OperatorHand...
Definition: alias_info.h:7