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

nn.Graph Tutorial of AMP #448

Open
strint opened this issue Mar 17, 2022 · 7 comments
Open

nn.Graph Tutorial of AMP #448

strint opened this issue Mar 17, 2022 · 7 comments

Comments

@strint
Copy link
Contributor

strint commented Mar 17, 2022

AMP背景资料:

OneFlow AMP

打开AMP

全局开关 + 显示标记一定可以做fp16的tensor

class CustomGraph(flow.nn.Graph):
            def __init__(self):
                super().__init__()
                self.m = CustomModule()

                # amp开关
                self.config.enable_amp(True)

            def build(self, x):
                # 在自动推理的基础上,指定一定可以fp16执行的tensor
                x = flow._C.amp_white_identity(x)
                x = self.m(x)
                return x

关闭AMP

待讨论

GradScale

和torch接口对齐的GradScaler

class CustomGraph(flow.nn.Graph):
            def __init__(self):
                super().__init__()
                self.m = CustomModule()

                self.config.enable_amp(True)
                
                # 和torch接口对齐的GradScaler
                grad_scaler = flow.amp.GradScaler(
                    init_scale=3000,
                    growth_factor=2.0,
                    backoff_factor=0.5,
                    growth_interval=1000,
                )
                self.set_grad_scaler(grad_scaler)

            def build(self, x):
                x = self.m(x)
                return x

关联接口:

@strint
Copy link
Contributor Author

strint commented Mar 17, 2022

GradScaler还没导出API

amp_white_identity还没导出API

@leaves-zwx
Copy link

amp_white_identity 这个解释确实困难,感觉得把那个染色算法解释了,才好说清楚这个 op 的含义。

@strint
Copy link
Contributor Author

strint commented Mar 17, 2022

明天能达到的标准,我们约定为How-To:

  • 大致的概念介绍
  • API使用演示的一个How-To的例子

达到的效果:给我们自己的算法同事一个最基本的教程,他们可以参考把功能启动起来;而不是接口使用都要口口相传;

@Alive1024
Copy link
Contributor

Alive1024 commented Mar 18, 2022

GradScaler还没导出API

amp_white_identity还没导出API

想确认下这里说的“还没导出”指的是目前还不能从 onflow 访问到吗?nightly 版的 OneFlow 使用它会直接报 AttributeError
还用不了的话,编写 tutorial 中的例子时该怎么说呢?
另外,是需要在所有 "一定可以fp16执行的tensor" 上都要执行这个 op 吗?
@strint

@strint
Copy link
Contributor Author

strint commented Mar 18, 2022

GradScaler还没导出API
amp_white_identity还没导出API

想确认下这里说的“还没导出”指的是目前还不能从 onflow 访问到吗?nightly 版的 OneFlow 使用它会直接报 AttributeError 还用不了的话,编写 tutorial 中的例子时该怎么说呢?

可以使用。不过没有加API Doc。我过会儿加下,在tutorial中不用管。

另外,是需要在所有 "一定可以fp16执行的tensor" 上都要执行这个 op 吗?

不是,默认可以不用加,amp_white_identity什么时候该使用比较隐晦,先不提这个api算了。

@Alive1024
Copy link
Contributor

@strint XiaoYu哥我想请教确认下, of 与 pytorch 有类似的 GradScaler,但是在使用 of 的 GradScaler 的时候,是只需要在 nn.Graph 中通过 set_grad_scaler 方法指定一个 GradScaler 对象就可以了,而不需要像 pytorch 那样手动显式调用 scalestep 等方法?看了一下 of 的 GradScaler 也没暴露出这些方法

@strint
Copy link
Contributor Author

strint commented Mar 29, 2022

@strint XiaoYu哥我想请教确认下, of 与 pytorch 有类似的 GradScaler,但是在使用 of 的 GradScaler 的时候,是只需要在 nn.Graph 中通过 set_grad_scaler 方法指定一个 GradScaler 对象就可以了,而不需要像 pytorch 那样手动显式调用 scalestep 等方法?看了一下 of 的 GradScaler 也没暴露出这些方法

是的。这个也是 nn.Graph 中在图里面自动调度的。

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

No branches or pull requests

3 participants