-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Fix/Fix memory leak in dygraph #17394
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/Fix memory leak in dygraph #17394
Conversation
… feature/sorted_gradient_dygraph
… feature/sorted_gradient_dygraph
… feature/sorted_gradient_dygraph
paddle/fluid/imperative/layer.cc
Outdated
bck_map = new BackwardSumMap(); | ||
grad_ref = new GradientRef(); | ||
bck_map = BackwardSumMap(); | ||
grad_ref = GradientRef(); |
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.
These two lines are unnecessary. BTW, it seems that it is not necessary thatbck_map
and grad_ref
are data member of Autograd
. Just make them temporary variable inside the function.
paddle/fluid/imperative/layer.cc
Outdated
if (grad_ref->find(vb) == grad_ref->end()) { | ||
grad_ref->insert(std::make_pair(vb, 1)); | ||
if (grad_ref.find(vb) == grad_ref.end()) { | ||
grad_ref.insert(std::make_pair(vb, 1)); |
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.
C++ guarantees zero-initialization of primitive types. See here. You can just write ++grad_ref[vb];
paddle/fluid/imperative/layer.cc
Outdated
BackwardSumMap* bck_map; | ||
GradientRef* grad_ref; | ||
BackwardSumMap bck_map; | ||
GradientRef grad_ref; |
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.
No need to be data members.
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.
Excellent.
Fix memory leak problem in dygraph mode and using
unique_ptr
to holdVariable
to make sure it can be freed automatically whenVarbase
is released by python gc