From 98e6a7c23a466cf51016e02dd0fcb2168e8fe043 Mon Sep 17 00:00:00 2001 From: Piotr Duda Date: Tue, 20 Oct 2026 18:45:18 +0200 Subject: [PATCH] Track Ultralytics predict through the common entry point --- CHANGELOG.md | 4 +++ tests/compat/test_ultralytics_compat.py | 11 ++++--- tests/test_integrations_ultralytics.py | 40 +++++++++++++++++++++++-- wildedge/integrations/ultralytics.py | 6 ++-- 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23f8ec5..5461a70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ under Unreleased and move into a version section at release time. ### Fixed +- Ultralytics `model.predict()` now records inference telemetry. Instrumentation + patches `predict`, which `model(...)` delegates to, so both entry points are + tracked exactly once. + - Streaming wrappers now finalize once across exhaustion, explicit closure, cancellation, and iterator errors. Early closure/cancellation records an unsuccessful inference with partial metadata when available. TTFT ignores diff --git a/tests/compat/test_ultralytics_compat.py b/tests/compat/test_ultralytics_compat.py index f22267f..818aada 100644 --- a/tests/compat/test_ultralytics_compat.py +++ b/tests/compat/test_ultralytics_compat.py @@ -35,10 +35,13 @@ def test_yolo_call_is_tracked(compat_client, yolo, image): assert inference["inference"]["success"] is True -@pytest.mark.xfail( - strict=True, - reason="model.predict() is not tracked, only model() (#72)", -) def test_yolo_predict_is_tracked(compat_client, yolo, image): yolo.predict(image, imgsz=64, verbose=False) compat_client.one("inference") + + +def test_predict_and_call_each_track_once(compat_client, yolo, image): + yolo.predict(image, imgsz=64, verbose=False) + assert len(compat_client.of_type("inference")) == 1 + yolo(image, imgsz=64, verbose=False) + assert len(compat_client.of_type("inference")) == 2 diff --git a/tests/test_integrations_ultralytics.py b/tests/test_integrations_ultralytics.py index dc49c2d..885c430 100644 --- a/tests/test_integrations_ultralytics.py +++ b/tests/test_integrations_ultralytics.py @@ -84,18 +84,21 @@ class YOLO: model = FakeInnerModel() def __call__(self, source=None, stream=False, **kwargs): + return self.predict(source, stream=stream, **kwargs) + + def predict(self, source=None, stream=False, **kwargs): return [FakeResult([(10, 20, 100, 200, 0.9, 0)])] class FailingYOLO(YOLO): - def __call__(self, source=None, stream=False, **kwargs): + def predict(self, source=None, stream=False, **kwargs): raise RuntimeError("cuda oom") class ClassifyYOLO(YOLO): task = "classify" - def __call__(self, source=None, stream=False, **kwargs): + def predict(self, source=None, stream=False, **kwargs): return [FakeClassifyResult()] @@ -464,3 +467,36 @@ def test_unsupported_ultralytics_version_warns_and_does_not_patch(monkeypatch, c assert fake_yolo.__init__ is original_init messages = [r.getMessage() for r in caplog.records] assert any("ultralytics 8.0.0 is older than 8.0.200" in m for m in messages) + + +@pytest.mark.parametrize("entrypoint", ["predict", "__call__"]) +def test_predict_and_call_track_once_with_repeated_registration( + publish_spy, entrypoint +): + model = YOLO() + handle = make_handle(publish_spy) + _extractor.install_hooks(model, handle) + _extractor.install_hooks(model, handle) + results = getattr(model, entrypoint)(source="image.png", verbose=False) + assert len(results) == 1 + assert len(publish_spy.events) == 1 + assert publish_spy.events[0]["event_type"] == "inference" + assert publish_spy.events[0]["inference"]["output_meta"]["num_predictions"] == 1 + + +def test_predict_error_is_preserved_and_tracked_once(publish_spy): + model = FailingYOLO() + _extractor.install_hooks(model, make_handle(publish_spy)) + with pytest.raises(RuntimeError, match="cuda oom"): + model.predict(source="image.png") + assert len(publish_spy.events) == 1 + assert publish_spy.events[0]["event_type"] == "error" + + +def test_predict_does_not_instrument_unregistered_instances(publish_spy): + tracked, untouched = YOLO(), YOLO() + _extractor.install_hooks(tracked, make_handle(publish_spy)) + untouched.predict() + assert not publish_spy.events + tracked.predict() + assert len(publish_spy.events) == 1 diff --git a/wildedge/integrations/ultralytics.py b/wildedge/integrations/ultralytics.py index 4a822d3..f929672 100644 --- a/wildedge/integrations/ultralytics.py +++ b/wildedge/integrations/ultralytics.py @@ -63,7 +63,7 @@ _ULTRALYTICS_PATCH_LOCK = threading.Lock() # --- Marker names written onto patched objects and classes --- -YOLO_CALL_PATCH_NAME = "ultralytics_call" +YOLO_PREDICT_PATCH_NAME = "ultralytics_predict" YOLO_HANDLE_ATTR = "__wildedge_yolo_handle__" YOLO_AUTO_LOAD_PATCH_NAME = "ultralytics_auto_load" @@ -391,9 +391,11 @@ def memory_bytes(self, obj: object) -> int | None: def install_hooks(self, obj: object, handle: ModelHandle) -> None: handle.detected_accelerator = detect_accelerator(obj) setattr(obj, YOLO_HANDLE_ATTR, handle) + # YOLO.__call__ delegates to predict; patch their common entry point. patch_instance_call_once( obj, - patch_name=YOLO_CALL_PATCH_NAME, + patch_name=YOLO_PREDICT_PATCH_NAME, + method="predict", make_patched_call=build_patched_call, )