Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: reduce serialization cost #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 12 additions & 8 deletions python/src/greenbids/tailor/core/app/routers/root.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from fastapi import APIRouter
import fastapi
from greenbids.tailor.core import fabric
from .. import resources

router = APIRouter(tags=["Main"])
router = fastapi.APIRouter(tags=["Main"])


@router.put("/")
async def get_buyers_probabilities(
fabrics: list[fabric.Fabric],
) -> list[fabric.Fabric]:
fabrics: list[fabric.PredictionInput],
) -> list[fabric.PredictionOutput]:
"""Compute the probability of the buyers to provide a bid.

This must be called for each adcall.
Expand All @@ -18,14 +18,18 @@ async def get_buyers_probabilities(
return resources.get_instance().gb_model.get_buyers_probabilities(fabrics)


@router.post("/")
@router.post(
"/",
response_class=fastapi.Response,
status_code=fastapi.status.HTTP_204_NO_CONTENT,
)
async def report_buyers_status(
fabrics: list[fabric.Fabric],
) -> list[fabric.Fabric]:
fabrics: list[fabric.ReportInput],
):
"""Train model according to actual outcome.

This must NOT be called for each adcall, but only for exploration ones.
All fields of the fabrics need to be set.
Returns the same data than the input.
"""
return resources.get_instance().gb_model.report_buyers_status(fabrics)
resources.get_instance().gb_model.report_buyers_status(fabrics)
46 changes: 25 additions & 21 deletions python/src/greenbids/tailor/core/fabric.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import typing
import pydantic
import pydantic.alias_generators


class _CamelSerialized(pydantic.BaseModel):
model_config = pydantic.ConfigDict(
alias_generator=pydantic.alias_generators.to_camel,
populate_by_name=True,
use_attribute_docstrings=True,
)
_BaseConfig = pydantic.ConfigDict(
alias_generator=pydantic.alias_generators.to_camel,
populate_by_name=True,
use_attribute_docstrings=True,
)
class _BaseTypedDict(typing.TypedDict):
__pydantic_config__ = _BaseConfig # type: ignore


class FeatureMap(_CamelSerialized, pydantic.RootModel):
"""Mapping describing the current opportunity."""
root: dict[str, bool | int | float | bytes | str] = pydantic.Field(
default_factory=dict
)
FeatureMap: typing.TypeAlias = dict[str, typing.Any]


class Prediction(_CamelSerialized):
class Prediction(pydantic.BaseModel):
model_config = _BaseConfig

"""Result of the shaping process."""
score: float = -1
"""Confidence score returned by the model"""
Expand All @@ -33,18 +33,22 @@ def should_send(self) -> bool:
return self.is_exploration or (self.score > self.threshold)


class GroundTruth(_CamelSerialized):
class GroundTruth(_BaseTypedDict):
"""Actual outcome of the opportunity"""
has_response: bool = True
has_response: typing.Annotated[bool, pydantic.Field(default=True)]
"""Did this opportunity lead to a valid buyer response?"""


class Fabric(_CamelSerialized):
"""Main entity used to tailor the traffic.
class ReportInput(_BaseTypedDict):
feature_map: FeatureMap
prediction: Prediction
ground_truth: GroundTruth


class PredictionInput(_BaseTypedDict):
feature_map: FeatureMap

All fields are optional when irrelevant.
"""

feature_map: FeatureMap = pydantic.Field(default_factory=FeatureMap)
prediction: Prediction = pydantic.Field(default_factory=Prediction)
ground_truth: GroundTruth = pydantic.Field(default_factory=GroundTruth)
class PredictionOutput(_BaseTypedDict):
feature_map: FeatureMap
prediction: Prediction
24 changes: 13 additions & 11 deletions python/src/greenbids/tailor/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class Model(ABC):
@abstractmethod
def get_buyers_probabilities(
self,
fabrics: list[fabric.Fabric],
) -> list[fabric.Fabric]:
fabrics: list[fabric.PredictionInput],
) -> list[fabric.PredictionOutput]:
raise NotImplementedError

@abstractmethod
def report_buyers_status(
self,
fabrics: list[fabric.Fabric],
) -> list[fabric.Fabric]:
fabrics: list[fabric.ReportInput],
) -> None:
raise NotImplementedError

def dump(self, fp: typing.BinaryIO) -> None:
Expand All @@ -43,17 +43,19 @@ def __init__(self):

def get_buyers_probabilities(
self,
fabrics: list[fabric.Fabric],
) -> list[fabric.Fabric]:
fabrics: list[fabric.PredictionInput],
) -> list[fabric.PredictionOutput]:
prediction = fabric.Prediction(score=1, is_exploration=(random.random() < 0.2))
return [f.model_copy(update=dict(prediction=prediction)) for f in fabrics]
return [
fabric.PredictionOutput(feature_map=f["feature_map"], prediction=prediction)
for f in fabrics
]

def report_buyers_status(
self,
fabrics: list[fabric.Fabric],
) -> list[fabric.Fabric]:
self._logger.debug([f.feature_map.root for f in fabrics[:1]])
return fabrics
fabrics: list[fabric.ReportInput],
) -> None:
self._logger.debug([f.get("feature_map", {}) for f in fabrics[:1]])


ENTRY_POINTS_GROUP = "greenbids-tailor-models"
Expand Down