From f9c876ca2d8938be565f595c104faa1eab4a8839 Mon Sep 17 00:00:00 2001 From: discountchubbs Date: Fri, 16 Oct 2026 20:50:25 -0700 Subject: [PATCH] upstream sync stuffs --- openpilot/sunnypilot/modeld_v2/modeld.py | 25 +++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/openpilot/sunnypilot/modeld_v2/modeld.py b/openpilot/sunnypilot/modeld_v2/modeld.py index 6e391235e4..dda0f834a4 100755 --- a/openpilot/sunnypilot/modeld_v2/modeld.py +++ b/openpilot/sunnypilot/modeld_v2/modeld.py @@ -7,8 +7,10 @@ See the LICENSE.md file in the root directory for more details. """ from collections.abc import Callable +import gc import os os.environ['GMMU'] = '0' +from tinygrad.device import Device import numpy as np import threading import time @@ -16,7 +18,7 @@ from setproctitle import setproctitle import openpilot.cereal.messaging as messaging from openpilot.common.hardware import COMMA_HARDWARE -from openpilot.selfdrive.modeld.helpers import chestnut_present +from openpilot.selfdrive.modeld.helpers import chestnut_present, wait_for_chestnut from openpilot.cereal import log from opendbc.car.structs import car from openpilot.cereal.services import SERVICE_LIST @@ -26,6 +28,7 @@ from msgq.visionipc import VisionIpcClient, VisionBuf from opendbc.car.car_helpers import get_demo_car_params from openpilot.common.swaglog import cloudlog from openpilot.common.params import Params +from openpilot.common.hardware.usb import cable_connected from openpilot.common.filter_simple import FirstOrderFilter from openpilot.common.realtime import config_realtime_process, DT_MDL from openpilot.common.transformations.camera import DEVICE_CAMERAS @@ -179,6 +182,8 @@ class ModelState(ModelStateBase): self.numpy_inputs['big_tfm'][:, :] = transforms[self._wide_key].reshape(3, 3) raw_outputs = self.adapter.run() + if self.chestnut and (err := Device[self.DEV].error_state.host.view(fmt='q')[0]): + raise RuntimeError(f"chestnut failed with {err}") if after_enqueue is not None: after_enqueue() @@ -253,9 +258,8 @@ def main(demo=False): cloudlog.bind(daemon=PROCESS_NAME) setproctitle(PROCESS_NAME) - config_realtime_process(7, 54) - CHESTNUT = chestnut_present() + CHESTNUT = _find_driving_pkl(get_active_bundle(chestnut=True)) is not None and (chestnut_present() or cable_connected()) if CHESTNUT: from tinygrad.runtime.ops_amd import AMDDevice AMDDevice.wait_timeout_ms = 3000 @@ -264,6 +268,8 @@ def main(demo=False): params.put_bool("ChestnutLoading", CHESTNUT) params.remove("ChestnutActive") + gc.disable() + # visionipc clients while True: available_streams = VisionIpcClient.available_streams("camerad", block=False) @@ -296,6 +302,7 @@ def main(demo=False): def load_big(): nonlocal big_model try: + wait_for_chestnut() m = ModelState(cam_w=vipc_client_main.width, cam_h=vipc_client_main.height, chestnut=True) m.warmup() big_model = m @@ -305,7 +312,13 @@ def main(demo=False): loader.start() loader.join(BIG_MODEL_TIMEOUT) model = big_model - params.put_bool("ChestnutActive", model is not None) + if model is None and not params.get_bool("ChestnutRetry"): + # a crank can cut the chestnut power during the load, retry once with an onroad cycle + cloudlog.error("big model load failed, cycling onroad to retry") + params.put_bool("ChestnutRetry", True) + params.put_bool("OnroadCycleRequested", True) + else: + params.put_bool("ChestnutActive", model is not None) small_model = ModelState(cam_w=vipc_client_main.width, cam_h=vipc_client_main.height, chestnut=False) if model is None or CHESTNUT else None if model is None: @@ -314,6 +327,8 @@ def main(demo=False): assert model is not None cloudlog.warning(f"models loaded in {time.monotonic() - st:.1f}s, modeld starting") + config_realtime_process(7, 54) + # messaging pub_socks = ["modelV2", "drivingModelData", "cameraOdometry", "modelDataV2SP"] + (["chestnutGpuState"] if CHESTNUT else []) pm = PubMaster(pub_socks) @@ -446,7 +461,7 @@ def main(demo=False): run_count % round(model.constants.MODEL_FREQ / SERVICE_LIST['chestnutGpuState'].frequency) == 0) model_output = model.run(bufs, transforms, inputs, chestnut_state.send if send_chestnut else None) except Exception: - if not params.get_bool("ChestnutActive"): + if not model.chestnut: raise cloudlog.exception("big model failed, fall back to small") params.put_bool("ChestnutActive", False)