elementwise.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #pragma once
  15. #include "ultra_infer/core/fd_scalar.h"
  16. #include "ultra_infer/core/fd_tensor.h"
  17. namespace ultra_infer {
  18. namespace function {
  19. /** Excute the add operation for input FDTensors. *out = x + y.
  20. @param x The input tensor.
  21. @param y The input tensor.
  22. @param out The output tensor which stores the result.
  23. */
  24. ULTRAINFER_DECL void Add(const FDTensor &x, const FDTensor &y, FDTensor *out);
  25. /** Excute the subtract operation for input FDTensors. *out = x - y.
  26. @param x The input tensor.
  27. @param y The input tensor.
  28. @param out The output tensor which stores the result.
  29. */
  30. ULTRAINFER_DECL void Subtract(const FDTensor &x, const FDTensor &y,
  31. FDTensor *out);
  32. /** Excute the multiply operation for input FDTensors. *out = x * y.
  33. @param x The input tensor.
  34. @param y The input tensor.
  35. @param out The output tensor which stores the result.
  36. */
  37. ULTRAINFER_DECL void Multiply(const FDTensor &x, const FDTensor &y,
  38. FDTensor *out);
  39. /** Excute the divide operation for input FDTensors. *out = x / y.
  40. @param x The input tensor.
  41. @param y The input tensor.
  42. @param out The output tensor which stores the result.
  43. */
  44. ULTRAINFER_DECL void Divide(const FDTensor &x, const FDTensor &y,
  45. FDTensor *out);
  46. /** Excute the maximum operation for input FDTensors. *out = max(x, y).
  47. @param x The input tensor.
  48. @param y The input tensor.
  49. @param out The output tensor which stores the result.
  50. */
  51. ULTRAINFER_DECL void Maximum(const FDTensor &x, const FDTensor &y,
  52. FDTensor *out);
  53. } // namespace function
  54. ULTRAINFER_DECL FDTensor operator+(const FDTensor &x, const FDTensor &y);
  55. template <typename T> FDTensor operator+(const FDTensor &x, T y) {
  56. return x + FDTensor(Scalar(y));
  57. }
  58. template <typename T> FDTensor operator+(T x, const FDTensor &y) {
  59. return FDTensor(Scalar(x)) + y;
  60. }
  61. ULTRAINFER_DECL FDTensor operator-(const FDTensor &x, const FDTensor &y);
  62. template <typename T> FDTensor operator-(const FDTensor &x, T y) {
  63. return x - FDTensor(Scalar(y));
  64. }
  65. template <typename T> FDTensor operator-(T x, const FDTensor &y) {
  66. return FDTensor(Scalar(x)) - y;
  67. }
  68. ULTRAINFER_DECL FDTensor operator*(const FDTensor &x, const FDTensor &y);
  69. template <typename T> FDTensor operator*(const FDTensor &x, T y) {
  70. return x * FDTensor(Scalar(y));
  71. }
  72. template <typename T> FDTensor operator*(T x, const FDTensor &y) {
  73. return FDTensor(Scalar(x)) * y;
  74. }
  75. ULTRAINFER_DECL FDTensor operator/(const FDTensor &x, const FDTensor &y);
  76. template <typename T> FDTensor operator/(const FDTensor &x, T y) {
  77. return x / FDTensor(Scalar(y));
  78. }
  79. template <typename T> FDTensor operator/(T x, const FDTensor &y) {
  80. return FDTensor(Scalar(x)) / y;
  81. }
  82. } // namespace ultra_infer