Skip to content

Conversation

@thisjiang
Copy link
Contributor

@thisjiang thisjiang commented Jun 24, 2021

PR types

Bug fixes

PR changes

APIs

Describe

简介

本PR用于解决ProgramDesc转Program时会丢失parameter信息的问题,本PR需要在Vardesc中添加is_parameter选项,该选项用于标识Variable是否为Parameter

起因

当前Program转Graph转Program会丢失parameter信息:

from paddle.fluid.framework import IrGraph
from paddle.fluid import core
before_graph = IrGraph(core.Graph(main_program.desc), for_test=False)
after_graph = before_graph.to_program()

以PaddleNLP里的GPT模型为例,打印转换前的program还有persist trainable param这种标识了paramvars信息:
截屏2021-06-24 下午5 05 37

而在转换之后的program中就只剩下persist var信息,param信息丢失了:
截屏2021-06-24 下午5 06 43

原因

Paddle中数据分为两类:Variable和Parameter,其中Variable存的是中间变量,Parameter存的是参数。此外,还有一个属性persitable用于标识哪些数据需要被持久化保留。由于现有desc中并没有标识是否为Parameter的选项,因此Parameter在desc中被保存为了persist var,而这就是为什么转换后的Program丢失了Parameter信息的原因:

Parameter in Program -> persistable in ProgramDesc -> Persistable Variable in Program

猜测

  1. Fleet API下转换前后模型性能下降的原因或许与Parameter丢失有很大关系(待验证)

修改点

paddle/fluid/framework/framework.protomessage VarDesc中添加如下两个新属性:

optional bool is_parameter = 5 [ default = false ];
optional bool stop_gradient = 6 [ default = false ];

is_parameter用于判断本var是否为Parameter。但var本身仍然只是Variable类型而非Parameter类型,并不能解决问题。既然需要保证转换前后的Program完全一致,那么转换前是Parameter类型,转换后也应该是Parameter类型,这并不是简单的添加一个is_parameter的属性就能解决的。

Parameter继承Variable,但多出了额外属性如trainable。若单纯使用is_parameter选项判断Parameter,由于在父类Variable的方法中用到了子类Parameter的特有属性,程序会因为在非Parameter中找不到该属性而报错。

修改python/paddle/fluid/framework.pyBlock类的_sync_with_cpp函数,在创建var时增加一个判断:

  • var.is_parameter()True,调用create_parameter创建Parameter
  • 否则调用create_var创建Variable

同时在创建时传入var.stop_gradient参数。

加上后,重新打印program:
截屏2021-07-02 下午4 27 33

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

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@thisjiang thisjiang changed the title [No Merge] test vardesc add parameterized attribute for graph_to_program ensure graph_to_program retain Parameter var Jun 29, 2021
@thisjiang thisjiang changed the title ensure graph_to_program retain Parameter var graph_to_program save parameter and stop_gradient information Jul 16, 2021
vardesc.type() == core.VarDesc.VarType.LOD_TENSOR and \
vardesc.need_check_feed() == True and \
varobj._stop_gradient == True and \
varobj.stop_gradient == True and \
Copy link
Member

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 ==:

7e22ef7f0594d5938d0160d254b790ab

Reference:
https://www.python.org/dev/peps/pep-0008/#programming-recommendations

Copy link
Contributor Author

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.
Copy link
Member

Choose a reason for hiding this comment

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

2018 -> 2021

Copy link
Contributor Author

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()
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

@zhhsplendid zhhsplendid 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
Contributor

@chenwhql chenwhql left a 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

@zhhsplendid zhhsplendid merged commit 8a7dee3 into PaddlePaddle:develop Jul 28, 2021
@thisjiang thisjiang deleted the test_vardesc_add_parameterized branch July 28, 2021 06:38
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.

3 participants