Skip to content

Conversation

@zhangbo9674
Copy link
Contributor

PR types

New features

PR changes

APIs

Describe

1、Split function GradScaler::minimize() to GradScaler::step() + GradScaler::update()

GradScaler::minimize():

    scaler = paddle.amp.GradScaler(init_loss_scaling=1024)

    with paddle.amp.auto_cast():
        output = model(data)
        loss = mse(output, label)

    scaled = scaler.scale(loss)
    scaled.backward()            
    scaler.minimize(optimizer, scaled)
    optimizer.clear_grad()

GradScaler::step() + GradScaler::update():

    scaler = paddle.amp.GradScaler(init_loss_scaling=1024)

    with paddle.amp.auto_cast():
        output = model(data)
        loss = mse(output, label)

    scaled = scaler.scale(loss)
    scaled.backward() 
    scaler.step(optimizer)
    scaler.update()
    optimizer.clear_grad()
  • minimize() and step()+update() are two methods of parameter gradient updating in amp. In paddle 2.0, we recommend using step()+update().

  • If optimizer belongs to paddle 1.0, only minimize() can be used.

2、Add GradScaler::unscale_(optimizer):

    scaler = paddle.amp.GradScaler(init_loss_scaling=1024)

    with paddle.amp.auto_cast():
        output = model(data)
        loss = mse(output, label)

    scaled = scaler.scale(loss)
    scaled.backward() 
    scaled.unscale_(optimizer)
    scaler.step(optimizer)
    scaler.update()
    optimizer.clear_grad()
  • This API is used to unscale the gradients of parameters, multiplies the gradients of parameters by 1/(loss scaling ratio).
  • If unscale_ is not called, minimize() or step() will call this api, else this call will not be repeated.

3、docs review:

GradScaler:
图片
step+update:
图片
unscale_:
图片
中文文档pr链接:
PaddlePaddle/docs#3897

* split minimize() to step() + update()

* add unscale and step for grad_scaler

* add unittest

* refine code in minimize

* delete step in loss_scaler

* fix example bug

* refine comment

* refine unittest

* add unittest
@paddle-bot-old
Copy link

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

TCChenlong
TCChenlong previously approved these changes Sep 23, 2021
lanxianghit
lanxianghit previously approved these changes Sep 23, 2021
@zhangbo9674 zhangbo9674 dismissed stale reviews from lanxianghit and TCChenlong via d1906e4 September 23, 2021 10:41
Copy link
Contributor

@TCChenlong TCChenlong 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

@zhiqiu zhiqiu left a comment

Choose a reason for hiding this comment

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

LGTM

@lanxianghit lanxianghit merged commit e262125 into PaddlePaddle:release/2.2 Sep 26, 2021
YuanRisheng added a commit to YuanRisheng/Paddle that referenced this pull request Sep 26, 2021
[cherry pick]split minimize and add unscale_ for GradScaler (PaddlePaddle#35927)
@zhangbo9674 zhangbo9674 deleted the cp/split_minimize_add_unscale branch March 2, 2023 02:56
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.

4 participants