-
Notifications
You must be signed in to change notification settings - Fork 9
DM-49302: Make GetTemplateTask more memory efficient #387
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
Conversation
parejkoj
left a comment
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.
Several comments; I'm concerned about some of these changes causing either failures or incorrect behavior in ways the current tests won't catch. I don't know for sure that it's wrong, but I'm worried.
I also don't think the dels provide any real benefit.
python/lsst/ip/diffim/getTemplate.py
Outdated
| del unwarped | ||
| del maskedImages |
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.
I don't think these can hurt, but they may not reduce memory usage here (the fact that they're C++ objects under the hood also complicates matters).
Maybe put an explicit note above the dels about why they're deleted 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.
I'l certainly add a comment. In my memory profiling, if I leave all of the dels out the total memory usage is ~500MB higher.
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.
Please do add that comment.
| return geom.Point2D(-10000, -10000) | ||
|
|
||
|
|
||
| def generate_data_id(*, |
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.
ooof, is this really necessary? That's unfortunate. I'd have thought there was an easier way to do this in the middleware, but maybe not if we don't have a real butler?
If you lifted this from elsewhere, is there a place we could put it that both could use instead of having it be copied?
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 original that I based this on is in lsst.cell_coadds. I certainly don't want to add a dependency on that package here, and my version is modified from that one. This code could be refactored and added to utils or pipe_base, but I would like to do that on a different ticket if possible.
python/lsst/ip/diffim/getTemplate.py
Outdated
| # Free memory between iterations | ||
| del weight |
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.
I don't think this does anything, because it gets overwrittten it immediately at the start of the loop, so the old array will go out of scope.
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.
It doesn't need to be in the for loop, and the comment is misleading. The memory savings is from the weight from the last iteration being deleted before new arrays are created after the loop. This saves ~200MB from the peak memory use in my profiling.
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.
Ah, I was thinking average memory, but I could see it shaving a bit off the peak.
| good = maskedImage.variance.array > 0 | ||
| weight = afwImage.ImageF(maskedImage.getBBox()) | ||
| weight.array[good] = maskedImage.variance.array[good]**(-0.5) | ||
| weight = maskedImage.variance.array[good]**(-0.5) |
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.
I think this is a logic change that is incorrect. good here is a bitmask and I don't know that the assignment below (maskedImage.image.array[good] *= weight) will behave correctly. I also am not sure the tests properly explore the cases here (e.g. bad variance on the edge and in the middle).
I don't know what numpy's behavior is when assigning to a bitmasked-array in this manner, and I'd be a bit surprised if it was fully self-consistent with the old logic.
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.
maskedImage.variance.array[good] returns a numpy array of only the values of maskedImage.variance.array where good is True. The later assignment maskedImage.image.array[good] *= weight does indeed update just the elements of maskedImage.image.array that correspond to indices where good is True. This is a feature of numpy arrays, which should be stable and reliable.
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.
Ah, ok. As I think about it more, since it's a 1d array under the hood, I guess the logic holds. I was just surprised, as I don't think I'd seen this style of usage before.
098b86e to
b0766d3
Compare
parejkoj
left a comment
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.
Please add the comment where you delete a couple images, otherwise this looks good. Hopefully it saves us enough memory and doesn't slow anything down!
b0766d3 to
858b34f
Compare
These changes include keeping the input coadds as deferredDatasetHandles until individual tracts are assembled, cleaning up variables in loops, and using indices instead of full arrays in a few places.
Edit: The other PR has merged, so now only commits from this ticket are on this PR