diff --git a/openpilot/starpilot/models/laboratory_replay.py b/openpilot/starpilot/models/laboratory_replay.py index c8fecf86d5..7ca023d628 100644 --- a/openpilot/starpilot/models/laboratory_replay.py +++ b/openpilot/starpilot/models/laboratory_replay.py @@ -71,3 +71,57 @@ def enable_replay(model): setattr(model, name, replay) replays.append(replay) model._laboratory_replays = tuple(replays) + + +def keep_mapped_image_on_qcom(model): + """Collapse the private host staging copies in a verified stateful QCOM policy.""" + from openpilot.starpilot.models.runtime_bridge.runtime import selected_legacy + + if (not selected_legacy() or model.chestnut or not model.onnx_history or model.queue_device != "QCOM" or + model.warp_device != "QCOM" or not model.metadata["model"].get("mapped_image_input")): + return + jit = model.run_policy + if type(jit) is not jit_module._TinyJit or (capture := jit.captured) is None: + return + + from tinygrad.dtype import dtypes + from tinygrad.engine.realize import get_call_arg_uops + from tinygrad.runtime.support.hcq2 import HCQInfo + from tinygrad.uop.ops import Ops + + linear = capture.linear + if len(linear.src) != 4: + return + scalar_copy, stage, upload, submit = (item.without_after for item in linear.src) + if (any(call.op is not Ops.CALL or call.body.op is not Ops.COPY or len(call.src) != 3 + for call in (scalar_copy, stage, upload)) or + submit.op is not Ops.CALL or not isinstance(info := submit.arg.aux, HCQInfo) or info.device != ("QCOM",)): + return + host, warped = stage.src[1:] + mapped, staging_input = upload.src[1:] + if (host.device, warped.device, mapped.device, staging_input.device) != ("CPU", "QCOM", "QCOM", "CPU"): + return + if any(arg.dtype != dtypes.uint8 or arg.shape != warped.shape for arg in (host, warped, mapped, staging_input)): + return + if warped.op is not Ops.PARAM or mapped.op is not Ops.PARAM: + return + names = capture.expected_names + if (not 0 <= warped.arg.slot < len(names) or names[warped.arg.slot] != "warped" or + not 0 <= mapped.arg.slot < len(names) or names[mapped.arg.slot] != "mapped_gpu_image"): + return + host_view = host.contiguous_view() + if host_view is None or host_view != staging_input.contiguous_view(): + return + scratch, _ = host_view + independent = (*get_call_arg_uops(scalar_copy), *get_call_arg_uops(submit), + *(arg for arg, _, _ in info.inputs)) + if any(arg.storage_base is scratch for arg in independent): + return + if (not isinstance(capture.ret, tuple) or len(capture.ret) != 1 or + not isinstance(capture.ret[0], Tensor) or capture.ret[0].device != "QCOM"): + return + + # Keep the runtime's COPY dispatch and synchronization, the mapped destination, + # and the original guarded policy submission. + direct = upload.replace(src=(upload.body, mapped, warped)) + capture.linear = linear.replace(src=(linear.src[0], direct, linear.src[3])) diff --git a/openpilot/starpilot/models/laboratory_runner.py b/openpilot/starpilot/models/laboratory_runner.py index b501ac46c5..1a62aa5261 100644 --- a/openpilot/starpilot/models/laboratory_runner.py +++ b/openpilot/starpilot/models/laboratory_runner.py @@ -156,7 +156,8 @@ def load_laboratory_pair(cam_w: int, cam_h: int, config: dict, *, root: Path = R loaded.append(load_verified_model(cam_w, cam_h, Path(role['path']), role['version'], role['chestnut'], role['model_id'], role['sha256'], root=root, coherent_warp=coherent)) if coherent is not None: - from openpilot.starpilot.models.laboratory_replay import enable_replay + from openpilot.starpilot.models.laboratory_replay import enable_replay, keep_mapped_image_on_qcom + keep_mapped_image_on_qcom(loaded[-1][0]) enable_replay(loaded[-1][0]) contracts.append(role['source_contract']) pair = LaboratoryPair(loaded[0][0], loaded[1][0], loaded[0][1], loaded[1][1], tuple(contracts)) diff --git a/prebuilt.json b/prebuilt.json index fd519956a3..159398270d 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "0695941fcc63664c91b1ff909fa769cbb6544d6a6f2fb1c1048128c3473a2e98", "schema_version": 1} +{"protected_inventory_sha256": "83cd96e260c627bf69475a8df80912b2e4b60c605b9a83bbb73a720fb60560cc", "schema_version": 1}