1 #include "caffe2/operators/relu_op.h" 3 #include "caffe2/utils/math.h" 8 bool ReluOp<float, CPUContext>::RunOnDevice() {
13 #ifdef CAFFE2_USE_ACCELERATE 14 const float zero = 0.0f;
15 vDSP_vthres(X.data<
float>(), 1, &zero, Y->mutable_data<
float>(), 1, X.size());
17 EigenVectorMap<float>(Y->mutable_data<
float>(), X.size()) =
18 ConstEigenVectorMap<float>(X.data<
float>(), X.size()).cwiseMax(0.f);
31 bool ReluGradientOp<float, CPUContext>::RunOnDevice() {
35 CAFFE_ENFORCE_EQ(dY.size(), Y.size());
38 const float* Ydata = Y.data<
float>();
39 const float* dYdata = dY.data<
float>();
40 float* dXdata = dX->mutable_data<
float>();
42 EigenVectorArrayMap<float> dXvec(dXdata, dX->size());
43 ConstEigenVectorArrayMap<float> Yvec(Ydata, Y.size());
44 ConstEigenVectorArrayMap<float> dYvec(dYdata, dY.size());
45 dXvec = dYvec * Yvec.cwiseSign();
55 OpSchema::Cost CostInferenceForRelu(
56 const OperatorDef& def,
57 const vector<TensorShape>& in) {
58 struct OpSchema::Cost cost = PointwiseCostInference<0>(def, in);
59 cost.params_bytes = 0;
64 REGISTER_CPU_OPERATOR(Relu, ReluOp<float, CPUContext>);
65 REGISTER_CPU_OPERATOR(ReluGradient, ReluGradientOp<float, CPUContext>);
71 .AllowInplace({{0, 0}})
72 .CostInferenceFunction(CostInferenceForRelu)
73 .IdenticalTypeAndShape()
75 Relu takes one input data (Tensor<T>) and produces one output data 76 (Tensor<T>) where the rectified linear function, y = max(0, x), is applied to 77 the tensor elementwise. 79 .Input(0, "X",
"1D input tensor")
80 .Output(0,
"Y",
"1D input tensor")
81 .InheritOnnxSchema(
"Relu");
84 OPERATOR_SCHEMA(ReluGradient)
87 .AllowInplace({{1, 0}})
89 ReluGradient takes both Y and dY and uses this to update dX according to the 90 chain rule and derivatives of the rectified linear function. 94 using GradientMakerBase::GradientMakerBase;
95 vector<OperatorDef> GetGradientDefs()
override {
97 def_.type() +
"Gradient",
99 vector<string>{O(0), GO(0)},
100 vector<string>{GI(0)});
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
static vector< OperatorDef > SingleGradientDef(const Args &...args)
a helper function to allow one to create one single operator def, which is usually the case for many ...