custom_relu_op.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (c) 2021 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. // reference from : https://github.com/PaddlePaddle/Paddle-Inference-Demo/blob/master/python/custom-operator/custom_relu_op.cc
  15. #include <iostream>
  16. #include <vector>
  17. #include "paddle/extension.h"
  18. template <typename data_t>
  19. void relu_cpu_forward_kernel(const data_t* x_data,
  20. data_t* out_data,
  21. int64_t x_numel) {
  22. for (int i = 0; i < x_numel; ++i) {
  23. out_data[i] = std::max(static_cast<data_t>(0.), x_data[i]);
  24. }
  25. }
  26. template <typename data_t>
  27. void relu_cpu_backward_kernel(const data_t* grad_out_data,
  28. const data_t* out_data,
  29. data_t* grad_x_data,
  30. int64_t out_numel) {
  31. for (int i = 0; i < out_numel; ++i) {
  32. grad_x_data[i] =
  33. grad_out_data[i] * (out_data[i] > static_cast<data_t>(0) ? 1. : 0.);
  34. }
  35. }
  36. std::vector<paddle::Tensor> relu_cpu_forward(const paddle::Tensor& x) {
  37. auto out = paddle::Tensor(paddle::PlaceType::kCPU);
  38. out.reshape(x.shape());
  39. PD_DISPATCH_FLOATING_TYPES(
  40. x.type(), "relu_cpu_forward", ([&] {
  41. relu_cpu_forward_kernel<data_t>(
  42. x.data<data_t>(), out.mutable_data<data_t>(x.place()), x.size());
  43. }));
  44. return {out};
  45. }
  46. std::vector<paddle::Tensor> relu_cpu_backward(const paddle::Tensor& x,
  47. const paddle::Tensor& out,
  48. const paddle::Tensor& grad_out) {
  49. auto grad_x = paddle::Tensor(paddle::PlaceType::kCPU);
  50. grad_x.reshape(x.shape());
  51. PD_DISPATCH_FLOATING_TYPES(out.type(), "relu_cpu_backward", ([&] {
  52. relu_cpu_backward_kernel<data_t>(
  53. grad_out.data<data_t>(),
  54. out.data<data_t>(),
  55. grad_x.mutable_data<data_t>(x.place()),
  56. out.size());
  57. }));
  58. return {grad_x};
  59. }
  60. std::vector<paddle::Tensor> relu_cuda_forward(const paddle::Tensor& x);
  61. std::vector<paddle::Tensor> relu_cuda_backward(const paddle::Tensor& x,
  62. const paddle::Tensor& out,
  63. const paddle::Tensor& grad_out);
  64. std::vector<paddle::Tensor> ReluForward(const paddle::Tensor& x) {
  65. // TODO(chenweihang): Check Input
  66. if (x.place() == paddle::PlaceType::kCPU) {
  67. return relu_cpu_forward(x);
  68. } else if (x.place() == paddle::PlaceType::kGPU) {
  69. return relu_cuda_forward(x);
  70. } else {
  71. throw std::runtime_error("Not implemented.");
  72. }
  73. }
  74. std::vector<paddle::Tensor> ReluBackward(const paddle::Tensor& x,
  75. const paddle::Tensor& out,
  76. const paddle::Tensor& grad_out) {
  77. // TODO(chenweihang): Check Input
  78. if (x.place() == paddle::PlaceType::kCPU) {
  79. return relu_cpu_backward(x, out, grad_out);
  80. } else if (x.place() == paddle::PlaceType::kGPU) {
  81. return relu_cuda_backward(x, out, grad_out);
  82. } else {
  83. throw std::runtime_error("Not implemented.");
  84. }
  85. }
  86. PD_BUILD_OP(custom_relu)
  87. .Inputs({"X"})
  88. .Outputs({"Out"})
  89. .SetKernelFn(PD_KERNEL(ReluForward));
  90. PD_BUILD_GRAD_OP(custom_relu)
  91. .Inputs({"X", "Out", paddle::Grad("Out")})
  92. .Outputs({paddle::Grad("X")})
  93. .SetKernelFn(PD_KERNEL(ReluBackward));