Skip to content

Commit f1f2d2f

Browse files
committed
Support more values for "metrics"
We now support boolean, null, and string values for some metrics (technically, they're not metrics, because you can't sum over them, but they're wired up the same and this is an experimental intenal function).
1 parent 5df702a commit f1f2d2f

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

python/cog/server/eventtypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Log:
3232
@define
3333
class PredictionMetric:
3434
name: str
35-
value: Union[float, int]
35+
value: Optional[Union[bool, float, int, str]]
3636

3737

3838
@define

python/cog/server/runner.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,15 @@ def append_logs(self, logs: str) -> None:
390390
self._p.logs += logs
391391
self._send_webhook(schema.WebhookEvent.LOGS)
392392

393-
def set_metric(self, key: str, value: Union[float, int]) -> None:
393+
def set_metric(self, key: str, value: Optional[Union[bool, float, int, str]]) -> None:
394394
if self._p.metrics is None:
395395
self._p.metrics = {}
396-
self._p.metrics[key] = value
396+
397+
if value is None:
398+
if key in self._p.metrics:
399+
del self._p.metrics[key]
400+
else:
401+
self._p.metrics[key] = value
397402

398403
def succeeded(self) -> None:
399404
self._log.info(("prediction" if not self._is_train else "train") + " succeeded")

python/cog/server/scope.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
@frozen
1212
class Scope:
13-
record_metric: Callable[[str, Union[float, int]], None]
13+
record_metric: Callable[[str, Optional[Union[bool, float, int, str]]], None]
1414
context: Dict[str, str] = {}
1515
_tag: Optional[str] = None
1616

python/cog/server/worker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ def send_cancel_signal(self) -> None:
489489
if self.is_alive() and self.pid:
490490
os.kill(self.pid, signal.SIGUSR1)
491491

492-
def record_metric(self, name: str, value: Union[float, int]) -> None:
492+
def record_metric(self, name: str, value: Optional[Union[bool, float, int, str]]) -> None:
493493
self._events.send(
494494
Envelope(PredictionMetric(name, value), tag=self._current_tag)
495495
)

0 commit comments

Comments
 (0)