CMakeLists.txt 16.2 KB
Newer Older
1 2
add_subdirectory(utils)

3 4 5 6 7 8 9 10 11 12
if(WITH_GPU)
  nv_library(
    phi_tensor_raw
    SRCS tensor.cc
    DEPS tensor_base dense_tensor phi_api_utils phi_enforce)
elseif(WITH_ROCM)
  hip_library(
    phi_tensor_raw
    SRCS tensor.cc
    DEPS tensor_base dense_tensor phi_api_utils phi_enforce)
13
else()
14 15 16 17
  cc_library(
    phi_tensor_raw
    SRCS tensor.cc
    DEPS tensor_base dense_tensor phi_api_utils phi_enforce)
18 19
endif()

20
set(api_gen_base ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/api_base.py)
21

22
# forward api file
23 24
set(api_gen_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/api_gen.py)
set(api_yaml_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/api.yaml)
Z
zyfncg 已提交
25
set(legacy_api_yaml_file
26
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/legacy_api.yaml)
27 28
set(api_header_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/include/api.h)
set(api_source_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/api.cc)
29 30 31
set(api_header_file_tmp ${api_header_file}.tmp)
set(api_source_file_tmp ${api_source_file}.tmp)

32
# backward api file
33
set(bw_api_gen_file
34 35
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/backward_api_gen.py)
set(bw_api_yaml_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/backward.yaml)
Z
zyfncg 已提交
36
set(legacy_bw_api_yaml_file
37
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/legacy_backward.yaml)
38 39
set(bw_api_header_file
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/backward/backward_api.h)
40
set(bw_api_source_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/backward_api.cc)
41 42 43
set(bw_api_header_file_tmp ${bw_api_header_file}.tmp)
set(bw_api_source_file_tmp ${bw_api_source_file}.tmp)

44
# dygraph(intermediate) api file
45
set(im_api_gen_file
46
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/intermediate_api_gen.py)
47 48 49 50
set(dygraph_api_header_file
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/dygraph_api.h)
set(dygraph_api_source_file
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/dygraph_api.cc)
51 52 53
set(dygraph_api_header_file_tmp ${dygraph_api_header_file}.tmp)
set(dygraph_api_source_file_tmp ${dygraph_api_source_file}.tmp)

54
# sparse api file
55
set(sparse_api_gen_file
56
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/sparse_api_gen.py)
57
set(sparse_api_yaml_file
58
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/sparse_api.yaml)
59 60
set(sparse_api_header_file
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/include/sparse_api.h)
61
set(sparse_api_source_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/sparse_api.cc)
62 63 64 65
set(sparse_api_header_file_tmp ${sparse_api_header_file}.tmp)
set(sparse_api_source_file_tmp ${sparse_api_source_file}.tmp)

# sparse bw api file
66
set(sparse_bw_api_gen_file
67
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/sparse_bw_api_gen.py)
68
set(sparse_bw_api_yaml_file
69
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/sparse_bw_api.yaml)
70 71 72 73
set(sparse_bw_api_header_file
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/backward/sparse_bw_api.h)
set(sparse_bw_api_source_file
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/sparse_bw_api.cc)
74 75
set(sparse_bw_api_header_file_tmp ${sparse_bw_api_header_file}.tmp)
set(sparse_bw_api_source_file_tmp ${sparse_bw_api_source_file}.tmp)
76

J
Jack Zhou 已提交
77
# strings api file
78
set(strings_api_gen_file
79
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/strings_api_gen.py)
80
set(strings_api_yaml_file
81
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/strings_api.yaml)
82 83 84 85
set(strings_api_header_file
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/include/strings_api.h)
set(strings_api_source_file
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/strings_api.cc)
J
Jack Zhou 已提交
86 87 88
set(strings_api_header_file_tmp ${strings_api_header_file}.tmp)
set(strings_api_source_file_tmp ${strings_api_source_file}.tmp)

89
# wrapped infermeta file
90
set(wrapped_infermeta_gen_file
91
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/wrapped_infermeta_gen.py)
92 93 94 95
set(wrapped_infermeta_header_file
    ${CMAKE_SOURCE_DIR}/paddle/phi/infermeta/generated.h)
set(wrapped_infermeta_source_file
    ${CMAKE_SOURCE_DIR}/paddle/phi/infermeta/generated.cc)
