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

【Hackathon No.65】Refactor and modernize for emscripten #8985

Merged
merged 6 commits into from
May 17, 2022

Conversation

daquexian
Copy link
Contributor

@daquexian daquexian commented May 2, 2022

hackathon 链接:#8600

rfc 链接:PaddlePaddle/community#86


对 cmake 和第三方库做了一些现代化的改造,以支持 emscripten

paddle 到 paddle lite 的 web 模型转换已经可以在 https://convertmodel.com 使用了

Signed-off-by: daquexian <daquexian566@gmail.com>
@paddle-bot-old
Copy link

paddle-bot-old bot commented May 2, 2022

Thanks for your contribution!

@CLAassistant
Copy link

CLAassistant commented May 2, 2022

CLA assistant check
All committers have signed the CLA.

@@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

if(NOT LITE_WITH_ARM)
if(LITE_WITH_X86)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个属于 bugfix

option(BUILD_STATIC_LIBS "" ON)
option(BUILD_TESTING "" OFF)
set(GFLAGS_NAMESPACE "google;gflags")
add_subdirectory(${PROJECT_SOURCE_DIR}/third-party/gflags)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

这样改动有两个原因

  1. cmake ExternalProject_Add 不知道为什么和 emscripten 不兼容
  2. ExternalProject_Add 本身也是很古代的、不应该再使用的 API 了,对 cmake 子项目应该用 add_subdirectory 或者 FetchContent。例如:https://cliutils.gitlab.io/modern-cmake/chapters/projects.html

@@ -140,6 +139,11 @@ set(COMMON_FLAGS
-Wno-error=maybe-uninitialized # Warning in boost gcc 7.2
)

if (NOT EMSCRIPTEN)
# disable -Werror for Emscripten
set(COMMON_FLAGS "${COMMON_FLAGS} -Werror")
Copy link
Contributor Author

@daquexian daquexian May 2, 2022

Choose a reason for hiding this comment

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

-Werror 会对 emscripten 链接器也生效,而且没办法用 -Wno-error=xxx 来 suppress,所以在使用 emscripten 时就关掉了

