|
| 1 | +# test/test_compare_models.py |
| 2 | +import pytest |
| 3 | +from codes.benchmark import bench_fcts |
| 4 | + |
| 5 | + |
| 6 | +@pytest.fixture(autouse=True) |
| 7 | +def record_calls(monkeypatch): |
| 8 | + """ |
| 9 | + Stub out all compare_* and plot_* functions so that calls |
| 10 | + just record their names into a shared list, instead of doing any real work. |
| 11 | + """ |
| 12 | + calls = [] |
| 13 | + names = [ |
| 14 | + "compare_relative_errors", |
| 15 | + "compare_main_losses", |
| 16 | + "compare_dynamic_accuracy", |
| 17 | + "compare_inference_time", |
| 18 | + "compare_interpolation", |
| 19 | + "compare_extrapolation", |
| 20 | + "compare_sparse", |
| 21 | + "plot_all_generalization_errors", |
| 22 | + "compare_batchsize", |
| 23 | + "compare_UQ", |
| 24 | + "tabular_comparison", |
| 25 | + ] |
| 26 | + for name in names: |
| 27 | + monkeypatch.setattr( |
| 28 | + bench_fcts, |
| 29 | + name, |
| 30 | + lambda *args, _n=name, **kw: calls.append(_n), |
| 31 | + ) |
| 32 | + return calls |
| 33 | + |
| 34 | + |
| 35 | +def make_dummy_metrics(): |
| 36 | + """ |
| 37 | + Build a minimal metrics dict that contains the keys |
| 38 | + your compare_models dispatcher will look up. |
| 39 | + Values themselves are never inspected by our stubs. |
| 40 | + """ |
| 41 | + return { |
| 42 | + "M1": { |
| 43 | + "accuracy": {"relative_errors": None}, |
| 44 | + "timesteps": None, |
| 45 | + "n_params": 0, |
| 46 | + # for each enabled branch add a dummy sub-dict: |
| 47 | + "timing": { |
| 48 | + "mean_inference_time_per_run": 1.0, |
| 49 | + "std_inference_time_per_run": 0.1, |
| 50 | + }, |
| 51 | + "gradients": { |
| 52 | + "gradients": None, |
| 53 | + "avg_correlation": 0.0, |
| 54 | + "max_gradient": 0, |
| 55 | + "max_error": 0, |
| 56 | + "max_counts": 0, |
| 57 | + }, |
| 58 | + "interpolation": {"intervals": [1], "model_errors": [0]}, |
| 59 | + "extrapolation": {"cutoffs": [1], "model_errors": [0]}, |
| 60 | + "sparse": {"n_train_samples": [10], "model_errors": [0]}, |
| 61 | + "batch_size": {"batch_sizes": [32], "model_errors": [0]}, |
| 62 | + "UQ": { |
| 63 | + "pred_uncertainty": None, |
| 64 | + "absolute_errors": None, |
| 65 | + "relative_errors": None, |
| 66 | + "axis_max": None, |
| 67 | + "max_counts": None, |
| 68 | + "correlation_metrics": None, |
| 69 | + "weighted_diff": None, |
| 70 | + }, |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize( |
| 76 | + "flags, expected_sequence", |
| 77 | + [ |
| 78 | + # all branches on |
| 79 | + ( |
| 80 | + { |
| 81 | + "losses": True, |
| 82 | + "gradients": True, |
| 83 | + "timing": True, |
| 84 | + "interpolation": {"enabled": True}, |
| 85 | + "extrapolation": {"enabled": True}, |
| 86 | + "sparse": {"enabled": True}, |
| 87 | + "batch_scaling": {"enabled": True}, |
| 88 | + "uncertainty": {"enabled": True}, |
| 89 | + }, |
| 90 | + [ |
| 91 | + "compare_relative_errors", |
| 92 | + "compare_main_losses", |
| 93 | + "compare_dynamic_accuracy", |
| 94 | + "compare_inference_time", |
| 95 | + "compare_interpolation", |
| 96 | + "compare_extrapolation", |
| 97 | + "compare_sparse", |
| 98 | + "plot_all_generalization_errors", # only if int+ext+sparse all enabled |
| 99 | + "compare_batchsize", |
| 100 | + "compare_UQ", |
| 101 | + "tabular_comparison", |
| 102 | + ], |
| 103 | + ), |
| 104 | + # only the mandatory relative-errors + table |
| 105 | + ( |
| 106 | + { |
| 107 | + "losses": False, |
| 108 | + "gradients": False, |
| 109 | + "timing": False, |
| 110 | + "interpolation": {"enabled": False}, |
| 111 | + "extrapolation": {"enabled": False}, |
| 112 | + "sparse": {"enabled": False}, |
| 113 | + "batch_scaling": {"enabled": False}, |
| 114 | + "uncertainty": {"enabled": False}, |
| 115 | + }, |
| 116 | + [ |
| 117 | + "compare_relative_errors", |
| 118 | + "tabular_comparison", |
| 119 | + ], |
| 120 | + ), |
| 121 | + # losses but nothing else |
| 122 | + ( |
| 123 | + { |
| 124 | + "losses": True, |
| 125 | + "gradients": False, |
| 126 | + "timing": False, |
| 127 | + "interpolation": {"enabled": False}, |
| 128 | + "extrapolation": {"enabled": False}, |
| 129 | + "sparse": {"enabled": False}, |
| 130 | + "batch_scaling": {"enabled": False}, |
| 131 | + "uncertainty": {"enabled": False}, |
| 132 | + }, |
| 133 | + [ |
| 134 | + "compare_relative_errors", |
| 135 | + "compare_main_losses", |
| 136 | + "tabular_comparison", |
| 137 | + ], |
| 138 | + ), |
| 139 | + ], |
| 140 | +) |
| 141 | +def test_compare_models_branching(record_calls, flags, expected_sequence): |
| 142 | + cfg = { |
| 143 | + "training_id": "test", |
| 144 | + "devices": ["cpu"], # for compare_main_losses |
| 145 | + "losses": flags["losses"], |
| 146 | + "gradients": flags["gradients"], |
| 147 | + "timing": flags["timing"], |
| 148 | + "interpolation": flags["interpolation"], |
| 149 | + "extrapolation": flags["extrapolation"], |
| 150 | + "sparse": flags["sparse"], |
| 151 | + "batch_scaling": flags["batch_scaling"], |
| 152 | + "uncertainty": flags["uncertainty"], |
| 153 | + } |
| 154 | + metrics = make_dummy_metrics() |
| 155 | + |
| 156 | + bench_fcts.compare_models(metrics, cfg) |
| 157 | + |
| 158 | + assert record_calls == expected_sequence |
0 commit comments