-
Notifications
You must be signed in to change notification settings - Fork 5.9k
graph_to_program save parameter and stop_gradient information #33771
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
graph_to_program save parameter and stop_gradient information #33771
Conversation
|
Thanks for your contribution! |
… test_vardesc_add_parameterized
| vardesc.type() == core.VarDesc.VarType.LOD_TENSOR and \ | ||
| vardesc.need_check_feed() == True and \ | ||
| varobj._stop_gradient == True and \ | ||
| varobj.stop_gradient == True and \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is old code, so it is not your fault and you have nothing to change. However, I would like you to know it, there is a coding style recommendation in Python: don't compare boolean values to True or False using ==:
Reference:
https://www.python.org/dev/peps/pep-0008/#programming-recommendations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reminder!
| @@ -0,0 +1,205 @@ | |||
| # Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2018 -> 2021
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
|
||
| @staticmethod | ||
| def build_program(): | ||
| program = fluid.default_main_program() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please write 2.0 API instead of fluid API for the test cases in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
zhhsplendid
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
chenwhql
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM for framework&var_desc change

PR types
Bug fixes
PR changes
APIs
Describe
简介
本PR用于解决ProgramDesc转Program时会丢失parameter信息的问题,本PR需要在
Vardesc中添加is_parameter选项,该选项用于标识Variable是否为Parameter。起因
当前Program转Graph转Program会丢失parameter信息:
以PaddleNLP里的GPT模型为例,打印转换前的program还有

persist trainable param这种标识了param的vars信息:而在转换之后的program中就只剩下

persist var信息,param信息丢失了:原因
Paddle中数据分为两类:Variable和Parameter,其中Variable存的是中间变量,Parameter存的是参数。此外,还有一个属性
persitable用于标识哪些数据需要被持久化保留。由于现有desc中并没有标识是否为Parameter的选项,因此Parameter在desc中被保存为了persist var,而这就是为什么转换后的Program丢失了Parameter信息的原因:猜测
修改点
在
paddle/fluid/framework/framework.proto的message VarDesc中添加如下两个新属性:is_parameter用于判断本var是否为Parameter。但var本身仍然只是Variable类型而非Parameter类型,并不能解决问题。既然需要保证转换前后的Program完全一致,那么转换前是Parameter类型,转换后也应该是Parameter类型,这并不是简单的添加一个is_parameter的属性就能解决的。修改
python/paddle/fluid/framework.py的Block类的_sync_with_cpp函数,在创建var时增加一个判断:var.is_parameter()为True,调用create_parameter创建Parametercreate_var创建Variable同时在创建时传入
var.stop_gradient参数。加上后,重新打印program:

可能看到
param已经被加上了,且数目能完全对上,此外stop_gradient属性也都能对上了。