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
|
#include "caffe2/operators/elu_op.h"
#include "caffe2/operators/hip/activation_ops_miopen.h"
namespace caffe2 {
template <>
class MIOPENActivationOp<miopenActivationELU> final
: public MIOPENActivationOpBase {
public:
USE_OPERATOR_FUNCTIONS(HIPContext);
MIOPENActivationOp(const OperatorDef& operator_def, Workspace* ws)
: MIOPENActivationOpBase(operator_def, ws),
OP_SINGLE_ARG(float, "alpha", alpha_, 1.0f) {
MIOPEN_ENFORCE(miopenSetActivationDescriptor(
act_desc_,
miopenActivationELU,
static_cast<double>(alpha_),
0.0,
1.0));
}
bool RunOnDevice() override {
return DispatchHelper<TensorTypes<float, at::Half>>::call(this, Input(0));
}
template <typename T>
bool DoRunWithType() {
const auto& X = Input(0);
auto* Y = Output(0);
Y->ResizeLike(X);
if (X.size() == 0) {
Y->template mutable_data<T>();
return true;
}
if (X.sizes() != mio_dims_) {
VLOG(1) << "Setting descriptors.";
mio_dims_ = X.sizes().vec();
int C = 1, H = 1, W = 1;
if (X.ndim() == 4) {
// Normal 4-dimensional tensors for images.
C = X.dim32(1);
H = X.dim32(2);
W = X.dim32(3);
} else {
// If X is not 4-dimensional, we will simply use H = 1 and W = 1
// and wrap everything into C.
C = X.size() / X.dim32(0);
}
MIOPEN_ENFORCE(miopenSet4dTensorDescriptor(
data_desc_, miopenTypeWrapper<T>::type, X.dim32(0), C, H, W));
}
MIOPEN_ENFORCE(miopenActivationForward(
this->miopen_wrapper_.inline_miopen_handle(),
this->act_desc_,
miopenTypeWrapper<T>::kOne(),
this->data_desc_,
X.template data<T>(),
miopenTypeWrapper<T>::kZero(),
this->data_desc_,
Y->template mutable_data<T>()));
return true;
}
private:
const float alpha_;
};
template <>
class MIOPENActivationGradientOp<miopenActivationELU> final
: public MIOPENActivationOpBase {
public:
USE_OPERATOR_FUNCTIONS(HIPContext);
MIOPENActivationGradientOp(const OperatorDef& operator_def, Workspace* ws)
: MIOPENActivationOpBase(operator_def, ws),
OP_SINGLE_ARG(float, "alpha", alpha_, 1.0f) {
MIOPEN_ENFORCE(miopenSetActivationDescriptor(
act_desc_,
miopenActivationELU,
static_cast<double>(alpha_),
0.0,
1.0));
}
bool RunOnDevice() override {
return DispatchHelper<TensorTypes<float, at::Half>>::call(this, Input(0));
}
template <typename T>
bool DoRunWithType() {
const auto& Y = Input(0);
const auto& dY = Input(1);
auto* dX = Output(0);
dX->ResizeLike(Y);
if (Y.size() == 0) {
dX->template mutable_data<T>();
return true;
}
if (Y.sizes() != mio_dims_) {
VLOG(1) << "Setting descriptors.";
mio_dims_ = Y.sizes().vec();
int C = 1, H = 1, W = 1;
if (Y.ndim() == 4) {
// Normal 4-dimensional tensors for images.
C = Y.dim32(1);
H = Y.dim32(2);
W = Y.dim32(3);
} else {
// If Y is not 4-dimensional, we will simply use H = 1 and W = 1
// and wrap everything into C.
C = Y.size() / Y.dim32(0);
}
MIOPEN_ENFORCE(miopenSet4dTensorDescriptor(
data_desc_, miopenTypeWrapper<T>::type, Y.dim32(0), C, H, W));
}
MIOPEN_ENFORCE(miopenActivationBackward(
this->miopen_wrapper_.inline_miopen_handle(),
this->act_desc_,
miopenTypeWrapper<T>::kOne(),
this->data_desc_,
Y.template data<T>(),
this->data_desc_,
dY.template data<T>(),
this->data_desc_,
Y.template data<T>(),
miopenTypeWrapper<T>::kZero(),
this->data_desc_,
dX->template mutable_data<T>()));
return true;
}
private:
const float alpha_;
};
REGISTER_MIOPEN_OPERATOR(Elu, MIOPENActivationOp<miopenActivationELU>);
REGISTER_MIOPEN_OPERATOR(
EluGradient,
MIOPENActivationGradientOp<miopenActivationELU>);
} // namespace caffe2
|