From ad78a21e3b809d31a6f34a66233598bb5a41ded9 Mon Sep 17 00:00:00 2001 From: Zhanlue Yang Date: Wed, 29 Dec 2021 19:26:02 +0800 Subject: [PATCH] Added copy_if_different for eager code generator (#38562) --- .../eager/auto_code_generator/CMakeLists.txt | 25 +++++++++++++++ .../auto_code_generator/eager_generator.cc | 32 ++++++++----------- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/paddle/fluid/eager/auto_code_generator/CMakeLists.txt b/paddle/fluid/eager/auto_code_generator/CMakeLists.txt index d8f1e32ff01..010c879571c 100644 --- a/paddle/fluid/eager/auto_code_generator/CMakeLists.txt +++ b/paddle/fluid/eager/auto_code_generator/CMakeLists.txt @@ -16,6 +16,15 @@ execute_process( COMMAND "${PYTHON_EXECUTABLE}" "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/auto_code_generator/generate_file_structures.py" "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/" ) +set(tmp_dygraph_forward_h_path "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/dygraph_forward_api.tmp.h") +set(tmp_dygraph_forward_cc_path "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/forwards/dygraph_forward_functions.tmp.cc") +set(tmp_dygraph_node_h_path "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/nodes/nodes.tmp.h") +set(tmp_dygraph_node_cc_path "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/nodes/nodes.tmp.cc") +set(dygraph_forward_h_path "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/dygraph_forward_api.h") +set(dygraph_forward_cc_path "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/forwards/dygraph_forward_functions.cc") +set(dygraph_node_h_path "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/nodes/nodes.h") +set(dygraph_node_cc_path "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/nodes/nodes.cc") + if(WIN32) set(EAGER_CODEGEN_DEPS eager_generator) if("${CMAKE_GENERATOR}" STREQUAL "Ninja") @@ -48,6 +57,14 @@ if(WIN32) add_custom_target(eager_codegen COMMAND "${eager_generator_path}/eager_generator.exe" "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated" + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tmp_dygraph_forward_h_path} ${dygraph_forward_h_path} + COMMENT "copy_if_different ${tmp_dygraph_forward_h_path} to ${dygraph_forward_h_path}" + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tmp_dygraph_forward_cc_path} ${dygraph_forward_cc_path} + COMMENT "copy_if_different ${tmp_dygraph_forward_cc_path} to ${dygraph_forward_cc_path}" + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tmp_dygraph_node_h_path} ${dygraph_node_h_path} + COMMENT "copy_if_different ${tmp_dygraph_node_h_path} to ${dygraph_node_h_path}" + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tmp_dygraph_node_cc_path} ${dygraph_node_cc_path} + COMMENT "copy_if_different ${tmp_dygraph_node_cc_path} to ${dygraph_node_cc_path}" DEPENDS ${EAGER_CODEGEN_DEPS} VERBATIM) else() @@ -55,6 +72,14 @@ else() COMMAND ${CMAKE_COMMAND} -E env "LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH}:${CMAKE_CURRENT_BINARY_DIR}/../../pybind" "${CMAKE_CURRENT_BINARY_DIR}/eager_generator" "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated" + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tmp_dygraph_forward_h_path} ${dygraph_forward_h_path} + COMMENT "copy_if_different ${tmp_dygraph_forward_h_path} to ${dygraph_forward_h_path}" + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tmp_dygraph_forward_cc_path} ${dygraph_forward_cc_path} + COMMENT "copy_if_different ${tmp_dygraph_forward_cc_path} to ${dygraph_forward_cc_path}" + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tmp_dygraph_node_h_path} ${dygraph_node_h_path} + COMMENT "copy_if_different ${tmp_dygraph_node_h_path} to ${dygraph_node_h_path}" + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tmp_dygraph_node_cc_path} ${dygraph_node_cc_path} + COMMENT "copy_if_different ${tmp_dygraph_node_cc_path} to ${dygraph_node_cc_path}" DEPENDS eager_generator VERBATIM) endif() diff --git a/paddle/fluid/eager/auto_code_generator/eager_generator.cc b/paddle/fluid/eager/auto_code_generator/eager_generator.cc index 12c14ebf5e5..43c40beeebb 100644 --- a/paddle/fluid/eager/auto_code_generator/eager_generator.cc +++ b/paddle/fluid/eager/auto_code_generator/eager_generator.cc @@ -1847,19 +1847,15 @@ static std::string GenerateDygraphHFileIncludes() { return dygraph_forward_api_includes_str; } -static void GenerateForwardHFile(const std::string& output_dir, +static void GenerateForwardHFile(const std::string& dygraph_forward_api_path, const std::string& dygraph_forward_api_str) { - std::string dygraph_forward_api_path = output_dir + "/dygraph_forward_api.h"; std::ofstream forward_header_stream(dygraph_forward_api_path, std::ios::out); forward_header_stream << dygraph_forward_api_str; forward_header_stream.close(); } -static void GenerateForwardDygraphFile(const std::string& output_dir, +static void GenerateForwardDygraphFile(const std::string& forward_cc_path, const std::string& fwd_function_str) { - std::string forwards_dir = output_dir + "/forwards/"; - std::string forward_cc_filename = "dygraph_forward_functions.cc"; - std::string forward_cc_path = forwards_dir + forward_cc_filename; const char* FORWARD_INCLUDE_TEMPLATE = "#include " "\"paddle/fluid/eager/api/generated/fluid_generated/" @@ -1876,11 +1872,8 @@ static void GenerateForwardDygraphFile(const std::string& output_dir, forward_cc_stream.close(); } -static void GenerateNodeHFile(const std::string& output_dir, +static void GenerateNodeHFile(const std::string& node_h_path, const std::string& grad_node_str) { - std::string nodes_dir = output_dir + "/nodes/"; - std::string node_h_filename = "nodes.h"; - std::string node_h_path = nodes_dir + node_h_filename; std::string node_h_include_str = "#pragma once\n" "#include \"paddle/fluid/eager/tensor_wrapper.h\"\n" @@ -1892,11 +1885,8 @@ static void GenerateNodeHFile(const std::string& output_dir, node_h_stream.close(); } -static void GenerateNodeCCFile(const std::string& output_dir, +static void GenerateNodeCCFile(const std::string& node_cc_path, const std::string& grad_function_str) { - std::string nodes_dir = output_dir + "/nodes/"; - std::string node_cc_filename = "nodes.cc"; - std::string node_cc_path = nodes_dir + node_cc_filename; const char* NODE_CC_INCLUDE_TEMPLATE = "#include \"glog/logging.h\"\n" "#include \"paddle/pten/api/all.h\"\n" @@ -2026,18 +2016,24 @@ static void DygraphCodeGeneration(const std::string& output_dir) { } VLOG(6) << "-------- GenerateDygraphForwardCCFile -------"; + std::string forward_cc_path = + output_dir + "/forwards/dygraph_forward_functions.tmp.cc"; fwd_function_str += "\n"; fwd_function_str += GenerateCoreOpsReturnsInfo(); - GenerateForwardDygraphFile(output_dir, fwd_function_str); + GenerateForwardDygraphFile(forward_cc_path, fwd_function_str); VLOG(6) << "-------- GenerateForwardHFile -------"; - GenerateForwardHFile(output_dir, dygraph_forward_api_str); + std::string dygraph_forward_api_path = + output_dir + "/dygraph_forward_api.tmp.h"; + GenerateForwardHFile(dygraph_forward_api_path, dygraph_forward_api_str); VLOG(6) << "-------- GenerateNodeHFile -------"; - GenerateNodeHFile(output_dir, grad_node_h_str); + std::string node_h_path = output_dir + "/nodes/nodes.tmp.h"; + GenerateNodeHFile(node_h_path, grad_node_h_str); VLOG(6) << "-------- GenerateNodeCCFile -------"; - GenerateNodeCCFile(output_dir, grad_node_cc_str); + std::string node_cc_path = output_dir + "/nodes/nodes.tmp.cc"; + GenerateNodeCCFile(node_cc_path, grad_node_cc_str); } static void PrepareAttrMapForOps() { -- GitLab