-
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
add varbasecopy func to fix the ParamBase type bug in layers.to API #32789
add varbasecopy func to fix the ParamBase type bug in layers.to API #32789
Conversation
Thanks for your contribution! |
static void VarBaseCopy(const imperative::VarBase &src, | ||
imperative::VarBase &dst, const P &dst_device, | ||
const bool blocking) { | ||
if (dst.SharedVar()->IsEmpty()) { |
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.
add else branch and throw error to avoid bug
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.
Done
paddle/fluid/pybind/imperative.cc
Outdated
dst.SetDataType(src.DataType()); | ||
dst.SetType(src.Type()); | ||
dst.SetOverridedStopGradient(src.OverridedStopGradient()); | ||
if (!src.SharedVar()->IsEmpty()) { |
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.
same above, add else branch and throw error to avoid bug, can use PADDLE_THROW
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.
Done
@@ -1639,6 +1672,10 @@ void BindImperative(py::module *m_ptr) { | |||
self.nrings_ = nrings; | |||
}); | |||
|
|||
m.def("varbase_copy", &VarBaseCopy<platform::Place>); | |||
m.def("varbase_copy", &VarBaseCopy<platform::CPUPlace>); | |||
m.def("varbase_copy", &VarBaseCopy<platform::CUDAPlace>); |
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.
do we need XPUPlace here?
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.
Done
new_t = t._copy_to(device, blocking) | ||
if dtype is not None and dtype != t.dtype: | ||
new_t = new_t.cast(dtype=dtype) | ||
if isinstance(t, framework.ParamBase): |
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.
the ParamBase._copy_to
is still error, whether override the ParamBase._copy_to
method to fix this bug?
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.
Done
new_t = new_t.cast(dtype=dtype) | ||
if isinstance(t, framework.ParamBase): | ||
state = copy.deepcopy(t.__dict__) | ||
new_param = framework.ParamBase(t.shape, dtype, **state) |
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.
能否给ParamBase添加一个_copy_to的方法覆盖原来的?
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.
已经添加了
auto *dst_tensor = dst.MutableVar()->GetMutable<framework::LoDTensor>(); | ||
dst_tensor->set_lod(src_tensor.lod()); | ||
framework::TensorCopy(src_tensor, dst_device, dst_tensor); | ||
if (blocking) { |
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.
if (!blocking), do we need IncreaseVarbaseReferenceCountUntilCopyComplete
?
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.
Added
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
PR types
Bug fixes
PR changes
APIs
Describe
Fix the bug that the layers.to API will not keep the parameters type. It will change the type from paddle.fluid.framework.ParamBase to paddle.Tensor and lose the attribution of raw type.