Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows system supports Ninja compilation #31161

Merged
merged 20 commits into from
Mar 4, 2021

Conversation

Avin0323
Copy link
Contributor

@Avin0323 Avin0323 commented Feb 23, 2021

PR types

Others

PR changes

Others

Describe

Windows系统支持使用Ninja编译Paddle代码,仅对Windows系统中生效,对Linux/MacOS系统无影响。

本PR支持Windows系统下Ninja构建系统后,Paddle在Windows下同时可以使用MSVC、Ninja进行构建,为后续依赖Ninja构建系统提升Windows/Windows CI效率体验做准备铺垫,Windows CI及用户文档均保持现状不直接切换Ninja使用,待后续逐步推进。

背景

Ninja是注重速度的小型构件系统,它存在优势:

  1. 构建速度快】与MSVC、MakeFile等构建系统在target层面上并行不同,Ninja在编译/链接子任务上进行并行,并行程度更高,构建速度更快;
  2. 快速暴露问题】由于Ninja在编译/链接任务层面并行,当出现编译问题时,可以终止构建等待并行任务结束退出,而MSVC、MakeFile需要等待全部target下编译任务结束才可以退出;
  3. 支持sccache等编译器缓存工具】MSVC中无法使用sccache等编译器缓存工具,在CI系统上会浪费大量编译耗时,Ninja支持较多的编译器缓存工具;

Windows下支持Ninja的主要原因:

  1. 与pytorch对齐,目前pytorch在windows下的编译构建工具为Ninja;
  2. 为Windows CI进一步提升做铺垫准备:
    a. 可以在windows CI中使用sccache,与Linux CI中的ccache类似,通过编译器缓存来加速CI编译;
    b. Windows CI编译错误时打印的信息过多,不利于快速定位问题,Ninja的日志展示与Linux/MacOS MakeFile类似,可以快速发现问题,提升CI体验;

存在问题

目前Paddle代码在windows上使用Ninja编译存在以下4个问题:

  1. paddle/fluid/pybind/CMakeLists.txtop_function_generator.exemklml.dlliomp.dllopenblas.dllmkldnn.dll共五处路径在Ninja和MSVC中有差异;
  2. 第三方依赖库cryptopp使用的CMakeLists.txt中存在一处编译参数FI\"winapifamily.h\"无法在Ninja构建系统中正确解析;
  3. cmake二次生成Ninja配置时出现extern_protobuf缺失;
  4. 编译third_party时提示缺失protoc.exe,编译paddle时提示缺失cryptopp依赖库;

解决方案

针对于上述存在的问题,分别做了如下优化修改:

  1. 对于构建系统运行期的路径选择,使用cmake生成表达式来忽略不同构建系统的差异;
  2. 通过patch解决依赖库中的编译问题;
  3. 优化编译依赖,确保依赖项在前置阶段先生成;

测试结果

  • windows cpu ci测试:

测试日志:https://xly.bce.baidu.com/paddlepaddle/paddle/newipipe/detail/2476450/job/3468727

image

  • windows gpu ci测试:

测试日志:https://xly.bce.baidu.com/paddlepaddle/paddle/newipipe/detail/2476451/job/3468728

image

@Avin0323 Avin0323 changed the title support for compiling with Ninja [WIP]support for compiling with Ninja Feb 23, 2021
@Avin0323 Avin0323 changed the title [WIP]support for compiling with Ninja support for compiling with Ninja Mar 3, 2021
Copy link
Contributor

@zhwesky2010 zhwesky2010 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看一下连续增量编译时,会不会报错

# file is that the compilation parameters are changed to 'FIwinapifamily.h'. This
# patch command can be removed when upgrading to a higher version.
if("${CMAKE_GENERATOR}" STREQUAL "Ninja")
set(CRYPTOPP_PATCH_COMMAND ${CMAKE_COMMAND} -E copy_if_different "${PADDLE_SOURCE_DIR}/patches/cryptopp/CMakeLists.txt" "<SOURCE_DIR>/")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

高版本的cryptopp就没这个问题对吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,升级的时候最好再验证一下

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cryptopp写个TODO吧,升级到哪个版本之后,删除patch中内容

@@ -374,11 +374,16 @@ if (LINUX)
endif()

if (WIN32)
if("${CMAKE_GENERATOR}" STREQUAL "Ninja")
set(paddle_framework_lib_path ${CMAKE_CURRENT_BINARY_DIR})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里也能改成 $<TARGET_FILE_DIR: paddle_framework_shared> 这种形式吗

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个就不行了,后面configure_file不能正确解析cmake的生成表达式。

@@ -417,11 +422,16 @@ if (LINUX)
endif()

if (WIN32)
if("${CMAKE_GENERATOR}" STREQUAL "Ninja")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同configure_file不能正确解析cmake的生成表达式。

@@ -26,4 +26,7 @@ cc_library(paddle_inference_c_shared SHARED SRCS ${C_API_SRCS} DEPS paddle_infer
set_target_properties(paddle_inference_c_shared PROPERTIES OUTPUT_NAME paddle_inference_c)
if(WIN32)
target_link_libraries(paddle_inference_c_shared shlwapi.lib)
if("${CMAKE_GENERATOR}" STREQUAL "Ninja")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

analysis_predictor 能否直接作为 paddle_inference_c_shared 的DEP

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个depend是不需要的了,已删除

@@ -101,11 +101,16 @@ if(WITH_PYTHON)
set(tmp_impl_file ${impl_file}.tmp)

if(WIN32)
if("${CMAKE_GENERATOR}" STREQUAL "Ninja")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上,能否改成 $<TARGET_FILE_DIR: xxx>的形式

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同configure_file不能正确解析cmake的生成表达式。

@@ -31,6 +31,7 @@ wmic process where name="op_function_generator.exe" call terminate
taskkill /f /im python.exe 2>NUL

rem ------initialize common variable------
set GENERATOR=Ninja
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最后还是 默认设 Visual Studio 14 2015 Win64吧,可以在CI配置里面加这句设 Ninja

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个后面会删掉,目前测试用。

@@ -146,6 +147,15 @@ rem set CLCACHE_OBJECT_CACHE_TIMEOUT_MS=1000000
:: set maximum cache size to 20G
rem clcache.exe -M 21474836480

:: install ninja if GENERATOR is Ninja
if "%GENERATOR%" == "Ninja" (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ninja的话,改一下build目录,和第三方库的目录吧,加个后缀,build_ninja,防止和原有的冲突。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

暂时先不用区分build目录吧,这个PR只是支持Ninja构建,暂时不会在CI中使用,等后续使用的时候再换吧?

Copy link
Contributor

@zhwesky2010 zhwesky2010 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM,后面如果应用ninja+sccache的话,CI目录要和vs分开。

Copy link
Contributor

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Avin0323 Avin0323 changed the title support for compiling with Ninja Windows system supports Ninja compilation Mar 4, 2021
Copy link
Collaborator

@raindrops2sea raindrops2sea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@luotao1 luotao1 merged commit 4d6d2db into PaddlePaddle:develop Mar 4, 2021
@Avin0323 Avin0323 deleted the compile-with-ninja branch March 5, 2021 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants