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

Add paddle custom flags support #56256

Merged
merged 53 commits into from
Aug 30, 2023
Merged

Conversation

huangjiyi
Copy link
Member

@huangjiyi huangjiyi commented Aug 14, 2023

PR types

Others

PR changes

Others

Description

背景

paddle 目前使用 gflags 第三方库来统一管理各文件中定义的 flag,以及通过命令行和环境变量的方式来对 flag 进行赋值,但由于 gflags 的内部机制,可能会导致外部用户在同时使用 Paddle C++ 库和 gflags 第三方库的时候会在运行报错,同时对于 Paddle 的使用需求,gflags 中的很多功能是冗余的,

Done

针对上述存在的问题,这个 PR 根据 Paddle 的功能需求实现了一个精简的 flags 独立工具库来替换 gflags,主要实现了以下功能:

  1. DEFINE 和 DECLARE Flag 的宏:PD_DEFINE_<type>PD_DECLARE_<type>
  2. 通过命令行参数或者环境变量给指定 Flag 赋值
  3. 其他需要的实用函数

实现上述功能的源文件为 paddle/utils/flags_native.hpaddle/utils/flags_native.cc,具体的设计方案可以查看:实现 Paddle flags 工具库

  • 另外,这个 PR 添加了一个编译选项 WITH_GFLAGS (默认 = OFF) 来切换到原始使用 gflags 第三方库的版本,该编译选项打开后的 CI 通过情况可查看 [flags test] turn on WITH_GFLAGS #56478

文件 paddle/utils/flags.h 统一了切换新旧版本 flags 工具库的接口

Todo

  1. 替换 cinn 中 gflags 的用法: update cinn flags usage #56896
  2. 控制在 Windows 上 Flag 的符号导出:export flags defined in phi on windows #56848
  3. 分析这个 PR 需要在一些 paddle::xxx 用法加上前缀 :: 的原因:fix paddle namespace conflict when using paddle_flags #56913

遗留问题

  1. 由于 gflags 目前被其他第三方库 glog 和 brpc 依赖,所以没办法完全移除 gflags 第三方库,但可以通过控制向外部用户暴露的符号来解决用户同时使用 Paddle C++ 库和 gflags 库出现的问题

@paddle-bot
Copy link

paddle-bot bot commented Aug 14, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added contributor External developers status: proposed labels Aug 14, 2023
@huangjiyi huangjiyi changed the title Add paddle custom flags support [WIP] Add paddle custom flags support Aug 14, 2023
@huangjiyi
Copy link
Member Author

huangjiyi commented Aug 22, 2023

@yuanlehome , 我打开 WITH_SHARED_PHI 选项编译 inference 库后,测试 Paddle-Inference-Demo/c++/gpu/resnet50 没有问题:
d3f7f87d9a82599b374fa9afeb2fd34

后续也在 develop 分支测了一下 inference 库用 libphi.so 的情况,发现确实会出现 gflags 的报错:
image

所以这个 PR 应该修复了这个问题

@yuanlehome
Copy link
Contributor

@yuanlehome , 我打开 WITH_SHARED_PHI 选项编译 inference 库后,测试 Paddle-Inference-Demo/c++/gpu/resnet50 没有问题: d3f7f87d9a82599b374fa9afeb2fd34

后续也在 develop 分支测了一下 inference 库用 libphi.so 的情况,发现确实会出现 gflags 的报错: image

所以这个 PR 应该修复了这个问题

好的,赞赞赞👍

YuanRisheng
YuanRisheng previously approved these changes Aug 24, 2023
zyfncg
zyfncg previously approved these changes Aug 25, 2023
Comment on lines +413 to +414
::paddle::distributed::PSParameter _ps_param;
::paddle::distributed::PaddlePSEnvironment _ps_env;
Copy link
Contributor

Choose a reason for hiding this comment

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

这个后续还是需要追查一下原因,毕竟原来不需要加::前缀,而且distributed目录下的命名空间都这么写感觉不是一个合理的形式

chenwhql
chenwhql previously approved these changes Aug 25, 2023
@huangjiyi huangjiyi dismissed stale reviews from chenwhql, zyfncg, and YuanRisheng via 6875a7a August 25, 2023 16:40
YuanRisheng
YuanRisheng previously approved these changes Aug 28, 2023
Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LGTM

@YuanRisheng YuanRisheng merged commit 2ef4ec7 into PaddlePaddle:develop Aug 30, 2023
@USTCKAY USTCKAY mentioned this pull request Sep 5, 2023
BeingGod pushed a commit to BeingGod/Paddle that referenced this pull request Sep 9, 2023
* update

* repalce gflags header

* replace DEFINE_<type> with PD_DEFINE_<type>

* fix bug

* fix bug

* fix bug

* update cmake

* add :: before some paddle namespace

* fix link error

* fix CI-Py3

* allow commandline parse

* fix SetFlagsFromEnv

* fix bug

* fix bug

* fix CI-CINN

* fix CI-Coverage-build

* fix CI-Windows-build

* fix CI-Inference

* fix bug

* fix bug

* fix CI-CINN

* fix inference api test

* fix infer_ut test

* revert infer_ut gflags usage

* update

* fix inference

* remove flags export macro

* revert inference demo_ci gflags usage

* update

* update

* update

* update

* update

* update

* update

* update

* fix bug when turn on WITH_GFLAGS

* turn on WITH_GFLAGS

* fix bug when turn on WITH_GFLAGS

* fix bug when turn on WITH_GFLAGS

* update

* update and add unittest

* add unittest

* fix conflict

* rerun ci

* update

* resolve conflict
@huangjiyi huangjiyi deleted the paddle_flags branch January 9, 2024 12:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants