diff --git a/openpilot/starpilot/models/laboratory_runner.py b/openpilot/starpilot/models/laboratory_runner.py index a706747978..d68882441b 100644 --- a/openpilot/starpilot/models/laboratory_runner.py +++ b/openpilot/starpilot/models/laboratory_runner.py @@ -115,6 +115,15 @@ class LaboratoryPair: raise +def _coherent_camera_output(buffer): + try: + from jetlink.openpilot.warp import make_coherent + except ImportError as error: + raise RuntimeError("Coherent camera allocator is unavailable") from error + buffer.ensure_allocated() + make_coherent(buffer) + + def load_laboratory_pair(cam_w: int, cam_h: int, config: dict, *, root: Path = ROOT) -> LaboratoryPair: from openpilot.starpilot.models.runtime_bridge.runtime import frozen_laboratory_request validate_configuration(config, root) @@ -125,7 +134,10 @@ def load_laboratory_pair(cam_w: int, cam_h: int, config: dict, *, root: Path = R contracts = [] for key in ('lateralModel', 'longitudinalModel'): role = request['roles'][key] + source = role['source_contract'] + coherent = (_coherent_camera_output if source.get('artifact_format') == LEGACY_ABI and + source.get('_source_contract') is None else None) loaded.append(load_verified_model(cam_w, cam_h, Path(role['path']), role['version'], role['chestnut'], - role['model_id'], role['sha256'], root=root)) + role['model_id'], role['sha256'], root=root, coherent_warp=coherent)) contracts.append(role['source_contract']) return LaboratoryPair(loaded[0][0], loaded[1][0], loaded[0][1], loaded[1][1], tuple(contracts)) diff --git a/openpilot/starpilot/models/runner.py b/openpilot/starpilot/models/runner.py index 0f71020abc..29777a27d3 100644 --- a/openpilot/starpilot/models/runner.py +++ b/openpilot/starpilot/models/runner.py @@ -208,7 +208,11 @@ class CatalogModelState: def move_warp_output(self, coherent_warp: Callable[[Any], None]) -> None: """On a comma, move this warp's output to memory the CPU reads cached with `coherent_warp` (Jetlink's make_coherent), for the link to send from. Before warmup(), whose first call links the warp to its address.""" - buffer = self.warp_enqueue.captured.ret.uop.base.buffer + output = getattr(getattr(self.warp_enqueue, "captured", None), "ret", None) + if not isinstance(output, Tensor): + self.warp_copied = "warp has no captured Tensor output" + return + buffer = output.uop.base.buffer if not buffer.device.startswith("QCOM"): self.warp_copied = f"a {buffer.device} buffer" return diff --git a/openpilot/starpilot/models/tests/test_laboratory_coherent.py b/openpilot/starpilot/models/tests/test_laboratory_coherent.py new file mode 100644 index 0000000000..e205b4d24c --- /dev/null +++ b/openpilot/starpilot/models/tests/test_laboratory_coherent.py @@ -0,0 +1,121 @@ +import builtins +from copy import deepcopy +from types import SimpleNamespace +import unittest +from unittest.mock import Mock, patch + +import numpy as np +from tinygrad import Tensor + +from openpilot.starpilot.models import laboratory, laboratory_runner +from openpilot.starpilot.models.runner import CatalogModelState, SUPPORTED_VERSIONS +from openpilot.starpilot.models.tests import test_laboratory_runner as load_tests + + +class TestCoherentOutput(unittest.TestCase): + def test_allocation_order_and_kernel_flags(self): + from jetlink.openpilot.warp import COHERENT_WRITEBACK + output = Mock(device='QCOM', nbytes=64) + memory = SimpleNamespace(meta=[SimpleNamespace(flags=COHERENT_WRITEBACK)]) + device = Mock() + events = [] + output.ensure_allocated.side_effect = lambda: events.append('allocated') + device._gpu_alloc.side_effect = lambda *a, **k: (events.append('coherent'), memory)[1] + output.deallocate.side_effect = lambda: events.append('freed') + output.allocate.side_effect = lambda **k: events.append('bound') + with patch('tinygrad.device.Device', {'QCOM': device}): + laboratory_runner._coherent_camera_output(output) + self.assertEqual(events, ['allocated', 'coherent', 'freed', 'bound']) + device._gpu_alloc.assert_called_once_with(64, flags=COHERENT_WRITEBACK) + output.allocate.assert_called_once_with(opaque=memory) + + def test_refused_flags_keep_old_storage(self): + from jetlink.openpilot.warp import COHERENT_WRITEBACK + for flags in (0, COHERENT_WRITEBACK & ~(1 << 31), COHERENT_WRITEBACK & ~(3 << 26)): + with self.subTest(flags=flags): + output = Mock(device='QCOM', nbytes=64) + memory = SimpleNamespace(meta=[SimpleNamespace(flags=flags)]) + device = Mock() + device._gpu_alloc.return_value = memory + with patch('tinygrad.device.Device', {'QCOM': device}), self.assertRaisesRegex(RuntimeError, 'kept back'): + laboratory_runner._coherent_camera_output(output) + device._gpu_free.assert_called_once_with(memory) + output.deallocate.assert_not_called() + output.allocate.assert_not_called() + + def test_missing_optional_allocator_refuses_before_touching_storage(self): + original = builtins.__import__ + def missing(name, *args, **kwargs): + if name == 'jetlink.openpilot.warp': + raise ImportError('missing optional allocator') + return original(name, *args, **kwargs) + output = Mock() + with patch('builtins.__import__', side_effect=missing), self.assertRaisesRegex(RuntimeError, 'unavailable'): + laboratory_runner._coherent_camera_output(output) + output.ensure_allocated.assert_not_called() + + def test_uncaptured_warps_keep_existing_execution(self): + for warp in (lambda: None, SimpleNamespace(captured=None), SimpleNamespace(captured=SimpleNamespace(ret=(1, 2)))): + model = CatalogModelState.__new__(CatalogModelState) + model.warp_enqueue = warp + callback = Mock() + model.move_warp_output(callback) + callback.assert_not_called() + self.assertEqual(model.warp_copied, 'warp has no captured Tensor output') + self.assertIs(model.warp_enqueue, warp) + + def test_non_qcom_tensor_is_unchanged(self): + tensor = Tensor(np.arange(16, dtype=np.uint8), device='NPY').realize() + model = CatalogModelState.__new__(CatalogModelState) + model.warp_enqueue = SimpleNamespace(captured=SimpleNamespace(ret=tensor)) + callback = Mock() + model.move_warp_output(callback) + callback.assert_not_called() + np.testing.assert_array_equal(tensor.numpy(), np.arange(16, dtype=np.uint8)) + self.assertEqual(model.warp_copied, 'a NPY buffer') + + +class TestCoherentFactory(unittest.TestCase): + setUp = load_tests.TestLaboratoryLoad.setUp + factory = load_tests.TestLaboratoryLoad.factory + + def test_all_supported_versions_apply_after_verification_before_warmup(self): + original = laboratory.freeze_request(self.root) + for version in sorted(SUPPORTED_VERSIONS): + with self.subTest(version=version): + request = deepcopy(original) + for role in request['roles'].values(): + role['version'] = version + events = [] + def factory(*args, events=events): + model = self.factory(*args) + model.move_warp_output = Mock(side_effect=lambda callback, events=events: events.append(('coherent', callback))) + model.warmup = Mock(side_effect=lambda events=events: events.append(('warmup', None))) + return model + with patch('openpilot.starpilot.models.runtime_bridge.runtime.frozen_laboratory_request', return_value=request), \ + patch('openpilot.starpilot.models.runner.CatalogModelState', side_effect=factory): + pair = laboratory_runner.load_laboratory_pair(8, 8, self.config, root=self.root) + self.assertEqual(events, [('coherent', laboratory_runner._coherent_camera_output), ('warmup', None)] * 2) + self.assertEqual(pair.lateral_role.behavior_version, version) + self.assertEqual(pair.longitudinal_role.behavior_version, version) + self.assertNotEqual(pair.lateral_role.artifact.sha256, pair.longitudinal_role.artifact.sha256) + + def test_original_dom_sources_retain_original_loading(self): + request = laboratory.freeze_request(self.root) + for role in request['roles'].values(): + role['source_contract']['_source_contract'] = 'dom_v25_protocol5' + role['source_contract']['source_format'] = 'tinygrad_single_v1' + models = [] + def factory(*args): + model = self.factory(*args) + model.move_warp_output = Mock() + models.append(model) + return model + with patch('openpilot.starpilot.models.runtime_bridge.runtime.frozen_laboratory_request', return_value=request), \ + patch('openpilot.starpilot.models.runner.CatalogModelState', side_effect=factory): + pair = laboratory_runner.load_laboratory_pair(8, 8, self.config, root=self.root) + self.assertEqual(len(models), 2) + for model in models: + model.move_warp_output.assert_not_called() + model.warmup.assert_called_once_with() + self.assertEqual(pair.lateral_role.source_format, 'tinygrad_single_v1') diff --git a/prebuilt.json b/prebuilt.json index e82b34bab7..157f700517 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "9a44589fdbc882922a83b4ae80f8932fe0598e10d20ac81043ba7ac4a1afc1e6", "schema_version": 1} +{"protected_inventory_sha256": "8a410639f747e0ea198cf8d185c6c306f8f1419e749c45acd5abf4ec2a262450", "schema_version": 1}