Skip to content

Fix slow optimal q detector test slowdwon

Miron van der Kolk requested to merge fix/637-optimal-q-detector-slowdown into develop

Fixes #637 (closed)

Credits to Pooya for suggesting the change.

For some reason, the test_optimal_q_detector.py tests were getting quite slow, and I pinned down the change by finding that using any numpy version newer than 1.22 would run quite slow.

After some profiling lead to the nested function fun in optimise_HG00_q_scipy in gaussian.py. The final line of this function was doing a matrix-vector multiplication and selecting only the 1st element of the result. I changed it such that it only multiplied the first row of the matrix and the vector together, getting the same result:

e @ kmat.data[0] instead of (kmat.data @ e)[0]

I don't know why this fixed the performance, since it does not explain the difference with the numpy versions. I also stepped through this code and it only a 66x66 size matrix, which should be trivial to multiply?

I can clearly see the difference:

Develop (numpy 2.0)

pytest tests/validation/mode_matching/test_optimal_q_detector.py -> 2m:11s

See also a recent pipeline from develop https://gitlab.com/ifosim/finesse/finesse3/-/jobs/7308514523

============================= slowest 20 durations =============================
15.41s call     tests/benchmarks/test_benchmarks.py::test_benchmark_creation
4.93s call     tests/functional/script/katscript/parse/test_arrays.py::test_array_fuzzing
4.61s call     tests/functional/test_propagate_beam.py::test_astigmatic_propagation_overlaps[True-ITMX.p1-PRM.p2]
4.53s call     tests/benchmarks/test_benchmarks.py::test_coupled_cavity_commands
3.89s call     tests/functional/test_propagate_beam.py::test_composite_abcd[True-x-ITMX.p2.i-PRM.p2]
3.84s call     tests/functional/test_propagate_beam.py::test_astigmatic_propagation_overlaps[True-L0.p1-ITMXlens.p2.o]
3.60s call     tests/validation/mode_matching/test_optimal_q_detector.py::test_optimise_HG00_q_scipy[0.011--1]
3.46s call     tests/functional/script/katscript/parse/test_ordering.py::test_script_order_doesnt_matter
3.43s call     tests/validation/mode_matching/test_optimal_q_detector.py::test_optimise_HG00_q_scipy[0.011-2]
3.41s call     tests/validation/mode_matching/test_optimal_q_detector.py::test_optimise_HG00_q_scipy[0.003--1]
3.39s call     tests/validation/mode_matching/test_optimal_q_detector.py::test_optimise_HG00_q_scipy[0.003-2]
3.37s call     tests/validation/mode_matching/test_optimal_q_detector.py::test_optimal_q_detector[0.011-2]
3.24s call     tests/validation/mode_matching/test_optimal_q_detector.py::test_optimise_HG00_q_scipy[0.003-0]
3.24s call     tests/validation/mode_matching/test_optimal_q_detector.py::test_optimal_q_detector[0.011-0]
3.19s call     tests/validation/mode_matching/test_optimal_q_detector.py::test_optimal_q_detector[0.011--1]
3.19s call     tests/validation/mode_matching/test_optimal_q_detector.py::test_optimise_HG00_q_scipy[0.011-0]
3.17s call     tests/validation/mode_matching/test_optimal_q_detector.py::test_optimal_q_detector[0.003-0]
3.12s call     tests/validation/mode_matching/test_optimal_q_detector.py::test_optimal_q_detector[0.003-2]
3.08s call     tests/benchmarks/test_benchmarks.py::test_lock_actions
3.08s call     tests/functional/script/katscript/parse/test_names.py::test_component_name_fuzzing
= 5323 passed, 5 skipped, 64 xfailed, 23 xpassed, 297 warnings in 323.12s (0:05:23) =

This branch (numpy 2.0)

pytest tests/validation/mode_matching/test_optimal_q_detector.py -> 7s

See also the pipeline from this branch https://gitlab.com/ifosim/finesse/finesse3/-/jobs/7309686668

============================= slowest 20 durations =============================
15.51s call     tests/benchmarks/test_benchmarks.py::test_benchmark_creation
6.32s call     tests/functional/script/katscript/parse/test_arrays.py::test_array_fuzzing
4.60s call     tests/benchmarks/test_benchmarks.py::test_coupled_cavity_commands
3.72s call     tests/functional/test_propagate_beam.py::test_astigmatic_propagation_overlaps[True-ITMX.p1-PRM.p2]
3.18s call     tests/functional/test_propagate_beam.py::test_composite_abcd[True-x-ITMX.p2.i-PRM.p2]
3.12s call     tests/functional/test_propagate_beam.py::test_astigmatic_propagation_overlaps[True-L0.p1-ITMXlens.p2.o]
3.08s call     tests/benchmarks/test_benchmarks.py::test_lock_actions
3.08s call     tests/functional/script/katscript/parse/test_ordering.py::test_script_order_doesnt_matter
2.48s call     tests/functional/test_propagate_beam.py::test_accumulated_gouy_phases[True-x-ITMX.p1-PRM.p2]
2.42s call     tests/functional/script/katscript/parse/test_names.py::test_component_name_fuzzing
2.35s call     tests/functional/test_propagate_beam.py::test_astigmatic_propagation_overlaps[True-PRM.p2.o-ITMX.p1.i]
2.00s call     tests/functional/test_propagate_beam.py::test_accumulated_gouy_phases[True-x-L0.p1-ITMXlens.p2.o]
1.99s call     tests/functional/test_propagate_beam.py::test_composite_abcd[True-x-L0.p1-ITMXlens.p2.o]
1.93s call     tests/functional/components/test_mirror.py::test_round_trip_power_fuzzing[p2-p2.o-p1.o]
1.90s call     tests/functional/test_propagate_beam.py::test_composite_abcd[True-y-ITMX.p2.i-PRM.p2]
1.60s call     tests/validation/hom/test_astigmatism_detector.py::test_astig_detector[scan6]
1.52s call     tests/benchmarks/test_benchmarks.py::test_frequency_dependant_squeezing
1.50s call     tests/functional/test_propagate_beam.py::test_accumulated_gouy_phases[True-x-PRM.p2.o-ITMX.p1.i]
1.48s call     tests/functional/test_propagate_beam.py::test_accumulated_gouy_phases[True-y-ITMX.p1-PRM.p2]
1.48s call     tests/functional/script/katscript/parse/test_expressions.py::test_expression_fuzzing
= 5323 passed, 5 skipped, 63 xfailed, 24 xpassed, 297 warnings in 244.55s (0:04:04) =
Edited by Miron van der Kolk

Merge request reports