From 7a44c35280ee1a00f84957c4851ef74ab14573dd Mon Sep 17 00:00:00 2001 From: jiahongyu Date: Sat, 18 Feb 2023 08:17:11 +0000 Subject: [PATCH 1/7] polish tensor operants implementation --- 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 db67739905cc3b..131532b386a07a 100755 --- a/paddle/fluid/framework/CMakeLists.txt +++ b/paddle/fluid/framework/CMakeLists.txt @@ -1203,6 +1203,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 f42eb8db5682ae..cc082a84299878 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 f06efbb211a014..c1855fac066e7b 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 40789b68db09f8..4924ff3f1a2a39 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 1da376b7efe3b7..4e5e6faec6510b 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) @@ -337,3 +337,7 @@ cc_library( operants_manager SRCS operants_manager.cc 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 239b88b327fad1..2448e88b65a964 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 00000000000000..3b260075e2ff11 --- /dev/null +++ b/paddle/phi/api/lib/tensor_api.cc @@ -0,0 +1,43 @@ +/* Copyright (c) 2021 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 From ea148e73f90fb96d44256846bfb091f9317c9f06 Mon Sep 17 00:00:00 2001 From: jiahongyu Date: Sat, 18 Feb 2023 08:24:26 +0000 Subject: [PATCH 2/7] change year, 2021->2023 --- paddle/phi/api/lib/tensor_api.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/phi/api/lib/tensor_api.cc b/paddle/phi/api/lib/tensor_api.cc index 3b260075e2ff11..c063730155e8a4 100644 --- a/paddle/phi/api/lib/tensor_api.cc +++ b/paddle/phi/api/lib/tensor_api.cc @@ -1,4 +1,4 @@ -/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +/* 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. From ff2382f32a719b22f15a606d44a9d391f0945636 Mon Sep 17 00:00:00 2001 From: jiahongyu Date: Sun, 19 Feb 2023 07:46:32 +0000 Subject: [PATCH 3/7] autogen tensor.h and tensor_api.cc --- .gitignore | 2 + paddle/phi/api/lib/CMakeLists.txt | 40 +++-- paddle/phi/api/lib/tensor_api.cc | 43 ----- .../generator/templates}/tensor.h | 3 + paddle/phi/api/yaml/generator/tensor_gen.py | 147 ++++++++++++++++++ 5 files changed, 181 insertions(+), 54 deletions(-) delete mode 100644 paddle/phi/api/lib/tensor_api.cc rename paddle/phi/api/{include => yaml/generator/templates}/tensor.h (99%) diff --git a/.gitignore b/.gitignore index ffb44db5f7181a..fc4e9ac0df3e67 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ paddle/phi/api/include/operants_base.h paddle/phi/api/include/operants_manager.h paddle/phi/api/include/sparse_api.h paddle/phi/api/include/strings_api.h +paddle/phi/api/include/tensor.h paddle/phi/api/include/tensor_operants.h paddle/phi/api/lib/api.cc paddle/phi/api/lib/dygraph_api.* @@ -21,6 +22,7 @@ paddle/phi/api/lib/operants_manager.cc paddle/phi/api/lib/sparse_api.cc paddle/phi/api/lib/strings_api.cc paddle/phi/api/lib/sparse_bw_api.cc +paddle/phi/api/lib/tensor_api.cc paddle/phi/api/lib/tensor_operants.cc paddle/phi/extension.h paddle/phi/include/* diff --git a/paddle/phi/api/lib/CMakeLists.txt b/paddle/phi/api/lib/CMakeLists.txt index 9ed29fdff6380b..8b6ed01246823f 100644 --- a/paddle/phi/api/lib/CMakeLists.txt +++ b/paddle/phi/api/lib/CMakeLists.txt @@ -101,8 +101,12 @@ set(wrapped_infermeta_source_file set(tensor_gen_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/tensor_gen.py) set(api_prim_yaml_file ${CMAKE_SOURCE_DIR}/paddle/fluid/prim/api/api.yaml) +set(tensor_header_file_template + ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/templates/tensor.h) +set(tensor_header_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/include/tensor.h) set(operants_base_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/include/operants_base.h) +set(tensor_api_source_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/tensor_api.cc) set(phi_tensor_operants_header_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/include/tensor_operants.h) set(phi_tensor_operants_source_file @@ -111,7 +115,9 @@ set(operants_manager_header_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/include/operants_manager.h) set(operants_manager_source_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/operants_manager.cc) +set(tensor_header_file_tmp ${tensor_header_file}.tmp) set(operants_base_file_tmp ${operants_base_file}.tmp) +set(tensor_api_source_file_tmp ${tensor_api_source_file}.tmp) set(phi_tensor_operants_header_file_tmp ${phi_tensor_operants_header_file}.tmp) set(phi_tensor_operants_source_file_tmp ${phi_tensor_operants_source_file}.tmp) set(operants_manager_header_file_tmp ${operants_manager_header_file}.tmp) @@ -237,20 +243,31 @@ add_custom_command( # generate tensor and tensor operants file add_custom_command( - OUTPUT ${operants_base_file} ${phi_tensor_operants_header_file} - ${phi_tensor_operants_source_file} ${operants_manager_header_file} + OUTPUT ${operants_base_file} + ${tensor_header_file} + ${tensor_api_source_file} + ${phi_tensor_operants_header_file} + ${phi_tensor_operants_source_file} + ${operants_manager_header_file} ${operants_manager_source_file} COMMAND ${PYTHON_EXECUTABLE} -m pip install pyyaml COMMAND ${PYTHON_EXECUTABLE} ${tensor_gen_file} --api_yaml_path ${api_yaml_file} - ${legacy_api_yaml_file} --operants_base_path ${operants_base_file_tmp} - --phi_tensor_operants_header_path ${phi_tensor_operants_header_file_tmp} - --phi_tensor_operants_source_path ${phi_tensor_operants_source_file_tmp} - --operants_manager_header_path ${operants_manager_header_file_tmp} - --operants_manager_source_path ${operants_manager_source_file_tmp} - --api_prim_yaml_path ${api_prim_yaml_file} + ${legacy_api_yaml_file} --tensor_header_path ${tensor_header_file_tmp} + --tensor_header_template_path ${tensor_header_file_template} + --operants_base_path ${operants_base_file_tmp} --tensor_api_source_path + ${tensor_api_source_file_tmp} --phi_tensor_operants_header_path + ${phi_tensor_operants_header_file_tmp} --phi_tensor_operants_source_path + ${phi_tensor_operants_source_file_tmp} --operants_manager_header_path + ${operants_manager_header_file_tmp} --operants_manager_source_path + ${operants_manager_source_file_tmp} --api_prim_yaml_path + ${api_prim_yaml_file} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tensor_header_file_tmp} + ${tensor_header_file} COMMAND ${CMAKE_COMMAND} -E copy_if_different ${operants_base_file_tmp} ${operants_base_file} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tensor_api_source_file_tmp} + ${tensor_api_source_file} COMMAND ${CMAKE_COMMAND} -E copy_if_different ${phi_tensor_operants_header_file_tmp} ${phi_tensor_operants_header_file} @@ -262,9 +279,10 @@ add_custom_command( COMMAND ${CMAKE_COMMAND} -E copy_if_different ${operants_manager_source_file_tmp} ${operants_manager_source_file} COMMENT - "copy_if_different ${phi_tensor_operants_header_file} ${phi_tensor_operants_source_file}" + "copy_if_different ${tensor_header_file} ${operants_base_file} ${tensor_api_source_file} ${phi_tensor_operants_header_file} + ${phi_tensor_operants_source_file} ${operants_manager_header_file} ${operants_manager_source_file}" DEPENDS ${api_yaml_file} ${legacy_api_yaml_file} ${tensor_gen_file} - ${api_gen_base} ${api_gen_file} + ${api_gen_base} ${api_gen_file} ${tensor_header_file_template} VERBATIM) cc_library( @@ -390,5 +408,5 @@ cc_library( DEPS phi_enforce) cc_library( tensor_api - SRCS tensor_api.cc + SRCS ${tensor_api_source_file} DEPS operants_manager) diff --git a/paddle/phi/api/lib/tensor_api.cc b/paddle/phi/api/lib/tensor_api.cc deleted file mode 100644 index c063730155e8a4..00000000000000 --- a/paddle/phi/api/lib/tensor_api.cc +++ /dev/null @@ -1,43 +0,0 @@ -/* 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 diff --git a/paddle/phi/api/include/tensor.h b/paddle/phi/api/yaml/generator/templates/tensor.h similarity index 99% rename from paddle/phi/api/include/tensor.h rename to paddle/phi/api/yaml/generator/templates/tensor.h index 4924ff3f1a2a39..65385496652dd0 100644 --- a/paddle/phi/api/include/tensor.h +++ b/paddle/phi/api/yaml/generator/templates/tensor.h @@ -645,6 +645,9 @@ class PADDLE_API Tensor final { * in the development of new dygraph. It may be removed in the future. */ std::string name_{""}; + + // Example: Tensor add(const Tensor& other) const; + $ { tensor_api_declaration } }; } // namespace experimental diff --git a/paddle/phi/api/yaml/generator/tensor_gen.py b/paddle/phi/api/yaml/generator/tensor_gen.py index f86c9361f999ad..4de583cfbd5716 100644 --- a/paddle/phi/api/yaml/generator/tensor_gen.py +++ b/paddle/phi/api/yaml/generator/tensor_gen.py @@ -13,6 +13,7 @@ # limitations under the License. import argparse +import string import yaml from api_gen import ForwardAPI @@ -58,6 +59,43 @@ class TensorOperantsBase { """ +tensor_api_source_include = """// Generated by paddle/phi/api/yaml/generator/tensor_gen.py + +#include "paddle/phi/api/include/tensor.h" + +#include "paddle/phi/api/include/operants_manager.h" + +""" + +tensor_api_source_start = """ +namespace paddle { + +namespace experimental { + +Tensor Tensor::operator+(const Tensor &other) const { + return add(other); +} + +Tensor Tensor::operator-(const Tensor &other) const { + return subtract(other); +} + +Tensor Tensor::operator*(const Tensor &other) const { + return multiply(other); +} + +Tensor Tensor::operator/(const Tensor &other) const { + return divide(other); +} +""" + + +tensor_api_source_end = """ +} // namespace experimental +} // namespace paddle + +""" + operants_header_include = """// Generated by paddle/phi/api/yaml/generator/tensor_gen.py @@ -222,6 +260,32 @@ def __init__(self, api_item_yaml, prims=tuple()): if self.get_api_func_name() in prims: self.is_prim_api = True + def get_declare_args_without_first_tensor(self, inplace_flag=False): + # NOTE(HongyuJia): consider vector becomes first input argument. + declare_args = self.get_input_tensor_args(inplace_flag) + assert ( + len(declare_args) > 1 + ), "Can't use tensor api without Tensor inputs" + + for name in self.attrs['names']: + default_value = '' + if self.attrs['attr_info'][name][1] is not None: + default_value = ' = ' + self.attrs['attr_info'][name][1] + declare_args.append( + self.attrs['attr_info'][name][0] + ' ' + name + default_value + ) + + return ", ".join(declare_args[1:]) + + def gene_tensor_declaration(self): + api_func_name = self.get_api_func_name() + if api_func_name[-1] != '_': + return f"""{indent}{self.get_return_type()} {api_func_name}({self.get_declare_args_without_first_tensor()}) const; +""" + else: + return f"""{indent}{self.get_return_type(inplace_flag=True)} {api_func_name}({self.get_declare_args_without_first_tensor(inplace_flag=True)}) const; +""" + def gene_operants_base(self): api_func_name = self.get_api_func_name() if api_func_name[-1] != '_': @@ -231,6 +295,40 @@ def gene_operants_base(self): else: return f""" {indent}virtual {self.get_return_type(inplace_flag=True)} {api_func_name}({self.get_declare_args(inplace_flag=True)}) = 0; +""" + + def get_define_args_without_first_tensor(self, inplace_flag=False): + # NOTE(HongyuJia): consider vector becomes first input argument. + define_args = self.get_input_tensor_args(inplace_flag) + assert ( + len(define_args) > 1 + ), "Can't use tensor api without Tensor inputs" + for name in self.attrs['names']: + define_args.append(self.attrs['attr_info'][name][0] + ' ' + name) + # remove first Tensor argument + return ", ".join(define_args[1:]) + + def gene_tensor_api_implementation(self): + func_name = self.get_api_func_name() + assert ( + len(self.inputs['names']) > 1 + ), "Can't use tensor api without Tensor inputs" + # remove first Tensor argument + func_args = self.inputs['names'][1:] + self.attrs['names'] + func_args_code = ", ".join(func_args) + # func decalaration + if func_name[-1] != '_': + return f""" +{self.get_return_type()} Tensor::{func_name}({self.get_define_args_without_first_tensor()}) const {{ +{indent}return paddle::OperantsManager::Instance().{func_name}(static_cast(*this), {func_args_code}); +}} +""" + else: + return f""" +{self.get_return_type(inplace_flag=True)} Tensor::{func_name}({self.get_define_args_without_first_tensor(inplace_flag=True)}) const {{ +{indent}return paddle::OperantsManager::Instance().{func_name}(static_cast(*this), {func_args_code}); +}} + """ def gene_operants_declaration(self): @@ -317,7 +415,10 @@ def gene_operants_manager_implementation(self): def generate_tensor_operants_api( api_yaml_path, + tensor_header_path, + tensor_header_template_path, operants_base_path, + tensor_api_source_path, operants_header_path, operants_source_path, operants_manager_header_path, @@ -332,7 +433,9 @@ def generate_tensor_operants_api( if api_list: apis.extend(api_list) + tensor_header_file = open(tensor_header_path, 'w') operants_base_file = open(operants_base_path, 'w') + tensor_api_source_file = open(tensor_api_source_path, 'w') operants_header_file = open(operants_header_path, 'w') operants_source_file = open(operants_source_path, 'w') operants_manager_header_file = open(operants_manager_header_path, 'w') @@ -340,6 +443,8 @@ def generate_tensor_operants_api( operants_base_file.write(operants_base_include) operants_base_file.write(operants_base_start) + tensor_api_source_file.write(tensor_api_source_include) + tensor_api_source_file.write(tensor_api_source_start) operants_header_file.write(operants_header_include) operants_header_file.write(operants_header_start) operants_source_file.write(operants_source_include) @@ -354,10 +459,17 @@ def generate_tensor_operants_api( # white list temporarily api_prims = ('add', 'subtract', 'multiply', 'divide') + tensor_api_declaration_code = "" for api in apis: operants_api = OperantsAPI(api, api_prims) if operants_api.is_prim_api: + tensor_api_declaration_code += ( + operants_api.gene_tensor_declaration() + ) operants_base_file.write(operants_api.gene_operants_base()) + tensor_api_source_file.write( + operants_api.gene_tensor_api_implementation() + ) operants_header_file.write(operants_api.gene_operants_declaration()) operants_source_file.write( operants_api.gene_operants_implementation() @@ -369,13 +481,24 @@ def generate_tensor_operants_api( operants_api.gene_operants_manager_implementation() ) + tensor_header_template = open(tensor_header_template_path) + tensor_header_template = string.Template(tensor_header_template.read()) + tensor_header_file.write( + tensor_header_template.substitute( + tensor_api_declaration=tensor_api_declaration_code + ) + ) + operants_base_file.write(operants_base_end) + tensor_api_source_file.write(tensor_api_source_end) operants_header_file.write(operants_header_end) operants_source_file.write(operants_source_end) operants_manager_header_file.write(operants_manager_header_end) operants_manager_source_file.write(operants_manager_source_end) + tensor_header_file.close() operants_base_file.close() + tensor_api_source_file.close() operants_header_file.close() operants_source_file.close() operants_manager_header_file.close() @@ -393,12 +516,30 @@ def main(): default=['paddle/phi/api/yaml/ops.yaml'], ) + parser.add_argument( + '--tensor_header_path', + help='output of generated tensor header code file', + default='paddle/phi/api/include/tensor.h', + ) + + parser.add_argument( + '--tensor_header_template_path', + help='the template file of tensor header code file', + default='paddle/phi/api/yaml/generator/templates/tensor.h', + ) + parser.add_argument( '--operants_base_path', help='output of generated operants_base header code file', default='paddle/phi/api/include/operants_base.h', ) + parser.add_argument( + '--tensor_api_source_path', + help='output of generated tensor_api source code file', + default='paddle/phi/api/lib/tensor_api.cc', + ) + parser.add_argument( '--phi_tensor_operants_header_path', help='output of generated phi_tensor_operants header code file', @@ -432,7 +573,10 @@ def main(): options = parser.parse_args() api_yaml_path = options.api_yaml_path + tensor_header_path = options.tensor_header_path + tensor_header_template_path = options.tensor_header_template_path operants_base_path = options.operants_base_path + tensor_api_source_path = options.tensor_api_source_path operants_header_path = options.phi_tensor_operants_header_path operants_source_path = options.phi_tensor_operants_source_path operants_manager_header_path = options.operants_manager_header_path @@ -441,7 +585,10 @@ def main(): generate_tensor_operants_api( api_yaml_path, + tensor_header_path, + tensor_header_template_path, operants_base_path, + tensor_api_source_path, operants_header_path, operants_source_path, operants_manager_header_path, From 38ecef92f43bfe89637221af8112bed3c66b70e8 Mon Sep 17 00:00:00 2001 From: jiahongyu Date: Sun, 19 Feb 2023 08:16:34 +0000 Subject: [PATCH 4/7] polish CMakeLists logic --- paddle/phi/api/lib/CMakeLists.txt | 64 ++++++++++--------- .../phi/api/yaml/generator/templates/tensor.h | 4 +- 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/paddle/phi/api/lib/CMakeLists.txt b/paddle/phi/api/lib/CMakeLists.txt index 8b6ed01246823f..0e37b05a9977fd 100644 --- a/paddle/phi/api/lib/CMakeLists.txt +++ b/paddle/phi/api/lib/CMakeLists.txt @@ -242,15 +242,10 @@ add_custom_command( VERBATIM) # generate tensor and tensor operants file -add_custom_command( - OUTPUT ${operants_base_file} - ${tensor_header_file} - ${tensor_api_source_file} - ${phi_tensor_operants_header_file} - ${phi_tensor_operants_source_file} - ${operants_manager_header_file} - ${operants_manager_source_file} - COMMAND ${PYTHON_EXECUTABLE} -m pip install pyyaml +message("create or copy auto-geneated tensor files") +execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install pyyaml) +execute_process( + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator COMMAND ${PYTHON_EXECUTABLE} ${tensor_gen_file} --api_yaml_path ${api_yaml_file} ${legacy_api_yaml_file} --tensor_header_path ${tensor_header_file_tmp} @@ -262,28 +257,35 @@ add_custom_command( ${operants_manager_header_file_tmp} --operants_manager_source_path ${operants_manager_source_file_tmp} --api_prim_yaml_path ${api_prim_yaml_file} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tensor_header_file_tmp} - ${tensor_header_file} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${operants_base_file_tmp} - ${operants_base_file} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tensor_api_source_file_tmp} - ${tensor_api_source_file} - COMMAND - ${CMAKE_COMMAND} -E copy_if_different ${phi_tensor_operants_header_file_tmp} - ${phi_tensor_operants_header_file} - COMMAND - ${CMAKE_COMMAND} -E copy_if_different ${phi_tensor_operants_source_file_tmp} - ${phi_tensor_operants_source_file} - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${operants_manager_header_file_tmp} ${operants_manager_header_file} - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${operants_manager_source_file_tmp} ${operants_manager_source_file} - COMMENT - "copy_if_different ${tensor_header_file} ${operants_base_file} ${tensor_api_source_file} ${phi_tensor_operants_header_file} - ${phi_tensor_operants_source_file} ${operants_manager_header_file} ${operants_manager_source_file}" - DEPENDS ${api_yaml_file} ${legacy_api_yaml_file} ${tensor_gen_file} - ${api_gen_base} ${api_gen_file} ${tensor_header_file_template} - VERBATIM) + RESULT_VARIABLE _result) +if(${_result}) + message(FATAL_ERROR "tensor codegen failed, exiting.") +endif() + +set(generated_tensor_files + "${tensor_header_file}" + "${operants_base_file}" + "${tensor_api_source_file}" + "${phi_tensor_operants_header_file}" + "${phi_tensor_operants_source_file}" + "${operants_manager_header_file}" + "${operants_manager_source_file}") + +foreach(generated_tensor_file ${generated_tensor_files}) + if(EXISTS "${generated_tensor_file}.tmp" AND EXISTS + "${generated_tensor_file}") + execute_process( + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${generated_tensor_file}.tmp" "${generated_tensor_file}") + message( + "copy if different ${generated_tensor_file}.tmp ${generated_tensor_file}") + elseif(EXISTS "${generated_tensor_file}.tmp") + execute_process( + COMMAND ${CMAKE_COMMAND} -E copy "${generated_tensor_file}.tmp" + "${generated_tensor_file}") + message("copy ${generated_tensor_file}.tmp ${generated_tensor_file}") + endif() +endforeach() cc_library( op_meta_info diff --git a/paddle/phi/api/yaml/generator/templates/tensor.h b/paddle/phi/api/yaml/generator/templates/tensor.h index 65385496652dd0..7b4fa8b585f44a 100644 --- a/paddle/phi/api/yaml/generator/templates/tensor.h +++ b/paddle/phi/api/yaml/generator/templates/tensor.h @@ -646,8 +646,10 @@ class PADDLE_API Tensor final { */ std::string name_{""}; + // clang-format off // Example: Tensor add(const Tensor& other) const; - $ { tensor_api_declaration } +${tensor_api_declaration} + // clang-format on }; } // namespace experimental From b6ba921b33c480a06714f7ee7588f726e6d48496 Mon Sep 17 00:00:00 2001 From: jiahongyu Date: Mon, 20 Feb 2023 06:24:05 +0000 Subject: [PATCH 5/7] cancel tensor.h auto-gen --- .gitignore | 1 - .../generator/templates => include}/tensor.h | 7 +++ paddle/phi/api/lib/CMakeLists.txt | 35 +++++------- paddle/phi/api/yaml/generator/tensor_gen.py | 55 ------------------- 4 files changed, 22 insertions(+), 76 deletions(-) rename paddle/phi/api/{yaml/generator/templates => include}/tensor.h (98%) diff --git a/.gitignore b/.gitignore index fc4e9ac0df3e67..eef92a0488cd22 100644 --- a/.gitignore +++ b/.gitignore @@ -13,7 +13,6 @@ paddle/phi/api/include/operants_base.h paddle/phi/api/include/operants_manager.h paddle/phi/api/include/sparse_api.h paddle/phi/api/include/strings_api.h -paddle/phi/api/include/tensor.h paddle/phi/api/include/tensor_operants.h paddle/phi/api/lib/api.cc paddle/phi/api/lib/dygraph_api.* diff --git a/paddle/phi/api/yaml/generator/templates/tensor.h b/paddle/phi/api/include/tensor.h similarity index 98% rename from paddle/phi/api/yaml/generator/templates/tensor.h rename to paddle/phi/api/include/tensor.h index 4924ff3f1a2a39..0cef41d5259a07 100644 --- a/paddle/phi/api/yaml/generator/templates/tensor.h +++ b/paddle/phi/api/include/tensor.h @@ -645,6 +645,13 @@ class PADDLE_API Tensor final { * in the development of new dygraph. It may be removed in the future. */ std::string name_{""}; + + // Tensor C++ APIs + // Example: Tensor add(const Tensor& other) const; + Tensor add(const Tensor& y) const; + Tensor divide(const Tensor& y) const; + Tensor multiply(const Tensor& y) const; + Tensor subtract(const Tensor& y) const; }; } // namespace experimental diff --git a/paddle/phi/api/lib/CMakeLists.txt b/paddle/phi/api/lib/CMakeLists.txt index ecbf4841d0a86e..57cb4952f0afee 100644 --- a/paddle/phi/api/lib/CMakeLists.txt +++ b/paddle/phi/api/lib/CMakeLists.txt @@ -101,9 +101,6 @@ set(wrapped_infermeta_source_file set(tensor_gen_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/tensor_gen.py) set(api_prim_yaml_file ${CMAKE_SOURCE_DIR}/paddle/fluid/prim/api/api.yaml) -set(tensor_header_file_template - ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/templates/tensor.h) -set(tensor_header_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/include/tensor.h) set(operants_base_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/include/operants_base.h) set(tensor_api_source_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/tensor_api.cc) @@ -115,7 +112,6 @@ set(operants_manager_header_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/include/operants_manager.h) set(operants_manager_source_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/operants_manager.cc) -set(tensor_header_file_tmp ${tensor_header_file}.tmp) set(operants_base_file_tmp ${operants_base_file}.tmp) set(tensor_api_source_file_tmp ${tensor_api_source_file}.tmp) set(phi_tensor_operants_header_file_tmp ${phi_tensor_operants_header_file}.tmp) @@ -248,28 +244,22 @@ execute_process( WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator COMMAND ${PYTHON_EXECUTABLE} ${tensor_gen_file} --api_yaml_path ${api_yaml_file} - ${legacy_api_yaml_file} --tensor_header_path ${tensor_header_file_tmp} - --tensor_header_template_path ${tensor_header_file_template} - --operants_base_path ${operants_base_file_tmp} --tensor_api_source_path - ${tensor_api_source_file_tmp} --phi_tensor_operants_header_path - ${phi_tensor_operants_header_file_tmp} --phi_tensor_operants_source_path - ${phi_tensor_operants_source_file_tmp} --operants_manager_header_path - ${operants_manager_header_file_tmp} --operants_manager_source_path - ${operants_manager_source_file_tmp} --api_prim_yaml_path - ${api_prim_yaml_file} + ${legacy_api_yaml_file} --operants_base_path ${operants_base_file_tmp} + --tensor_api_source_path ${tensor_api_source_file_tmp} + --phi_tensor_operants_header_path ${phi_tensor_operants_header_file_tmp} + --phi_tensor_operants_source_path ${phi_tensor_operants_source_file_tmp} + --operants_manager_header_path ${operants_manager_header_file_tmp} + --operants_manager_source_path ${operants_manager_source_file_tmp} + --api_prim_yaml_path ${api_prim_yaml_file} RESULT_VARIABLE _result) if(${_result}) message(FATAL_ERROR "tensor codegen failed, exiting.") endif() set(generated_tensor_files - "${tensor_header_file}" - "${operants_base_file}" - "${tensor_api_source_file}" - "${phi_tensor_operants_header_file}" - "${phi_tensor_operants_source_file}" - "${operants_manager_header_file}" - "${operants_manager_source_file}") + "${operants_base_file}" "${tensor_api_source_file}" + "${phi_tensor_operants_header_file}" "${phi_tensor_operants_source_file}" + "${operants_manager_header_file}" "${operants_manager_source_file}") foreach(generated_tensor_file ${generated_tensor_files}) if(EXISTS "${generated_tensor_file}.tmp" AND EXISTS @@ -408,3 +398,8 @@ cc_library( operants_manager SRCS ${operants_manager_source_file} DEPS phi_enforce) +cc_library( + tensor_api + SRCS ${tensor_api_source_file} + SRCS tensor_api.cc + DEPS operants_manager) diff --git a/paddle/phi/api/yaml/generator/tensor_gen.py b/paddle/phi/api/yaml/generator/tensor_gen.py index 4de583cfbd5716..4574fb41581e80 100644 --- a/paddle/phi/api/yaml/generator/tensor_gen.py +++ b/paddle/phi/api/yaml/generator/tensor_gen.py @@ -13,7 +13,6 @@ # limitations under the License. import argparse -import string import yaml from api_gen import ForwardAPI @@ -260,32 +259,6 @@ def __init__(self, api_item_yaml, prims=tuple()): if self.get_api_func_name() in prims: self.is_prim_api = True - def get_declare_args_without_first_tensor(self, inplace_flag=False): - # NOTE(HongyuJia): consider vector becomes first input argument. - declare_args = self.get_input_tensor_args(inplace_flag) - assert ( - len(declare_args) > 1 - ), "Can't use tensor api without Tensor inputs" - - for name in self.attrs['names']: - default_value = '' - if self.attrs['attr_info'][name][1] is not None: - default_value = ' = ' + self.attrs['attr_info'][name][1] - declare_args.append( - self.attrs['attr_info'][name][0] + ' ' + name + default_value - ) - - return ", ".join(declare_args[1:]) - - def gene_tensor_declaration(self): - api_func_name = self.get_api_func_name() - if api_func_name[-1] != '_': - return f"""{indent}{self.get_return_type()} {api_func_name}({self.get_declare_args_without_first_tensor()}) const; -""" - else: - return f"""{indent}{self.get_return_type(inplace_flag=True)} {api_func_name}({self.get_declare_args_without_first_tensor(inplace_flag=True)}) const; -""" - def gene_operants_base(self): api_func_name = self.get_api_func_name() if api_func_name[-1] != '_': @@ -415,8 +388,6 @@ def gene_operants_manager_implementation(self): def generate_tensor_operants_api( api_yaml_path, - tensor_header_path, - tensor_header_template_path, operants_base_path, tensor_api_source_path, operants_header_path, @@ -433,7 +404,6 @@ def generate_tensor_operants_api( if api_list: apis.extend(api_list) - tensor_header_file = open(tensor_header_path, 'w') operants_base_file = open(operants_base_path, 'w') tensor_api_source_file = open(tensor_api_source_path, 'w') operants_header_file = open(operants_header_path, 'w') @@ -459,13 +429,9 @@ def generate_tensor_operants_api( # white list temporarily api_prims = ('add', 'subtract', 'multiply', 'divide') - tensor_api_declaration_code = "" for api in apis: operants_api = OperantsAPI(api, api_prims) if operants_api.is_prim_api: - tensor_api_declaration_code += ( - operants_api.gene_tensor_declaration() - ) operants_base_file.write(operants_api.gene_operants_base()) tensor_api_source_file.write( operants_api.gene_tensor_api_implementation() @@ -481,14 +447,6 @@ def generate_tensor_operants_api( operants_api.gene_operants_manager_implementation() ) - tensor_header_template = open(tensor_header_template_path) - tensor_header_template = string.Template(tensor_header_template.read()) - tensor_header_file.write( - tensor_header_template.substitute( - tensor_api_declaration=tensor_api_declaration_code - ) - ) - operants_base_file.write(operants_base_end) tensor_api_source_file.write(tensor_api_source_end) operants_header_file.write(operants_header_end) @@ -496,7 +454,6 @@ def generate_tensor_operants_api( operants_manager_header_file.write(operants_manager_header_end) operants_manager_source_file.write(operants_manager_source_end) - tensor_header_file.close() operants_base_file.close() tensor_api_source_file.close() operants_header_file.close() @@ -516,18 +473,6 @@ def main(): default=['paddle/phi/api/yaml/ops.yaml'], ) - parser.add_argument( - '--tensor_header_path', - help='output of generated tensor header code file', - default='paddle/phi/api/include/tensor.h', - ) - - parser.add_argument( - '--tensor_header_template_path', - help='the template file of tensor header code file', - default='paddle/phi/api/yaml/generator/templates/tensor.h', - ) - parser.add_argument( '--operants_base_path', help='output of generated operants_base header code file', From fbb7f13b3db42e1ee1dee473a65c3e005e37171f Mon Sep 17 00:00:00 2001 From: jiahongyu Date: Mon, 20 Feb 2023 06:31:25 +0000 Subject: [PATCH 6/7] clean useless parameter --- paddle/phi/api/lib/CMakeLists.txt | 1 - paddle/phi/api/yaml/generator/tensor_gen.py | 4 ---- 2 files changed, 5 deletions(-) diff --git a/paddle/phi/api/lib/CMakeLists.txt b/paddle/phi/api/lib/CMakeLists.txt index 57cb4952f0afee..85a145f71c04c7 100644 --- a/paddle/phi/api/lib/CMakeLists.txt +++ b/paddle/phi/api/lib/CMakeLists.txt @@ -401,5 +401,4 @@ cc_library( cc_library( tensor_api SRCS ${tensor_api_source_file} - SRCS tensor_api.cc DEPS operants_manager) diff --git a/paddle/phi/api/yaml/generator/tensor_gen.py b/paddle/phi/api/yaml/generator/tensor_gen.py index 4574fb41581e80..0a36a34a1c35c5 100644 --- a/paddle/phi/api/yaml/generator/tensor_gen.py +++ b/paddle/phi/api/yaml/generator/tensor_gen.py @@ -518,8 +518,6 @@ def main(): options = parser.parse_args() api_yaml_path = options.api_yaml_path - tensor_header_path = options.tensor_header_path - tensor_header_template_path = options.tensor_header_template_path operants_base_path = options.operants_base_path tensor_api_source_path = options.tensor_api_source_path operants_header_path = options.phi_tensor_operants_header_path @@ -530,8 +528,6 @@ def main(): generate_tensor_operants_api( api_yaml_path, - tensor_header_path, - tensor_header_template_path, operants_base_path, tensor_api_source_path, operants_header_path, From 94fdb4a403c4500c2545abc66c70e56de5798f42 Mon Sep 17 00:00:00 2001 From: jiahongyu Date: Mon, 20 Feb 2023 06:36:05 +0000 Subject: [PATCH 7/7] delete tensor_api.cc --- paddle/phi/api/lib/tensor_api.cc | 43 -------------------------------- 1 file changed, 43 deletions(-) delete mode 100644 paddle/phi/api/lib/tensor_api.cc diff --git a/paddle/phi/api/lib/tensor_api.cc b/paddle/phi/api/lib/tensor_api.cc deleted file mode 100644 index c063730155e8a4..00000000000000 --- a/paddle/phi/api/lib/tensor_api.cc +++ /dev/null @@ -1,43 +0,0 @@ -/* 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