Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions tests/compat/test_ultralytics_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 38 additions & 2 deletions tests/test_integrations_ultralytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()]


Expand Down Expand Up @@ -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
6 changes: 4 additions & 2 deletions wildedge/integrations/ultralytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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,
)

Expand Down
Loading