From 8c844356016e8cf0d59ceeeb0d7034d7eec10d2f Mon Sep 17 00:00:00 2001 From: HongyuJia Date: Mon, 20 Feb 2023 14:08:22 +0800 Subject: [PATCH] [Tensor operants] Polish tensor operants implementation (#50634) * polish tensor operants implementation * change year, 2021->2023 --- paddle/fluid/framework/CMakeLists.txt | 1 + paddle/fluid/prim/tests/CMakeLists.txt | 4 ++- paddle/fluid/pybind/CMakeLists.txt | 1 + paddle/phi/api/include/tensor.h | 22 ++++++++----- paddle/phi/api/lib/CMakeLists.txt | 10 ++++-- paddle/phi/api/lib/tensor.cc | 17 ---------- paddle/phi/api/lib/tensor_api.cc | 43 ++++++++++++++++++++++++++ 7 files changed, 69 insertions(+), 29 deletions(-) create mode 100644 paddle/phi/api/lib/tensor_api.cc diff --git a/paddle/fluid/framework/CMakeLists.txt b/paddle/fluid/framework/CMakeLists.txt index 156ce70afc4..31c71928bb0 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 f42eb8db568..cc082a84299 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 f06efbb211a..c1855fac066 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 40789b68db0..4924ff3f1a2 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 e3b83a4ad33..9ed29fdff63 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 239b88b327f..2448e88b65a 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 00000000000..c063730155e --- /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 -- GitLab