diff --git a/paddle/fluid/framework/CMakeLists.txt b/paddle/fluid/framework/CMakeLists.txt index 156ce70afc4c84b8f31b406ed44abaf7230cdba9..31c71928bb00c0a4b6c73eeca9e75a6b66038e8f 100755 --- a/paddle/fluid/framework/CMakeLists.txt +++ b/paddle/fluid/framework/CMakeLists.txt @@ -1191,6 +1191,7 @@ cc_library( phi_tensor op_meta_info phi_api + tensor_api phi_tensor_operants operants_manager) diff --git a/paddle/fluid/prim/tests/CMakeLists.txt b/paddle/fluid/prim/tests/CMakeLists.txt index f42eb8db5682ae38bf42a0dfc046c9085e4bc58a..cc082a8429987862c054d9a53a102447458b9f17 100644 --- a/paddle/fluid/prim/tests/CMakeLists.txt +++ b/paddle/fluid/prim/tests/CMakeLists.txt @@ -35,13 +35,15 @@ cc_test_old( phi_dygraph_api static_global_utils static_tensor_operants + tensor_api operants_manager) if(NOT (NOT WITH_PYTHON AND ON_INFER)) cc_library( init_env_utils SRCS init_env_utils.cc - DEPS operants_manager eager_tensor_operants static_tensor_operants) + DEPS operants_manager tensor_api eager_tensor_operants + static_tensor_operants) cc_test_old( test_comp_eager diff --git a/paddle/fluid/pybind/CMakeLists.txt b/paddle/fluid/pybind/CMakeLists.txt index f06efbb211a0140b8febd3fd5006f8b34d84c4ce..c1855fac066e7b4597ca172765428e0dd13d5af8 100755 --- a/paddle/fluid/pybind/CMakeLists.txt +++ b/paddle/fluid/pybind/CMakeLists.txt @@ -497,6 +497,7 @@ if(WITH_PYTHON) list(APPEND PYBIND_DEPS python) list(APPEND PYBIND_DEPS custom_operator) list(APPEND PYBIND_DEPS custom_operator_node) + list(APPEND PYBIND_DEPS tensor_api) list(APPEND PYBIND_DEPS operants_manager) list(APPEND PYBIND_DEPS eager_tensor_operants) list(APPEND PYBIND_DEPS static_tensor_operants) diff --git a/paddle/phi/api/include/tensor.h b/paddle/phi/api/include/tensor.h index 40789b68db09f8c1b5fbaffc6ace36f941d781b0..4924ff3f1a2a391d70fcc75bcc924b19c4fa58e7 100644 --- a/paddle/phi/api/include/tensor.h +++ b/paddle/phi/api/include/tensor.h @@ -524,6 +524,20 @@ class PADDLE_API Tensor final { */ Tensor& operator=(Tensor&& x) &; + /** + * @brief Tensor operants + * + * @param other + * @return Tensor + */ + Tensor operator+(const Tensor& other) const; + + Tensor operator-(const Tensor& other) const; + + Tensor operator*(const Tensor& other) const; + + Tensor operator/(const Tensor& other) const; + /* Part 8: Autograd methods */ /** @@ -633,13 +647,5 @@ class PADDLE_API Tensor final { std::string name_{""}; }; -PADDLE_API Tensor operator+(const Tensor& x, const Tensor& y); - -PADDLE_API Tensor operator-(const Tensor& x, const Tensor& y); - -PADDLE_API Tensor operator*(const Tensor& x, const Tensor& y); - -PADDLE_API Tensor operator/(const Tensor& x, const Tensor& y); - } // namespace experimental } // namespace paddle diff --git a/paddle/phi/api/lib/CMakeLists.txt b/paddle/phi/api/lib/CMakeLists.txt index e3b83a4ad33771ce896cdb330c73554bf708095d..9ed29fdff6380b86c2f8b527a8219dea0de2dc55 100644 --- a/paddle/phi/api/lib/CMakeLists.txt +++ b/paddle/phi/api/lib/CMakeLists.txt @@ -5,19 +5,19 @@ if(WITH_GPU) phi_tensor_raw SRCS tensor.cc DEPS tensor_base dense_tensor phi_api_utils phi_enforce context_pool - operants_manager) + tensor_api) elseif(WITH_ROCM) hip_library( phi_tensor_raw SRCS tensor.cc DEPS tensor_base dense_tensor phi_api_utils phi_enforce context_pool - operants_manager) + tensor_api) else() cc_library( phi_tensor_raw SRCS tensor.cc DEPS tensor_base dense_tensor phi_api_utils phi_enforce context_pool - operants_manager) + tensor_api) endif() set(api_gen_base ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/api_base.py) @@ -388,3 +388,7 @@ cc_library( operants_manager SRCS ${operants_manager_source_file} DEPS phi_enforce) +cc_library( + tensor_api + SRCS tensor_api.cc + DEPS operants_manager) diff --git a/paddle/phi/api/lib/tensor.cc b/paddle/phi/api/lib/tensor.cc index 239b88b327fad11e53d6977b3a569f8cda93675f..2448e88b65a96494d46d423b5d1dbc8255c1653e 100644 --- a/paddle/phi/api/lib/tensor.cc +++ b/paddle/phi/api/lib/tensor.cc @@ -21,7 +21,6 @@ limitations under the License. */ #include "glog/logging.h" #include "paddle/phi/api/include/context_pool.h" -#include "paddle/phi/api/include/operants_manager.h" #include "paddle/phi/api/lib/utils/allocator.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/backends/gpu/gpu_info.h" @@ -434,21 +433,5 @@ void Tensor::reset_inplace_version(bool set_to_zero) { } } -PADDLE_API Tensor operator+(const Tensor &x, const Tensor &y) { - return paddle::OperantsManager::Instance().add(x, y); -} - -PADDLE_API Tensor operator-(const Tensor &x, const Tensor &y) { - return paddle::OperantsManager::Instance().subtract(x, y); -} - -PADDLE_API Tensor operator*(const Tensor &x, const Tensor &y) { - return paddle::OperantsManager::Instance().multiply(x, y); -} - -PADDLE_API Tensor operator/(const Tensor &x, const Tensor &y) { - return paddle::OperantsManager::Instance().divide(x, y); -} - } // namespace experimental } // namespace paddle diff --git a/paddle/phi/api/lib/tensor_api.cc b/paddle/phi/api/lib/tensor_api.cc new file mode 100644 index 0000000000000000000000000000000000000000..c063730155e8a4227bbeecc815248fc36469ec6a --- /dev/null +++ b/paddle/phi/api/lib/tensor_api.cc @@ -0,0 +1,43 @@ +/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#include "paddle/phi/api/include/tensor.h" + +#include "paddle/phi/api/include/operants_manager.h" + +namespace paddle { +namespace experimental { + +Tensor Tensor::operator+(const Tensor &other) const { + return paddle::OperantsManager::Instance().add( + static_cast(*this), other); +} + +Tensor Tensor::operator-(const Tensor &other) const { + return paddle::OperantsManager::Instance().subtract( + static_cast(*this), other); +} + +Tensor Tensor::operator*(const Tensor &other) const { + return paddle::OperantsManager::Instance().multiply( + static_cast(*this), other); +} + +Tensor Tensor::operator/(const Tensor &other) const { + return paddle::OperantsManager::Instance().divide( + static_cast(*this), other); +} + +} // namespace experimental +} // namespace paddle