Skip to content

CI: Test only changed modules and those that depend on them - #7749

Closed
stefanv wants to merge 37 commits into
scikit-image:mainfrom
stefanv:test-changed-module-dependencies
Closed

stefanv wants to merge 37 commits into
scikit-image:mainfrom
stefanv:test-changed-module-dependencies

Conversation

@stefanv

@stefanv stefanv commented Mar 12, 2025 •

Copy link
Copy Markdown
Member

Credit for this feature goes to @mdhaber.

The purpose of the change is to vastly accelerate CI.
The method is to detect which modules are touched by a PR, and to only test them and their dependencies.

The dependency graph is calculated by (in separate subprocesses) importing each module in skimage, and seeing which modules get brought in by it. These are considered its dependencies.
From the graph, we can answer: if I touch module M, which modules depend on it?

E.g., if I touch the color submodule, I also need to test skimage.io and skimage.segmentation, since skimage.color is imported by those submodules. If I touch skimage._shared, I need to test all submodules.

There may be exceptions and special cases which are not currently taken into consideration.

This PR aims to measure the speed-up that can be achieved, consider the robustness of the approach, and identify any oversights.


Success!: CI run, total runtime 3m49s, of which 2m12s build, 30s for dependency detection and testing

@stefanv stefanv added the 🤖 type: Infrastructure CI, packaging, tools and automation label Mar 12, 2025
@stefanv
stefanv force-pushed the test-changed-module-dependencies branch 4 times, most recently from 5f72999 to 8e3620f Compare March 12, 2025 09:37
@stefanv
stefanv force-pushed the test-changed-module-dependencies branch from 8e3620f to d5ab33a Compare March 12, 2025 09:46
@stefanv
stefanv marked this pull request as draft March 12, 2025 09:52

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

See scipy/scipy#22671 for an alternative to this script

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The intent here was to do something super, super simple to get the ball rolling. It's been possible to do this sort of thing for many years, so I imagined that it was due to lack of trust for more advanced approaches. If we trust tach and don't mind adding the dependency, great. It's probably faster, and it looks like it can be more fine-grained. If this is working, though, I wouldn't wait for it. What's most important here is using this sort of information in CI somehow; how we collect the information can certainly be improved in follow-ups.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yeah, not blocking!

@mdhaber

mdhaber commented Mar 12, 2025 •

Copy link
Copy Markdown
Contributor

Thanks @stefanv! This looks much, much cleaner and more general than what I sent : )

As we discussed, an important exception is when functions perform an import at runtime. I think it's ok to add a mechanism for that later, since:

  • It's relatively rare
  • We will catch these situations when partial tests pass but full tests fail.
  • It's only a problem to skip tests that would fail.

The sooner we see a full test run failure after passing the "smart" run, the more urgent it is : )

That said, it's possible that tach (scipy/scipy#22671) considers of this sort of dependency already? So we might be able to fix it automatically by just using that.

It looks like you committed the NPZ file (which maybe would be better as plain text to avoid unreadable diffs?). I imagine that was intentional because it can be used to check whether new dependencies have been added. I didn't see it, but does this already perform that check?

@stefanv

stefanv commented Mar 12, 2025

Copy link
Copy Markdown
Member Author

It looks like you committed the NPZ file (which maybe would be better as plain text to avoid unreadable diffs?).

That file came from you, actually ;) 1f758cd

@stefanv

stefanv commented Mar 12, 2025 •

Copy link
Copy Markdown
Member Author

I now understand what you are saying: that it would be useful to also use this to detect dependency changes. For that we'd need to store something on disk. Maybe it's indeed just a yaml file: submodule -> dependencies.

@lucascolley

lucascolley commented Mar 12, 2025 •

Copy link
Copy Markdown

Maybe it's indeed just a yaml file: submodule -> dependencies.