@@ -152,7 +152,7 @@ class LightPredictorImpl : public lite_api::PaddlePredictor {
virtual ~LightPredictorImpl();
std::unique_ptr<lite_api::Tensor> GetInput(int i) override;
std::unique_ptr<const lite_api::Tensor> GetOutput(int i) const override;
std::unique_ptr<lite_api::Tensor> GetInputByName(const std::string& name);
std::unique_ptr<lite_api::Tensor> GetInputByName(const std::string& name) override;
Copy link
Contributor Author

@daquexian daquexian May 2, 2022

Choose a reason for hiding this comment

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

所有对 .h/.cc 的改动都是在修复 emscripten 所用的 clang 报出来的错误,如

  1. 对临时变量使用 std::move 而影响了 copy elision
  2. 重写基类的方法但没加 override
  3. 定义却没使用的 private field 和变量
  4. 不全面的分支逻辑
  5. char* c = std::string(...).c_str() 引起的野指针 bug

@@ -0,0 +1,365 @@
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
Copy link
Contributor Author

@daquexian daquexian May 2, 2022

Choose a reason for hiding this comment

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

paddle lite 所用的 protobuf 版本是六年前的,已经非常旧了,在使用 emscripten 编译时会出现错误,此外也不支持跳过编译 protoc。本来想升级 protobuf-host submodule,但为了稳妥,还是选择了用 patch 的形式 cherry-pick 两个必要的 pr:

protocolbuffers/protobuf#3480
protocolbuffers/protobuf#3878

虽然这样做能解决问题,但是还是强烈建议 paddle lite 团队升级依赖库

…ARY_DIR

Signed-off-by: daquexian <daquexian566@gmail.com>

# Thirdparty
set(THIRD_PARTY_PATH "${CMAKE_BINARY_DIR}/third_party" CACHE STRING
set(THIRD_PARTY_PATH "${PADDLE_BINARY_DIR}/third_party" CACHE STRING
Copy link
Contributor Author

@daquexian daquexian May 2, 2022

Choose a reason for hiding this comment

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

所有的 CMAKE_SOURCE_DIR 改成了正确的 PADDLE_SOURCE_DIR,CMAKE_BINARY_DIR 改成了正确的 PADDLE_BINARY_DIR,否则 paddle-lite 无法作为其它项目的 subdirectory

@daquexian daquexian changed the title 【PaddlePaddle Hackathon 2】Refactor for emscripten and also modernize 【Hackathon No.65】Refactor for emscripten and also modernize May 2, 2022
@daquexian daquexian changed the title 【Hackathon No.65】Refactor for emscripten and also modernize 【Hackathon No.65】Refactor and modernize for emscripten May 2, 2022
Copy link
Collaborator

@hong19860320 hong19860320 left a comment

Choose a reason for hiding this comment

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

感谢大佬~ 辛苦@Shixiaowei02 @DannyIsFunny 两位review下吧,特别是第三方库的修改。

@hong19860320
Copy link
Collaborator

hackathon 链接:#8600

rfc 链接:PaddlePaddle/community#86

对 cmake 和第三方库做了一些现代化的改造,以支持 emscripten

paddle 到 paddle lite 的 web 模型转换已经可以在 https://convertmodel.com 使用了

试用了一下,非常赞~
我这有个小问题, 目前转换后的模型默认是arm cpu,是否能额外提供设置参数的文本输入框呢?

@@ -675,6 +676,8 @@ void SparseConvDetectPass::Apply(const std::unique_ptr<SSAGraph>& graph) {
&flag_semi,
ch_out,
ch_in);
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里请用 LOG(FATAL) 报错

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 Author

Choose a reason for hiding this comment

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

已修改 @Shixiaowei02

@Shixiaowei02
Copy link
Collaborator

Shixiaowei02 commented May 5, 2022

Thank you very much for your contribution! Changes to file cmake/protobuf-host-patch need to be fully tested, thanks!

@daquexian
Copy link
Contributor Author

我这有个小问题, 目前转换后的模型默认是arm cpu,是否能额外提供设置参数的文本输入框呢?

可以的~

@daquexian
Copy link
Contributor Author

Thank you very much for your contribution! Changes to file #8985 (comment) need to be fully tested, thanks!

这里的改动来自于官方的两个 PR protocolbuffers/protobuf#3480protocolbuffers/protobuf#3878 ,而且只有使用 emscripten 编译时才会应用,所以感觉还是比较稳妥的~

Signed-off-by: daquexian <daquexian566@gmail.com>
@daquexian daquexian requested a review from liyancas as a code owner May 6, 2022 07:24
@hong19860320
Copy link
Collaborator

部分required CI 没过,正在重跑哈~

@daquexian
Copy link
Contributor Author

😂 ci 好像仍然因为奇怪的原因挂掉了

@hong19860320
Copy link
Collaborator

😂 ci 好像仍然因为奇怪的原因挂掉了

Lite-CodeStyle-Precommit 这个 CI 主要是检查代码风格的,不知道为啥总是挂,也没提示代码风格错误,其它 CI 我再在内部确认下。

@hong19860320
Copy link
Collaborator

Lite-CodeStyle-Precommit 这个 CI 仍然挂了,估计是代码风格检查失败,你能在paddlelite 的docker https://paddle-lite.readthedocs.io/zh/develop/source_compile/docker_env.html 内提交代码吗?里面能够保障clang-format版本和CI 是一致的。

@daquexian
Copy link
Contributor Author

Lite-CodeStyle-Precommit 这个 CI 仍然挂了,估计是代码风格检查失败,你能在paddlelite 的docker https://paddle-lite.readthedocs.io/zh/develop/source_compile/docker_env.html 内提交代码吗?里面能够保障clang-format版本和CI 是一致的。

好的

@daquexian
Copy link
Contributor Author

daquexian commented May 13, 2022

另外 web 模型转换工具前端的代码和 convertmodel.com 本身有比较高的集成,并且有一些历史遗留的质量不高的代码。如果可以的话,我私发给你们? 😂

Signed-off-by: daquexian <daquexian566@gmail.com>
@daquexian
Copy link
Contributor Author

Lite-CodeStyle-Precommit 这个 CI 仍然挂了,估计是代码风格检查失败,你能在paddlelite 的docker https://paddle-lite.readthedocs.io/zh/develop/source_compile/docker_env.html 内提交代码吗?里面能够保障clang-format版本和CI 是一致的。

确实有一个代码风格错误,已修复并 push

Copy link
Collaborator

@hong19860320 hong19860320 left a comment

Choose a reason for hiding this comment

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

LGTM

@hong19860320
Copy link
Collaborator

Lite-CodeStyle-Precommit 这个 CI 仍然挂了,估计是代码风格检查失败,你能在paddlelite 的docker https://paddle-lite.readthedocs.io/zh/develop/source_compile/docker_env.html 内提交代码吗?里面能够保障clang-format版本和CI 是一致的。

确实有一个代码风格错误,已修复并 push

好的,看来 Lite-CodeStyle-Precommit 已经过了,其它出错的 CI 我再 rerun下,Required 的都过了就可以合入了

@hong19860320
Copy link
Collaborator

另外 web 模型转换工具前端的代码和 convertmodel.com 本身有比较高的集成,并且有一些历史遗留的质量不高的代码。如果可以的话,我私发给你们? 😂

感谢!可以发到这个邮箱哈~ dongzihan@baidu.com

@hong19860320 hong19860320 merged commit 7e42611 into PaddlePaddle:develop May 17, 2022
@daquexian daquexian deleted the build_refactor branch June 12, 2022 12:33
@zhupengyang zhupengyang mentioned this pull request Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants