Option to use proper normalization for QS objectives - #2304
Conversation
Memory benchmark result| Test Name | %Δ | Master (MB) | PR (MB) | Δ (MB) | Time PR (s) | Time Master (s) |
| -------------------------------------- | ------------ | ------------------ | ------------------ | ------------ | ------------------ | ------------------ |
test_objective_jac_w7x | -0.16 % | 4.244e+03 | 4.237e+03 | -6.91 | 32.28 | 28.45 |
test_proximal_jac_w7x_with_eq_update | -0.62 % | 6.851e+03 | 6.809e+03 | -42.39 | 157.68 | 152.10 |
test_proximal_freeb_jac | -0.07 % | 1.355e+04 | 1.354e+04 | -9.63 | 82.53 | 80.79 |
test_proximal_freeb_jac_blocked | -0.45 % | 7.904e+03 | 7.869e+03 | -35.26 | 69.71 | 69.13 |
test_proximal_freeb_jac_batched | -0.21 % | 7.894e+03 | 7.877e+03 | -16.41 | 69.42 | 68.23 |
test_proximal_jac_ripple | -1.24 % | 3.800e+03 | 3.753e+03 | -47.14 | 53.98 | 53.18 |
test_proximal_jac_ripple_bounce1d | 0.74 % | 3.767e+02 | 3.795e+02 | 2.80 | 3.74 | 3.45 |
test_eq_solve | -0.14 % | 1.831e+03 | 1.829e+03 | -2.51 | 53.44 | 52.29 |
test_objective_quadratic_flux_jac | 0.58 % | 1.880e+03 | 1.890e+03 | 10.86 | 35.04 | 34.70 |For the memory plots, go to the summary of |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2304 +/- ##
=======================================
Coverage 94.33% 94.34%
=======================================
Files 101 101
Lines 29055 29080 +25
=======================================
+ Hits 27410 27436 +26
+ Misses 1645 1644 -1
🚀 New features to boost your workflow:
|
| benchmark_name | dt(%) | dt(s) | t_new(s) | t_old(s) |
| -------------------------------------- | ---------------------- | ---------------------- | ---------------------- | ---------------------- |
test_build_transform_fft_midres | +3.86 +/- 3.76 | +3.48e-02 +/- 3.39e-02 | 9.36e-01 +/- 2.4e-02 | 9.01e-01 +/- 2.4e-02 |
test_build_transform_fft_highres | +2.02 +/- 2.30 | +2.41e-02 +/- 2.74e-02 | 1.22e+00 +/- 5.6e-03 | 1.19e+00 +/- 2.7e-02 |
test_equilibrium_init_medres | +2.31 +/- 3.71 | +1.63e-01 +/- 2.62e-01 | 7.21e+00 +/- 1.6e-01 | 7.05e+00 +/- 2.1e-01 |
test_objective_compile_dshape_current | +2.57 +/- 2.04 | +1.06e-01 +/- 8.42e-02 | 4.23e+00 +/- 8.0e-02 | 4.12e+00 +/- 2.7e-02 |
test_objective_compute_dshape_current | -0.75 +/- 18.33 | -5.40e-06 +/- 1.32e-04 | 7.14e-04 +/- 1.1e-04 | 7.19e-04 +/- 7.1e-05 |
test_objective_jac_dshape_current | +8.40 +/- 24.58 | +1.92e-03 +/- 5.62e-03 | 2.48e-02 +/- 4.5e-03 | 2.29e-02 +/- 3.4e-03 |
test_perturb_2 | -0.19 +/- 3.75 | -3.01e-02 +/- 5.95e-01 | 1.58e+01 +/- 4.7e-01 | 1.59e+01 +/- 3.7e-01 |
test_proximal_jac_atf_with_eq_update | -0.49 +/- 1.36 | -5.84e-02 +/- 1.64e-01 | 1.20e+01 +/- 7.3e-02 | 1.20e+01 +/- 1.5e-01 |
test_proximal_freeb_jac | +1.11 +/- 3.99 | +5.28e-02 +/- 1.89e-01 | 4.80e+00 +/- 1.8e-01 | 4.74e+00 +/- 6.0e-02 |
test_solve_fixed_iter_compiled | -1.11 +/- 2.26 | -6.62e-02 +/- 1.34e-01 | 5.89e+00 +/- 1.1e-01 | 5.96e+00 +/- 8.2e-02 |
test_objective_grad_ripple | +1.19 +/- 3.10 | +1.01e-02 +/- 2.65e-02 | 8.64e-01 +/- 1.9e-02 | 8.54e-01 +/- 1.8e-02 |
test_objective_quadratic_flux_jac | -2.21 +/- 1.82 | -4.98e-02 +/- 4.11e-02 | 2.20e+00 +/- 1.9e-02 | 2.25e+00 +/- 3.6e-02 |
test_build_transform_fft_lowres | +2.52 +/- 3.87 | +2.11e-02 +/- 3.24e-02 | 8.60e-01 +/- 2.1e-02 | 8.39e-01 +/- 2.5e-02 |
test_equilibrium_init_lowres | -0.03 +/- 3.52 | -1.78e-03 +/- 2.25e-01 | 6.40e+00 +/- 1.8e-01 | 6.40e+00 +/- 1.4e-01 |
test_objective_compile_atf | -1.42 +/- 2.12 | -8.79e-02 +/- 1.32e-01 | 6.11e+00 +/- 1.0e-01 | 6.19e+00 +/- 8.0e-02 |
test_objective_compute_atf | -0.46 +/- 8.81 | -9.73e-06 +/- 1.88e-04 | 2.12e-03 +/- 1.3e-04 | 2.13e-03 +/- 1.4e-04 |
test_objective_jac_atf | +0.58 +/- 4.15 | +8.95e-03 +/- 6.43e-02 | 1.56e+00 +/- 4.9e-02 | 1.55e+00 +/- 4.2e-02 |
test_perturb_1 | +0.69 +/- 2.25 | +8.17e-02 +/- 2.67e-01 | 1.20e+01 +/- 2.5e-01 | 1.19e+01 +/- 1.0e-01 |
test_proximal_jac_atf | -0.36 +/- 1.36 | -1.87e-02 +/- 7.04e-02 | 5.17e+00 +/- 5.5e-02 | 5.18e+00 +/- 4.3e-02 |
test_proximal_freeb_compute | -1.26 +/- 2.81 | -2.11e-03 +/- 4.71e-03 | 1.65e-01 +/- 2.7e-03 | 1.67e-01 +/- 3.8e-03 |
test_solve_fixed_iter | +0.47 +/- 2.71 | +1.13e-01 +/- 6.55e-01 | 2.43e+01 +/- 4.1e-01 | 2.42e+01 +/- 5.1e-01 |
test_LinearConstraintProjection_build | +1.26 +/- 4.75 | +8.51e-02 +/- 3.22e-01 | 6.85e+00 +/- 2.5e-01 | 6.77e+00 +/- 2.1e-01 |
test_objective_compute_ripple | +0.81 +/- 4.77 | +1.93e-03 +/- 1.13e-02 | 2.40e-01 +/- 7.0e-03 | 2.38e-01 +/- 8.9e-03 |
test_objective_quadratic_flux_compute | -0.24 +/- 1.55 | -5.21e-05 +/- 3.34e-04 | 2.16e-02 +/- 1.8e-04 | 2.16e-02 +/- 2.8e-04 |Github CI performance can be noisy. When evaluating the benchmarks, developers should take this into account. |
dpanici
left a comment
There was a problem hiding this comment.
I'd prefer to just silently ignore normalize=True when the hatted metric is requested, as technically it IS normalized. Just set the normalization in the build to 1 in those cases
YigitElma
left a comment
There was a problem hiding this comment.
Also add the equations in the docstring
| # the harmonics on each surface are all scaled by the same factor, and the | ||
| # norm of the residuals on each surface should be between 0 and 1 | ||
| ratio = (f["fb_hat"] / f["fb"]).reshape((rho.size, -1)) | ||
| np.testing.assert_allclose(ratio / ratio[:, :1], 1, rtol=1e-12) |
There was a problem hiding this comment.
That was testing if the normalization is constant per surface but yeah it is not necessary and a weird way to test. I changed it to just checking between 0 and 1
During stage-1 optimization, if the volume or the average magnetic field is not constrained, the strength of the magnetic field can be reduced by inflating the equilibrium (while still having the same Psi). The actual definition of these objectives (for example, in Landreman&Paul 2022) already includes these normalizations. Our precomputed normalizations cannot prevent these kinds of "cheats" in some cases.
This PR adds the option to choose which equation to use by the new
modeargument. The default keeps the old behavior.