TensorOperators.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #pragma once
  2. #include <ATen/core/Tensor.h>
  3. #include <c10/core/Scalar.h>
  4. #ifndef AT_PER_OPERATOR_HEADERS
  5. #include <ATen/Functions.h>
  6. #else
  7. #include <ATen/ops/empty_like.h>
  8. #endif
  9. namespace at {
  10. #define AT_FORALL_BINARY_OPS(_) \
  11. _(+, x.add(y), y.add(x)) \
  12. _(*, x.mul(y), y.mul(x)) \
  13. _(-, \
  14. x.sub(y), \
  15. ::at::empty_like(y, at::MemoryFormat::Preserve).fill_(x).sub_(y)) \
  16. _(/, \
  17. x.div(y), \
  18. ::at::empty_like(y, at::MemoryFormat::Preserve).fill_(x).div_(y)) \
  19. _(%, \
  20. x.remainder(y), \
  21. ::at::empty_like(y, at::MemoryFormat::Preserve).fill_(x).remainder_(y)) \
  22. _(&, x.bitwise_and(y), y.bitwise_and(x)) \
  23. _(|, x.bitwise_or(y), y.bitwise_or(x)) \
  24. _(^, x.bitwise_xor(y), y.bitwise_xor(x)) \
  25. _(<, x.lt(y), y.gt(x)) \
  26. _(<=, x.le(y), y.ge(x)) \
  27. _(>, x.gt(y), y.lt(x)) \
  28. _(>=, x.ge(y), y.le(x)) \
  29. _(==, x.eq(y), y.eq(x)) \
  30. _(!=, x.ne(y), y.ne(x))
  31. #define DEFINE_OPERATOR(op, body, reverse_scalar_body) \
  32. static inline Tensor operator op(const Tensor& x, const Tensor& y) { \
  33. return body; \
  34. } \
  35. static inline Tensor operator op(const Tensor& x, const Scalar& y) { \
  36. return body; \
  37. } \
  38. static inline Tensor operator op(const Scalar& x, const Tensor& y) { \
  39. return reverse_scalar_body; \
  40. }
  41. AT_FORALL_BINARY_OPS(DEFINE_OPERATOR)
  42. #undef DEFINE_OPERATOR
  43. #undef AT_FORALL_BINARY_OPS
  44. } // namespace at