Skip to content

MAINT: sparse: add lazy loading for csgraph and linalg #22815

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

Merged
merged 3 commits into from
Apr 8, 2025

Conversation

j-bowhay
Copy link
Member

@j-bowhay j-bowhay commented Apr 8, 2025

Reference issue

closes #22382

What does this implement/fix?

Adds lazy loading for the two submodules in order to speed up import times.

Additional information

@github-actions github-actions bot added scipy.sparse maintenance Items related to regular maintenance tasks labels Apr 8, 2025
@j-bowhay j-bowhay requested a review from dschult April 8, 2025 13:22
@lucascolley lucascolley added this to the 1.16.0 milestone Apr 8, 2025
Copy link
Contributor

@dschult dschult left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks like what we want.

  • csgraph will no longer be imported at the time sparse is imported; only when sparse.csgraph` is accessed.
  • linalg will be auto-imported when accessed (it currently is not imported with sparse)

I'm not sure about the details of tach.toml. Why does linalg stay in the "depends_on" for scipy.sparse? Does something in sparse depends on linalg? If so does linalg get imported when importing sparse when using "tach"?

@j-bowhay
Copy link
Member Author

j-bowhay commented Apr 8, 2025

This change looks like what we want.

  • csgraph will no longer be imported at the time sparse is imported; only when sparse.csgraph` is accessed.
  • linalg will be auto-imported when accessed (it currently is not imported with sparse)

I'm not sure about the details of tach.toml. Why does linalg stay in the "depends_on" for scipy.sparse? Does something in sparse depends on linalg? If so does linalg get imported when importing sparse when using "tach"?

For whatever reason tach didn't like the style of one of the imports. I tweaked it slightly which now allows us to remove both csgraph and linalg from the tach.toml.

Copy link
Contributor

@dschult dschult left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a nice way to import matrix_power and allow lazy loading. Thanks!

This looks good to me!

@lucascolley
Copy link
Member

lucascolley commented Apr 8, 2025

For whatever reason tach didn't like the style of one of the imports. I tweaked it slightly which now allows us to remove both csgraph and linalg from the tach.toml.

I would suggest reverting that change. It is true that sparse depends on sparse.linalg for spmatrix.matrix_power, even though the dependency is not at import-time.

One could argue that keeping track of import-time dependencies only is more useful than dependencies in general, but I think at this stage it is helpful to be explicit about potential runtime gotchas.

EDIT: tach itself is not just about import-time deps (hence the initial failures until you tweaked the style), but rather modularity more broadly, so I think that change is more of a circumvention of tach's detection than a sort of 'fix'

@j-bowhay
Copy link
Member Author

j-bowhay commented Apr 8, 2025

For whatever reason tach didn't like the style of one of the imports. I tweaked it slightly which now allows us to remove both csgraph and linalg from the tach.toml.

I would suggest reverting that change. It is true that sparse depends on sparse.linalg for spmatrix.matrix_power, even though the dependency is not at import-time.

One could argue that keeping track of import-time dependencies only is more useful than dependencies in general, but I think at this stage it is helpful to be explicit about potential runtime gotchas.

I'm not sure I entirely agree. If we also use also track runtime dependencies then it looses its "sharpness" as I could add sparse.linalg as an import-time dependency without complaint. The original complaint was about import-time so I think we should focus on that.

@lucascolley
Copy link
Member

The original complaint was about import-time so I think we should focus on that.

Sure, but I could imagine a similar issue about runtime instead, so I don't think either way is favoured. Hence I think explicit rather than implicit is better.

If we also use also track runtime dependencies then it looses its "sharpness" as I could add sparse.linalg as an import-time dependency without complaint

There should be other ways to achieve this (or better) sharpness. Reading the docs, it looks like we could add something like:

[[interfaces]]
expose = ["matrix_power"]
from = ["scipy.sparse.linalg"]
visibility = ["scipy.sparse"]
exclusive = true

I'm running into some errors when trying to get recent versions of tach to run locally, though

Comment on lines 52 to 53
import scipy
return scipy.sparse.linalg.matrix_power(self, power)
Copy link
Member

@lucascolley lucascolley Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mean to block the PR as tach-bikeshedding is insignificant compared to the import performance improvement, but as another data-point, Ralf was against this pattern at #22412 (comment)

Copy link
Member

@lucascolley lucascolley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks Jake, and for the review Dan!

@j-bowhay j-bowhay merged commit 392c13f into scipy:main Apr 8, 2025
38 of 40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Items related to regular maintenance tasks scipy.sparse
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: sparse.spmatrix: allow fast import
3 participants