-
Notifications
You must be signed in to change notification settings - Fork 802
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
fix flow.set_grad_mode when directly calling #10059
Conversation
return func(*args, **kwargs) | ||
|
||
result = func(*args, **kwargs) | ||
oneflow._oneflow_internal.autograd.set_grad_enabled(self.prev_mode) |
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.
可以导出这个api?看torch有torch.set_grad_enabled : https://pytorch.org/docs/stable/_modules/torch/autograd/grad_mode.html#set_grad_enabled
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.
导出api不太行,这个class还有其它用法,只导出成api就只能当函数调用了
|
||
with flow.set_grad_enabled(True): | ||
test_case.assertTrue(flow.is_grad_enabled()) | ||
flow.set_grad_enabled(False) |
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.
这个类的用法比较多,可以当装饰器、with域、也可以直接调用,可以考虑在里面测试一下装饰器的用法,保证 __call__
方法调用的时候恢复的 prev_mode 数据是正确的
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.
这个类的用法比较多,可以当装饰器、with域、也可以直接调用,可以考虑在里面测试一下装饰器的用法,保证
__call__
方法调用的时候恢复的 prev_mode 数据是正确的
加上了
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
Code got formatted by CI. Please request CI again if you still want to have this PR merged. If the PR is from a forked repo, please download the patch files from the GitHub Actions web page and apply them locally. |
…-Inc/oneflow into dev_fix_set_grad_enabled
Speed stats:
|
Speed stats:
|
CI failed when running job: cuda-module. PR label automerge has been removed |
Speed stats:
|
CI failed when running job: cpu-module. PR label automerge has been removed |
Speed stats:
|
View latest API docs preview at: https://staging.oneflow.info/docs/Oneflow-Inc/oneflow/pr/10059/ |
Speed stats:
|
#close https://github.com/Oneflow-Inc/OneCloud/issues/203#issuecomment-1473171630
原先这里和 torch 的实现不同,我们是用 AutoGradMode 这个 C++ 对象的 RAII 来实现更改 grad mode 的,而 torch 是显式地调用 set_grad_mode 来更改的。这就导致了 OneFlow 里面没法全局修改线程里面的 grad mode,只能在装饰器或者上下文语句里面修改。