Skip to content

Feature: in-place solvers#665

Merged
mrava87 merged 23 commits into
PyLops:devfrom
mrava87:feat-inplacesolvers
Jul 6, 2025
Merged

Feature: in-place solvers#665
mrava87 merged 23 commits into
PyLops:devfrom
mrava87:feat-inplacesolvers

Conversation

@mrava87
Copy link
Copy Markdown
Collaborator

@mrava87 mrava87 commented May 17, 2025

This PR introduces two new functionalities to all of the solvers in the optimization module, namely:

  • a method called memory_usage that can be used before running the setup and step methods to get an approximate estimate of the memory usage of the solver.
  • a new optional parameter called preallocate that modifies the behavior of the solver compared to the current one, by preallocating all of the arrays in the setup method and performing all operations on arrays in-place (note that this does not apply to the Op.matvec/Op.rmatvec operations as those are currently not implemented in-place)

* approximate refers to the fact that the memory usage is calculated taking into account only the main variables of the solvers (ND-arrays of the size of either the model or the data) and not additional lists or arrays that store losses or other metrics.

@mrava87 mrava87 added the enhancement New feature or request label May 17, 2025
@mrava87 mrava87 requested a review from cako May 23, 2025 11:59
@mrava87
Copy link
Copy Markdown
Collaborator Author

mrava87 commented May 23, 2025

@cako I understand if you are busy and have no time for this, but if you can and are interested I'd appreciate any feedback (see https://github.com/PyLops/pylops_inplacesolvers for comparison with current solvers and in particular https://github.com/PyLops/pylops_inplacesolvers/blob/main/notebooks/Visualize.ipynb for some visualization of timings)

@mrava87 mrava87 marked this pull request as draft May 29, 2025 20:28
@cako
Copy link
Copy Markdown
Collaborator

cako commented May 31, 2025

Had a cursory look, and everything looked good. I did have a couple questions regarding intended use. Seems like preallocating is slower? And it doesnt change the memory consumption? If so, is there any reason to doing it? Regarding the memory usage computation, I think it's very useful.

mrava87 added 3 commits June 7, 2025 20:44
When preallocate=False is selected, all solvers behave
equivalently to the current solvers without performing
any operation in-place.
@mrava87
Copy link
Copy Markdown
Collaborator Author

mrava87 commented Jun 8, 2025

Had a cursory look, and everything looked good. I did have a couple questions regarding intended use. Seems like preallocating is slower? And it doesnt change the memory consumption? If so, is there any reason to doing it? Regarding the memory usage computation, I think it's very useful.

Thanks @cako!

Mmh not sure where you see that preallocating is slower. I re-run all experiments with N=3_000_000 (data and model size) and I get the below speed-ups:
speedups

Regarding memory, yes I do not expect the peak memory to change with pre-allocation as anyways the same variables are either created in the step method over and over again (current behavior) or upfront and filled (new behavior)...

In general, after some thinking, I decided to modify a bit the inner working of all the solvers when preallocate=False and revert back to the original codes (like they were still used with JAX arrays); this is mostly to avoid any backward compatibility problem as I noticed that numpy (and even more cupy) are quite strict in terms of data types when doing operations in place (eg +=) - basically an error will be raised if np.complex64 is added in place to a np.float32... whilst I think this is actually good and helps users to make sure their operators and solvers don't do unwanted internal casting (and that would be another incentive for using the new preallocate option), I do not want codes already written to break 😉

@mrava87 mrava87 marked this pull request as ready for review June 15, 2025 21:20
@mrava87
Copy link
Copy Markdown
Collaborator Author

mrava87 commented Jun 24, 2025

@cako just checking you saw my reply?

@cako
Copy link
Copy Markdown
Collaborator

cako commented Jun 28, 2025

Yup! Sorry forgot to reply. But yes, your answer makes sense.

@mrava87
Copy link
Copy Markdown
Collaborator Author

mrava87 commented Jul 2, 2025

Yup! Sorry forgot to reply. But yes, your answer makes sense.

Great, I am going to do one last look and polishing and then I will merge 😄

@mrava87 mrava87 merged commit 1db6169 into PyLops:dev Jul 6, 2025
13 of 14 checks passed
@mrava87 mrava87 deleted the feat-inplacesolvers branch July 6, 2025 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants