Skip to content

Commit 05691eb

Browse files
authored
Merge pull request #84 from wild-edge/fix/ultralytics-predict
Track Ultralytics predict() without double counting
2 parents 66a0fb5 + 98e6a7c commit 05691eb

4 files changed

Lines changed: 53 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ under Unreleased and move into a version section at release time.
77

88
### Fixed
99

10+
- Ultralytics `model.predict()` now records inference telemetry. Instrumentation
11+
patches `predict`, which `model(...)` delegates to, so both entry points are
12+
tracked exactly once.
13+
1014
- Streaming wrappers now finalize once across exhaustion, explicit closure,
1115
cancellation, and iterator errors. Early closure/cancellation records an
1216
unsuccessful inference with partial metadata when available. TTFT ignores

‎tests/compat/test_ultralytics_compat.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ def test_yolo_call_is_tracked(compat_client, yolo, image):
3535
assert inference["inference"]["success"] is True
3636

3737

38-
@pytest.mark.xfail(
39-
strict=True,
40-
reason="model.predict() is not tracked, only model() (#72)",
41-
)
4238
def test_yolo_predict_is_tracked(compat_client, yolo, image):
4339
yolo.predict(image, imgsz=64, verbose=False)
4440
compat_client.one("inference")
41+
42+
43+
def test_predict_and_call_each_track_once(compat_client, yolo, image):
44+
yolo.predict(image, imgsz=64, verbose=False)
45+
assert len(compat_client.of_type("inference")) == 1
46+
yolo(image, imgsz=64, verbose=False)
47+
assert len(compat_client.of_type("inference")) == 2

‎tests/test_integrations_ultralytics.py‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,21 @@ class YOLO:
8484
model = FakeInnerModel()
8585

8686
def __call__(self, source=None, stream=False, **kwargs):
87+
return self.predict(source, stream=stream, **kwargs)
88+
89+
def predict(self, source=None, stream=False, **kwargs):
8790
return [FakeResult([(10, 20, 100, 200, 0.9, 0)])]
8891

8992

9093
class FailingYOLO(YOLO):
91-
def __call__(self, source=None, stream=False, **kwargs):
94+
def predict(self, source=None, stream=False, **kwargs):
9295
raise RuntimeError("cuda oom")
9396

9497

9598
class ClassifyYOLO(YOLO):
9699
task = "classify"
97100

98-
def __call__(self, source=None, stream=False, **kwargs):
101+
def predict(self, source=None, stream=False, **kwargs):
99102
return [FakeClassifyResult()]
100103

101104

@@ -464,3 +467,36 @@ def test_unsupported_ultralytics_version_warns_and_does_not_patch(monkeypatch, c
464467
assert fake_yolo.__init__ is original_init
465468
messages = [r.getMessage() for r in caplog.records]
466469
assert any("ultralytics 8.0.0 is older than 8.0.200" in m for m in messages)
470+
471+
472+
@pytest.mark.parametrize("entrypoint", ["predict", "__call__"])
473+
def test_predict_and_call_track_once_with_repeated_registration(
474+
publish_spy, entrypoint
475+
):
476+
model = YOLO()
477+
handle = make_handle(publish_spy)
478+
_extractor.install_hooks(model, handle)
479+
_extractor.install_hooks(model, handle)
480+
results = getattr(model, entrypoint)(source="image.png", verbose=False)
481+
assert len(results) == 1
482+
assert len(publish_spy.events) == 1
483+
assert publish_spy.events[0]["event_type"] == "inference"
484+
assert publish_spy.events[0]["inference"]["output_meta"]["num_predictions"] == 1
485+
486+
487+
def test_predict_error_is_preserved_and_tracked_once(publish_spy):
488+
model = FailingYOLO()
489+
_extractor.install_hooks(model, make_handle(publish_spy))
490+
with pytest.raises(RuntimeError, match="cuda oom"):
491+
model.predict(source="image.png")
492+
assert len(publish_spy.events) == 1
493+
assert publish_spy.events[0]["event_type"] == "error"
494+
495+
496+
def test_predict_does_not_instrument_unregistered_instances(publish_spy):
497+
tracked, untouched = YOLO(), YOLO()
498+
_extractor.install_hooks(tracked, make_handle(publish_spy))
499+
untouched.predict()
500+
assert not publish_spy.events
501+
tracked.predict()
502+
assert len(publish_spy.events) == 1

‎wildedge/integrations/ultralytics.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
_ULTRALYTICS_PATCH_LOCK = threading.Lock()
6464

6565
# --- Marker names written onto patched objects and classes ---
66-
YOLO_CALL_PATCH_NAME = "ultralytics_call"
66+
YOLO_PREDICT_PATCH_NAME = "ultralytics_predict"
6767
YOLO_HANDLE_ATTR = "__wildedge_yolo_handle__"
6868
YOLO_AUTO_LOAD_PATCH_NAME = "ultralytics_auto_load"
6969

@@ -391,9 +391,11 @@ def memory_bytes(self, obj: object) -> int | None:
391391
def install_hooks(self, obj: object, handle: ModelHandle) -> None:
392392
handle.detected_accelerator = detect_accelerator(obj)
393393
setattr(obj, YOLO_HANDLE_ATTR, handle)
394+
# YOLO.__call__ delegates to predict; patch their common entry point.
394395
patch_instance_call_once(
395396
obj,
396-
patch_name=YOLO_CALL_PATCH_NAME,
397+
patch_name=YOLO_PREDICT_PATCH_NAME,
398+
method="predict",
397399
make_patched_call=build_patched_call,
398400
)
399401

0 commit comments

Comments
 (0)