1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
#include "caffe2/operators/relu_op.h"
#include <algorithm>
#include <functional>
#include <string>
#include "caffe2/utils/eigen_utils.h"
namespace caffe2 {
template <>
template <typename T>
bool ReluFunctor<CPUContext>::
operator()(const int N, const T* X, T* Y, CPUContext* /* context */) const {
EigenVectorMap<T>(Y, N) = ConstEigenVectorMap<float>(X, N).cwiseMax(T(0));
return true;
}
#ifdef CAFFE2_USE_ACCELERATE
template <>
template <>
bool ReluFunctor<CPUContext>::operator()<float>(
const int N,
const float* X,
float* Y,
CPUContext* /* context */) const {
const float zero = 0.0f;
vDSP_vthres(X, 1, &zero, Y, 1, N);
return true;
}
#endif // CAFFE2_USE_ACCELERATE
template <>
template <typename T>
bool ReluGradientFunctor<CPUContext>::Forward(
const std::vector<int>& Y_dims,
const std::vector<int>& /* dY_dims */,
const T* Y,
const T* dY,
T* dX,
CPUContext* /* context */) const {
const int size = std::accumulate(
// NOLINTNEXTLINE(modernize-use-transparent-functors)
Y_dims.cbegin(), Y_dims.cend(), 1, std::multiplies<int>());
EigenVectorArrayMap<T>(dX, size) =
(ConstEigenVectorArrayMap<T>(Y, size) > T(0))
.select(ConstEigenVectorArrayMap<T>(dY, size), T(0));
return true;
}
namespace {
OpSchema::Cost CostInferenceForRelu(
const OperatorDef& def,
const vector<TensorShape>& in) {
struct OpSchema::Cost cost = PointwiseCostInference<0>(def, in);
cost.params_bytes = 0;
return cost;
}
} // namespace
REGISTER_CPU_OPERATOR(
Relu,
UnaryElementwiseOp<
TensorTypes<float>,
CPUContext,
ReluFunctor<CPUContext>>);
REGISTER_CPU_GRADIENT_OPERATOR(
ReluGradient,
BinaryElementwiseOp<
TensorTypes<float>,
CPUContext,
ReluGradientFunctor<CPUContext>>);
// Input: X, output: Y
OPERATOR_SCHEMA(Relu)
.NumInputs(1)
.NumOutputs(1)
.AllowInplace({{0, 0}})
.CostInferenceFunction(CostInferenceForRelu)
.IdenticalTypeAndShape()
.SetDoc(R"DOC(
Applies rectified linear unit operation to the input data element-wise. The Relu operation takes one input $X$, produces one output $Y$, and is defined as:
$$Y = max(0,X)$$
Github Links:
- https://github.com/pytorch/pytorch/blob/master/caffe2/operators/relu_op.h
- https://github.com/pytorch/pytorch/blob/master/caffe2/operators/relu_op.cc
<details>
<summary> <b>Example</b> </summary>
**Code**
```
workspace.ResetWorkspace()
op = core.CreateOperator(
"Relu",
["X"],
["Y"]
)
workspace.FeedBlob("X", np.random.randn(4, 4).astype(np.float32)) // NCHW
print("X:\n", workspace.FetchBlob("X"), "\n")
workspace.RunOperatorOnce(op)
print("Y:\n", workspace.FetchBlob("Y"))
```
**Result**
```
X:
[[-1.4655551 0.64575136 0.7921748 0.4150579 ]
[ 0.41085166 -0.2837964 0.9881425 -1.9300346 ]
[ 0.39705405 0.44639114 0.9940703 0.2926532 ]
[-0.6726489 0.01330667 1.101319 0.33858967]]
Y:
[[0. 0.64575136 0.7921748 0.4150579 ]
[0.41085166 0. 0.9881425 0. ]
[0.39705405 0.44639114 0.9940703 0.2926532 ]
[0. 0.01330667 1.101319 0.33858967]]
```
</details>
)DOC")
.Input(0, "X", "1D input tensor")
.Output(0, "Y", "1D output tensor with same shape as input")
.InheritOnnxSchema();
// Input: Y, dY, output: dX
GRADIENT_OPERATOR_SCHEMA(ReluGradient)
.NumInputs(2)
.NumOutputs(1)
.AllowInplace({{1, 0}})
.IdenticalTypeAndShapeOfInput(1)
.SetDoc(R"DOC(
ReluGradient takes both Y and dY and uses this to update dX according to the
chain rule and derivatives of the rectified linear function.
)DOC");
namespace {
class GetReluGradient : public GradientMakerBase {
using GradientMakerBase::GradientMakerBase;
std::vector<OperatorDef> GetGradientDefs() override {
return SingleGradientDef(
def_.type() + "Gradient",
"",
std::vector<std::string>{O(0), GO(0)},
std::vector<std::string>{GI(0)});
}
};
} // namespace
REGISTER_GRADIENT(Relu, GetReluGradient);
} // namespace caffe2
|