-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
disable __setitem__ in static mode & add API paddle.static.setitem with dy2st strategy #53682
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
❌ The PR is not created using PR's template. You can refer to this Demo. |
Sorry to inform you that 788f8ac's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually. |
|
||
def __setitem__(self, item, value): | ||
if isinstance(self.obj, Variable): | ||
return paddle.fluid.framework._setitem_impl_(self.obj, item, value) |
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.
return paddle.fluid.framework._setitem_impl_(self.obj, item, value) | |
new_var = paddle.fluid.framework._setitem_impl_(self.obj, item, value) | |
# NOTE(dev): Update __dict__ will not modify the id(), but only move the | |
# pointed reference object to the new one. | |
self.obj.__dict__.update(new_var.__dict__) |
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
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
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
…th dy2st strategy (PaddlePaddle#53682) * add paddle.static.setitem * add some help doc * support setitem * support machanism * add more unittest * remove usless code * raise error in static setitem * fix d2s UT * remove static only for both-used code * fix bool set_value in static, fix set_value_op UT * fix unittests * [May case some error]: remove inplace-version check * add two test case for dy2st * fix function in vision * fix dy2st setitem support, refine UT case * fix slice in static_mode * add ParametersMap * remove pop * modify place * [fix]: variable is also a tensor * rewrite some ut & remove slicetransformer in dy2st * solve error in static-mode * fix ut * return a result for set_array_write * fix test_set_value_op_xpu * code is different in dynamic / static mode --------- Co-authored-by: Aurelius84 <zhangliujie@baidu.com> Co-authored-by: NotHaozi <zhangmenghao@baidu.com>
…th dy2st strategy (PaddlePaddle#53682) * add paddle.static.setitem * add some help doc * support setitem * support machanism * add more unittest * remove usless code * raise error in static setitem * fix d2s UT * remove static only for both-used code * fix bool set_value in static, fix set_value_op UT * fix unittests * [May case some error]: remove inplace-version check * add two test case for dy2st * fix function in vision * fix dy2st setitem support, refine UT case * fix slice in static_mode * add ParametersMap * remove pop * modify place * [fix]: variable is also a tensor * rewrite some ut & remove slicetransformer in dy2st * solve error in static-mode * fix ut * return a result for set_array_write * fix test_set_value_op_xpu * code is different in dynamic / static mode --------- Co-authored-by: Aurelius84 <zhangliujie@baidu.com> Co-authored-by: NotHaozi <zhangmenghao@baidu.com>
PR types
Breaking changes
PR changes
APIs
Description
Pcard-66985
Motivation
Currently, the
__setitem__
in static mode is implemented by directly overwriting the result on the original Variable. This leads to although the forward result is correct, the calculation result is wrong in the backward stage (some are because result relies on the initial value of this Variable; some are because the graph in backward constructed according to this also has the problem of overwriting ).The essence of this problem is that the method of directly rewriting variables in static graphs violates the static single assignment form (SSA).
What this PR changed
In Dynamic Mode:
The behavior of
__setitem__
is unchanged. Users can still write code like:x[0] = 10
.In Static Mode
__setitem__
is disabled. Users should usex = paddle.static.setitem(x, 0, 10)
to replacex[0] = 10
.API
paddle.static.setitem
will provide the variable assignment with indexing in static mode, but unlike__setitem__
, it will return a new variable instead of overwriting the original variable.@paddle.jit.to_static
)As Fix setitem error in static mode #55282 , introducing a
ParametersMap
, if there are a new variable mapped to an old variable, the old one will be replaced by the new one.Users are not aware of this change in d2s, with just still using
paddle.jit.to_static
.