or a TOML file (wink wink scipy/scipy#22671 :) )

@stefanv

stefanv commented Mar 12, 2025

Copy link
Copy Markdown
Member Author

or a TOML file (wink wink scipy/scipy#22671 :) )

:) Yeah, I saw that. I am not super enthused about TOML, until 1.1 comes out which gives us proper multi-line dictionaries. But whatever works.

Comment thread tools/module_dependencies.py Resolved
Comment thread tools/module_dependencies.py Outdated
Comment on lines +41 to +44
pkg_modules = _pkg_modules()
pkg_sys_modules = {mod for mod in sys.modules if package in mod}

return pkg_modules & pkg_sys_modules

@mdhaber mdhaber Mar 12, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Assuming _pkg_modules() ends up returning a sorted list, can this just be:

Suggested change
pkg_modules = _pkg_modules()
pkg_sys_modules = {mod for mod in sys.modules if package in mod}
return pkg_modules & pkg_sys_modules
return set(_pkg_modules()) & set(sys.module)

We don't really need to pre-filter pkg_sys_modules if we're &ind with pkg_modules, right?

Comment thread tools/module_dependencies.py Resolved
Comment thread tools/module_dependencies.py Resolved
@stefanv stefanv changed the title Test only changed modules and their dependencies Test only changed modules (and their dependencies?) Mar 14, 2025
@stefanv stefanv changed the title Test only changed modules (and their dependencies?) Test only changed modules (and those that depend on them?) Mar 14, 2025

@mdhaber mdhaber left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for taking this from a half-baked idea to reality @stefanv! I think this is in pretty good shape.

The time savings will depend on the module. For instance, changes to _shared will run tests for all modules because every other module uses something from _shared. These test runs will be a few seconds slower than they would be otherwise due to the time taken to compute the dependency graph. On the other hand, changes to modules like graph and registration will only require running tests for those modules, since nothing (see Details below) else imports from them, making test runs very fast. As an example of something in between, this PR makes a change to morphology. This causes tests in nine other modules, each of which relies on morphology somehow, to run. The slowest test run in this PR is less than 3 minutes, whereas the fastest run on macOS I've seen in other open PRs is at least 5 minutes.

Details

It is possible that as-is, this misses some dependencies (e.g. imports within functions). In the worst case scenario, CI passes in a PR but fail after the PR merges and the full suite runs.

This will be rare because four things have to happen:

  • there has to be a bug (or something else that would cause a test to fail),
  • the bug cannot be caught in review or by unit tests in the module $A$ being modfied, which will always run,
  • this script does not detect that another module $B$ depends on $A$, and
  • a test in $B$ must fail due to the change.

In this case, an issue will be opened automatically (with gh-7752), and the PR can be amended or reverted, depending on the difficulty of the fix and severity of the disruption of test failures in main. The hole that allowed the dependency to slip through can be patched, making this sort of disruption even more rare, and beyond identifying a bug, this identifies a hole in unit tests for the feature being modified.

Besides the time savings, this PR makes another improvement by running tests for the modified module(s) first. Regardless of the module that is modified, this will dramatically reduce the time from pushing a commit to notification of most failures, which typically should be caught by tests in the module(s) being modified. In the case that tests in the modified module(s) don't fail but a test in another module does, the failure message provides a valuable reminder to look for holes in the unit test of the modified code. The project probably does not rely on unit tests of other projects for coverage; likewise, module $A$ should not rely on unit tests of module $B$ for coverage.

Thanks in advance to skimage maintainers for their feedback!

echo "!!! Tests failed. This is **unexpected**, since the"
echo "!!! modified modules tests above passed."
echo "!!! You should find out what went wrong, and then update"
echo "!!! those modified module test suites to be more complete."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's also possible that a feature or test in the unmodified module was incorrectly relying on a certain behavior of the function. For instance, a PR may fix a bug, but an outside test may have been relying on the faulty behavior, causing it to fail when the bug is fixed. Probably should acknowledge this possibility that it's not always the fault of the modified function.

mods = _pkg_modules()

n = len(mods)
A = np.zeros((n, n), dtype=bool)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A dict[tuple[str, str], bool] with the keys being module names might be a bit more "self-describing" and easier to debug?

But probably not worth the refactor. :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, but we use NumPy functionality in some places, so this would be harder.

return A


def dependency_toml():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think I'm missing why this is named toml which is also used elsewhere? I don't see it serialized to a TOML file anywhere. Probably a left-over from refactoring?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This added a TOML file compatible with SciPy's checker, in case that became useful.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yeah, I think the idea was that something like https://github.com/scipy/scipy/blob/main/tach.toml could be substituted in in the future.

@lagru lagru left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Okay, the idea looks simple enough. Except for a few nitpicks I have only one real comment left.

Just so I understand correctly, this still needs to update other workflows so that only this one is run for PRs right?

Comment on lines -22 to -29
# Ensure that a wheel builder finishes even if another fails
fail-fast: false
matrix:
python-version: ["3.11", "3.12", "3.13"]
OPTIONAL_DEPS: [1]
OPTIONS_NAME: ["default"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Guessing this is cleanup. 👍

@@ -0,0 +1,79 @@
name: Run tests identified via dependency analysis

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nitpick, but I think having shorter names here helps the UX in quite a few places. E.g. the "Successful in 8m" isn't truncated in the PR dialog showing the CI checks, etc.

Suggested change
name: Run tests identified via dependency analysis
name: Test only changed

Comment thread .spin/cmds.py
['git', 'diff', 'main', '--stat', '--name-only'], output=False, echo=False
)
if p.returncode != 0:
raise (click.ClickException('Could not git-diff against main'))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nitpick: What's with the enclosing parenthesis here? Weird that ruff didn't remove these since they seem unnecessary?

Suggested change
raise (click.ClickException('Could not git-diff against main'))
raise click.ClickException('Could not git-diff against main')

Comment thread .spin/cmds.py
pkg_mods = module_dependencies._pkg_modules()

p = spin.util.run(
['git', 'diff', 'main', '--stat', '--name-only'], output=False, echo=False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it possible that this returns more files than necessary? Running this locally on the current version of the branch, it looks like this command also includes changes from commits on main that are not in HEAD too. E.g. skimage/exposure/exposure.py wasn't modified by this PR but is listed.

Details
git diff main --stat --name-only
.github/MAIN_FAIL_TEMPLATE.md
.github/workflows/emscripten.yaml
.github/workflows/test-autodepends.yaml
.github/workflows/test-docs.yaml
.github/workflows/test-linux.yaml
.github/workflows/test-macos.yaml
.github/workflows/test-nightlies-on-main.yaml
.github/workflows/test-windows.yaml
.github/workflows/wheels-recipe.yaml
.spin/cmds.py
CONTRIBUTING.rst
INSTALL.rst
TODO.txt
benchmarks/benchmark_restoration.py
doc/examples/applications/plot_coins_segmentation.py
doc/examples/applications/plot_thresholding_guide.py
doc/examples/features_detection/plot_remove_objects.py
doc/examples/filters/plot_deconvolution.py
doc/examples/segmentation/plot_mask_slic.py
doc/examples/segmentation/plot_metrics.py
doc/examples/segmentation/plot_regionprops.py
environment.yml
skimage/_shared/testing.py
skimage/_shared/tests/test_testing.py
skimage/exposure/exposure.py
skimage/feature/_basic_features.py
skimage/measure/_regionprops.py
skimage/measure/tests/test_regionprops.py
skimage/morphology/_flood_fill.py
skimage/morphology/misc.py
skimage/morphology/tests/test_misc.py
skimage/restoration/_cycle_spin.py
skimage/restoration/_rolling_ball.py
skimage/restoration/_rolling_ball_cy.pyx
skimage/restoration/tests/test_denoise.py
skimage/restoration/tests/test_rolling_ball.py
skimage/transform/_geometric.py
skimage/transform/_thin_plate_splines.py
skimage/transform/_warps.py
skimage/transform/tests/test_geometric.py
tools/generate_requirements.py
tools/module_dependencies.py

Do we instead only want changes that are new in HEAD but not in main?

@lagru lagru mentioned this pull request Jun 19, 2025
@stefanv stefanv changed the title Test only changed modules (and those that depend on them?) CI: Test only changed modules and those that depend on them Aug 20, 2025
Comment thread .spin/cmds.py
to_test = to_test | importers_of_modified

pytest_args = kwargs.get('pytest_args', ())
if "--pyargs" in pytest_args:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The changes here now have somewhat major conflict with the recently introduced implementation for the --doctest option (#7786). Both override --pyargs.

@lagru lagru Aug 27, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I attempted a merge in a304648. But will likely need some more love. The logic seems a bit too complicated and I wouldn't be surprised if there is unexpected / confusing behavior when combining --test-modified with --doctest.

@stefanv

stefanv commented Mar 17, 2026

Copy link
Copy Markdown
Member Author

FWIW easier alternative would be to just run test suite for module X that modified function resides in, but that requires that the test suite is "complete".

@github-actions github-actions Bot removed the 😴 Dormant No recent activity label Mar 18, 2026
@blink1073

Copy link
Copy Markdown
Contributor

FWIW easier alternative would be to just run test suite for module X that modified function resides in, but that requires that the test suite is "complete".

IMO this is actually better because it introduces less complexity, and gives us a way to discover which test suites are "incomplete". I'd be more hesitant if we didn't have the auto-issue-creation steps on failure.

@stefanv

stefanv commented Mar 19, 2026

Copy link
Copy Markdown
Member Author

I'm happy to go that direction too, @blink1073. This was a really fun and interesting experiment to do with Matt, but the ultimate goal remains simply to vastly reduce CI runtime.

@mdhaber

mdhaber commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

That sounds fine. The original was a balance between speed and avoiding red CI workflows in main. But if a project is confident enough in their intra-module tests to skip the stepping stone, great!

@stefanv

stefanv commented Apr 21, 2026

Copy link
Copy Markdown
Member Author

This work is being continued in #8103

@stefanv stefanv closed this Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖 type: Infrastructure CI, packaging, tools and automation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants