-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a scope contextvar to to record metrics during a prediction
We use metrics internally to transport metadata about how the model ran out of the model. It's private because it's entirely invisible within our system and we make no claims of stability for how it works.
- Loading branch information
Showing
8 changed files
with
142 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import warnings | ||
from contextlib import contextmanager | ||
from contextvars import ContextVar | ||
from typing import Callable, Generator, Optional, Union | ||
|
||
from ..types import ExperimentalFeatureWarning | ||
|
||
|
||
class Scope: | ||
def __init__( | ||
self, | ||
*, | ||
record_metric: Callable[[str, Union[float, int]], None], | ||
) -> None: | ||
self.record_metric = record_metric | ||
|
||
|
||
_current_scope: ContextVar[Optional[Scope]] = ContextVar("scope", default=None) | ||
|
||
|
||
def current_scope() -> Scope: | ||
warnings.warn( | ||
"current_scope is an experimental internal function. It may change or be removed without warning.", | ||
category=ExperimentalFeatureWarning, | ||
stacklevel=1, | ||
) | ||
s = _current_scope.get() | ||
if s is None: | ||
raise RuntimeError("No scope available") | ||
return s | ||
|
||
|
||
@contextmanager | ||
def scope(sc: Scope) -> Generator[None, None, None]: | ||
s = _current_scope.set(sc) | ||
try: | ||
yield | ||
finally: | ||
_current_scope.reset(s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from cog import current_scope | ||
|
||
|
||
class Predictor: | ||
def setup(self): | ||
print("did setup") | ||
|
||
def predict(self, *, name: str) -> str: | ||
print(f"hello, {name}") | ||
|
||
current_scope().record_metric("foo", 123) | ||
|
||
return f"hello, {name}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters