From e73857a329ee1f951c739662616922dd06ae4f82 Mon Sep 17 00:00:00 2001 From: Jack Zhou Date: Mon, 28 Mar 2022 13:31:38 +0800 Subject: [PATCH] Add string api python c code gen (#40992) --- .../final_state_generator/CMakeLists.txt | 4 +++- .../final_state_generator/codegen_utils.py | 2 ++ .../final_state_generator/python_c_gen.py | 13 +++++++++---- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/eager/auto_code_generator/final_state_generator/CMakeLists.txt b/paddle/fluid/eager/auto_code_generator/final_state_generator/CMakeLists.txt index 771351dd4a..8e89ea3f19 100644 --- a/paddle/fluid/eager/auto_code_generator/final_state_generator/CMakeLists.txt +++ b/paddle/fluid/eager/auto_code_generator/final_state_generator/CMakeLists.txt @@ -8,6 +8,8 @@ set(forwards_cc_path "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/eage set(forwards_h_path "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/eager_generated/forwards/dygraph_functions.h") set(nodes_cc_path "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/eager_generated/backwards/nodes.cc") set(nodes_h_path "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/eager_generated/backwards/nodes.h") +# StringTensor only needs forward api +set(fwd_api_yaml_path "${PADDLE_SOURCE_DIR}/python/paddle/utils/code_gen/strings_api.yaml") message("Final State Eager CodeGen") add_custom_target(eager_final_state_codegen @@ -30,7 +32,7 @@ set(python_c_output_path "${PADDLE_SOURCE_DIR}/paddle/fluid/pybind/eager_final_s add_custom_target(eager_final_state_python_c_codegen COMMAND "${PYTHON_EXECUTABLE}" "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/auto_code_generator/final_state_generator/python_c_gen.py" - "--api_yaml_path=${api_yaml_path}" + "--api_yaml_path=${api_yaml_path},${fwd_api_yaml_path}" "--output_path=${tmp_python_c_output_path}" COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tmp_python_c_output_path} ${python_c_output_path} VERBATIM diff --git a/paddle/fluid/eager/auto_code_generator/final_state_generator/codegen_utils.py b/paddle/fluid/eager/auto_code_generator/final_state_generator/codegen_utils.py index 4699fe7355..89939a68f2 100644 --- a/paddle/fluid/eager/auto_code_generator/final_state_generator/codegen_utils.py +++ b/paddle/fluid/eager/auto_code_generator/final_state_generator/codegen_utils.py @@ -421,3 +421,5 @@ class YamlGeneratorBase: api_yaml_path = self.api_yaml_path if "sparse" in api_yaml_path: self.namespace = "sparse::" + elif "strings" in api_yaml_path: + self.namespace = "strings::" diff --git a/paddle/fluid/eager/auto_code_generator/final_state_generator/python_c_gen.py b/paddle/fluid/eager/auto_code_generator/final_state_generator/python_c_gen.py index 4b557f7f5c..797fec9752 100644 --- a/paddle/fluid/eager/auto_code_generator/final_state_generator/python_c_gen.py +++ b/paddle/fluid/eager/auto_code_generator/final_state_generator/python_c_gen.py @@ -125,7 +125,7 @@ FUNCTION_NAME_TEMPLATE = \ PYTHON_C_FUNCTION_REG_TEMPLATE = \ """ -{{\"final_state_{}\", (PyCFunction)(void(*)(void)) {}eager_final_state_api_{}, METH_VARARGS | METH_KEYWORDS, \"C++ interface function for {} in dygraph.\"}} +{{\"final_state_{}{}\", (PyCFunction)(void(*)(void)) {}eager_final_state_api_{}, METH_VARARGS | METH_KEYWORDS, \"C++ interface function for {} in dygraph.\"}} """ @@ -142,6 +142,7 @@ PYTHON_C_WRAPPER_TEMPLATE = \ #include "paddle/phi/common/scalar.h" #include "paddle/phi/common/scalar_array.h" #include "paddle/phi/api/include/sparse_api.h" +#include "paddle/phi/api/include/strings_api.h" #include "paddle/fluid/pybind/op_function_common.h" #include "paddle/fluid/eager/api/generated/eager_generated/forwards/dygraph_functions.h" #include "paddle/fluid/pybind/exception.h" @@ -328,9 +329,13 @@ class PythonCSingleFunctionGenerator(FunctionGeneratorBase): get_eager_tensor_str, parse_attributes_str, fwd_function_name, dygraph_function_call_str, return_str) + # Set prefix of forward_api_name to avoid conflicts + prefix = self.namespace.strip("::") + forward_api_name_prefix = "" if prefix == "" else prefix + "_" # Generate Python-C Function Registration self.python_c_function_reg_str = PYTHON_C_FUNCTION_REG_TEMPLATE.format( - forward_api_name, namespace, forward_api_name, forward_api_name) + forward_api_name_prefix, forward_api_name, namespace, + forward_api_name, forward_api_name) if len(inplace_map) > 0: assert len( @@ -361,8 +366,8 @@ class PythonCSingleFunctionGenerator(FunctionGeneratorBase): # Generate Python-C Function Registration self.python_c_function_reg_str += "\n," + PYTHON_C_FUNCTION_REG_TEMPLATE.format( - inplaced_forward_api_name, namespace, inplaced_forward_api_name, - inplaced_forward_api_name) + forward_api_name_prefix, inplaced_forward_api_name, namespace, + inplaced_forward_api_name, inplaced_forward_api_name) def run(self): # Initialized is_forward_only -- GitLab