generate_file_structures.py 7.1 KB
Newer Older
1
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2
#
3 4 5
# 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
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9 10 11 12 13 14 15 16 17
# 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.

import sys
import os

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

def GenerateFileStructureForFinalDygraph(eager_dir):
    """
    paddle/fluid/eager
    |- generated
    |  |- CMakeLists.txt
    |  |  "add_subdirectory(forwards), add_subdirectory(backwards)"
    |  
    |  |- forwards
    |     |- "dygraph_functions.cc"
    |     |- "dygraph_functions.h"
    |
    |  |- backwards
    |     |- "nodes.cc"
    |     |- "nodes.h"
    """
    # Directory Generation
    generated_dir = os.path.join(eager_dir, "api/generated/eager_generated")
    forwards_dir = os.path.join(generated_dir, "forwards")
    nodes_dir = os.path.join(generated_dir, "backwards")
    dirs = [generated_dir, forwards_dir, nodes_dir]
    for directory in dirs:
        if not os.path.exists(directory):
            os.mkdir(directory)

    # Empty files
    dygraph_forward_api_h_path = os.path.join(generated_dir,
                                              "dygraph_functions.h")
    empty_files = [dygraph_forward_api_h_path]
    empty_files.append(os.path.join(forwards_dir, "dygraph_functions.cc"))
    empty_files.append(os.path.join(nodes_dir, "nodes.cc"))
    empty_files.append(os.path.join(nodes_dir, "nodes.h"))

    for path in empty_files:
        if not os.path.exists(path):
            open(path, 'a').close()


56
def GenerateFileStructureForIntermediateDygraph(eager_dir, split_count):
57 58 59 60 61 62 63
    """
    paddle/fluid/eager
    |- generated
    |  |- CMakeLists.txt
    |  |  "add_subdirectory(forwards), add_subdirectory(nodes)"
    |  
    |  |- forwards
64
    |     |- "dygraph_forward_functions.cc"
65
    |     |- CMakeLists.txt
66
    |     |  "cc_library(dygraph_function SRCS dygraph_forward_functions.cc DEPS ${eager_deps} ${fluid_deps} GLOB_OP_LIB)"
67 68
    |
    |  |- nodes
69 70
    |     |- "nodes.cc"
    |     |- "nodes.h"
71
    |     |- CMakeLists.txt
72
    |     |  "cc_library(dygraph_node SRCS nodes.cc DEPS ${eager_deps} ${fluid_deps})"
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    | 
    |  |- dygraph_forward_api.h
    """
    # Directory Generation
    generated_dir = os.path.join(eager_dir, "api/generated/fluid_generated")
    forwards_dir = os.path.join(generated_dir, "forwards")
    nodes_dir = os.path.join(generated_dir, "nodes")
    dirs = [generated_dir, forwards_dir, nodes_dir]
    for directory in dirs:
        if not os.path.exists(directory):
            os.mkdir(directory)

    # Empty files
    dygraph_forward_api_h_path = os.path.join(generated_dir,
                                              "dygraph_forward_api.h")
    empty_files = [dygraph_forward_api_h_path]
89
    empty_files.append(os.path.join(nodes_dir, "nodes.h"))
90

91 92 93 94 95 96 97 98
    for i in range(split_count):
        empty_files.append(
            os.path.join(forwards_dir,
                         "dygraph_forward_functions" + str(i + 1) + ".cc"))
        empty_files.append(os.path.join(nodes_dir,
                                        "nodes" + str(i + 1) + ".cc"))
    empty_files.append(
        os.path.join(forwards_dir, "dygraph_forward_functions_args_info.cc"))
99 100 101 102 103 104 105 106 107 108 109
    for path in empty_files:
        if not os.path.exists(path):
            open(path, 'a').close()

    # CMakeLists
    nodes_level_cmakelist_path = os.path.join(nodes_dir, "CMakeLists.txt")
    generated_level_cmakelist_path = os.path.join(generated_dir,
                                                  "CMakeLists.txt")
    forwards_level_cmakelist_path = os.path.join(forwards_dir, "CMakeLists.txt")

    with open(nodes_level_cmakelist_path, "w") as f:
110 111
        f.write("add_custom_target(\n")
        f.write("  copy_dygraph_node\n")
112
        f.write(
113
            "  COMMAND ${CMAKE_COMMAND} -E copy_if_different \"${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/nodes/nodes.tmp.h\" \"${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/nodes/nodes.h\"\n"
114
        )
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        for i in range(split_count):
            f.write(
                "  COMMAND ${CMAKE_COMMAND} -E copy_if_different \"${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/nodes/nodes"
                + str(i + 1) +
                ".tmp.cc\" \"${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/nodes/nodes"
                + str(i + 1) + ".cc\"\n")

        f.write("  DEPENDS eager_codegen\n")
        f.write("  VERBATIM)\n")

        f.write("cc_library(dygraph_node SRCS ")
        for i in range(split_count):
            f.write("nodes" + str(i + 1) + ".cc ")
        f.write("DEPS ${eager_deps} ${fluid_deps} ${fluid_manual_nodes})\n")
        f.write("add_dependencies(dygraph_node copy_dygraph_node)")
130 131

    with open(forwards_level_cmakelist_path, "w") as f:
132 133
        f.write("add_custom_target(\n")
        f.write("  copy_dygraph_forward_functions\n")
134
        f.write(
135
            "  COMMAND ${CMAKE_COMMAND} -E copy_if_different \"${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/dygraph_forward_api.tmp.h\" \"${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/dygraph_forward_api.h\"\n"
136
        )
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        for i in range(split_count):
            f.write(
                "  COMMAND ${CMAKE_COMMAND} -E copy_if_different \"${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/forwards/dygraph_forward_functions"
                + str(i + 1) +
                ".tmp.cc\" \"${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/forwards/dygraph_forward_functions"
                + str(i + 1) + ".cc\"\n")
        f.write(
            "  COMMAND ${CMAKE_COMMAND} -E copy_if_different \"${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/forwards/dygraph_forward_functions_args_info.tmp.cc\" \"${PADDLE_SOURCE_DIR}/paddle/fluid/eager/api/generated/fluid_generated/forwards/dygraph_forward_functions_args_info.cc\"\n"
        )
        f.write("  DEPENDS eager_codegen\n")
        f.write("  VERBATIM)\n")

        f.write("cc_library(dygraph_function SRCS ")
        for i in range(split_count):
            f.write("dygraph_forward_functions" + str(i + 1) + ".cc ")
        f.write("dygraph_forward_functions_args_info.cc ")
        f.write(
            "DEPS ${eager_deps} ${fluid_deps} ${GLOB_OP_LIB} ${GLOB_OPERATOR_DEPS} ${fluid_manual_functions})\n"
        )
        f.write(
            "add_dependencies(dygraph_function copy_dygraph_forward_functions)")
158 159 160

    with open(generated_level_cmakelist_path, "w") as f:
        f.write("add_subdirectory(forwards)\nadd_subdirectory(nodes)")
161 162 163


if __name__ == "__main__":
164
    assert len(sys.argv) == 3
165
    eager_dir = sys.argv[1]
166 167
    split_count = int(sys.argv[2])
    GenerateFileStructureForIntermediateDygraph(eager_dir, split_count)
168
    GenerateFileStructureForFinalDygraph(eager_dir)