Skip to content

【MODEL UPDATE】Combine params and model#2800

Merged
DannyIsFunny merged 25 commits into
PaddlePaddle:developfrom
DannyIsFunny:MODEL_UPDATE
Feb 13, 2020
Merged

【MODEL UPDATE】Combine params and model#2800
DannyIsFunny merged 25 commits into
PaddlePaddle:developfrom
DannyIsFunny:MODEL_UPDATE

Conversation

@DannyIsFunny
Copy link
Copy Markdown
Collaborator

@DannyIsFunny DannyIsFunny commented Feb 3, 2020

本PR目的:修改Paddle-Lite的模型格式,新的模型格式要求请参考:[设计说明]()
【本PR的修改】:

【1】修改Lite的version,当前代码最后一个commit 不是git log时,将最后一个short commit id作为version : lite/api/version.h.in

【2】修改model_parser.cc模型保存与加载方法:
模型存储: SaveModelNaive方法,修改为保存为单个文件
模型加载: 原来加载方法保存LoadModelNaiveLoadModelNaiveFromMemory(...)
添加新模型的加载方法: LoadModelNaiveFromFileLoadModelNaiveFromMemory(...)

【3】API接口:
paddle_api.cc: MobileConfig添加新接口set_model_from_fileset_model_from_buffer,添加变 lite_model_file和返回值函数lite_model_file()
light_api.cc: 重载新的Build函数、和predictor的构造函数(老方法保存)。用于light_api加载新格式的模型
light_api_impl.cc: 修改CreatePredictor(MobileConfig)方法,实现根据MobileConfig的值选择调用老接口、还是新接口。 (当set_model_dirset_model_buffer后会使用老方法;set_model_from_fileset_model_from_buffer后会使用新方法)

注意:因为发现light_api实际没有加载pb格式模型的接口,本PR在新的light_predictor::build()函数中去除了model_type接口,只加载naive_buffer格式模型。

【4】相关单测修改:
model_parser_test.cc
light_api_test.cc, cxx_api_test.cc, paddle_api_test.cc, apis_test.cc
其他:subgraph_pass_test.cc (因为调用了SaveOptimizedModel + LoadNaiveModel 过程)

【5】关联模块修改 (因为下面两者都调用了SaveOptimizedModel + LoadNaiveModel过程)
benchmark: benchmark.cc
model_test: model_test.cc

【6】添加Python接口: pybind.cc中mobileconfig添加接口: set_model_from_bufferset_model_from_file

【7】添加Java接口: 在MobileConfig.javaconvert_util_jni.h中为JavaAPI添加MobileConfig接口: setModelFromFilesetModelFromBuffer
编译时默认不编译Java API,只有当build_java=ON时才编译java预测库。

【8】本PR将模型转化工具名称修改为opt
编译出的二进制文件名为 opt
流程:输入命令: ./lite/tool/build.sh build_optimize_tool
编译结果:build.opt/lite/api/opt
工具使用方法 : ./opt --model_dir= ...

【9】本次修改对预测库体积大小的影响:
经测试:本PR加入前armv8、GCC Android动态库大小:1653528字节,本PR加入后动态库大小1653528字节,对动态库大小没有影响

Copy link
Copy Markdown
Contributor

@Superjomn Superjomn left a comment

Choose a reason for hiding this comment

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

给出 mobile so 前后的大小变化,避免feature 增加大小

Comment thread lite/model_parser/model_parser.cc Outdated
memcpy(paddle_version_table.cursor(), paddle_version.c_str(), 16);
paddle_version_table.Consume(16);
paddle_version_table.AppendToFile(prog_path);
std::cout << "paddle_version:" << paddle_version << std::endl;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

所有的 cout 改为 glog
非必须log 可以去掉,或者使用 VLOG

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

已经🙆‍♂️修改为VLOG(4)

