Repository navigation
Conversation
5f72999 to
8e3620f
Compare
8e3620f to
d5ab33a
Compare
There was a problem hiding this comment.
See scipy/scipy#22671 for an alternative to this script
There was a problem hiding this comment.
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.
|
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:
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? |
That file came from you, actually ;) 1f758cd |
|
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. |
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. |
| pkg_modules = _pkg_modules() | ||
| pkg_sys_modules = {mod for mod in sys.modules if package in mod} | ||
|
|
||
| return pkg_modules & pkg_sys_modules |
There was a problem hiding this comment.
Assuming _pkg_modules() ends up returning a sorted list, can this just be:
| 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?
Co-authored-by: Matt Haberland <[email protected]>
There was a problem hiding this comment.
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
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." |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
Yes, but we use NumPy functionality in some places, so this would be harder.
| return A | ||
|
|
||
|
|
||
| def dependency_toml(): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
This added a TOML file compatible with SciPy's checker, in case that became useful.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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?
| # 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"] |
| @@ -0,0 +1,79 @@ | |||
| name: Run tests identified via dependency analysis | |||
There was a problem hiding this comment.
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.
| name: Run tests identified via dependency analysis | |
| name: Test only changed |
| ['git', 'diff', 'main', '--stat', '--name-only'], output=False, echo=False | ||
| ) | ||
| if p.returncode != 0: | ||
| raise (click.ClickException('Could not git-diff against main')) |
There was a problem hiding this comment.
Nitpick: What's with the enclosing parenthesis here? Weird that ruff didn't remove these since they seem unnecessary?
| raise (click.ClickException('Could not git-diff against main')) | |
| raise click.ClickException('Could not git-diff against main') |
| pkg_mods = module_dependencies._pkg_modules() | ||
|
|
||
| p = spin.util.run( | ||
| ['git', 'diff', 'main', '--stat', '--name-only'], output=False, echo=False |
There was a problem hiding this comment.
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?
| to_test = to_test | importers_of_modified | ||
|
|
||
| pytest_args = kwargs.get('pytest_args', ()) | ||
| if "--pyargs" in pytest_args: |
There was a problem hiding this comment.
The changes here now have somewhat major conflict with the recently introduced implementation for the --doctest option (#7786). Both override --pyargs.
There was a problem hiding this comment.
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.
|
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. |
|
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. |
|
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! |
|
This work is being continued in #8103 |
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.ioandskimage.segmentation, sinceskimage.coloris imported by those submodules. If I touchskimage._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