96

97 98 99 100 101 102 103 104
# op extra info file
set(ops_extra_info_gen_file
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/ops_extra_info_gen.py)
set(api_compat_yaml_file
    ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/api_compat.yaml)
set(ops_extra_info_file
    ${CMAKE_SOURCE_DIR}/paddle/fluid/operators/ops_extra_info.h)

105
if(NOT PYTHONINTERP_FOUND)
106 107 108
  find_package(PythonInterp REQUIRED)
endif()

109
# install extra dependencies
110 111 112 113 114 115 116
if(${PYTHON_VERSION_STRING} VERSION_LESS "3.6.2")
  execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install -U pyyaml
                          typing-extensions>=4.1.1 jinja2==2.11.3)
else()
  execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install -U pyyaml jinja2
                          typing-extensions)
endif()
117 118

# parse apis
119
set(parsed_api_dir ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/parsed_apis)
120 121 122 123 124 125
set(generated_op_path
    ${CMAKE_SOURCE_DIR}/paddle/fluid/operators/generated_op.cc)
set(generated_argument_mapping_path
    ${CMAKE_SOURCE_DIR}/paddle/phi/ops/compat/generated_sig.cc)
message(
  "parse api yamls:
126
- ${api_yaml_file}
Z
zyfncg 已提交
127
- ${legacy_api_yaml_file}
128
- ${bw_api_yaml_file}
Z
zyfncg 已提交
129
- ${legacy_bw_api_yaml_file}")
130
execute_process(
131
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml
132
  COMMAND ${CMAKE_COMMAND} -E make_directory ${parsed_api_dir}
133
  COMMAND ${PYTHON_EXECUTABLE} generator/parse_api.py --api_yaml_path ./api.yaml
134
          --output_path ./parsed_apis/api.parsed.yaml
135 136
  COMMAND ${PYTHON_EXECUTABLE} generator/parse_api.py --api_yaml_path
          ./legacy_api.yaml --output_path ./parsed_apis/legacy_api.parsed.yaml
137
  COMMAND
138 139 140 141 142 143 144
    ${PYTHON_EXECUTABLE} generator/parse_api.py --api_yaml_path ./backward.yaml
    --output_path ./parsed_apis/backward_api.parsed.yaml --backward
  COMMAND
    ${PYTHON_EXECUTABLE} generator/parse_api.py --api_yaml_path
    ./legacy_backward.yaml --output_path
    ./parsed_apis/legacy_backward_api.parsed.yaml --backward RESULTS_VARIABLE
    _results)
145
foreach(_result in ${_results})
146
  if(${_result})
147 148 149 150 151 152
    message(FATAL_ERROR "api yaml parsing failed, exiting.")
  endif()
endforeach()

# validation of api yamls
message("validate api yaml:
153 154
- ${parsed_api_dir}/api.parsed.yaml
- ${parsed_api_dir}/backward_api.parsed.yaml")
155
execute_process(
156
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml
157
  COMMAND
158
    ${PYTHON_EXECUTABLE} generator/cross_validate.py --forward_yaml_paths
Z
zyfncg 已提交
159
    ./parsed_apis/api.parsed.yaml ./parsed_apis/legacy_api.parsed.yaml
160
    --backward_yaml_paths ./parsed_apis/backward_api.parsed.yaml
Z
zyfncg 已提交
161
    ./parsed_apis/legacy_backward_api.parsed.yaml
162 163 164
  RESULT_VARIABLE _result)
if(${_result})
  message(FATAL_ERROR "api validation failed, exiting.")
165 166 167
endif()

# code generation for op, op makers, and argument mapping functions
168 169 170 171
message(
  "create or remove auto-geneated operators: ${generated_op_path}.tmp
create or remove auto-geneated argument mappings: ${generated_argument_mapping_path}.tmp"
)
172
execute_process(
173
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml
174
  COMMAND
175
    ${PYTHON_EXECUTABLE} generator/generate_op.py --api_yaml_path
Z
zyfncg 已提交
176
    ./parsed_apis/api.parsed.yaml --backward_api_yaml_path
177
    ./parsed_apis/backward_api.parsed.yaml --api_version_yaml_path
178 179
    api_version.yaml --api_compat_yaml_path api_compat.yaml --output_op_path
    "${generated_op_path}.tmp" --output_arg_map_path
180 181 182 183
    "${generated_argument_mapping_path}.tmp"
  RESULT_VARIABLE _result)
if(${_result})
  message(FATAL_ERROR "operator codegen failed, exiting.")
184 185 186
endif()

if(EXISTS "${generated_op_path}.tmp" AND EXISTS "${generated_op_path}")
187 188
  execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
                          "${generated_op_path}.tmp" "${generated_op_path}")
189 190
  message("copy if different ${generated_op_path}.tmp ${generated_op_path}")
elseif(EXISTS "${generated_op_path}.tmp")
191 192
  execute_process(COMMAND ${CMAKE_COMMAND} -E copy "${generated_op_path}.tmp"
                          "${generated_op_path}")
193 194
  message("copy ${generated_op_path}.tmp ${generated_op_path}")
else()
195
  execute_process(COMMAND ${CMAKE_COMMAND} -E remove -f "${generated_op_path}")
196 197 198
  message("remove ${generated_op_path}")
endif()

199 200 201 202 203 204 205 206 207 208
if(EXISTS "${generated_argument_mapping_path}.tmp"
   AND EXISTS "${generated_argument_mapping_path}")
  execute_process(
    COMMAND
      ${CMAKE_COMMAND} -E copy_if_different
      "${generated_argument_mapping_path}.tmp"
      "${generated_argument_mapping_path}")
  message(
    "copy if different ${generated_argument_mapping_path}.tmp ${generated_argument_mapping_path}"
  )
209
elseif(EXISTS "${generated_argument_mapping_path}.tmp")
210 211 212 213 214 215
  execute_process(
    COMMAND ${CMAKE_COMMAND} -E copy "${generated_argument_mapping_path}.tmp"
            "${generated_argument_mapping_path}")
  message(
    "copy ${generated_argument_mapping_path}.tmp ${generated_argument_mapping_path}"
  )
216
else()
217 218
  execute_process(COMMAND ${CMAKE_COMMAND} -E remove -f
                          "${generated_argument_mapping_path}")
219 220 221
  message("remove ${generated_argument_mapping_path}")
endif()

222 223 224 225 226 227 228
# generate ops extra info
execute_process(
  COMMAND
    ${PYTHON_EXECUTABLE} ${ops_extra_info_gen_file} --api_compat_yaml_path
    ${api_compat_yaml_file} --ops_extra_info_path ${ops_extra_info_file})
message("generate ${ops_extra_info_file}")

229
# generate forward api
230
add_custom_command(
231
  OUTPUT ${api_header_file} ${api_source_file}
232
  COMMAND ${PYTHON_EXECUTABLE} -m pip install pyyaml
233 234
  COMMAND
    ${PYTHON_EXECUTABLE} ${api_gen_file} --api_yaml_path ${api_yaml_file}
Z
zyfncg 已提交
235
    ${legacy_api_yaml_file} --api_header_path ${api_header_file_tmp}
236 237 238 239 240 241
    --api_header_path ${api_header_file_tmp} --api_source_path
    ${api_source_file_tmp}
  COMMAND ${CMAKE_COMMAND} -E copy_if_different ${api_header_file_tmp}
          ${api_header_file}
  COMMAND ${CMAKE_COMMAND} -E copy_if_different ${api_source_file_tmp}
          ${api_source_file}
242
  COMMENT "copy_if_different ${api_header_file} ${api_source_file}"
243 244
  DEPENDS ${api_yaml_file} ${legacy_api_yaml_file} ${api_gen_file}
          ${api_gen_base}
245 246
  VERBATIM)

247 248
# generate backward api
add_custom_command(
249 250 251 252
  OUTPUT ${bw_api_header_file} ${bw_api_source_file} ${bw_api_header_file_tmp}
         ${bw_api_source_file_tmp}
  COMMAND
    ${PYTHON_EXECUTABLE} ${bw_api_gen_file} --backward_yaml_path
Z
zyfncg 已提交
253
    ${bw_api_yaml_file} ${legacy_bw_api_yaml_file} --backward_header_path
254 255 256 257 258
    ${bw_api_header_file_tmp} --backward_source_path ${bw_api_source_file_tmp}
  COMMAND ${CMAKE_COMMAND} -E copy_if_different ${bw_api_header_file_tmp}
          ${bw_api_header_file}
  COMMAND ${CMAKE_COMMAND} -E copy_if_different ${bw_api_source_file_tmp}
          ${bw_api_source_file}
259
  COMMENT "copy_if_different ${bw_api_header_file} ${bw_api_source_file}"
260 261
  DEPENDS ${bw_api_yaml_file} ${bw_api_gen_file} ${api_gen_base}
          ${legacy_bw_api_yaml_file}
262 263
  VERBATIM)

264 265 266
# generate sparse api
add_custom_command(
  OUTPUT ${sparse_api_header_file} ${sparse_api_source_file}
267 268 269 270 271 272 273 274 275 276 277 278
  COMMAND
    ${PYTHON_EXECUTABLE} ${sparse_api_gen_file} --api_yaml_path
    ${sparse_api_yaml_file} --api_header_path ${sparse_api_header_file_tmp}
    --api_source_path ${sparse_api_source_file_tmp}
  COMMAND ${CMAKE_COMMAND} -E copy_if_different ${sparse_api_header_file_tmp}
          ${sparse_api_header_file}
  COMMAND ${CMAKE_COMMAND} -E copy_if_different ${sparse_api_source_file_tmp}
          ${sparse_api_source_file}
  COMMENT
    "copy_if_different ${sparse_api_header_file} ${sparse_sparse_api_source_file}"
  DEPENDS ${sparse_api_yaml_file} ${sparse_api_gen_file} ${api_gen_base}
          ${api_gen_file}
279 280 281 282 283
  VERBATIM)

# generate backward sparse api
add_custom_command(
  OUTPUT ${sparse_bw_api_header_file} ${sparse_bw_api_source_file}
284 285 286 287 288 289 290 291 292 293 294 295 296
  COMMAND
    ${PYTHON_EXECUTABLE} ${sparse_bw_api_gen_file} --api_yaml_path
    ${sparse_bw_api_yaml_file} --api_header_path
    ${sparse_bw_api_header_file_tmp} --api_source_path
    ${sparse_bw_api_source_file_tmp}
  COMMAND ${CMAKE_COMMAND} -E copy_if_different ${sparse_bw_api_header_file_tmp}
          ${sparse_bw_api_header_file}
  COMMAND ${CMAKE_COMMAND} -E copy_if_different ${sparse_bw_api_source_file_tmp}
          ${sparse_bw_api_source_file}
  COMMENT
    "copy_if_different ${sparse_bw_api_header_file} ${sparse_bw_sparse_api_source_file}"
  DEPENDS ${sparse_bw_api_yaml_file} ${sparse_bw_api_gen_file} ${api_gen_base}
          ${api_gen_file} ${sparse_api_gen_file} ${bw_api_gen_file}
297 298
  VERBATIM)

J
Jack Zhou 已提交
299 300 301
# generate strings api
add_custom_command(
  OUTPUT ${strings_api_header_file} ${strings_api_source_file}
302 303 304 305 306 307 308 309 310 311 312 313
  COMMAND
    ${PYTHON_EXECUTABLE} ${strings_api_gen_file} --api_yaml_path
    ${strings_api_yaml_file} --api_header_path ${strings_api_header_file_tmp}
    --api_source_path ${strings_api_source_file_tmp}
  COMMAND ${CMAKE_COMMAND} -E copy_if_different ${strings_api_header_file_tmp}
          ${strings_api_header_file}
  COMMAND ${CMAKE_COMMAND} -E copy_if_different ${strings_api_source_file_tmp}
          ${strings_api_source_file}
  COMMENT
    "copy_if_different ${strings_api_header_file} ${strings_strings_api_source_file}"
  DEPENDS ${strings_api_yaml_file} ${strings_api_gen_file} ${api_gen_base}
          ${api_gen_file}
J
Jack Zhou 已提交
314 315
  VERBATIM)

316 317 318
# generate dygraph(intermediate) api
add_custom_command(
  OUTPUT ${dygraph_api_header_file} ${dygraph_api_source_file}
319 320
  COMMAND
    ${PYTHON_EXECUTABLE} ${im_api_gen_file} --api_yaml_path ${api_yaml_file}
Z
zyfncg 已提交
321
    ${legacy_api_yaml_file} --sparse_api_yaml_path ${sparse_api_yaml_file}
322 323 324 325 326 327 328 329
    --dygraph_api_header_path ${dygraph_api_header_file_tmp}
    --dygraph_api_source_path ${dygraph_api_source_file_tmp}
  COMMAND ${CMAKE_COMMAND} -E copy_if_different ${dygraph_api_header_file_tmp}
          ${dygraph_api_header_file}
  COMMAND ${CMAKE_COMMAND} -E copy_if_different ${dygraph_api_source_file_tmp}
          ${dygraph_api_source_file}
  DEPENDS ${api_yaml_file} ${sparse_api_yaml_file} ${im_api_gen_file}
          ${api_gen_base} ${api_gen_file}
330 331
  VERBATIM)

332 333 334
# generate wrapped infermeta
add_custom_command(
  OUTPUT ${wrapped_infermeta_header_file} ${wrapped_infermeta_source_file}
335 336
  COMMAND
    ${PYTHON_EXECUTABLE} ${wrapped_infermeta_gen_file} --api_yaml_path
Z
zyfncg 已提交
337
    ${api_yaml_file} ${legacy_api_yaml_file} --wrapped_infermeta_header_path
338 339
    ${wrapped_infermeta_header_file} --wrapped_infermeta_source_path
    ${wrapped_infermeta_source_file}
340 341 342
  DEPENDS ${api_yaml_file} ${wrapped_infermeta_gen_file} ${api_gen_base}
  VERBATIM)

343 344 345 346 347 348 349 350 351 352 353
cc_library(
  op_meta_info
  SRCS op_meta_info.cc
  DEPS phi_tensor_raw)
cc_library(
  wrapped_infermeta
  SRCS ${wrapped_infermeta_source_file}
  DEPS phi)
cc_library(
  context_pool
  SRCS context_pool.cc
354
  DEPS phi_backends phi_enforce place init)
355

356 357 358
cc_library(
  kernel_dispatch
  SRCS kernel_dispatch.cc
359
  DEPS phi_tensor_raw phi_backends kernel_factory context_pool)
360 361 362
cc_library(
  api_gen_utils
  SRCS api_gen_utils.cc
363 364
  DEPS phi_tensor_raw selected_rows sparse_csr_tensor sparse_coo_tensor
       infermeta_utils)
365 366 367
cc_library(
  phi_data_transform
  SRCS data_transform.cc
368
  DEPS phi_tensor_raw phi tensor)
369 370 371 372 373 374 375 376 377 378
cc_library(
  api_custom_impl
  SRCS api_custom_impl.cc
  DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils backward_infermeta
       phi_data_transform)
cc_library(
  sparse_api_custom_impl
  SRCS sparse_api_custom_impl.cc
  DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils phi_data_transform
       tensor_copy)
379

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
cc_library(
  phi_function_api
  SRCS ${api_source_file}
  DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils phi_data_transform
       api_custom_impl)
cc_library(
  phi_bw_function_api
  SRCS ${bw_api_source_file}
  DEPS phi_tensor_raw
       phi
       kernel_dispatch
       api_gen_utils
       backward_infermeta
       phi_data_transform
       phi_function_api
       api_custom_impl
       global_utils)
cc_library(
  sparse_api
  SRCS ${sparse_api_source_file}
  DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils sparse_api_custom_impl)
cc_library(
  sparse_bw_api
  SRCS ${sparse_bw_api_source_file}
  DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils sparse_api
       sparse_api_custom_impl)
cc_library(
  phi_dygraph_api
  SRCS ${dygraph_api_source_file}
  DEPS phi_tensor_raw
       phi
       kernel_dispatch
       api_gen_utils
       phi_data_transform
       phi_function_api
       sparse_api)
cc_library(
  strings_api
  SRCS ${strings_api_source_file}
  DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils)
cc_library(
  phi_tensor
  SRCS tensor_method.cc
  DEPS phi_tensor_raw
       phi_function_api
       api_gen_utils
       kernel_dispatch
       infermeta
       sparse_api
       strings_api)
cc_library(
  tensor_copy
  SRCS tensor_copy.cc
433
  DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils)
434 435 436 437 438 439 440 441
cc_library(
  api_scalar
  SRCS scalar.cc
  DEPS tensor_copy)
cc_library(
  api_int_array
  SRCS int_array.cc
  DEPS tensor_copy)