Skip to content
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

Move index dependent calculation to megacomplexes for speed-up #1175

Merged
merged 11 commits into from
Nov 20, 2022

Conversation

joernweissenborn
Copy link
Member

@joernweissenborn joernweissenborn commented Nov 11, 2022

This PR changes the way index dependent matrices are calculated. Instead of looping in the engine, the full matrix is now calculated by the megacomplex. This should speed up index dependent calculations.

It also fixed a bug in dampened oscillation, the matrix was initialized with ones instead zeros.

Note to reviewers: I needed to touch all megacomplexes. They all need (further) brush up, that will be done in follow-up PR.

Change summary

  • Change1
  • Change2

Checklist

  • ✔️ Passing the tests (mandatory for all PR's)
  • 🚧 Added changes to changelog (mandatory for all PR's)

@github-actions
Copy link
Contributor

Binder 👈 Launch a binder notebook on branch joernweissenborn/pyglotaran/refactor/index_dependency

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Nov 11, 2022

Sourcery Code Quality Report

❌  Merging this PR will decrease code quality in the affected files by 1.21%.

Quality metrics Before After Change
Complexity 4.67 ⭐ 5.24 ⭐ 0.57 👎
Method Length 77.02 🙂 80.96 🙂 3.94 👎
Working memory 9.32 🙂 9.44 🙂 0.12 👎
Quality 63.02% 🙂 61.81% 🙂 -1.21% 👎
Other metrics Before After Change
Lines 4042 4020 -22
Changed files Quality Before Quality After Quality Change
benchmark/pytest/analysis/test_optimization_group.py 81.17% ⭐ 81.27% ⭐ 0.10% 👍
glotaran/builtin/megacomplexes/baseline/baseline_megacomplex.py 87.42% ⭐ 85.47% ⭐ -1.95% 👎
glotaran/builtin/megacomplexes/baseline/test/test_baseline_megacomplex.py 73.12% 🙂 73.25% 🙂 0.13% 👍
glotaran/builtin/megacomplexes/clp_guide/clp_guide_megacomplex.py 89.32% ⭐ 87.68% ⭐ -1.64% 👎
glotaran/builtin/megacomplexes/coherent_artifact/coherent_artifact_megacomplex.py 67.82% 🙂 61.53% 🙂 -6.29% 👎
glotaran/builtin/megacomplexes/coherent_artifact/test/test_coherent_artifact.py 46.35% 😞 44.35% 😞 -2.00% 👎
glotaran/builtin/megacomplexes/damped_oscillation/damped_oscillation_megacomplex.py 52.12% 🙂 52.02% 🙂 -0.10% 👎
glotaran/builtin/megacomplexes/decay/decay_megacomplex.py 86.06% ⭐ 85.48% ⭐ -0.58% 👎
glotaran/builtin/megacomplexes/decay/decay_parallel_megacomplex.py 90.19% ⭐ 89.75% ⭐ -0.44% 👎
glotaran/builtin/megacomplexes/decay/decay_sequential_megacomplex.py 90.21% ⭐ 89.54% ⭐ -0.67% 👎
glotaran/builtin/megacomplexes/decay/irf.py 57.01% 🙂 56.83% 🙂 -0.18% 👎
glotaran/builtin/megacomplexes/decay/util.py 54.03% 🙂 54.68% 🙂 0.65% 👍
glotaran/builtin/megacomplexes/decay/test/test_decay_megacomplex.py 55.92% 🙂 55.92% 🙂 0.00%
glotaran/builtin/megacomplexes/spectral/spectral_megacomplex.py 63.33% 🙂 61.23% 🙂 -2.10% 👎
glotaran/builtin/megacomplexes/spectral/test/test_spectral_model.py 54.37% 🙂 54.37% 🙂 0.00%
glotaran/model/init.py % % %
glotaran/model/clp_constraint.py 91.43% ⭐ 91.43% ⭐ 0.00%
glotaran/model/dataset_model.py 79.23% ⭐ 78.36% ⭐ -0.87% 👎
glotaran/model/megacomplex.py 89.74% ⭐ 89.09% ⭐ -0.65% 👎
glotaran/optimization/estimation_provider.py 63.68% 🙂 65.14% 🙂 1.46% 👍
glotaran/optimization/matrix_provider.py 66.24% 🙂 63.46% 🙂 -2.78% 👎
glotaran/optimization/test/models.py 79.55% ⭐ 73.41% 🙂 -6.14% 👎
glotaran/optimization/test/test_constraints.py 65.91% 🙂 65.98% 🙂 0.07% 👍
glotaran/optimization/test/test_optimization.py 38.97% 😞 37.22% 😞 -1.75% 👎
glotaran/optimization/test/test_relations.py 62.37% 🙂 62.43% 🙂 0.06% 👍
glotaran/simulation/simulation.py 57.65% 🙂 60.61% 🙂 2.96% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
glotaran/optimization/test/test_optimization.py test_optimization 21 😞 621 ⛔ 15 😞 23.40% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
glotaran/builtin/megacomplexes/decay/irf.py IrfMultiGaussian.parameter 14 🙂 235 ⛔ 13 😞 37.66% 😞 Try splitting into smaller methods. Extract out complex expressions
glotaran/builtin/megacomplexes/decay/irf.py IrfSpectralMultiGaussian.parameter 14 🙂 165 😞 17 ⛔ 38.19% 😞 Try splitting into smaller methods. Extract out complex expressions
glotaran/builtin/megacomplexes/damped_oscillation/damped_oscillation_megacomplex.py DampedOscillationMegacomplex.finalize_data 6 ⭐ 377 ⛔ 14 😞 38.84% 😞 Try splitting into smaller methods. Extract out complex expressions
glotaran/optimization/matrix_provider.py MatrixProvider.combine_megacomplex_matrices 19 😞 203 😞 11 😞 39.11% 😞 Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

Copy link
Member

@jsnel jsnel left a comment

Choose a reason for hiding this comment

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

  1. examples are not running on CI,
    doas example is broken
    locally the coherent artifact unit test is also broken (but can't see that in the CI yet)
    (can't start proper review until that's fixed)

FAILED glotaran/builtin/megacomplexes/coherent_artifact/test/test_coherent_artifact.py::test_coherent_artifact[none] - assert (34, 4) == (3, 34, 4)

>       assert matrix.matrix.shape == (spectral.size, time.size, 4)
E       assert (34, 4) == (3, 34, 4)
E         At index 0 diff: 34 != 3
E         Right contains one more item: 4
E         Use -v to get more diff
  1. my terminal is spammed with deprecation warnings from numba:
numba\core\ir_utils.py:2147: NumbaPendingDeprecationWarning: 
Encountered the use of a type that is scheduled for deprecation: type 'reflected list' found for argument 'centers' of function '_calculate_coherent_artifact_matrix'.

For more information visit https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-reflection-for-list-and-set-types

File "..\pyglotaran\glotaran\builtin\megacomplexes\coherent_artifact\coherent_artifact_megacomplex.py", line 104:
@nb.jit(nopython=True, parallel=True)
def _calculate_coherent_artifact_matrix(
^

  warnings.warn(NumbaPendingDeprecationWarning(msg, loc=loc))
numba\core\ir_utils.py:2147: NumbaPendingDeprecationWarning: 
Encountered the use of a type that is scheduled for deprecation: type 'reflected list' found for argument 'widths' of function '_calculate_coherent_artifact_matrix'.

For more information visit https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-reflection-for-list-and-set-types

File "..\pyglotaran\glotaran\builtin\megacomplexes\coherent_artifact\coherent_artifact_megacomplex.py", line 104:
@nb.jit(nopython=True, parallel=True)
def _calculate_coherent_artifact_matrix(
^

  warnings.warn(NumbaPendingDeprecationWarning(msg, loc=loc))
numba\core\ir_utils.py:2147: NumbaPendingDeprecationWarning: 
Encountered the use of a type that is scheduled for deprecation: type 'reflected list' found for argument 'centers' of function '__numba_parfor_gufunc_0x1b19cd827a0'.

For more information visit https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-reflection-for-list-and-set-types

File "<string>", line 1:
<source missing, REPL/exec in use?>

  warnings.warn(NumbaPendingDeprecationWarning(msg, loc=loc))
numba\core\ir_utils.py:2147: NumbaPendingDeprecationWarning:
Encountered the use of a type that is scheduled for deprecation: type 'reflected list' found for argument 'widths' of function '__numba_parfor_gufunc_0x1b19cd827a0'.
For more information visit https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-reflection-for-list-and-set-types

File "<string>", line 1:
<source missing, REPL/exec in use?>

@jsnel jsnel force-pushed the refactor/index_dependency branch from 6a99cf6 to 49daf7d Compare November 12, 2022 21:34
@jsnel
Copy link
Member

jsnel commented Nov 12, 2022

I just benchmarked one of the 'heaviest' example we have, pyglotaran_examples\test\simultaneous_analysis_6d_disp\sim_analysis_script_6d_disp.py and the results are impressive!

main: 32.1s
this PR: 18.2s

Nice @joernweissenborn , I think @ism200 will happy to learn about this!

@codecov
Copy link

codecov bot commented Nov 12, 2022

Codecov Report

Base: 87.7% // Head: 87.6% // Decreases project coverage by -0.1% ⚠️

Coverage data is based on head (d98dae1) compared to base (7fecbc1).
Patch coverage: 78.0% of modified lines in pull request are covered.

Additional details and impacted files
@@           Coverage Diff           @@
##            main   #1175     +/-   ##
=======================================
- Coverage   87.7%   87.6%   -0.2%     
=======================================
  Files        103     104      +1     
  Lines       4906    4946     +40     
  Branches     809     819     +10     
=======================================
+ Hits        4306    4335     +29     
- Misses       486     493      +7     
- Partials     114     118      +4     
Impacted Files Coverage Δ
...tin/megacomplexes/baseline/baseline_megacomplex.py 87.5% <ø> (+4.1%) ⬆️
...n/megacomplexes/clp_guide/clp_guide_megacomplex.py 100.0% <ø> (ø)
...tin/megacomplexes/spectral/spectral_megacomplex.py 86.0% <ø> (-0.6%) ⬇️
glotaran/model/__init__.py 100.0% <ø> (ø)
glotaran/model/dataset_model.py 95.0% <ø> (+2.1%) ⬆️
glotaran/model/megacomplex.py 100.0% <ø> (ø)
...n/megacomplexes/decay/decay_matrix_gaussian_irf.py 42.8% <42.8%> (ø)
glotaran/builtin/megacomplexes/decay/irf.py 86.3% <50.0%> (ø)
glotaran/simulation/simulation.py 83.3% <50.0%> (ø)
...mped_oscillation/damped_oscillation_megacomplex.py 81.0% <68.7%> (-1.7%) ⬇️
... and 8 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@s-weigand s-weigand requested a review from a team as a code owner November 12, 2022 22:17
@s-weigand
Copy link
Member

Even so b7d0eeb didn't fix the strange CI issue IMHO we should keep it for consistency.

@jsnel jsnel force-pushed the refactor/index_dependency branch from b7d0eeb to 818f807 Compare November 14, 2022 05:42
@github-actions
Copy link
Contributor

github-actions bot commented Nov 14, 2022

Benchmark is done. Checkout the benchmark result page.
Benchmark differences below 5% might be due to CI noise.

Benchmark diff v0.6.0 vs. main

Parametrized benchmark signatures:

BenchmarkOptimize.time_optimize(index_dependent, grouped, weight)

All benchmarks:

       before           after         ratio
     [6c3c390e]       [7fecbc11]
     <v0.6.0>                   
!      49.1±0.2ms           failed      n/a  BenchmarkOptimize.time_optimize(False, False, False)
!      52.4±0.4ms           failed      n/a  BenchmarkOptimize.time_optimize(False, False, True)
!      49.1±0.4ms           failed      n/a  BenchmarkOptimize.time_optimize(False, True, False)
!       59.3±20ms           failed      n/a  BenchmarkOptimize.time_optimize(False, True, True)
!      60.2±0.8ms           failed      n/a  BenchmarkOptimize.time_optimize(True, False, False)
!       81.5±30ms           failed      n/a  BenchmarkOptimize.time_optimize(True, False, True)
!        62.5±1ms           failed      n/a  BenchmarkOptimize.time_optimize(True, True, False)
!       72.2±20ms           failed      n/a  BenchmarkOptimize.time_optimize(True, True, True)
             202M             211M     1.04  IntegrationTwoDatasets.peakmem_optimize
-      1.78±0.06s       1.22±0.04s     0.68  IntegrationTwoDatasets.time_optimize

Benchmark diff main vs. PR

Parametrized benchmark signatures:

BenchmarkOptimize.time_optimize(index_dependent, grouped, weight)

All benchmarks:

       before           after         ratio
     [7fecbc11]       [d98dae10]
           failed           failed      n/a  BenchmarkOptimize.time_optimize(False, False, False)
           failed           failed      n/a  BenchmarkOptimize.time_optimize(False, False, True)
           failed           failed      n/a  BenchmarkOptimize.time_optimize(False, True, False)
           failed           failed      n/a  BenchmarkOptimize.time_optimize(False, True, True)
           failed           failed      n/a  BenchmarkOptimize.time_optimize(True, False, False)
           failed           failed      n/a  BenchmarkOptimize.time_optimize(True, False, True)
           failed           failed      n/a  BenchmarkOptimize.time_optimize(True, True, False)
           failed           failed      n/a  BenchmarkOptimize.time_optimize(True, True, True)
             211M             205M     0.97  IntegrationTwoDatasets.peakmem_optimize
       1.22±0.04s       1.11±0.03s     0.91  IntegrationTwoDatasets.time_optimize

@jsnel jsnel changed the title Calculate indexdependency within megacomplexes Move index dependent calculation to megacomplexes for speed-up Nov 15, 2022
joernweissenborn and others added 7 commits November 20, 2022 16:33
Adapted to changed Megacomplex API
Fix the issue:
`TypeError: unsupported operand type(s) for -: 'list' and 'int'`
This causes issues on Mac OS X. Also the fix results in a 2x speedup on Windows in some cases.

Also know as fix for "Terminating: Nested parallel kernel launch detected, the workqueue threading layer does not supported nested parallelism. Try the TBB threading layer."

Ref insightful comment on GitHub: lmcinnes/umap#665 (comment)
@s-weigand s-weigand force-pushed the refactor/index_dependency branch from d4a78e5 to 26b0101 Compare November 20, 2022 15:53
s-weigand and others added 3 commits November 20, 2022 17:43
Was np.ones, was changed to np.zeros, now changes to np.ones again.

It should be document why it should be like that!
Change added:
- ♻️ Move index dependent calculation to megacomplexes for speed-up (glotaran#1175)

Not that this results in a significant speedup in simple cases with dispersion and/or multiple datasets, up to 4x. For more complex cases there is little to no speedup.
@jsnel jsnel self-requested a review November 20, 2022 20:41
jsnel
jsnel previously approved these changes Nov 20, 2022
Copy link
Member

@jsnel jsnel left a comment

Choose a reason for hiding this comment

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

Reviewed and tested ok.

Address some deprecation warnings shown when running the doas example.

2x NumbaPendingDeprecationWarning:
- Encountered the use of a type that is scheduled for deprecation: type 'reflected list' found for argument 'centers' of function '_calculate_coherent_artifact_matrix'.
- Encountered the use of a type that is scheduled for deprecation: type 'reflected list' found for argument 'widths' of function '_calculate_coherent_artifact_matrix'.

For more information visit https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-reflection-for-list-and-set-types
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 4 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@jsnel jsnel merged commit 86e0190 into glotaran:main Nov 20, 2022
@jsnel jsnel mentioned this pull request Nov 28, 2022
3 tasks
jsnel added a commit that referenced this pull request Nov 28, 2022
* 🩹Fix matrix provider
This PR fixes an issue with grouping inadvertently introduced in PR #1175

* 📚 Added change to changelog
Since this fixed a bug introduced within the 0.7.0 development cycle (so after 0.6.0 but before 0.7.0) the change is included in a 'hidden' manner (not rendered unless you look at the source code of the MD file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants