From 909efb4ba9a24b6a62aae2db8536f68dc4c35312 Mon Sep 17 00:00:00 2001 From: Dezhen Lu Date: Fri, 20 Nov 2026 07:52:54 +0100 Subject: [PATCH] Import time in _setup_trt_for_ablation so the TRT paths run _setup_trt_for_ablation called _time.perf_counter() without importing time, so ONNX_WORKFLOW=2 and 3 in the decoder ablation hit a NameError, which the except blocks turned into a silent PyTorch fallback. The mock TensorRT tests did not catch it: the mock Logger took no arguments, so they also fell back. Let the mock Logger take the severity, count execute_v2 calls, and check that the engine ran. Refs #106 Signed-off-by: Dezhen Lu --- code/evaluation/failure_analysis.py | 2 ++ code/tests/test_failure_analysis.py | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/code/evaluation/failure_analysis.py b/code/evaluation/failure_analysis.py index ee63cf7..f1bf305 100644 --- a/code/evaluation/failure_analysis.py +++ b/code/evaluation/failure_analysis.py @@ -698,6 +698,8 @@ def _setup_trt_for_ablation(model, cfg, dist, device, basis, D, half, stim_dets) Returns ``(trt_context, onnx_workflow)`` where *trt_context* is either a ``(execution_context, engine)`` pair or ``None`` (PyTorch fallback). """ + import time as _time + trt_context = None onnx_workflow = OnnxWorkflow.TORCH_ONLY try: diff --git a/code/tests/test_failure_analysis.py b/code/tests/test_failure_analysis.py index fcb40e6..3f47027 100644 --- a/code/tests/test_failure_analysis.py +++ b/code/tests/test_failure_analysis.py @@ -992,7 +992,8 @@ def get_tensor_shape(self, name): return (B_holder[0], 1 + num_detectors) def execute_v2(self, bindings): - pass # output tensor remains zeroed — valid binary values + # Output tensor remains zeroed — valid binary values. + mock_trt.execute_v2_calls += 1 @property def _engine(self): @@ -1014,6 +1015,9 @@ def deserialize_cuda_engine(self, data): class _Logger: WARNING = 1 + def __init__(self, severity=None): + pass + class _BuilderFlag: FP16 = 0 @@ -1060,6 +1064,7 @@ def build_serialized_network(self, network, config): return b"" mock_trt = types.ModuleType("tensorrt") + mock_trt.execute_v2_calls = 0 mock_trt.Logger = _Logger mock_trt.Runtime = lambda logger: _Runtime() mock_trt.Builder = lambda logger: _Builder() @@ -1384,8 +1389,16 @@ def _run_once(cls, basis="X"): _patch_tensor_to_for_mock_cuda(): mf.create_datapipe_inference.return_value = real_ds result = decoder_ablation_study(_ZeroModel(), mock_device, _DummyDist(), cfg) + cls._execute_v2_calls = getattr(cls, "_execute_v2_calls", {}) + cls._execute_v2_calls[basis] = mock_trt.execute_v2_calls return result + def test_trt_path_runs_the_engine(self): + # A failed engine load falls back to PyTorch and still returns a + # well-formed result, so check that the TRT context was used. + self.assertGreater(self._execute_v2_calls["X"], 0) + self.assertGreater(self._execute_v2_calls["Z"], 0) + def test_trt_path_returns_correct_sample_count(self): self.assertEqual(self._result_x["total_samples"], self._N) @@ -1473,8 +1486,14 @@ def _fake_onnx_export(module, *args, **kwargs): _patch_tensor_to_for_mock_cuda(): mf.create_datapipe_inference.return_value = real_ds result = decoder_ablation_study(_ZeroModel(), mock_device, _DummyDist(), cfg) + cls._execute_v2_calls = mock_trt.execute_v2_calls return result + def test_export_and_build_runs_the_engine(self): + # A failed engine build falls back to PyTorch and still returns a + # well-formed result, so check that the TRT context was used. + self.assertGreater(self._execute_v2_calls, 0) + def test_export_and_build_returns_correct_sample_count(self): self.assertEqual(self._result["total_samples"], self._N)