-
Notifications
You must be signed in to change notification settings - Fork 240
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 memory leak by removing custom __copy__ logic #1227
Fix memory leak by removing custom __copy__ logic #1227
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1227 +/- ##
==========================================
- Coverage 84.53% 84.51% -0.03%
==========================================
Files 92 92
Lines 5982 5966 -16
==========================================
- Hits 5057 5042 -15
+ Misses 925 924 -1 ☔ View full report in Codecov by Sentry. |
Update: I am able to run my models with roughly 6GB of memory usage instead of 70-100GB. It appears the change fixed the memory leak. |
Hi |
Not sure this is related to this issue, but I now wonder what the copy argument of transform is intended for ?
|
@romainVala I assume the copy argument is to make a deep copy of the subject and only operate on a copy of the subject, whereas the initial loaded subject stays unchanged. I think one use case of |
Is this not what you would expect? Otherwise, you might run out of memory if your dataset is not small. |
This is exactly what I expect but wouldn't then the copy be entirely obsolete? We are re-loading it from disk every time so we always have the initial state available. What is the point to protect (through copying) the initial subject, if we never re-store it, i.e. change the image on disk? |
this was exactly my point, I do not see how the copy argument change the behaviour If I test with a tensor, it is the same
So both original subject and the transformed one are identical despite copy=False argument. but wait , I find out where it comes from: If I change the line then so this is indeed related to this issue #1223 Note that it is now also working for my first exemple (with subject colin) so the copy argument act similary wheter or not one use tensor as input |
@nicoloesch I'm assuming #1228 needs to be merged before this? Sorry folks, I'm a bit busy these days. |
@fepegar please excuse my delayed response. I think the order of merging both commits should not matter as they touch upon different parts of the code base. The only mutual file is |
Hello, Is there any chance that this gets merged? |
@sh-shahrokhi I am not entirely sure when this is getting merged into the main branch. In the meantime, you can still utilise my feature branch that alleviates the issue (also referenced in the PR). The only ``issue'' is that its 0.20.1 instead of the newest version of torchio |
Picking this up now. |
7245fb8
to
f8335ef
Compare
Thanks everyone for the discussion and your patience, and of course thanks @nicoloesch for your great work! |
Fixes #1222 .
Description
This change removes the custom
__copy__
logic intorchio.Subject
. As a result, callingcopy.copy()
on atorchio.Subject
will no longer create a mixture of shallow and deep copies. Therefore, tocopy.copy(subject)
copy.deepcopy(subject)
Removing this mix of deep and shallow copies within the method for a shallow copy (
__copy__
) appears to be beneficial for the memory management in python and the garbage collector correctly freeingtorchio.Subject
and their images that are no longer referenced.This required the change from
copy.copy
tocopy.deepcopy
in thetorchio.Transform
base class to not modify the original subject but rather create a copy of it that is modified using the transform (applied only ifcopy=True
during instantiation of any transform). I assume this is the excepted behaviour based on the provided documentation and code logic.Checklist
CONTRIBUTING
docs and have a developer setup (especially important arepre-commit
andpytest
)pytest
make html
inside thedocs/
folderImportant Notes
The absence of the memory leak has only been verified on my local machine as outlined in the comment in the original issue and should be verified independently by the reviewer on their machine given the instructions in the linked comment.