|
| 1 | +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <iostream> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +#include "paddle/include/experimental/ext_all.h" |
| 19 | + |
| 20 | +template <typename data_t> |
| 21 | +void relu_cpu_forward_kernel(const data_t* x_data, |
| 22 | + data_t* out_data, |
| 23 | + int64_t x_numel) { |
| 24 | + for (int i = 0; i < x_numel; ++i) { |
| 25 | + out_data[i] = std::max(static_cast<data_t>(0.), x_data[i]); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +template <typename data_t> |
| 30 | +void relu_cpu_backward_kernel(const data_t* grad_out_data, |
| 31 | + const data_t* out_data, |
| 32 | + data_t* grad_x_data, |
| 33 | + int64_t out_numel) { |
| 34 | + for (int i = 0; i < out_numel; ++i) { |
| 35 | + grad_x_data[i] = |
| 36 | + grad_out_data[i] * (out_data[i] > static_cast<data_t>(0) ? 1. : 0.); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +std::vector<paddle::Tensor> relu_cpu_forward(const paddle::Tensor& x) { |
| 41 | + auto out = paddle::Tensor(paddle::PlaceType::kCPU, x.shape()); |
| 42 | + |
| 43 | + PD_DISPATCH_FLOATING_TYPES( |
| 44 | + x.type(), "relu_cpu_forward", ([&] { |
| 45 | + relu_cpu_forward_kernel<data_t>( |
| 46 | + x.data<data_t>(), out.mutable_data<data_t>(x.place()), x.size()); |
| 47 | + })); |
| 48 | + |
| 49 | + return {out}; |
| 50 | +} |
| 51 | + |
| 52 | +std::vector<paddle::Tensor> relu_cpu_backward(const paddle::Tensor& x, |
| 53 | + const paddle::Tensor& out, |
| 54 | + const paddle::Tensor& grad_out) { |
| 55 | + auto grad_x = paddle::Tensor(paddle::PlaceType::kCPU, x.shape()); |
| 56 | + |
| 57 | + PD_DISPATCH_FLOATING_TYPES(out.type(), "relu_cpu_backward", ([&] { |
| 58 | + relu_cpu_backward_kernel<data_t>( |
| 59 | + grad_out.data<data_t>(), |
| 60 | + out.data<data_t>(), |
| 61 | + grad_x.mutable_data<data_t>(x.place()), |
| 62 | + out.size()); |
| 63 | + })); |
| 64 | + |
| 65 | + return {grad_x}; |
| 66 | +} |
| 67 | + |
| 68 | +std::vector<paddle::Tensor> ReluForward(const paddle::Tensor& x) { |
| 69 | + return relu_cpu_forward(x); |
| 70 | +} |
| 71 | + |
| 72 | +std::vector<paddle::Tensor> ReluBackward(const paddle::Tensor& x, |
| 73 | + const paddle::Tensor& out, |
| 74 | + const paddle::Tensor& grad_out) { |
| 75 | + return relu_cpu_backward(x, out, grad_out); |
| 76 | +} |
| 77 | + |
| 78 | +PD_BUILD_OP(custom_relu) |
| 79 | + .Inputs({"X"}) |
| 80 | + .Outputs({"Out"}) |
| 81 | + .SetKernelFn(PD_KERNEL(ReluForward)); |
| 82 | + |
| 83 | +PD_BUILD_GRAD_OP(custom_relu) |
| 84 | + .Inputs({"X", "Out", paddle::Grad("Out")}) |
| 85 | + .Outputs({paddle::Grad("X")}) |
| 86 | + .SetKernelFn(PD_KERNEL(ReluBackward)); |
0 commit comments