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

[Eager]Support Lazy initialization for nn.Layer #44990

Merged
merged 12 commits into from
Aug 17, 2022

Conversation

Aurelius84
Copy link
Contributor

@Aurelius84 Aurelius84 commented Aug 8, 2022

PR types

New features

PR changes

APIs

Describe

what's New?

  • 动态图 Layer 实例化时,新增模型参数延迟初始化功能

  • 新增 LazyInit 类接口,支持两种方式触发参数的Lazy初始化

    • 方式一:LazyInit(Model)(...),即在实例化时,用 LazyInit 进行包裹
    • 方式二:with LazyInit() as lz 方式,在with block内的所有Parameter参数都会lazy初始化
  • 给EagerParamBase 新增一个initialize() 接口,用于应用Lazy hook函数,以自定义初始化param的数据.

import paddle
from paddle import LazyInit
from paddle.nn import Linear
paddle.set_device('cpu')

weight_attr = paddle.ParamAttr(
    name="weight",
    initializer=paddle.nn.initializer.Constant(value=0.5))

bias_attr = paddle.ParamAttr(
    name="bias",
    initializer=paddle.nn.initializer.Constant(value=0.3))

# Usage 1:  LazyInit(Model)
model = LazyInit(Linear)(2, 4, weight_attr=weight_attr, bias_attr=bias_attr)

print(model.weight)       # <------ not initialized
program = model.startup_program

# initialize param
for param in model.parameters():
    param.initialize()     # <------ trigger lazy hook



# Usage 2: with context
with LazyInit() as lz:
     model = Linear(2, 4, weight_attr=weight_attr, bias_attr=bias_attr)
      
    print(model.weight)       # <------ not initialized
    print(lz.startup_program())


# initialize param
for param in model.parameters():
    param.initialize()     # <------ trigger lazy hook

@paddle-bot
Copy link

paddle-bot bot commented Aug 8, 2022

你的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.

@Aurelius84 Aurelius84 changed the title [Eager]Support Lazy initialization for nn.Lazyer [Eager]Support Lazy initialization for nn.Layer Aug 8, 2022
@Aurelius84 Aurelius84 changed the title [Eager]Support Lazy initialization for nn.Layer [WIP][Eager]Support Lazy initialization for nn.Layer Aug 8, 2022
@Aurelius84 Aurelius84 changed the title [WIP][Eager]Support Lazy initialization for nn.Layer [Eager]Support Lazy initialization for nn.Layer Aug 9, 2022
JiabinYang
JiabinYang previously approved these changes Aug 9, 2022
Copy link
Contributor

@JiabinYang JiabinYang left a comment

Choose a reason for hiding this comment

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

LGTM

aoyulong
aoyulong previously approved these changes Aug 10, 2022
Copy link
Contributor

@aoyulong aoyulong left a comment

Choose a reason for hiding this comment

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

现在实现OK,但是还是希望能够有更好的办法隐藏LazyInit调用。

python/paddle/fluid/lazy_init.py Show resolved Hide resolved
@Aurelius84
Copy link
Contributor Author

现在实现OK,但是还是希望能够有更好的办法隐藏LazyInit调用。

如昨天讨论,暂时还没有比较好的方式能够实现概念的完全隐藏

phlrain
phlrain previously approved these changes Aug 10, 2022
aoyulong
aoyulong previously approved these changes Aug 16, 2022
Copy link
Contributor

@aoyulong aoyulong left a comment

Choose a reason for hiding this comment

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

LGTM

JiabinYang
JiabinYang previously approved these changes Aug 16, 2022
Copy link
Contributor

@JiabinYang JiabinYang left a comment

Choose a reason for hiding this comment

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

LGTM

__all__ = ["LazyInit"]


class LazyGuard(object):
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. 为了保持外部开发者的代码统一,建议使用一种使用方式 LazyInit 或者 LazyGuard
  2. 考虑到使用Guard和不使用Guard的时候会需要做代码缩进的调整,改动量较大,Paddle一般不推荐使用Guard的方式,推荐使用LazyInit(Linear)这种方式,除非这种写法写起来确实很麻烦。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已移除 LazyInit中的__enter__和__exit__ 函数

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

@Aurelius84 Aurelius84 merged commit f59c666 into PaddlePaddle:develop Aug 17, 2022
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.

7 participants