STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_accel_is_actually_used.py
Go to the documentation of this file.
1"""Guard against a SILENT BYPASS: production quietly stopping using the accelerator.
2
3Everything else in CI proves the accelerator *works* -- that numba imports, that
4the kernels compile, that their numbers match Python. None of it proves the
5production call sites still *call* them. Delete the `accel.evaluate(...)` line
6from Layer-1's `_eval_candidate` and every other check stays green while the
7optimizer silently runs ~120x slower on the Python path. That was verified by
8doing exactly that: pre-flight, the strict impinging tests, the A/B parity suite
9and the outcome tests all passed with the call disabled.
10
11These tests close that hole by asserting the accelerator is actually reached
12through the real code paths, not called directly by a test.
13"""
14from __future__ import annotations
15
16import copy
17from pathlib import Path
18
19import pytest
20
21ROOT = Path(__file__).resolve().parents[1]
22PA = 101325.0
23
24
25@pytest.fixture(scope="module")
26def cfg():
27 from engine import accel
28 from engine.pipeline.io import load_config
29 if not accel.available():
30 pytest.skip("numba unavailable")
31 return load_config(str(ROOT / "configs/canonical/impinging.yaml"))
32
33
35 """chamber_solver / closure must reach the accelerator, not just be able to.
36
37 runner.evaluate -> chamber_solver._accel_chamber_pc -> accel.chamber_solve,
38 and the Brent residual -> closure.flows -> accel.solve.
39 """
40 from engine import accel
41 from engine.core.runner import PintleEngineRunner
42
43 calls = {"chamber_solve": 0, "solve": 0}
44 for name in calls:
45 real = getattr(accel, name)
46 def counting(*a, _r=real, _n=name, **k):
47 calls[_n] += 1
48 return _r(*a, **k)
49 monkeypatch.setattr(accel, name, counting)
50
51 r = PintleEngineRunner(cfg)
52 res = r.evaluate(4.0e6, 4.0e6, P_ambient=PA, silent=True)
53 assert res is not None and res["Pc"] > 0
54
55 assert sum(calls.values()) > 0, (
56 "runner.evaluate completed without reaching the accelerator at all -- the "
57 f"call sites have been bypassed (counts={calls}). Everything still "
58 "'passes' because Python silently produces the same numbers, just ~120x "
59 "slower."
60 )
61
62
64 """The Layer-1 seam: EVERY candidate must go through accel.evaluate.
65
66 Asserts a RATIO, not merely a non-zero count. A count-only check would pass
67 if one candidate went through the accelerator and the next thousand did not.
68 Measured ratio is exactly 1.000 -- every candidate -- so the 0.9 floor has
69 real margin while still failing hard (ratio 0.0) on a bypass.
70
71 Runs a genuine one-iteration smoke optimization rather than calling the
72 accelerator directly, because the thing under test is the WIRING.
73 """
74 from engine import accel
76 from engine.core.runner import PintleEngineRunner
77
78 n = {"cand": 0, "accel": 0}
79 real_ec, real_ev = L1._eval_candidate, accel.evaluate
80
81 def counting_candidate(x):
82 n["cand"] += 1
83 return real_ec(x)
84
85 def counting_evaluate(*a, **k):
86 n["accel"] += 1
87 return real_ev(*a, **k)
88
89 base = copy.deepcopy(cfg)
90 req = base.design_requirements.model_dump()
91 req["layer1_random_seed"] = 0 # pin the CMA trajectory
92 pcfg = {"mode": "optimizer_controlled",
93 "max_lox_pressure_psi": float(req["max_lox_tank_pressure_psi"]),
94 "max_fuel_pressure_psi": float(req["max_fuel_tank_pressure_psi"])}
95
96 L1._get_num_workers = lambda c: 1 # serial, so the patches are in-process
97 L1._eval_candidate, accel.evaluate = counting_candidate, counting_evaluate
98 try:
99 L1.run_layer1_optimization(
100 copy.deepcopy(base), PintleEngineRunner(copy.deepcopy(base)), req,
101 target_burn_time=float(req.get("target_burn_time", 6.0)),
102 tolerances={"thrust": 0.10, "apogee": 0.15}, pressure_config=pcfg,
103 layer1_smoke=True, layer1_max_iterations=1, layer1_cma_restarts=1)
104 finally:
105 L1._eval_candidate, accel.evaluate = real_ec, real_ev
106
107 assert n["cand"] > 0, "no candidates were evaluated -- the smoke run did nothing"
108 ratio = n["accel"] / n["cand"]
109 assert ratio >= 0.9, (
110 f"only {n['accel']}/{n['cand']} candidates ({ratio:.1%}) went through the "
111 "accelerator -- the Layer-1 inner-loop fast path is bypassed. The optimizer "
112 "still produces correct results on the Python path, which is why no other "
113 "test catches this."
114 )
115
116
118 """The chug seam: stability analysis must reach accel.chug_margin_fast.
119
120 Separate from the others because it is reached through
121 comprehensive_stability_analysis rather than the chamber solve, so a bypass
122 there is invisible to every check above. Chug is ~53x accelerated and is the
123 dominant per-evaluation stability cost, so silently dropping to Python here
124 is a large regression with no wrong answer to give it away.
125 """
126 from engine import accel
127 from engine.core.runner import PintleEngineRunner
128
129 n = {"calls": 0}
130 real = accel.chug_margin_fast
131
132 def counting(*a, **k):
133 n["calls"] += 1
134 return real(*a, **k)
135
136 monkeypatch.setattr(accel, "chug_margin_fast", counting)
137 r = PintleEngineRunner(cfg)
138 res = r.evaluate(4.0e6, 4.0e6, P_ambient=PA, silent=True)
139 assert res is not None
140
141 assert n["calls"] > 0, (
142 "stability analysis ran without reaching accel.chug_margin_fast -- the "
143 "compiled chug sweep is bypassed and the ~53x slower Python sweep is "
144 "running instead, with identical numbers."
145 )
test_runner_evaluate_routes_through_the_accelerator(cfg, monkeypatch)
test_stability_tail_routes_through_the_chug_kernel(cfg, monkeypatch)