Comment thread lite/api/light_api.h
const std::string& param_buffer = "",
bool model_from_memory = false,
lite_api::LiteModelType model_type = lite_api::LiteModelType::kProtobuf) {
LightPredictor(const std::string& lite_model_file,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

加注释

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

加注释

已经在MobileConfig中所有函数与成员添加注释,当调用set_model_buffer老接口是LOG(WARNING)输出warning,建议使用新接口。

Comment thread lite/api/paddle_api.h
Comment thread lite/api/paddle_api.h Outdated

public:
void set_model_from_file(const std::string& x) { lite_model_file_ = x; }
void set_model_from_buffer(const std::string& x) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

实现都放到.cc 里

头文件里放重要接口和注释文档,方便看代码的人快速了解相关接口,不陷入实现细节里

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

实现都放到.cc 里

头文件里放重要接口和注释文档,方便看代码的人快速了解相关接口,不陷入实现细节里

已经修改完毕

Comment thread lite/model_parser/model_parser.cc Outdated

// Save lite_version(char[16]) into file
naive_buffer::BinaryTable paddle_version_table;
paddle_version_table.Require(16);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

不要有 magic number

16 和其他数字,特别跟二进制表格式有关的,用 const 常量定义,并且加注释解释下

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

不要有 magic number

16 和其他数字,特别跟二进制表格式有关的,用 const 常量定义,并且加注释解释下

已经修改为const int8 paddle_version_length = 16 * sizeof(char);

@DannyIsFunny
Copy link
Copy Markdown
Collaborator Author

给出 mobile so 前后的大小变化,避免feature 增加大小

经测试:本PR加入前armv8、GCC Android动态库大小:1653528字节,本PR加入后动态库大小1653528字节,对动态库大小没有影响

Comment thread lite/api/light_api.h Outdated
Build(lite_model_file, model_from_memory);
}

// warning: old inference and will be abandened in release/v3.0.0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

NOTE: This is a deprecated API and will be removed in latter release.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

NOTE: This is a deprecated API and will be removed in latter release.

已经替换

Comment thread lite/api/light_api.h Outdated
void Build(const std::string& lite_model_file,
bool model_from_memory = false);

// warning: old inference and will be abandened in release/v3.0.0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

同上

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

同上

已经全量修改

Comment thread lite/api/paddle_api.cc Outdated
lite_model_file_ = x;
model_from_memory_ = true;
}
// warning: set model data from memory buffer, which is in old format and will
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

.cc 的实现里面不需要加这个 warning,.h 里面需要。 一般人只会看 .h 里面的接口,不会深入到实现

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

.cc 的实现里面不需要加这个 warning,.h 里面需要。 一般人只会看 .h 里面的接口,不会深入到实现
已经删除cc文件中的warning 信息

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

在comment 中添加信息描述模型的二进制结构,comment内容如下:

image

Comment thread lite/model_parser/model_parser.cc
Superjomn
Superjomn previously approved these changes Feb 13, 2020
Copy link
Copy Markdown
Contributor

@Superjomn Superjomn left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Contributor

@Superjomn Superjomn left a comment

Choose a reason for hiding this comment

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

现在编译出来的二进制文件是叫 opt 吗?

Comment thread lite/tools/build.sh
# global variables
BUILD_EXTRA=OFF
BUILD_JAVA=ON
BUILD_JAVA=OFF
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

这里为啥关了?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

这里为啥关了?

因为设计评审有一条建议
image
建议最小库不编译java,所以我将build_java设置为关闭,需要手动设置BUILD_JAVA=ON才可以触发编译

Copy link
Copy Markdown
Collaborator Author

@DannyIsFunny DannyIsFunny left a comment

Choose a reason for hiding this comment

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

现在编译出来的二进制文件是叫 opt 吗?

是的,编译出的二进制文件名为 opt
流程:输入命令: ./lite/tool/build.sh build_optimize_tool
编译结果:build.opt/lite/api/opt
工具使用方法 : ./opt --model_dir= ...

Comment thread lite/api/CMakeLists.txt
@@ -296,10 +296,10 @@ if (LITE_ON_TINY_PUBLISH)
endif()
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

修改optimize_tool的名称:从OPT 修改为opt

@@ -64,6 +64,44 @@ public int getPowerModeInt() {
return powerMode.value();
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

新增Java API接口

Comment thread lite/tools/build.sh
# global variables
BUILD_EXTRA=OFF
BUILD_JAVA=ON
BUILD_JAVA=OFF
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

这里为啥关了?

因为设计评审有一条建议
image
建议最小库不编译java,所以我将build_java设置为关闭,需要手动设置BUILD_JAVA=ON才可以触发编译

Copy link
Copy Markdown
Contributor

@Superjomn Superjomn left a comment

Choose a reason for hiding this comment

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

LGTM

@DannyIsFunny DannyIsFunny merged commit 63eca0a into PaddlePaddle:develop Feb 13, 2020
@DannyIsFunny DannyIsFunny deleted the MODEL_UPDATE branch February 13, 2020 10:39
@DannyIsFunny DannyIsFunny changed the title 【MODEL UPDATE】: combine params and model 【MODEL UPDATE】Combine params and model Aug 25, 2020
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.

2 participants